strutl.cc 35 KB

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