1 // -*- mode: cpp; mode: fold -*-
3 // $Id: strutl.cc,v 1.48 2003/07/18 14:15:11 mdz Exp $
4 /* ######################################################################
6 String Util - Some useful string functions.
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.
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>
15 ##################################################################### */
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/error.h>
40 // UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/
41 // ---------------------------------------------------------------------
42 /* This is handy to use before display some information for enduser */
43 bool UTF8ToCodeset(const char *codeset
, const string
&orig
, string
*dest
)
48 size_t insize
, bufsize
;
51 cd
= iconv_open(codeset
, "UTF-8");
52 if (cd
== (iconv_t
)(-1)) {
53 // Something went wrong
55 _error
->Error("conversion from 'UTF-8' to '%s' not available",
63 insize
= bufsize
= orig
.size();
65 inptr
= (char *)inbuf
;
66 outbuf
= new char[bufsize
];
67 size_t lastError
= -1;
71 char *outptr
= outbuf
;
72 size_t outsize
= bufsize
;
73 size_t const err
= iconv(cd
, &inptr
, &insize
, &outptr
, &outsize
);
74 dest
->append(outbuf
, outptr
- outbuf
);
75 if (err
== (size_t)(-1))
82 // replace a series of unknown multibytes with a single "?"
83 if (lastError
!= insize
) {
84 lastError
= insize
- 1;
96 outbuf
= new char[bufsize
];
110 // strstrip - Remove white space from the front and back of a string /*{{{*/
111 // ---------------------------------------------------------------------
112 /* This is handy to use when parsing a file. It also removes \n's left
113 over from fgets and company */
114 char *_strstrip(char *String
)
116 for (;*String
!= 0 && (*String
== ' ' || *String
== '\t'); String
++);
120 return _strrstrip(String
);
123 // strrstrip - Remove white space from the back of a string /*{{{*/
124 // ---------------------------------------------------------------------
125 char *_strrstrip(char *String
)
127 char *End
= String
+ strlen(String
) - 1;
128 for (;End
!= String
- 1 && (*End
== ' ' || *End
== '\t' || *End
== '\n' ||
129 *End
== '\r'); End
--);
135 // strtabexpand - Converts tabs into 8 spaces /*{{{*/
136 // ---------------------------------------------------------------------
138 char *_strtabexpand(char *String
,size_t Len
)
140 for (char *I
= String
; I
!= I
+ Len
&& *I
!= 0; I
++)
144 if (I
+ 8 > String
+ Len
)
150 /* Assume the start of the string is 0 and find the next 8 char
156 Len
= 8 - ((String
- I
) % 8);
164 memmove(I
+ Len
,I
+ 1,strlen(I
) + 1);
165 for (char *J
= I
; J
+ Len
!= I
; *I
= ' ', I
++);
170 // ParseQuoteWord - Parse a single word out of a string /*{{{*/
171 // ---------------------------------------------------------------------
172 /* This grabs a single word, converts any % escaped characters to their
173 proper values and advances the pointer. Double quotes are understood
174 and striped out as well. This is for URI/URL parsing. It also can
175 understand [] brackets.*/
176 bool ParseQuoteWord(const char *&String
,string
&Res
)
178 // Skip leading whitespace
179 const char *C
= String
;
180 for (;*C
!= 0 && *C
== ' '; C
++);
184 // Jump to the next word
185 for (;*C
!= 0 && isspace(*C
) == 0; C
++)
189 C
= strchr(C
+ 1, '"');
195 C
= strchr(C
+ 1, ']');
201 // Now de-quote characters
204 const char *Start
= String
;
206 for (I
= Buffer
; I
< Buffer
+ sizeof(Buffer
) && Start
!= C
; I
++)
208 if (*Start
== '%' && Start
+ 2 < C
&&
209 isxdigit(Start
[1]) && isxdigit(Start
[2]))
214 *I
= (char)strtol(Tmp
,0,16);
227 // Skip ending white space
228 for (;*C
!= 0 && isspace(*C
) != 0; C
++);
233 // ParseCWord - Parses a string like a C "" expression /*{{{*/
234 // ---------------------------------------------------------------------
235 /* This expects a series of space separated strings enclosed in ""'s.
236 It concatenates the ""'s into a single string. */
237 bool ParseCWord(const char *&String
,string
&Res
)
239 // Skip leading whitespace
240 const char *C
= String
;
241 for (;*C
!= 0 && *C
== ' '; C
++);
247 if (strlen(String
) >= sizeof(Buffer
))
254 for (C
++; *C
!= 0 && *C
!= '"'; C
++)
263 if (C
!= String
&& isspace(*C
) != 0 && isspace(C
[-1]) != 0)
265 if (isspace(*C
) == 0)
275 // QuoteString - Convert a string into quoted from /*{{{*/
276 // ---------------------------------------------------------------------
278 string
QuoteString(const string
&Str
, const char *Bad
)
281 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end(); ++I
)
283 if (strchr(Bad
,*I
) != 0 || isprint(*I
) == 0 ||
284 *I
== 0x25 || // percent '%' char
285 *I
<= 0x20 || *I
>= 0x7F) // control chars
288 sprintf(Buf
,"%%%02x",(int)*I
);
297 // DeQuoteString - Convert a string from quoted from /*{{{*/
298 // ---------------------------------------------------------------------
299 /* This undoes QuoteString */
300 string
DeQuoteString(const string
&Str
)
302 return DeQuoteString(Str
.begin(),Str
.end());
304 string
DeQuoteString(string::const_iterator
const &begin
,
305 string::const_iterator
const &end
)
308 for (string::const_iterator I
= begin
; I
!= end
; ++I
)
310 if (*I
== '%' && I
+ 2 < end
&&
311 isxdigit(I
[1]) && isxdigit(I
[2]))
317 Res
+= (char)strtol(Tmp
,0,16);
328 // SizeToStr - Convert a long into a human readable size /*{{{*/
329 // ---------------------------------------------------------------------
330 /* A max of 4 digits are shown before conversion to the next highest unit.
331 The max length of the string will be 5 chars unless the size is > 10
333 string
SizeToStr(double Size
)
342 /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes,
343 ExaBytes, ZettaBytes, YottaBytes */
344 char Ext
[] = {'\0','k','M','G','T','P','E','Z','Y'};
348 if (ASize
< 100 && I
!= 0)
350 sprintf(S
,"%'.1f %c",ASize
,Ext
[I
]);
356 sprintf(S
,"%'.0f %c",ASize
,Ext
[I
]);
366 // TimeToStr - Convert the time into a string /*{{{*/
367 // ---------------------------------------------------------------------
368 /* Converts a number of seconds to a hms format */
369 string
TimeToStr(unsigned long Sec
)
377 //d means days, h means hours, min means minutes, s means seconds
378 sprintf(S
,_("%lid %lih %limin %lis"),Sec
/60/60/24,(Sec
/60/60) % 24,(Sec
/60) % 60,Sec
% 60);
384 //h means hours, min means minutes, s means seconds
385 sprintf(S
,_("%lih %limin %lis"),Sec
/60/60,(Sec
/60) % 60,Sec
% 60);
391 //min means minutes, s means seconds
392 sprintf(S
,_("%limin %lis"),Sec
/60,Sec
% 60);
397 sprintf(S
,_("%lis"),Sec
);
404 // SubstVar - Substitute a string for another string /*{{{*/
405 // ---------------------------------------------------------------------
406 /* This replaces all occurances of Subst with Contents in Str. */
407 string
SubstVar(const string
&Str
,const string
&Subst
,const string
&Contents
)
409 string::size_type Pos
= 0;
410 string::size_type OldPos
= 0;
413 while (OldPos
< Str
.length() &&
414 (Pos
= Str
.find(Subst
,OldPos
)) != string::npos
)
416 Temp
+= string(Str
,OldPos
,Pos
) + Contents
;
417 OldPos
= Pos
+ Subst
.length();
423 return Temp
+ string(Str
,OldPos
);
426 string
SubstVar(string Str
,const struct SubstVar
*Vars
)
428 for (; Vars
->Subst
!= 0; Vars
++)
429 Str
= SubstVar(Str
,Vars
->Subst
,*Vars
->Contents
);
433 // OutputInDepth - return a string with separator multiplied with depth /*{{{*/
434 // ---------------------------------------------------------------------
435 /* Returns a string with the supplied separator depth + 1 times in it */
436 std::string
OutputInDepth(const unsigned long Depth
, const char* Separator
)
438 std::string output
= "";
439 for(unsigned long d
=Depth
+1; d
> 0; d
--)
440 output
.append(Separator
);
444 // URItoFileName - Convert the uri into a unique file name /*{{{*/
445 // ---------------------------------------------------------------------
446 /* This converts a URI into a safe filename. It quotes all unsafe characters
447 and converts / to _ and removes the scheme identifier. The resulting
448 file name should be unique and never occur again for a different file */
449 string
URItoFileName(const string
&URI
)
451 // Nuke 'sensitive' items
457 // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
458 string NewURI
= QuoteString(U
,"\\|{}[]<>\"^~_=!@#$%^&*");
459 replace(NewURI
.begin(),NewURI
.end(),'/','_');
463 // Base64Encode - Base64 Encoding routine for short strings /*{{{*/
464 // ---------------------------------------------------------------------
465 /* This routine performs a base64 transformation on a string. It was ripped
466 from wget and then patched and bug fixed.
468 This spec can be found in rfc2045 */
469 string
Base64Encode(const string
&S
)
472 static char tbl
[64] = {'A','B','C','D','E','F','G','H',
473 'I','J','K','L','M','N','O','P',
474 'Q','R','S','T','U','V','W','X',
475 'Y','Z','a','b','c','d','e','f',
476 'g','h','i','j','k','l','m','n',
477 'o','p','q','r','s','t','u','v',
478 'w','x','y','z','0','1','2','3',
479 '4','5','6','7','8','9','+','/'};
481 // Pre-allocate some space
483 Final
.reserve((4*S
.length() + 2)/3 + 2);
485 /* Transform the 3x8 bits to 4x6 bits, as required by
487 for (string::const_iterator I
= S
.begin(); I
< S
.end(); I
+= 3)
489 char Bits
[3] = {0,0,0};
496 Final
+= tbl
[Bits
[0] >> 2];
497 Final
+= tbl
[((Bits
[0] & 3) << 4) + (Bits
[1] >> 4)];
499 if (I
+ 1 >= S
.end())
502 Final
+= tbl
[((Bits
[1] & 0xf) << 2) + (Bits
[2] >> 6)];
504 if (I
+ 2 >= S
.end())
507 Final
+= tbl
[Bits
[2] & 0x3f];
510 /* Apply the padding elements, this tells how many bytes the remote
511 end should discard */
512 if (S
.length() % 3 == 2)
514 if (S
.length() % 3 == 1)
520 // stringcmp - Arbitrary string compare /*{{{*/
521 // ---------------------------------------------------------------------
522 /* This safely compares two non-null terminated strings of arbitrary
524 int stringcmp(const char *A
,const char *AEnd
,const char *B
,const char *BEnd
)
526 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
530 if (A
== AEnd
&& B
== BEnd
)
542 int stringcmp(string::const_iterator A
,string::const_iterator AEnd
,
543 const char *B
,const char *BEnd
)
545 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
549 if (A
== AEnd
&& B
== BEnd
)
559 int stringcmp(string::const_iterator A
,string::const_iterator AEnd
,
560 string::const_iterator B
,string::const_iterator BEnd
)
562 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
566 if (A
== AEnd
&& B
== BEnd
)
578 // stringcasecmp - Arbitrary case insensitive string compare /*{{{*/
579 // ---------------------------------------------------------------------
581 int stringcasecmp(const char *A
,const char *AEnd
,const char *B
,const char *BEnd
)
583 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
584 if (tolower_ascii(*A
) != tolower_ascii(*B
))
587 if (A
== AEnd
&& B
== BEnd
)
593 if (tolower_ascii(*A
) < tolower_ascii(*B
))
598 int stringcasecmp(string::const_iterator A
,string::const_iterator AEnd
,
599 const char *B
,const char *BEnd
)
601 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
602 if (tolower_ascii(*A
) != tolower_ascii(*B
))
605 if (A
== AEnd
&& B
== BEnd
)
611 if (tolower_ascii(*A
) < tolower_ascii(*B
))
615 int stringcasecmp(string::const_iterator A
,string::const_iterator AEnd
,
616 string::const_iterator B
,string::const_iterator BEnd
)
618 for (; A
!= AEnd
&& B
!= BEnd
; A
++, B
++)
619 if (tolower_ascii(*A
) != tolower_ascii(*B
))
622 if (A
== AEnd
&& B
== BEnd
)
628 if (tolower_ascii(*A
) < tolower_ascii(*B
))
634 // LookupTag - Lookup the value of a tag in a taged string /*{{{*/
635 // ---------------------------------------------------------------------
636 /* The format is like those used in package files and the method
637 communication system */
638 string
LookupTag(const string
&Message
,const char *Tag
,const char *Default
)
640 // Look for a matching tag.
641 int Length
= strlen(Tag
);
642 for (string::const_iterator I
= Message
.begin(); I
+ Length
< Message
.end(); ++I
)
645 if (I
[Length
] == ':' && stringcasecmp(I
,I
+Length
,Tag
) == 0)
647 // Find the end of line and strip the leading/trailing spaces
648 string::const_iterator J
;
650 for (; isspace(*I
) != 0 && I
< Message
.end(); ++I
);
651 for (J
= I
; *J
!= '\n' && J
< Message
.end(); ++J
);
652 for (; J
> I
&& isspace(J
[-1]) != 0; --J
);
657 for (; *I
!= '\n' && I
< Message
.end(); ++I
);
660 // Failed to find a match
666 // StringToBool - Converts a string into a boolean /*{{{*/
667 // ---------------------------------------------------------------------
668 /* This inspects the string to see if it is true or if it is false and
669 then returns the result. Several varients on true/false are checked. */
670 int StringToBool(const string
&Text
,int Default
)
673 int Res
= strtol(Text
.c_str(),&End
,0);
674 if (End
!= Text
.c_str() && Res
>= 0 && Res
<= 1)
677 // Check for positives
678 if (strcasecmp(Text
.c_str(),"no") == 0 ||
679 strcasecmp(Text
.c_str(),"false") == 0 ||
680 strcasecmp(Text
.c_str(),"without") == 0 ||
681 strcasecmp(Text
.c_str(),"off") == 0 ||
682 strcasecmp(Text
.c_str(),"disable") == 0)
685 // Check for negatives
686 if (strcasecmp(Text
.c_str(),"yes") == 0 ||
687 strcasecmp(Text
.c_str(),"true") == 0 ||
688 strcasecmp(Text
.c_str(),"with") == 0 ||
689 strcasecmp(Text
.c_str(),"on") == 0 ||
690 strcasecmp(Text
.c_str(),"enable") == 0)
696 // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/
697 // ---------------------------------------------------------------------
698 /* This converts a time_t into a string time representation that is
699 year 2000 complient and timezone neutral */
700 string
TimeRFC1123(time_t Date
)
703 if (gmtime_r(&Date
, &Conv
) == NULL
)
707 const char *Day
[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
708 const char *Month
[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul",
709 "Aug","Sep","Oct","Nov","Dec"};
711 snprintf(Buf
, sizeof(Buf
), "%s, %02i %s %i %02i:%02i:%02i GMT",Day
[Conv
.tm_wday
],
712 Conv
.tm_mday
,Month
[Conv
.tm_mon
],Conv
.tm_year
+1900,Conv
.tm_hour
,
713 Conv
.tm_min
,Conv
.tm_sec
);
717 // ReadMessages - Read messages from the FD /*{{{*/
718 // ---------------------------------------------------------------------
719 /* This pulls full messages from the input FD into the message buffer.
720 It assumes that messages will not pause during transit so no
721 fancy buffering is used.
723 In particular: this reads blocks from the input until it believes
724 that it's run out of input text. Each block is terminated by a
725 double newline ('\n' followed by '\n'). As noted below, there is a
726 bug in this code: it assumes that all the blocks have been read if
727 it doesn't see additional text in the buffer after the last one is
728 parsed, which will cause it to lose blocks if the last block
729 coincides with the end of the buffer.
731 bool ReadMessages(int Fd
, vector
<string
> &List
)
735 // Represents any left-over from the previous iteration of the
736 // parse loop. (i.e., if a message is split across the end
737 // of the buffer, it goes here)
738 string PartialMessage
;
742 int Res
= read(Fd
,End
,sizeof(Buffer
) - (End
-Buffer
));
743 if (Res
< 0 && errno
== EINTR
)
746 // Process is dead, this is kind of bad..
751 if (Res
< 0 && errno
== EAGAIN
)
758 // Look for the end of the message
759 for (char *I
= Buffer
; I
+ 1 < End
; I
++)
763 (I
[0] != '\n' && strncmp(I
, "\r\n\r\n", 4) != 0))
766 // Pull the message out
767 string
Message(Buffer
,I
-Buffer
);
768 PartialMessage
+= Message
;
771 for (; I
< End
&& (*I
== '\n' || *I
== '\r'); ++I
);
773 memmove(Buffer
,I
,End
-Buffer
);
776 List
.push_back(PartialMessage
);
777 PartialMessage
.clear();
781 // If there's text left in the buffer, store it
782 // in PartialMessage and throw the rest of the buffer
783 // away. This allows us to handle messages that
784 // are longer than the static buffer size.
785 PartialMessage
+= string(Buffer
, End
);
790 // BUG ALERT: if a message block happens to end at a
791 // multiple of 64000 characters, this will cause it to
792 // terminate early, leading to a badly formed block and
793 // probably crashing the method. However, this is the only
794 // way we have to find the end of the message block. I have
795 // an idea of how to fix this, but it will require changes
796 // to the protocol (essentially to mark the beginning and
797 // end of the block).
799 // -- dburrows 2008-04-02
803 if (WaitFd(Fd
) == false)
808 // MonthConv - Converts a month string into a number /*{{{*/
809 // ---------------------------------------------------------------------
810 /* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
811 Made it a bit more robust with a few tolower_ascii though. */
812 static int MonthConv(char *Month
)
814 switch (tolower_ascii(*Month
))
817 return tolower_ascii(Month
[1]) == 'p'?3:7;
823 if (tolower_ascii(Month
[1]) == 'a')
825 return tolower_ascii(Month
[2]) == 'n'?5:6;
827 return tolower_ascii(Month
[2]) == 'r'?2:4;
835 // Pretend it is January..
841 // timegm - Internal timegm if the gnu version is not available /*{{{*/
842 // ---------------------------------------------------------------------
843 /* Converts struct tm to time_t, assuming the data in tm is UTC rather
844 than local timezone (mktime assumes the latter).
846 This function is a nonstandard GNU extension that is also present on
847 the BSDs and maybe other systems. For others we follow the advice of
848 the manpage of timegm and use his portable replacement. */
850 static time_t timegm(struct tm
*t
)
852 char *tz
= getenv("TZ");
855 time_t ret
= mktime(t
);
865 // FullDateToTime - Converts a HTTP1.1 full date strings into a time_t /*{{{*/
866 // ---------------------------------------------------------------------
867 /* tries to parses a full date as specified in RFC2616 Section 3.3.1
868 with one exception: All timezones (%Z) are accepted but the protocol
869 says that it MUST be GMT, but this one is equal to UTC which we will
870 encounter from time to time (e.g. in Release files) so we accept all
871 here and just assume it is GMT (or UTC) later on */
872 bool RFC1123StrToTime(const char* const str
,time_t &time
)
875 setlocale (LC_ALL
,"C");
877 // Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
878 (strptime(str
, "%a, %d %b %Y %H:%M:%S %Z", &Tm
) == NULL
&&
879 // Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
880 strptime(str
, "%A, %d-%b-%y %H:%M:%S %Z", &Tm
) == NULL
&&
881 // Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
882 strptime(str
, "%a %b %d %H:%M:%S %Y", &Tm
) == NULL
);
883 setlocale (LC_ALL
,"");
891 // FTPMDTMStrToTime - Converts a ftp modification date into a time_t /*{{{*/
892 // ---------------------------------------------------------------------
894 bool FTPMDTMStrToTime(const char* const str
,time_t &time
)
897 // MDTM includes no whitespaces but recommend and ignored by strptime
898 if (strptime(str
, "%Y %m %d %H %M %S", &Tm
) == NULL
)
905 // StrToTime - Converts a string into a time_t /*{{{*/
906 // ---------------------------------------------------------------------
907 /* This handles all 3 populare time formats including RFC 1123, RFC 1036
908 and the C library asctime format. It requires the GNU library function
909 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
910 reason the C library does not provide any such function :< This also
911 handles the weird, but unambiguous FTP time format*/
912 bool StrToTime(const string
&Val
,time_t &Result
)
917 // Skip the day of the week
918 const char *I
= strchr(Val
.c_str(), ' ');
920 // Handle RFC 1123 time
922 if (sscanf(I
," %2d %3s %4d %2d:%2d:%2d GMT",&Tm
.tm_mday
,Month
,&Tm
.tm_year
,
923 &Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) != 6)
925 // Handle RFC 1036 time
926 if (sscanf(I
," %2d-%3s-%3d %2d:%2d:%2d GMT",&Tm
.tm_mday
,Month
,
927 &Tm
.tm_year
,&Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) == 6)
932 if (sscanf(I
," %3s %2d %2d:%2d:%2d %4d",Month
,&Tm
.tm_mday
,
933 &Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
,&Tm
.tm_year
) != 6)
936 if (sscanf(Val
.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm
.tm_year
,&Tm
.tm_mon
,
937 &Tm
.tm_mday
,&Tm
.tm_hour
,&Tm
.tm_min
,&Tm
.tm_sec
) != 6)
946 Tm
.tm_mon
= MonthConv(Month
);
949 // Convert to local time and then to GMT
950 Result
= timegm(&Tm
);
954 // StrToNum - Convert a fixed length string to a number /*{{{*/
955 // ---------------------------------------------------------------------
956 /* This is used in decoding the crazy fixed length string headers in
958 bool StrToNum(const char *Str
,unsigned long &Res
,unsigned Len
,unsigned Base
)
961 if (Len
>= sizeof(S
))
966 // All spaces is a zero
969 for (I
= 0; S
[I
] == ' '; I
++);
974 Res
= strtoul(S
,&End
,Base
);
981 // StrToNum - Convert a fixed length string to a number /*{{{*/
982 // ---------------------------------------------------------------------
983 /* This is used in decoding the crazy fixed length string headers in
985 bool StrToNum(const char *Str
,unsigned long long &Res
,unsigned Len
,unsigned Base
)
988 if (Len
>= sizeof(S
))
993 // All spaces is a zero
996 for (I
= 0; S
[I
] == ' '; I
++);
1001 Res
= strtoull(S
,&End
,Base
);
1009 // Base256ToNum - Convert a fixed length binary to a number /*{{{*/
1010 // ---------------------------------------------------------------------
1011 /* This is used in decoding the 256bit encoded fixed length fields in
1013 bool Base256ToNum(const char *Str
,unsigned long &Res
,unsigned int Len
)
1015 if ((Str
[0] & 0x80) == 0)
1019 Res
= Str
[0] & 0x7F;
1020 for(unsigned int i
= 1; i
< Len
; ++i
)
1021 Res
= (Res
<<8) + Str
[i
];
1026 // HexDigit - Convert a hex character into an integer /*{{{*/
1027 // ---------------------------------------------------------------------
1028 /* Helper for Hex2Num */
1029 static int HexDigit(int c
)
1031 if (c
>= '0' && c
<= '9')
1033 if (c
>= 'a' && c
<= 'f')
1034 return c
- 'a' + 10;
1035 if (c
>= 'A' && c
<= 'F')
1036 return c
- 'A' + 10;
1040 // Hex2Num - Convert a long hex number into a buffer /*{{{*/
1041 // ---------------------------------------------------------------------
1042 /* The length of the buffer must be exactly 1/2 the length of the string. */
1043 bool Hex2Num(const string
&Str
,unsigned char *Num
,unsigned int Length
)
1045 if (Str
.length() != Length
*2)
1048 // Convert each digit. We store it in the same order as the string
1050 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end();J
++, I
+= 2)
1052 if (isxdigit(*I
) == 0 || isxdigit(I
[1]) == 0)
1055 Num
[J
] = HexDigit(I
[0]) << 4;
1056 Num
[J
] += HexDigit(I
[1]);
1062 // TokSplitString - Split a string up by a given token /*{{{*/
1063 // ---------------------------------------------------------------------
1064 /* This is intended to be a faster splitter, it does not use dynamic
1065 memories. Input is changed to insert nulls at each token location. */
1066 bool TokSplitString(char Tok
,char *Input
,char **List
,
1067 unsigned long ListMax
)
1069 // Strip any leading spaces
1070 char *Start
= Input
;
1071 char *Stop
= Start
+ strlen(Start
);
1072 for (; *Start
!= 0 && isspace(*Start
) != 0; Start
++);
1074 unsigned long Count
= 0;
1078 // Skip to the next Token
1079 for (; Pos
!= Stop
&& *Pos
!= Tok
; Pos
++);
1081 // Back remove spaces
1083 for (; End
> Start
&& (End
[-1] == Tok
|| isspace(End
[-1]) != 0); End
--);
1086 List
[Count
++] = Start
;
1087 if (Count
>= ListMax
)
1094 for (; Pos
!= Stop
&& (*Pos
== Tok
|| isspace(*Pos
) != 0 || *Pos
== 0); Pos
++);
1102 // VectorizeString - Split a string up into a vector of strings /*{{{*/
1103 // ---------------------------------------------------------------------
1104 /* This can be used to split a given string up into a vector, so the
1105 propose is the same as in the method above and this one is a bit slower
1106 also, but the advantage is that we have an iteratable vector */
1107 vector
<string
> VectorizeString(string
const &haystack
, char const &split
)
1109 string::const_iterator start
= haystack
.begin();
1110 string::const_iterator end
= start
;
1111 vector
<string
> exploded
;
1113 for (; end
!= haystack
.end() && *end
!= split
; ++end
);
1114 exploded
.push_back(string(start
, end
));
1116 } while (end
!= haystack
.end() && (++end
) != haystack
.end());
1120 // RegexChoice - Simple regex list/list matcher /*{{{*/
1121 // ---------------------------------------------------------------------
1123 unsigned long RegexChoice(RxChoiceList
*Rxs
,const char **ListBegin
,
1124 const char **ListEnd
)
1126 for (RxChoiceList
*R
= Rxs
; R
->Str
!= 0; R
++)
1129 unsigned long Hits
= 0;
1130 for (; ListBegin
!= ListEnd
; ListBegin
++)
1132 // Check if the name is a regex
1135 for (I
= *ListBegin
; *I
!= 0; I
++)
1136 if (*I
== '.' || *I
== '?' || *I
== '*' || *I
== '|')
1141 // Compile the regex pattern
1144 if (regcomp(&Pattern
,*ListBegin
,REG_EXTENDED
| REG_ICASE
|
1150 for (RxChoiceList
*R
= Rxs
; R
->Str
!= 0; R
++)
1155 if (strcasecmp(R
->Str
,*ListBegin
) != 0)
1159 if (regexec(&Pattern
,R
->Str
,0,0,0) != 0)
1164 if (R
->Hit
== false)
1174 _error
->Warning(_("Selection %s not found"),*ListBegin
);
1180 // {str,io}printf - C format string outputter to C++ strings/iostreams /*{{{*/
1181 // ---------------------------------------------------------------------
1182 /* This is used to make the internationalization strings easier to translate
1183 and to allow reordering of parameters */
1184 static bool iovprintf(ostream
&out
, const char *format
,
1185 va_list &args
, ssize_t
&size
) {
1186 char *S
= (char*)malloc(size
);
1187 ssize_t
const n
= vsnprintf(S
, size
, format
, args
);
1188 if (n
> -1 && n
< size
) {
1201 void ioprintf(ostream
&out
,const char *format
,...)
1206 va_start(args
,format
);
1207 if (iovprintf(out
, format
, args
, size
) == true)
1212 void strprintf(string
&out
,const char *format
,...)
1216 std::ostringstream outstr
;
1218 va_start(args
,format
);
1219 if (iovprintf(outstr
, format
, args
, size
) == true)
1226 // safe_snprintf - Safer snprintf /*{{{*/
1227 // ---------------------------------------------------------------------
1228 /* This is a snprintf that will never (ever) go past 'End' and returns a
1229 pointer to the end of the new string. The returned string is always null
1230 terminated unless Buffer == end. This is a better alterantive to using
1231 consecutive snprintfs. */
1232 char *safe_snprintf(char *Buffer
,char *End
,const char *Format
,...)
1237 va_start(args
,Format
);
1242 Did
= vsnprintf(Buffer
,End
- Buffer
,Format
,args
);
1243 if (Did
< 0 || Buffer
+ Did
> End
)
1245 return Buffer
+ Did
;
1248 // StripEpoch - Remove the version "epoch" from a version string /*{{{*/
1249 // ---------------------------------------------------------------------
1250 string
StripEpoch(const string
&VerStr
)
1252 size_t i
= VerStr
.find(":");
1253 if (i
== string::npos
)
1255 return VerStr
.substr(i
+1);
1258 // tolower_ascii - tolower() function that ignores the locale /*{{{*/
1259 // ---------------------------------------------------------------------
1260 /* This little function is the most called method we have and tries
1261 therefore to do the absolut minimum - and is noteable faster than
1262 standard tolower/toupper and as a bonus avoids problems with different
1263 locales - we only operate on ascii chars anyway. */
1264 int tolower_ascii(int const c
)
1266 if (c
>= 'A' && c
<= 'Z')
1272 // CheckDomainList - See if Host is in a , seperate list /*{{{*/
1273 // ---------------------------------------------------------------------
1274 /* The domain list is a comma seperate list of domains that are suffix
1275 matched against the argument */
1276 bool CheckDomainList(const string
&Host
,const string
&List
)
1278 string::const_iterator Start
= List
.begin();
1279 for (string::const_iterator Cur
= List
.begin(); Cur
<= List
.end(); ++Cur
)
1281 if (Cur
< List
.end() && *Cur
!= ',')
1284 // Match the end of the string..
1285 if ((Host
.size() >= (unsigned)(Cur
- Start
)) &&
1287 stringcasecmp(Host
.end() - (Cur
- Start
),Host
.end(),Start
,Cur
) == 0)
1295 // DeEscapeString - unescape (\0XX and \xXX) from a string /*{{{*/
1296 // ---------------------------------------------------------------------
1298 string
DeEscapeString(const string
&input
)
1301 string::const_iterator it
;
1303 for (it
= input
.begin(); it
!= input
.end(); ++it
)
1305 // just copy non-escape chars
1312 // deal with double escape
1314 (it
+ 1 < input
.end()) && it
[1] == '\\')
1318 // advance iterator one step further
1323 // ensure we have a char to read
1324 if (it
+ 1 == input
.end())
1332 if (it
+ 2 <= input
.end()) {
1336 output
+= (char)strtol(tmp
, 0, 8);
1341 if (it
+ 2 <= input
.end()) {
1345 output
+= (char)strtol(tmp
, 0, 16);
1350 // FIXME: raise exception here?
1357 // URI::CopyFrom - Copy from an object /*{{{*/
1358 // ---------------------------------------------------------------------
1359 /* This parses the URI into all of its components */
1360 void URI::CopyFrom(const string
&U
)
1362 string::const_iterator I
= U
.begin();
1364 // Locate the first colon, this separates the scheme
1365 for (; I
< U
.end() && *I
!= ':' ; ++I
);
1366 string::const_iterator FirstColon
= I
;
1368 /* Determine if this is a host type URI with a leading double //
1369 and then search for the first single / */
1370 string::const_iterator SingleSlash
= I
;
1371 if (I
+ 3 < U
.end() && I
[1] == '/' && I
[2] == '/')
1374 /* Find the / indicating the end of the hostname, ignoring /'s in the
1376 bool InBracket
= false;
1377 for (; SingleSlash
< U
.end() && (*SingleSlash
!= '/' || InBracket
== true); ++SingleSlash
)
1379 if (*SingleSlash
== '[')
1381 if (InBracket
== true && *SingleSlash
== ']')
1385 if (SingleSlash
> U
.end())
1386 SingleSlash
= U
.end();
1388 // We can now write the access and path specifiers
1389 Access
.assign(U
.begin(),FirstColon
);
1390 if (SingleSlash
!= U
.end())
1391 Path
.assign(SingleSlash
,U
.end());
1392 if (Path
.empty() == true)
1395 // Now we attempt to locate a user:pass@host fragment
1396 if (FirstColon
+ 2 <= U
.end() && FirstColon
[1] == '/' && FirstColon
[2] == '/')
1400 if (FirstColon
>= U
.end())
1403 if (FirstColon
> SingleSlash
)
1404 FirstColon
= SingleSlash
;
1406 // Find the colon...
1408 if (I
> SingleSlash
)
1410 for (; I
< SingleSlash
&& *I
!= ':'; ++I
);
1411 string::const_iterator SecondColon
= I
;
1413 // Search for the @ after the colon
1414 for (; I
< SingleSlash
&& *I
!= '@'; ++I
);
1415 string::const_iterator At
= I
;
1417 // Now write the host and user/pass
1418 if (At
== SingleSlash
)
1420 if (FirstColon
< SingleSlash
)
1421 Host
.assign(FirstColon
,SingleSlash
);
1425 Host
.assign(At
+1,SingleSlash
);
1426 // username and password must be encoded (RFC 3986)
1427 User
.assign(DeQuoteString(FirstColon
,SecondColon
));
1428 if (SecondColon
< At
)
1429 Password
.assign(DeQuoteString(SecondColon
+1,At
));
1432 // Now we parse the RFC 2732 [] hostnames.
1433 unsigned long PortEnd
= 0;
1435 for (unsigned I
= 0; I
!= Host
.length();)
1444 if (InBracket
== true && Host
[I
] == ']')
1455 if (InBracket
== true)
1461 // Now we parse off a port number from the hostname
1463 string::size_type Pos
= Host
.rfind(':');
1464 if (Pos
== string::npos
|| Pos
< PortEnd
)
1467 Port
= atoi(string(Host
,Pos
+1).c_str());
1468 Host
.assign(Host
,0,Pos
);
1471 // URI::operator string - Convert the URI to a string /*{{{*/
1472 // ---------------------------------------------------------------------
1474 URI::operator string()
1478 if (Access
.empty() == false)
1481 if (Host
.empty() == false)
1483 if (Access
.empty() == false)
1486 if (User
.empty() == false)
1488 // FIXME: Technically userinfo is permitted even less
1489 // characters than these, but this is not conveniently
1490 // expressed with a blacklist.
1491 Res
+= QuoteString(User
, ":/?#[]@");
1492 if (Password
.empty() == false)
1493 Res
+= ":" + QuoteString(Password
, ":/?#[]@");
1497 // Add RFC 2732 escaping characters
1498 if (Access
.empty() == false &&
1499 (Host
.find('/') != string::npos
|| Host
.find(':') != string::npos
))
1500 Res
+= '[' + Host
+ ']';
1507 sprintf(S
,":%u",Port
);
1512 if (Path
.empty() == false)
1523 // URI::SiteOnly - Return the schema and site for the URI /*{{{*/
1524 // ---------------------------------------------------------------------
1526 string
URI::SiteOnly(const string
&URI
)
1535 // URI::NoUserPassword - Return the schema, site and path for the URI /*{{{*/
1536 // ---------------------------------------------------------------------
1538 string
URI::NoUserPassword(const string
&URI
)