Beispiel #1
0
/**
 * Translates a binary buffer into a BASE64 encoded string using the specified
 * character to value mapping. The returned string is guaranteed to be a 7-bit
 * ASCII string. If buffer is empty, returns an empty string. Only returns NULL
 * if memory allocation fails. The caller will have to deallocate the returned
 * string with MEM_Free
 */
Char * BASE64_Encode(const void * data, size_t n, int flags)
{
    const char* encodeMap = ((flags & BASE64_URLSAFE) ? 
        base64_safeEncodeMap : 
        base64_encodeMap);

    size_t alloc = ((n+2)/3)*4 + 1;
    Char * dest = MEM_NewArray(Char, alloc);
    if (dest) {
        const char * buf = (const char *)data;
        Char * p = dest;
        size_t off;

        /* encode the bulk of the data */
        for (off=0; (off+3)<=n; off+=3) {
            *p++ = encodeMap[BASE64_ENCODE_1(buf,off)];
            *p++ = encodeMap[BASE64_ENCODE_2(buf,off)];
            *p++ = encodeMap[BASE64_ENCODE_3(buf,off)];
            *p++ = encodeMap[BASE64_ENCODE_4(buf,off)];
        }

        /* manage the last few bytes */
        switch (n%3) {
        case 0:
            break;
        case 1:
            *p++ = encodeMap[BASE64_ENCODE_1(buf,off)];
            *p++ = encodeMap[BASE64_ENCODE_2_1(buf,off)];
            if (flags & BASE64_PAD) {
                *p++ = '=';
                *p++ = '=';
            }
            break;
        case 2:
            *p++ = encodeMap[BASE64_ENCODE_1(buf,off)];
            *p++ = encodeMap[BASE64_ENCODE_2(buf,off)];
            *p++ = encodeMap[BASE64_ENCODE_3_1(buf,off)];
            if (flags & BASE64_PAD) *p++ = '=';
            break;
        }

        /* null terminate the destination string */
        *p = 0;
        ASSERT(StrLen(dest) < (size_t)alloc);
    }
    return dest;
}
Beispiel #2
0
/**
 * The same as BASE64_Encode but stores encoded data into a string buffer.
 * Does not destroy the original contents of the string buffer. The
 * Base64 string is appended to it. Returns pointer to string buffer
 * data, NULL on memory allocation failure
 */
Str BASE64_EncodeStr(const void * data, size_t size, StrBuf * sb, int flags)
{
    const char* encodeMap = ((flags & BASE64_URLSAFE) ? 
        base64_safeEncodeMap : 
        base64_encodeMap);

    size_t alloc = ((size+2)/3)*4;
    if (STRBUF_Alloc(sb, STRBUF_Length(sb)+alloc)) {
        const char * buf = (const char *)data;
        size_t off;

        /* encode the bulk of the data */
        for (off=0; (off+3)<=size; off+=3) {
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_1(buf,off)]);
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_2(buf,off)]);
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_3(buf,off)]);
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_4(buf,off)]);
        }

        /* manage last one or two bytes */
        switch (size%3) {
        case 0:
            break;
        case 1:
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_1(buf,off)]);
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_2_1(buf,off)]);
            if (flags & BASE64_PAD) {
                STRBUF_AppendChar(sb,'=');
                STRBUF_AppendChar(sb,'=');
            }
            break;
        case 2:
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_1(buf,off)]);
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_2(buf,off)]);
            STRBUF_AppendChar(sb,encodeMap[BASE64_ENCODE_3_1(buf,off)]);
            if (flags & BASE64_PAD) STRBUF_AppendChar(sb,'=');
            break;
        }
        
        return STRBUF_Text(sb);
    }
    
    /* could not allocate memory */
    return NULL;    
}
Beispiel #3
0
/*****************************************************************************
 * FUNCTION
 *  applib_base64_part_encode_append
 * DESCRIPTION
 *  This function encode next part data to BASE64.
 * PARAMETERS
 *  cntx        [IN]            Part process context
 *  src         [IN]            Source data
 *  srcl        [IN/OUT]        Source data length
 *  dest        [IN/OUT]        Encoded data
 *  destl       [IN]            Destination buffer length
 * RETURNS
 *  negative  : parameter error
 *  otherwise : length of encoded bytes
 *****************************************************************************/
kal_int32 applib_base64_part_encode_append(
            applib_base64_part_context *cntx,
            const kal_char *src,
            kal_int32 *srcl,
            kal_char  *dst,
            kal_int32 dstl)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 i_input,i_output;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (cntx == NULL || cntx->buf_cnt > 2 ||
        srcl == NULL || (*srcl > 0 && src == NULL))
    {
        BASE64_ASSERT(0);
        return -1;
    }

    /* encode it */
    i_input = i_output = 0;
    while(i_input < *srcl)
    {
        BASE64_ASSERT(cntx->buf_cnt <= 2);

        cntx->buf[cntx->buf_cnt++] = src[i_input++];
        if (cntx->buf_cnt < 3)
        {
            continue;
        }

        /* cntx->buf_cnt == 3 */
        if (i_output + 4 > dstl)
        {
            /* roll back */
            i_input--;
            cntx->buf_cnt--;
            break;
        }

        /* Encode the three-bytes to four bytes */
        BASE64_ENCODE_3((dst+i_output),cntx->buf);
        i_output += 4;
        cntx->buf_cnt = 0;
        
        /* line break */
        if (cntx->wrap_max == 0)
        {
            /* No need to break line */
            continue;
        }

        cntx->wrap_count += 4;
        if (cntx->wrap_count < cntx->wrap_max)
        {
            /* Not reach the wrap count */
            continue;
        }

        /* Check if output buffer is enough */
        if (i_output + 2 > dstl)
        {
            /* roll back one bytes */
            i_input--;
            cntx->buf_cnt = 2;
            i_output -= 4;
            cntx->wrap_count -= 4;
            break;
        }

        if (dst == NULL)
        {
            BASE64_ASSERT(0);
            return -1;
        }
        dst[i_output++] = '\r';
        dst[i_output++] = '\n';
        cntx->wrap_count = 0;
    }

    if (*srcl != i_input)
    {
        /* Waring */
        BASE64_WARING();
    }

    BASE64_ASSERT(cntx->buf_cnt <= 2);

    *srcl = i_input;
    return i_output;
}