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