/* 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 #include #include "xds.h" #define bits(c) (0x80 | ((c) & 0x3F)) #define put(c) *strptr++ = (c); #define putbits(c) put(bits(c)) #define finish() *strptr = '\0' static char* sputu8(xds_uint32_t c, char* strptr) { if (strptr == NULL) return NULL; if (c < 0x80) { put(c); finish(); } else if (c < 0x800) { put(0xC0 | (c >> 6)); putbits(c); finish(); } else if (c < 0x10000) { put(0xE0 | (c >> 12)); putbits(c >> 6); putbits(c); finish(); } else if (c < 0x200000) { put(0xF0 | (c >> 18)); putbits(c >> 12); putbits(c >> 6); putbits(c); finish(); } else if (c < 0x400000) { put(0xF8 | (c >> 24)); putbits(c >> 18); putbits(c >> 12); putbits(c >> 6); putbits(c); finish(); } else if (c < 0x80000000) { put(0xFC | (c >> 30)); putbits(c >> 24); putbits(c >> 18); putbits(c >> 12); putbits(c >> 6); putbits(c); finish(); } else finish(); /* Not a valid Unicode "character" */ return strptr; } static const char TAG_OPEN[] = ""; static const char TAG_CLOSE[] = ""; static const size_t TAG_OPEN_LEN = sizeof(TAG_OPEN)-1; static const size_t TAG_CLOSE_LEN = sizeof(TAG_CLOSE)-1; int xml_encode_string(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, size_t* used_buffer_size, va_list* args) { char* src; size_t src_len; char* dst; size_t dst_size; char* tmp; /* 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); /* Get the data from the stack. */ src = va_arg(*args, char*); xds_check_parameter(src != NULL); src_len = strlen(src); /* Set up the target buffer. */ dst = buffer; dst_size = buffer_size; /* Write the opening tag. */ memmove(dst, TAG_OPEN, TAG_OPEN_LEN); dst += TAG_OPEN_LEN; dst_size -= TAG_OPEN_LEN; /* Format the data into the buffer. */ while(src_len > 0 && dst_size > 0) { if (*((xds_uint8_t*)src) >= 0x80) { /* UTF-8ify it. */ if (dst_size >= 7) { tmp = sputu8((xds_uint32_t)*((xds_uint8_t*)src), dst); ++src; --src_len; dst_size -= tmp - dst; dst = tmp; } else dst_size = 0; } else if (*src == '<') { /* Turn into "<". */ if (dst_size >= 4) { *dst++ = '&'; --dst_size; *dst++ = 'l'; --dst_size; *dst++ = 't'; --dst_size; *dst++ = ';'; --dst_size; ++src; --src_len; } else dst_size = 0; } else if (*src == '&') { /* Turn into "&". */ if (dst_size >= 5) { *dst++ = '&'; --dst_size; *dst++ = 'a'; --dst_size; *dst++ = 'm'; --dst_size; *dst++ = 'p'; --dst_size; *dst++ = ';'; --dst_size; ++src; --src_len; } else dst_size = 0; } else if (*src == '>') { /* Turn into ">". */ if (dst_size >= 4) { *dst++ = '&'; --dst_size; *dst++ = 'g'; --dst_size; *dst++ = 't'; --dst_size; *dst++ = ';'; --dst_size; ++src; --src_len; } else dst_size = 0; } else { /* No special character; just copy it. */ *dst++ = *src++; --src_len; --dst_size; } } if (src_len > 0) { /* Target buffer was too small. */ *used_buffer_size = buffer_size + 1; return XDS_ERR_OVERFLOW; } /* Write the closing tag. */ memmove(dst, TAG_CLOSE, TAG_CLOSE_LEN); dst += TAG_CLOSE_LEN; dst_size -= TAG_CLOSE_LEN; /* Done. */ *used_buffer_size = dst - (char*)buffer; return XDS_OK; }