You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1841 lines
51 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. String Util - Some useful string functions.
  5. These have been collected from here and there to do all sorts of useful
  6. things to strings. They are useful in file parsers, URI handlers and
  7. especially in APT methods.
  8. This source is placed in the Public Domain, do with it what you will
  9. It was originally written by Jason Gunthorpe <jgg@gpu.srv.ualberta.ca>
  10. ##################################################################### */
  11. /*}}}*/
  12. // Includes /*{{{*/
  13. #include <config.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <algorithm>
  18. #include <array>
  19. #include <iomanip>
  20. #include <locale>
  21. #include <sstream>
  22. #include <sstream>
  23. #include <string>
  24. #include <vector>
  25. #include <ctype.h>
  26. #include <errno.h>
  27. #include <iconv.h>
  28. #include <regex.h>
  29. #include <stdarg.h>
  30. #include <stddef.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #include <unistd.h>
  36. #include <apti18n.h>
  37. /*}}}*/
  38. using namespace std;
  39. // Strip - Remove white space from the front and back of a string /*{{{*/
  40. // ---------------------------------------------------------------------
  41. namespace APT {
  42. namespace String {
  43. std::string Strip(const std::string &str)
  44. {
  45. // ensure we have at least one character
  46. if (str.empty() == true)
  47. return str;
  48. char const * const s = str.c_str();
  49. size_t start = 0;
  50. for (; isspace(s[start]) != 0; ++start)
  51. ; // find the first not-space
  52. // string contains only whitespaces
  53. if (s[start] == '\0')
  54. return "";
  55. size_t end = str.length() - 1;
  56. for (; isspace(s[end]) != 0; --end)
  57. ; // find the last not-space
  58. return str.substr(start, end - start + 1);
  59. }
  60. bool Endswith(const std::string &s, const std::string &end)
  61. {
  62. if (end.size() > s.size())
  63. return false;
  64. return (s.compare(s.size() - end.size(), end.size(), end) == 0);
  65. }
  66. bool Startswith(const std::string &s, const std::string &start)
  67. {
  68. if (start.size() > s.size())
  69. return false;
  70. return (s.compare(0, start.size(), start) == 0);
  71. }
  72. std::string Join(std::vector<std::string> list, const std::string &sep)
  73. {
  74. std::ostringstream oss;
  75. for (auto it = list.begin(); it != list.end(); it++)
  76. {
  77. if (it != list.begin()) oss << sep;
  78. oss << *it;
  79. }
  80. return oss.str();
  81. }
  82. }
  83. }
  84. /*}}}*/
  85. // UTF8ToCodeset - Convert some UTF-8 string for some codeset /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* This is handy to use before display some information for enduser */
  88. bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest)
  89. {
  90. iconv_t cd;
  91. const char *inbuf;
  92. char *inptr, *outbuf;
  93. size_t insize, bufsize;
  94. dest->clear();
  95. cd = iconv_open(codeset, "UTF-8");
  96. if (cd == (iconv_t)(-1)) {
  97. // Something went wrong
  98. if (errno == EINVAL)
  99. _error->Error("conversion from 'UTF-8' to '%s' not available",
  100. codeset);
  101. else
  102. perror("iconv_open");
  103. return false;
  104. }
  105. insize = bufsize = orig.size();
  106. inbuf = orig.data();
  107. inptr = (char *)inbuf;
  108. outbuf = new char[bufsize];
  109. size_t lastError = -1;
  110. while (insize != 0)
  111. {
  112. char *outptr = outbuf;
  113. size_t outsize = bufsize;
  114. size_t const err = iconv(cd, &inptr, &insize, &outptr, &outsize);
  115. dest->append(outbuf, outptr - outbuf);
  116. if (err == (size_t)(-1))
  117. {
  118. switch (errno)
  119. {
  120. case EILSEQ:
  121. insize--;
  122. inptr++;
  123. // replace a series of unknown multibytes with a single "?"
  124. if (lastError != insize) {
  125. lastError = insize - 1;
  126. dest->append("?");
  127. }
  128. break;
  129. case EINVAL:
  130. insize = 0;
  131. break;
  132. case E2BIG:
  133. if (outptr == outbuf)
  134. {
  135. bufsize *= 2;
  136. delete[] outbuf;
  137. outbuf = new char[bufsize];
  138. }
  139. break;
  140. }
  141. }
  142. }
  143. delete[] outbuf;
  144. iconv_close(cd);
  145. return true;
  146. }
  147. /*}}}*/
  148. // strstrip - Remove white space from the front and back of a string /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* This is handy to use when parsing a file. It also removes \n's left
  151. over from fgets and company */
  152. char *_strstrip(char *String)
  153. {
  154. for (;*String != 0 && (*String == ' ' || *String == '\t'); String++);
  155. if (*String == 0)
  156. return String;
  157. return _strrstrip(String);
  158. }
  159. /*}}}*/
  160. // strrstrip - Remove white space from the back of a string /*{{{*/
  161. // ---------------------------------------------------------------------
  162. char *_strrstrip(char *String)
  163. {
  164. char *End = String + strlen(String) - 1;
  165. for (;End != String - 1 && (*End == ' ' || *End == '\t' || *End == '\n' ||
  166. *End == '\r'); End--);
  167. End++;
  168. *End = 0;
  169. return String;
  170. }
  171. /*}}}*/
  172. // strtabexpand - Converts tabs into 8 spaces /*{{{*/
  173. // ---------------------------------------------------------------------
  174. /* */
  175. char *_strtabexpand(char *String,size_t Len)
  176. {
  177. for (char *I = String; I != I + Len && *I != 0; I++)
  178. {
  179. if (*I != '\t')
  180. continue;
  181. if (I + 8 > String + Len)
  182. {
  183. *I = 0;
  184. return String;
  185. }
  186. /* Assume the start of the string is 0 and find the next 8 char
  187. division */
  188. int Len;
  189. if (String == I)
  190. Len = 1;
  191. else
  192. Len = 8 - ((String - I) % 8);
  193. Len -= 2;
  194. if (Len <= 0)
  195. {
  196. *I = ' ';
  197. continue;
  198. }
  199. memmove(I + Len,I + 1,strlen(I) + 1);
  200. for (char *J = I; J + Len != I; *I = ' ', I++);
  201. }
  202. return String;
  203. }
  204. /*}}}*/
  205. // ParseQuoteWord - Parse a single word out of a string /*{{{*/
  206. // ---------------------------------------------------------------------
  207. /* This grabs a single word, converts any % escaped characters to their
  208. proper values and advances the pointer. Double quotes are understood
  209. and striped out as well. This is for URI/URL parsing. It also can
  210. understand [] brackets.*/
  211. bool ParseQuoteWord(const char *&String,string &Res)
  212. {
  213. // Skip leading whitespace
  214. const char *C = String;
  215. for (;*C != 0 && *C == ' '; C++);
  216. if (*C == 0)
  217. return false;
  218. // Jump to the next word
  219. for (;*C != 0 && isspace(*C) == 0; C++)
  220. {
  221. if (*C == '"')
  222. {
  223. C = strchr(C + 1, '"');
  224. if (C == NULL)
  225. return false;
  226. }
  227. if (*C == '[')
  228. {
  229. C = strchr(C + 1, ']');
  230. if (C == NULL)
  231. return false;
  232. }
  233. }
  234. // Now de-quote characters
  235. char Buffer[1024];
  236. char Tmp[3];
  237. const char *Start = String;
  238. char *I;
  239. for (I = Buffer; I < Buffer + sizeof(Buffer) && Start != C; I++)
  240. {
  241. if (*Start == '%' && Start + 2 < C &&
  242. isxdigit(Start[1]) && isxdigit(Start[2]))
  243. {
  244. Tmp[0] = Start[1];
  245. Tmp[1] = Start[2];
  246. Tmp[2] = 0;
  247. *I = (char)strtol(Tmp,0,16);
  248. Start += 3;
  249. continue;
  250. }
  251. if (*Start != '"')
  252. *I = *Start;
  253. else
  254. I--;
  255. Start++;
  256. }
  257. *I = 0;
  258. Res = Buffer;
  259. // Skip ending white space
  260. for (;*C != 0 && isspace(*C) != 0; C++);
  261. String = C;
  262. return true;
  263. }
  264. /*}}}*/
  265. // ParseCWord - Parses a string like a C "" expression /*{{{*/
  266. // ---------------------------------------------------------------------
  267. /* This expects a series of space separated strings enclosed in ""'s.
  268. It concatenates the ""'s into a single string. */
  269. bool ParseCWord(const char *&String,string &Res)
  270. {
  271. // Skip leading whitespace
  272. const char *C = String;
  273. for (;*C != 0 && *C == ' '; C++);
  274. if (*C == 0)
  275. return false;
  276. char Buffer[1024];
  277. char *Buf = Buffer;
  278. if (strlen(String) >= sizeof(Buffer))
  279. return false;
  280. for (; *C != 0; C++)
  281. {
  282. if (*C == '"')
  283. {
  284. for (C++; *C != 0 && *C != '"'; C++)
  285. *Buf++ = *C;
  286. if (*C == 0)
  287. return false;
  288. continue;
  289. }
  290. if (C != String && isspace(*C) != 0 && isspace(C[-1]) != 0)
  291. continue;
  292. if (isspace(*C) == 0)
  293. return false;
  294. *Buf++ = ' ';
  295. }
  296. *Buf = 0;
  297. Res = Buffer;
  298. String = C;
  299. return true;
  300. }
  301. /*}}}*/
  302. // QuoteString - Convert a string into quoted from /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* */
  305. string QuoteString(const string &Str, const char *Bad)
  306. {
  307. std::stringstream Res;
  308. for (string::const_iterator I = Str.begin(); I != Str.end(); ++I)
  309. {
  310. if (strchr(Bad,*I) != 0 || isprint(*I) == 0 ||
  311. *I == 0x25 || // percent '%' char
  312. *I <= 0x20 || *I >= 0x7F) // control chars
  313. {
  314. ioprintf(Res, "%%%02hhx", *I);
  315. }
  316. else
  317. Res << *I;
  318. }
  319. return Res.str();
  320. }
  321. /*}}}*/
  322. // DeQuoteString - Convert a string from quoted from /*{{{*/
  323. // ---------------------------------------------------------------------
  324. /* This undoes QuoteString */
  325. string DeQuoteString(const string &Str)
  326. {
  327. return DeQuoteString(Str.begin(),Str.end());
  328. }
  329. string DeQuoteString(string::const_iterator const &begin,
  330. string::const_iterator const &end)
  331. {
  332. string Res;
  333. for (string::const_iterator I = begin; I != end; ++I)
  334. {
  335. if (*I == '%' && I + 2 < end &&
  336. isxdigit(I[1]) && isxdigit(I[2]))
  337. {
  338. char Tmp[3];
  339. Tmp[0] = I[1];
  340. Tmp[1] = I[2];
  341. Tmp[2] = 0;
  342. Res += (char)strtol(Tmp,0,16);
  343. I += 2;
  344. continue;
  345. }
  346. else
  347. Res += *I;
  348. }
  349. return Res;
  350. }
  351. /*}}}*/
  352. // SizeToStr - Convert a long into a human readable size /*{{{*/
  353. // ---------------------------------------------------------------------
  354. /* A max of 4 digits are shown before conversion to the next highest unit.
  355. The max length of the string will be 5 chars unless the size is > 10
  356. YottaBytes (E24) */
  357. string SizeToStr(double Size)
  358. {
  359. double ASize;
  360. if (Size >= 0)
  361. ASize = Size;
  362. else
  363. ASize = -1*Size;
  364. /* bytes, KiloBytes, MegaBytes, GigaBytes, TeraBytes, PetaBytes,
  365. ExaBytes, ZettaBytes, YottaBytes */
  366. char Ext[] = {'\0','k','M','G','T','P','E','Z','Y'};
  367. int I = 0;
  368. while (I <= 8)
  369. {
  370. if (ASize < 100 && I != 0)
  371. {
  372. std::string S;
  373. strprintf(S, "%'.1f %c", ASize, Ext[I]);
  374. return S;
  375. }
  376. if (ASize < 10000)
  377. {
  378. std::string S;
  379. strprintf(S, "%'.0f %c", ASize, Ext[I]);
  380. return S;
  381. }
  382. ASize /= 1000.0;
  383. I++;
  384. }
  385. return "";
  386. }
  387. /*}}}*/
  388. // TimeToStr - Convert the time into a string /*{{{*/
  389. // ---------------------------------------------------------------------
  390. /* Converts a number of seconds to a hms format */
  391. string TimeToStr(unsigned long Sec)
  392. {
  393. std::string S;
  394. if (Sec > 60*60*24)
  395. {
  396. //TRANSLATOR: d means days, h means hours, min means minutes, s means seconds
  397. strprintf(S,_("%lid %lih %limin %lis"),Sec/60/60/24,(Sec/60/60) % 24,(Sec/60) % 60,Sec % 60);
  398. }
  399. else if (Sec > 60*60)
  400. {
  401. //TRANSLATOR: h means hours, min means minutes, s means seconds
  402. strprintf(S,_("%lih %limin %lis"),Sec/60/60,(Sec/60) % 60,Sec % 60);
  403. }
  404. else if (Sec > 60)
  405. {
  406. //TRANSLATOR: min means minutes, s means seconds
  407. strprintf(S,_("%limin %lis"),Sec/60,Sec % 60);
  408. }
  409. else
  410. {
  411. //TRANSLATOR: s means seconds
  412. strprintf(S,_("%lis"),Sec);
  413. }
  414. return S;
  415. }
  416. /*}}}*/
  417. // SubstVar - Substitute a string for another string /*{{{*/
  418. // ---------------------------------------------------------------------
  419. /* This replaces all occurrences of Subst with Contents in Str. */
  420. string SubstVar(const string &Str,const string &Subst,const string &Contents)
  421. {
  422. if (Subst.empty() == true)
  423. return Str;
  424. string::size_type Pos = 0;
  425. string::size_type OldPos = 0;
  426. string Temp;
  427. while (OldPos < Str.length() &&
  428. (Pos = Str.find(Subst,OldPos)) != string::npos)
  429. {
  430. if (OldPos != Pos)
  431. Temp.append(Str, OldPos, Pos - OldPos);
  432. if (Contents.empty() == false)
  433. Temp.append(Contents);
  434. OldPos = Pos + Subst.length();
  435. }
  436. if (OldPos == 0)
  437. return Str;
  438. if (OldPos >= Str.length())
  439. return Temp;
  440. Temp.append(Str, OldPos, string::npos);
  441. return Temp;
  442. }
  443. string SubstVar(string Str,const struct SubstVar *Vars)
  444. {
  445. for (; Vars->Subst != 0; Vars++)
  446. Str = SubstVar(Str,Vars->Subst,*Vars->Contents);
  447. return Str;
  448. }
  449. /*}}}*/
  450. // OutputInDepth - return a string with separator multiplied with depth /*{{{*/
  451. // ---------------------------------------------------------------------
  452. /* Returns a string with the supplied separator depth + 1 times in it */
  453. std::string OutputInDepth(const unsigned long Depth, const char* Separator)
  454. {
  455. std::string output = "";
  456. for(unsigned long d=Depth+1; d > 0; d--)
  457. output.append(Separator);
  458. return output;
  459. }
  460. /*}}}*/
  461. // URItoFileName - Convert the uri into a unique file name /*{{{*/
  462. // ---------------------------------------------------------------------
  463. /* This converts a URI into a safe filename. It quotes all unsafe characters
  464. and converts / to _ and removes the scheme identifier. The resulting
  465. file name should be unique and never occur again for a different file */
  466. string URItoFileName(const string &URI)
  467. {
  468. // Nuke 'sensitive' items
  469. ::URI U(URI);
  470. U.User.clear();
  471. U.Password.clear();
  472. U.Access.clear();
  473. // "\x00-\x20{}|\\\\^\\[\\]<>\"\x7F-\xFF";
  474. string NewURI = QuoteString(U,"\\|{}[]<>\"^~_=!@#$%^&*");
  475. replace(NewURI.begin(),NewURI.end(),'/','_');
  476. return NewURI;
  477. }
  478. /*}}}*/
  479. // Base64Encode - Base64 Encoding routine for short strings /*{{{*/
  480. // ---------------------------------------------------------------------
  481. /* This routine performs a base64 transformation on a string. It was ripped
  482. from wget and then patched and bug fixed.
  483. This spec can be found in rfc2045 */
  484. string Base64Encode(const string &S)
  485. {
  486. // Conversion table.
  487. static char tbl[64] = {'A','B','C','D','E','F','G','H',
  488. 'I','J','K','L','M','N','O','P',
  489. 'Q','R','S','T','U','V','W','X',
  490. 'Y','Z','a','b','c','d','e','f',
  491. 'g','h','i','j','k','l','m','n',
  492. 'o','p','q','r','s','t','u','v',
  493. 'w','x','y','z','0','1','2','3',
  494. '4','5','6','7','8','9','+','/'};
  495. // Pre-allocate some space
  496. string Final;
  497. Final.reserve((4*S.length() + 2)/3 + 2);
  498. /* Transform the 3x8 bits to 4x6 bits, as required by
  499. base64. */
  500. for (string::const_iterator I = S.begin(); I < S.end(); I += 3)
  501. {
  502. char Bits[3] = {0,0,0};
  503. Bits[0] = I[0];
  504. if (I + 1 < S.end())
  505. Bits[1] = I[1];
  506. if (I + 2 < S.end())
  507. Bits[2] = I[2];
  508. Final += tbl[Bits[0] >> 2];
  509. Final += tbl[((Bits[0] & 3) << 4) + (Bits[1] >> 4)];
  510. if (I + 1 >= S.end())
  511. break;
  512. Final += tbl[((Bits[1] & 0xf) << 2) + (Bits[2] >> 6)];
  513. if (I + 2 >= S.end())
  514. break;
  515. Final += tbl[Bits[2] & 0x3f];
  516. }
  517. /* Apply the padding elements, this tells how many bytes the remote
  518. end should discard */
  519. if (S.length() % 3 == 2)
  520. Final += '=';
  521. if (S.length() % 3 == 1)
  522. Final += "==";
  523. return Final;
  524. }
  525. /*}}}*/
  526. // stringcmp - Arbitrary string compare /*{{{*/
  527. // ---------------------------------------------------------------------
  528. /* This safely compares two non-null terminated strings of arbitrary
  529. length */
  530. int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
  531. {
  532. for (; A != AEnd && B != BEnd; A++, B++)
  533. if (*A != *B)
  534. break;
  535. if (A == AEnd && B == BEnd)
  536. return 0;
  537. if (A == AEnd)
  538. return 1;
  539. if (B == BEnd)
  540. return -1;
  541. if (*A < *B)
  542. return -1;
  543. return 1;
  544. }
  545. #if __GNUC__ >= 3
  546. int stringcmp(string::const_iterator A,string::const_iterator AEnd,
  547. const char *B,const char *BEnd)
  548. {
  549. for (; A != AEnd && B != BEnd; A++, B++)
  550. if (*A != *B)
  551. break;
  552. if (A == AEnd && B == BEnd)
  553. return 0;
  554. if (A == AEnd)
  555. return 1;
  556. if (B == BEnd)
  557. return -1;
  558. if (*A < *B)
  559. return -1;
  560. return 1;
  561. }
  562. int stringcmp(string::const_iterator A,string::const_iterator AEnd,
  563. string::const_iterator B,string::const_iterator BEnd)
  564. {
  565. for (; A != AEnd && B != BEnd; A++, B++)
  566. if (*A != *B)
  567. break;
  568. if (A == AEnd && B == BEnd)
  569. return 0;
  570. if (A == AEnd)
  571. return 1;
  572. if (B == BEnd)
  573. return -1;
  574. if (*A < *B)
  575. return -1;
  576. return 1;
  577. }
  578. #endif
  579. /*}}}*/
  580. // stringcasecmp - Arbitrary case insensitive string compare /*{{{*/
  581. // ---------------------------------------------------------------------
  582. /* */
  583. int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd)
  584. {
  585. for (; A != AEnd && B != BEnd; A++, B++)
  586. if (tolower_ascii(*A) != tolower_ascii(*B))
  587. break;
  588. if (A == AEnd && B == BEnd)
  589. return 0;
  590. if (A == AEnd)
  591. return 1;
  592. if (B == BEnd)
  593. return -1;
  594. if (tolower_ascii(*A) < tolower_ascii(*B))
  595. return -1;
  596. return 1;
  597. }
  598. #if __GNUC__ >= 3
  599. int stringcasecmp(string::const_iterator A,string::const_iterator AEnd,
  600. const char *B,const char *BEnd)
  601. {
  602. for (; A != AEnd && B != BEnd; A++, B++)
  603. if (tolower_ascii(*A) != tolower_ascii(*B))
  604. break;
  605. if (A == AEnd && B == BEnd)
  606. return 0;
  607. if (A == AEnd)
  608. return 1;
  609. if (B == BEnd)
  610. return -1;
  611. if (tolower_ascii(*A) < tolower_ascii(*B))
  612. return -1;
  613. return 1;
  614. }
  615. int stringcasecmp(string::const_iterator A,string::const_iterator AEnd,
  616. string::const_iterator B,string::const_iterator BEnd)
  617. {
  618. for (; A != AEnd && B != BEnd; A++, B++)
  619. if (tolower_ascii(*A) != tolower_ascii(*B))
  620. break;
  621. if (A == AEnd && B == BEnd)
  622. return 0;
  623. if (A == AEnd)
  624. return 1;
  625. if (B == BEnd)
  626. return -1;
  627. if (tolower_ascii(*A) < tolower_ascii(*B))
  628. return -1;
  629. return 1;
  630. }
  631. #endif
  632. /*}}}*/
  633. // LookupTag - Lookup the value of a tag in a tagged string /*{{{*/
  634. // ---------------------------------------------------------------------
  635. /* The format is like those used in package files and the method
  636. communication system */
  637. std::string LookupTag(const std::string &Message, const char *TagC, const char *Default)
  638. {
  639. std::string tag = std::string("\n") + TagC + ":";
  640. if (Default == nullptr)
  641. Default = "";
  642. if (Message.length() < tag.length())
  643. return Default;
  644. std::transform(tag.begin(), tag.end(), tag.begin(), tolower_ascii);
  645. auto valuestart = Message.cbegin();
  646. // maybe the message starts directly with tag
  647. if (Message[tag.length() - 2] == ':')
  648. {
  649. std::string lowstart = std::string("\n") + Message.substr(0, tag.length() - 1);
  650. std::transform(lowstart.begin(), lowstart.end(), lowstart.begin(), tolower_ascii);
  651. if (lowstart == tag)
  652. valuestart = std::next(valuestart, tag.length() - 1);
  653. }
  654. // the tag is somewhere in the message
  655. if (valuestart == Message.cbegin())
  656. {
  657. auto const tagbegin = std::search(Message.cbegin(), Message.cend(), tag.cbegin(), tag.cend(),
  658. [](char const a, char const b) { return tolower_ascii(a) == b; });
  659. if (tagbegin == Message.cend())
  660. return Default;
  661. valuestart = std::next(tagbegin, tag.length());
  662. }
  663. auto const is_whitespace = [](char const c) { return isspace_ascii(c) != 0 && c != '\n'; };
  664. auto const is_newline = [](char const c) { return c == '\n'; };
  665. std::string result;
  666. valuestart = std::find_if_not(valuestart, Message.cend(), is_whitespace);
  667. // is the first line of the value empty?
  668. if (valuestart != Message.cend() && *valuestart == '\n')
  669. {
  670. valuestart = std::next(valuestart);
  671. if (valuestart != Message.cend() && *valuestart == ' ')
  672. valuestart = std::next(valuestart);
  673. }
  674. // extract the value over multiple lines removing trailing whitespace
  675. while (valuestart < Message.cend())
  676. {
  677. auto const linebreak = std::find_if(valuestart, Message.cend(), is_newline);
  678. auto valueend = std::prev(linebreak);
  679. // skip spaces at the end of the line
  680. while (valueend > valuestart && is_whitespace(*valueend))
  681. valueend = std::prev(valueend);
  682. // append found line to result
  683. {
  684. std::string tmp(valuestart, std::next(valueend));
  685. if (tmp != ".")
  686. {
  687. if (result.empty())
  688. result.assign(std::move(tmp));
  689. else
  690. result.append(tmp);
  691. }
  692. }
  693. // see if the value is multiline
  694. if (linebreak == Message.cend())
  695. break;
  696. valuestart = std::next(linebreak);
  697. if (valuestart == Message.cend() || *valuestart != ' ')
  698. break;
  699. result.append("\n");
  700. // skip the space leading a multiline (Keep all other whitespaces in the value)
  701. valuestart = std::next(valuestart);
  702. }
  703. auto const valueend = result.find_last_not_of("\n");
  704. if (valueend == std::string::npos)
  705. result.clear();
  706. else
  707. result.erase(valueend + 1);
  708. return result;
  709. }
  710. /*}}}*/
  711. // StringToBool - Converts a string into a boolean /*{{{*/
  712. // ---------------------------------------------------------------------
  713. /* This inspects the string to see if it is true or if it is false and
  714. then returns the result. Several variants on true/false are checked. */
  715. int StringToBool(const string &Text,int Default)
  716. {
  717. char *ParseEnd;
  718. int Res = strtol(Text.c_str(),&ParseEnd,0);
  719. // ensure that the entire string was converted by strtol to avoid
  720. // failures on "apt-cache show -a 0ad" where the "0" is converted
  721. const char *TextEnd = Text.c_str()+Text.size();
  722. if (ParseEnd == TextEnd && Res >= 0 && Res <= 1)
  723. return Res;
  724. // Check for positives
  725. if (strcasecmp(Text.c_str(),"no") == 0 ||
  726. strcasecmp(Text.c_str(),"false") == 0 ||
  727. strcasecmp(Text.c_str(),"without") == 0 ||
  728. strcasecmp(Text.c_str(),"off") == 0 ||
  729. strcasecmp(Text.c_str(),"disable") == 0)
  730. return 0;
  731. // Check for negatives
  732. if (strcasecmp(Text.c_str(),"yes") == 0 ||
  733. strcasecmp(Text.c_str(),"true") == 0 ||
  734. strcasecmp(Text.c_str(),"with") == 0 ||
  735. strcasecmp(Text.c_str(),"on") == 0 ||
  736. strcasecmp(Text.c_str(),"enable") == 0)
  737. return 1;
  738. return Default;
  739. }
  740. /*}}}*/
  741. // TimeRFC1123 - Convert a time_t into RFC1123 format /*{{{*/
  742. // ---------------------------------------------------------------------
  743. /* This converts a time_t into a string time representation that is
  744. year 2000 compliant and timezone neutral */
  745. string TimeRFC1123(time_t Date)
  746. {
  747. return TimeRFC1123(Date, false);
  748. }
  749. string TimeRFC1123(time_t Date, bool const NumericTimezone)
  750. {
  751. struct tm Conv;
  752. if (gmtime_r(&Date, &Conv) == NULL)
  753. return "";
  754. auto const posix = std::locale::classic();
  755. std::ostringstream datestr;
  756. datestr.imbue(posix);
  757. APT::StringView const fmt("%a, %d %b %Y %H:%M:%S");
  758. std::use_facet<std::time_put<char>>(posix).put(
  759. std::ostreambuf_iterator<char>(datestr),
  760. datestr, ' ', &Conv, fmt.data(), fmt.data() + fmt.size());
  761. if (NumericTimezone)
  762. datestr << " +0000";
  763. else
  764. datestr << " GMT";
  765. return datestr.str();
  766. }
  767. /*}}}*/
  768. // ReadMessages - Read messages from the FD /*{{{*/
  769. // ---------------------------------------------------------------------
  770. /* This pulls full messages from the input FD into the message buffer.
  771. It assumes that messages will not pause during transit so no
  772. fancy buffering is used.
  773. In particular: this reads blocks from the input until it believes
  774. that it's run out of input text. Each block is terminated by a
  775. double newline ('\n' followed by '\n').
  776. */
  777. bool ReadMessages(int Fd, vector<string> &List)
  778. {
  779. char Buffer[64000];
  780. // Represents any left-over from the previous iteration of the
  781. // parse loop. (i.e., if a message is split across the end
  782. // of the buffer, it goes here)
  783. string PartialMessage;
  784. do {
  785. int const Res = read(Fd, Buffer, sizeof(Buffer));
  786. if (Res < 0 && errno == EINTR)
  787. continue;
  788. // process we read from has died
  789. if (Res == 0)
  790. return false;
  791. // No data
  792. #if EAGAIN != EWOULDBLOCK
  793. if (Res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
  794. #else
  795. if (Res < 0 && errno == EAGAIN)
  796. #endif
  797. return true;
  798. if (Res < 0)
  799. return false;
  800. // extract the message(s) from the buffer
  801. char const *Start = Buffer;
  802. char const * const End = Buffer + Res;
  803. char const * NL = (char const *) memchr(Start, '\n', End - Start);
  804. if (NL == NULL)
  805. {
  806. // end of buffer: store what we have so far and read new data in
  807. PartialMessage.append(Start, End - Start);
  808. Start = End;
  809. }
  810. else
  811. ++NL;
  812. if (PartialMessage.empty() == false && Start < End)
  813. {
  814. // if we start with a new line, see if the partial message we have ended with one
  815. // so that we properly detect records ending between two read() runs
  816. // cases are: \n|\n , \r\n|\r\n and \r\n\r|\n
  817. // the case \r|\n\r\n is handled by the usual double-newline handling
  818. if ((NL - Start) == 1 || ((NL - Start) == 2 && *Start == '\r'))
  819. {
  820. if (APT::String::Endswith(PartialMessage, "\n") || APT::String::Endswith(PartialMessage, "\r\n\r"))
  821. {
  822. PartialMessage.erase(PartialMessage.find_last_not_of("\r\n") + 1);
  823. List.push_back(PartialMessage);
  824. PartialMessage.clear();
  825. while (NL < End && (*NL == '\n' || *NL == '\r')) ++NL;
  826. Start = NL;
  827. }
  828. }
  829. }
  830. while (Start < End) {
  831. char const * NL2 = (char const *) memchr(NL, '\n', End - NL);
  832. if (NL2 == NULL)
  833. {
  834. // end of buffer: store what we have so far and read new data in
  835. PartialMessage.append(Start, End - Start);
  836. break;
  837. }
  838. ++NL2;
  839. // did we find a double newline?
  840. if ((NL2 - NL) == 1 || ((NL2 - NL) == 2 && *NL == '\r'))
  841. {
  842. PartialMessage.append(Start, NL2 - Start);
  843. PartialMessage.erase(PartialMessage.find_last_not_of("\r\n") + 1);
  844. List.push_back(PartialMessage);
  845. PartialMessage.clear();
  846. while (NL2 < End && (*NL2 == '\n' || *NL2 == '\r')) ++NL2;
  847. Start = NL2;
  848. }
  849. NL = NL2;
  850. }
  851. // we have read at least one complete message and nothing left
  852. if (PartialMessage.empty() == true)
  853. return true;
  854. if (WaitFd(Fd) == false)
  855. return false;
  856. } while (true);
  857. }
  858. /*}}}*/
  859. // MonthConv - Converts a month string into a number /*{{{*/
  860. // ---------------------------------------------------------------------
  861. /* This was lifted from the boa webserver which lifted it from 'wn-v1.07'
  862. Made it a bit more robust with a few tolower_ascii though. */
  863. static int MonthConv(char const * const Month)
  864. {
  865. switch (tolower_ascii(*Month))
  866. {
  867. case 'a':
  868. return tolower_ascii(Month[1]) == 'p'?3:7;
  869. case 'd':
  870. return 11;
  871. case 'f':
  872. return 1;
  873. case 'j':
  874. if (tolower_ascii(Month[1]) == 'a')
  875. return 0;
  876. return tolower_ascii(Month[2]) == 'n'?5:6;
  877. case 'm':
  878. return tolower_ascii(Month[2]) == 'r'?2:4;
  879. case 'n':
  880. return 10;
  881. case 'o':
  882. return 9;
  883. case 's':
  884. return 8;
  885. // Pretend it is January..
  886. default:
  887. return 0;
  888. }
  889. }
  890. /*}}}*/
  891. // timegm - Internal timegm if the gnu version is not available /*{{{*/
  892. // ---------------------------------------------------------------------
  893. /* Converts struct tm to time_t, assuming the data in tm is UTC rather
  894. than local timezone (mktime assumes the latter).
  895. This function is a nonstandard GNU extension that is also present on
  896. the BSDs and maybe other systems. For others we follow the advice of
  897. the manpage of timegm and use his portable replacement. */
  898. #ifndef HAVE_TIMEGM
  899. static time_t timegm(struct tm *t)
  900. {
  901. char *tz = getenv("TZ");
  902. setenv("TZ", "", 1);
  903. tzset();
  904. time_t ret = mktime(t);
  905. if (tz)
  906. setenv("TZ", tz, 1);
  907. else
  908. unsetenv("TZ");
  909. tzset();
  910. return ret;
  911. }
  912. #endif
  913. /*}}}*/
  914. // RFC1123StrToTime - Converts an HTTP1.1 full date strings into a time_t /*{{{*/
  915. // ---------------------------------------------------------------------
  916. /* tries to parses a full date as specified in RFC7231 §7.1.1.1
  917. with one exception: HTTP/1.1 valid dates need to have GMT as timezone.
  918. As we encounter dates from UTC or with a numeric timezone in other places,
  919. we allow them here to to be able to reuse the method. Either way, a date
  920. must be in UTC or parsing will fail. Previous implementations of this
  921. method used to ignore the timezone and assume always UTC. */
  922. bool RFC1123StrToTime(const char* const str,time_t &time)
  923. {
  924. unsigned short day = 0;
  925. signed int year = 0; // yes, Y23K problem – we going to worry then…
  926. std::string weekday, month, datespec, timespec, zone;
  927. std::istringstream ss(str);
  928. auto const &posix = std::locale::classic();
  929. ss.imbue(posix);
  930. ss >> weekday;
  931. // we only superficially check weekday, mostly to avoid accepting localized
  932. // weekdays here and take only its length to decide which datetime format we
  933. // encounter here. The date isn't stored.
  934. std::transform(weekday.begin(), weekday.end(), weekday.begin(), ::tolower);
  935. std::array<char const * const, 7> c_weekdays = {{ "sun", "mon", "tue", "wed", "thu", "fri", "sat" }};
  936. if (std::find(c_weekdays.begin(), c_weekdays.end(), weekday.substr(0,3)) == c_weekdays.end())
  937. return false;
  938. switch (weekday.length())
  939. {
  940. case 4:
  941. // Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
  942. if (weekday[3] != ',')
  943. return false;
  944. ss >> day >> month >> year >> timespec >> zone;
  945. break;
  946. case 3:
  947. // Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
  948. ss >> month >> day >> timespec >> year;
  949. zone = "UTC";
  950. break;
  951. case 0:
  952. case 1:
  953. case 2:
  954. return false;
  955. default:
  956. // Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
  957. if (weekday[weekday.length() - 1] != ',')
  958. return false;
  959. ss >> datespec >> timespec >> zone;
  960. auto const expldate = VectorizeString(datespec, '-');
  961. if (expldate.size() != 3)
  962. return false;
  963. try {
  964. size_t pos;
  965. day = std::stoi(expldate[0], &pos);
  966. if (pos != expldate[0].length())
  967. return false;
  968. year = 1900 + std::stoi(expldate[2], &pos);
  969. if (pos != expldate[2].length())
  970. return false;
  971. strprintf(datespec, "%.4d-%.2d-%.2d", year, MonthConv(expldate[1].c_str()) + 1, day);
  972. } catch (...) {
  973. return false;
  974. }
  975. break;
  976. }
  977. if (ss.fail() || ss.bad() || !ss.eof())
  978. return false;
  979. if (zone != "GMT" && zone != "UTC" && zone != "Z") // RFC 822
  980. {
  981. // numeric timezones as a should of RFC 1123 and generally preferred
  982. try {
  983. size_t pos;
  984. auto const z = std::stoi(zone, &pos);
  985. if (z != 0 || pos != zone.length())
  986. return false;
  987. } catch (...) {
  988. return false;
  989. }
  990. }
  991. if (datespec.empty())
  992. {
  993. if (month.empty())
  994. return false;
  995. strprintf(datespec, "%.4d-%.2d-%.2d", year, MonthConv(month.c_str()) + 1, day);
  996. }
  997. std::string const datetime = datespec + ' ' + timespec;
  998. struct tm Tm;
  999. if (strptime(datetime.c_str(), "%Y-%m-%d %H:%M:%S", &Tm) == nullptr)
  1000. return false;
  1001. time = timegm(&Tm);
  1002. return true;
  1003. }
  1004. /*}}}*/
  1005. // FTPMDTMStrToTime - Converts a ftp modification date into a time_t /*{{{*/
  1006. // ---------------------------------------------------------------------
  1007. /* */
  1008. bool FTPMDTMStrToTime(const char* const str,time_t &time)
  1009. {
  1010. struct tm Tm;
  1011. // MDTM includes no whitespaces but recommend and ignored by strptime
  1012. if (strptime(str, "%Y %m %d %H %M %S", &Tm) == NULL)
  1013. return false;
  1014. time = timegm(&Tm);
  1015. return true;
  1016. }
  1017. /*}}}*/
  1018. // StrToTime - Converts a string into a time_t /*{{{*/
  1019. // ---------------------------------------------------------------------
  1020. /* This handles all 3 popular time formats including RFC 1123, RFC 1036
  1021. and the C library asctime format. It requires the GNU library function
  1022. 'timegm' to convert a struct tm in UTC to a time_t. For some bizzar
  1023. reason the C library does not provide any such function :< This also
  1024. handles the weird, but unambiguous FTP time format*/
  1025. bool StrToTime(const string &Val,time_t &Result)
  1026. {
  1027. struct tm Tm;
  1028. char Month[10];
  1029. // Skip the day of the week
  1030. const char *I = strchr(Val.c_str(), ' ');
  1031. // Handle RFC 1123 time
  1032. Month[0] = 0;
  1033. if (sscanf(I," %2d %3s %4d %2d:%2d:%2d GMT",&Tm.tm_mday,Month,&Tm.tm_year,
  1034. &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6)
  1035. {
  1036. // Handle RFC 1036 time
  1037. if (sscanf(I," %2d-%3s-%3d %2d:%2d:%2d GMT",&Tm.tm_mday,Month,
  1038. &Tm.tm_year,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) == 6)
  1039. Tm.tm_year += 1900;
  1040. else
  1041. {
  1042. // asctime format
  1043. if (sscanf(I," %3s %2d %2d:%2d:%2d %4d",Month,&Tm.tm_mday,
  1044. &Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec,&Tm.tm_year) != 6)
  1045. {
  1046. // 'ftp' time
  1047. if (sscanf(Val.c_str(),"%4d%2d%2d%2d%2d%2d",&Tm.tm_year,&Tm.tm_mon,
  1048. &Tm.tm_mday,&Tm.tm_hour,&Tm.tm_min,&Tm.tm_sec) != 6)
  1049. return false;
  1050. Tm.tm_mon--;
  1051. }
  1052. }
  1053. }
  1054. Tm.tm_isdst = 0;
  1055. if (Month[0] != 0)
  1056. Tm.tm_mon = MonthConv(Month);
  1057. else
  1058. Tm.tm_mon = 0; // we don't have a month, so pick something
  1059. Tm.tm_year -= 1900;
  1060. // Convert to local time and then to GMT
  1061. Result = timegm(&Tm);
  1062. return true;
  1063. }
  1064. /*}}}*/
  1065. // StrToNum - Convert a fixed length string to a number /*{{{*/
  1066. // ---------------------------------------------------------------------
  1067. /* This is used in decoding the crazy fixed length string headers in
  1068. tar and ar files. */
  1069. bool StrToNum(const char *Str,unsigned long &Res,unsigned Len,unsigned Base)
  1070. {
  1071. char S[30];
  1072. if (Len >= sizeof(S))
  1073. return false;
  1074. memcpy(S,Str,Len);
  1075. S[Len] = 0;
  1076. // All spaces is a zero
  1077. Res = 0;
  1078. unsigned I;
  1079. for (I = 0; S[I] == ' '; I++);
  1080. if (S[I] == 0)
  1081. return true;
  1082. char *End;
  1083. Res = strtoul(S,&End,Base);
  1084. if (End == S)
  1085. return false;
  1086. return true;
  1087. }
  1088. /*}}}*/
  1089. // StrToNum - Convert a fixed length string to a number /*{{{*/
  1090. // ---------------------------------------------------------------------
  1091. /* This is used in decoding the crazy fixed length string headers in
  1092. tar and ar files. */
  1093. bool StrToNum(const char *Str,unsigned long long &Res,unsigned Len,unsigned Base)
  1094. {
  1095. char S[30];
  1096. if (Len >= sizeof(S))
  1097. return false;
  1098. memcpy(S,Str,Len);
  1099. S[Len] = 0;
  1100. // All spaces is a zero
  1101. Res = 0;
  1102. unsigned I;
  1103. for (I = 0; S[I] == ' '; I++);
  1104. if (S[I] == 0)
  1105. return true;
  1106. char *End;
  1107. Res = strtoull(S,&End,Base);
  1108. if (End == S)
  1109. return false;
  1110. return true;
  1111. }
  1112. /*}}}*/
  1113. // Base256ToNum - Convert a fixed length binary to a number /*{{{*/
  1114. // ---------------------------------------------------------------------
  1115. /* This is used in decoding the 256bit encoded fixed length fields in
  1116. tar files */
  1117. bool Base256ToNum(const char *Str,unsigned long long &Res,unsigned int Len)
  1118. {
  1119. if ((Str[0] & 0x80) == 0)
  1120. return false;
  1121. else
  1122. {
  1123. Res = Str[0] & 0x7F;
  1124. for(unsigned int i = 1; i < Len; ++i)
  1125. Res = (Res<<8) + Str[i];
  1126. return true;
  1127. }
  1128. }
  1129. /*}}}*/
  1130. // Base256ToNum - Convert a fixed length binary to a number /*{{{*/
  1131. // ---------------------------------------------------------------------
  1132. /* This is used in decoding the 256bit encoded fixed length fields in
  1133. tar files */
  1134. bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len)
  1135. {
  1136. unsigned long long Num = 0;
  1137. bool rc;
  1138. rc = Base256ToNum(Str, Num, Len);
  1139. // rudimentary check for overflow (Res = ulong, Num = ulonglong)
  1140. Res = Num;
  1141. if (Res != Num)
  1142. return false;
  1143. return rc;
  1144. }
  1145. /*}}}*/
  1146. // HexDigit - Convert a hex character into an integer /*{{{*/
  1147. // ---------------------------------------------------------------------
  1148. /* Helper for Hex2Num */
  1149. static int HexDigit(int c)
  1150. {
  1151. if (c >= '0' && c <= '9')
  1152. return c - '0';
  1153. if (c >= 'a' && c <= 'f')
  1154. return c - 'a' + 10;
  1155. if (c >= 'A' && c <= 'F')
  1156. return c - 'A' + 10;
  1157. return -1;
  1158. }
  1159. /*}}}*/
  1160. // Hex2Num - Convert a long hex number into a buffer /*{{{*/
  1161. // ---------------------------------------------------------------------
  1162. /* The length of the buffer must be exactly 1/2 the length of the string. */
  1163. bool Hex2Num(const string &Str,unsigned char *Num,unsigned int Length)
  1164. {
  1165. return Hex2Num(APT::StringView(Str), Num, Length);
  1166. }
  1167. bool Hex2Num(const APT::StringView Str,unsigned char *Num,unsigned int Length)
  1168. {
  1169. if (Str.length() != Length*2)
  1170. return false;
  1171. // Convert each digit. We store it in the same order as the string
  1172. int J = 0;
  1173. for (auto I = Str.begin(); I != Str.end();J++, I += 2)
  1174. {
  1175. int first_half = HexDigit(I[0]);
  1176. int second_half;
  1177. if (first_half < 0)
  1178. return false;
  1179. second_half = HexDigit(I[1]);
  1180. if (second_half < 0)
  1181. return false;
  1182. Num[J] = first_half << 4;
  1183. Num[J] += second_half;
  1184. }
  1185. return true;
  1186. }
  1187. /*}}}*/
  1188. // TokSplitString - Split a string up by a given token /*{{{*/
  1189. // ---------------------------------------------------------------------
  1190. /* This is intended to be a faster splitter, it does not use dynamic
  1191. memories. Input is changed to insert nulls at each token location. */
  1192. bool TokSplitString(char Tok,char *Input,char **List,
  1193. unsigned long ListMax)
  1194. {
  1195. // Strip any leading spaces
  1196. char *Start = Input;
  1197. char *Stop = Start + strlen(Start);
  1198. for (; *Start != 0 && isspace(*Start) != 0; Start++);
  1199. unsigned long Count = 0;
  1200. char *Pos = Start;
  1201. while (Pos != Stop)
  1202. {
  1203. // Skip to the next Token
  1204. for (; Pos != Stop && *Pos != Tok; Pos++);
  1205. // Back remove spaces
  1206. char *End = Pos;
  1207. for (; End > Start && (End[-1] == Tok || isspace(End[-1]) != 0); End--);
  1208. *End = 0;
  1209. List[Count++] = Start;
  1210. if (Count >= ListMax)
  1211. {
  1212. List[Count-1] = 0;
  1213. return false;
  1214. }
  1215. // Advance pos
  1216. for (; Pos != Stop && (*Pos == Tok || isspace(*Pos) != 0 || *Pos == 0); Pos++);
  1217. Start = Pos;
  1218. }
  1219. List[Count] = 0;
  1220. return true;
  1221. }
  1222. /*}}}*/
  1223. // VectorizeString - Split a string up into a vector of strings /*{{{*/
  1224. // ---------------------------------------------------------------------
  1225. /* This can be used to split a given string up into a vector, so the
  1226. propose is the same as in the method above and this one is a bit slower
  1227. also, but the advantage is that we have an iteratable vector */
  1228. vector<string> VectorizeString(string const &haystack, char const &split)
  1229. {
  1230. vector<string> exploded;
  1231. if (haystack.empty() == true)
  1232. return exploded;
  1233. string::const_iterator start = haystack.begin();
  1234. string::const_iterator end = start;
  1235. do {
  1236. for (; end != haystack.end() && *end != split; ++end);
  1237. exploded.push_back(string(start, end));
  1238. start = end + 1;
  1239. } while (end != haystack.end() && (++end) != haystack.end());
  1240. return exploded;
  1241. }
  1242. /*}}}*/
  1243. // StringSplit - split a string into a string vector by token /*{{{*/
  1244. // ---------------------------------------------------------------------
  1245. /* See header for details.
  1246. */
  1247. vector<string> StringSplit(std::string const &s, std::string const &sep,
  1248. unsigned int maxsplit)
  1249. {
  1250. vector<string> split;
  1251. size_t start, pos;
  1252. // no separator given, this is bogus
  1253. if(sep.size() == 0)
  1254. return split;
  1255. start = pos = 0;
  1256. while (pos != string::npos)
  1257. {
  1258. pos = s.find(sep, start);
  1259. split.push_back(s.substr(start, pos-start));
  1260. // if maxsplit is reached, the remaining string is the last item
  1261. if(split.size() >= maxsplit)
  1262. {
  1263. split[split.size()-1] = s.substr(start);
  1264. break;
  1265. }
  1266. start = pos+sep.size();
  1267. }
  1268. return split;
  1269. }
  1270. /*}}}*/
  1271. // RegexChoice - Simple regex list/list matcher /*{{{*/
  1272. // ---------------------------------------------------------------------
  1273. /* */
  1274. unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
  1275. const char **ListEnd)
  1276. {
  1277. for (RxChoiceList *R = Rxs; R->Str != 0; R++)
  1278. R->Hit = false;
  1279. unsigned long Hits = 0;
  1280. for (; ListBegin < ListEnd; ++ListBegin)
  1281. {
  1282. // Check if the name is a regex
  1283. const char *I;
  1284. bool Regex = true;
  1285. for (I = *ListBegin; *I != 0; I++)
  1286. if (*I == '.' || *I == '?' || *I == '*' || *I == '|')
  1287. break;
  1288. if (*I == 0)
  1289. Regex = false;
  1290. // Compile the regex pattern
  1291. regex_t Pattern;
  1292. if (Regex == true)
  1293. if (regcomp(&Pattern,*ListBegin,REG_EXTENDED | REG_ICASE |
  1294. REG_NOSUB) != 0)
  1295. Regex = false;
  1296. // Search the list
  1297. bool Done = false;
  1298. for (RxChoiceList *R = Rxs; R->Str != 0; R++)
  1299. {
  1300. if (R->Str[0] == 0)
  1301. continue;
  1302. if (strcasecmp(R->Str,*ListBegin) != 0)
  1303. {
  1304. if (Regex == false)
  1305. continue;
  1306. if (regexec(&Pattern,R->Str,0,0,0) != 0)
  1307. continue;
  1308. }
  1309. Done = true;
  1310. if (R->Hit == false)
  1311. Hits++;
  1312. R->Hit = true;
  1313. }
  1314. if (Regex == true)
  1315. regfree(&Pattern);
  1316. if (Done == false)
  1317. _error->Warning(_("Selection %s not found"),*ListBegin);
  1318. }
  1319. return Hits;
  1320. }
  1321. /*}}}*/
  1322. // {str,io}printf - C format string outputter to C++ strings/iostreams /*{{{*/
  1323. // ---------------------------------------------------------------------
  1324. /* This is used to make the internationalization strings easier to translate
  1325. and to allow reordering of parameters */
  1326. static bool iovprintf(ostream &out, const char *format,
  1327. va_list &args, ssize_t &size) {
  1328. char *S = (char*)malloc(size);
  1329. ssize_t const n = vsnprintf(S, size, format, args);
  1330. if (n > -1 && n < size) {
  1331. out << S;
  1332. free(S);
  1333. return true;
  1334. } else {
  1335. if (n > -1)
  1336. size = n + 1;
  1337. else
  1338. size *= 2;
  1339. }
  1340. free(S);
  1341. return false;
  1342. }
  1343. void ioprintf(ostream &out,const char *format,...)
  1344. {
  1345. va_list args;
  1346. ssize_t size = 400;
  1347. while (true) {
  1348. bool ret;
  1349. va_start(args,format);
  1350. ret = iovprintf(out, format, args, size);
  1351. va_end(args);
  1352. if (ret == true)
  1353. return;
  1354. }
  1355. }
  1356. void strprintf(string &out,const char *format,...)
  1357. {
  1358. va_list args;
  1359. ssize_t size = 400;
  1360. std::ostringstream outstr;
  1361. while (true) {
  1362. bool ret;
  1363. va_start(args,format);
  1364. ret = iovprintf(outstr, format, args, size);
  1365. va_end(args);
  1366. if (ret == true)
  1367. break;
  1368. }
  1369. out = outstr.str();
  1370. }
  1371. /*}}}*/
  1372. // safe_snprintf - Safer snprintf /*{{{*/
  1373. // ---------------------------------------------------------------------
  1374. /* This is a snprintf that will never (ever) go past 'End' and returns a
  1375. pointer to the end of the new string. The returned string is always null
  1376. terminated unless Buffer == end. This is a better alterantive to using
  1377. consecutive snprintfs. */
  1378. char *safe_snprintf(char *Buffer,char *End,const char *Format,...)
  1379. {
  1380. va_list args;
  1381. int Did;
  1382. if (End <= Buffer)
  1383. return End;
  1384. va_start(args,Format);
  1385. Did = vsnprintf(Buffer,End - Buffer,Format,args);
  1386. va_end(args);
  1387. if (Did < 0 || Buffer + Did > End)
  1388. return End;
  1389. return Buffer + Did;
  1390. }
  1391. /*}}}*/
  1392. // StripEpoch - Remove the version "epoch" from a version string /*{{{*/
  1393. // ---------------------------------------------------------------------
  1394. string StripEpoch(const string &VerStr)
  1395. {
  1396. size_t i = VerStr.find(":");
  1397. if (i == string::npos)
  1398. return VerStr;
  1399. return VerStr.substr(i+1);
  1400. }
  1401. /*}}}*/
  1402. // tolower_ascii - tolower() function that ignores the locale /*{{{*/
  1403. // ---------------------------------------------------------------------
  1404. /* This little function is the most called method we have and tries
  1405. therefore to do the absolute minimum - and is notable faster than
  1406. standard tolower/toupper and as a bonus avoids problems with different
  1407. locales - we only operate on ascii chars anyway. */
  1408. #undef tolower_ascii
  1409. int tolower_ascii(int const c) APT_PURE APT_COLD;
  1410. int tolower_ascii(int const c)
  1411. {
  1412. return tolower_ascii_inline(c);
  1413. }
  1414. /*}}}*/
  1415. // isspace_ascii - isspace() function that ignores the locale /*{{{*/
  1416. // ---------------------------------------------------------------------
  1417. /* This little function is one of the most called methods we have and tries
  1418. therefore to do the absolute minimum - and is notable faster than
  1419. standard isspace() and as a bonus avoids problems with different
  1420. locales - we only operate on ascii chars anyway. */
  1421. #undef isspace_ascii
  1422. int isspace_ascii(int const c) APT_PURE APT_COLD;
  1423. int isspace_ascii(int const c)
  1424. {
  1425. return isspace_ascii_inline(c);
  1426. }
  1427. /*}}}*/
  1428. // CheckDomainList - See if Host is in a , separate list /*{{{*/
  1429. // ---------------------------------------------------------------------
  1430. /* The domain list is a comma separate list of domains that are suffix
  1431. matched against the argument */
  1432. bool CheckDomainList(const string &Host,const string &List)
  1433. {
  1434. string::const_iterator Start = List.begin();
  1435. for (string::const_iterator Cur = List.begin(); Cur <= List.end(); ++Cur)
  1436. {
  1437. if (Cur < List.end() && *Cur != ',')
  1438. continue;
  1439. // Match the end of the string..
  1440. if ((Host.size() >= (unsigned)(Cur - Start)) &&
  1441. Cur - Start != 0 &&
  1442. stringcasecmp(Host.end() - (Cur - Start),Host.end(),Start,Cur) == 0)
  1443. return true;
  1444. Start = Cur + 1;
  1445. }
  1446. return false;
  1447. }
  1448. /*}}}*/
  1449. // strv_length - Return the length of a NULL-terminated string array /*{{{*/
  1450. // ---------------------------------------------------------------------
  1451. /* */
  1452. size_t strv_length(const char **str_array)
  1453. {
  1454. size_t i;
  1455. for (i=0; str_array[i] != NULL; i++)
  1456. /* nothing */
  1457. ;
  1458. return i;
  1459. }
  1460. /*}}}*/
  1461. // DeEscapeString - unescape (\0XX and \xXX) from a string /*{{{*/
  1462. // ---------------------------------------------------------------------
  1463. /* */
  1464. string DeEscapeString(const string &input)
  1465. {
  1466. char tmp[3];
  1467. string::const_iterator it;
  1468. string output;
  1469. for (it = input.begin(); it != input.end(); ++it)
  1470. {
  1471. // just copy non-escape chars
  1472. if (*it != '\\')
  1473. {
  1474. output += *it;
  1475. continue;
  1476. }
  1477. // deal with double escape
  1478. if (*it == '\\' &&
  1479. (it + 1 < input.end()) && it[1] == '\\')
  1480. {
  1481. // copy
  1482. output += *it;
  1483. // advance iterator one step further
  1484. ++it;
  1485. continue;
  1486. }
  1487. // ensure we have a char to read
  1488. if (it + 1 == input.end())
  1489. continue;
  1490. // read it
  1491. ++it;
  1492. switch (*it)
  1493. {
  1494. case '0':
  1495. if (it + 2 <= input.end()) {
  1496. tmp[0] = it[1];
  1497. tmp[1] = it[2];
  1498. tmp[2] = 0;
  1499. output += (char)strtol(tmp, 0, 8);
  1500. it += 2;
  1501. }
  1502. break;
  1503. case 'x':
  1504. if (it + 2 <= input.end()) {
  1505. tmp[0] = it[1];
  1506. tmp[1] = it[2];
  1507. tmp[2] = 0;
  1508. output += (char)strtol(tmp, 0, 16);
  1509. it += 2;
  1510. }
  1511. break;
  1512. default:
  1513. // FIXME: raise exception here?
  1514. break;
  1515. }
  1516. }
  1517. return output;
  1518. }
  1519. /*}}}*/
  1520. // URI::CopyFrom - Copy from an object /*{{{*/
  1521. // ---------------------------------------------------------------------
  1522. /* This parses the URI into all of its components */
  1523. void URI::CopyFrom(const string &U)
  1524. {
  1525. string::const_iterator I = U.begin();
  1526. // Locate the first colon, this separates the scheme
  1527. for (; I < U.end() && *I != ':' ; ++I);
  1528. string::const_iterator FirstColon = I;
  1529. /* Determine if this is a host type URI with a leading double //
  1530. and then search for the first single / */
  1531. string::const_iterator SingleSlash = I;
  1532. if (I + 3 < U.end() && I[1] == '/' && I[2] == '/')
  1533. SingleSlash += 3;
  1534. /* Find the / indicating the end of the hostname, ignoring /'s in the
  1535. square brackets */
  1536. bool InBracket = false;
  1537. for (; SingleSlash < U.end() && (*SingleSlash != '/' || InBracket == true); ++SingleSlash)
  1538. {
  1539. if (*SingleSlash == '[')
  1540. InBracket = true;
  1541. if (InBracket == true && *SingleSlash == ']')
  1542. InBracket = false;
  1543. }
  1544. if (SingleSlash > U.end())
  1545. SingleSlash = U.end();
  1546. // We can now write the access and path specifiers
  1547. Access.assign(U.begin(),FirstColon);
  1548. if (SingleSlash != U.end())
  1549. Path.assign(SingleSlash,U.end());
  1550. if (Path.empty() == true)
  1551. Path = "/";
  1552. // Now we attempt to locate a user:pass@host fragment
  1553. if (FirstColon + 2 <= U.end() && FirstColon[1] == '/' && FirstColon[2] == '/')
  1554. FirstColon += 3;
  1555. else
  1556. FirstColon += 1;
  1557. if (FirstColon >= U.end())
  1558. return;
  1559. if (FirstColon > SingleSlash)
  1560. FirstColon = SingleSlash;
  1561. // Find the colon...
  1562. I = FirstColon + 1;
  1563. if (I > SingleSlash)
  1564. I = SingleSlash;
  1565. // Search for the @ separating user:pass from host
  1566. auto const RevAt = std::find(
  1567. std::string::const_reverse_iterator(SingleSlash),
  1568. std::string::const_reverse_iterator(I), '@');
  1569. string::const_iterator const At = RevAt.base() == I ? SingleSlash : std::prev(RevAt.base());
  1570. // and then look for the colon between user and pass
  1571. string::const_iterator const SecondColon = std::find(I, At, ':');
  1572. // Now write the host and user/pass
  1573. if (At == SingleSlash)
  1574. {
  1575. if (FirstColon < SingleSlash)
  1576. Host.assign(FirstColon,SingleSlash);
  1577. }
  1578. else
  1579. {
  1580. Host.assign(At+1,SingleSlash);
  1581. // username and password must be encoded (RFC 3986)
  1582. User.assign(DeQuoteString(FirstColon,SecondColon));
  1583. if (SecondColon < At)
  1584. Password.assign(DeQuoteString(SecondColon+1,At));
  1585. }
  1586. // Now we parse the RFC 2732 [] hostnames.
  1587. unsigned long PortEnd = 0;
  1588. InBracket = false;
  1589. for (unsigned I = 0; I != Host.length();)
  1590. {
  1591. if (Host[I] == '[')
  1592. {
  1593. InBracket = true;
  1594. Host.erase(I,1);
  1595. continue;
  1596. }
  1597. if (InBracket == true && Host[I] == ']')
  1598. {
  1599. InBracket = false;
  1600. Host.erase(I,1);
  1601. PortEnd = I;
  1602. continue;
  1603. }
  1604. I++;
  1605. }
  1606. // Tsk, weird.
  1607. if (InBracket == true)
  1608. {
  1609. Host.clear();
  1610. return;
  1611. }
  1612. // Now we parse off a port number from the hostname
  1613. Port = 0;
  1614. string::size_type Pos = Host.rfind(':');
  1615. if (Pos == string::npos || Pos < PortEnd)
  1616. return;
  1617. Port = atoi(string(Host,Pos+1).c_str());
  1618. Host.assign(Host,0,Pos);
  1619. }
  1620. /*}}}*/
  1621. // URI::operator string - Convert the URI to a string /*{{{*/
  1622. // ---------------------------------------------------------------------
  1623. /* */
  1624. URI::operator string()
  1625. {
  1626. std::stringstream Res;
  1627. if (Access.empty() == false)
  1628. Res << Access << ':';
  1629. if (Host.empty() == false)
  1630. {
  1631. if (Access.empty() == false)
  1632. Res << "//";
  1633. if (User.empty() == false)
  1634. {
  1635. // FIXME: Technically userinfo is permitted even less
  1636. // characters than these, but this is not conveniently
  1637. // expressed with a blacklist.
  1638. Res << QuoteString(User, ":/?#[]@");
  1639. if (Password.empty() == false)
  1640. Res << ":" << QuoteString(Password, ":/?#[]@");
  1641. Res << "@";
  1642. }
  1643. // Add RFC 2732 escaping characters
  1644. if (Access.empty() == false && Host.find_first_of("/:") != string::npos)
  1645. Res << '[' << Host << ']';
  1646. else
  1647. Res << Host;
  1648. if (Port != 0)
  1649. Res << ':' << std::to_string(Port);
  1650. }
  1651. if (Path.empty() == false)
  1652. {
  1653. if (Path[0] != '/')
  1654. Res << "/" << Path;
  1655. else
  1656. Res << Path;
  1657. }
  1658. return Res.str();
  1659. }
  1660. /*}}}*/
  1661. // URI::SiteOnly - Return the schema and site for the URI /*{{{*/
  1662. string URI::SiteOnly(const string &URI)
  1663. {
  1664. ::URI U(URI);
  1665. U.User.clear();
  1666. U.Password.clear();
  1667. U.Path.clear();
  1668. return U;
  1669. }
  1670. /*}}}*/
  1671. // URI::ArchiveOnly - Return the schema, site and cleaned path for the URI /*{{{*/
  1672. string URI::ArchiveOnly(const string &URI)
  1673. {
  1674. ::URI U(URI);
  1675. U.User.clear();
  1676. U.Password.clear();
  1677. if (U.Path.empty() == false && U.Path[U.Path.length() - 1] == '/')
  1678. U.Path.erase(U.Path.length() - 1);
  1679. return U;
  1680. }
  1681. /*}}}*/
  1682. // URI::NoUserPassword - Return the schema, site and path for the URI /*{{{*/
  1683. string URI::NoUserPassword(const string &URI)
  1684. {
  1685. ::URI U(URI);
  1686. U.User.clear();
  1687. U.Password.clear();
  1688. return U;
  1689. }
  1690. /*}}}*/