Ejemplo n.º 1
0
// Reads MessagePack bin bytes and outputs a JSON base64 string
static bool base64_bin(mpack_reader_t* reader, yajl_gen gen, options_t* options, uint32_t len, bool prefix) {
    uint32_t new_len = base64_len(len) + (prefix ? strlen(b64_str) : 0);
    char* output = (char*)malloc(new_len);

    bool ret = base64(reader, gen, options, len, output, output, prefix);

    mpack_done_bin(reader);
    free(output);
    return ret;
}
Ejemplo n.º 2
0
/** Decode a base64-encoded string
 * @param[in] data The base64-encoded string
 * @param[in] len Length of the base64-encoded string
 * @param[out] obuf If obuf is not set to NULL, store the decoded data in obuf. Otherwise, the decoded data is stored in a dynamically-allocated buffer.
 * @param[out] olen The length of the decoded data
 * @return The base64-decoded data
 */
void *cl_base64_decode(char *data, size_t len, void *obuf, size_t *olen, int oneline)
{
    BIO *bio, *b64;
    void *buf;

    buf = (obuf) ? obuf : malloc(base64_len(data, len)+1);
    if (!(buf))
        return NULL;

    b64 = BIO_new(BIO_f_base64());
    if (!(b64)) {
        if (!(obuf))
            free(buf);

        return NULL;
    }

    bio = BIO_new_mem_buf(data, len);
    if (!(bio)) {
        BIO_free(b64);
        if (!(obuf))
            free(buf);

        return NULL;
    }

    bio = BIO_push(b64, bio);
    if (oneline)
        BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);

    *olen = BIO_read(bio, buf, base64_len(data, len));

    BIO_free_all(bio);

    return buf;
}
Ejemplo n.º 3
0
// Reads MessagePack ext bytes and outputs a JSON base64 string
static bool base64_ext(mpack_reader_t* reader, yajl_gen gen, options_t* options, int8_t exttype, uint32_t len) {
    uint32_t new_len = base64_len(len) + strlen(ext_str) + 5 + strlen(b64_str);
    char* output = (char*)malloc(new_len);

    char* p = output;
    strcpy(p, ext_str);
    p += strlen(ext_str);
    sprintf(p, "%i", exttype);
    p += strlen(p);
    *p++ = ':';

    bool ret = base64(reader, gen, options, len, output, p, true);

    mpack_done_ext(reader);
    free(output);
    return ret;
}