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 * parse.h - functions for octet-by-octet parsing of an octstr 00059 * 00060 * Interface to keep track of position in an octstr, and remember 00061 * error conditions so that they can be checked after a bunch of 00062 * calls. Also allows temporary clipping of the string, so that 00063 * parsing doesn't go past a boundary until it's explicitly allowed 00064 * to. This helps parse strings containing length-defined chunks. 00065 * 00066 * The main use of this interface is to simplify code that does 00067 * this kind of parsing, so that it can pass around a single 00068 * ParseContext value instead of an octstr and one or more offset 00069 * and length parameters. 00070 * 00071 * Note: The octstr involved MUST NOT change during parsing. 00072 * 00073 * Richard Braakman 00074 */ 00075 00076 #ifndef PARSE_H 00077 #define PARSE_H 00078 00079 typedef struct context ParseContext; 00080 00081 /* 00082 * Return a ParseContext object for this octstr, with parsing starting 00083 * at position 0 and the limit at the end of the string. 00084 */ 00085 ParseContext *parse_context_create(Octstr *str); 00086 00087 /* 00088 * Destroy a ParseContext object. Note that this does not free the string 00089 * that was parsed. 00090 */ 00091 void parse_context_destroy(ParseContext *context); 00092 00093 /* 00094 * Return -1 if any error has occurred during parsing, otherwise 0. 00095 */ 00096 int parse_error(ParseContext *context); 00097 00098 /* 00099 * Clear the error flag for the next call to parse_error. 00100 */ 00101 void parse_clear_error(ParseContext *context); 00102 00103 /* 00104 * Set the error flag. 00105 */ 00106 void parse_set_error(ParseContext *context); 00107 00108 /* 00109 * Set a new "end" of the string, for parsing purposes, at length 00110 * octets from the current position. Return 0 if it's okay. 00111 * If it doesn't fit in the current limit, don't do anything and 00112 * return -1. 00113 */ 00114 int parse_limit(ParseContext *context, long length); 00115 00116 /* 00117 * Restore the previous "end" of the string. Limits can be stacked 00118 * as deeply as needed. The original limit (end-of-string) can 00119 * not be popped. Return -1 and set the error flag if there was 00120 * nothing to pop, otherwise return 0. 00121 */ 00122 int parse_pop_limit(ParseContext *context); 00123 00124 /* 00125 * Return the number of octets between the current position and 00126 * the current limit. 00127 */ 00128 long parse_octets_left(ParseContext *context); 00129 00130 /* 00131 * Skip count octets. If that would go past the limit, return -1, 00132 * skip to the limit, and set the error flag. Otherwise return 0. 00133 */ 00134 int parse_skip(ParseContext *context, long count); 00135 00136 /* 00137 * Skip to the current limit. Cannot fail. 00138 */ 00139 void parse_skip_to_limit(ParseContext *context); 00140 00141 /* 00142 * Set offset to new position. If that would go past the limit, return 00143 * -1, skip to the limit, and set the error flag. Otherwise return 0. 00144 */ 00145 int parse_skip_to(ParseContext *context, long pos); 00146 00147 /* 00148 * Return the next octet, but do not skip over it. 00149 * If already at the limit, return -1 and set the error flag. 00150 */ 00151 int parse_peek_char(ParseContext *context); 00152 00153 /* 00154 * Return the next octet and skip one position forward. 00155 * If already at the limit, return -1 and set the error flag. 00156 */ 00157 int parse_get_char(ParseContext *context); 00158 00159 /* 00160 * Return "length" octets starting at current position, and skip 00161 * that many octets forward. If that would go over the limit, 00162 * return NULL, do not skip, and set the error flag. 00163 */ 00164 Octstr *parse_get_octets(ParseContext *context, long length); 00165 00166 /* 00167 * Return the value of an uintvar-encoded integer at current 00168 * position, then skip over it. If there's an error in the 00169 * encoding, or if it would go past the limit, then return 0, 00170 * do not skip, and set the error flag. Since 0 is a valid 00171 * uintvar value, the error flag is only way to detect this error. 00172 */ 00173 unsigned long parse_get_uintvar(ParseContext *context); 00174 00175 /* 00176 * Look for a NUL-terminated string starting at the current offset, 00177 * and return it (without the NUL) as an Octstr. Skip forward past 00178 * the NUL. If there is no NUL, return NULL and set the error flag. 00179 */ 00180 Octstr *parse_get_nul_string(ParseContext *context); 00181 00182 /* 00183 * Look for a EOL-terminated line starting at the current offset, 00184 * and return it (without the EOL) as an Octstr. Skip forward past 00185 * the EOL. If there is no EOL, return NULL and set the error flag. 00186 */ 00187 Octstr *parse_get_line(ParseContext *context); 00188 00189 /* 00190 * Look for an Octstr block that is between two seperators defined by 00191 * the seperator Octstr. Return the Octstr between the seperators and 00192 * move the parsing context up before the last of the two seperators. 00193 * So the last seperator is next in the parsing scope. If there are 00194 * no two seperators in the parsing scope return NULL. 00195 */ 00196 Octstr *parse_get_seperated_block(ParseContext *context, Octstr *seperator); 00197 00198 /* 00199 * Return unparsed content. This should be used only after all 00200 * headers are parsed (and the headers and content are stored in 00201 * same octstr). 00202 */ 00203 Octstr *parse_get_rest(ParseContext *context); 00204 00205 #endif