archive_read_support_format_ar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*-
  2. * Copyright (c) 2007 Kai Wang
  3. * Copyright (c) 2007 Tim Kientzle
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer
  11. * in this position and unchanged.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_ar.c 201101 2009-12-28 03:06:27Z kientzle $");
  29. #ifdef HAVE_SYS_STAT_H
  30. #include <sys/stat.h>
  31. #endif
  32. #ifdef HAVE_ERRNO_H
  33. #include <errno.h>
  34. #endif
  35. #ifdef HAVE_STDLIB_H
  36. #include <stdlib.h>
  37. #endif
  38. #ifdef HAVE_STRING_H
  39. #include <string.h>
  40. #endif
  41. #ifdef HAVE_LIMITS_H
  42. #include <limits.h>
  43. #endif
  44. #include "archive.h"
  45. #include "archive_entry.h"
  46. #include "archive_private.h"
  47. #include "archive_read_private.h"
  48. struct ar {
  49. int64_t entry_bytes_remaining;
  50. /* unconsumed is purely to track data we've gotten from readahead,
  51. * but haven't yet marked as consumed. Must be paired with
  52. * entry_bytes_remaining usage/modification.
  53. */
  54. size_t entry_bytes_unconsumed;
  55. int64_t entry_offset;
  56. int64_t entry_padding;
  57. char *strtab;
  58. size_t strtab_size;
  59. char read_global_header;
  60. };
  61. /*
  62. * Define structure of the "ar" header.
  63. */
  64. #define AR_name_offset 0
  65. #define AR_name_size 16
  66. #define AR_date_offset 16
  67. #define AR_date_size 12
  68. #define AR_uid_offset 28
  69. #define AR_uid_size 6
  70. #define AR_gid_offset 34
  71. #define AR_gid_size 6
  72. #define AR_mode_offset 40
  73. #define AR_mode_size 8
  74. #define AR_size_offset 48
  75. #define AR_size_size 10
  76. #define AR_fmag_offset 58
  77. #define AR_fmag_size 2
  78. static int archive_read_format_ar_bid(struct archive_read *a, int);
  79. static int archive_read_format_ar_cleanup(struct archive_read *a);
  80. static int archive_read_format_ar_read_data(struct archive_read *a,
  81. const void **buff, size_t *size, int64_t *offset);
  82. static int archive_read_format_ar_skip(struct archive_read *a);
  83. static int archive_read_format_ar_read_header(struct archive_read *a,
  84. struct archive_entry *e);
  85. static uint64_t ar_atol8(const char *p, unsigned char_cnt);
  86. static uint64_t ar_atol10(const char *p, unsigned char_cnt);
  87. static int ar_parse_gnu_filename_table(struct archive_read *a);
  88. static int ar_parse_common_header(struct ar *ar, struct archive_entry *,
  89. const char *h);
  90. int
  91. archive_read_support_format_ar(struct archive *_a)
  92. {
  93. struct archive_read *a = (struct archive_read *)_a;
  94. struct ar *ar;
  95. int r;
  96. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  97. ARCHIVE_STATE_NEW, "archive_read_support_format_ar");
  98. ar = (struct ar *)malloc(sizeof(*ar));
  99. if (ar == NULL) {
  100. archive_set_error(&a->archive, ENOMEM,
  101. "Can't allocate ar data");
  102. return (ARCHIVE_FATAL);
  103. }
  104. memset(ar, 0, sizeof(*ar));
  105. ar->strtab = NULL;
  106. r = __archive_read_register_format(a,
  107. ar,
  108. "ar",
  109. archive_read_format_ar_bid,
  110. NULL,
  111. archive_read_format_ar_read_header,
  112. archive_read_format_ar_read_data,
  113. archive_read_format_ar_skip,
  114. NULL,
  115. archive_read_format_ar_cleanup);
  116. if (r != ARCHIVE_OK) {
  117. free(ar);
  118. return (r);
  119. }
  120. return (ARCHIVE_OK);
  121. }
  122. static int
  123. archive_read_format_ar_cleanup(struct archive_read *a)
  124. {
  125. struct ar *ar;
  126. ar = (struct ar *)(a->format->data);
  127. if (ar->strtab)
  128. free(ar->strtab);
  129. free(ar);
  130. (a->format->data) = NULL;
  131. return (ARCHIVE_OK);
  132. }
  133. static int
  134. archive_read_format_ar_bid(struct archive_read *a, int best_bid)
  135. {
  136. const void *h;
  137. (void)best_bid; /* UNUSED */
  138. /*
  139. * Verify the 8-byte file signature.
  140. * TODO: Do we need to check more than this?
  141. */
  142. if ((h = __archive_read_ahead(a, 8, NULL)) == NULL)
  143. return (-1);
  144. if (memcmp(h, "!<arch>\n", 8) == 0) {
  145. return (64);
  146. }
  147. return (-1);
  148. }
  149. static int
  150. _ar_read_header(struct archive_read *a, struct archive_entry *entry,
  151. struct ar *ar, const char *h, size_t *unconsumed)
  152. {
  153. char filename[AR_name_size + 1];
  154. uint64_t number; /* Used to hold parsed numbers before validation. */
  155. size_t bsd_name_length, entry_size;
  156. char *p, *st;
  157. const void *b;
  158. int r;
  159. /* Verify the magic signature on the file header. */
  160. if (strncmp(h + AR_fmag_offset, "`\n", 2) != 0) {
  161. archive_set_error(&a->archive, EINVAL,
  162. "Incorrect file header signature");
  163. return (ARCHIVE_WARN);
  164. }
  165. /* Copy filename into work buffer. */
  166. strncpy(filename, h + AR_name_offset, AR_name_size);
  167. filename[AR_name_size] = '\0';
  168. /*
  169. * Guess the format variant based on the filename.
  170. */
  171. if (a->archive.archive_format == ARCHIVE_FORMAT_AR) {
  172. /* We don't already know the variant, so let's guess. */
  173. /*
  174. * Biggest clue is presence of '/': GNU starts special
  175. * filenames with '/', appends '/' as terminator to
  176. * non-special names, so anything with '/' should be
  177. * GNU except for BSD long filenames.
  178. */
  179. if (strncmp(filename, "#1/", 3) == 0)
  180. a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
  181. else if (strchr(filename, '/') != NULL)
  182. a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
  183. else if (strncmp(filename, "__.SYMDEF", 9) == 0)
  184. a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
  185. /*
  186. * XXX Do GNU/SVR4 'ar' programs ever omit trailing '/'
  187. * if name exactly fills 16-byte field? If so, we
  188. * can't assume entries without '/' are BSD. XXX
  189. */
  190. }
  191. /* Update format name from the code. */
  192. if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU)
  193. a->archive.archive_format_name = "ar (GNU/SVR4)";
  194. else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD)
  195. a->archive.archive_format_name = "ar (BSD)";
  196. else
  197. a->archive.archive_format_name = "ar";
  198. /*
  199. * Remove trailing spaces from the filename. GNU and BSD
  200. * variants both pad filename area out with spaces.
  201. * This will only be wrong if GNU/SVR4 'ar' implementations
  202. * omit trailing '/' for 16-char filenames and we have
  203. * a 16-char filename that ends in ' '.
  204. */
  205. p = filename + AR_name_size - 1;
  206. while (p >= filename && *p == ' ') {
  207. *p = '\0';
  208. p--;
  209. }
  210. /*
  211. * Remove trailing slash unless first character is '/'.
  212. * (BSD entries never end in '/', so this will only trim
  213. * GNU-format entries. GNU special entries start with '/'
  214. * and are not terminated in '/', so we don't trim anything
  215. * that starts with '/'.)
  216. */
  217. if (filename[0] != '/' && *p == '/')
  218. *p = '\0';
  219. /*
  220. * '//' is the GNU filename table.
  221. * Later entries can refer to names in this table.
  222. */
  223. if (strcmp(filename, "//") == 0) {
  224. /* This must come before any call to _read_ahead. */
  225. ar_parse_common_header(ar, entry, h);
  226. archive_entry_copy_pathname(entry, filename);
  227. archive_entry_set_filetype(entry, AE_IFREG);
  228. /* Get the size of the filename table. */
  229. number = ar_atol10(h + AR_size_offset, AR_size_size);
  230. if (number > SIZE_MAX) {
  231. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  232. "Filename table too large");
  233. return (ARCHIVE_FATAL);
  234. }
  235. entry_size = (size_t)number;
  236. if (entry_size == 0) {
  237. archive_set_error(&a->archive, EINVAL,
  238. "Invalid string table");
  239. return (ARCHIVE_WARN);
  240. }
  241. if (ar->strtab != NULL) {
  242. archive_set_error(&a->archive, EINVAL,
  243. "More than one string tables exist");
  244. return (ARCHIVE_WARN);
  245. }
  246. /* Read the filename table into memory. */
  247. st = malloc(entry_size);
  248. if (st == NULL) {
  249. archive_set_error(&a->archive, ENOMEM,
  250. "Can't allocate filename table buffer");
  251. return (ARCHIVE_FATAL);
  252. }
  253. ar->strtab = st;
  254. ar->strtab_size = entry_size;
  255. if (*unconsumed) {
  256. __archive_read_consume(a, *unconsumed);
  257. *unconsumed = 0;
  258. }
  259. if ((b = __archive_read_ahead(a, entry_size, NULL)) == NULL)
  260. return (ARCHIVE_FATAL);
  261. memcpy(st, b, entry_size);
  262. __archive_read_consume(a, entry_size);
  263. /* All contents are consumed. */
  264. ar->entry_bytes_remaining = 0;
  265. archive_entry_set_size(entry, ar->entry_bytes_remaining);
  266. /* Parse the filename table. */
  267. return (ar_parse_gnu_filename_table(a));
  268. }
  269. /*
  270. * GNU variant handles long filenames by storing /<number>
  271. * to indicate a name stored in the filename table.
  272. * XXX TODO: Verify that it's all digits... Don't be fooled
  273. * by "/9xyz" XXX
  274. */
  275. if (filename[0] == '/' && filename[1] >= '0' && filename[1] <= '9') {
  276. number = ar_atol10(h + AR_name_offset + 1, AR_name_size - 1);
  277. /*
  278. * If we can't look up the real name, warn and return
  279. * the entry with the wrong name.
  280. */
  281. if (ar->strtab == NULL || number > ar->strtab_size) {
  282. archive_set_error(&a->archive, EINVAL,
  283. "Can't find long filename for entry");
  284. archive_entry_copy_pathname(entry, filename);
  285. /* Parse the time, owner, mode, size fields. */
  286. ar_parse_common_header(ar, entry, h);
  287. return (ARCHIVE_WARN);
  288. }
  289. archive_entry_copy_pathname(entry, &ar->strtab[(size_t)number]);
  290. /* Parse the time, owner, mode, size fields. */
  291. return (ar_parse_common_header(ar, entry, h));
  292. }
  293. /*
  294. * BSD handles long filenames by storing "#1/" followed by the
  295. * length of filename as a decimal number, then prepends the
  296. * the filename to the file contents.
  297. */
  298. if (strncmp(filename, "#1/", 3) == 0) {
  299. /* Parse the time, owner, mode, size fields. */
  300. /* This must occur before _read_ahead is called again. */
  301. ar_parse_common_header(ar, entry, h);
  302. /* Parse the size of the name, adjust the file size. */
  303. number = ar_atol10(h + AR_name_offset + 3, AR_name_size - 3);
  304. bsd_name_length = (size_t)number;
  305. /* Guard against the filename + trailing NUL
  306. * overflowing a size_t and against the filename size
  307. * being larger than the entire entry. */
  308. if (number > (uint64_t)(bsd_name_length + 1)
  309. || (int64_t)bsd_name_length > ar->entry_bytes_remaining) {
  310. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  311. "Bad input file size");
  312. return (ARCHIVE_FATAL);
  313. }
  314. ar->entry_bytes_remaining -= bsd_name_length;
  315. /* Adjust file size reported to client. */
  316. archive_entry_set_size(entry, ar->entry_bytes_remaining);
  317. if (*unconsumed) {
  318. __archive_read_consume(a, *unconsumed);
  319. *unconsumed = 0;
  320. }
  321. /* Read the long name into memory. */
  322. if ((b = __archive_read_ahead(a, bsd_name_length, NULL)) == NULL) {
  323. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  324. "Truncated input file");
  325. return (ARCHIVE_FATAL);
  326. }
  327. /* Store it in the entry. */
  328. p = (char *)malloc(bsd_name_length + 1);
  329. if (p == NULL) {
  330. archive_set_error(&a->archive, ENOMEM,
  331. "Can't allocate fname buffer");
  332. return (ARCHIVE_FATAL);
  333. }
  334. strncpy(p, b, bsd_name_length);
  335. p[bsd_name_length] = '\0';
  336. __archive_read_consume(a, bsd_name_length);
  337. archive_entry_copy_pathname(entry, p);
  338. free(p);
  339. return (ARCHIVE_OK);
  340. }
  341. /*
  342. * "/" is the SVR4/GNU archive symbol table.
  343. */
  344. if (strcmp(filename, "/") == 0) {
  345. archive_entry_copy_pathname(entry, "/");
  346. /* Parse the time, owner, mode, size fields. */
  347. r = ar_parse_common_header(ar, entry, h);
  348. /* Force the file type to a regular file. */
  349. archive_entry_set_filetype(entry, AE_IFREG);
  350. return (r);
  351. }
  352. /*
  353. * "__.SYMDEF" is a BSD archive symbol table.
  354. */
  355. if (strcmp(filename, "__.SYMDEF") == 0) {
  356. archive_entry_copy_pathname(entry, filename);
  357. /* Parse the time, owner, mode, size fields. */
  358. return (ar_parse_common_header(ar, entry, h));
  359. }
  360. /*
  361. * Otherwise, this is a standard entry. The filename
  362. * has already been trimmed as much as possible, based
  363. * on our current knowledge of the format.
  364. */
  365. archive_entry_copy_pathname(entry, filename);
  366. return (ar_parse_common_header(ar, entry, h));
  367. }
  368. static int
  369. archive_read_format_ar_read_header(struct archive_read *a,
  370. struct archive_entry *entry)
  371. {
  372. struct ar *ar = (struct ar*)(a->format->data);
  373. size_t unconsumed;
  374. const void *header_data;
  375. int ret;
  376. if (!ar->read_global_header) {
  377. /*
  378. * We are now at the beginning of the archive,
  379. * so we need first consume the ar global header.
  380. */
  381. __archive_read_consume(a, 8);
  382. ar->read_global_header = 1;
  383. /* Set a default format code for now. */
  384. a->archive.archive_format = ARCHIVE_FORMAT_AR;
  385. }
  386. /* Read the header for the next file entry. */
  387. if ((header_data = __archive_read_ahead(a, 60, NULL)) == NULL)
  388. /* Broken header. */
  389. return (ARCHIVE_EOF);
  390. unconsumed = 60;
  391. ret = _ar_read_header(a, entry, ar, (const char *)header_data, &unconsumed);
  392. if (unconsumed)
  393. __archive_read_consume(a, unconsumed);
  394. return ret;
  395. }
  396. static int
  397. ar_parse_common_header(struct ar *ar, struct archive_entry *entry,
  398. const char *h)
  399. {
  400. uint64_t n;
  401. /* Copy remaining header */
  402. archive_entry_set_mtime(entry,
  403. (time_t)ar_atol10(h + AR_date_offset, AR_date_size), 0L);
  404. archive_entry_set_uid(entry,
  405. (uid_t)ar_atol10(h + AR_uid_offset, AR_uid_size));
  406. archive_entry_set_gid(entry,
  407. (gid_t)ar_atol10(h + AR_gid_offset, AR_gid_size));
  408. archive_entry_set_mode(entry,
  409. (mode_t)ar_atol8(h + AR_mode_offset, AR_mode_size));
  410. n = ar_atol10(h + AR_size_offset, AR_size_size);
  411. ar->entry_offset = 0;
  412. ar->entry_padding = n % 2;
  413. archive_entry_set_size(entry, n);
  414. ar->entry_bytes_remaining = n;
  415. return (ARCHIVE_OK);
  416. }
  417. static int
  418. archive_read_format_ar_read_data(struct archive_read *a,
  419. const void **buff, size_t *size, int64_t *offset)
  420. {
  421. ssize_t bytes_read;
  422. struct ar *ar;
  423. ar = (struct ar *)(a->format->data);
  424. if (ar->entry_bytes_unconsumed) {
  425. __archive_read_consume(a, ar->entry_bytes_unconsumed);
  426. ar->entry_bytes_unconsumed = 0;
  427. }
  428. if (ar->entry_bytes_remaining > 0) {
  429. *buff = __archive_read_ahead(a, 1, &bytes_read);
  430. if (bytes_read == 0) {
  431. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  432. "Truncated ar archive");
  433. return (ARCHIVE_FATAL);
  434. }
  435. if (bytes_read < 0)
  436. return (ARCHIVE_FATAL);
  437. if (bytes_read > ar->entry_bytes_remaining)
  438. bytes_read = (ssize_t)ar->entry_bytes_remaining;
  439. *size = bytes_read;
  440. ar->entry_bytes_unconsumed = bytes_read;
  441. *offset = ar->entry_offset;
  442. ar->entry_offset += bytes_read;
  443. ar->entry_bytes_remaining -= bytes_read;
  444. return (ARCHIVE_OK);
  445. } else {
  446. int64_t skipped = __archive_read_consume(a, ar->entry_padding);
  447. if (skipped >= 0) {
  448. ar->entry_padding -= skipped;
  449. }
  450. if (ar->entry_padding) {
  451. if (skipped >= 0) {
  452. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  453. "Truncated ar archive- failed consuming padding");
  454. }
  455. return (ARCHIVE_FATAL);
  456. }
  457. *buff = NULL;
  458. *size = 0;
  459. *offset = ar->entry_offset;
  460. return (ARCHIVE_EOF);
  461. }
  462. }
  463. static int
  464. archive_read_format_ar_skip(struct archive_read *a)
  465. {
  466. int64_t bytes_skipped;
  467. struct ar* ar;
  468. ar = (struct ar *)(a->format->data);
  469. bytes_skipped = __archive_read_consume(a,
  470. ar->entry_bytes_remaining + ar->entry_padding
  471. + ar->entry_bytes_unconsumed);
  472. if (bytes_skipped < 0)
  473. return (ARCHIVE_FATAL);
  474. ar->entry_bytes_remaining = 0;
  475. ar->entry_bytes_unconsumed = 0;
  476. ar->entry_padding = 0;
  477. return (ARCHIVE_OK);
  478. }
  479. static int
  480. ar_parse_gnu_filename_table(struct archive_read *a)
  481. {
  482. struct ar *ar;
  483. char *p;
  484. size_t size;
  485. ar = (struct ar*)(a->format->data);
  486. size = ar->strtab_size;
  487. for (p = ar->strtab; p < ar->strtab + size - 1; ++p) {
  488. if (*p == '/') {
  489. *p++ = '\0';
  490. if (*p != '\n')
  491. goto bad_string_table;
  492. *p = '\0';
  493. }
  494. }
  495. /*
  496. * GNU ar always pads the table to an even size.
  497. * The pad character is either '\n' or '`'.
  498. */
  499. if (p != ar->strtab + size && *p != '\n' && *p != '`')
  500. goto bad_string_table;
  501. /* Enforce zero termination. */
  502. ar->strtab[size - 1] = '\0';
  503. return (ARCHIVE_OK);
  504. bad_string_table:
  505. archive_set_error(&a->archive, EINVAL,
  506. "Invalid string table");
  507. free(ar->strtab);
  508. ar->strtab = NULL;
  509. return (ARCHIVE_WARN);
  510. }
  511. static uint64_t
  512. ar_atol8(const char *p, unsigned char_cnt)
  513. {
  514. uint64_t l, limit, last_digit_limit;
  515. unsigned int digit, base;
  516. base = 8;
  517. limit = UINT64_MAX / base;
  518. last_digit_limit = UINT64_MAX % base;
  519. while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
  520. p++;
  521. l = 0;
  522. digit = *p - '0';
  523. while (*p >= '0' && digit < base && char_cnt-- > 0) {
  524. if (l>limit || (l == limit && digit > last_digit_limit)) {
  525. l = UINT64_MAX; /* Truncate on overflow. */
  526. break;
  527. }
  528. l = (l * base) + digit;
  529. digit = *++p - '0';
  530. }
  531. return (l);
  532. }
  533. static uint64_t
  534. ar_atol10(const char *p, unsigned char_cnt)
  535. {
  536. uint64_t l, limit, last_digit_limit;
  537. unsigned int base, digit;
  538. base = 10;
  539. limit = UINT64_MAX / base;
  540. last_digit_limit = UINT64_MAX % base;
  541. while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
  542. p++;
  543. l = 0;
  544. digit = *p - '0';
  545. while (*p >= '0' && digit < base && char_cnt-- > 0) {
  546. if (l > limit || (l == limit && digit > last_digit_limit)) {
  547. l = UINT64_MAX; /* Truncate on overflow. */
  548. break;
  549. }
  550. l = (l * base) + digit;
  551. digit = *++p - '0';
  552. }
  553. return (l);
  554. }