📄 wsbc.c
字号:
/* ==================================================================== * The Kannel Software License, Version 1.0 * * Copyright (c) 2001-2004 Kannel Group * Copyright (c) 1998-2001 WapIT Ltd. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Kannel Group (http://www.kannel.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Kannel" and "Kannel Group" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please * contact org@kannel.org. * * 5. Products derived from this software may not be called "Kannel", * nor may "Kannel" appear in their name, without prior written * permission of the Kannel Group. * * 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 KANNEL GROUP OR ITS 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. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Kannel Group. For more information on * the Kannel Group, please see <http://www.kannel.org/>. * * Portions of this software are based upon software originally written at * WapIT Ltd., Helsinki, Finland for the Kannel project. */ /* * * wsbc.c * * Author: Markku Rossi <mtr@iki.fi> * * Copyright (c) 1999-2000 WAPIT OY LTD. * All rights reserved. * * Byte-code handling functions. * */#include "wsint.h"#include "wsbc.h"/********************* Prototypes for static functions ******************//* Add a new pragma of type `type' to the byte-code `bc'. The * function returns a pointer to an internal pragma structure that * must not be freed by the caller. It is freed when the byte-code * `bc' is freed. The function returns NULL if the pragma structure * could not be allocated. */static WsBcPragma *add_pragma(WsBc *bc, WsBcPragmaType type);/********************* Manipulating byte-code structure *****************/WsBc *ws_bc_alloc(WsBcStringEncoding string_encoding){ WsBc *bc = ws_calloc(1, sizeof(WsBc)); if (bc == NULL) return NULL; bc->string_encoding = string_encoding; return bc;}void ws_bc_free(WsBc *bc){ WsUInt16 i; WsUInt8 j; if (bc == NULL) return; /* Free constants. */ for (i = 0; i < bc->num_constants; i++) { WsBcConstant *c = &bc->constants[i]; if (c->type == WS_BC_CONST_TYPE_UTF8_STRING) ws_free(c->u.v_string.data); } ws_free(bc->constants); /* Free pragmas. */ ws_free(bc->pragmas); /* Free function names. */ for (j = 0; j < bc->num_function_names; j++) ws_free(bc->function_names[j].name); ws_free(bc->function_names); /* Free functions. */ for (j = 0; j < bc->num_functions; j++) ws_free(bc->functions[j].code); ws_free(bc->functions); /* Free the byte-code structure. */ ws_free(bc);}WsBool ws_bc_encode(WsBc *bc, unsigned char **data_return, size_t *data_len_return){ WsBuffer buffer; WsUInt32 ui; unsigned char data[64]; unsigned char *p, *mb; size_t len; ws_buffer_init(&buffer); /* Append space for the header. We do not know yet the size of the resulting byte-code. */ if (!ws_buffer_append_space(&buffer, NULL, WS_BC_MAX_HEADER_LEN)) goto error; /* Constants. */ if (!ws_encode_buffer(&buffer, WS_ENC_MB_UINT16, bc->num_constants, WS_ENC_MB_UINT16, (WsUInt16) bc->string_encoding, WS_ENC_END)) goto error; for (ui = 0 ; ui < bc->num_constants; ui++) { switch (bc->constants[ui].type) { case WS_BC_CONST_TYPE_INT: if (WS_INT8_MIN <= bc->constants[ui].u.v_int && bc->constants[ui].u.v_int <= WS_INT8_MAX) { if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_CONST_INT8, WS_ENC_INT8, (WsInt8) bc->constants[ui].u.v_int, WS_ENC_END)) goto error; } else if (WS_INT16_MIN <= bc->constants[ui].u.v_int && bc->constants[ui].u.v_int <= WS_INT16_MAX) { if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_CONST_INT16, WS_ENC_INT16, (WsInt16) bc->constants[ui].u.v_int, WS_ENC_END)) goto error; } else { if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_CONST_INT32, WS_ENC_INT32, bc->constants[ui].u.v_int, WS_ENC_END)) goto error; } break; case WS_BC_CONST_TYPE_FLOAT32: case WS_BC_CONST_TYPE_FLOAT32_NAN: case WS_BC_CONST_TYPE_FLOAT32_POSITIVE_INF: case WS_BC_CONST_TYPE_FLOAT32_NEGATIVE_INF: switch (bc->constants[ui].type) { case WS_BC_CONST_TYPE_FLOAT32: ws_ieee754_encode_single(bc->constants[ui].u.v_float, data); p = data; break; case WS_BC_CONST_TYPE_FLOAT32_NAN: p = ws_ieee754_nan; break; case WS_BC_CONST_TYPE_FLOAT32_POSITIVE_INF: p = ws_ieee754_positive_inf; break; case WS_BC_CONST_TYPE_FLOAT32_NEGATIVE_INF: p = ws_ieee754_negative_inf; break; default: ws_fatal("ws_bc_encode(): internal inconsistency"); /* NOTREACHED */ p = NULL; /* Initialized to keep compiler quiet. */ break; } if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_CONST_FLOAT32, WS_ENC_DATA, p, 4, WS_ENC_END)) goto error; break; break; case WS_BC_CONST_TYPE_UTF8_STRING: /* Encode the strings as requested. */ switch (bc->string_encoding) { case WS_BC_STRING_ENC_ISO_8859_1: { WsUtf8String *string = ws_utf8_alloc(); unsigned char *latin1; size_t latin1_len; WsBool success; if (string == NULL) goto error; /* Create an UTF-8 string. */ if (!ws_utf8_set_data(string, bc->constants[ui].u.v_string.data, bc->constants[ui].u.v_string.len)) { ws_utf8_free(string); goto error; } /* Convert it to latin1. */ latin1 = ws_utf8_to_latin1(string, '?', &latin1_len); /* We'r done with the UTF-8 string. */ ws_utf8_free(string); if (latin1 == NULL) goto error; /* Encode it. */ success = ws_encode_buffer( &buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_CONST_EXT_ENC_STRING, WS_ENC_MB_UINT32, (WsUInt32) latin1_len, WS_ENC_DATA, latin1, latin1_len, WS_ENC_END); ws_utf8_free_data(latin1); if (!success) goto error; } break; case WS_BC_STRING_ENC_UTF8: if (!ws_encode_buffer( &buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_CONST_UTF8_STRING, WS_ENC_MB_UINT32, (WsUInt32) bc->constants[ui].u.v_string.len, WS_ENC_DATA, bc->constants[ui].u.v_string.data, bc->constants[ui].u.v_string.len, WS_ENC_END)) goto error; break; } break; case WS_BC_CONST_TYPE_EMPTY_STRING: if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_CONST_EMPTY_STRING, WS_ENC_END)) goto error; break; } } /* Pragmas. */ if (!ws_encode_buffer(&buffer, WS_ENC_MB_UINT16, bc->num_pragmas, WS_ENC_END)) goto error; for (ui = 0; ui < bc->num_pragmas; ui++) { switch (bc->pragmas[ui].type) { case WS_BC_PRAGMA_TYPE_ACCESS_DOMAIN: if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_PRAGMA_ACCESS_DOMAIN, WS_ENC_MB_UINT16, bc->pragmas[ui].index_1, WS_ENC_END)) goto error; break; case WS_BC_PRAGMA_TYPE_ACCESS_PATH: if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_PRAGMA_ACCESS_PATH, WS_ENC_MB_UINT16, bc->pragmas[ui].index_1, WS_ENC_END)) goto error; break; case WS_BC_PRAGMA_TYPE_USER_AGENT_PROPERTY: if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_PRAGMA_USER_AGENT_PROPERTY, WS_ENC_MB_UINT16, bc->pragmas[ui].index_1, WS_ENC_MB_UINT16, bc->pragmas[ui].index_2, WS_ENC_END)) goto error; break; case WS_BC_PRAGMA_TYPE_USER_AGENT_PROPERTY_AND_SCHEME: if (!ws_encode_buffer( &buffer, WS_ENC_UINT8, (WsUInt8) WS_BC_PRAGMA_USER_AGENT_PROPERTY_AND_SCHEME, WS_ENC_MB_UINT16, bc->pragmas[ui].index_1, WS_ENC_MB_UINT16, bc->pragmas[ui].index_2, WS_ENC_MB_UINT16, bc->pragmas[ui].index_3, WS_ENC_END)) goto error; break; } } /* Function pool. */ if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, bc->num_functions, WS_ENC_END)) goto error; /* Function names. */ if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, bc->num_function_names, WS_ENC_END)) goto error; for (ui = 0; ui < bc->num_function_names; ui++) { size_t name_len = strlen(bc->function_names[ui].name); if (!ws_encode_buffer(&buffer, WS_ENC_UINT8, bc->function_names[ui].index, WS_ENC_UINT8, (WsUInt8) name_len, WS_ENC_DATA, bc->function_names[ui].name, name_len, WS_ENC_END))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -