head 1.15; access; symbols last-version-in-peti-style:1.14 callback-semantic-rewrite:1.12.0.2; locks; strict; comment @ * @; 1.15 date 2001.08.08.19.15.23; author rse; state dead; branches; next 1.14; 1.14 date 2001.08.08.11.50.16; author simons; state Exp; branches; next 1.13; 1.13 date 2001.08.01.15.25.47; author simons; state Exp; branches; next 1.12; 1.12 date 2001.07.31.06.33.55; author simons; state Exp; branches 1.12.2.1; next 1.11; 1.11 date 2001.07.24.15.46.18; author simons; state Exp; branches; next 1.10; 1.10 date 2001.07.19.15.21.35; author simons; state Exp; branches; next 1.9; 1.9 date 2001.07.18.17.49.51; author simons; state Exp; branches; next 1.8; 1.8 date 2001.07.18.17.38.37; author simons; state Exp; branches; next 1.7; 1.7 date 2001.07.16.18.31.36; author simons; state Exp; branches; next 1.6; 1.6 date 2001.07.16.18.28.32; author simons; state Exp; branches; next 1.5; 1.5 date 2001.07.16.17.50.07; author simons; state Exp; branches; next 1.4; 1.4 date 2001.07.09.17.44.29; author simons; state Exp; branches; next 1.3; 1.3 date 2001.07.09.17.36.33; author simons; state Exp; branches; next 1.2; 1.2 date 2001.07.09.17.19.58; author simons; state Exp; branches; next 1.1; 1.1 date 2001.07.04.15.58.51; author simons; state Exp; branches; next ; 1.12.2.1 date 2001.08.01.09.55.58; author simons; state Exp; branches; next 1.12.2.2; 1.12.2.2 date 2001.08.01.11.26.16; author simons; state Exp; branches; next 1.12.2.3; 1.12.2.3 date 2001.08.01.12.01.32; author simons; state Exp; branches; next 1.12.2.4; 1.12.2.4 date 2001.08.01.13.26.29; author simons; state Exp; branches; next 1.12.2.5; 1.12.2.5 date 2001.08.01.14.37.32; author simons; state Exp; branches; next ; desc @@ 1.15 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 "internal.h" int xds_vencode(xds_t* xds, const char* fmt_arg, va_list args) { va_list args_backup; size_t buffer_len_backup; char* name; char* p; char* fmt; int rc; /* Sanity checks. */ xds_check_parameter(xds != NULL); xds_check_parameter(fmt_arg != NULL); assert(xds->mode == XDS_ENCODE); if (xds->mode != XDS_ENCODE) return XDS_ERR_INVALID_MODE; /* Ensure we have a buffer allocated ready for use. */ if (xds->buffer == NULL) { /* allocate a new buffer */ rc = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, XDS_INITIAL_BUFFER_CAPACITY, sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM); if (rc != XDS_OK) return rc; xds->buffer_len = 0; xds->we_own_buffer = XDS_TRUE; } /* Iterate through the items in the format string and execute the apropriate engines. */ fmt = p = strdup(fmt_arg); if (fmt == NULL) return XDS_ERR_NO_MEM; buffer_len_backup = xds->buffer_len; for(name = p; *p != '\0'; name = p) { while(isalnum((int)*p) || *p == '-' || *p == '_') ++p; if (*p) *p++ = '\0'; else *p = '\0'; if (strlen(name) > 0) { int restart_engine; size_t used_buffer_size; size_t pos; if (xds_find_engine(xds->engines, xds->engines_len, name, &pos) == XDS_FALSE) { rc = XDS_ERR_UNKNOWN_ENGINE; goto leave; } do { /* Ensure the buffer has free space. */ assert(xds->buffer_len <= xds->buffer_capacity); if (xds->buffer_len == xds->buffer_capacity) { if (xds->we_own_buffer) { rc = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, xds->buffer_len + 1, sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM); if (rc != XDS_OK) goto leave; } else { rc = XDS_ERR_OVERFLOW; goto leave; } } /* Execute the engine. */ used_buffer_size = 0; args_backup = args; rc = (*xds->engines[pos].engine)(xds, xds->engines[pos].context, xds->buffer + xds->buffer_len, xds->buffer_capacity - xds->buffer_len, &used_buffer_size, &args); assert(rc <= 0); if (rc == XDS_OK) { restart_engine = XDS_FALSE; xds->buffer_len += used_buffer_size; } else if (rc == XDS_ERR_OVERFLOW) { /* enlarge buffer */ if (!xds->we_own_buffer) goto leave; restart_engine = XDS_TRUE; args = args_backup; rc = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, xds->buffer_capacity + ((used_buffer_size == 0) ? 1 : used_buffer_size), sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM); if (rc != XDS_OK) goto leave; } else goto leave; } while (restart_engine); } } rc = XDS_OK; /* Clean up and leave. */ leave: free(fmt); if (rc != XDS_OK) xds->buffer_len = buffer_len_backup; return rc; } @ 1.14 log @Fixed warnings that complained about feeding a "char" into the isxxx() routines, which expect an "int". @ text @@ 1.13 log @Merged the whole contents of branch "callback-semantic-rewrite" into the main branch. The changes there include: - A change of the callback semantics; callbacks do now return XDS_ERR_(UNDER|OVER)FLOW in case the buffer size doesn't fit. Rather than passing the differing byte size through the return code, it is stored in the location used_buffer_size points to -- a new parameter provided to all callbacks. In order to implement this, the framework, all callbacks and most of the test suits had to be adapted. - internal.h now provides the macro xds_check_parameter(), which can be used to verify the contents of function parameters with assert() and if in one line. If assert()s are deactivated, the routine will still return XDS_ERR_INVALID_ARG. Because of this change, internal.h now includes the system header assert.h. This means that this include coulde be removed from almost all modules. - internal.h now provides the macros xds_init_(en|de)coding_engine(). These can be used to comfortably verify a callback's parameters and to verify the buffer size. All engines have been rewritten to use these. @ text @d74 1 a74 1 while(isalnum(*p) || *p == '-' || *p == '_') @ 1.12 log @Fixed three bugs revealed by RSE's review of the code. @ text @a29 1 #include d43 2 a44 2 assert(xds != NULL); assert(fmt_arg != NULL); a45 2 if (xds == NULL || fmt_arg == NULL) return XDS_ERR_INVALID_ARG; a64 20 /* Ensure the buffer has free space. */ assert(xds->buffer_len <= xds->buffer_capacity); if (xds->buffer_len == xds->buffer_capacity) { if (xds->we_own_buffer) { rc = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, xds->buffer_len + 1, sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM); if (rc != XDS_OK) return rc; } else return XDS_ERR_OVERFLOW; } d83 2 d86 8 a93 1 if (xds_find_engine(xds->engines, xds->engines_len, name, &pos)) d95 4 a98 2 int restart_engine; do d100 9 a108 26 args_backup = args; rc = (*xds->engines[pos].engine)(xds, xds->engines[pos].context, xds->buffer + xds->buffer_len, xds->buffer_capacity - xds->buffer_len, &args); if (rc < 0) goto leave; else if (rc >= xds->buffer_capacity - xds->buffer_len) { /* enlarge buffer */ int rc2; if (rc > xds->buffer_capacity - xds->buffer_len) { restart_engine = XDS_TRUE; args = args_backup; } else { restart_engine = XDS_FALSE; xds->buffer_len += rc; } if (!xds->we_own_buffer) { rc = XDS_ERR_OVERFLOW; a109 13 } rc2 = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, xds->buffer_capacity + rc, sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); assert(rc2 == XDS_OK || rc2 == XDS_ERR_NO_MEM); if (rc2 != XDS_OK) { rc = rc2; goto leave; } d113 2 a114 2 restart_engine = XDS_FALSE; xds->buffer_len += rc; d117 36 a152 6 while (restart_engine); } else { rc = XDS_ERR_UNKNOWN_ENGINE; goto leave; d154 1 @ 1.12.2.1 log @Added xds_check_parameter() macro to short-cut the assert() and if-statements at the begining of every routine. Also rewrote some modules to take advantage of that macro. @ text @d44 5 a48 3 xds_check_parameter(xds != NULL); xds_check_parameter(fmt_arg != NULL); xds_check_parameter(xds->mode == XDS_ENCODE); @ 1.12.2.2 log @Rewrote framework for new callback semantics: Callbacks now return an XDS_* code all the time. The length of the buffer used by the callback is returned via used_buffer_size. If the buffer is too small, XDS_ERR_OVERFLOW is returned. @ text @a107 1 size_t used_buffer_size; a114 1 &used_buffer_size, d116 3 a118 7 assert(rc <= 0); if (rc == XDS_OK) { restart_engine = XDS_FALSE; xds->buffer_len += used_buffer_size; } else if (rc == XDS_ERR_OVERFLOW) d120 13 d134 2 d137 1 d139 9 a147 10 restart_engine = XDS_TRUE; args = args_backup; rc = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, xds->buffer_capacity + 1, sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM); if (rc != XDS_OK) d149 1 d152 4 a155 1 goto leave; @ 1.12.2.3 log @Moved the code that enlarges the buffer in case it doesn't have any free space into the engine execution loop to make sure no engine is called with a "buffer_size" argument of 0. @ text @d66 20 a103 2 int restart_engine; size_t used_buffer_size; d105 1 a105 2 if (xds_find_engine(xds->engines, xds->engines_len, name, &pos) == XDS_FALSE) d107 21 a127 3 rc = XDS_ERR_UNKNOWN_ENGINE; goto leave; } d129 2 a130 3 do { /* Ensure the buffer has free space. */ a131 5 assert(xds->buffer_len <= xds->buffer_capacity); if (xds->buffer_len == xds->buffer_capacity) { if (xds->we_own_buffer) { d134 1 a134 1 xds->buffer_len + 1, a141 2 { rc = XDS_ERR_OVERFLOW; a142 1 } d144 6 a149 35 /* Execute the engine. */ args_backup = args; rc = (*xds->engines[pos].engine)(xds, xds->engines[pos].context, xds->buffer + xds->buffer_len, xds->buffer_capacity - xds->buffer_len, &used_buffer_size, &args); assert(rc <= 0); if (rc == XDS_OK) { restart_engine = XDS_FALSE; xds->buffer_len += used_buffer_size; } else if (rc == XDS_ERR_OVERFLOW) { /* enlarge buffer */ if (!xds->we_own_buffer) goto leave; restart_engine = XDS_TRUE; args = args_backup; rc = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, xds->buffer_capacity + 1, sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM); if (rc != XDS_OK) goto leave; } else goto leave; a150 1 while (restart_engine); @ 1.12.2.4 log @Now that internal.h includes assert.h we don't need to do that anymore. @ text @d30 1 @ 1.12.2.5 log @- Make sure that used_buffer_size is initialized to zero before the callback is executed. - Use used_buffer_size to determine the new size of the reallocated buffer in case of an overflow. If used_buffer_size is 0 (default), use 1 instead. @ text @d45 1 a45 1 assert(xds->mode == XDS_ENCODE); a119 1 used_buffer_size = 0; d143 1 a143 1 xds->buffer_capacity + ((used_buffer_size == 0) ? 1 : used_buffer_size), @ 1.11 log @xds_encode() and xds_decode() had two problems: - In case any of the callbacks returns with an error, the original xds->buffer_len value must be restored or the buffer is messed up with values from successful callback executions. The routines wouldn't be atomic in that case. - If an encoding callback uses up all buffer space but formatted the value successfully nonetheless, the xds->buffer_len wasn't incremented. Both problems have been fixed. @ text @d65 1 a65 1 xds->we_own_buffer = XDS_FALSE; @ 1.10 log @Removed debug output. @ text @d36 1 d94 1 d130 1 d132 2 d173 2 @ 1.9 log @Added XDS_TRUE and XDS_FALSE defines and started using them in the sources. @ text @a27 2 #include /* delete me */ a62 1 printf("Setting up buffer with capacity %d byte.\n", xds->buffer_capacity); a81 1 printf("Enlarged buffer to %d byte.\n", xds->buffer_capacity); a109 1 printf("Executing engine '%s' ...\n", name); a146 1 printf("Enlarged buffer to %d byte.\n", xds->buffer_capacity); @ 1.8 log @When an engine is restarted after buffer enlargement, the original value of the ap_list must be restored. Fixed that bug and added a test suite that verifies this behavior. @ text @d67 1 a67 1 xds->we_own_buffer = (1==1); d129 1 a129 1 restart_engine = (1==1); d133 1 a133 1 restart_engine = (1!=1); d156 1 a156 1 restart_engine = (1!=1); @ 1.7 log @We need to make a difference between the callback using up all buffer capacity, in which case we need to enlarge the buffer but don't need to restart the callback, and the callback truncating its output, because the buffer was too small, in which case we need to enlarge the buffer _and_ restart the callback. @ text @d37 1 d115 1 d128 1 d130 2 @ 1.6 log @Improved clarity of debug message. @ text @d125 5 a146 1 restart_engine = (1==1); @ 1.5 log @Passing va_list parameters by value is fine, unless the called routine modifies the parameter list and you expect the change to propagate back to the mother routine. What is what we want. Hence, I changed the prototype of the engine callback to expect a pointer to va_list. @ text @d64 1 a64 1 printf("Enlarged buffer to %d byte.\n", xds->buffer_capacity); @ 1.4 log @Removed two instances where a local definition of "rc" shadowed an earlier one in the routine. @ text @d118 1 a118 1 args); @ 1.3 log @Fixed a bug in xds_vencode() that would cause the routine not to recognize the end of the fmt string correctly. @ text @d56 5 a60 5 int rc = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, XDS_INITIAL_BUFFER_CAPACITY, sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); d76 5 a80 5 int rc = xds_set_capacity((void**)&xds->buffer, &xds->buffer_capacity, xds->buffer_len + 1, sizeof(char), XDS_INITIAL_BUFFER_CAPACITY); @ 1.2 log @Implemented xds_encode() and xds_vencode(). @ text @d100 4 a103 1 *p++ = '\0'; @ 1.1 log @Added function stubs that return errors when called. @ text @d28 5 d35 1 a35 1 int xds_vencode(xds_t* xds, const char* fmt, va_list args) d37 126 a162 1 return XDS_ERR_INVALID_ARG; @