00001 00002 // 00003 // Copyright (c) 2000-2003 Intel Corporation 00004 // All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // * Redistributions of source code must retain the above copyright notice, 00010 // this list of conditions and the following disclaimer. 00011 // * Redistributions in binary form must reproduce the above copyright notice, 00012 // this list of conditions and the following disclaimer in the documentation 00013 // and/or other materials provided with the distribution. 00014 // * Neither name of Intel Corporation nor the names of its contributors 00015 // may be used to endorse or promote products derived from this software 00016 // without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 00022 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00026 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00031 00032 #ifndef GENLIB_UTIL_MEMBUFFER_H 00033 #define GENLIB_UTIL_MEMBUFFER_H 00034 00035 00036 #include <stdlib.h> 00037 #include "util.h" 00038 00039 #define MINVAL( a, b ) ( (a) < (b) ? (a) : (b) ) 00040 #define MAXVAL( a, b ) ( (a) > (b) ? (a) : (b) ) 00041 00042 // pointer to a chunk of memory 00043 typedef struct // memptr 00044 { 00045 char *buf; // start of memory (read/write) 00046 size_t length; // length of memory (read-only) 00047 } memptr; 00048 00049 00050 // maintains a block of dynamically allocated memory 00051 // note: Total length/capacity should not exceed MAX_INT 00052 typedef struct // membuffer 00053 { 00054 char *buf; // mem buffer; must not write 00055 // beyond buf[length-1] (read/write) 00056 size_t length; // length of buffer (read-only) 00057 size_t capacity; // total allocated memory (read-only) 00058 size_t size_inc; // used to increase size; MUST be > 0; (read/write) 00059 00060 // default value of size_inc 00061 #define MEMBUF_DEF_SIZE_INC 5 00062 } membuffer; 00063 00064 //-------------------------------------------------- 00066 //-------------------------------------------------- 00067 00068 #ifdef __cplusplus 00069 extern "C" { 00070 #endif // __cplusplus 00071 00072 /************************************************************************ 00073 * Function : str_alloc 00074 * 00075 * Parameters : 00076 * IN const char* str ; input string object 00077 * IN size_t str_len ; input string length 00078 * 00079 * Description : Allocate memory and copy information from the input 00080 * string to the newly allocated memory. 00081 * 00082 * Return : char* ; 00083 * Pointer to the newly allocated memory. 00084 * NULL if memory cannot be allocated. 00085 * 00086 * Note : 00087 ************************************************************************/ 00088 char* str_alloc( IN const char* str, IN size_t str_len ); 00089 00090 /************************************************************************ 00091 * Function : memptr_cmp 00092 * 00093 * Parameters : 00094 * IN memptr* m ; input memory object 00095 * IN const char* s ; constatnt string for the memory object to be 00096 * compared with 00097 * 00098 * Description : Compares characters of strings passed for number of 00099 * bytes. If equal for the number of bytes, the length of the bytes 00100 * determines which buffer is shorter. 00101 * 00102 * Return : int ; 00103 * < 0 string1 substring less than string2 substring 00104 * 0 string1 substring identical to string2 substring 00105 * > 0 string1 substring greater than string2 substring 00106 * 00107 * Note : 00108 ************************************************************************/ 00109 int memptr_cmp( IN memptr* m, IN const char* s ); 00110 00111 /************************************************************************ 00112 * Function : memptr_cmp_nocase 00113 * 00114 * Parameters : 00115 * IN memptr* m ; input memory object 00116 * IN const char* s ; constatnt string for the memory object to be 00117 * compared with 00118 * 00119 * Description : Compares characters of 2 strings irrespective of the 00120 * case for a specific count of bytes If the character comparison 00121 * is the same the length of the 2 srings determines the shorter 00122 * of the 2 strings. 00123 * 00124 * Return : int ; 00125 * < 0 string1 substring less than string2 substring 00126 * 0 string1 substring identical to string2 substring 00127 * > 0 string1 substring greater than string2 substring 00128 * 00129 * Note : 00130 ************************************************************************/ 00131 int memptr_cmp_nocase( IN memptr* m, IN const char* s ); 00132 00133 00134 /************************************************************************ 00135 * Function : membuffer_set_size 00136 * 00137 * Parameters : 00138 * INOUT membuffer* m ; buffer whose size is to be modified 00139 * IN size_t new_length ; new size to which the buffer will be 00140 * modified 00141 * 00142 * Description : Increases or decreases buffer cap so that at least 00143 * 'new_length' bytes can be stored 00144 * 00145 * Return : int ; 00146 * UPNP_E_SUCCESS - On Success 00147 * UPNP_E_OUTOF_MEMORY - On failure to allocate memory. 00148 * 00149 * Note : 00150 ************************************************************************/ 00151 int membuffer_set_size( INOUT membuffer* m, 00152 IN size_t new_length ); 00153 00154 /************************************************************************ 00155 * Function : membuffer_init 00156 * 00157 * Parameters : 00158 * INOUT membuffer* m ; buffer to be initialized 00159 * 00160 * Description : Wrapper to membuffer_initialize(). 00161 * Set the size of the buffer to MEMBUF_DEF_SIZE_INC 00162 * Initializes m->buf to NULL, length=0 00163 * 00164 * Return : void ; 00165 * 00166 * Note : 00167 ************************************************************************/ 00168 void membuffer_init( INOUT membuffer* m ); 00169 00170 /************************************************************************ 00171 * Function : membuffer_destroy 00172 * 00173 * Parameters : 00174 * INOUT membuffer* m ; buffer to be destroyed 00175 * 00176 * Description : Free's memory allocated for membuffer* m. 00177 * 00178 * Return : void ; 00179 * 00180 * Note : 00181 ************************************************************************/ 00182 void membuffer_destroy( INOUT membuffer* m ); 00183 00184 00185 /************************************************************************ 00186 * Function : membuffer_assign 00187 * 00188 * Parameters : 00189 * INOUT membuffer* m ; buffer whose memory is to be allocated and 00190 * assigned. 00191 * IN const void* buf ; source buffer whose contents will be copied 00192 * IN size_t buf_len ; length of the source buffer 00193 * 00194 * Description : Allocate memory to membuffer* m and copy the contents 00195 * of the in parameter IN const void* buf. 00196 * 00197 * Return : int ; 00198 * UPNP_E_SUCCESS 00199 * UPNP_E_OUTOF_MEMORY 00200 * 00201 * Note : 00202 ************************************************************************/ 00203 int membuffer_assign( INOUT membuffer* m, IN const void* buf, 00204 IN size_t buf_len ); 00205 00206 /************************************************************************ 00207 * Function : membuffer_assign_str 00208 * 00209 * Parameters : 00210 * INOUT membuffer* m ; buffer to be allocated and assigned 00211 * IN const char* c_str ; source buffer whose contents will be 00212 * copied 00213 * 00214 * Description : Wrapper function for membuffer_assign() 00215 * 00216 * Return : int ; 00217 * UPNP_E_SUCCESS 00218 * UPNP_E_OUTOF_MEMORY 00219 * 00220 * Note : 00221 ************************************************************************/ 00222 int membuffer_assign_str( INOUT membuffer* m, IN const char* c_str ); 00223 00224 /************************************************************************ 00225 * Function : membuffer_append 00226 * 00227 * Parameters : 00228 * INOUT membuffer* m ; buffer whose memory is to be appended. 00229 * IN const void* buf ; source buffer whose contents will be 00230 * copied 00231 * IN size_t buf_len ; length of the source buffer 00232 * 00233 * Description : Invokes function to appends data from a constant buffer 00234 * to the buffer 00235 * 00236 * Return : int ; 00237 * 00238 * Note : 00239 ************************************************************************/ 00240 int membuffer_append( INOUT membuffer* m, IN const void* buf, 00241 IN size_t buf_len ); 00242 00243 /************************************************************************ 00244 * Function : membuffer_append_str 00245 * 00246 * Parameters : 00247 * INOUT membuffer* m ; buffer whose memory is to be appended. 00248 * IN const char* c_str ; source buffer whose contents will be 00249 * copied 00250 * 00251 * Description : Invokes function to appends data from a constant string 00252 * to the buffer 00253 * 00254 * Return : int ; 00255 * 00256 * Note : 00257 ************************************************************************/ 00258 int membuffer_append_str( INOUT membuffer* m, IN const char* c_str ); 00259 00260 /************************************************************************ 00261 * Function : membuffer_insert 00262 * 00263 * Parameters : 00264 * INOUT membuffer* m ; buffer whose memory size is to be increased 00265 * and appended. 00266 * IN const void* buf ; source buffer whose contents will be 00267 * copied 00268 * IN size_t buf_len ; size of the source buffer 00269 * int index ; index to determine the bounds while movinf the data 00270 * 00271 * Description : Allocates memory for the new data to be inserted. Does 00272 * memory management by moving the data from the existing memory to 00273 * the newly allocated memory and then appending the new data. 00274 * 00275 * Return : int ; 00276 * 00277 * Note : 00278 ************************************************************************/ 00279 int membuffer_insert( INOUT membuffer* m, IN const void* buf, 00280 IN size_t buf_len, int index ); 00281 00282 00283 /************************************************************************ 00284 * Function : membuffer_delete 00285 * 00286 * Parameters : 00287 * INOUT membuffer* m ; buffer whose memory size is to be decreased 00288 * and copied to the odified location 00289 * IN int index ; index to determine bounds while moving data 00290 * IN size_t num_bytes ; number of bytes that the data needs to 00291 * shrink by 00292 * 00293 * Description : Shrink the size of the buffer depending on the current 00294 * size of the bufer and te input parameters. Move contents from the 00295 * old buffer to the new sized buffer. 00296 * 00297 * Return : void ; 00298 * 00299 * Note : 00300 ************************************************************************/ 00301 void membuffer_delete( INOUT membuffer* m, IN int index, 00302 IN size_t num_bytes ); 00303 00304 00305 /************************************************************************ 00306 * Function : membuffer_detach 00307 * 00308 * Parameters : 00309 * INOUT membuffer* m ; buffer to be returned and updated. 00310 * 00311 * Description : Detaches current buffer and returns it. The caller 00312 * must free the returned buffer using free(). 00313 * After this call, length becomes 0. 00314 * 00315 * Return : char* ; 00316 * a pointer to the current buffer 00317 * 00318 * Note : 00319 ************************************************************************/ 00320 char* membuffer_detach( INOUT membuffer* m ); 00321 00322 /************************************************************************ 00323 * Function : membuffer_attach 00324 * 00325 * Parameters : 00326 * INOUT membuffer* m ; buffer to be updated 00327 * IN char* new_buf ; source buffer which will be assigned to the 00328 * buffer to be updated 00329 * IN size_t buf_len ; length of the source buffer 00330 * 00331 * Description : Free existing memory in membuffer and assign the new 00332 * buffer in its place. 00333 * 00334 * Return : void ; 00335 * 00336 * Note : 'new_buf' must be allocted using malloc or realloc so 00337 * that it can be freed using free() 00338 ************************************************************************/ 00339 void membuffer_attach( INOUT membuffer* m, IN char* new_buf, 00340 IN size_t buf_len ); 00341 #ifdef __cplusplus 00342 } // extern "C" 00343 #endif // __cplusplus 00344 00345 #endif // GENLIB_UTIL_H
1.6.1