Kannel: Open Source WAP and SMS gateway  svn-r5335
parse.h File Reference

Go to the source code of this file.

Typedefs

typedef struct context ParseContext
 

Functions

ParseContextparse_context_create (Octstr *str)
 
void parse_context_destroy (ParseContext *context)
 
int parse_error (ParseContext *context)
 
void parse_clear_error (ParseContext *context)
 
void parse_set_error (ParseContext *context)
 
int parse_limit (ParseContext *context, long length)
 
int parse_pop_limit (ParseContext *context)
 
long parse_octets_left (ParseContext *context)
 
int parse_skip (ParseContext *context, long count)
 
void parse_skip_to_limit (ParseContext *context)
 
int parse_skip_to (ParseContext *context, long pos)
 
int parse_peek_char (ParseContext *context)
 
int parse_get_char (ParseContext *context)
 
Octstrparse_get_octets (ParseContext *context, long length)
 
unsigned long parse_get_uintvar (ParseContext *context)
 
Octstrparse_get_nul_string (ParseContext *context)
 
Octstrparse_get_line (ParseContext *context)
 
Octstrparse_get_seperated_block (ParseContext *context, Octstr *seperator)
 
Octstrparse_get_rest (ParseContext *context)
 

Typedef Documentation

◆ ParseContext

typedef struct context ParseContext

Definition at line 79 of file parse.h.

Function Documentation

◆ parse_clear_error()

void parse_clear_error ( ParseContext context)

Definition at line 107 of file parse.c.

References context::error, and gw_assert().

Referenced by unpack_range_value().

