00001 /* ==================================================================== 00002 * The Kannel Software License, Version 1.0 00003 * 00004 * Copyright (c) 2001-2008 Kannel Group 00005 * Copyright (c) 1998-2001 WapIT Ltd. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 00015 * 2. Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in 00017 * the documentation and/or other materials provided with the 00018 * distribution. 00019 * 00020 * 3. The end-user documentation included with the redistribution, 00021 * if any, must include the following acknowledgment: 00022 * "This product includes software developed by the 00023 * Kannel Group (http://www.kannel.org/)." 00024 * Alternately, this acknowledgment may appear in the software itself, 00025 * if and wherever such third-party acknowledgments normally appear. 00026 * 00027 * 4. The names "Kannel" and "Kannel Group" must not be used to 00028 * endorse or promote products derived from this software without 00029 * prior written permission. For written permission, please 00030 * contact org@kannel.org. 00031 * 00032 * 5. Products derived from this software may not be called "Kannel", 00033 * nor may "Kannel" appear in their name, without prior written 00034 * permission of the Kannel Group. 00035 * 00036 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00037 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00038 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00039 * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 00040 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00041 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00042 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00043 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00044 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00045 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00046 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00047 * ==================================================================== 00048 * 00049 * This software consists of voluntary contributions made by many 00050 * individuals on behalf of the Kannel Group. For more information on 00051 * the Kannel Group, please see <http://www.kannel.org/>. 00052 * 00053 * Portions of this software are based upon software originally written at 00054 * WapIT Ltd., Helsinki, Finland for the Kannel project. 00055 */ 00056 00057 /* 00058 * 00059 * wsbc.h 00060 * 00061 * Author: Markku Rossi <mtr@iki.fi> 00062 * 00063 * Copyright (c) 1999-2000 WAPIT OY LTD. 00064 * All rights reserved. 00065 * 00066 * Byte-code handling. 00067 * 00068 */ 00069 00070 #ifndef WSBC_H 00071 #define WSBC_H 00072 00073 #include "wsint.h" 00074 00075 /********************* Types and defintions *****************************/ 00076 00077 /* The byte-code version numbers. */ 00078 #define WS_BC_VERSION_MAJOR 1 00079 #define WS_BC_VERSION_MINOR 1 00080 #define WS_BC_VERSION (((WS_BC_VERSION_MAJOR - 1) << 4) | WS_BC_VERSION_MINOR) 00081 00082 /* The maximum length of the byte-code header: the multi-byte encoded 00083 length + one byte for the version information. */ 00084 #define WS_BC_MAX_HEADER_LEN (WS_MB_UINT32_MAX_ENCODED_LEN + 1) 00085 00086 /* The string encoding, used in the byte-code data. These are the 00087 MIBEnum values, assigned by IANA. For a complete description of 00088 the character sets and values, please see the document 00089 `rfc/iana/assignments/character-sets'. */ 00090 typedef enum 00091 { 00092 WS_BC_STRING_ENC_ISO_8859_1 = 4, 00093 WS_BC_STRING_ENC_UTF8 = 106 00094 } WsBcStringEncoding; 00095 00096 /* Constant types in the BC constants pool. */ 00097 #define WS_BC_CONST_INT8 0 00098 #define WS_BC_CONST_INT16 1 00099 #define WS_BC_CONST_INT32 2 00100 #define WS_BC_CONST_FLOAT32 3 00101 #define WS_BC_CONST_UTF8_STRING 4 00102 #define WS_BC_CONST_EMPTY_STRING 5 00103 #define WS_BC_CONST_EXT_ENC_STRING 6 00104 #define WS_BC_CONST_FIRST_RESERVED 7 00105 00106 /* An in-memory byte-code constant. */ 00107 00108 typedef enum 00109 { 00110 WS_BC_CONST_TYPE_INT, 00111 WS_BC_CONST_TYPE_FLOAT32, 00112 WS_BC_CONST_TYPE_FLOAT32_NAN, 00113 WS_BC_CONST_TYPE_FLOAT32_POSITIVE_INF, 00114 WS_BC_CONST_TYPE_FLOAT32_NEGATIVE_INF, 00115 WS_BC_CONST_TYPE_UTF8_STRING, 00116 WS_BC_CONST_TYPE_EMPTY_STRING 00117 } WsBcConstantType; 00118 00119 struct WsBcConstantRec 00120 { 00121 WsBcConstantType type; 00122 00123 union 00124 { 00125 WsInt32 v_int; 00126 WsFloat v_float; 00127 WsUtf8String v_string; 00128 } u; 00129 }; 00130 00131 typedef struct WsBcConstantRec WsBcConstant; 00132 00133 /* Pragma types in the BC pragma pool. */ 00134 #define WS_BC_PRAGMA_ACCESS_DOMAIN 0 00135 #define WS_BC_PRAGMA_ACCESS_PATH 1 00136 #define WS_BC_PRAGMA_USER_AGENT_PROPERTY 2 00137 #define WS_BC_PRAGMA_USER_AGENT_PROPERTY_AND_SCHEME 3 00138 #define WS_BC_PRAGMA_FIRST_RESERVED 4 00139 00140 /* An in-memory byte-code pragma. */ 00141 00142 typedef enum 00143 { 00144 WS_BC_PRAGMA_TYPE_ACCESS_DOMAIN, 00145 WS_BC_PRAGMA_TYPE_ACCESS_PATH, 00146 WS_BC_PRAGMA_TYPE_USER_AGENT_PROPERTY, 00147 WS_BC_PRAGMA_TYPE_USER_AGENT_PROPERTY_AND_SCHEME 00148 } WsBcPragmaType; 00149 00150 struct WsBcPragmaRec 00151 { 00152 WsBcPragmaType type; 00153 00154 WsUInt16 index_1; 00155 WsUInt16 index_2; 00156 WsUInt16 index_3; 00157 }; 00158 00159 typedef struct WsBcPragmaRec WsBcPragma; 00160 00161 /* An in-memory byte-code function name. */ 00162 struct WsBcFunctionNameRec 00163 { 00164 /* Index to the function pool. */ 00165 WsUInt8 index; 00166 00167 /* The name of the function as a 7 bit ASCII. This is as-is in the 00168 UTF-8 format. */ 00169 char *name; 00170 }; 00171 00172 typedef struct WsBcFunctionNameRec WsBcFunctionName; 00173 00174 /* An in-memory byte-code function. */ 00175 struct WsBcFunctionRec 00176 { 00177 WsUInt8 num_arguments; 00178 WsUInt8 num_locals; 00179 WsUInt32 code_size; 00180 unsigned char *code; 00181 }; 00182 00183 typedef struct WsBcFunctionRec WsBcFunction; 00184 00185 /* An in-memory byte-code file. */ 00186 struct WsBcRec 00187 { 00188 /* How the strings are encoded in linearization. */ 00189 WsBcStringEncoding string_encoding; 00190 00191 /* Constant pool. In this structure, all strings are in UTF-8 00192 format. However, they can be converted to different formats - if 00193 requested - when linearizing the byte-code. */ 00194 WsUInt16 num_constants; 00195 WsBcConstant *constants; 00196 00197 /* Pragma pool. */ 00198 WsUInt16 num_pragmas; 00199 WsBcPragma *pragmas; 00200 00201 /* Function pool. */ 00202 00203 WsUInt8 num_function_names; 00204 WsBcFunctionName *function_names; 00205 00206 WsUInt8 num_functions; 00207 WsBcFunction *functions; 00208 }; 00209 00210 typedef struct WsBcRec WsBc; 00211 00212 /********************* Manipulating byte-code structure *****************/ 00213 00214 /* Allocate a new byte-code structure. The argument `string_encoding' 00215 specifies the encoding that is used for strings. The function 00216 returns NULL if the allocation failed. */ 00217 WsBc *ws_bc_alloc(WsBcStringEncoding string_encoding); 00218 00219 /* Free the byte-code structure `bc' and all its internally allocated 00220 data structures. The byte-code handle `bc' should not be used 00221 after this function. */ 00222 void ws_bc_free(WsBc *bc); 00223 00224 /* Encode the byte-code structure `bc' into a linearized binary 00225 byte-code blob. The function returns WS_TRUE if the encoding was 00226 successful of WS_FALSE otherwise. The result blob is returned in 00227 `data_return' and its length is returned in `data_len_return'. The 00228 returned byte-code block must be freed with the ws_bc_data_free() 00229 function. You *must* not free it with ws_free() since it will 00230 corrupt the heap. */ 00231 WsBool ws_bc_encode(WsBc *bc, unsigned char **data_return, 00232 size_t *data_len_return); 00233 00234 /* Free a byte-code data `data', returned by the ws_bc_encode() 00235 function. */ 00236 void ws_bc_data_free(unsigned char *data); 00237 00238 /* Decode the byte-code data `data' into an in-memory byte-code 00239 structure. The function returns the byte-code structure or NULL if 00240 the decoding fails. The argument `data_len' specfies the length of 00241 the byte-code data `data'. The returned byte-code structure must 00242 be freed with the ws_bc_free() function when it is not needed 00243 anymore. */ 00244 WsBc *ws_bc_decode(const unsigned char *data, size_t data_len); 00245 00246 /********************* Adding constant elements *************************/ 00247 00248 /* Add an integer constant `value' to the constant pool of the 00249 byte-code structure `bc'. The index of the constant is returned in 00250 `index_return'. The function returns WS_TRUE if the operation was 00251 successful or WS_FALSE otherwise (out of memory). */ 00252 WsBool ws_bc_add_const_int(WsBc *bc, WsUInt16 *index_return, 00253 WsInt32 value); 00254 00255 /* Add a floating point constant `value' to the constant pool of the 00256 byte-code structure `bc'. */ 00257 WsBool ws_bc_add_const_float(WsBc *bc, WsUInt16 *index_return, WsFloat value); 00258 00259 /* Add an UTF-8 encoded string to the constant pool of the byte-code 00260 structure `bc'. */ 00261 WsBool ws_bc_add_const_utf8_string(WsBc *bc, WsUInt16 *index_return, 00262 const unsigned char *data, size_t len); 00263 00264 /* Add an empty string to the constant pool of the byte-code structure 00265 `bc'. */ 00266 WsBool ws_bc_add_const_empty_string(WsBc *bc, WsUInt16 *index_return); 00267 00268 /********************* Adding pragmas ***********************************/ 00269 00270 /* Add an access control specifier pragma to the constant and pragma 00271 pools of the byte-code structure `bc'. The argument `domain' has 00272 `domain_len' bytes of UTF-8 data specifying the access domain. */ 00273 WsBool ws_bc_add_pragma_access_domain(WsBc *bc, 00274 const unsigned char *domain, 00275 size_t domain_len); 00276 00277 /* Add an access control specifier pragma to the constant and pragma 00278 pools of the byte-code structure `bc'. The argument `path' has 00279 `path_len' bytes of UTF-8 data specifying the access path. */ 00280 WsBool ws_bc_add_pragma_access_path(WsBc *bc, 00281 const unsigned char *path, 00282 size_t path_len); 00283 00284 /* Add a use agent property pragma to the constant and pragma pools of 00285 the byte-code structure `bc'. The arguments `name' and `property' 00286 are UTF-8 encoded use agent property. */ 00287 WsBool ws_bc_add_pragma_user_agent_property(WsBc *bc, 00288 const unsigned char *name, 00289 size_t name_len, 00290 const unsigned char *property, 00291 size_t property_len); 00292 00293 /* Add a use agent property pragma to the constant and pragma pools of 00294 the byte-code structure `bc'. The arguments `name', `property', 00295 and `scheme' are UTF-8 encoded use agent property and scheme. */ 00296 WsBool ws_bc_add_pragma_user_agent_property_and_scheme( 00297 WsBc *bc, 00298 const unsigned char *name, 00299 size_t name_len, 00300 const unsigned char *property, 00301 size_t property_len, 00302 const unsigned char *scheme, 00303 size_t scheme_len); 00304 00305 /********************* Adding functions *********************************/ 00306 00307 /* Add a new function to the function pool of the byte-code structure 00308 `bc'. The argument `name' specifies the name of the function for 00309 external functions. For internal functions, the `name' argument 00310 must be NULL. The argument `num_arguments' specifies the number of 00311 arguments the function takes. The argument `num_locals' specifies 00312 how many local variables the function needs. The byte-code of the 00313 function is in `code' and it is `code_size' bytes long. The 00314 function takes a copy of the byte-code array `code'. The caller 00315 can free / modify the original array, pointed by the argument 00316 `code', after the function returns. The function returns WS_TRUE 00317 if the adding was successful or WS_FALSE otherwise (out of 00318 memory). */ 00319 WsBool ws_bc_add_function(WsBc *bc, WsUInt8 *index_return, 00320 char *name, WsUInt8 num_arguments, 00321 WsUInt8 num_locals, WsUInt32 code_size, 00322 unsigned char *code); 00323 00324 #endif /* not WSBC_H */