/src/httrack/src/htsurlport.h
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 1998 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* File: bounded decimal and TCP port parsers, byte spans, and */ |
30 | | /* header parameters, shared by the engine, htsserver and */ |
31 | | /* proxytrack */ |
32 | | /* Author: Xavier Roche */ |
33 | | /* ------------------------------------------------------------ */ |
34 | | |
35 | | #ifndef HTSURLPORT_DEFH |
36 | | #define HTSURLPORT_DEFH |
37 | | |
38 | | #include "htsglobal.h" |
39 | | |
40 | | #include <string.h> |
41 | | |
42 | | /* Read the unsigned decimal digits at s into *out if the value is in [min, max] |
43 | | and return TRUE, or return FALSE and leave *out alone. *end (if not NULL) is |
44 | | set past the digits, and overflow is refused before it can happen. */ |
45 | | HTS_CHECK_RESULT hts_boolean hts_parse_llint(const char *s, const char **end, |
46 | | LLint min, LLint max, LLint *out); |
47 | | |
48 | | /* What hts_scan_llint() found. */ |
49 | | typedef enum { |
50 | | HTS_SCAN_REFUSED = -1, /* The number is signed or outside [min, max]. */ |
51 | | HTS_SCAN_NONE = 0, /* There is no number. */ |
52 | | HTS_SCAN_OK = 1 /* The number is in *out. */ |
53 | | } hts_scan_result; |
54 | | |
55 | | /* Skip white space, then hts_parse_llint() the number there. On HTS_SCAN_OK |
56 | | and HTS_SCAN_REFUSED, *s is moved past the number, so a caller can go on |
57 | | matching. On HTS_SCAN_NONE, *s is unchanged. */ |
58 | | hts_scan_result hts_scan_llint(const char **s, LLint min, LLint max, |
59 | | LLint *out); |
60 | | |
61 | | /* Parse the port text "a" (after the ':', up to the end of the string): TRUE |
62 | | and *port set for a bare decimal in 1..65535, else FALSE and *port left |
63 | | alone. Not sscanf("%d"), which range-checks nothing and wraps past INT_MAX. |
64 | | Its own file so proxytrack, which does not link the library, can share it. */ |
65 | | HTS_CHECK_RESULT hts_boolean hts_parse_url_port(const char *a, int *port); |
66 | | |
67 | | /* P points at LEN read-only bytes, which may hold NULs and lack an end NUL. */ |
68 | | typedef struct { |
69 | | const char *p; |
70 | | size_t len; |
71 | | } hts_span; |
72 | | |
73 | | /* Return the span over the LEN bytes at P. */ |
74 | 220k | static HTS_INLINE HTS_UNUSED hts_span hts_span_at(const char *p, size_t len) { |
75 | 220k | hts_span r; |
76 | | |
77 | 220k | r.p = p; |
78 | 220k | r.len = len; |
79 | 220k | return r; |
80 | 220k | } Line | Count | Source | 74 | 1.02k | static HTS_INLINE HTS_UNUSED hts_span hts_span_at(const char *p, size_t len) { | 75 | 1.02k | hts_span r; | 76 | | | 77 | 1.02k | r.p = p; | 78 | 1.02k | r.len = len; | 79 | 1.02k | return r; | 80 | 1.02k | } |
Line | Count | Source | 74 | 218k | static HTS_INLINE HTS_UNUSED hts_span hts_span_at(const char *p, size_t len) { | 75 | 218k | hts_span r; | 76 | | | 77 | 218k | r.p = p; | 78 | 218k | r.len = len; | 79 | 218k | return r; | 80 | 218k | } |
Line | Count | Source | 74 | 1.21k | static HTS_INLINE HTS_UNUSED hts_span hts_span_at(const char *p, size_t len) { | 75 | 1.21k | hts_span r; | 76 | | | 77 | 1.21k | r.p = p; | 78 | 1.21k | r.len = len; | 79 | 1.21k | return r; | 80 | 1.21k | } |
|
81 | | |
82 | | /* Return the span over the C string S, or an empty span when S is NULL. Pass a |
83 | | literal to a compare as hts_span_of("lit"). */ |
84 | 216k | static HTS_INLINE HTS_UNUSED hts_span hts_span_of(const char *s) { |
85 | 216k | if (s == NULL) |
86 | 0 | s = ""; |
87 | 216k | return hts_span_at(s, strlen(s)); |
88 | 216k | } Unexecuted instantiation: fuzz-sniff.c:hts_span_of Line | Count | Source | 84 | 216k | static HTS_INLINE HTS_UNUSED hts_span hts_span_of(const char *s) { | 85 | 216k | if (s == NULL) | 86 | 0 | s = ""; | 87 | 216k | return hts_span_at(s, strlen(s)); | 88 | 216k | } |
Unexecuted instantiation: htsurlport.c:hts_span_of |
89 | | |
90 | | /* Return the span over [FROM, TO), where TO must not precede FROM. */ |
91 | | hts_span hts_span_between(const char *from, const char *to); |
92 | | |
93 | | /* Return S without its first N bytes, or the empty span at its end when N |
94 | | exceeds its length. */ |
95 | 17.9k | static HTS_INLINE HTS_UNUSED hts_span hts_span_skip(hts_span s, size_t n) { |
96 | 17.9k | if (n > s.len) |
97 | 50 | n = s.len; |
98 | 17.9k | if (n != 0) { |
99 | 17.6k | s.p += n; |
100 | 17.6k | s.len -= n; |
101 | 17.6k | } |
102 | 17.9k | return s; |
103 | 17.9k | } Unexecuted instantiation: fuzz-sniff.c:hts_span_skip Line | Count | Source | 95 | 17.9k | static HTS_INLINE HTS_UNUSED hts_span hts_span_skip(hts_span s, size_t n) { | 96 | 17.9k | if (n > s.len) | 97 | 50 | n = s.len; | 98 | 17.9k | if (n != 0) { | 99 | 17.6k | s.p += n; | 100 | 17.6k | s.len -= n; | 101 | 17.6k | } | 102 | 17.9k | return s; | 103 | 17.9k | } |
Unexecuted instantiation: htsurlport.c:hts_span_skip |
104 | | |
105 | | /* Return the first byte of S as an unsigned char, or -1 when S is empty. */ |
106 | 0 | static HTS_INLINE HTS_UNUSED int hts_span_first(hts_span s) { |
107 | 0 | return s.len != 0 ? (unsigned char) s.p[0] : -1; |
108 | 0 | } Unexecuted instantiation: fuzz-sniff.c:hts_span_first Unexecuted instantiation: htssniff.c:hts_span_first Unexecuted instantiation: htsurlport.c:hts_span_first |
109 | | |
110 | | /* Return the last byte of S as an unsigned char, or -1 when S is empty. */ |
111 | 0 | static HTS_INLINE HTS_UNUSED int hts_span_last(hts_span s) { |
112 | 0 | return s.len != 0 ? (unsigned char) s.p[s.len - 1] : -1; |
113 | 0 | } Unexecuted instantiation: fuzz-sniff.c:hts_span_last Unexecuted instantiation: htssniff.c:hts_span_last Unexecuted instantiation: htsurlport.c:hts_span_last |
114 | | |
115 | | /* Return the index of the first C in S, or S.len when S holds none. */ |
116 | 0 | static HTS_INLINE HTS_UNUSED size_t hts_span_find(hts_span s, char c) { |
117 | 0 | const char *const at = |
118 | 0 | s.len != 0 ? (const char *) memchr(s.p, c, s.len) : NULL; |
119 | |
|
120 | 0 | return at != NULL ? (size_t) (at - s.p) : s.len; |
121 | 0 | } Unexecuted instantiation: fuzz-sniff.c:hts_span_find Unexecuted instantiation: htssniff.c:hts_span_find Unexecuted instantiation: htsurlport.c:hts_span_find |
122 | | |
123 | | /* Return the index of the first byte of S found in SET, or S.len. SET never |
124 | | matches a NUL. */ |
125 | | static HTS_INLINE HTS_UNUSED size_t hts_span_find_any(hts_span s, |
126 | 0 | const char *set) { |
127 | 0 | size_t i; |
128 | |
|
129 | 0 | for (i = 0; i < s.len; i++) |
130 | 0 | if (s.p[i] != '\0' && strchr(set, s.p[i]) != NULL) |
131 | 0 | break; |
132 | 0 | return i; |
133 | 0 | } Unexecuted instantiation: fuzz-sniff.c:hts_span_find_any Unexecuted instantiation: htssniff.c:hts_span_find_any Unexecuted instantiation: htsurlport.c:hts_span_find_any |
134 | | |
135 | | /* Return the bytes of *REST before its first byte found in SET, and move |
136 | | *REST to start at that byte, or to its end when there is none. */ |
137 | | static HTS_INLINE HTS_UNUSED hts_span hts_span_take_until(hts_span *rest, |
138 | 0 | const char *set) { |
139 | 0 | const hts_span head = hts_span_at(rest->p, hts_span_find_any(*rest, set)); |
140 | 0 |
|
141 | 0 | *rest = hts_span_skip(*rest, head.len); |
142 | 0 | return head; |
143 | 0 | } Unexecuted instantiation: fuzz-sniff.c:hts_span_take_until Unexecuted instantiation: htssniff.c:hts_span_take_until Unexecuted instantiation: htsurlport.c:hts_span_take_until |
144 | | |
145 | | /* Is S present? An absent hts_url_view part is not, but an empty one is. */ |
146 | | hts_boolean hts_span_present(hts_span s); |
147 | | |
148 | | /* Return S without the leading bytes found in LEFT and the trailing ones found |
149 | | in RIGHT. A set may be empty, and it never matches a NUL. */ |
150 | | hts_span hts_span_trim(hts_span s, const char *left, const char *right); |
151 | | |
152 | | /* Split S at its first C into *HEAD and *TAIL, C in neither. Return HTS_FALSE |
153 | | and leave both unchanged when S holds no C. */ |
154 | | HTS_CHECK_RESULT hts_boolean hts_span_split(hts_span s, char c, hts_span *head, |
155 | | hts_span *tail); |
156 | | |
157 | | /* Read the field of *REST up to SEP into *FIELD and shrink *REST past that SEP. |
158 | | Return FALSE once *REST is empty, so "" yields nothing and "a," yields only |
159 | | "a". After a TRUE return, [field->p, rest->p) is the field plus its SEP. */ |
160 | | HTS_CHECK_RESULT hts_boolean hts_span_next_in(hts_span *rest, char sep, |
161 | | hts_span *field); |
162 | | |
163 | | /* Does S hold no bytes? Its pointer does not matter. */ |
164 | | hts_boolean hts_span_empty(hts_span s); |
165 | | |
166 | | /* Do A and B hold the same bytes? */ |
167 | | hts_boolean hts_span_eq(hts_span a, hts_span b); |
168 | | |
169 | | /* Do A and B hold the same bytes once A-Z are folded to a-z? */ |
170 | | hts_boolean hts_span_eq_ci(hts_span a, hts_span b); |
171 | | |
172 | | /* Does S start with the bytes of PREFIX? */ |
173 | | hts_boolean hts_span_starts_with(hts_span s, hts_span prefix); |
174 | | |
175 | | /* Does S start with PREFIX once A-Z are folded to a-z? */ |
176 | | hts_boolean hts_span_starts_with_ci(hts_span s, hts_span prefix); |
177 | | |
178 | | /* Does S end with the bytes of SUFFIX? */ |
179 | | hts_boolean hts_span_ends_with(hts_span s, hts_span suffix); |
180 | | |
181 | | /* Does S end with SUFFIX once A-Z are folded to a-z? */ |
182 | | hts_boolean hts_span_ends_with_ci(hts_span s, hts_span suffix); |
183 | | |
184 | | /* The words of a list for hts_span_index, then the NULL it ends at. */ |
185 | | #define HTS_SPAN_WORDS(...) __VA_ARGS__, NULL |
186 | | |
187 | | /* Return the index of S in the NULL-terminated WORDS, or -1. */ |
188 | | HTS_CHECK_RESULT int hts_span_index(hts_span s, const char *const *words); |
189 | | |
190 | | /* hts_span_index with A-Z compared as a-z. */ |
191 | | HTS_CHECK_RESULT int hts_span_index_ci(hts_span s, const char *const *words); |
192 | | |
193 | | /* Copy S into DST as a terminated string, clipped to SIZE - 1 bytes. Return |
194 | | FALSE if it was clipped, or if SIZE is 0, which writes nothing. */ |
195 | | HTS_CHECK_RESULT hts_boolean hts_span_copy(hts_span s, char *dst, size_t size); |
196 | | |
197 | | /* Does the SEP-separated LIST hold TOKEN, A-Z compared as a-z? Each item is |
198 | | trimmed of " \t\n\v\f\r" first, and an empty item never matches. */ |
199 | | HTS_CHECK_RESULT hts_boolean hts_span_list_has(hts_span list, char sep, |
200 | | hts_span token); |
201 | | |
202 | | /* Read the next SEP-separated field of *REST as NAME=VALUE, split at its first |
203 | | '=' and not trimmed. VALUE is absent (see hts_span_present) when the field |
204 | | has no '='. Return FALSE once *REST is empty, as hts_span_next_in does. */ |
205 | | HTS_CHECK_RESULT hts_boolean hts_param_next(hts_span *rest, char sep, |
206 | | hts_span *name, hts_span *value); |
207 | | |
208 | | /* One line of a header block. NAME is empty on the status line, a fold, or a |
209 | | line with no ':'. */ |
210 | | typedef struct { |
211 | | hts_span line; /* without its LF and one CR */ |
212 | | hts_span name; /* without the blanks before the colon */ |
213 | | hts_span value; /* trimmed of SP and HT */ |
214 | | } hts_header_field; |
215 | | |
216 | | /* Split LINE at its first ':' into *NAME and *VALUE as hts_header_field has |
217 | | them. Both are empty for a fold (LINE opens with SP or HT) or a line with no |
218 | | ':'. */ |
219 | | void hts_header_split(hts_span line, hts_span *name, hts_span *value); |
220 | | |
221 | | /* Read the next line of the header block *REST, ended by LF or CRLF, into *F. |
222 | | Return FALSE at the end of *REST or at the blank line ending the block. */ |
223 | | HTS_CHECK_RESULT hts_boolean hts_header_next(hts_span *rest, |
224 | | hts_header_field *f); |
225 | | |
226 | | /* A malloct'ed copy of S as a C string, or NULL when out of memory. */ |
227 | | HTS_OWNED char *hts_span_dup(hts_span s); |
228 | | |
229 | | /* Is S one or more decimal digits? */ |
230 | | HTS_CHECK_RESULT hts_boolean hts_span_is_digits(hts_span s); |
231 | | |
232 | | /* Is all of S decimal digits, with a value in [MIN, MAX]? On TRUE the value is |
233 | | in *OUT. An empty span, signs, blanks and overflow are refused, and *OUT is |
234 | | then untouched. */ |
235 | | HTS_CHECK_RESULT hts_boolean hts_span_parse_llint(hts_span s, LLint min, |
236 | | LLint max, LLint *out); |
237 | | |
238 | | /* Return a pointer to the '.' that starts the extension of PATH's last |
239 | | segment, or NULL. A byte of SEPS ends a segment, '?' is a plain byte, and a |
240 | | '.' at PATH[0] counts. */ |
241 | | HTS_CHECK_RESULT const char *hts_path_ext_dot(const char *path, |
242 | | const char *seps); |
243 | | |
244 | | /* Return a span of the text after FIL's last '.' before the first '?', or an |
245 | | empty span. The search crosses directories, so "/v1.2/page" gives "2/page". |
246 | | */ |
247 | | hts_span hts_fil_ext(const char *fil); |
248 | | |
249 | | /* Find parameter NAME (exact, any case) in a header VALUE whose fields are |
250 | | split by any byte of SEPS, as in "type; a=1; NAME=\"v\"". The first NAME |
251 | | wins. A quoted value unescapes only \" and \\, and a missing end quote |
252 | | runs to the end. Return TRUE with the value in OUT, or FALSE when NAME is |
253 | | absent or its value does not fit SIZE - 1 bytes. FALSE leaves OUT empty, |
254 | | unless SIZE is 0, which writes nothing. */ |
255 | | HTS_CHECK_RESULT hts_boolean hts_header_param(const char *value, |
256 | | const char *seps, |
257 | | const char *name, char *out, |
258 | | size_t size); |
259 | | |
260 | | /* Return the static reason phrase of HTTP status CODE, or NULL if unknown. |
261 | | It sits here for proxytrack, and HTSEXT_API would break its MSVC build. */ |
262 | | HTS_CHECK_RESULT const char *hts_status_reason(int code); |
263 | | |
264 | | #endif |