108 {
109  gw_assert(context != NULL);
110 
111  context->error = 0;
112 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
int error
Definition: parse.c:71

◆ parse_context_create()

ParseContext* parse_context_create ( Octstr str)

Definition at line 74 of file parse.c.

References context::data, context::error, context::limit, context::limit_stack, octstr_len(), and context::pos.

Referenced by mime_decompile(), mime_entity_body(), mime_something_to_entity(), radius_pdu_unpack(), and wsp_headers_unpack().

75 {
76  ParseContext *result;
77 
78  result = gw_malloc(sizeof(*result));
79  result->data = str;
80  result->pos = 0;
81  result->limit = octstr_len(str);
82  result->limit_stack = NULL;
83  result->error = 0;
84 
85  return result;
86 }
Octstr * data
Definition: parse.c:67
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
int error
Definition: parse.c:71
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
List * limit_stack
Definition: parse.c:70

◆ parse_context_destroy()

void parse_context_destroy ( ParseContext context)

Definition at line 88 of file parse.c.

References gw_assert(), gwlist_destroy(), gwlist_extract_first(), gwlist_len(), and context::limit_stack.

Referenced by mime_entity_body(), mime_something_to_entity(), radius_pdu_unpack(), and wsp_headers_unpack().

89 {
90  gw_assert(context != NULL);
91 
92  if (context->limit_stack) {
93  while (gwlist_len(context->limit_stack) > 0)
96  }
97  gw_free(context);
98 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long gwlist_len(List *list)
Definition: list.c:166
void * gwlist_extract_first(List *list)
Definition: list.c:305
List * limit_stack
Definition: parse.c:70
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

◆ parse_error()

◆ parse_get_char()

int parse_get_char ( ParseContext context)

Definition at line 218 of file parse.c.

References context::data, context::error, gw_assert(), context::limit, octstr_get_char(), and context::pos.

Referenced by radius_pdu_unpack(), unpack_disposition(), unpack_encoding_version(), unpack_multi_octet_integer(), unpack_q_value(), unpack_range_value(), unpack_retry_after(), wsp_field_value(), wsp_headers_unpack(), wsp_secondary_field_value(), wsp_unpack_date_value(), and wsp_unpack_integer_value().

219 {
220  gw_assert(context != NULL);
221 
222  if (context->pos == context->limit) {
223  context->error = 1;
224  return -1;
225  }
226 
227  return octstr_get_char(context->data, context->pos++);
228 }
Octstr * data
Definition: parse.c:67
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
int error
Definition: parse.c:71
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406

◆ parse_get_line()

Octstr* parse_get_line ( ParseContext context)

Definition at line 282 of file parse.c.

References context::data, context::error, gw_assert(), context::limit, octstr_copy, octstr_search_char(), octstr_strip_crlfs(), and context::pos.

Referenced by mime_something_to_entity(), and read_mime_headers().

283 {
284  Octstr *result;
285  long pos;
286 
287  gw_assert(context != NULL);
288 
289  pos = octstr_search_char(context->data, '\n', context->pos);
290  if (pos < 0 || pos >= context->limit) {
291  context->error = 1;
292  return NULL;
293  }
294 
295  result = octstr_copy(context->data, context->pos, pos - context->pos);
296  context->pos = pos + 1;
297 
298  octstr_strip_crlfs(result);
299 
300  return result;
301 }
Octstr * data
Definition: parse.c:67
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1012
int error
Definition: parse.c:71
void octstr_strip_crlfs(Octstr *text)
Definition: octstr.c:1378
Definition: octstr.c:118

◆ parse_get_nul_string()

Octstr* parse_get_nul_string ( ParseContext context)

Definition at line 263 of file parse.c.

References context::data, context::error, gw_assert(), context::limit, octstr_copy, octstr_search_char(), and context::pos.

Referenced by proxy_unpack_credentials(), unpack_accept_language_general_form(), unpack_cache_directive(), unpack_challenge(), unpack_credentials(), unpack_field_name(), unpack_parameter(), unpack_warning_value(), wsp_unpack_accept_charset_general_form(), wsp_unpack_accept_general_form(), wsp_unpack_app_header(), and wsp_unpack_well_known_field().

264 {
265  Octstr *result;
266  long pos;
267 
268  gw_assert(context != NULL);
269 
271  if (pos < 0 || pos >= context->limit) {
272  context->error = 1;
273  return NULL;
274  }
275 
276  result = octstr_copy(context->data, context->pos, pos - context->pos);
277  context->pos = pos + 1;
278 
279  return result;
280 }
Octstr * data
Definition: parse.c:67
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1012
int error
Definition: parse.c:71
Definition: octstr.c:118

◆ parse_get_octets()

Octstr* parse_get_octets ( ParseContext context,
long  length 
)

Definition at line 230 of file parse.c.

References context::data, context::error, gw_assert(), context::limit, octstr_copy, and context::pos.

Referenced by mime_decompile(), radius_pdu_unpack(), and wsp_unpack_well_known_field().

231 {
232  Octstr *result;
233 
234  gw_assert(context != NULL);
235 
236  if (context->pos + length > context->limit) {
237  context->error = 1;
238  return NULL;
239  }
240 
241  result = octstr_copy(context->data, context->pos, length);
242  context->pos += length;
243  return result;
244 }
Octstr * data
Definition: parse.c:67
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
int error
Definition: parse.c:71
Definition: octstr.c:118

◆ parse_get_rest()

Octstr* parse_get_rest ( ParseContext context)

Definition at line 329 of file parse.c.

References context::data, gw_assert(), octstr_delete(), octstr_duplicate, and context::pos.

Referenced by mime_entity_body(), and mime_something_to_entity().

330 {
331  Octstr *rest;
332 
333  gw_assert(context != NULL);
334 
336  rest = octstr_duplicate(context->data);
337 
338  return rest;
339 }
Octstr * data
Definition: parse.c:67
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1527
#define octstr_duplicate(ostr)
Definition: octstr.h:187
Definition: octstr.c:118

◆ parse_get_seperated_block()

Octstr* parse_get_seperated_block ( ParseContext context,
Octstr seperator 
)

Definition at line 303 of file parse.c.

References context::data, context::error, gw_assert(), context::limit, octstr_copy, octstr_len(), octstr_search(), and context::pos.

Referenced by mime_something_to_entity().

304 {
305  Octstr *result;
306  long spos, epos;
307 
308  gw_assert(context != NULL);
309  gw_assert(seperator != NULL);
310 
311  spos = octstr_search(context->data, seperator, context->pos);
312  if (spos < 0 || spos >= context->limit) {
313  context->error = 1;
314  return NULL;
315  }
316  epos = octstr_search(context->data, seperator, spos + octstr_len(seperator));
317  if (epos < 0 || epos >= context->limit) {
318  context->error = 1;
319  return NULL;
320  }
321 
322  spos = spos + octstr_len(seperator);
323  result = octstr_copy(context->data, spos, epos - spos);
324  context->pos = epos;
325 
326  return result;
327 }
Octstr * data
Definition: parse.c:67
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
long octstr_search(const Octstr *haystack, const Octstr *needle, long pos)
Definition: octstr.c:1070
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
int error
Definition: parse.c:71
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118

◆ parse_get_uintvar()

unsigned long parse_get_uintvar ( ParseContext context)

Definition at line 246 of file parse.c.

References context::data, context::error, gw_assert(), context::limit, octstr_extract_uintvar(), and context::pos.

Referenced by mime_decompile(), unpack_range_value(), wsp_field_value(), and wsp_secondary_field_value().

247 {
248  long pos;
249  unsigned long value;
250 
251  gw_assert(context != NULL);
252 
253  pos = octstr_extract_uintvar(context->data, &value, context->pos);
254  if (pos < 0 || pos > context->limit) {
255  context->error = 1;
256  return 0;
257  }
258 
259  context->pos = pos;
260  return value;
261 }
Octstr * data
Definition: parse.c:67
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
int error
Definition: parse.c:71
long octstr_extract_uintvar(Octstr *ostr, unsigned long *value, long pos)
Definition: octstr.c:1954

◆ parse_limit()

int parse_limit ( ParseContext context,
long  length 
)

Definition at line 121 of file parse.c.

References context::error, gw_assert(), gwlist_create, gwlist_insert(), context::limit, context::limit_stack, and context::pos.

Referenced by wsp_field_value().

122 {
123  long *elem;
124 
125  gw_assert(context != NULL);
126 
127  if (context->pos + length > context->limit) {
128  context->error = 1;
129  return -1;
130  }
131 
132  if (context->limit_stack == NULL)
134 
135  elem = gw_malloc(sizeof(*elem));
136  *elem = context->limit;
137  gwlist_insert(context->limit_stack, 0, elem);
138  context->limit = context->pos + length;
139  return 0;
140 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
int error
Definition: parse.c:71
void gwlist_insert(List *list, long pos, void *item)
Definition: list.c:214
#define gwlist_create()
Definition: list.h:136
List * limit_stack
Definition: parse.c:70

◆ parse_octets_left()

long parse_octets_left ( ParseContext context)

Definition at line 159 of file parse.c.

References gw_assert(), context::limit, and context::pos.

Referenced by mime_decompile(), unpack_broken_parameters(), unpack_cache_directive(), unpack_challenge(), unpack_optional_q_value(), wsp_headers_unpack(), wsp_unpack_all_parameters(), and wsp_unpack_well_known_field().

160 {
161  gw_assert(context != NULL);
162 
163  return context->limit - context->pos;
164 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69

◆ parse_peek_char()

int parse_peek_char ( ParseContext context)

Definition at line 206 of file parse.c.

References context::data, context::error, gw_assert(), context::limit, octstr_get_char(), and context::pos.

Referenced by proxy_unpack_credentials(), unpack_challenge(), and unpack_credentials().

207 {
208  gw_assert(context != NULL);
209 
210  if (context->pos == context->limit) {
211  context->error = 1;
212  return -1;
213  }
214 
216 }
Octstr * data
Definition: parse.c:67
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
int error
Definition: parse.c:71
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:406

◆ parse_pop_limit()

int parse_pop_limit ( ParseContext context)

Definition at line 142 of file parse.c.

References context::error, gw_assert(), gwlist_extract_first(), gwlist_len(), context::limit, and context::limit_stack.

Referenced by wsp_skip_field_value(), and wsp_unpack_well_known_field().

143 {
144  long *elem;
145 
146  gw_assert(context != NULL);
147 
148  if (context->limit_stack == NULL || gwlist_len(context->limit_stack) == 0) {
149  context->error = 1;
150  return -1;
151  }
152 
154  context->limit = *elem;
155  gw_free(elem);
156  return 0;
157 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long gwlist_len(List *list)
Definition: list.c:166
long limit
Definition: parse.c:69
void * gwlist_extract_first(List *list)
Definition: list.c:305
int error
Definition: parse.c:71
List * limit_stack
Definition: parse.c:70

◆ parse_set_error()

void parse_set_error ( ParseContext context)

Definition at line 114 of file parse.c.

References context::error, and gw_assert().

Referenced by unpack_parameter().

115 {
116  gw_assert(context != NULL);
117 
118  context->error = 1;
119 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
int error
Definition: parse.c:71

◆ parse_skip()

int parse_skip ( ParseContext context,
long  count 
)

Definition at line 166 of file parse.c.

References context::error, gw_assert(), context::limit, and context::pos.

Referenced by proxy_unpack_credentials(), radius_pdu_unpack(), unpack_challenge(), unpack_credentials(), wsp_field_value(), wsp_headers_unpack(), wsp_secondary_field_value(), and wsp_unpack_well_known_field().

167 {
168  gw_assert(context != NULL);
169 
170  if (context->pos + count > context->limit) {
171  context->pos = context->limit;
172  context->error = 1;
173  return -1;
174  }
175 
176  context->pos += count;
177  return 0;
178 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
int error
Definition: parse.c:71

◆ parse_skip_to()

int parse_skip_to ( ParseContext context,
long  pos 
)

Definition at line 187 of file parse.c.

References context::error, gw_assert(), context::limit, and context::pos.

188 {
189  gw_assert(context != NULL);
190 
191  if (pos < 0) {
192  context->error = 1;
193  return -1;
194  }
195 
196  if (pos > context->limit) {
197  context->pos = context->limit;
198  context->error = 1;
199  return -1;
200  }
201 
202  context->pos = pos;
203  return 0;
204 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
int error
Definition: parse.c:71

◆ parse_skip_to_limit()

void parse_skip_to_limit ( ParseContext context)

Definition at line 180 of file parse.c.

References gw_assert(), context::limit, and context::pos.

Referenced by unpack_parameter(), wsp_skip_field_value(), and wsp_unpack_well_known_field().

181 {
182  gw_assert(context != NULL);
183 
184  context->pos = context->limit;
185 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Definition: parse.c:65
long pos
Definition: parse.c:68
long limit
Definition: parse.c:69
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.