head 1.3; access; symbols last-version-in-peti-style:1.2; locks; strict; comment @ * @; 1.3 date 2001.08.08.19.15.23; author rse; state dead; branches; next 1.2; 1.2 date 2001.08.08.09.50.27; author simons; state Exp; branches; next 1.1; 1.1 date 2001.08.02.17.24.50; author simons; state Exp; branches; next ; desc @@ 1.3 log @First cut of the ruthless style adjustments to OSSP XDS: o adjust source tree to follow OSSP source tree style by heavily combining sources into smaller sets (needs more work when still missing parts are added later) o automatic re-indentation of sources with GNU indent (still needs manual review and adjustments; will follow) These two heavy steps were mostly done automatically with the help of two helper scripts written in Perl. So expect more manual adjustments to follow... @ text @/* 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" 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, "", 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, "", 14); /* Done. */ return XDS_OK; } @ 1.2 log @Fixed base64_(en|de)code()'s problem with buffer's of input size zero. @ text @@ 1.1 log @Implemented XML engines for octet streams. The base64-encoding code still needs some work, though: The test with an empty string currently fails. Also we had to do some weird work-arounds in the decoding routine because base64_decode does not honor srclen. @ text @d44 1 a44 1 return -1; @