OSSP CVS Repository

ossp - ossp-pkg/xds/xml-encode-octetstream.c 1.2
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/xds/xml-encode-octetstream.c 1.2
/*
   XDS - OSSP Extensible Data Serialization Library
   Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
   Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)

   This file is part of OSSP XDS, an extensible data serialization
   library which can be found at http://www.ossp.com/pkg/xds/.

   Permission to use, copy, modify, and distribute this software for
   any purpose with or without fee is hereby granted, provided that
   the above copyright notice and this permission notice appear in all
   copies.

   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   SUCH DAMAGE.
*/

#include <string.h>
#include <ctype.h>
#include "xds.h"

const char xds_base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char xds_pad64    = '=';

static int base64_encode(char *dst, size_t dstlen, unsigned char const *src, size_t srclen)
    {
    size_t dstpos;
    unsigned char input[3];
    unsigned char output[4];
    int ocnt;
    size_t i;

    if (srclen == 0)
        return 0;
    if (dst == NULL)
	{
        /* just calculate required length of dst */
        dstlen = (((srclen + 2) / 3) * 4);
        return dstlen;
	}

    /* bulk encoding */
    dstpos = 0;
    ocnt = 0;
    while (srclen >= 3)
	{
        input[0] = *src++;
        input[1] = *src++;
        input[2] = *src++;
        srclen -= 3;

        output[0] = (input[0] >> 2);
        output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
        output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
        output[3] = (input[2] & 0x3f);

        if (dstpos + 4 > dstlen)
            return -1;
        dst[dstpos++] = xds_base64[output[0]];
        dst[dstpos++] = xds_base64[output[1]];
        dst[dstpos++] = xds_base64[output[2]];
        dst[dstpos++] = xds_base64[output[3]];
	}

    /* now worry about padding with remaining 1 or 2 bytes */
    if (srclen != 0)
	{
        input[0] = input[1] = input[2] = '\0';
        for (i = 0; i < srclen; i++)
            input[i] = *src++;

        output[0] = (input[0] >> 2);
        output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
        output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);

        if (dstpos + 4 > dstlen)
            return -1;
        dst[dstpos++] = xds_base64[output[0]];
        dst[dstpos++] = xds_base64[output[1]];
        if (srclen == 1)
            dst[dstpos++] = xds_pad64;
        else
            dst[dstpos++] = xds_base64[output[2]];
        dst[dstpos++] = xds_pad64;
	}

    if (dstpos >= dstlen)
        return -1;
    dst[dstpos] = '\0';

    return dstpos;
    }

int xml_encode_octetstream(xds_t* xds, void* engine_context,
			   void* buffer, size_t buffer_size, size_t* used_buffer_size,
			   va_list* args)
    {
    xds_uint8_t* src;
    size_t       src_len;

    /* We need at least 27 byte for the starting and ending tag. */

    xds_init_encoding_engine(13 + 14);

    /* Get parameters from stack. */

    src     = (xds_uint8_t*)va_arg(*args, void*);
    xds_check_parameter(src != NULL);
    src_len = va_arg(*args, size_t);

    /* Calculate how many bytes we'll need in buffer and make sure we
       have them. */

    *used_buffer_size = base64_encode(NULL, 0, src, src_len);
    if (*used_buffer_size == (size_t)-1)
	return XDS_ERR_UNKNOWN;
    else
	*used_buffer_size += 13 + 14;
    if (buffer_size < *used_buffer_size)
	return XDS_ERR_OVERFLOW;

    /* Format the data into the buffer. */

    memmove(buffer, "<octetstream>", 13);
    if (base64_encode((char*)buffer + 13, buffer_size - 13, src, src_len) < 0)
	return XDS_ERR_UNKNOWN;
    memmove((char*)buffer + *used_buffer_size - 14, "</octetstream>", 14);

    /* Done. */

    return XDS_OK;
    }

CVSTrac 2.0.1