strutl.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: strutl.cc,v 1.4 1999/10/24 06:53:12 jgg Exp $
  4. /* ######################################################################
  5. String Util - Some usefull string functions.
  6. These have been collected from here and there to do all sorts of usefull
  7. things to strings. They are usefull in file parsers, URI handlers and
  8. especially in APT methods.
  9. This source is placed in the Public Domain, do with it what you will
  10. It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
  11. ##################################################################### */
  12. /*}}}*/
  13. // Includes /*{{{*/
  14. #ifdef __GNUG__
  15. #pragma implementation "dsync/strutl.h"
  16. #endif
  17. #include <dsync/strutl.h>
  18. #include <dsync/fileutl.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. /*}}}*/
  25. // strstrip - Remove white space from the front and back of a string /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* This is handy to use when parsing a file. It also removes \n's left
  28. over from fgets and company */
  29. char *_strstrip(char *String)
  30. {
  31. for (;*String != 0 && (*String == ' ' || *String == '\t'); String++);
  32. if (*String == 0)
  33. return String;
  34. char *End = String + strlen(String) - 1;
  35. for (;End != String - 1 && (*End == ' ' || *End == '\t' || *End == '\n' ||
  36. *End == '\r'); End--);
  37. End++;
  38. *End = 0;
  39. return String;
  40. };
  41. /*}}}*/
  42. // strtabexpand - Converts tabs into 8 spaces /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* */
  45. char *_strtabexpand(char *String,size_t Len)
  46. {
  47. for (char *I = String; I != I + Len && *I != 0; I++)
  48. {
  49. if (*I != '\t')
  50. continue;
  51. if (I + 8 > String + Len)
  52. {
  53. *I = 0;
  54. return String;
  55. }
  56. /* Assume the start of the string is 0 and find the next 8 char
  57. division */
  58. int Len;
  59. if (String == I)
  60. Len = 1;
  61. else
  62. Len = 8 - ((String - I) % 8);
  63. Len -= 2;
  64. if (Len <= 0)
  65. {
  66. *I = ' ';
  67. continue;
  68. }
  69. memmove(I + Len,I + 1,strlen(I) + 1);
  70. for (char *J = I; J + Len != I; *I = ' ', I++);
  71. }
  72. return String;
  73. }
  74. /*}}}*/
  75. // ParseQuoteWord - Parse a single word out of a string /*{{{*/
  76. // ---------------------------------------------------------------------
  77. /* This grabs a single word, converts any % escaped characters to their
  78. proper values and advances the pointer. Double quotes are understood
  79. and striped out as well. This is for URI/URL parsing. */
  80. bool ParseQuoteWord(const char *&String,string &Res)
  81. {
  82. // Skip leading whitespace
  83. const char *C = String;
  84. for (;*C != 0 && *C == ' '; C++);
  85. if (*C == 0)
  86. return false;
  87. // Jump to the next word
  88. for (;*C != 0 && isspace(*C) == 0; C++)
  89. {
  90. if (*C == '"')
  91. {
  92. for (C++;*C != 0 && *C != '"'; C++);
  93. if (*C == 0)
  94. return false;
  95. }
  96. }
  97. // Now de-quote characters
  98. char Buffer[1024];
  99. char Tmp[3];
  100. const char *Start = String;
  101. char *I;
  102. for (I = Buffer; I < Buffer + sizeof(Buffer) && Start != C; I++)
  103. {
  104. if (*Start == '%' && Start + 2 < C)
  105. {
  106. Tmp[0] = Start[1];
  107. Tmp[1] = Start[2];
  108. Tmp[2] = 0;
  109. *I = (char)strtol(Tmp,0,16);
  110. Start += 3;
  111. continue;
  112. }
  113. if (*Start != '"')
  114. *I = *Start;
  115. else
  116. I--;
  117. Start++;
  118. }
  119. *I = 0;
  120. Res = Buffer;
  121. // Skip ending white space
  122. for (;*C != 0 && isspace(*C) != 0; C++);
  123. String = C;
  124. return true;
  125. }
  126. /*}}}*/
  127. // ParseCWord - Parses a string like a C "" expression /*{{{*/
  128. // ---------------------------------------------------------------------
  129. /* This expects a series of space separated strings enclosed in ""'s.
  130. It concatenates the ""'s into a single string. */
  131. bool ParseCWord(const char *String,string &Res)
  132. {
  133. // Skip leading whitespace
  134. const char *C = String;
  135. for (;*C != 0 && *C == ' '; C++);
  136. if (*C == 0)
  137. return false;
  138. char Buffer[1024];
  139. char *Buf = Buffer;
  140. if (strlen(String) >= sizeof(Buffer))
  141. return false;
  142. for (; *C != 0; C++)
  143. {
  144. if (*C == '"')
  145. {
  146. for (C++; *C != 0 && *C != '"'; C++)
  147. *Buf++ = *C;
  148. if (*C == 0)
  149. return false;
  150. continue;
  151. }
  152. if (C != String && isspace(*C) != 0 && isspace(C[-1]) != 0)
  153. continue;
  154. if (isspace(*C) == 0)
  155. return false;
  156. *Buf++ = ' ';
  157. }
  158. *Buf = 0;
  159. Res = Buffer;
  160. return true;
  161. }
  162. /*}}}*/
  163. // QuoteString - Convert a string into quoted from /*{{{*/
  164. // ---------------------------------------------------------------------
  165. /* */
  166. string QuoteString(string Str,const char *Bad)
  167. {
  168. string Res;
  169. for (string::iterator I = Str.begin(); I != Str.end(); I++)
  170. {
  171. if (strchr(Bad,*I) != 0 || isprint(*I) == 0 ||
  172. *I <= 0x20 || *I >= 0x7F)
  173. {
  174. char Buf[10];
  175. sprintf(Buf,"%%%02x",(int)*I);
  176. Res += Buf;
  177. }
  178. else
  179. Res += *I;
  180. }
  181. return Res;
  182. }
  183. /*}}}*/
  184. // DeQuoteString - Convert a string from quoted from /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* This undoes QuoteString */
  187. string DeQuoteString(string Str)
  188. {
  189. string Res;
  190. for (string::iterator I = Str.begin(); I != Str.end(); I++)
  191. {
  192. if (*I == '%' && I + 2 < Str.end())
  193. {
  194. char Tmp[3];
  195. Tmp[0] = I[1];
  196. Tmp[1] = I[2];
  197. Tmp[2] = 0;
  198. Res += (char)strtol(Tmp,0,16);
  199. I += 2;
  200. continue;
  201. }
  202. else
  203. Res += *I;
  204. }
  205. return Res;
  206. }
  207. /*}}}*/
  208. // SizeToStr - Convert a long into a human readable size /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* A max of 4 digits are shown before conversion to the next highest unit.
  211. The max length of the string will be 5 chars unless the size is > 10
  212. YottaBytes (E24) */
  213. string SizeToStr(double Size)
  214. {
  215. char S[300];
  216. double ASize;
  217. if (Size >= 0)
  218. ASize = Size;
  219. else
  220. ASize = -1*Size;
  221. /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes,
  222. ExaBytes, ZettaBytes, YottaBytes */
  223. char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'};
  224. int I = 0;
  225. while (I <= 8)
  226. {
  227. if (ASize < 100 && I != 0)
  228. {
  229. sprintf(S,"%.1f%c",ASize,Ext[I]);
  230. break;
  231. }
  232. if (ASize < 10000)
  233. {
  234. sprintf(S,"%.0f%c",ASize,Ext[I]);
  235. break;
  236. }
  237. ASize /= 1000.0;
  238. I++;
  239. }
  240. return S;
  241. }
  242. /*}}}*/
  243. // TimeToStr - Convert the time into a string /*{{{*/
  244. // ---------------------------------------------------------------------
  245. /* Converts a number of seconds to a hms format */
  246. string TimeToStr(unsigned long Sec)
  247. {
  248. char S[300];
  249. while (1)
  250. {
  251. if (Sec > 60*60*24)
  252. {
  253. sprintf(S,"%lid %lih%lim%lis",Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60);
  254. break;
  255. }
  256. if (Sec > 60*60)
  257. {
  258. sprintf(S,"%lih%lim%lis",Sec/60/60,(Sec/60) % 60,Sec % 60);
  259. break;
  260. }
  261. if (Sec > 60)
  262. {
  263. sprintf(S,"%lim%lis",Sec/60,Sec % 60);
  264. break;
  265. }
  266. sprintf(S,"%lis",Sec);
  267. break;
  268. }
  269. return S;
  270. }
  271. /*}}}*/
  272. // SubstVar - Substitute a string for another string /*{{{*/
  273. // ---------------------------------------------------------------------
  274. /* This replaces all occurances of Subst with Contents in Str. */
  275. string SubstVar(string Str,string Subst,string Contents)
  276. {
  277. string::size_type Pos = 0;
  278. string::size_type OldPos = 0;
  279. string Temp;
  280. while (OldPos < Str.length() &&
  281. (Pos = Str.find(Subst,OldPos)) != string::npos)
  282. {
  283. Temp += string(Str,OldPos,Pos) + Contents;
  284. OldPos = Pos + Subst.length();
  285. }
  286. if (OldPos == 0)
  287. return Str;
  288. return Temp + string(Str,OldPos);
  289. }
  290. /*}}}*/
  291. // URItoFileName - Convert the uri into a unique file name /*{{{*/
  292. // ---------------------------------------------------------------------
  293. /* This converts a URI into a safe filename. It quotes all unsafe characters
  294. and converts / to _ and removes the scheme identifier. The resulting
  295. file name should be unique and never occur again for a different file */
  296. string URItoFileName(string URI)
  297. {
  298. // Nuke 'sensitive' items
  299. ::URI U(URI);
  300. U.User = string();
  301. U.Password = string();
  302. U.Access = "";
  303. // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
  304. URI = QuoteString(U,"\\|{}[]<>\"^~_=!@#$%^&*");
  305. string::iterator J = URI.begin();
  306. for (; J != URI.end(); J++)
  307. if (*J == '/')
  308. *J = '_';
  309. return URI;
  310. }
  311. /*}}}*/
  312. // Base64Encode - Base64 Encoding routine for short strings /*{{{*/
  313. // ---------------------------------------------------------------------
  314. /* This routine performs a base64 transformation on a string. It was ripped
  315. from wget and then patched and bug fixed.
  316. This spec can be found in rfc2045 */
  317. string Base64Encode(string S)
  318. {
  319. // Conversion table.
  320. static char tbl[64] = {'A','B','C','D','E','F','G','H',
  321. 'I','J','K','L','M','N','O','P',
  322. 'Q','R','S','T','U','V','W','X',
  323. 'Y','Z','a','b','c','d','e','f',
  324. 'g','h','i','j','k','l','m','n',
  325. 'o','p','q','r','s','t','u','v',
  326. 'w','x','y','z','0','1','2','3',
  327. '4','5','6','7','8','9','+','/'};
  328. // Pre-allocate some space
  329. string Final;
  330. Final.reserve((4*S.length() + 2)/3 + 2);
  331. /* Transform the 3x8 bits to 4x6 bits, as required by
  332. base64. */
  333. for (string::const_iterator I = S.begin(); I < S.end(); I += 3)
  334. {
  335. char Bits[3] = {0,0,0};
  336. Bits[0] = I[0];
  337. if (I + 1 < S.end())
  338. Bits[1] = I[1];
  339. if (I + 2 < S.end())
  340. Bits[2] = I[2];
  341. Final += tbl[Bits[0] >> 2];
  342. Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)];
  343. if (I + 1 >= S.end())
  344. break;
  345. Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)];
  346. if (I + 2 >= S.end())
  347. break;
  348. Final += tbl[Bits[2] & 0x3f];
  349. }
  350. /* Apply the padding elements, this tells how many bytes the remote
  351. end should discard */
  352. if (S.length() % 3 == 2)
  353. Final += '=';
  354. if (S.length() % 3 == 1)
  355. Final += "==";
  356. return Final;
  357. }
  358. /*}}}*/
  359. // stringcmp - Arbitary string compare /*{{{*/
  360. // ---------------------------------------------------------------------
  361. /* This safely compares two non-null terminated strings of arbitary
  362. length */
  363. int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
  364. {
  365. for (; A != AEnd && B != BEnd; A++, B++)
  366. if (*A != *B)
  367. break;
  368. if (A == AEnd && B == BEnd)
  369. return 0;
  370. if (A == AEnd)
  371. return 1;
  372. if (B == BEnd)
  373. return -1;
  374. if (*A < *B)
  375. return -1;
  376. return 1;
  377. }
  378. /*}}}*/
  379. // stringcasecmp - Arbitary case insensitive string compare /*{{{*/
  380. // ---------------------------------------------------------------------
  381. /* */
  382. int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
  383. {
  384. for (; A != AEnd && B != BEnd; A++, B++)
  385. if (toupper(*A) != toupper(*B))
  386. break;
  387. if (A == AEnd && B == BEnd)
  388. return 0;
  389. if (A == AEnd)
  390. return 1;
  391. if (B == BEnd)
  392. return -1;
  393. if (toupper(*A) < toupper(*B))
  394. return -1;
  395. return 1;
  396. }
  397. /*}}}*/
  398. // LookupTag - Lookup the value of a tag in a taged string /*{{{*/
  399. // ---------------------------------------------------------------------
  400. /* The format is like those used in package files and the method
  401. communication system */
  402. string LookupTag(string Message,const char *Tag,const char *Default)
  403. {
  404. // Look for a matching tag.
  405. int Length = strlen(Tag);
  406. for (string::iterator I = Message.begin(); I + Length < Message.end(); I++)
  407. {
  408. // Found the tag
  409. const char *i = Message.c_str() + (I - Message.begin());
  410. if (I[Length] == ':' && stringcasecmp(i,i+Length,Tag) == 0)
  411. {
  412. // Find the end of line and strip the leading/trailing spaces
  413. string::iterator J;
  414. I += Length + 1;
  415. for (; isspace(*I) != 0 && I < Message.end(); I++);
  416. for (J = I; *J != '\n' && J < Message.end(); J++);
  417. for (; J > I && isspace(J[-1]) != 0; J--);
  418. return string(i,J-I);
  419. }
  420. for (; *I != '\n' && I < Message.end(); I++);
  421. }
  422. // Failed to find a match
  423. if (Default == 0)
  424. return string();
  425. return Default;
  426. }
  427. /*}}}*/
  428. // StringToBool - Converts a string into a boolean /*{{{*/
  429. // ---------------------------------------------------------------------
  430. /* This inspects the string to see if it is true or if it is false and
  431. then returns the result. Several varients on true/false are checked. */
  432. int StringToBool(string Text,int Default)
  433. {
  434. char *End;
  435. int Res = strtol(Text.c_str(),&End,0);
  436. if (End != Text.c_str() && Res >= 0 && Res <= 1)
  437. return Res;
  438. // Check for positives
  439. if (strcasecmp(Text.c_str(),"no") == 0 ||
  440. strcasecmp(Text.c_str(),"false") == 0 ||
  441. strcasecmp(Text.c_str(),"without") == 0 ||
  442. strcasecmp(Text.c_str(),"off") == 0 ||
  443. strcasecmp(Text.c_str(),"disable") == 0)
  444. return 0;
  445. // Check for negatives
  446. if (strcasecmp(Text.c_str(),"yes") == 0 ||
  447. strcasecmp(Text.c_str(),"true") == 0 ||
  448. strcasecmp(Text.c_str(),"with") == 0 ||
  449. strcasecmp(Text.c_str(),"on") == 0 ||
  450. strcasecmp(Text.c_str(),"enable") == 0)
  451. return 1;
  452. return Default;
  453. }
  454. /*}}}*/
  455. // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/
  456. // ---------------------------------------------------------------------
  457. /* This converts a time_t into a string time representation that is
  458. year 2000 complient and timezone neutral */
  459. string TimeRFC1123(time_t Date)
  460. {
  461. struct tm Conv = *gmtime(&Date);
  462. char Buf[300];
  463. const char *Day[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  464. const char *Month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul",
  465. "Aug","Sep","Oct","Nov","Dec"};
  466. sprintf(Buf,"%s, %02i %s %i %02i:%02i:%02i GMT",Day[Conv.tm_wday],
  467. Conv.tm_mday,Month[Conv.tm_mon],Conv.tm_year+1900,Conv.tm_hour,
  468. Conv.tm_min,Conv.tm_sec);
  469. return Buf;
  470. }
  471. /*}}}*/
  472. // ReadMessages - Read messages from the FD /*{{{*/
  473. // ---------------------------------------------------------------------
  474. /* This pulls full messages from the input FD into the message buffer.
  475. It assumes that messages will not pause during transit so no
  476. fancy buffering is used. */
  477. bool ReadMessages(int Fd, vector<string> &List)
  478. {
  479. char Buffer[4000];
  480. char *End = Buffer;
  481. while (1)
  482. {
  483. int Res = read(Fd,End,sizeof(Buffer) - (End-Buffer));
  484. if (Res < 0 && errno == EINTR)
  485. continue;
  486. // Process is dead, this is kind of bad..
  487. if (Res == 0)
  488. return false;
  489. // No data
  490. if (Res <= 0)
  491. return true;
  492. End += Res;
  493. // Look for the end of the message
  494. for (char *I = Buffer; I + 1 < End; I++)
  495. {
  496. if (I[0] != '\n' || I[1] != '\n')
  497. continue;
  498. // Pull the message out
  499. string Message(Buffer,0,I-Buffer);
  500. // Fix up the buffer
  501. for (; I < End && *I == '\n'; I++);
  502. End -= I-Buffer;
  503. memmove(Buffer,I,End-Buffer);
  504. I = Buffer;
  505. List.push_back(Message);
  506. }
  507. if (End == Buffer)
  508. return true;
  509. if (WaitFd(Fd) == false)
  510. return false;
  511. }
  512. }
  513. /*}}}*/
  514. // MonthConv - Converts a month string into a number /*{{{*/
  515. // ---------------------------------------------------------------------
  516. /* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
  517. Made it a bit more robust with a few touppers though. */
  518. static int MonthConv(char *Month)
  519. {
  520. switch (toupper(*Month))
  521. {
  522. case 'A':
  523. return toupper(Month[1]) == 'P'?3:7;
  524. case 'D':
  525. return 11;
  526. case 'F':
  527. return 1;
  528. case 'J':
  529. if (toupper(Month[1]) == 'A')
  530. return 0;
  531. return toupper(Month[2]) == 'N'?5:6;
  532. case 'M':
  533. return toupper(Month[2]) == 'R'?2:4;
  534. case 'N':
  535. return 10;
  536. case 'O':
  537. return 9;
  538. case 'S':
  539. return 8;
  540. // Pretend it is January..
  541. default:
  542. return 0;
  543. }
  544. }
  545. /*}}}*/
  546. // timegm - Internal timegm function if gnu is not available /*{{{*/
  547. // ---------------------------------------------------------------------
  548. /* Ripped this evil little function from wget - I prefer the use of
  549. GNU timegm if possible as this technique will have interesting problems
  550. with leap seconds, timezones and other.
  551. Converts struct tm to time_t, assuming the data in tm is UTC rather
  552. than local timezone (mktime assumes the latter).
  553. Contributed by Roger Beeman <beeman@cisco.com>, with the help of
  554. Mark Baushke <mdb@cisco.com> and the rest of the Gurus at CISCO. */
  555. #ifndef __USE_MISC // glib sets this
  556. static time_t timegm(struct tm *t)
  557. {
  558. time_t tl, tb;
  559. tl = mktime (t);
  560. if (tl == -1)
  561. return -1;
  562. tb = mktime (gmtime (&tl));
  563. return (tl <= tb ? (tl + (tl - tb)) : (tl - (tb - tl)));
  564. }
  565. #endif
  566. /*}}}*/
  567. // StrToTime - Converts a string into a time_t /*{{{*/
  568. // ---------------------------------------------------------------------
  569. /* This handles all 3 populare time formats including RFC 1123, RFC 1036
  570. and the C library asctime format. It requires the GNU library function
  571. 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
  572. reason the C library does not provide any such function :<*/
  573. bool StrToTime(string Val,time_t &Result)
  574. {
  575. struct tm Tm;
  576. char Month[10];
  577. const char *I = Val.c_str();
  578. // Skip the day of the week
  579. for (;*I != 0 && *I != ' '; I++);
  580. // Handle RFC 1123 time
  581. if (sscanf(I," %d %3s %d %d:%d:%d GMT",&Tm.tm_mday,Month,&Tm.tm_year,
  582. &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6)
  583. {
  584. // Handle RFC 1036 time
  585. if (sscanf(I," %d-%3s-%d %d:%d:%d GMT",&Tm.tm_mday,Month,
  586. &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6)
  587. Tm.tm_year += 1900;
  588. else
  589. {
  590. // asctime format
  591. if (sscanf(I," %3s %d %d:%d:%d %d",Month,&Tm.tm_mday,
  592. &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6)
  593. return false;
  594. }
  595. }
  596. Tm.tm_isdst = 0;
  597. Tm.tm_mon = MonthConv(Month);
  598. Tm.tm_year -= 1900;
  599. // Convert to local time and then to GMT
  600. Result = timegm(&Tm);
  601. return true;
  602. }
  603. /*}}}*/
  604. // StrToNum - Convert a fixed length string to a number /*{{{*/
  605. // ---------------------------------------------------------------------
  606. /* This is used in decoding the crazy fixed length string headers in
  607. tar and ar files. */
  608. bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base)
  609. {
  610. char S[30];
  611. if (Len >= sizeof(S))
  612. return false;
  613. memcpy(S,Str,Len);
  614. S[Len] = 0;
  615. // All spaces is a zero
  616. Res = 0;
  617. unsigned I;
  618. for (I = 0; S[I] == ' '; I++);
  619. if (S[I] == 0)
  620. return true;
  621. char *End;
  622. Res = strtoul(S,&End,Base);
  623. if (End == S)
  624. return false;
  625. return true;
  626. }
  627. /*}}}*/
  628. // HexDigit - Convert a hex character into an integer /*{{{*/
  629. // ---------------------------------------------------------------------
  630. /* Helper for Hex2Num */
  631. static int HexDigit(int c)
  632. {
  633. if (c >= '0' && c <= '9')
  634. return c - '0';
  635. if (c >= 'a' && c <= 'f')
  636. return c - 'a' + 10;
  637. if (c >= 'A' && c <= 'F')
  638. return c - 'A' + 10;
  639. return 0;
  640. }
  641. /*}}}*/
  642. // Hex2Num - Convert a long hex number into a buffer /*{{{*/
  643. // ---------------------------------------------------------------------
  644. /* The length of the buffer must be exactly 1/2 the length of the string. */
  645. bool Hex2Num(const char *Start,const char *End,unsigned char *Num,
  646. unsigned int Length)
  647. {
  648. if (End - Start != (signed)(Length*2))
  649. return false;
  650. // Convert each digit. We store it in the same order as the string
  651. int J = 0;
  652. for (const char *I = Start; I < End;J++, I += 2)
  653. {
  654. if (isxdigit(*I) == 0 || isxdigit(I[1]) == 0)
  655. return false;
  656. Num[J] = HexDigit(I[0]) << 4;
  657. Num[J] += HexDigit(I[1]);
  658. }
  659. return true;
  660. }
  661. /*}}}*/
  662. // URI::CopyFrom - Copy from an object /*{{{*/
  663. // ---------------------------------------------------------------------
  664. /* This parses the URI into all of its components */
  665. void URI::CopyFrom(string U)
  666. {
  667. string::const_iterator I = U.begin();
  668. // Locate the first colon, this separates the scheme
  669. for (; I < U.end() && *I != ':' ; I++);
  670. string::const_iterator FirstColon = I;
  671. /* Determine if this is a host type URI with a leading double //
  672. and then search for the first single / */
  673. string::const_iterator SingleSlash = I;
  674. if (I + 3 < U.end() && I[1] == '/' && I[2] == '/')
  675. SingleSlash += 3;
  676. for (; SingleSlash < U.end() && *SingleSlash != '/'; SingleSlash++);
  677. if (SingleSlash > U.end())
  678. SingleSlash = U.end();
  679. // We can now write the access and path specifiers
  680. Access = string(U,0,FirstColon - U.begin());
  681. if (SingleSlash != U.end())
  682. Path = string(U,SingleSlash - U.begin());
  683. if (Path.empty() == true)
  684. Path = "/";
  685. // Now we attempt to locate a user:pass@host fragment
  686. if (FirstColon[1] == '/' && FirstColon[2] == '/')
  687. FirstColon += 3;
  688. else
  689. FirstColon += 1;
  690. if (FirstColon >= U.end())
  691. return;
  692. if (FirstColon > SingleSlash)
  693. FirstColon = SingleSlash;
  694. // Find the colon...
  695. I = FirstColon + 1;
  696. if (I > SingleSlash)
  697. I = SingleSlash;
  698. for (; I < SingleSlash && *I != ':'; I++);
  699. string::const_iterator SecondColon = I;
  700. // Search for the @ after the colon
  701. for (; I < SingleSlash && *I != '@'; I++);
  702. string::const_iterator At = I;
  703. // Now write the host and user/pass
  704. if (At == SingleSlash)
  705. {
  706. if (FirstColon < SingleSlash)
  707. Host = string(U,FirstColon - U.begin(),SingleSlash - FirstColon);
  708. }
  709. else
  710. {
  711. Host = string(U,At - U.begin() + 1,SingleSlash - At - 1);
  712. User = string(U,FirstColon - U.begin(),SecondColon - FirstColon);
  713. if (SecondColon < At)
  714. Password = string(U,SecondColon - U.begin() + 1,At - SecondColon - 1);
  715. }
  716. // Now we parse off a port number from the hostname
  717. Port = 0;
  718. string::size_type Pos = Host.rfind(':');
  719. if (Pos == string::npos)
  720. return;
  721. Port = atoi(string(Host,Pos+1).c_str());
  722. Host = string(Host,0,Pos);
  723. }
  724. /*}}}*/
  725. // URI::operator string - Convert the URI to a string /*{{{*/
  726. // ---------------------------------------------------------------------
  727. /* */
  728. URI::operator string()
  729. {
  730. string Res;
  731. if (Access.empty() == false)
  732. Res = Access + ':';
  733. if (Host.empty() == false)
  734. {
  735. if (Access.empty() == false)
  736. Res += "//";
  737. if (User.empty() == false)
  738. {
  739. Res += User;
  740. if (Password.empty() == false)
  741. Res += ":" + Password;
  742. Res += "@";
  743. }
  744. Res += Host;
  745. if (Port != 0)
  746. {
  747. char S[30];
  748. sprintf(S,":%u",Port);
  749. Res += S;
  750. }
  751. }
  752. if (Path.empty() == false)
  753. {
  754. if (Path[0] != '/')
  755. Res += "/" + Path;
  756. else
  757. Res += Path;
  758. }
  759. return Res;
  760. }
  761. /*}}}*/