archive_read_support_format_ar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 *)calloc(1, 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. ar->strtab = NULL;
  105. r = __archive_read_register_format(a,
  106. ar,
  107. "ar",
  108. archive_read_format_ar_bid,
  109. NULL,
  110. archive_read_format_ar_read_header,
  111. archive_read_format_ar_read_data,
  112. archive_read_format_ar_skip,
  113. NULL,
  114. archive_read_format_ar_cleanup,
  115. NULL,
  116. NULL);
  117. if (r != ARCHIVE_OK) {
  118. free(ar);
  119. return (r);
  120. }
  121. return (ARCHIVE_OK);
  122. }
  123. static int
  124. archive_read_format_ar_cleanup(struct archive_read *a)
  125. {
  126. struct ar *ar;
  127. ar = (struct ar *)(a->format->data);
  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_FATAL);
  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 > filename && *p == '/') {
  218. *p = '\0';
  219. }
  220. if (p < filename) {
  221. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  222. "Found entry with empty filename");
  223. return (ARCHIVE_FATAL);
  224. }
  225. /*
  226. * '//' is the GNU filename table.
  227. * Later entries can refer to names in this table.
  228. */
  229. if (strcmp(filename, "//") == 0) {
  230. /* This must come before any call to _read_ahead. */
  231. ar_parse_common_header(ar, entry, h);
  232. archive_entry_copy_pathname(entry, filename);
  233. archive_entry_set_filetype(entry, AE_IFREG);
  234. /* Get the size of the filename table. */
  235. number = ar_atol10(h + AR_size_offset, AR_size_size);
  236. if (number > SIZE_MAX || number > 1024 * 1024 * 1024) {
  237. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  238. "Filename table too large");
  239. return (ARCHIVE_FATAL);
  240. }
  241. entry_size = (size_t)number;
  242. if (entry_size == 0) {
  243. archive_set_error(&a->archive, EINVAL,
  244. "Invalid string table");
  245. return (ARCHIVE_FATAL);
  246. }
  247. if (ar->strtab != NULL) {
  248. archive_set_error(&a->archive, EINVAL,
  249. "More than one string tables exist");
  250. return (ARCHIVE_FATAL);
  251. }
  252. /* Read the filename table into memory. */
  253. st = malloc(entry_size);
  254. if (st == NULL) {
  255. archive_set_error(&a->archive, ENOMEM,
  256. "Can't allocate filename table buffer");
  257. return (ARCHIVE_FATAL);
  258. }
  259. ar->strtab = st;
  260. ar->strtab_size = entry_size;
  261. if (*unconsumed) {
  262. __archive_read_consume(a, *unconsumed);
  263. *unconsumed = 0;
  264. }
  265. if ((b = __archive_read_ahead(a, entry_size, NULL)) == NULL)
  266. return (ARCHIVE_FATAL);
  267. memcpy(st, b, entry_size);
  268. __archive_read_consume(a, entry_size);
  269. /* All contents are consumed. */
  270. ar->entry_bytes_remaining = 0;
  271. archive_entry_set_size(entry, ar->entry_bytes_remaining);
  272. /* Parse the filename table. */
  273. return (ar_parse_gnu_filename_table(a));
  274. }
  275. /*
  276. * GNU variant handles long filenames by storing /<number>
  277. * to indicate a name stored in the filename table.
  278. * XXX TODO: Verify that it's all digits... Don't be fooled
  279. * by "/9xyz" XXX
  280. */
  281. if (filename[0] == '/' && filename[1] >= '0' && filename[1] <= '9') {
  282. number = ar_atol10(h + AR_name_offset + 1, AR_name_size - 1);
  283. /*
  284. * If we can't look up the real name, warn and return
  285. * the entry with the wrong name.
  286. */
  287. if (ar->strtab == NULL || number >= ar->strtab_size) {
  288. archive_set_error(&a->archive, EINVAL,
  289. "Can't find long filename for GNU/SVR4 archive entry");
  290. archive_entry_copy_pathname(entry, filename);
  291. /* Parse the time, owner, mode, size fields. */
  292. ar_parse_common_header(ar, entry, h);
  293. return (ARCHIVE_FATAL);
  294. }
  295. archive_entry_copy_pathname(entry, &ar->strtab[(size_t)number]);
  296. /* Parse the time, owner, mode, size fields. */
  297. return (ar_parse_common_header(ar, entry, h));
  298. }
  299. /*
  300. * BSD handles long filenames by storing "#1/" followed by the
  301. * length of filename as a decimal number, then prepends the
  302. * the filename to the file contents.
  303. */
  304. if (strncmp(filename, "#1/", 3) == 0) {
  305. /* Parse the time, owner, mode, size fields. */
  306. /* This must occur before _read_ahead is called again. */
  307. ar_parse_common_header(ar, entry, h);
  308. /* Parse the size of the name, adjust the file size. */
  309. number = ar_atol10(h + AR_name_offset + 3, AR_name_size - 3);
  310. /* Sanity check the filename length:
  311. * = Must be <= SIZE_MAX - 1
  312. * = Must be <= 1MB
  313. * = Cannot be bigger than the entire entry
  314. */
  315. if (number > SIZE_MAX - 1
  316. || number > 1024 * 1024
  317. || (int64_t)number > ar->entry_bytes_remaining) {
  318. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  319. "Bad input file size");
  320. return (ARCHIVE_FATAL);
  321. }
  322. bsd_name_length = (size_t)number;
  323. ar->entry_bytes_remaining -= bsd_name_length;
  324. /* Adjust file size reported to client. */
  325. archive_entry_set_size(entry, ar->entry_bytes_remaining);
  326. if (*unconsumed) {
  327. __archive_read_consume(a, *unconsumed);
  328. *unconsumed = 0;
  329. }
  330. /* Read the long name into memory. */
  331. if ((b = __archive_read_ahead(a, bsd_name_length, NULL)) == NULL) {
  332. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  333. "Truncated input file");
  334. return (ARCHIVE_FATAL);
  335. }
  336. /* Store it in the entry. */
  337. p = (char *)malloc(bsd_name_length + 1);
  338. if (p == NULL) {
  339. archive_set_error(&a->archive, ENOMEM,
  340. "Can't allocate fname buffer");
  341. return (ARCHIVE_FATAL);
  342. }
  343. strncpy(p, b, bsd_name_length);
  344. p[bsd_name_length] = '\0';
  345. __archive_read_consume(a, bsd_name_length);
  346. archive_entry_copy_pathname(entry, p);
  347. free(p);
  348. return (ARCHIVE_OK);
  349. }
  350. /*
  351. * "/" is the SVR4/GNU archive symbol table.
  352. * "/SYM64/" is the SVR4/GNU 64-bit variant archive symbol table.
  353. */
  354. if (strcmp(filename, "/") == 0 || strcmp(filename, "/SYM64/") == 0) {
  355. archive_entry_copy_pathname(entry, filename);
  356. /* Parse the time, owner, mode, size fields. */
  357. r = ar_parse_common_header(ar, entry, h);
  358. /* Force the file type to a regular file. */
  359. archive_entry_set_filetype(entry, AE_IFREG);
  360. return (r);
  361. }
  362. /*
  363. * "__.SYMDEF" is a BSD archive symbol table.
  364. */
  365. if (strcmp(filename, "__.SYMDEF") == 0) {
  366. archive_entry_copy_pathname(entry, filename);
  367. /* Parse the time, owner, mode, size fields. */
  368. return (ar_parse_common_header(ar, entry, h));
  369. }
  370. /*
  371. * Otherwise, this is a standard entry. The filename
  372. * has already been trimmed as much as possible, based
  373. * on our current knowledge of the format.
  374. */
  375. archive_entry_copy_pathname(entry, filename);
  376. return (ar_parse_common_header(ar, entry, h));
  377. }
  378. static int
  379. archive_read_format_ar_read_header(struct archive_read *a,
  380. struct archive_entry *entry)
  381. {
  382. struct ar *ar = (struct ar*)(a->format->data);
  383. size_t unconsumed;
  384. const void *header_data;
  385. int ret;
  386. if (!ar->read_global_header) {
  387. /*
  388. * We are now at the beginning of the archive,
  389. * so we need first consume the ar global header.
  390. */
  391. __archive_read_consume(a, 8);
  392. ar->read_global_header = 1;
  393. /* Set a default format code for now. */
  394. a->archive.archive_format = ARCHIVE_FORMAT_AR;
  395. }
  396. /* Read the header for the next file entry. */
  397. if ((header_data = __archive_read_ahead(a, 60, NULL)) == NULL)
  398. /* Broken header. */
  399. return (ARCHIVE_EOF);
  400. unconsumed = 60;
  401. ret = _ar_read_header(a, entry, ar, (const char *)header_data, &unconsumed);
  402. if (unconsumed)
  403. __archive_read_consume(a, unconsumed);
  404. return ret;
  405. }
  406. static int
  407. ar_parse_common_header(struct ar *ar, struct archive_entry *entry,
  408. const char *h)
  409. {
  410. uint64_t n;
  411. /* Copy remaining header */
  412. archive_entry_set_filetype(entry, AE_IFREG);
  413. archive_entry_set_mtime(entry,
  414. (time_t)ar_atol10(h + AR_date_offset, AR_date_size), 0L);
  415. archive_entry_set_uid(entry,
  416. (uid_t)ar_atol10(h + AR_uid_offset, AR_uid_size));
  417. archive_entry_set_gid(entry,
  418. (gid_t)ar_atol10(h + AR_gid_offset, AR_gid_size));
  419. archive_entry_set_mode(entry,
  420. (mode_t)ar_atol8(h + AR_mode_offset, AR_mode_size));
  421. n = ar_atol10(h + AR_size_offset, AR_size_size);
  422. ar->entry_offset = 0;
  423. ar->entry_padding = n % 2;
  424. archive_entry_set_size(entry, n);
  425. ar->entry_bytes_remaining = n;
  426. return (ARCHIVE_OK);
  427. }
  428. static int
  429. archive_read_format_ar_read_data(struct archive_read *a,
  430. const void **buff, size_t *size, int64_t *offset)
  431. {
  432. ssize_t bytes_read;
  433. struct ar *ar;
  434. ar = (struct ar *)(a->format->data);
  435. if (ar->entry_bytes_unconsumed) {
  436. __archive_read_consume(a, ar->entry_bytes_unconsumed);
  437. ar->entry_bytes_unconsumed = 0;
  438. }
  439. if (ar->entry_bytes_remaining > 0) {
  440. *buff = __archive_read_ahead(a, 1, &bytes_read);
  441. if (bytes_read == 0) {
  442. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  443. "Truncated ar archive");
  444. return (ARCHIVE_FATAL);
  445. }
  446. if (bytes_read < 0)
  447. return (ARCHIVE_FATAL);
  448. if (bytes_read > ar->entry_bytes_remaining)
  449. bytes_read = (ssize_t)ar->entry_bytes_remaining;
  450. *size = bytes_read;
  451. ar->entry_bytes_unconsumed = bytes_read;
  452. *offset = ar->entry_offset;
  453. ar->entry_offset += bytes_read;
  454. ar->entry_bytes_remaining -= bytes_read;
  455. return (ARCHIVE_OK);
  456. } else {
  457. int64_t skipped = __archive_read_consume(a, ar->entry_padding);
  458. if (skipped >= 0) {
  459. ar->entry_padding -= skipped;
  460. }
  461. if (ar->entry_padding) {
  462. if (skipped >= 0) {
  463. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  464. "Truncated ar archive- failed consuming padding");
  465. }
  466. return (ARCHIVE_FATAL);
  467. }
  468. *buff = NULL;
  469. *size = 0;
  470. *offset = ar->entry_offset;
  471. return (ARCHIVE_EOF);
  472. }
  473. }
  474. static int
  475. archive_read_format_ar_skip(struct archive_read *a)
  476. {
  477. int64_t bytes_skipped;
  478. struct ar* ar;
  479. ar = (struct ar *)(a->format->data);
  480. bytes_skipped = __archive_read_consume(a,
  481. ar->entry_bytes_remaining + ar->entry_padding
  482. + ar->entry_bytes_unconsumed);
  483. if (bytes_skipped < 0)
  484. return (ARCHIVE_FATAL);
  485. ar->entry_bytes_remaining = 0;
  486. ar->entry_bytes_unconsumed = 0;
  487. ar->entry_padding = 0;
  488. return (ARCHIVE_OK);
  489. }
  490. static int
  491. ar_parse_gnu_filename_table(struct archive_read *a)
  492. {
  493. struct ar *ar;
  494. char *p;
  495. size_t size;
  496. ar = (struct ar*)(a->format->data);
  497. size = ar->strtab_size;
  498. for (p = ar->strtab; p < ar->strtab + size - 1; ++p) {
  499. if (*p == '/') {
  500. *p++ = '\0';
  501. if (*p != '\n')
  502. goto bad_string_table;
  503. *p = '\0';
  504. }
  505. }
  506. /*
  507. * GNU ar always pads the table to an even size.
  508. * The pad character is either '\n' or '`'.
  509. */
  510. if (p != ar->strtab + size && *p != '\n' && *p != '`')
  511. goto bad_string_table;
  512. /* Enforce zero termination. */
  513. ar->strtab[size - 1] = '\0';
  514. return (ARCHIVE_OK);
  515. bad_string_table:
  516. archive_set_error(&a->archive, EINVAL,
  517. "Invalid string table");
  518. free(ar->strtab);
  519. ar->strtab = NULL;
  520. return (ARCHIVE_FATAL);
  521. }
  522. static uint64_t
  523. ar_atol8(const char *p, unsigned char_cnt)
  524. {
  525. uint64_t l, limit, last_digit_limit;
  526. unsigned int digit, base;
  527. base = 8;
  528. limit = UINT64_MAX / base;
  529. last_digit_limit = UINT64_MAX % base;
  530. while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
  531. p++;
  532. l = 0;
  533. digit = *p - '0';
  534. while (*p >= '0' && digit < base && char_cnt-- > 0) {
  535. if (l>limit || (l == limit && digit > last_digit_limit)) {
  536. l = UINT64_MAX; /* Truncate on overflow. */
  537. break;
  538. }
  539. l = (l * base) + digit;
  540. digit = *++p - '0';
  541. }
  542. return (l);
  543. }
  544. static uint64_t
  545. ar_atol10(const char *p, unsigned char_cnt)
  546. {
  547. uint64_t l, limit, last_digit_limit;
  548. unsigned int base, digit;
  549. base = 10;
  550. limit = UINT64_MAX / base;
  551. last_digit_limit = UINT64_MAX % base;
  552. while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
  553. p++;
  554. l = 0;
  555. digit = *p - '0';
  556. while (*p >= '0' && digit < base && char_cnt-- > 0) {
  557. if (l > limit || (l == limit && digit > last_digit_limit)) {
  558. l = UINT64_MAX; /* Truncate on overflow. */
  559. break;
  560. }
  561. l = (l * base) + digit;
  562. digit = *++p - '0';
  563. }
  564. return (l);
  565. }