head 1.7; access; symbols last-version-in-peti-style:1.6 callback-semantic-rewrite:1.4.0.2; locks; strict; comment @ * @; 1.7 date 2001.08.08.19.15.23; author rse; state dead; branches; next 1.6; 1.6 date 2001.08.08.11.50.16; author simons; state Exp; branches; next 1.5; 1.5 date 2001.08.01.15.25.47; author simons; state Exp; branches; next 1.4; 1.4 date 2001.07.24.15.46.18; author simons; state Exp; branches 1.4.2.1; next 1.3; 1.3 date 2001.07.16.17.50.07; author simons; state Exp; branches; next 1.2; 1.2 date 2001.07.09.19.00.44; author simons; state Exp; branches; next 1.1; 1.1 date 2001.07.04.15.58.51; author simons; state Exp; branches; next ; 1.4.2.1 date 2001.08.01.09.55.58; author simons; state Exp; branches; next 1.4.2.2; 1.4.2.2 date 2001.08.01.11.26.16; author simons; state Exp; branches; next 1.4.2.3; 1.4.2.3 date 2001.08.01.13.26.29; author simons; state Exp; branches; next 1.4.2.4; 1.4.2.4 date 2001.08.01.14.37.32; author simons; state Exp; branches; next ; desc @@ 1.7 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_vdecode(xds_t* xds, const char* fmt_arg, va_list args) { 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_DECODE); if (xds->mode != XDS_DECODE) return XDS_ERR_INVALID_MODE; /* Ensure we have a buffer to decode. */ if (xds->buffer == NULL || xds->buffer_capacity == 0) return XDS_ERR_UNDERFLOW; /* 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) { size_t pos; size_t used_buffer_size = 0; if (xds_find_engine(xds->engines, xds->engines_len, name, &pos)) { 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) xds->buffer_len += used_buffer_size; else goto leave; } else { rc = XDS_ERR_UNKNOWN_ENGINE; goto leave; } } } rc = XDS_OK; /* Clean up and leave. */ leave: free(fmt); if (rc != XDS_OK) xds->buffer_len = buffer_len_backup; return rc; } @ 1.6 log @Fixed warnings that complained about feeding a "char" into the isxxx() routines, which expect an "int". @ text @@ 1.5 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 @d62 1 a62 1 while(isalnum(*p) || *p == '-' || *p == '_') @ 1.4 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 @a29 1 #include d42 2 a43 2 assert(xds != NULL); assert(fmt_arg != NULL); a44 2 if (xds == NULL || fmt_arg == NULL) return XDS_ERR_INVALID_ARG; d72 1 d79 1 d81 4 a84 1 if (rc < 0) a85 2 else xds->buffer_len += rc; @ 1.4.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 @d43 2 a44 2 xds_check_parameter(xds != NULL); xds_check_parameter(fmt_arg != NULL); d46 2 @ 1.4.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 @a72 1 size_t used_buffer_size; a78 1 &used_buffer_size, d80 2 a81 3 assert(rc <= 0); if (rc == XDS_OK) xds->buffer_len += used_buffer_size; d83 1 a83 1 goto leave; @ 1.4.2.3 log @Now that internal.h includes assert.h we don't need to do that anymore. @ text @d30 1 @ 1.4.2.4 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 @d72 1 a72 1 size_t used_buffer_size = 0; @ 1.3 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 @d35 1 d62 1 d99 2 @ 1.2 log @Implemented xds_decode(). @ text @d79 1 a79 1 args); @ 1.1 log @Added function stubs that return errors when called. @ text @d28 3 d33 1 a33 1 int xds_vdecode(xds_t* xds, const char* fmt, va_list args) d35 63 a97 1 return XDS_ERR_INVALID_ARG; @