]>
Commit | Line | Data |
---|---|---|
6c139d6e AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
d48c6a7d | 3 | // $Id: strutl.cc,v 1.48 2003/07/18 14:15:11 mdz Exp $ |
6c139d6e AL |
4 | /* ###################################################################### |
5 | ||
b2e465d6 | 6 | String Util - Some useful string functions. |
6c139d6e | 7 | |
b2e465d6 AL |
8 | These have been collected from here and there to do all sorts of useful |
9 | things to strings. They are useful in file parsers, URI handlers and | |
24231681 | 10 | especially in APT methods. |
6c139d6e AL |
11 | |
12 | This source is placed in the Public Domain, do with it what you will | |
24231681 | 13 | It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca> |
6c139d6e AL |
14 | |
15 | ##################################################################### */ | |
16 | /*}}}*/ | |
17 | // Includes /*{{{*/ | |
ea542140 DK |
18 | #include <config.h> |
19 | ||
cdcc6d34 | 20 | #include <apt-pkg/strutl.h> |
7049d16d | 21 | #include <apt-pkg/fileutl.h> |
b2e465d6 | 22 | #include <apt-pkg/error.h> |
0a8a80e5 | 23 | |
9febc2b2 DK |
24 | #include <algorithm> |
25 | #include <iomanip> | |
26 | #include <locale> | |
27 | #include <sstream> | |
28 | #include <string> | |
29 | #include <vector> | |
30 | ||
453b82a3 DK |
31 | #include <stddef.h> |
32 | #include <stdlib.h> | |
33 | #include <time.h> | |
6c139d6e AL |
34 | #include <ctype.h> |
35 | #include <string.h> | |
36 | #include <stdio.h> | |
2b154e53 | 37 | #include <unistd.h> |
b2e465d6 | 38 | #include <regex.h> |
b0db36b1 | 39 | #include <errno.h> |
b2e465d6 | 40 | #include <stdarg.h> |
a52f938b | 41 | #include <iconv.h> |
0db4a45b | 42 | |
ea542140 | 43 | #include <apti18n.h> |
6c139d6e | 44 | /*}}}*/ |
453b82a3 DK |
45 | using namespace std; |
46 | ||
65dbd5a1 MV |
47 | // Strip - Remove white space from the front and back of a string /*{{{*/ |
48 | // --------------------------------------------------------------------- | |
49 | namespace APT { | |
50 | namespace String { | |
b5787388 | 51 | std::string Strip(const std::string &str) |
65dbd5a1 | 52 | { |
b5787388 DK |
53 | // ensure we have at least one character |
54 | if (str.empty() == true) | |
55 | return str; | |
56 | ||
57 | char const * const s = str.c_str(); | |
58 | size_t start = 0; | |
59 | for (; isspace(s[start]) != 0; ++start) | |
60 | ; // find the first not-space | |
61 | ||
62 | // string contains only whitespaces | |
63 | if (s[start] == '\0') | |
65dbd5a1 | 64 | return ""; |
b5787388 DK |
65 | |
66 | size_t end = str.length() - 1; | |
67 | for (; isspace(s[end]) != 0; --end) | |
68 | ; // find the last not-space | |
69 | ||
70 | return str.substr(start, end - start + 1); | |
65dbd5a1 | 71 | } |
cf993341 MV |
72 | |
73 | bool Endswith(const std::string &s, const std::string &end) | |
74 | { | |
75 | if (end.size() > s.size()) | |
76 | return false; | |
c1f961ec | 77 | return (s.compare(s.size() - end.size(), end.size(), end) == 0); |
cf993341 MV |
78 | } |
79 | ||
a5bb5e1e MV |
80 | bool Startswith(const std::string &s, const std::string &start) |
81 | { | |
82 | if (start.size() > s.size()) | |
83 | return false; | |
c1f961ec | 84 | return (s.compare(0, start.size(), start) == 0); |
a5bb5e1e MV |
85 | } |
86 | ||
65dbd5a1 MV |
87 | } |
88 | } | |
89 | /*}}}*/ | |
a52f938b OS |
90 | // UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/ |
91 | // --------------------------------------------------------------------- | |
92 | /* This is handy to use before display some information for enduser */ | |
93 | bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest) | |
94 | { | |
95 | iconv_t cd; | |
96 | const char *inbuf; | |
b39c1859 MV |
97 | char *inptr, *outbuf; |
98 | size_t insize, bufsize; | |
99 | dest->clear(); | |
100 | ||
a52f938b OS |
101 | cd = iconv_open(codeset, "UTF-8"); |
102 | if (cd == (iconv_t)(-1)) { | |
103 | // Something went wrong | |
104 | if (errno == EINVAL) | |
105 | _error->Error("conversion from 'UTF-8' to '%s' not available", | |
106 | codeset); | |
107 | else | |
108 | perror("iconv_open"); | |
109 | ||
a52f938b OS |
110 | return false; |
111 | } | |
112 | ||
b39c1859 | 113 | insize = bufsize = orig.size(); |
a52f938b OS |
114 | inbuf = orig.data(); |
115 | inptr = (char *)inbuf; | |
b39c1859 MV |
116 | outbuf = new char[bufsize]; |
117 | size_t lastError = -1; | |
a52f938b | 118 | |
1f99b6d3 DK |
119 | while (insize != 0) |
120 | { | |
b39c1859 MV |
121 | char *outptr = outbuf; |
122 | size_t outsize = bufsize; | |
1f99b6d3 | 123 | size_t const err = iconv(cd, &inptr, &insize, &outptr, &outsize); |
b39c1859 | 124 | dest->append(outbuf, outptr - outbuf); |
1f99b6d3 DK |
125 | if (err == (size_t)(-1)) |
126 | { | |
b39c1859 MV |
127 | switch (errno) |
128 | { | |
129 | case EILSEQ: | |
130 | insize--; | |
131 | inptr++; | |
132 | // replace a series of unknown multibytes with a single "?" | |
133 | if (lastError != insize) { | |
134 | lastError = insize - 1; | |
135 | dest->append("?"); | |
136 | } | |
137 | break; | |
138 | case EINVAL: | |
139 | insize = 0; | |
140 | break; | |
141 | case E2BIG: | |
142 | if (outptr == outbuf) | |
143 | { | |
144 | bufsize *= 2; | |
145 | delete[] outbuf; | |
146 | outbuf = new char[bufsize]; | |
147 | } | |
148 | break; | |
149 | } | |
1f99b6d3 DK |
150 | } |
151 | } | |
a52f938b | 152 | |
a52f938b OS |
153 | delete[] outbuf; |
154 | ||
155 | iconv_close(cd); | |
156 | ||
157 | return true; | |
158 | } | |
159 | /*}}}*/ | |
6c139d6e AL |
160 | // strstrip - Remove white space from the front and back of a string /*{{{*/ |
161 | // --------------------------------------------------------------------- | |
162 | /* This is handy to use when parsing a file. It also removes \n's left | |
163 | over from fgets and company */ | |
164 | char *_strstrip(char *String) | |
165 | { | |
166 | for (;*String != 0 && (*String == ' ' || *String == '\t'); String++); | |
167 | ||
168 | if (*String == 0) | |
169 | return String; | |
4fb400a6 MV |
170 | return _strrstrip(String); |
171 | } | |
172 | /*}}}*/ | |
173 | // strrstrip - Remove white space from the back of a string /*{{{*/ | |
174 | // --------------------------------------------------------------------- | |
175 | char *_strrstrip(char *String) | |
176 | { | |
6c139d6e AL |
177 | char *End = String + strlen(String) - 1; |
178 | for (;End != String - 1 && (*End == ' ' || *End == '\t' || *End == '\n' || | |
179 | *End == '\r'); End--); | |
180 | End++; | |
181 | *End = 0; | |
182 | return String; | |
d3e8fbb3 | 183 | } |
6c139d6e AL |
184 | /*}}}*/ |
185 | // strtabexpand - Converts tabs into 8 spaces /*{{{*/ | |
186 | // --------------------------------------------------------------------- | |
187 | /* */ | |
188 | char *_strtabexpand(char *String,size_t Len) | |
189 | { | |
190 | for (char *I = String; I != I + Len && *I != 0; I++) | |
191 | { | |
192 | if (*I != '\t') | |
193 | continue; | |
194 | if (I + 8 > String + Len) | |
195 | { | |
196 | *I = 0; | |
197 | return String; | |
198 | } | |
199 | ||
200 | /* Assume the start of the string is 0 and find the next 8 char | |
201 | division */ | |
202 | int Len; | |
203 | if (String == I) | |
204 | Len = 1; | |
205 | else | |
206 | Len = 8 - ((String - I) % 8); | |
207 | Len -= 2; | |
208 | if (Len <= 0) | |
209 | { | |
210 | *I = ' '; | |
211 | continue; | |
212 | } | |
213 | ||
214 | memmove(I + Len,I + 1,strlen(I) + 1); | |
215 | for (char *J = I; J + Len != I; *I = ' ', I++); | |
216 | } | |
217 | return String; | |
218 | } | |
219 | /*}}}*/ | |
220 | // ParseQuoteWord - Parse a single word out of a string /*{{{*/ | |
221 | // --------------------------------------------------------------------- | |
222 | /* This grabs a single word, converts any % escaped characters to their | |
223 | proper values and advances the pointer. Double quotes are understood | |
7834cb57 AL |
224 | and striped out as well. This is for URI/URL parsing. It also can |
225 | understand [] brackets.*/ | |
6c139d6e AL |
226 | bool ParseQuoteWord(const char *&String,string &Res) |
227 | { | |
228 | // Skip leading whitespace | |
229 | const char *C = String; | |
230 | for (;*C != 0 && *C == ' '; C++); | |
231 | if (*C == 0) | |
232 | return false; | |
233 | ||
234 | // Jump to the next word | |
36f610f1 | 235 | for (;*C != 0 && isspace(*C) == 0; C++) |
6c139d6e AL |
236 | { |
237 | if (*C == '"') | |
238 | { | |
404528bd DK |
239 | C = strchr(C + 1, '"'); |
240 | if (C == NULL) | |
7834cb57 AL |
241 | return false; |
242 | } | |
243 | if (*C == '[') | |
244 | { | |
404528bd DK |
245 | C = strchr(C + 1, ']'); |
246 | if (C == NULL) | |
6c139d6e AL |
247 | return false; |
248 | } | |
249 | } | |
250 | ||
251 | // Now de-quote characters | |
252 | char Buffer[1024]; | |
253 | char Tmp[3]; | |
254 | const char *Start = String; | |
255 | char *I; | |
256 | for (I = Buffer; I < Buffer + sizeof(Buffer) && Start != C; I++) | |
257 | { | |
436d7eab DK |
258 | if (*Start == '%' && Start + 2 < C && |
259 | isxdigit(Start[1]) && isxdigit(Start[2])) | |
6c139d6e AL |
260 | { |
261 | Tmp[0] = Start[1]; | |
262 | Tmp[1] = Start[2]; | |
1bc849af | 263 | Tmp[2] = 0; |
6c139d6e AL |
264 | *I = (char)strtol(Tmp,0,16); |
265 | Start += 3; | |
266 | continue; | |
267 | } | |
268 | if (*Start != '"') | |
269 | *I = *Start; | |
270 | else | |
271 | I--; | |
272 | Start++; | |
273 | } | |
274 | *I = 0; | |
275 | Res = Buffer; | |
276 | ||
277 | // Skip ending white space | |
36f610f1 | 278 | for (;*C != 0 && isspace(*C) != 0; C++); |
6c139d6e AL |
279 | String = C; |
280 | return true; | |
281 | } | |
282 | /*}}}*/ | |
08e8f724 AL |
283 | // ParseCWord - Parses a string like a C "" expression /*{{{*/ |
284 | // --------------------------------------------------------------------- | |
b2e465d6 | 285 | /* This expects a series of space separated strings enclosed in ""'s. |
08e8f724 | 286 | It concatenates the ""'s into a single string. */ |
b2e465d6 | 287 | bool ParseCWord(const char *&String,string &Res) |
08e8f724 AL |
288 | { |
289 | // Skip leading whitespace | |
290 | const char *C = String; | |
291 | for (;*C != 0 && *C == ' '; C++); | |
292 | if (*C == 0) | |
293 | return false; | |
294 | ||
295 | char Buffer[1024]; | |
296 | char *Buf = Buffer; | |
297 | if (strlen(String) >= sizeof(Buffer)) | |
298 | return false; | |
299 | ||
300 | for (; *C != 0; C++) | |
301 | { | |
302 | if (*C == '"') | |
303 | { | |
304 | for (C++; *C != 0 && *C != '"'; C++) | |
305 | *Buf++ = *C; | |
306 | ||
307 | if (*C == 0) | |
308 | return false; | |
309 | ||
310 | continue; | |
311 | } | |
312 | ||
313 | if (C != String && isspace(*C) != 0 && isspace(C[-1]) != 0) | |
314 | continue; | |
315 | if (isspace(*C) == 0) | |
316 | return false; | |
317 | *Buf++ = ' '; | |
b2e465d6 | 318 | } |
08e8f724 AL |
319 | *Buf = 0; |
320 | Res = Buffer; | |
b2e465d6 | 321 | String = C; |
08e8f724 AL |
322 | return true; |
323 | } | |
324 | /*}}}*/ | |
6d5dd02a | 325 | // QuoteString - Convert a string into quoted from /*{{{*/ |
1bc849af | 326 | // --------------------------------------------------------------------- |
6d5dd02a | 327 | /* */ |
171c75f1 | 328 | string QuoteString(const string &Str, const char *Bad) |
1bc849af | 329 | { |
b8eba208 | 330 | std::stringstream Res; |
f7f0d6c7 | 331 | for (string::const_iterator I = Str.begin(); I != Str.end(); ++I) |
1bc849af | 332 | { |
b8eba208 | 333 | if (strchr(Bad,*I) != 0 || isprint(*I) == 0 || |
436d7eab DK |
334 | *I == 0x25 || // percent '%' char |
335 | *I <= 0x20 || *I >= 0x7F) // control chars | |
1bc849af | 336 | { |
4453cfdc | 337 | ioprintf(Res, "%%%02hhx", *I); |
1bc849af AL |
338 | } |
339 | else | |
b8eba208 | 340 | Res << *I; |
1bc849af | 341 | } |
b8eba208 | 342 | return Res.str(); |
1bc849af AL |
343 | } |
344 | /*}}}*/ | |
6d5dd02a | 345 | // DeQuoteString - Convert a string from quoted from /*{{{*/ |
6c139d6e | 346 | // --------------------------------------------------------------------- |
6d5dd02a | 347 | /* This undoes QuoteString */ |
171c75f1 | 348 | string DeQuoteString(const string &Str) |
436d7eab DK |
349 | { |
350 | return DeQuoteString(Str.begin(),Str.end()); | |
351 | } | |
352 | string DeQuoteString(string::const_iterator const &begin, | |
353 | string::const_iterator const &end) | |
6c139d6e AL |
354 | { |
355 | string Res; | |
f7f0d6c7 | 356 | for (string::const_iterator I = begin; I != end; ++I) |
6c139d6e | 357 | { |
436d7eab DK |
358 | if (*I == '%' && I + 2 < end && |
359 | isxdigit(I[1]) && isxdigit(I[2])) | |
6c139d6e | 360 | { |
6d5dd02a AL |
361 | char Tmp[3]; |
362 | Tmp[0] = I[1]; | |
363 | Tmp[1] = I[2]; | |
364 | Tmp[2] = 0; | |
365 | Res += (char)strtol(Tmp,0,16); | |
366 | I += 2; | |
367 | continue; | |
6c139d6e AL |
368 | } |
369 | else | |
370 | Res += *I; | |
371 | } | |
6d5dd02a | 372 | return Res; |
6c139d6e | 373 | } |
6d5dd02a AL |
374 | |
375 | /*}}}*/ | |
6c139d6e AL |
376 | // SizeToStr - Convert a long into a human readable size /*{{{*/ |
377 | // --------------------------------------------------------------------- | |
24231681 AL |
378 | /* A max of 4 digits are shown before conversion to the next highest unit. |
379 | The max length of the string will be 5 chars unless the size is > 10 | |
6c139d6e AL |
380 | YottaBytes (E24) */ |
381 | string SizeToStr(double Size) | |
382 | { | |
6c139d6e AL |
383 | double ASize; |
384 | if (Size >= 0) | |
385 | ASize = Size; | |
386 | else | |
387 | ASize = -1*Size; | |
b8eba208 | 388 | |
6c139d6e AL |
389 | /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes, |
390 | ExaBytes, ZettaBytes, YottaBytes */ | |
7f25bdff | 391 | char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'}; |
6c139d6e AL |
392 | int I = 0; |
393 | while (I <= 8) | |
394 | { | |
395 | if (ASize < 100 && I != 0) | |
396 | { | |
b8eba208 DK |
397 | std::string S; |
398 | strprintf(S, "%'.1f %c", ASize, Ext[I]); | |
399 | return S; | |
6c139d6e | 400 | } |
b8eba208 | 401 | |
6c139d6e AL |
402 | if (ASize < 10000) |
403 | { | |
b8eba208 DK |
404 | std::string S; |
405 | strprintf(S, "%'.0f %c", ASize, Ext[I]); | |
406 | return S; | |
6c139d6e AL |
407 | } |
408 | ASize /= 1000.0; | |
409 | I++; | |
410 | } | |
b8eba208 | 411 | return ""; |
6c139d6e AL |
412 | } |
413 | /*}}}*/ | |
414 | // TimeToStr - Convert the time into a string /*{{{*/ | |
415 | // --------------------------------------------------------------------- | |
416 | /* Converts a number of seconds to a hms format */ | |
417 | string TimeToStr(unsigned long Sec) | |
418 | { | |
b8eba208 DK |
419 | std::string S; |
420 | if (Sec > 60*60*24) | |
6c139d6e | 421 | { |
b8eba208 DK |
422 | //TRANSLATOR: d means days, h means hours, min means minutes, s means seconds |
423 | strprintf(S,_("%lid %lih %limin %lis"),Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60); | |
424 | } | |
425 | else if (Sec > 60*60) | |
426 | { | |
427 | //TRANSLATOR: h means hours, min means minutes, s means seconds | |
428 | strprintf(S,_("%lih %limin %lis"),Sec/60/60,(Sec/60) % 60,Sec % 60); | |
429 | } | |
430 | else if (Sec > 60) | |
431 | { | |
432 | //TRANSLATOR: min means minutes, s means seconds | |
433 | strprintf(S,_("%limin %lis"),Sec/60,Sec % 60); | |
434 | } | |
435 | else | |
436 | { | |
437 | //TRANSLATOR: s means seconds | |
438 | strprintf(S,_("%lis"),Sec); | |
6c139d6e | 439 | } |
6c139d6e AL |
440 | return S; |
441 | } | |
442 | /*}}}*/ | |
443 | // SubstVar - Substitute a string for another string /*{{{*/ | |
444 | // --------------------------------------------------------------------- | |
1e3f4083 | 445 | /* This replaces all occurrences of Subst with Contents in Str. */ |
171c75f1 | 446 | string SubstVar(const string &Str,const string &Subst,const string &Contents) |
6c139d6e | 447 | { |
224dc038 DK |
448 | if (Subst.empty() == true) |
449 | return Str; | |
450 | ||
8efa2a3b | 451 | string::size_type Pos = 0; |
6c139d6e AL |
452 | string::size_type OldPos = 0; |
453 | string Temp; | |
224dc038 DK |
454 | |
455 | while (OldPos < Str.length() && | |
6c139d6e AL |
456 | (Pos = Str.find(Subst,OldPos)) != string::npos) |
457 | { | |
224dc038 DK |
458 | if (OldPos != Pos) |
459 | Temp.append(Str, OldPos, Pos - OldPos); | |
460 | if (Contents.empty() == false) | |
461 | Temp.append(Contents); | |
462 | OldPos = Pos + Subst.length(); | |
6c139d6e | 463 | } |
224dc038 | 464 | |
6c139d6e AL |
465 | if (OldPos == 0) |
466 | return Str; | |
224dc038 DK |
467 | |
468 | if (OldPos >= Str.length()) | |
469 | return Temp; | |
732510fe AW |
470 | |
471 | Temp.append(Str, OldPos, string::npos); | |
472 | return Temp; | |
6c139d6e | 473 | } |
b2e465d6 AL |
474 | string SubstVar(string Str,const struct SubstVar *Vars) |
475 | { | |
476 | for (; Vars->Subst != 0; Vars++) | |
477 | Str = SubstVar(Str,Vars->Subst,*Vars->Contents); | |
478 | return Str; | |
479 | } | |
6c139d6e | 480 | /*}}}*/ |
fa3b0945 MV |
481 | // OutputInDepth - return a string with separator multiplied with depth /*{{{*/ |
482 | // --------------------------------------------------------------------- | |
483 | /* Returns a string with the supplied separator depth + 1 times in it */ | |
484 | std::string OutputInDepth(const unsigned long Depth, const char* Separator) | |
485 | { | |
486 | std::string output = ""; | |
487 | for(unsigned long d=Depth+1; d > 0; d--) | |
488 | output.append(Separator); | |
489 | return output; | |
490 | } | |
491 | /*}}}*/ | |
ad00ae81 AL |
492 | // URItoFileName - Convert the uri into a unique file name /*{{{*/ |
493 | // --------------------------------------------------------------------- | |
494 | /* This converts a URI into a safe filename. It quotes all unsafe characters | |
495 | and converts / to _ and removes the scheme identifier. The resulting | |
496 | file name should be unique and never occur again for a different file */ | |
171c75f1 | 497 | string URItoFileName(const string &URI) |
ad00ae81 | 498 | { |
54cf15cb AL |
499 | // Nuke 'sensitive' items |
500 | ::URI U(URI); | |
171c75f1 MV |
501 | U.User.clear(); |
502 | U.Password.clear(); | |
503 | U.Access.clear(); | |
54cf15cb | 504 | |
ad00ae81 | 505 | // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF"; |
171c75f1 MV |
506 | string NewURI = QuoteString(U,"\\|{}[]<>\"^~_=!@#$%^&*"); |
507 | replace(NewURI.begin(),NewURI.end(),'/','_'); | |
508 | return NewURI; | |
ad00ae81 AL |
509 | } |
510 | /*}}}*/ | |
6c139d6e AL |
511 | // Base64Encode - Base64 Encoding routine for short strings /*{{{*/ |
512 | // --------------------------------------------------------------------- | |
513 | /* This routine performs a base64 transformation on a string. It was ripped | |
514 | from wget and then patched and bug fixed. | |
515 | ||
516 | This spec can be found in rfc2045 */ | |
171c75f1 | 517 | string Base64Encode(const string &S) |
6c139d6e AL |
518 | { |
519 | // Conversion table. | |
520 | static char tbl[64] = {'A','B','C','D','E','F','G','H', | |
521 | 'I','J','K','L','M','N','O','P', | |
522 | 'Q','R','S','T','U','V','W','X', | |
523 | 'Y','Z','a','b','c','d','e','f', | |
524 | 'g','h','i','j','k','l','m','n', | |
525 | 'o','p','q','r','s','t','u','v', | |
526 | 'w','x','y','z','0','1','2','3', | |
527 | '4','5','6','7','8','9','+','/'}; | |
528 | ||
529 | // Pre-allocate some space | |
530 | string Final; | |
531 | Final.reserve((4*S.length() + 2)/3 + 2); | |
532 | ||
533 | /* Transform the 3x8 bits to 4x6 bits, as required by | |
534 | base64. */ | |
5933aab2 | 535 | for (string::const_iterator I = S.begin(); I < S.end(); I += 3) |
6c139d6e AL |
536 | { |
537 | char Bits[3] = {0,0,0}; | |
538 | Bits[0] = I[0]; | |
5933aab2 | 539 | if (I + 1 < S.end()) |
6c139d6e | 540 | Bits[1] = I[1]; |
5933aab2 | 541 | if (I + 2 < S.end()) |
6c139d6e AL |
542 | Bits[2] = I[2]; |
543 | ||
544 | Final += tbl[Bits[0] >> 2]; | |
545 | Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)]; | |
546 | ||
5933aab2 | 547 | if (I + 1 >= S.end()) |
6c139d6e AL |
548 | break; |
549 | ||
550 | Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)]; | |
551 | ||
5933aab2 | 552 | if (I + 2 >= S.end()) |
6c139d6e AL |
553 | break; |
554 | ||
555 | Final += tbl[Bits[2] & 0x3f]; | |
556 | } | |
557 | ||
558 | /* Apply the padding elements, this tells how many bytes the remote | |
559 | end should discard */ | |
560 | if (S.length() % 3 == 2) | |
561 | Final += '='; | |
562 | if (S.length() % 3 == 1) | |
563 | Final += "=="; | |
564 | ||
565 | return Final; | |
566 | } | |
567 | /*}}}*/ | |
0da8987a | 568 | // stringcmp - Arbitrary string compare /*{{{*/ |
6c139d6e | 569 | // --------------------------------------------------------------------- |
7365ff46 | 570 | /* This safely compares two non-null terminated strings of arbitrary |
6c139d6e AL |
571 | length */ |
572 | int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd) | |
47db8997 AL |
573 | { |
574 | for (; A != AEnd && B != BEnd; A++, B++) | |
575 | if (*A != *B) | |
576 | break; | |
577 | ||
578 | if (A == AEnd && B == BEnd) | |
579 | return 0; | |
580 | if (A == AEnd) | |
581 | return 1; | |
582 | if (B == BEnd) | |
583 | return -1; | |
584 | if (*A < *B) | |
585 | return -1; | |
586 | return 1; | |
587 | } | |
ae0b19f5 AL |
588 | |
589 | #if __GNUC__ >= 3 | |
47db8997 AL |
590 | int stringcmp(string::const_iterator A,string::const_iterator AEnd, |
591 | const char *B,const char *BEnd) | |
592 | { | |
593 | for (; A != AEnd && B != BEnd; A++, B++) | |
594 | if (*A != *B) | |
595 | break; | |
596 | ||
597 | if (A == AEnd && B == BEnd) | |
598 | return 0; | |
599 | if (A == AEnd) | |
600 | return 1; | |
601 | if (B == BEnd) | |
602 | return -1; | |
603 | if (*A < *B) | |
604 | return -1; | |
605 | return 1; | |
606 | } | |
607 | int stringcmp(string::const_iterator A,string::const_iterator AEnd, | |
608 | string::const_iterator B,string::const_iterator BEnd) | |
6c139d6e AL |
609 | { |
610 | for (; A != AEnd && B != BEnd; A++, B++) | |
611 | if (*A != *B) | |
612 | break; | |
613 | ||
614 | if (A == AEnd && B == BEnd) | |
615 | return 0; | |
616 | if (A == AEnd) | |
617 | return 1; | |
618 | if (B == BEnd) | |
619 | return -1; | |
620 | if (*A < *B) | |
621 | return -1; | |
622 | return 1; | |
623 | } | |
ae0b19f5 | 624 | #endif |
6c139d6e | 625 | /*}}}*/ |
0da8987a | 626 | // stringcasecmp - Arbitrary case insensitive string compare /*{{{*/ |
6c139d6e AL |
627 | // --------------------------------------------------------------------- |
628 | /* */ | |
629 | int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd) | |
47db8997 AL |
630 | { |
631 | for (; A != AEnd && B != BEnd; A++, B++) | |
6dc60370 | 632 | if (tolower_ascii(*A) != tolower_ascii(*B)) |
47db8997 AL |
633 | break; |
634 | ||
635 | if (A == AEnd && B == BEnd) | |
636 | return 0; | |
637 | if (A == AEnd) | |
638 | return 1; | |
639 | if (B == BEnd) | |
640 | return -1; | |
6dc60370 | 641 | if (tolower_ascii(*A) < tolower_ascii(*B)) |
47db8997 AL |
642 | return -1; |
643 | return 1; | |
644 | } | |
ae0b19f5 | 645 | #if __GNUC__ >= 3 |
47db8997 AL |
646 | int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, |
647 | const char *B,const char *BEnd) | |
648 | { | |
649 | for (; A != AEnd && B != BEnd; A++, B++) | |
6dc60370 | 650 | if (tolower_ascii(*A) != tolower_ascii(*B)) |
47db8997 AL |
651 | break; |
652 | ||
653 | if (A == AEnd && B == BEnd) | |
654 | return 0; | |
655 | if (A == AEnd) | |
656 | return 1; | |
657 | if (B == BEnd) | |
658 | return -1; | |
6dc60370 | 659 | if (tolower_ascii(*A) < tolower_ascii(*B)) |
47db8997 AL |
660 | return -1; |
661 | return 1; | |
662 | } | |
663 | int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, | |
664 | string::const_iterator B,string::const_iterator BEnd) | |
6c139d6e AL |
665 | { |
666 | for (; A != AEnd && B != BEnd; A++, B++) | |
6dc60370 | 667 | if (tolower_ascii(*A) != tolower_ascii(*B)) |
6c139d6e | 668 | break; |
3b5421b4 | 669 | |
6c139d6e AL |
670 | if (A == AEnd && B == BEnd) |
671 | return 0; | |
672 | if (A == AEnd) | |
673 | return 1; | |
674 | if (B == BEnd) | |
675 | return -1; | |
6dc60370 | 676 | if (tolower_ascii(*A) < tolower_ascii(*B)) |
6c139d6e AL |
677 | return -1; |
678 | return 1; | |
679 | } | |
ae0b19f5 | 680 | #endif |
6c139d6e | 681 | /*}}}*/ |
3b5421b4 AL |
682 | // LookupTag - Lookup the value of a tag in a taged string /*{{{*/ |
683 | // --------------------------------------------------------------------- | |
684 | /* The format is like those used in package files and the method | |
685 | communication system */ | |
171c75f1 | 686 | string LookupTag(const string &Message,const char *Tag,const char *Default) |
3b5421b4 AL |
687 | { |
688 | // Look for a matching tag. | |
689 | int Length = strlen(Tag); | |
f7f0d6c7 | 690 | for (string::const_iterator I = Message.begin(); I + Length < Message.end(); ++I) |
3b5421b4 AL |
691 | { |
692 | // Found the tag | |
693 | if (I[Length] == ':' && stringcasecmp(I,I+Length,Tag) == 0) | |
694 | { | |
695 | // Find the end of line and strip the leading/trailing spaces | |
171c75f1 | 696 | string::const_iterator J; |
3b5421b4 | 697 | I += Length + 1; |
74dedb4a | 698 | for (; isspace_ascii(*I) != 0 && I < Message.end(); ++I); |
f7f0d6c7 | 699 | for (J = I; *J != '\n' && J < Message.end(); ++J); |
74dedb4a | 700 | for (; J > I && isspace_ascii(J[-1]) != 0; --J); |
3b5421b4 | 701 | |
0db4a45b | 702 | return string(I,J); |
3b5421b4 AL |
703 | } |
704 | ||
f7f0d6c7 | 705 | for (; *I != '\n' && I < Message.end(); ++I); |
3b5421b4 AL |
706 | } |
707 | ||
708 | // Failed to find a match | |
709 | if (Default == 0) | |
710 | return string(); | |
711 | return Default; | |
712 | } | |
713 | /*}}}*/ | |
714 | // StringToBool - Converts a string into a boolean /*{{{*/ | |
715 | // --------------------------------------------------------------------- | |
716 | /* This inspects the string to see if it is true or if it is false and | |
717 | then returns the result. Several varients on true/false are checked. */ | |
171c75f1 | 718 | int StringToBool(const string &Text,int Default) |
3b5421b4 | 719 | { |
08be0ca3 MV |
720 | char *ParseEnd; |
721 | int Res = strtol(Text.c_str(),&ParseEnd,0); | |
722 | // ensure that the entire string was converted by strtol to avoid | |
723 | // failures on "apt-cache show -a 0ad" where the "0" is converted | |
724 | const char *TextEnd = Text.c_str()+Text.size(); | |
725 | if (ParseEnd == TextEnd && Res >= 0 && Res <= 1) | |
3b5421b4 AL |
726 | return Res; |
727 | ||
728 | // Check for positives | |
729 | if (strcasecmp(Text.c_str(),"no") == 0 || | |
730 | strcasecmp(Text.c_str(),"false") == 0 || | |
731 | strcasecmp(Text.c_str(),"without") == 0 || | |
7f25bdff | 732 | strcasecmp(Text.c_str(),"off") == 0 || |
3b5421b4 AL |
733 | strcasecmp(Text.c_str(),"disable") == 0) |
734 | return 0; | |
735 | ||
736 | // Check for negatives | |
737 | if (strcasecmp(Text.c_str(),"yes") == 0 || | |
738 | strcasecmp(Text.c_str(),"true") == 0 || | |
739 | strcasecmp(Text.c_str(),"with") == 0 || | |
7f25bdff | 740 | strcasecmp(Text.c_str(),"on") == 0 || |
3b5421b4 AL |
741 | strcasecmp(Text.c_str(),"enable") == 0) |
742 | return 1; | |
743 | ||
744 | return Default; | |
745 | } | |
746 | /*}}}*/ | |
0a8a80e5 AL |
747 | // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/ |
748 | // --------------------------------------------------------------------- | |
749 | /* This converts a time_t into a string time representation that is | |
750 | year 2000 complient and timezone neutral */ | |
751 | string TimeRFC1123(time_t Date) | |
752 | { | |
410327e1 DK |
753 | struct tm Conv; |
754 | if (gmtime_r(&Date, &Conv) == NULL) | |
755 | return ""; | |
0a8a80e5 | 756 | |
eceb219c DK |
757 | auto const posix = std::locale("C.UTF-8"); |
758 | std::ostringstream datestr; | |
759 | datestr.imbue(posix); | |
760 | datestr << std::put_time(&Conv, "%a, %d %b %Y %H:%M:%S GMT"); | |
761 | return datestr.str(); | |
0a8a80e5 AL |
762 | } |
763 | /*}}}*/ | |
764 | // ReadMessages - Read messages from the FD /*{{{*/ | |
765 | // --------------------------------------------------------------------- | |
766 | /* This pulls full messages from the input FD into the message buffer. | |
767 | It assumes that messages will not pause during transit so no | |
ffc36991 DB |
768 | fancy buffering is used. |
769 | ||
770 | In particular: this reads blocks from the input until it believes | |
771 | that it's run out of input text. Each block is terminated by a | |
d8c71b3b | 772 | double newline ('\n' followed by '\n'). |
ffc36991 | 773 | */ |
0a8a80e5 AL |
774 | bool ReadMessages(int Fd, vector<string> &List) |
775 | { | |
aee70518 | 776 | char Buffer[64000]; |
ffc36991 DB |
777 | // Represents any left-over from the previous iteration of the |
778 | // parse loop. (i.e., if a message is split across the end | |
779 | // of the buffer, it goes here) | |
780 | string PartialMessage; | |
d8c71b3b DK |
781 | |
782 | do { | |
783 | int const Res = read(Fd, Buffer, sizeof(Buffer)); | |
b0db36b1 AL |
784 | if (Res < 0 && errno == EINTR) |
785 | continue; | |
d8c71b3b DK |
786 | |
787 | // process we read from has died | |
0a8a80e5 AL |
788 | if (Res == 0) |
789 | return false; | |
d8c71b3b | 790 | |
0a8a80e5 | 791 | // No data |
3b0e76ec | 792 | #if EAGAIN != EWOULDBLOCK |
d8c71b3b | 793 | if (Res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) |
3b0e76ec DK |
794 | #else |
795 | if (Res < 0 && errno == EAGAIN) | |
796 | #endif | |
0a8a80e5 | 797 | return true; |
b2e465d6 AL |
798 | if (Res < 0) |
799 | return false; | |
d8c71b3b DK |
800 | |
801 | // extract the message(s) from the buffer | |
802 | char const *Start = Buffer; | |
803 | char const * const End = Buffer + Res; | |
804 | ||
805 | char const * NL = (char const *) memchr(Start, '\n', End - Start); | |
806 | if (NL == NULL) | |
0a8a80e5 | 807 | { |
d8c71b3b DK |
808 | // end of buffer: store what we have so far and read new data in |
809 | PartialMessage.append(Start, End - Start); | |
810 | Start = End; | |
0a8a80e5 | 811 | } |
ffc36991 | 812 | else |
d8c71b3b DK |
813 | ++NL; |
814 | ||
815 | if (PartialMessage.empty() == false && Start < End) | |
816 | { | |
817 | // if we start with a new line, see if the partial message we have ended with one | |
818 | // so that we properly detect records ending between two read() runs | |
819 | // cases are: \n|\n , \r\n|\r\n and \r\n\r|\n | |
820 | // the case \r|\n\r\n is handled by the usual double-newline handling | |
821 | if ((NL - Start) == 1 || ((NL - Start) == 2 && *Start == '\r')) | |
822 | { | |
823 | if (APT::String::Endswith(PartialMessage, "\n") || APT::String::Endswith(PartialMessage, "\r\n\r")) | |
824 | { | |
825 | PartialMessage.erase(PartialMessage.find_last_not_of("\r\n") + 1); | |
826 | List.push_back(PartialMessage); | |
827 | PartialMessage.clear(); | |
828 | while (NL < End && (*NL == '\n' || *NL == '\r')) ++NL; | |
829 | Start = NL; | |
830 | } | |
831 | } | |
832 | } | |
833 | ||
834 | while (Start < End) { | |
835 | char const * NL2 = (char const *) memchr(NL, '\n', End - NL); | |
836 | if (NL2 == NULL) | |
837 | { | |
838 | // end of buffer: store what we have so far and read new data in | |
839 | PartialMessage.append(Start, End - Start); | |
840 | break; | |
841 | } | |
842 | ++NL2; | |
843 | ||
844 | // did we find a double newline? | |
845 | if ((NL2 - NL) == 1 || ((NL2 - NL) == 2 && *NL == '\r')) | |
846 | { | |
847 | PartialMessage.append(Start, NL2 - Start); | |
848 | PartialMessage.erase(PartialMessage.find_last_not_of("\r\n") + 1); | |
849 | List.push_back(PartialMessage); | |
850 | PartialMessage.clear(); | |
851 | while (NL2 < End && (*NL2 == '\n' || *NL2 == '\r')) ++NL2; | |
852 | Start = NL2; | |
853 | } | |
854 | NL = NL2; | |
855 | } | |
856 | ||
857 | // we have read at least one complete message and nothing left | |
858 | if (PartialMessage.empty() == true) | |
859 | return true; | |
0a8a80e5 AL |
860 | |
861 | if (WaitFd(Fd) == false) | |
862 | return false; | |
d8c71b3b | 863 | } while (true); |
0a8a80e5 AL |
864 | } |
865 | /*}}}*/ | |
24231681 AL |
866 | // MonthConv - Converts a month string into a number /*{{{*/ |
867 | // --------------------------------------------------------------------- | |
868 | /* This was lifted from the boa webserver which lifted it from 'wn-v1.07' | |
6dc60370 | 869 | Made it a bit more robust with a few tolower_ascii though. */ |
24231681 AL |
870 | static int MonthConv(char *Month) |
871 | { | |
6dc60370 | 872 | switch (tolower_ascii(*Month)) |
24231681 | 873 | { |
6dc60370 DK |
874 | case 'a': |
875 | return tolower_ascii(Month[1]) == 'p'?3:7; | |
876 | case 'd': | |
24231681 | 877 | return 11; |
6dc60370 | 878 | case 'f': |
24231681 | 879 | return 1; |
6dc60370 DK |
880 | case 'j': |
881 | if (tolower_ascii(Month[1]) == 'a') | |
24231681 | 882 | return 0; |
6dc60370 DK |
883 | return tolower_ascii(Month[2]) == 'n'?5:6; |
884 | case 'm': | |
885 | return tolower_ascii(Month[2]) == 'r'?2:4; | |
886 | case 'n': | |
24231681 | 887 | return 10; |
6dc60370 | 888 | case 'o': |
24231681 | 889 | return 9; |
6dc60370 | 890 | case 's': |
24231681 AL |
891 | return 8; |
892 | ||
893 | // Pretend it is January.. | |
894 | default: | |
895 | return 0; | |
896 | } | |
897 | } | |
898 | /*}}}*/ | |
55089145 | 899 | // timegm - Internal timegm if the gnu version is not available /*{{{*/ |
6d5dd02a | 900 | // --------------------------------------------------------------------- |
55089145 | 901 | /* Converts struct tm to time_t, assuming the data in tm is UTC rather |
6d5dd02a | 902 | than local timezone (mktime assumes the latter). |
41b6caf4 | 903 | |
55089145 DK |
904 | This function is a nonstandard GNU extension that is also present on |
905 | the BSDs and maybe other systems. For others we follow the advice of | |
906 | the manpage of timegm and use his portable replacement. */ | |
907 | #ifndef HAVE_TIMEGM | |
6d5dd02a AL |
908 | static time_t timegm(struct tm *t) |
909 | { | |
55089145 DK |
910 | char *tz = getenv("TZ"); |
911 | setenv("TZ", "", 1); | |
912 | tzset(); | |
913 | time_t ret = mktime(t); | |
914 | if (tz) | |
915 | setenv("TZ", tz, 1); | |
916 | else | |
917 | unsetenv("TZ"); | |
918 | tzset(); | |
919 | return ret; | |
6d5dd02a AL |
920 | } |
921 | #endif | |
922 | /*}}}*/ | |
9febc2b2 | 923 | // RFC1123StrToTime - Converts a HTTP1.1 full date strings into a time_t /*{{{*/ |
cd8cf88f | 924 | // --------------------------------------------------------------------- |
9febc2b2 DK |
925 | /* tries to parses a full date as specified in RFC7231 §7.1.1.1 |
926 | with one exception: HTTP/1.1 valid dates need to have GMT as timezone. | |
927 | As we encounter dates from UTC or with a numeric timezone in other places, | |
928 | we allow them here to to be able to reuse the method. Either way, a date | |
929 | must be in UTC or parsing will fail. Previous implementations of this | |
930 | method used to ignore the timezone and assume always UTC. */ | |
cd8cf88f DK |
931 | bool RFC1123StrToTime(const char* const str,time_t &time) |
932 | { | |
9febc2b2 DK |
933 | struct tm t; |
934 | auto const &posix = std::locale("C.UTF-8"); | |
935 | auto const parse_time = [&](char const * const s, bool const has_timezone) { | |
936 | std::istringstream ss(str); | |
937 | ss.imbue(posix); | |
938 | ss >> std::get_time(&t, s); | |
939 | if (has_timezone && ss.fail() == false) | |
940 | { | |
941 | std::string timezone; | |
942 | ss >> timezone; | |
943 | if (timezone.empty()) | |
944 | return false; | |
945 | if (timezone != "GMT" && timezone != "UTC" && timezone != "Z") // RFC 822 | |
946 | { | |
947 | // numeric timezones as a should of RFC 1123 and generally preferred | |
948 | try { | |
949 | size_t pos; | |
950 | auto const zone = std::stoi(timezone, &pos); | |
951 | if (zone != 0 || pos != timezone.length()) | |
952 | return false; | |
953 | } catch (...) { | |
954 | return false; | |
955 | } | |
956 | } | |
957 | } | |
958 | t.tm_isdst = 0; | |
959 | return ss.fail() == false; | |
960 | }; | |
961 | ||
962 | bool const good = | |
963 | // Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 | |
964 | parse_time("%a, %d %b %Y %H:%M:%S", true) || | |
965 | // Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 | |
966 | parse_time("%A, %d-%b-%y %H:%M:%S", true) || | |
967 | // Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format | |
968 | parse_time("%c", false); // "%a %b %d %H:%M:%S %Y" | |
969 | if (good == false) | |
cd8cf88f DK |
970 | return false; |
971 | ||
9febc2b2 | 972 | time = timegm(&t); |
cd8cf88f DK |
973 | return true; |
974 | } | |
975 | /*}}}*/ | |
976 | // FTPMDTMStrToTime - Converts a ftp modification date into a time_t /*{{{*/ | |
977 | // --------------------------------------------------------------------- | |
978 | /* */ | |
979 | bool FTPMDTMStrToTime(const char* const str,time_t &time) | |
980 | { | |
981 | struct tm Tm; | |
982 | // MDTM includes no whitespaces but recommend and ignored by strptime | |
983 | if (strptime(str, "%Y %m %d %H %M %S", &Tm) == NULL) | |
984 | return false; | |
985 | ||
986 | time = timegm(&Tm); | |
987 | return true; | |
988 | } | |
989 | /*}}}*/ | |
24231681 AL |
990 | // StrToTime - Converts a string into a time_t /*{{{*/ |
991 | // --------------------------------------------------------------------- | |
1e3f4083 | 992 | /* This handles all 3 popular time formats including RFC 1123, RFC 1036 |
24231681 AL |
993 | and the C library asctime format. It requires the GNU library function |
994 | 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar | |
f58a97d3 AL |
995 | reason the C library does not provide any such function :< This also |
996 | handles the weird, but unambiguous FTP time format*/ | |
171c75f1 | 997 | bool StrToTime(const string &Val,time_t &Result) |
24231681 AL |
998 | { |
999 | struct tm Tm; | |
1000 | char Month[10]; | |
404528bd | 1001 | |
24231681 | 1002 | // Skip the day of the week |
404528bd DK |
1003 | const char *I = strchr(Val.c_str(), ' '); |
1004 | ||
24231681 | 1005 | // Handle RFC 1123 time |
f58a97d3 | 1006 | Month[0] = 0; |
324cbd56 | 1007 | if (sscanf(I," %2d %3s %4d %2d:%2d:%2d GMT",&Tm.tm_mday,Month,&Tm.tm_year, |
24231681 AL |
1008 | &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6) |
1009 | { | |
1010 | // Handle RFC 1036 time | |
324cbd56 | 1011 | if (sscanf(I," %2d-%3s-%3d %2d:%2d:%2d GMT",&Tm.tm_mday,Month, |
24231681 AL |
1012 | &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6) |
1013 | Tm.tm_year += 1900; | |
1014 | else | |
1015 | { | |
1016 | // asctime format | |
324cbd56 | 1017 | if (sscanf(I," %3s %2d %2d:%2d:%2d %4d",Month,&Tm.tm_mday, |
24231681 | 1018 | &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6) |
f58a97d3 AL |
1019 | { |
1020 | // 'ftp' time | |
7ef72446 | 1021 | if (sscanf(Val.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm.tm_year,&Tm.tm_mon, |
f58a97d3 AL |
1022 | &Tm.tm_mday,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6) |
1023 | return false; | |
1024 | Tm.tm_mon--; | |
1025 | } | |
24231681 AL |
1026 | } |
1027 | } | |
1028 | ||
1029 | Tm.tm_isdst = 0; | |
f58a97d3 AL |
1030 | if (Month[0] != 0) |
1031 | Tm.tm_mon = MonthConv(Month); | |
70e0c168 MV |
1032 | else |
1033 | Tm.tm_mon = 0; // we don't have a month, so pick something | |
24231681 AL |
1034 | Tm.tm_year -= 1900; |
1035 | ||
1036 | // Convert to local time and then to GMT | |
1037 | Result = timegm(&Tm); | |
1038 | return true; | |
1039 | } | |
1040 | /*}}}*/ | |
ddc1d8d0 AL |
1041 | // StrToNum - Convert a fixed length string to a number /*{{{*/ |
1042 | // --------------------------------------------------------------------- | |
1043 | /* This is used in decoding the crazy fixed length string headers in | |
1044 | tar and ar files. */ | |
1045 | bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base) | |
1046 | { | |
1047 | char S[30]; | |
1048 | if (Len >= sizeof(S)) | |
1049 | return false; | |
1050 | memcpy(S,Str,Len); | |
1051 | S[Len] = 0; | |
1052 | ||
1053 | // All spaces is a zero | |
1054 | Res = 0; | |
1055 | unsigned I; | |
1056 | for (I = 0; S[I] == ' '; I++); | |
1057 | if (S[I] == 0) | |
1058 | return true; | |
1059 | ||
1060 | char *End; | |
1061 | Res = strtoul(S,&End,Base); | |
1062 | if (End == S) | |
1063 | return false; | |
1064 | ||
1065 | return true; | |
1066 | } | |
1067 | /*}}}*/ | |
650faab0 DK |
1068 | // StrToNum - Convert a fixed length string to a number /*{{{*/ |
1069 | // --------------------------------------------------------------------- | |
1070 | /* This is used in decoding the crazy fixed length string headers in | |
1071 | tar and ar files. */ | |
1072 | bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base) | |
1073 | { | |
1074 | char S[30]; | |
1075 | if (Len >= sizeof(S)) | |
1076 | return false; | |
1077 | memcpy(S,Str,Len); | |
1078 | S[Len] = 0; | |
1079 | ||
1080 | // All spaces is a zero | |
1081 | Res = 0; | |
1082 | unsigned I; | |
1083 | for (I = 0; S[I] == ' '; I++); | |
1084 | if (S[I] == 0) | |
1085 | return true; | |
1086 | ||
1087 | char *End; | |
1088 | Res = strtoull(S,&End,Base); | |
1089 | if (End == S) | |
1090 | return false; | |
1091 | ||
1092 | return true; | |
1093 | } | |
1094 | /*}}}*/ | |
1095 | ||
54f2f0a3 NH |
1096 | // Base256ToNum - Convert a fixed length binary to a number /*{{{*/ |
1097 | // --------------------------------------------------------------------- | |
1098 | /* This is used in decoding the 256bit encoded fixed length fields in | |
1099 | tar files */ | |
3c09d634 | 1100 | bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len) |
54f2f0a3 | 1101 | { |
54f2f0a3 NH |
1102 | if ((Str[0] & 0x80) == 0) |
1103 | return false; | |
1104 | else | |
1105 | { | |
1106 | Res = Str[0] & 0x7F; | |
f688d1d3 | 1107 | for(unsigned int i = 1; i < Len; ++i) |
54f2f0a3 NH |
1108 | Res = (Res<<8) + Str[i]; |
1109 | return true; | |
1110 | } | |
1111 | } | |
1112 | /*}}}*/ | |
3c09d634 GJ |
1113 | // Base256ToNum - Convert a fixed length binary to a number /*{{{*/ |
1114 | // --------------------------------------------------------------------- | |
1115 | /* This is used in decoding the 256bit encoded fixed length fields in | |
1116 | tar files */ | |
1117 | bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len) | |
1118 | { | |
1119 | unsigned long long Num; | |
1120 | bool rc; | |
1121 | ||
1122 | rc = Base256ToNum(Str, Num, Len); | |
1123 | Res = Num; | |
1124 | if (Res != Num) | |
1125 | return false; | |
1126 | ||
1127 | return rc; | |
1128 | } | |
1129 | /*}}}*/ | |
6e52073f AL |
1130 | // HexDigit - Convert a hex character into an integer /*{{{*/ |
1131 | // --------------------------------------------------------------------- | |
1132 | /* Helper for Hex2Num */ | |
1133 | static int HexDigit(int c) | |
1134 | { | |
1135 | if (c >= '0' && c <= '9') | |
1136 | return c - '0'; | |
1137 | if (c >= 'a' && c <= 'f') | |
1138 | return c - 'a' + 10; | |
1139 | if (c >= 'A' && c <= 'F') | |
1140 | return c - 'A' + 10; | |
fc8f1c22 | 1141 | return -1; |
6e52073f AL |
1142 | } |
1143 | /*}}}*/ | |
1144 | // Hex2Num - Convert a long hex number into a buffer /*{{{*/ | |
1145 | // --------------------------------------------------------------------- | |
1146 | /* The length of the buffer must be exactly 1/2 the length of the string. */ | |
171c75f1 | 1147 | bool Hex2Num(const string &Str,unsigned char *Num,unsigned int Length) |
eff0c22e JAK |
1148 | { |
1149 | return Hex2Num(APT::StringView(Str), Num, Length); | |
1150 | } | |
1151 | ||
1152 | bool Hex2Num(const APT::StringView Str,unsigned char *Num,unsigned int Length) | |
6e52073f | 1153 | { |
0db4a45b | 1154 | if (Str.length() != Length*2) |
6e52073f AL |
1155 | return false; |
1156 | ||
1157 | // Convert each digit. We store it in the same order as the string | |
1158 | int J = 0; | |
eff0c22e | 1159 | for (auto I = Str.begin(); I != Str.end();J++, I += 2) |
6e52073f | 1160 | { |
fc8f1c22 NT |
1161 | int first_half = HexDigit(I[0]); |
1162 | int second_half; | |
1163 | if (first_half < 0) | |
6e52073f AL |
1164 | return false; |
1165 | ||
fc8f1c22 NT |
1166 | second_half = HexDigit(I[1]); |
1167 | if (second_half < 0) | |
1168 | return false; | |
1169 | Num[J] = first_half << 4; | |
1170 | Num[J] += second_half; | |
6e52073f AL |
1171 | } |
1172 | ||
1173 | return true; | |
1174 | } | |
1175 | /*}}}*/ | |
b2e465d6 AL |
1176 | // TokSplitString - Split a string up by a given token /*{{{*/ |
1177 | // --------------------------------------------------------------------- | |
1178 | /* This is intended to be a faster splitter, it does not use dynamic | |
1179 | memories. Input is changed to insert nulls at each token location. */ | |
1180 | bool TokSplitString(char Tok,char *Input,char **List, | |
1181 | unsigned long ListMax) | |
1182 | { | |
1183 | // Strip any leading spaces | |
1184 | char *Start = Input; | |
1185 | char *Stop = Start + strlen(Start); | |
1186 | for (; *Start != 0 && isspace(*Start) != 0; Start++); | |
1187 | ||
1188 | unsigned long Count = 0; | |
1189 | char *Pos = Start; | |
1190 | while (Pos != Stop) | |
1191 | { | |
1192 | // Skip to the next Token | |
1193 | for (; Pos != Stop && *Pos != Tok; Pos++); | |
1194 | ||
1195 | // Back remove spaces | |
1196 | char *End = Pos; | |
1197 | for (; End > Start && (End[-1] == Tok || isspace(End[-1]) != 0); End--); | |
1198 | *End = 0; | |
1199 | ||
1200 | List[Count++] = Start; | |
1201 | if (Count >= ListMax) | |
1202 | { | |
1203 | List[Count-1] = 0; | |
1204 | return false; | |
1205 | } | |
1206 | ||
1207 | // Advance pos | |
1208 | for (; Pos != Stop && (*Pos == Tok || isspace(*Pos) != 0 || *Pos == 0); Pos++); | |
1209 | Start = Pos; | |
1210 | } | |
1211 | ||
1212 | List[Count] = 0; | |
1213 | return true; | |
1214 | } | |
1215 | /*}}}*/ | |
3f42500d | 1216 | // VectorizeString - Split a string up into a vector of strings /*{{{*/ |
d7cf5923 DK |
1217 | // --------------------------------------------------------------------- |
1218 | /* This can be used to split a given string up into a vector, so the | |
1219 | propose is the same as in the method above and this one is a bit slower | |
3f42500d DK |
1220 | also, but the advantage is that we have an iteratable vector */ |
1221 | vector<string> VectorizeString(string const &haystack, char const &split) | |
d7cf5923 | 1222 | { |
a5414e56 DK |
1223 | vector<string> exploded; |
1224 | if (haystack.empty() == true) | |
1225 | return exploded; | |
d7cf5923 DK |
1226 | string::const_iterator start = haystack.begin(); |
1227 | string::const_iterator end = start; | |
d7cf5923 DK |
1228 | do { |
1229 | for (; end != haystack.end() && *end != split; ++end); | |
1230 | exploded.push_back(string(start, end)); | |
1231 | start = end + 1; | |
1232 | } while (end != haystack.end() && (++end) != haystack.end()); | |
1233 | return exploded; | |
1234 | } | |
1235 | /*}}}*/ | |
9572a54b | 1236 | // StringSplit - split a string into a string vector by token /*{{{*/ |
00f4d9ff | 1237 | // --------------------------------------------------------------------- |
41053d72 | 1238 | /* See header for details. |
00f4d9ff | 1239 | */ |
41053d72 | 1240 | vector<string> StringSplit(std::string const &s, std::string const &sep, |
85bf0019 | 1241 | unsigned int maxsplit) |
00f4d9ff MV |
1242 | { |
1243 | vector<string> split; | |
1244 | size_t start, pos; | |
85bf0019 | 1245 | |
8d89cda7 | 1246 | // no separator given, this is bogus |
00f4d9ff MV |
1247 | if(sep.size() == 0) |
1248 | return split; | |
85bf0019 MV |
1249 | |
1250 | start = pos = 0; | |
9572a54b MV |
1251 | while (pos != string::npos) |
1252 | { | |
00f4d9ff MV |
1253 | pos = s.find(sep, start); |
1254 | split.push_back(s.substr(start, pos-start)); | |
85bf0019 | 1255 | |
9572a54b | 1256 | // if maxsplit is reached, the remaining string is the last item |
2ddab3fb | 1257 | if(split.size() >= maxsplit) |
85bf0019 MV |
1258 | { |
1259 | split[split.size()-1] = s.substr(start); | |
1260 | break; | |
1261 | } | |
1262 | start = pos+sep.size(); | |
9572a54b | 1263 | } |
00f4d9ff MV |
1264 | return split; |
1265 | } | |
1266 | /*}}}*/ | |
b2e465d6 AL |
1267 | // RegexChoice - Simple regex list/list matcher /*{{{*/ |
1268 | // --------------------------------------------------------------------- | |
1269 | /* */ | |
1270 | unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, | |
1271 | const char **ListEnd) | |
1272 | { | |
1273 | for (RxChoiceList *R = Rxs; R->Str != 0; R++) | |
1274 | R->Hit = false; | |
1275 | ||
1276 | unsigned long Hits = 0; | |
ef74268b | 1277 | for (; ListBegin < ListEnd; ++ListBegin) |
b2e465d6 AL |
1278 | { |
1279 | // Check if the name is a regex | |
1280 | const char *I; | |
1281 | bool Regex = true; | |
1282 | for (I = *ListBegin; *I != 0; I++) | |
1283 | if (*I == '.' || *I == '?' || *I == '*' || *I == '|') | |
1284 | break; | |
1285 | if (*I == 0) | |
1286 | Regex = false; | |
1287 | ||
1288 | // Compile the regex pattern | |
1289 | regex_t Pattern; | |
1290 | if (Regex == true) | |
1291 | if (regcomp(&Pattern,*ListBegin,REG_EXTENDED | REG_ICASE | | |
1292 | REG_NOSUB) != 0) | |
1293 | Regex = false; | |
1294 | ||
1295 | // Search the list | |
1296 | bool Done = false; | |
1297 | for (RxChoiceList *R = Rxs; R->Str != 0; R++) | |
1298 | { | |
1299 | if (R->Str[0] == 0) | |
1300 | continue; | |
1301 | ||
1302 | if (strcasecmp(R->Str,*ListBegin) != 0) | |
1303 | { | |
1304 | if (Regex == false) | |
1305 | continue; | |
1306 | if (regexec(&Pattern,R->Str,0,0,0) != 0) | |
1307 | continue; | |
1308 | } | |
1309 | Done = true; | |
1310 | ||
1311 | if (R->Hit == false) | |
1312 | Hits++; | |
1313 | ||
1314 | R->Hit = true; | |
1315 | } | |
1316 | ||
1317 | if (Regex == true) | |
1318 | regfree(&Pattern); | |
1319 | ||
1320 | if (Done == false) | |
1321 | _error->Warning(_("Selection %s not found"),*ListBegin); | |
1322 | } | |
1323 | ||
1324 | return Hits; | |
1325 | } | |
1326 | /*}}}*/ | |
5076b3c2 | 1327 | // {str,io}printf - C format string outputter to C++ strings/iostreams /*{{{*/ |
b2e465d6 | 1328 | // --------------------------------------------------------------------- |
1168596f AL |
1329 | /* This is used to make the internationalization strings easier to translate |
1330 | and to allow reordering of parameters */ | |
5076b3c2 DK |
1331 | static bool iovprintf(ostream &out, const char *format, |
1332 | va_list &args, ssize_t &size) { | |
1333 | char *S = (char*)malloc(size); | |
1334 | ssize_t const n = vsnprintf(S, size, format, args); | |
1335 | if (n > -1 && n < size) { | |
1336 | out << S; | |
1337 | free(S); | |
1338 | return true; | |
1339 | } else { | |
1340 | if (n > -1) | |
1341 | size = n + 1; | |
1342 | else | |
1343 | size *= 2; | |
1344 | } | |
1345 | free(S); | |
1346 | return false; | |
1347 | } | |
1348 | void ioprintf(ostream &out,const char *format,...) | |
b2e465d6 AL |
1349 | { |
1350 | va_list args; | |
5076b3c2 DK |
1351 | ssize_t size = 400; |
1352 | while (true) { | |
e8afd168 | 1353 | bool ret; |
5076b3c2 | 1354 | va_start(args,format); |
ce105e87 | 1355 | ret = iovprintf(out, format, args, size); |
5076b3c2 | 1356 | va_end(args); |
ce105e87 DK |
1357 | if (ret == true) |
1358 | return; | |
5076b3c2 | 1359 | } |
1168596f | 1360 | } |
5076b3c2 | 1361 | void strprintf(string &out,const char *format,...) |
d4cd303e MV |
1362 | { |
1363 | va_list args; | |
5076b3c2 DK |
1364 | ssize_t size = 400; |
1365 | std::ostringstream outstr; | |
1366 | while (true) { | |
e8afd168 | 1367 | bool ret; |
5076b3c2 | 1368 | va_start(args,format); |
ce105e87 | 1369 | ret = iovprintf(outstr, format, args, size); |
5076b3c2 | 1370 | va_end(args); |
ce105e87 DK |
1371 | if (ret == true) |
1372 | break; | |
5076b3c2 DK |
1373 | } |
1374 | out = outstr.str(); | |
d4cd303e MV |
1375 | } |
1376 | /*}}}*/ | |
1168596f AL |
1377 | // safe_snprintf - Safer snprintf /*{{{*/ |
1378 | // --------------------------------------------------------------------- | |
1379 | /* This is a snprintf that will never (ever) go past 'End' and returns a | |
1380 | pointer to the end of the new string. The returned string is always null | |
1381 | terminated unless Buffer == end. This is a better alterantive to using | |
1382 | consecutive snprintfs. */ | |
1383 | char *safe_snprintf(char *Buffer,char *End,const char *Format,...) | |
1384 | { | |
1385 | va_list args; | |
ea6db08d | 1386 | int Did; |
1168596f | 1387 | |
1168596f AL |
1388 | if (End <= Buffer) |
1389 | return End; | |
163dc55b | 1390 | va_start(args,Format); |
1168596f | 1391 | Did = vsnprintf(Buffer,End - Buffer,Format,args); |
163dc55b MV |
1392 | va_end(args); |
1393 | ||
1168596f AL |
1394 | if (Did < 0 || Buffer + Did > End) |
1395 | return End; | |
1396 | return Buffer + Did; | |
b2e465d6 AL |
1397 | } |
1398 | /*}}}*/ | |
cdb9307c MV |
1399 | // StripEpoch - Remove the version "epoch" from a version string /*{{{*/ |
1400 | // --------------------------------------------------------------------- | |
1401 | string StripEpoch(const string &VerStr) | |
1402 | { | |
1403 | size_t i = VerStr.find(":"); | |
1404 | if (i == string::npos) | |
1405 | return VerStr; | |
1406 | return VerStr.substr(i+1); | |
1407 | } | |
69c2ecbd | 1408 | /*}}}*/ |
98eb4e9e | 1409 | |
4e86942a MV |
1410 | // tolower_ascii - tolower() function that ignores the locale /*{{{*/ |
1411 | // --------------------------------------------------------------------- | |
6dc60370 | 1412 | /* This little function is the most called method we have and tries |
1e3f4083 | 1413 | therefore to do the absolut minimum - and is notable faster than |
6dc60370 DK |
1414 | standard tolower/toupper and as a bonus avoids problems with different |
1415 | locales - we only operate on ascii chars anyway. */ | |
98eb4e9e | 1416 | #undef tolower_ascii |
390344f9 | 1417 | int tolower_ascii(int const c) APT_CONST APT_COLD; |
6dc60370 | 1418 | int tolower_ascii(int const c) |
4e86942a | 1419 | { |
98eb4e9e | 1420 | return tolower_ascii_inline(c); |
4e86942a MV |
1421 | } |
1422 | /*}}}*/ | |
1423 | ||
98b06343 JAK |
1424 | // isspace_ascii - isspace() function that ignores the locale /*{{{*/ |
1425 | // --------------------------------------------------------------------- | |
1426 | /* This little function is one of the most called methods we have and tries | |
1427 | therefore to do the absolut minimum - and is notable faster than | |
1428 | standard isspace() and as a bonus avoids problems with different | |
1429 | locales - we only operate on ascii chars anyway. */ | |
98eb4e9e | 1430 | #undef isspace_ascii |
390344f9 | 1431 | int isspace_ascii(int const c) APT_CONST APT_COLD; |
98b06343 JAK |
1432 | int isspace_ascii(int const c) |
1433 | { | |
98eb4e9e | 1434 | return isspace_ascii_inline(c); |
98b06343 JAK |
1435 | } |
1436 | /*}}}*/ | |
1437 | ||
1e3f4083 | 1438 | // CheckDomainList - See if Host is in a , separate list /*{{{*/ |
f8081133 | 1439 | // --------------------------------------------------------------------- |
1e3f4083 | 1440 | /* The domain list is a comma separate list of domains that are suffix |
f8081133 | 1441 | matched against the argument */ |
171c75f1 | 1442 | bool CheckDomainList(const string &Host,const string &List) |
f8081133 | 1443 | { |
47db8997 | 1444 | string::const_iterator Start = List.begin(); |
f7f0d6c7 | 1445 | for (string::const_iterator Cur = List.begin(); Cur <= List.end(); ++Cur) |
f8081133 | 1446 | { |
47db8997 | 1447 | if (Cur < List.end() && *Cur != ',') |
f8081133 AL |
1448 | continue; |
1449 | ||
1450 | // Match the end of the string.. | |
e2c7e6b5 | 1451 | if ((Host.size() >= (unsigned)(Cur - Start)) && |
f8081133 | 1452 | Cur - Start != 0 && |
47db8997 | 1453 | stringcasecmp(Host.end() - (Cur - Start),Host.end(),Start,Cur) == 0) |
f8081133 AL |
1454 | return true; |
1455 | ||
1456 | Start = Cur + 1; | |
1457 | } | |
1458 | return false; | |
1459 | } | |
1460 | /*}}}*/ | |
b9179170 MV |
1461 | // strv_length - Return the length of a NULL-terminated string array /*{{{*/ |
1462 | // --------------------------------------------------------------------- | |
1463 | /* */ | |
1464 | size_t strv_length(const char **str_array) | |
1465 | { | |
1466 | size_t i; | |
1467 | for (i=0; str_array[i] != NULL; i++) | |
1468 | /* nothing */ | |
1469 | ; | |
1470 | return i; | |
1471 | } | |
b8eba208 | 1472 | /*}}}*/ |
69c2ecbd | 1473 | // DeEscapeString - unescape (\0XX and \xXX) from a string /*{{{*/ |
a513ace2 | 1474 | // --------------------------------------------------------------------- |
cca2efe6 MV |
1475 | /* */ |
1476 | string DeEscapeString(const string &input) | |
a513ace2 | 1477 | { |
b9dc4706 | 1478 | char tmp[3]; |
69c2ecbd DK |
1479 | string::const_iterator it; |
1480 | string output; | |
f7f0d6c7 | 1481 | for (it = input.begin(); it != input.end(); ++it) |
a513ace2 MV |
1482 | { |
1483 | // just copy non-escape chars | |
1484 | if (*it != '\\') | |
1485 | { | |
1486 | output += *it; | |
1487 | continue; | |
1488 | } | |
f8081133 | 1489 | |
a513ace2 MV |
1490 | // deal with double escape |
1491 | if (*it == '\\' && | |
1492 | (it + 1 < input.end()) && it[1] == '\\') | |
1493 | { | |
1494 | // copy | |
1495 | output += *it; | |
1496 | // advance iterator one step further | |
f7f0d6c7 | 1497 | ++it; |
a513ace2 MV |
1498 | continue; |
1499 | } | |
1500 | ||
1501 | // ensure we have a char to read | |
1502 | if (it + 1 == input.end()) | |
1503 | continue; | |
f8081133 | 1504 | |
a513ace2 | 1505 | // read it |
f7f0d6c7 | 1506 | ++it; |
a513ace2 MV |
1507 | switch (*it) |
1508 | { | |
1509 | case '0': | |
b9dc4706 | 1510 | if (it + 2 <= input.end()) { |
a513ace2 MV |
1511 | tmp[0] = it[1]; |
1512 | tmp[1] = it[2]; | |
b9dc4706 | 1513 | tmp[2] = 0; |
a513ace2 MV |
1514 | output += (char)strtol(tmp, 0, 8); |
1515 | it += 2; | |
1516 | } | |
1517 | break; | |
1518 | case 'x': | |
1519 | if (it + 2 <= input.end()) { | |
1520 | tmp[0] = it[1]; | |
1521 | tmp[1] = it[2]; | |
1522 | tmp[2] = 0; | |
1523 | output += (char)strtol(tmp, 0, 16); | |
1524 | it += 2; | |
1525 | } | |
1526 | break; | |
1527 | default: | |
1528 | // FIXME: raise exception here? | |
a513ace2 MV |
1529 | break; |
1530 | } | |
1531 | } | |
1532 | return output; | |
1533 | } | |
1534 | /*}}}*/ | |
be4401bf | 1535 | // URI::CopyFrom - Copy from an object /*{{{*/ |
93bf083d AL |
1536 | // --------------------------------------------------------------------- |
1537 | /* This parses the URI into all of its components */ | |
171c75f1 | 1538 | void URI::CopyFrom(const string &U) |
93bf083d | 1539 | { |
5933aab2 | 1540 | string::const_iterator I = U.begin(); |
93bf083d | 1541 | |
b2e465d6 | 1542 | // Locate the first colon, this separates the scheme |
f7f0d6c7 | 1543 | for (; I < U.end() && *I != ':' ; ++I); |
5933aab2 | 1544 | string::const_iterator FirstColon = I; |
93bf083d | 1545 | |
bfd22fc0 AL |
1546 | /* Determine if this is a host type URI with a leading double // |
1547 | and then search for the first single / */ | |
5933aab2 AL |
1548 | string::const_iterator SingleSlash = I; |
1549 | if (I + 3 < U.end() && I[1] == '/' && I[2] == '/') | |
bfd22fc0 | 1550 | SingleSlash += 3; |
67ff87bf AL |
1551 | |
1552 | /* Find the / indicating the end of the hostname, ignoring /'s in the | |
1553 | square brackets */ | |
1554 | bool InBracket = false; | |
f7f0d6c7 | 1555 | for (; SingleSlash < U.end() && (*SingleSlash != '/' || InBracket == true); ++SingleSlash) |
67ff87bf AL |
1556 | { |
1557 | if (*SingleSlash == '[') | |
1558 | InBracket = true; | |
1559 | if (InBracket == true && *SingleSlash == ']') | |
1560 | InBracket = false; | |
1561 | } | |
1562 | ||
5933aab2 AL |
1563 | if (SingleSlash > U.end()) |
1564 | SingleSlash = U.end(); | |
93bf083d AL |
1565 | |
1566 | // We can now write the access and path specifiers | |
171c75f1 | 1567 | Access.assign(U.begin(),FirstColon); |
5933aab2 | 1568 | if (SingleSlash != U.end()) |
171c75f1 | 1569 | Path.assign(SingleSlash,U.end()); |
92e889c8 AL |
1570 | if (Path.empty() == true) |
1571 | Path = "/"; | |
1572 | ||
93bf083d | 1573 | // Now we attempt to locate a user:pass@host fragment |
d48c6a7d | 1574 | if (FirstColon + 2 <= U.end() && FirstColon[1] == '/' && FirstColon[2] == '/') |
f46e7681 AL |
1575 | FirstColon += 3; |
1576 | else | |
1577 | FirstColon += 1; | |
5933aab2 | 1578 | if (FirstColon >= U.end()) |
93bf083d AL |
1579 | return; |
1580 | ||
1581 | if (FirstColon > SingleSlash) | |
1582 | FirstColon = SingleSlash; | |
1583 | ||
3856756b AL |
1584 | // Find the colon... |
1585 | I = FirstColon + 1; | |
1d38d0e9 AL |
1586 | if (I > SingleSlash) |
1587 | I = SingleSlash; | |
f7f0d6c7 | 1588 | for (; I < SingleSlash && *I != ':'; ++I); |
5933aab2 | 1589 | string::const_iterator SecondColon = I; |
3856756b AL |
1590 | |
1591 | // Search for the @ after the colon | |
f7f0d6c7 | 1592 | for (; I < SingleSlash && *I != '@'; ++I); |
5933aab2 | 1593 | string::const_iterator At = I; |
93bf083d | 1594 | |
93bf083d AL |
1595 | // Now write the host and user/pass |
1596 | if (At == SingleSlash) | |
1597 | { | |
1598 | if (FirstColon < SingleSlash) | |
171c75f1 | 1599 | Host.assign(FirstColon,SingleSlash); |
93bf083d AL |
1600 | } |
1601 | else | |
1602 | { | |
171c75f1 | 1603 | Host.assign(At+1,SingleSlash); |
436d7eab DK |
1604 | // username and password must be encoded (RFC 3986) |
1605 | User.assign(DeQuoteString(FirstColon,SecondColon)); | |
93bf083d | 1606 | if (SecondColon < At) |
436d7eab | 1607 | Password.assign(DeQuoteString(SecondColon+1,At)); |
93bf083d AL |
1608 | } |
1609 | ||
67ff87bf AL |
1610 | // Now we parse the RFC 2732 [] hostnames. |
1611 | unsigned long PortEnd = 0; | |
1612 | InBracket = false; | |
1613 | for (unsigned I = 0; I != Host.length();) | |
1614 | { | |
1615 | if (Host[I] == '[') | |
1616 | { | |
1617 | InBracket = true; | |
1618 | Host.erase(I,1); | |
1619 | continue; | |
1620 | } | |
1621 | ||
1622 | if (InBracket == true && Host[I] == ']') | |
1623 | { | |
1624 | InBracket = false; | |
1625 | Host.erase(I,1); | |
1626 | PortEnd = I; | |
1627 | continue; | |
1628 | } | |
1629 | I++; | |
1630 | } | |
1631 | ||
1632 | // Tsk, weird. | |
1633 | if (InBracket == true) | |
1634 | { | |
171c75f1 | 1635 | Host.clear(); |
67ff87bf AL |
1636 | return; |
1637 | } | |
1638 | ||
1d38d0e9 | 1639 | // Now we parse off a port number from the hostname |
93bf083d AL |
1640 | Port = 0; |
1641 | string::size_type Pos = Host.rfind(':'); | |
67ff87bf | 1642 | if (Pos == string::npos || Pos < PortEnd) |
93bf083d AL |
1643 | return; |
1644 | ||
1645 | Port = atoi(string(Host,Pos+1).c_str()); | |
171c75f1 | 1646 | Host.assign(Host,0,Pos); |
93bf083d AL |
1647 | } |
1648 | /*}}}*/ | |
1649 | // URI::operator string - Convert the URI to a string /*{{{*/ | |
1650 | // --------------------------------------------------------------------- | |
1651 | /* */ | |
1652 | URI::operator string() | |
1653 | { | |
b8eba208 DK |
1654 | std::stringstream Res; |
1655 | ||
54cf15cb | 1656 | if (Access.empty() == false) |
b8eba208 DK |
1657 | Res << Access << ':'; |
1658 | ||
93bf083d | 1659 | if (Host.empty() == false) |
b8eba208 | 1660 | { |
54cf15cb | 1661 | if (Access.empty() == false) |
b8eba208 DK |
1662 | Res << "//"; |
1663 | ||
93bf083d AL |
1664 | if (User.empty() == false) |
1665 | { | |
5b63d2a9 MV |
1666 | // FIXME: Technically userinfo is permitted even less |
1667 | // characters than these, but this is not conveniently | |
1668 | // expressed with a blacklist. | |
b8eba208 | 1669 | Res << QuoteString(User, ":/?#[]@"); |
93bf083d | 1670 | if (Password.empty() == false) |
b8eba208 DK |
1671 | Res << ":" << QuoteString(Password, ":/?#[]@"); |
1672 | Res << "@"; | |
93bf083d | 1673 | } |
b8eba208 | 1674 | |
7834cb57 | 1675 | // Add RFC 2732 escaping characters |
b8eba208 DK |
1676 | if (Access.empty() == false && Host.find_first_of("/:") != string::npos) |
1677 | Res << '[' << Host << ']'; | |
7834cb57 | 1678 | else |
b8eba208 DK |
1679 | Res << Host; |
1680 | ||
492f957a | 1681 | if (Port != 0) |
b58e2c7c | 1682 | Res << ':' << std::to_string(Port); |
93bf083d | 1683 | } |
b8eba208 | 1684 | |
93bf083d | 1685 | if (Path.empty() == false) |
492f957a AL |
1686 | { |
1687 | if (Path[0] != '/') | |
b8eba208 | 1688 | Res << "/" << Path; |
492f957a | 1689 | else |
b8eba208 | 1690 | Res << Path; |
492f957a | 1691 | } |
b8eba208 DK |
1692 | |
1693 | return Res.str(); | |
93bf083d AL |
1694 | } |
1695 | /*}}}*/ | |
b2e465d6 | 1696 | // URI::SiteOnly - Return the schema and site for the URI /*{{{*/ |
171c75f1 | 1697 | string URI::SiteOnly(const string &URI) |
b2e465d6 AL |
1698 | { |
1699 | ::URI U(URI); | |
171c75f1 MV |
1700 | U.User.clear(); |
1701 | U.Password.clear(); | |
1702 | U.Path.clear(); | |
b2e465d6 AL |
1703 | return U; |
1704 | } | |
1705 | /*}}}*/ | |
1da3b7b8 DK |
1706 | // URI::ArchiveOnly - Return the schema, site and cleaned path for the URI /*{{{*/ |
1707 | string URI::ArchiveOnly(const string &URI) | |
1708 | { | |
1709 | ::URI U(URI); | |
1710 | U.User.clear(); | |
1711 | U.Password.clear(); | |
1712 | if (U.Path.empty() == false && U.Path[U.Path.length() - 1] == '/') | |
1713 | U.Path.erase(U.Path.length() - 1); | |
1714 | return U; | |
1715 | } | |
1716 | /*}}}*/ | |
5e02df82 | 1717 | // URI::NoUserPassword - Return the schema, site and path for the URI /*{{{*/ |
5e02df82 MV |
1718 | string URI::NoUserPassword(const string &URI) |
1719 | { | |
1720 | ::URI U(URI); | |
1721 | U.User.clear(); | |
1722 | U.Password.clear(); | |
5e02df82 MV |
1723 | return U; |
1724 | } | |
1725 | /*}}}*/ |