📄 octstr.h
字号:
/* ==================================================================== * 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. */ /* * octstr.h - Octet strings * * This header file declares an abstract data type, Octstr, for storing * and manipulating octet strings: strings of arbitrary binary data in * 8-bit bytes. Unlike C strings, they can contain the NUL byte ('\0'). * Conceptually, they consist of a sequence of octets (bytes) and the * length of the sequence. There are various basic operations on octet * strings: concatenating, comparing, printing, etc. * * Octet strings come in two flavors: mutable and immutable. Mutable * octet strings are the normal kind and they can be modified and * otherwise manipulated at will. Immutable octet strings are meant to * be wrappers around a C string literal. They may not be modified, though * they may be destroyed. * * Immutable octet strings are meant to simplify usage of octet strings * together with C strings by reducing the number of octstr_* functions. * For example, we need a function for searching one string within another. * There needs to be different flavors of this: exact search, case-insensitive * search, and a search limited to the first N octets of the strings. * If in each of these one of the arguments may be either an octet string * or a C string, the number of functions doubles. Thus, we use immutable * strings instead: * * octstr_search(os, octstr_imm("foo"), 0) * * The above looks like a memory leak, but it is not. Each immutable * octet string (i.e., with the same C string literal pointer) is really * created only the first time, and octstr_destroy won't destroy it, * either. The immutable octet strings are destroyed automatically when * the process ends. * * See comments below for explanations on individual functions. Note that * all functions use gw_malloc and friends, so they won't return if the * memory allocations fail. Octet string functions are thread safe, as * long as they only one thread at a time operates on each octet string. */#ifndef OCTSTR_H#define OCTSTR_H#include <stdio.h>#include <stdarg.h>#include "list.h"typedef struct Octstr Octstr;/* * Initialize the Octstr subsystem. */void octstr_init(void);/* * Shut down the Octstr subsystem. */void octstr_shutdown(void);/* * Create an octet string from a NUL-terminated C string. Return pointer to * the new object. */Octstr *octstr_create_real(const char *cstr, const char *file, long line, const char *func);#define octstr_create(cstr) \ gw_claim_area(octstr_create_real((cstr), __FILE__, __LINE__, __func__))/* * Create an octet string from arbitrary binary data. The length of the * data is given, so it can contain NUL characters. */Octstr *octstr_create_from_data_real(const char *data, long len, const char *file, long line, const char *func);#define octstr_create_from_data(data, len) \ gw_claim_area(octstr_create_from_data_real((data), (len), __FILE__, __LINE__, __func__))#define octstr_create_from_data_trace(data, len, file, line, func) \ gw_claim_area(octstr_create_from_data_real(data, len, file, line, func))/* * Create an immutable octet string from a C string literal. The * C string literal MUST NOT be modified and it MUST exist until the * octet string is destroyed. The immutable octet string need not be * destroyed - it is destroyed automatically when octstr_shutdown is * called. In fact, octstr_destroy is a no-op for immutables. */Octstr *octstr_imm(const char *cstr);/* * Destroy an octet string, freeing all memory it uses. A NULL argument * is ignored. */void octstr_destroy(Octstr *ostr);/* * Destroy an octet string. Wrapper around octstr_destroy that is callable * via list_destroy. */void octstr_destroy_item(void *os);/* * Return the length of (number of octets in) an object string. */long octstr_len(const Octstr *ostr);/* * Create a new octet string by copying part of an existing one. Return * pointer to the new object. If `from' is after end of `ostr', an empty * octet string is created. If `from+len' is after the end of `ostr', * `len' is reduced appropriately. */Octstr *octstr_copy_real(const Octstr *ostr, long from, long len, const char *file, long line, const char *func);#define octstr_copy(ostr, from, len) \ gw_claim_area(octstr_copy_real((ostr), (from), (len), __FILE__, __LINE__, __func__))/* * Copy all of an octet string. */Octstr *octstr_duplicate_real(const Octstr *ostr, const char *file, long line, const char *func);#define octstr_duplicate(ostr) \ gw_claim_area(octstr_duplicate_real((ostr), __FILE__, __LINE__, __func__))/* * Create a new octet string by catenating two existing ones. Return * pointer to the new object. */Octstr *octstr_cat(Octstr *ostr1, Octstr *ostr2);/* * Return value of octet at a given position in an octet string. The returned * value has a range of 0..255 for valid positions, and -1 if `pos' is * after the end of the octet string. */int octstr_get_char(const Octstr *ostr, long pos);/* * Replace a single, existing character in an octet string. Operation cannot * fail: if pos is not inside the string, the operation will silently be * ignored. */void octstr_set_char(Octstr *ostr, long pos, int ch);/* * Copy bytes from octet string into array. */void octstr_get_many_chars(char *buf, Octstr *ostr, long pos, long len);/* * Return pointer to contents of octet string as a NUL-terminated C string. * This is guaranteed to have a NUL character at the end, but it is not * guaranteed (how could it?) to not contain NUL characters elsewhere. * The pointer points directly into the internal buffer of the octet * string, and must not be modified, and must not be used after any * octstr_* function that modifies the octet string is called after this * one. It is meant for printing debug messages easily. * * If the octet string is empty, an empty C string is returned, not NULL. */char *octstr_get_cstr_real(const Octstr *ostr, const char *file, long line, const char *func);#define octstr_get_cstr(ostr) \ (octstr_get_cstr_real(ostr, __FILE__, __LINE__, __func__))/* * Append characters from printable hexadecimal format at the tail of * an octet string. "78797a" or "78797A" would be converted to "xyz" * and then appended. */void octstr_append_from_hex(Octstr *ostr, char *hex);/* Convert the octet string in-place to printable hexadecimal format. * "xyz" would be converted to "78797a". If the uppercase * flag is set, 'A' through 'F' are used instead of 'a' through 'f'. */void octstr_binary_to_hex(Octstr *ostr, int uppercase);/* Convert the octet string in-place from printable hexadecimal * format to binary. "78797a" or "78797A" would be converted to "xyz". * If the string is not in the expected format, return -1 and leave * the string unchanged. If all was fine, return 0. */int octstr_hex_to_binary(Octstr *ostr);/* Base64-encode the octet string in-place, using the MIME base64 * encoding defined in RFC 2045. Note that the result may be * multi-line and is always terminated with a CR LF sequence. */void octstr_binary_to_base64(Octstr *ostr);/* Base64-decode the octet string in-place, using the MIME base64 * encoding defined in RFC 2045. */void octstr_base64_to_binary(Octstr *ostr);/* Parse a number at position 'pos' in 'ostr', using the same rules as * strtol uses regarding 'base'. Skip leading whitespace. * * Return the position of the first character after the number, * or -1 if there was an error. Return the length of the octet string * if the number ran to the end of the string. * * Assign the number itself to the location pointed to by 'number', if * there was no error. * * Possible errno values in case of an error: * ERANGE The number did not fit in a long. * EINVAL No digits of the appropriate base were found. */long octstr_parse_long(long *number, Octstr *ostr, long pos, int base);/* As above but parses and assigns double number. */long octstr_parse_double(double *number, Octstr *ostr, long pos);/* Run the 'filter' function over each character in the specified range. * Return 1 if the filter returned true for all characters, otherwise 0. * The octet string is not changed. * For example: ok = octstr_check_range(o, 1, 10, gw_isdigit); */typedef int (*octstr_func_t)(int);int octstr_check_range(Octstr *ostr, long pos, long len, octstr_func_t filter);/* Run the 'map' function over each character in the specified range, * replacing each character with the return value of that function. * For example: octstr_convert_range(o, 1, 10, tolower); */void octstr_convert_range(Octstr *ostr, long pos, long len, octstr_func_t map);/* * Compare two octet strings, returning 0 if they are equal, negative if * `ostr1' is less than `ostr2' (when compared octet-value by octet-value), * and positive if greater. */int octstr_compare(const Octstr *ostr1, const Octstr *ostr2);/* * Like octstr_compare, except compares bytes without case sensitivity. * Note that this probably doesn't work for Unicode, but should work * for such 8-bit character sets as are supported by libc. */int octstr_case_compare(const Octstr *ostr1, const Octstr *ostr2);/* * as above, but comparing is done only up to n bytes */int octstr_ncompare(const Octstr *ostr1, const Octstr *ostr2, long n);/* * Same as octstr_compare, but compares the content of the octet string to * a C string. */int octstr_str_compare(const Octstr *ostr1, const char *str);/* * Same as octstr_str_compare, but comparing is done only up to n bytes. */int octstr_str_ncompare(const Octstr *ostr, const char *str, long n);/* * Write contents of octet string to a file. Return -1 for error, 0 for OK. */int octstr_print(FILE *f, Octstr *ostr);/* * Search the character from octet string starting from position pos. Returns * the position (index) of the char in string, -1 if not found. */int octstr_search_char(const Octstr *ostr, int ch, long pos);/* * Search several character from octet string starting from position pos. Returns * the position (index) of the first char found in string, -1 if none was found. */int octstr_search_chars(const Octstr *ostr, const Octstr *chars, long pos);/* * Search for the octet string 'needle' in the octet string 'haystack'.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -