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