]>
Commit | Line | Data |
---|---|---|
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 <apt-pkg/strutl.h> | |
19 | #include <apt-pkg/fileutl.h> | |
20 | #include <apt-pkg/error.h> | |
21 | ||
22 | #include <apti18n.h> | |
23 | ||
24 | #include <ctype.h> | |
25 | #include <string.h> | |
26 | #include <stdio.h> | |
27 | #include <algorithm> | |
28 | #include <unistd.h> | |
29 | #include <regex.h> | |
30 | #include <errno.h> | |
31 | #include <stdarg.h> | |
32 | #include <iconv.h> | |
33 | ||
34 | #include "config.h" | |
35 | ||
36 | using namespace std; | |
37 | /*}}}*/ | |
38 | ||
39 | // UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/ | |
40 | // --------------------------------------------------------------------- | |
41 | /* This is handy to use before display some information for enduser */ | |
42 | bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest) | |
43 | { | |
44 | iconv_t cd; | |
45 | const char *inbuf; | |
46 | char *inptr, *outbuf, *outptr; | |
47 | size_t insize, outsize; | |
48 | ||
49 | cd = iconv_open(codeset, "UTF-8"); | |
50 | if (cd == (iconv_t)(-1)) { | |
51 | // Something went wrong | |
52 | if (errno == EINVAL) | |
53 | _error->Error("conversion from 'UTF-8' to '%s' not available", | |
54 | codeset); | |
55 | else | |
56 | perror("iconv_open"); | |
57 | ||
58 | // Clean the destination string | |
59 | *dest = ""; | |
60 | ||
61 | return false; | |
62 | } | |
63 | ||
64 | insize = outsize = orig.size(); | |
65 | inbuf = orig.data(); | |
66 | inptr = (char *)inbuf; | |
67 | outbuf = new char[insize+1]; | |
68 | outptr = outbuf; | |
69 | ||
70 | iconv(cd, &inptr, &insize, &outptr, &outsize); | |
71 | *outptr = '\0'; | |
72 | ||
73 | *dest = outbuf; | |
74 | delete[] outbuf; | |
75 | ||
76 | iconv_close(cd); | |
77 | ||
78 | return true; | |
79 | } | |
80 | /*}}}*/ | |
81 | // strstrip - Remove white space from the front and back of a string /*{{{*/ | |
82 | // --------------------------------------------------------------------- | |
83 | /* This is handy to use when parsing a file. It also removes \n's left | |
84 | over from fgets and company */ | |
85 | char *_strstrip(char *String) | |
86 | { | |
87 | for (;*String != 0 && (*String == ' ' || *String == '\t'); String++); | |
88 | ||
89 | if (*String == 0) | |
90 | return String; | |
91 | ||
92 | char *End = String + strlen(String) - 1; | |
93 | for (;End != String - 1 && (*End == ' ' || *End == '\t' || *End == '\n' || | |
94 | *End == '\r'); End--); | |
95 | End++; | |
96 | *End = 0; | |
97 | return String; | |
98 | }; | |
99 | /*}}}*/ | |
100 | // strtabexpand - Converts tabs into 8 spaces /*{{{*/ | |
101 | // --------------------------------------------------------------------- | |
102 | /* */ | |
103 | char *_strtabexpand(char *String,size_t Len) | |
104 | { | |
105 | for (char *I = String; I != I + Len && *I != 0; I++) | |
106 | { | |
107 | if (*I != '\t') | |
108 | continue; | |
109 | if (I + 8 > String + Len) | |
110 | { | |
111 | *I = 0; | |
112 | return String; | |
113 | } | |
114 | ||
115 | /* Assume the start of the string is 0 and find the next 8 char | |
116 | division */ | |
117 | int Len; | |
118 | if (String == I) | |
119 | Len = 1; | |
120 | else | |
121 | Len = 8 - ((String - I) % 8); | |
122 | Len -= 2; | |
123 | if (Len <= 0) | |
124 | { | |
125 | *I = ' '; | |
126 | continue; | |
127 | } | |
128 | ||
129 | memmove(I + Len,I + 1,strlen(I) + 1); | |
130 | for (char *J = I; J + Len != I; *I = ' ', I++); | |
131 | } | |
132 | return String; | |
133 | } | |
134 | /*}}}*/ | |
135 | // ParseQuoteWord - Parse a single word out of a string /*{{{*/ | |
136 | // --------------------------------------------------------------------- | |
137 | /* This grabs a single word, converts any % escaped characters to their | |
138 | proper values and advances the pointer. Double quotes are understood | |
139 | and striped out as well. This is for URI/URL parsing. It also can | |
140 | understand [] brackets.*/ | |
141 | bool ParseQuoteWord(const char *&String,string &Res) | |
142 | { | |
143 | // Skip leading whitespace | |
144 | const char *C = String; | |
145 | for (;*C != 0 && *C == ' '; C++); | |
146 | if (*C == 0) | |
147 | return false; | |
148 | ||
149 | // Jump to the next word | |
150 | for (;*C != 0 && isspace(*C) == 0; C++) | |
151 | { | |
152 | if (*C == '"') | |
153 | { | |
154 | for (C++; *C != 0 && *C != '"'; C++); | |
155 | if (*C == 0) | |
156 | return false; | |
157 | } | |
158 | if (*C == '[') | |
159 | { | |
160 | for (C++; *C != 0 && *C != ']'; C++); | |
161 | if (*C == 0) | |
162 | return false; | |
163 | } | |
164 | } | |
165 | ||
166 | // Now de-quote characters | |
167 | char Buffer[1024]; | |
168 | char Tmp[3]; | |
169 | const char *Start = String; | |
170 | char *I; | |
171 | for (I = Buffer; I < Buffer + sizeof(Buffer) && Start != C; I++) | |
172 | { | |
173 | if (*Start == '%' && Start + 2 < C) | |
174 | { | |
175 | Tmp[0] = Start[1]; | |
176 | Tmp[1] = Start[2]; | |
177 | Tmp[2] = 0; | |
178 | *I = (char)strtol(Tmp,0,16); | |
179 | Start += 3; | |
180 | continue; | |
181 | } | |
182 | if (*Start != '"') | |
183 | *I = *Start; | |
184 | else | |
185 | I--; | |
186 | Start++; | |
187 | } | |
188 | *I = 0; | |
189 | Res = Buffer; | |
190 | ||
191 | // Skip ending white space | |
192 | for (;*C != 0 && isspace(*C) != 0; C++); | |
193 | String = C; | |
194 | return true; | |
195 | } | |
196 | /*}}}*/ | |
197 | // ParseCWord - Parses a string like a C "" expression /*{{{*/ | |
198 | // --------------------------------------------------------------------- | |
199 | /* This expects a series of space separated strings enclosed in ""'s. | |
200 | It concatenates the ""'s into a single string. */ | |
201 | bool ParseCWord(const char *&String,string &Res) | |
202 | { | |
203 | // Skip leading whitespace | |
204 | const char *C = String; | |
205 | for (;*C != 0 && *C == ' '; C++); | |
206 | if (*C == 0) | |
207 | return false; | |
208 | ||
209 | char Buffer[1024]; | |
210 | char *Buf = Buffer; | |
211 | if (strlen(String) >= sizeof(Buffer)) | |
212 | return false; | |
213 | ||
214 | for (; *C != 0; C++) | |
215 | { | |
216 | if (*C == '"') | |
217 | { | |
218 | for (C++; *C != 0 && *C != '"'; C++) | |
219 | *Buf++ = *C; | |
220 | ||
221 | if (*C == 0) | |
222 | return false; | |
223 | ||
224 | continue; | |
225 | } | |
226 | ||
227 | if (C != String && isspace(*C) != 0 && isspace(C[-1]) != 0) | |
228 | continue; | |
229 | if (isspace(*C) == 0) | |
230 | return false; | |
231 | *Buf++ = ' '; | |
232 | } | |
233 | *Buf = 0; | |
234 | Res = Buffer; | |
235 | String = C; | |
236 | return true; | |
237 | } | |
238 | /*}}}*/ | |
239 | // QuoteString - Convert a string into quoted from /*{{{*/ | |
240 | // --------------------------------------------------------------------- | |
241 | /* */ | |
242 | string QuoteString(const string &Str, const char *Bad) | |
243 | { | |
244 | string Res; | |
245 | for (string::const_iterator I = Str.begin(); I != Str.end(); I++) | |
246 | { | |
247 | if (strchr(Bad,*I) != 0 || isprint(*I) == 0 || | |
248 | *I <= 0x20 || *I >= 0x7F) | |
249 | { | |
250 | char Buf[10]; | |
251 | sprintf(Buf,"%%%02x",(int)*I); | |
252 | Res += Buf; | |
253 | } | |
254 | else | |
255 | Res += *I; | |
256 | } | |
257 | return Res; | |
258 | } | |
259 | /*}}}*/ | |
260 | // DeQuoteString - Convert a string from quoted from /*{{{*/ | |
261 | // --------------------------------------------------------------------- | |
262 | /* This undoes QuoteString */ | |
263 | string DeQuoteString(const string &Str) | |
264 | { | |
265 | string Res; | |
266 | for (string::const_iterator I = Str.begin(); I != Str.end(); I++) | |
267 | { | |
268 | if (*I == '%' && I + 2 < Str.end()) | |
269 | { | |
270 | char Tmp[3]; | |
271 | Tmp[0] = I[1]; | |
272 | Tmp[1] = I[2]; | |
273 | Tmp[2] = 0; | |
274 | Res += (char)strtol(Tmp,0,16); | |
275 | I += 2; | |
276 | continue; | |
277 | } | |
278 | else | |
279 | Res += *I; | |
280 | } | |
281 | return Res; | |
282 | } | |
283 | ||
284 | /*}}}*/ | |
285 | // SizeToStr - Convert a long into a human readable size /*{{{*/ | |
286 | // --------------------------------------------------------------------- | |
287 | /* A max of 4 digits are shown before conversion to the next highest unit. | |
288 | The max length of the string will be 5 chars unless the size is > 10 | |
289 | YottaBytes (E24) */ | |
290 | string SizeToStr(double Size) | |
291 | { | |
292 | char S[300]; | |
293 | double ASize; | |
294 | if (Size >= 0) | |
295 | ASize = Size; | |
296 | else | |
297 | ASize = -1*Size; | |
298 | ||
299 | /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes, | |
300 | ExaBytes, ZettaBytes, YottaBytes */ | |
301 | char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'}; | |
302 | int I = 0; | |
303 | while (I <= 8) | |
304 | { | |
305 | if (ASize < 100 && I != 0) | |
306 | { | |
307 | sprintf(S,"%.1f%c",ASize,Ext[I]); | |
308 | break; | |
309 | } | |
310 | ||
311 | if (ASize < 10000) | |
312 | { | |
313 | sprintf(S,"%.0f%c",ASize,Ext[I]); | |
314 | break; | |
315 | } | |
316 | ASize /= 1000.0; | |
317 | I++; | |
318 | } | |
319 | ||
320 | return S; | |
321 | } | |
322 | /*}}}*/ | |
323 | // TimeToStr - Convert the time into a string /*{{{*/ | |
324 | // --------------------------------------------------------------------- | |
325 | /* Converts a number of seconds to a hms format */ | |
326 | string TimeToStr(unsigned long Sec) | |
327 | { | |
328 | char S[300]; | |
329 | ||
330 | while (1) | |
331 | { | |
332 | if (Sec > 60*60*24) | |
333 | { | |
334 | sprintf(S,"%lid %lih%limin%lis",Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60); | |
335 | break; | |
336 | } | |
337 | ||
338 | if (Sec > 60*60) | |
339 | { | |
340 | sprintf(S,"%lih%limin%lis",Sec/60/60,(Sec/60) % 60,Sec % 60); | |
341 | break; | |
342 | } | |
343 | ||
344 | if (Sec > 60) | |
345 | { | |
346 | sprintf(S,"%limin%lis",Sec/60,Sec % 60); | |
347 | break; | |
348 | } | |
349 | ||
350 | sprintf(S,"%lis",Sec); | |
351 | break; | |
352 | } | |
353 | ||
354 | return S; | |
355 | } | |
356 | /*}}}*/ | |
357 | // SubstVar - Substitute a string for another string /*{{{*/ | |
358 | // --------------------------------------------------------------------- | |
359 | /* This replaces all occurances of Subst with Contents in Str. */ | |
360 | string SubstVar(const string &Str,const string &Subst,const string &Contents) | |
361 | { | |
362 | string::size_type Pos = 0; | |
363 | string::size_type OldPos = 0; | |
364 | string Temp; | |
365 | ||
366 | while (OldPos < Str.length() && | |
367 | (Pos = Str.find(Subst,OldPos)) != string::npos) | |
368 | { | |
369 | Temp += string(Str,OldPos,Pos) + Contents; | |
370 | OldPos = Pos + Subst.length(); | |
371 | } | |
372 | ||
373 | if (OldPos == 0) | |
374 | return Str; | |
375 | ||
376 | return Temp + string(Str,OldPos); | |
377 | } | |
378 | ||
379 | string SubstVar(string Str,const struct SubstVar *Vars) | |
380 | { | |
381 | for (; Vars->Subst != 0; Vars++) | |
382 | Str = SubstVar(Str,Vars->Subst,*Vars->Contents); | |
383 | return Str; | |
384 | } | |
385 | /*}}}*/ | |
386 | // URItoFileName - Convert the uri into a unique file name /*{{{*/ | |
387 | // --------------------------------------------------------------------- | |
388 | /* This converts a URI into a safe filename. It quotes all unsafe characters | |
389 | and converts / to _ and removes the scheme identifier. The resulting | |
390 | file name should be unique and never occur again for a different file */ | |
391 | string URItoFileName(const string &URI) | |
392 | { | |
393 | // Nuke 'sensitive' items | |
394 | ::URI U(URI); | |
395 | U.User.clear(); | |
396 | U.Password.clear(); | |
397 | U.Access.clear(); | |
398 | ||
399 | // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF"; | |
400 | string NewURI = QuoteString(U,"\\|{}[]<>\"^~_=!@#$%^&*"); | |
401 | replace(NewURI.begin(),NewURI.end(),'/','_'); | |
402 | return NewURI; | |
403 | } | |
404 | /*}}}*/ | |
405 | // Base64Encode - Base64 Encoding routine for short strings /*{{{*/ | |
406 | // --------------------------------------------------------------------- | |
407 | /* This routine performs a base64 transformation on a string. It was ripped | |
408 | from wget and then patched and bug fixed. | |
409 | ||
410 | This spec can be found in rfc2045 */ | |
411 | string Base64Encode(const string &S) | |
412 | { | |
413 | // Conversion table. | |
414 | static char tbl[64] = {'A','B','C','D','E','F','G','H', | |
415 | 'I','J','K','L','M','N','O','P', | |
416 | 'Q','R','S','T','U','V','W','X', | |
417 | 'Y','Z','a','b','c','d','e','f', | |
418 | 'g','h','i','j','k','l','m','n', | |
419 | 'o','p','q','r','s','t','u','v', | |
420 | 'w','x','y','z','0','1','2','3', | |
421 | '4','5','6','7','8','9','+','/'}; | |
422 | ||
423 | // Pre-allocate some space | |
424 | string Final; | |
425 | Final.reserve((4*S.length() + 2)/3 + 2); | |
426 | ||
427 | /* Transform the 3x8 bits to 4x6 bits, as required by | |
428 | base64. */ | |
429 | for (string::const_iterator I = S.begin(); I < S.end(); I += 3) | |
430 | { | |
431 | char Bits[3] = {0,0,0}; | |
432 | Bits[0] = I[0]; | |
433 | if (I + 1 < S.end()) | |
434 | Bits[1] = I[1]; | |
435 | if (I + 2 < S.end()) | |
436 | Bits[2] = I[2]; | |
437 | ||
438 | Final += tbl[Bits[0] >> 2]; | |
439 | Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)]; | |
440 | ||
441 | if (I + 1 >= S.end()) | |
442 | break; | |
443 | ||
444 | Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)]; | |
445 | ||
446 | if (I + 2 >= S.end()) | |
447 | break; | |
448 | ||
449 | Final += tbl[Bits[2] & 0x3f]; | |
450 | } | |
451 | ||
452 | /* Apply the padding elements, this tells how many bytes the remote | |
453 | end should discard */ | |
454 | if (S.length() % 3 == 2) | |
455 | Final += '='; | |
456 | if (S.length() % 3 == 1) | |
457 | Final += "=="; | |
458 | ||
459 | return Final; | |
460 | } | |
461 | /*}}}*/ | |
462 | // stringcmp - Arbitary string compare /*{{{*/ | |
463 | // --------------------------------------------------------------------- | |
464 | /* This safely compares two non-null terminated strings of arbitary | |
465 | length */ | |
466 | int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd) | |
467 | { | |
468 | for (; A != AEnd && B != BEnd; A++, B++) | |
469 | if (*A != *B) | |
470 | break; | |
471 | ||
472 | if (A == AEnd && B == BEnd) | |
473 | return 0; | |
474 | if (A == AEnd) | |
475 | return 1; | |
476 | if (B == BEnd) | |
477 | return -1; | |
478 | if (*A < *B) | |
479 | return -1; | |
480 | return 1; | |
481 | } | |
482 | ||
483 | #if __GNUC__ >= 3 | |
484 | int stringcmp(string::const_iterator A,string::const_iterator AEnd, | |
485 | const char *B,const char *BEnd) | |
486 | { | |
487 | for (; A != AEnd && B != BEnd; A++, B++) | |
488 | if (*A != *B) | |
489 | break; | |
490 | ||
491 | if (A == AEnd && B == BEnd) | |
492 | return 0; | |
493 | if (A == AEnd) | |
494 | return 1; | |
495 | if (B == BEnd) | |
496 | return -1; | |
497 | if (*A < *B) | |
498 | return -1; | |
499 | return 1; | |
500 | } | |
501 | int stringcmp(string::const_iterator A,string::const_iterator AEnd, | |
502 | string::const_iterator B,string::const_iterator BEnd) | |
503 | { | |
504 | for (; A != AEnd && B != BEnd; A++, B++) | |
505 | if (*A != *B) | |
506 | break; | |
507 | ||
508 | if (A == AEnd && B == BEnd) | |
509 | return 0; | |
510 | if (A == AEnd) | |
511 | return 1; | |
512 | if (B == BEnd) | |
513 | return -1; | |
514 | if (*A < *B) | |
515 | return -1; | |
516 | return 1; | |
517 | } | |
518 | #endif | |
519 | /*}}}*/ | |
520 | // stringcasecmp - Arbitary case insensitive string compare /*{{{*/ | |
521 | // --------------------------------------------------------------------- | |
522 | /* */ | |
523 | int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd) | |
524 | { | |
525 | for (; A != AEnd && B != BEnd; A++, B++) | |
526 | if (toupper(*A) != toupper(*B)) | |
527 | break; | |
528 | ||
529 | if (A == AEnd && B == BEnd) | |
530 | return 0; | |
531 | if (A == AEnd) | |
532 | return 1; | |
533 | if (B == BEnd) | |
534 | return -1; | |
535 | if (toupper(*A) < toupper(*B)) | |
536 | return -1; | |
537 | return 1; | |
538 | } | |
539 | #if __GNUC__ >= 3 | |
540 | int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, | |
541 | const char *B,const char *BEnd) | |
542 | { | |
543 | for (; A != AEnd && B != BEnd; A++, B++) | |
544 | if (toupper(*A) != toupper(*B)) | |
545 | break; | |
546 | ||
547 | if (A == AEnd && B == BEnd) | |
548 | return 0; | |
549 | if (A == AEnd) | |
550 | return 1; | |
551 | if (B == BEnd) | |
552 | return -1; | |
553 | if (toupper(*A) < toupper(*B)) | |
554 | return -1; | |
555 | return 1; | |
556 | } | |
557 | int stringcasecmp(string::const_iterator A,string::const_iterator AEnd, | |
558 | string::const_iterator B,string::const_iterator BEnd) | |
559 | { | |
560 | for (; A != AEnd && B != BEnd; A++, B++) | |
561 | if (toupper(*A) != toupper(*B)) | |
562 | break; | |
563 | ||
564 | if (A == AEnd && B == BEnd) | |
565 | return 0; | |
566 | if (A == AEnd) | |
567 | return 1; | |
568 | if (B == BEnd) | |
569 | return -1; | |
570 | if (toupper(*A) < toupper(*B)) | |
571 | return -1; | |
572 | return 1; | |
573 | } | |
574 | #endif | |
575 | /*}}}*/ | |
576 | // LookupTag - Lookup the value of a tag in a taged string /*{{{*/ | |
577 | // --------------------------------------------------------------------- | |
578 | /* The format is like those used in package files and the method | |
579 | communication system */ | |
580 | string LookupTag(const string &Message,const char *Tag,const char *Default) | |
581 | { | |
582 | // Look for a matching tag. | |
583 | int Length = strlen(Tag); | |
584 | for (string::const_iterator I = Message.begin(); I + Length < Message.end(); I++) | |
585 | { | |
586 | // Found the tag | |
587 | if (I[Length] == ':' && stringcasecmp(I,I+Length,Tag) == 0) | |
588 | { | |
589 | // Find the end of line and strip the leading/trailing spaces | |
590 | string::const_iterator J; | |
591 | I += Length + 1; | |
592 | for (; isspace(*I) != 0 && I < Message.end(); I++); | |
593 | for (J = I; *J != '\n' && J < Message.end(); J++); | |
594 | for (; J > I && isspace(J[-1]) != 0; J--); | |
595 | ||
596 | return string(I,J); | |
597 | } | |
598 | ||
599 | for (; *I != '\n' && I < Message.end(); I++); | |
600 | } | |
601 | ||
602 | // Failed to find a match | |
603 | if (Default == 0) | |
604 | return string(); | |
605 | return Default; | |
606 | } | |
607 | /*}}}*/ | |
608 | // StringToBool - Converts a string into a boolean /*{{{*/ | |
609 | // --------------------------------------------------------------------- | |
610 | /* This inspects the string to see if it is true or if it is false and | |
611 | then returns the result. Several varients on true/false are checked. */ | |
612 | int StringToBool(const string &Text,int Default) | |
613 | { | |
614 | char *End; | |
615 | int Res = strtol(Text.c_str(),&End,0); | |
616 | if (End != Text.c_str() && Res >= 0 && Res <= 1) | |
617 | return Res; | |
618 | ||
619 | // Check for positives | |
620 | if (strcasecmp(Text.c_str(),"no") == 0 || | |
621 | strcasecmp(Text.c_str(),"false") == 0 || | |
622 | strcasecmp(Text.c_str(),"without") == 0 || | |
623 | strcasecmp(Text.c_str(),"off") == 0 || | |
624 | strcasecmp(Text.c_str(),"disable") == 0) | |
625 | return 0; | |
626 | ||
627 | // Check for negatives | |
628 | if (strcasecmp(Text.c_str(),"yes") == 0 || | |
629 | strcasecmp(Text.c_str(),"true") == 0 || | |
630 | strcasecmp(Text.c_str(),"with") == 0 || | |
631 | strcasecmp(Text.c_str(),"on") == 0 || | |
632 | strcasecmp(Text.c_str(),"enable") == 0) | |
633 | return 1; | |
634 | ||
635 | return Default; | |
636 | } | |
637 | /*}}}*/ | |
638 | // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/ | |
639 | // --------------------------------------------------------------------- | |
640 | /* This converts a time_t into a string time representation that is | |
641 | year 2000 complient and timezone neutral */ | |
642 | string TimeRFC1123(time_t Date) | |
643 | { | |
644 | struct tm Conv = *gmtime(&Date); | |
645 | char Buf[300]; | |
646 | ||
647 | const char *Day[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; | |
648 | const char *Month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul", | |
649 | "Aug","Sep","Oct","Nov","Dec"}; | |
650 | ||
651 | sprintf(Buf,"%s, %02i %s %i %02i:%02i:%02i GMT",Day[Conv.tm_wday], | |
652 | Conv.tm_mday,Month[Conv.tm_mon],Conv.tm_year+1900,Conv.tm_hour, | |
653 | Conv.tm_min,Conv.tm_sec); | |
654 | return Buf; | |
655 | } | |
656 | /*}}}*/ | |
657 | // ReadMessages - Read messages from the FD /*{{{*/ | |
658 | // --------------------------------------------------------------------- | |
659 | /* This pulls full messages from the input FD into the message buffer. | |
660 | It assumes that messages will not pause during transit so no | |
661 | fancy buffering is used. */ | |
662 | bool ReadMessages(int Fd, vector<string> &List) | |
663 | { | |
664 | char Buffer[64000]; | |
665 | char *End = Buffer; | |
666 | ||
667 | while (1) | |
668 | { | |
669 | int Res = read(Fd,End,sizeof(Buffer) - (End-Buffer)); | |
670 | if (Res < 0 && errno == EINTR) | |
671 | continue; | |
672 | ||
673 | // Process is dead, this is kind of bad.. | |
674 | if (Res == 0) | |
675 | return false; | |
676 | ||
677 | // No data | |
678 | if (Res < 0 && errno == EAGAIN) | |
679 | return true; | |
680 | if (Res < 0) | |
681 | return false; | |
682 | ||
683 | End += Res; | |
684 | ||
685 | // Look for the end of the message | |
686 | for (char *I = Buffer; I + 1 < End; I++) | |
687 | { | |
688 | if (I[0] != '\n' || I[1] != '\n') | |
689 | continue; | |
690 | ||
691 | // Pull the message out | |
692 | string Message(Buffer,I-Buffer); | |
693 | ||
694 | // Fix up the buffer | |
695 | for (; I < End && *I == '\n'; I++); | |
696 | End -= I-Buffer; | |
697 | memmove(Buffer,I,End-Buffer); | |
698 | I = Buffer; | |
699 | ||
700 | List.push_back(Message); | |
701 | } | |
702 | if (End == Buffer) | |
703 | return true; | |
704 | ||
705 | if (WaitFd(Fd) == false) | |
706 | return false; | |
707 | } | |
708 | } | |
709 | /*}}}*/ | |
710 | // MonthConv - Converts a month string into a number /*{{{*/ | |
711 | // --------------------------------------------------------------------- | |
712 | /* This was lifted from the boa webserver which lifted it from 'wn-v1.07' | |
713 | Made it a bit more robust with a few touppers though. */ | |
714 | static int MonthConv(char *Month) | |
715 | { | |
716 | switch (toupper(*Month)) | |
717 | { | |
718 | case 'A': | |
719 | return toupper(Month[1]) == 'P'?3:7; | |
720 | case 'D': | |
721 | return 11; | |
722 | case 'F': | |
723 | return 1; | |
724 | case 'J': | |
725 | if (toupper(Month[1]) == 'A') | |
726 | return 0; | |
727 | return toupper(Month[2]) == 'N'?5:6; | |
728 | case 'M': | |
729 | return toupper(Month[2]) == 'R'?2:4; | |
730 | case 'N': | |
731 | return 10; | |
732 | case 'O': | |
733 | return 9; | |
734 | case 'S': | |
735 | return 8; | |
736 | ||
737 | // Pretend it is January.. | |
738 | default: | |
739 | return 0; | |
740 | } | |
741 | } | |
742 | /*}}}*/ | |
743 | // timegm - Internal timegm function if gnu is not available /*{{{*/ | |
744 | // --------------------------------------------------------------------- | |
745 | /* Ripped this evil little function from wget - I prefer the use of | |
746 | GNU timegm if possible as this technique will have interesting problems | |
747 | with leap seconds, timezones and other. | |
748 | ||
749 | Converts struct tm to time_t, assuming the data in tm is UTC rather | |
750 | than local timezone (mktime assumes the latter). | |
751 | ||
752 | Contributed by Roger Beeman <beeman@cisco.com>, with the help of | |
753 | Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO. */ | |
754 | ||
755 | /* Turned it into an autoconf check, because GNU is not the only thing which | |
756 | can provide timegm. -- 2002-09-22, Joel Baker */ | |
757 | ||
758 | #ifndef HAVE_TIMEGM // Now with autoconf! | |
759 | static time_t timegm(struct tm *t) | |
760 | { | |
761 | time_t tl, tb; | |
762 | ||
763 | tl = mktime (t); | |
764 | if (tl == -1) | |
765 | return -1; | |
766 | tb = mktime (gmtime (&tl)); | |
767 | return (tl <= tb ? (tl + (tl - tb)) : (tl - (tb - tl))); | |
768 | } | |
769 | #endif | |
770 | /*}}}*/ | |
771 | // StrToTime - Converts a string into a time_t /*{{{*/ | |
772 | // --------------------------------------------------------------------- | |
773 | /* This handles all 3 populare time formats including RFC 1123, RFC 1036 | |
774 | and the C library asctime format. It requires the GNU library function | |
775 | 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar | |
776 | reason the C library does not provide any such function :< This also | |
777 | handles the weird, but unambiguous FTP time format*/ | |
778 | bool StrToTime(const string &Val,time_t &Result) | |
779 | { | |
780 | struct tm Tm; | |
781 | char Month[10]; | |
782 | const char *I = Val.c_str(); | |
783 | ||
784 | // Skip the day of the week | |
785 | for (;*I != 0 && *I != ' '; I++); | |
786 | ||
787 | // Handle RFC 1123 time | |
788 | Month[0] = 0; | |
789 | if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year, | |
790 | &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6) | |
791 | { | |
792 | // Handle RFC 1036 time | |
793 | if (sscanf(I," %d-%3s-%d %d:%d:%d GMT",&Tm.tm_mday,Month, | |
794 | &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6) | |
795 | Tm.tm_year += 1900; | |
796 | else | |
797 | { | |
798 | // asctime format | |
799 | if (sscanf(I," %3s %d %d:%d:%d %d",Month,&Tm.tm_mday, | |
800 | &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6) | |
801 | { | |
802 | // 'ftp' time | |
803 | if (sscanf(Val.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm.tm_year,&Tm.tm_mon, | |
804 | &Tm.tm_mday,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6) | |
805 | return false; | |
806 | Tm.tm_mon--; | |
807 | } | |
808 | } | |
809 | } | |
810 | ||
811 | Tm.tm_isdst = 0; | |
812 | if (Month[0] != 0) | |
813 | Tm.tm_mon = MonthConv(Month); | |
814 | Tm.tm_year -= 1900; | |
815 | ||
816 | // Convert to local time and then to GMT | |
817 | Result = timegm(&Tm); | |
818 | return true; | |
819 | } | |
820 | /*}}}*/ | |
821 | // StrToNum - Convert a fixed length string to a number /*{{{*/ | |
822 | // --------------------------------------------------------------------- | |
823 | /* This is used in decoding the crazy fixed length string headers in | |
824 | tar and ar files. */ | |
825 | bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base) | |
826 | { | |
827 | char S[30]; | |
828 | if (Len >= sizeof(S)) | |
829 | return false; | |
830 | memcpy(S,Str,Len); | |
831 | S[Len] = 0; | |
832 | ||
833 | // All spaces is a zero | |
834 | Res = 0; | |
835 | unsigned I; | |
836 | for (I = 0; S[I] == ' '; I++); | |
837 | if (S[I] == 0) | |
838 | return true; | |
839 | ||
840 | char *End; | |
841 | Res = strtoul(S,&End,Base); | |
842 | if (End == S) | |
843 | return false; | |
844 | ||
845 | return true; | |
846 | } | |
847 | /*}}}*/ | |
848 | // HexDigit - Convert a hex character into an integer /*{{{*/ | |
849 | // --------------------------------------------------------------------- | |
850 | /* Helper for Hex2Num */ | |
851 | static int HexDigit(int c) | |
852 | { | |
853 | if (c >= '0' && c <= '9') | |
854 | return c - '0'; | |
855 | if (c >= 'a' && c <= 'f') | |
856 | return c - 'a' + 10; | |
857 | if (c >= 'A' && c <= 'F') | |
858 | return c - 'A' + 10; | |
859 | return 0; | |
860 | } | |
861 | /*}}}*/ | |
862 | // Hex2Num - Convert a long hex number into a buffer /*{{{*/ | |
863 | // --------------------------------------------------------------------- | |
864 | /* The length of the buffer must be exactly 1/2 the length of the string. */ | |
865 | bool Hex2Num(const string &Str,unsigned char *Num,unsigned int Length) | |
866 | { | |
867 | if (Str.length() != Length*2) | |
868 | return false; | |
869 | ||
870 | // Convert each digit. We store it in the same order as the string | |
871 | int J = 0; | |
872 | for (string::const_iterator I = Str.begin(); I != Str.end();J++, I += 2) | |
873 | { | |
874 | if (isxdigit(*I) == 0 || isxdigit(I[1]) == 0) | |
875 | return false; | |
876 | ||
877 | Num[J] = HexDigit(I[0]) << 4; | |
878 | Num[J] += HexDigit(I[1]); | |
879 | } | |
880 | ||
881 | return true; | |
882 | } | |
883 | /*}}}*/ | |
884 | // TokSplitString - Split a string up by a given token /*{{{*/ | |
885 | // --------------------------------------------------------------------- | |
886 | /* This is intended to be a faster splitter, it does not use dynamic | |
887 | memories. Input is changed to insert nulls at each token location. */ | |
888 | bool TokSplitString(char Tok,char *Input,char **List, | |
889 | unsigned long ListMax) | |
890 | { | |
891 | // Strip any leading spaces | |
892 | char *Start = Input; | |
893 | char *Stop = Start + strlen(Start); | |
894 | for (; *Start != 0 && isspace(*Start) != 0; Start++); | |
895 | ||
896 | unsigned long Count = 0; | |
897 | char *Pos = Start; | |
898 | while (Pos != Stop) | |
899 | { | |
900 | // Skip to the next Token | |
901 | for (; Pos != Stop && *Pos != Tok; Pos++); | |
902 | ||
903 | // Back remove spaces | |
904 | char *End = Pos; | |
905 | for (; End > Start && (End[-1] == Tok || isspace(End[-1]) != 0); End--); | |
906 | *End = 0; | |
907 | ||
908 | List[Count++] = Start; | |
909 | if (Count >= ListMax) | |
910 | { | |
911 | List[Count-1] = 0; | |
912 | return false; | |
913 | } | |
914 | ||
915 | // Advance pos | |
916 | for (; Pos != Stop && (*Pos == Tok || isspace(*Pos) != 0 || *Pos == 0); Pos++); | |
917 | Start = Pos; | |
918 | } | |
919 | ||
920 | List[Count] = 0; | |
921 | return true; | |
922 | } | |
923 | /*}}}*/ | |
924 | // RegexChoice - Simple regex list/list matcher /*{{{*/ | |
925 | // --------------------------------------------------------------------- | |
926 | /* */ | |
927 | unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, | |
928 | const char **ListEnd) | |
929 | { | |
930 | for (RxChoiceList *R = Rxs; R->Str != 0; R++) | |
931 | R->Hit = false; | |
932 | ||
933 | unsigned long Hits = 0; | |
934 | for (; ListBegin != ListEnd; ListBegin++) | |
935 | { | |
936 | // Check if the name is a regex | |
937 | const char *I; | |
938 | bool Regex = true; | |
939 | for (I = *ListBegin; *I != 0; I++) | |
940 | if (*I == '.' || *I == '?' || *I == '*' || *I == '|') | |
941 | break; | |
942 | if (*I == 0) | |
943 | Regex = false; | |
944 | ||
945 | // Compile the regex pattern | |
946 | regex_t Pattern; | |
947 | if (Regex == true) | |
948 | if (regcomp(&Pattern,*ListBegin,REG_EXTENDED | REG_ICASE | | |
949 | REG_NOSUB) != 0) | |
950 | Regex = false; | |
951 | ||
952 | // Search the list | |
953 | bool Done = false; | |
954 | for (RxChoiceList *R = Rxs; R->Str != 0; R++) | |
955 | { | |
956 | if (R->Str[0] == 0) | |
957 | continue; | |
958 | ||
959 | if (strcasecmp(R->Str,*ListBegin) != 0) | |
960 | { | |
961 | if (Regex == false) | |
962 | continue; | |
963 | if (regexec(&Pattern,R->Str,0,0,0) != 0) | |
964 | continue; | |
965 | } | |
966 | Done = true; | |
967 | ||
968 | if (R->Hit == false) | |
969 | Hits++; | |
970 | ||
971 | R->Hit = true; | |
972 | } | |
973 | ||
974 | if (Regex == true) | |
975 | regfree(&Pattern); | |
976 | ||
977 | if (Done == false) | |
978 | _error->Warning(_("Selection %s not found"),*ListBegin); | |
979 | } | |
980 | ||
981 | return Hits; | |
982 | } | |
983 | /*}}}*/ | |
984 | // ioprintf - C format string outputter to C++ iostreams /*{{{*/ | |
985 | // --------------------------------------------------------------------- | |
986 | /* This is used to make the internationalization strings easier to translate | |
987 | and to allow reordering of parameters */ | |
988 | void ioprintf(ostream &out,const char *format,...) | |
989 | { | |
990 | va_list args; | |
991 | va_start(args,format); | |
992 | ||
993 | // sprintf the description | |
994 | char S[400]; | |
995 | vsnprintf(S,sizeof(S),format,args); | |
996 | out << S; | |
997 | } | |
998 | /*}}}*/ | |
999 | // safe_snprintf - Safer snprintf /*{{{*/ | |
1000 | // --------------------------------------------------------------------- | |
1001 | /* This is a snprintf that will never (ever) go past 'End' and returns a | |
1002 | pointer to the end of the new string. The returned string is always null | |
1003 | terminated unless Buffer == end. This is a better alterantive to using | |
1004 | consecutive snprintfs. */ | |
1005 | char *safe_snprintf(char *Buffer,char *End,const char *Format,...) | |
1006 | { | |
1007 | va_list args; | |
1008 | unsigned long Did; | |
1009 | ||
1010 | va_start(args,Format); | |
1011 | ||
1012 | if (End <= Buffer) | |
1013 | return End; | |
1014 | ||
1015 | Did = vsnprintf(Buffer,End - Buffer,Format,args); | |
1016 | if (Did < 0 || Buffer + Did > End) | |
1017 | return End; | |
1018 | return Buffer + Did; | |
1019 | } | |
1020 | /*}}}*/ | |
1021 | ||
1022 | // CheckDomainList - See if Host is in a , seperate list /*{{{*/ | |
1023 | // --------------------------------------------------------------------- | |
1024 | /* The domain list is a comma seperate list of domains that are suffix | |
1025 | matched against the argument */ | |
1026 | bool CheckDomainList(const string &Host,const string &List) | |
1027 | { | |
1028 | string::const_iterator Start = List.begin(); | |
1029 | for (string::const_iterator Cur = List.begin(); Cur <= List.end(); Cur++) | |
1030 | { | |
1031 | if (Cur < List.end() && *Cur != ',') | |
1032 | continue; | |
1033 | ||
1034 | // Match the end of the string.. | |
1035 | if ((Host.size() >= (unsigned)(Cur - Start)) && | |
1036 | Cur - Start != 0 && | |
1037 | stringcasecmp(Host.end() - (Cur - Start),Host.end(),Start,Cur) == 0) | |
1038 | return true; | |
1039 | ||
1040 | Start = Cur + 1; | |
1041 | } | |
1042 | return false; | |
1043 | } | |
1044 | /*}}}*/ | |
1045 | ||
1046 | // URI::CopyFrom - Copy from an object /*{{{*/ | |
1047 | // --------------------------------------------------------------------- | |
1048 | /* This parses the URI into all of its components */ | |
1049 | void URI::CopyFrom(const string &U) | |
1050 | { | |
1051 | string::const_iterator I = U.begin(); | |
1052 | ||
1053 | // Locate the first colon, this separates the scheme | |
1054 | for (; I < U.end() && *I != ':' ; I++); | |
1055 | string::const_iterator FirstColon = I; | |
1056 | ||
1057 | /* Determine if this is a host type URI with a leading double // | |
1058 | and then search for the first single / */ | |
1059 | string::const_iterator SingleSlash = I; | |
1060 | if (I + 3 < U.end() && I[1] == '/' && I[2] == '/') | |
1061 | SingleSlash += 3; | |
1062 | ||
1063 | /* Find the / indicating the end of the hostname, ignoring /'s in the | |
1064 | square brackets */ | |
1065 | bool InBracket = false; | |
1066 | for (; SingleSlash < U.end() && (*SingleSlash != '/' || InBracket == true); SingleSlash++) | |
1067 | { | |
1068 | if (*SingleSlash == '[') | |
1069 | InBracket = true; | |
1070 | if (InBracket == true && *SingleSlash == ']') | |
1071 | InBracket = false; | |
1072 | } | |
1073 | ||
1074 | if (SingleSlash > U.end()) | |
1075 | SingleSlash = U.end(); | |
1076 | ||
1077 | // We can now write the access and path specifiers | |
1078 | Access.assign(U.begin(),FirstColon); | |
1079 | if (SingleSlash != U.end()) | |
1080 | Path.assign(SingleSlash,U.end()); | |
1081 | if (Path.empty() == true) | |
1082 | Path = "/"; | |
1083 | ||
1084 | // Now we attempt to locate a user:pass@host fragment | |
1085 | if (FirstColon + 2 <= U.end() && FirstColon[1] == '/' && FirstColon[2] == '/') | |
1086 | FirstColon += 3; | |
1087 | else | |
1088 | FirstColon += 1; | |
1089 | if (FirstColon >= U.end()) | |
1090 | return; | |
1091 | ||
1092 | if (FirstColon > SingleSlash) | |
1093 | FirstColon = SingleSlash; | |
1094 | ||
1095 | // Find the colon... | |
1096 | I = FirstColon + 1; | |
1097 | if (I > SingleSlash) | |
1098 | I = SingleSlash; | |
1099 | for (; I < SingleSlash && *I != ':'; I++); | |
1100 | string::const_iterator SecondColon = I; | |
1101 | ||
1102 | // Search for the @ after the colon | |
1103 | for (; I < SingleSlash && *I != '@'; I++); | |
1104 | string::const_iterator At = I; | |
1105 | ||
1106 | // Now write the host and user/pass | |
1107 | if (At == SingleSlash) | |
1108 | { | |
1109 | if (FirstColon < SingleSlash) | |
1110 | Host.assign(FirstColon,SingleSlash); | |
1111 | } | |
1112 | else | |
1113 | { | |
1114 | Host.assign(At+1,SingleSlash); | |
1115 | User.assign(FirstColon,SecondColon); | |
1116 | if (SecondColon < At) | |
1117 | Password.assign(SecondColon+1,At); | |
1118 | } | |
1119 | ||
1120 | // Now we parse the RFC 2732 [] hostnames. | |
1121 | unsigned long PortEnd = 0; | |
1122 | InBracket = false; | |
1123 | for (unsigned I = 0; I != Host.length();) | |
1124 | { | |
1125 | if (Host[I] == '[') | |
1126 | { | |
1127 | InBracket = true; | |
1128 | Host.erase(I,1); | |
1129 | continue; | |
1130 | } | |
1131 | ||
1132 | if (InBracket == true && Host[I] == ']') | |
1133 | { | |
1134 | InBracket = false; | |
1135 | Host.erase(I,1); | |
1136 | PortEnd = I; | |
1137 | continue; | |
1138 | } | |
1139 | I++; | |
1140 | } | |
1141 | ||
1142 | // Tsk, weird. | |
1143 | if (InBracket == true) | |
1144 | { | |
1145 | Host.clear(); | |
1146 | return; | |
1147 | } | |
1148 | ||
1149 | // Now we parse off a port number from the hostname | |
1150 | Port = 0; | |
1151 | string::size_type Pos = Host.rfind(':'); | |
1152 | if (Pos == string::npos || Pos < PortEnd) | |
1153 | return; | |
1154 | ||
1155 | Port = atoi(string(Host,Pos+1).c_str()); | |
1156 | Host.assign(Host,0,Pos); | |
1157 | } | |
1158 | /*}}}*/ | |
1159 | // URI::operator string - Convert the URI to a string /*{{{*/ | |
1160 | // --------------------------------------------------------------------- | |
1161 | /* */ | |
1162 | URI::operator string() | |
1163 | { | |
1164 | string Res; | |
1165 | ||
1166 | if (Access.empty() == false) | |
1167 | Res = Access + ':'; | |
1168 | ||
1169 | if (Host.empty() == false) | |
1170 | { | |
1171 | if (Access.empty() == false) | |
1172 | Res += "//"; | |
1173 | ||
1174 | if (User.empty() == false) | |
1175 | { | |
1176 | Res += User; | |
1177 | if (Password.empty() == false) | |
1178 | Res += ":" + Password; | |
1179 | Res += "@"; | |
1180 | } | |
1181 | ||
1182 | // Add RFC 2732 escaping characters | |
1183 | if (Access.empty() == false && | |
1184 | (Host.find('/') != string::npos || Host.find(':') != string::npos)) | |
1185 | Res += '[' + Host + ']'; | |
1186 | else | |
1187 | Res += Host; | |
1188 | ||
1189 | if (Port != 0) | |
1190 | { | |
1191 | char S[30]; | |
1192 | sprintf(S,":%u",Port); | |
1193 | Res += S; | |
1194 | } | |
1195 | } | |
1196 | ||
1197 | if (Path.empty() == false) | |
1198 | { | |
1199 | if (Path[0] != '/') | |
1200 | Res += "/" + Path; | |
1201 | else | |
1202 | Res += Path; | |
1203 | } | |
1204 | ||
1205 | return Res; | |
1206 | } | |
1207 | /*}}}*/ | |
1208 | // URI::SiteOnly - Return the schema and site for the URI /*{{{*/ | |
1209 | // --------------------------------------------------------------------- | |
1210 | /* */ | |
1211 | string URI::SiteOnly(const string &URI) | |
1212 | { | |
1213 | ::URI U(URI); | |
1214 | U.User.clear(); | |
1215 | U.Password.clear(); | |
1216 | U.Path.clear(); | |
1217 | U.Port = 0; | |
1218 | return U; | |
1219 | } | |
1220 | /*}}}*/ |