OSSP CVS Repository

ossp - ossp-pkg/xds/xml-decode-string.c 1.3
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/xds/xml-decode-string.c 1.3
/*
   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 <stdio.h>
#include <string.h>
#include "xds.h"

#define	INVALID	0x80000000

#define	get(c)	c = *strptr++; \
	if (chars) (*chars)++; \
	if ((c) == 0) return (unsigned int)EOF

static unsigned int sgetu8(unsigned char *strptr, int *chars)
    {
    unsigned int c;
    int i, iterations;
    unsigned char ch;

    if (chars)
	*chars = 0;

    if (strptr == NULL)
	return (unsigned int)EOF;

    get(c);

    if ((c & 0xFE) == 0xFC)
	{
	c &= 0x01;
	iterations = 5;
	}
    else if ((c & 0xFC) == 0xF8)
	{
	c &= 0x03;
	iterations = 4;
	}
    else if ((c & 0xF8) == 0xF0)
	{
	c &= 0x07;
	iterations = 3;
	}
    else if ((c & 0xF0) == 0xE0)
	{
	c &= 0x0F;
	iterations = 2;
	}
    else if ((c & 0xE0) == 0xC0)
	{
	c &= 0x1F;
	iterations = 1;
	}
    else if ((c & 0x80) == 0x80)
	return INVALID;
    else return c;

    for (i = 0; i < iterations; i++)
	{
	get(ch);
	if ((ch & 0xC0) != 0x80)
	    return INVALID;
	c <<= 6;
	c |= ch & 0x3F;
	}

    return c;
    }

static const char TAG_OPEN[] = "<string>";
static const char TAG_CLOSE[] = "</string>";
static const size_t TAG_OPEN_LEN = sizeof(TAG_OPEN)-1;
static const size_t TAG_CLOSE_LEN = sizeof(TAG_CLOSE)-1;

int xml_decode_string(xds_t* xds, void* engine_context,
		      void* buffer, size_t buffer_size, size_t* used_buffer_size,
		      va_list* args)
    {
    char**  target_buffer;
    char*   src;
    size_t  src_len;
    char*   dst;
    int     utf8_len;
    unsigned int rc;

    /* Setup the engine. We need at least space for our tags; how long
       the actual content is going to be will be seen soon. */

    xds_init_encoding_engine(TAG_OPEN_LEN + TAG_CLOSE_LEN);

    /* Is the opening tag there? */

    if (strncasecmp(buffer, TAG_OPEN, TAG_OPEN_LEN) != 0)
        return XDS_ERR_TYPE_MISMATCH;

    /* Determine the length of the encoded data. */

    src = (char*)buffer + TAG_OPEN_LEN;
    for (src_len = 0; src[src_len] != '<'; ++src_len)
	if (src[src_len] == '\0')
	    return XDS_ERR_TYPE_MISMATCH;

    /* Check the closing tag. */

    if (strncasecmp(src + src_len, TAG_CLOSE, TAG_CLOSE_LEN) != 0)
        return XDS_ERR_TYPE_MISMATCH;
    *used_buffer_size = TAG_OPEN_LEN + src_len + TAG_CLOSE_LEN;

    /* Allocate target buffer. */

    target_buffer = va_arg(*args, char**);
    xds_check_parameter(target_buffer != NULL);
    *target_buffer = dst = malloc(src_len + 1);
    if (dst == NULL)
	return XDS_ERR_NO_MEM;

    /* Decode the data into the target buffer. */

    while(src_len > 0)
	{
	if (*src == '&')
	    {
	    if (src_len >= 4 && strncmp(src, "&lt;", 4) == 0)
		{
		*dst++ = '<';
		src += 4; src_len -= 4;
		}
	    else if (src_len >= 4 && strncmp(src, "&gt;", 4) == 0)
		{
		*dst++ = '>';
		src += 4; src_len -= 4;
		}
	    else if (src_len >= 5 && strncmp(src, "&amp;", 5) == 0)
		{
		*dst++ = '&';
		src += 5; src_len -= 5;
		}
	    else
		{
		free(dst);
		return XDS_ERR_TYPE_MISMATCH;
		}
	    }
	else if (*((xds_uint8_t*)src) >= 0x80)
	    {
	    rc = sgetu8((xds_uint8_t*)src, &utf8_len);
	    if (rc == (unsigned int)EOF)
		return XDS_ERR_UNDERFLOW;
	    else if (rc == INVALID || rc > 255)
		return XDS_ERR_TYPE_MISMATCH;
	    *dst++ = (xds_uint8_t)rc;
	    src += utf8_len; src_len -= utf8_len;
	    }
	else
	    {
	    *dst++ = *src++;
	    --src_len;
	    }
	}
    *dst = '\0';

    /* Done. */

    return XDS_OK;
    }

CVSTrac 2.0.1