1 : #define VSTR_PARSE_NETSTR_C
2 : /*
3 : * Copyright (C) 1999, 2000, 2001, 2002, 2003 James Antill
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2 of the License, or (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with this library; if not, write to the Free Software
17 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 : *
19 : * email: james@and.org
20 : */
21 : /* netstr (http://cr.yp.to/proto/netstrings.txt). This is basically
22 : * <num ':' data ','>
23 : * where
24 : * num is an ascii number (base 10)
25 : * data is 8 bit binary data (Ie. any value 0 - 255 is allowed).
26 : */
27 : /* netstr2 like netstr (http://cr.yp.to/proto/netstrings.txt)
28 : * but allows leading '0' characters */
29 :
30 : #include "main.h"
31 :
32 : static size_t vstr__parse_netstr(const Vstr_base *base, size_t pos, size_t len,
33 : size_t *ret_pos, size_t *ret_data_len,
34 : int netstr2)
35 6382 : {
36 6382 : unsigned int flags = VSTR_FLAG_PARSE_NUM_OVERFLOW;
37 6382 : size_t num_len = 0;
38 6382 : size_t ret_len = 0;
39 6382 : size_t dummy_ret_pos = 0;
40 6382 : size_t dummy_ret_data_len = 0;
41 :
42 6382 : if (!ret_pos)
43 70 : ret_pos = &dummy_ret_pos;
44 6382 : if (!ret_data_len)
45 70 : ret_data_len = &dummy_ret_data_len;
46 :
47 6382 : *ret_pos = 0;
48 6382 : *ret_data_len = 0;
49 :
50 6382 : if (!netstr2)
51 6337 : flags |= VSTR_FLAG_PARSE_NUM_NO_BEG_ZERO;
52 6382 : ret_len = vstr_parse_ulong(base, pos, len, 10 | flags, &num_len, NULL);
53 :
54 6382 : if (!num_len)
55 74 : return (0);
56 :
57 6308 : if (num_len == len)
58 10 : return (0);
59 :
60 6298 : if (vstr_export_chr(base, pos + num_len) != VSTR__ASCII_COLON())
61 10 : return (0);
62 :
63 6288 : *ret_pos = pos + num_len + 1;
64 6288 : *ret_data_len = ret_len;
65 :
66 6288 : ret_len += (num_len + 2); /* colon and comma */
67 6288 : if (ret_len > len)
68 23 : return (0);
69 :
70 6265 : if (vstr_export_chr(base, pos - 1 + ret_len) != VSTR__ASCII_COMMA())
71 10 : return (0);
72 :
73 6255 : return (ret_len);
74 : }
75 :
76 : size_t vstr_parse_netstr2(const Vstr_base *base, size_t pos, size_t len,
77 : size_t *ret_pos, size_t *ret_len)
78 45 : {
79 45 : return (vstr__parse_netstr(base, pos, len, ret_pos, ret_len, TRUE));
80 : }
81 :
82 : size_t vstr_parse_netstr(const Vstr_base *base, size_t pos, size_t len,
83 : size_t *ret_pos, size_t *ret_len)
84 6337 : {
85 6337 : return (vstr__parse_netstr(base, pos, len, ret_pos, ret_len, FALSE));
86 : }
|