archive_read_support_format_ar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. if (ar->strtab)
  129. free(ar->strtab);
  130. free(ar);
  131. (a->format->data) = NULL;
  132. return (ARCHIVE_OK);
  133. }
  134. static int
  135. archive_read_format_ar_bid(struct archive_read *a, int best_bid)
  136. {
  137. const void *h;
  138. (void)best_bid; /* UNUSED */
  139. /*
  140. * Verify the 8-byte file signature.
  141. * TODO: Do we need to check more than this?
  142. */
  143. if ((h = __archive_read_ahead(a, 8, NULL)) == NULL)
  144. return (-1);
  145. if (memcmp(h, "!<arch>\n", 8) == 0) {
  146. return (64);
  147. }
  148. return (-1);
  149. }
  150. static int
  151. _ar_read_header(struct archive_read *a, struct archive_entry *entry,
  152. struct ar *ar, const char *h, size_t *unconsumed)
  153. {
  154. char filename[AR_name_size + 1];
  155. uint64_t number; /* Used to hold parsed numbers before validation. */
  156. size_t bsd_name_length, entry_size;
  157. char *p, *st;
  158. const void *b;
  159. int r;
  160. /* Verify the magic signature on the file header. */
  161. if (strncmp(h + AR_fmag_offset, "`\n", 2) != 0) {
  162. archive_set_error(&a->archive, EINVAL,
  163. "Incorrect file header signature");
  164. return (ARCHIVE_FATAL);
  165. }
  166. /* Copy filename into work buffer. */
  167. strncpy(filename, h + AR_name_offset, AR_name_size);
  168. filename[AR_name_size] = '\0';
  169. /*
  170. * Guess the format variant based on the filename.
  171. */
  172. if (a->archive.archive_format == ARCHIVE_FORMAT_AR) {
  173. /* We don't already know the variant, so let's guess. */
  174. /*
  175. * Biggest clue is presence of '/': GNU starts special
  176. * filenames with '/', appends '/' as terminator to
  177. * non-special names, so anything with '/' should be
  178. * GNU except for BSD long filenames.
  179. */
  180. if (strncmp(filename, "#1/", 3) == 0)
  181. a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
  182. else if (strchr(filename, '/') != NULL)
  183. a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
  184. else if (strncmp(filename, "__.SYMDEF", 9) == 0)
  185. a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
  186. /*
  187. * XXX Do GNU/SVR4 'ar' programs ever omit trailing '/'
  188. * if name exactly fills 16-byte field? If so, we
  189. * can't assume entries without '/' are BSD. XXX
  190. */
  191. }
  192. /* Update format name from the code. */
  193. if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU)
  194. a->archive.archive_format_name = "ar (GNU/SVR4)";
  195. else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD)
  196. a->archive.archive_format_name = "ar (BSD)";
  197. else
  198. a->archive.archive_format_name = "ar";
  199. /*
  200. * Remove trailing spaces from the filename. GNU and BSD
  201. * variants both pad filename area out with spaces.
  202. * This will only be wrong if GNU/SVR4 'ar' implementations
  203. * omit trailing '/' for 16-char filenames and we have
  204. * a 16-char filename that ends in ' '.
  205. */
  206. p = filename + AR_name_size - 1;
  207. while (p >= filename && *p == ' ') {
  208. *p = '\0';
  209. p--;
  210. }
  211. /*
  212. * Remove trailing slash unless first character is '/'.
  213. * (BSD entries never end in '/', so this will only trim
  214. * GNU-format entries. GNU special entries start with '/'
  215. * and are not terminated in '/', so we don't trim anything
  216. * that starts with '/'.)
  217. */
  218. if (filename[0] != '/' && p > filename && *p == '/') {
  219. *p = '\0';
  220. }
  221. if (p < filename) {
  222. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  223. "Found entry with empty filename");
  224. return (ARCHIVE_FATAL);
  225. }
  226. /*
  227. * '//' is the GNU filename table.
  228. * Later entries can refer to names in this table.
  229. */
  230. if (strcmp(filename, "//") == 0) {
  231. /* This must come before any call to _read_ahead. */
  232. ar_parse_common_header(ar, entry, h);
  233. archive_entry_copy_pathname(entry, filename);
  234. archive_entry_set_filetype(entry, AE_IFREG);
  235. /* Get the size of the filename table. */
  236. number = ar_atol10(h + AR_size_offset, AR_size_size);
  237. if (number > SIZE_MAX || number > 1024 * 1024 * 1024) {
  238. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  239. "Filename table too large");
  240. return (ARCHIVE_FATAL);
  241. }
  242. entry_size = (size_t)number;
  243. if (entry_size == 0) {
  244. archive_set_error(&a->archive, EINVAL,
  245. "Invalid string table");
  246. return (ARCHIVE_FATAL);
  247. }
  248. if (ar->strtab != NULL) {
  249. archive_set_error(&a->archive, EINVAL,
  250. "More than one string tables exist");
  251. return (ARCHIVE_FATAL);
  252. }
  253. /* Read the filename table into memory. */
  254. st = malloc(entry_size);
  255. if (st == NULL) {
  256. archive_set_error(&a->archive, ENOMEM,
  257. "Can't allocate filename table buffer");
  258. return (ARCHIVE_FATAL);
  259. }
  260. ar->strtab = st;
  261. ar->strtab_size = entry_size;
  262. if (*unconsumed) {
  263. __archive_read_consume(a, *unconsumed);
  264. *unconsumed = 0;
  265. }
  266. if ((b = __archive_read_ahead(a, entry_size, NULL)) == NULL)
  267. return (ARCHIVE_FATAL);
  268. memcpy(st, b, entry_size);
  269. __archive_read_consume(a, entry_size);
  270. /* All contents are consumed. */
  271. ar->entry_bytes_remaining = 0;
  272. archive_entry_set_size(entry, ar->entry_bytes_remaining);
  273. /* Parse the filename table. */
  274. return (ar_parse_gnu_filename_table(a));
  275. }
  276. /*
  277. * GNU variant handles long filenames by storing /<number>
  278. * to indicate a name stored in the filename table.
  279. * XXX TODO: Verify that it's all digits... Don't be fooled
  280. * by "/9xyz" XXX
  281. */
  282. if (filename[0] == '/' && filename[1] >= '0' && filename[1] <= '9') {
  283. number = ar_atol10(h + AR_name_offset + 1, AR_name_size - 1);
  284. /*
  285. * If we can't look up the real name, warn and return
  286. * the entry with the wrong name.
  287. */
  288. if (ar->strtab == NULL || number >= ar->strtab_size) {
  289. archive_set_error(&a->archive, EINVAL,
  290. "Can't find long filename for GNU/SVR4 archive entry");
  291. archive_entry_copy_pathname(entry, filename);
  292. /* Parse the time, owner, mode, size fields. */
  293. ar_parse_common_header(ar, entry, h);
  294. return (ARCHIVE_FATAL);
  295. }
  296. archive_entry_copy_pathname(entry, &ar->strtab[(size_t)number]);
  297. /* Parse the time, owner, mode, size fields. */
  298. return (ar_parse_common_header(ar, entry, h));
  299. }
  300. /*
  301. * BSD handles long filenames by storing "#1/" followed by the
  302. * length of filename as a decimal number, then prepends the
  303. * the filename to the file contents.
  304. */
  305. if (strncmp(filename, "#1/", 3) == 0) {
  306. /* Parse the time, owner, mode, size fields. */
  307. /* This must occur before _read_ahead is called again. */
  308. ar_parse_common_header(ar, entry, h);
  309. /* Parse the size of the name, adjust the file size. */
  310. number = ar_atol10(h + AR_name_offset + 3, AR_name_size - 3);
  311. /* Sanity check the filename length:
  312. * = Must be <= SIZE_MAX - 1
  313. * = Must be <= 1MB
  314. * = Cannot be bigger than the entire entry
  315. */
  316. if (number > SIZE_MAX - 1
  317. || number > 1024 * 1024
  318. || (int64_t)number > ar->entry_bytes_remaining) {
  319. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  320. "Bad input file size");
  321. return (ARCHIVE_FATAL);
  322. }
  323. bsd_name_length = (size_t)number;
  324. ar->entry_bytes_remaining -= bsd_name_length;
  325. /* Adjust file size reported to client. */
  326. archive_entry_set_size(entry, ar->entry_bytes_remaining);
  327. if (*unconsumed) {
  328. __archive_read_consume(a, *unconsumed);
  329. *unconsumed = 0;
  330. }
  331. /* Read the long name into memory. */
  332. if ((b = __archive_read_ahead(a, bsd_name_length, NULL)) == NULL) {
  333. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  334. "Truncated input file");
  335. return (ARCHIVE_FATAL);
  336. }
  337. /* Store it in the entry. */
  338. p = (char *)malloc(bsd_name_length + 1);
  339. if (p == NULL) {
  340. archive_set_error(&a->archive, ENOMEM,
  341. "Can't allocate fname buffer");
  342. return (ARCHIVE_FATAL);
  343. }
  344. strncpy(p, b, bsd_name_length);
  345. p[bsd_name_length] = '\0';
  346. __archive_read_consume(a, bsd_name_length);
  347. archive_entry_copy_pathname(entry, p);
  348. free(p);
  349. return (ARCHIVE_OK);
  350. }
  351. /*
  352. * "/" is the SVR4/GNU archive symbol table.
  353. */
  354. if (strcmp(filename, "/") == 0) {
  355. archive_entry_copy_pathname(entry, "/");
  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_mtime(entry,
  413. (time_t)ar_atol10(h + AR_date_offset, AR_date_size), 0L);
  414. archive_entry_set_uid(entry,
  415. (uid_t)ar_atol10(h + AR_uid_offset, AR_uid_size));
  416. archive_entry_set_gid(entry,
  417. (gid_t)ar_atol10(h + AR_gid_offset, AR_gid_size));
  418. archive_entry_set_mode(entry,
  419. (mode_t)ar_atol8(h + AR_mode_offset, AR_mode_size));
  420. n = ar_atol10(h + AR_size_offset, AR_size_size);
  421. ar->entry_offset = 0;
  422. ar->entry_padding = n % 2;
  423. archive_entry_set_size(entry, n);
  424. ar->entry_bytes_remaining = n;
  425. return (ARCHIVE_OK);
  426. }
  427. static int
  428. archive_read_format_ar_read_data(struct archive_read *a,
  429. const void **buff, size_t *size, int64_t *offset)
  430. {
  431. ssize_t bytes_read;
  432. struct ar *ar;
  433. ar = (struct ar *)(a->format->data);
  434. if (ar->entry_bytes_unconsumed) {
  435. __archive_read_consume(a, ar->entry_bytes_unconsumed);
  436. ar->entry_bytes_unconsumed = 0;
  437. }
  438. if (ar->entry_bytes_remaining > 0) {
  439. *buff = __archive_read_ahead(a, 1, &bytes_read);
  440. if (bytes_read == 0) {
  441. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  442. "Truncated ar archive");
  443. return (ARCHIVE_FATAL);
  444. }
  445. if (bytes_read < 0)
  446. return (ARCHIVE_FATAL);
  447. if (bytes_read > ar->entry_bytes_remaining)
  448. bytes_read = (ssize_t)ar->entry_bytes_remaining;
  449. *size = bytes_read;
  450. ar->entry_bytes_unconsumed = bytes_read;
  451. *offset = ar->entry_offset;
  452. ar->entry_offset += bytes_read;
  453. ar->entry_bytes_remaining -= bytes_read;
  454. return (ARCHIVE_OK);
  455. } else {
  456. int64_t skipped = __archive_read_consume(a, ar->entry_padding);
  457. if (skipped >= 0) {
  458. ar->entry_padding -= skipped;
  459. }
  460. if (ar->entry_padding) {
  461. if (skipped >= 0) {
  462. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  463. "Truncated ar archive- failed consuming padding");
  464. }
  465. return (ARCHIVE_FATAL);
  466. }
  467. *buff = NULL;
  468. *size = 0;
  469. *offset = ar->entry_offset;
  470. return (ARCHIVE_EOF);
  471. }
  472. }
  473. static int
  474. archive_read_format_ar_skip(struct archive_read *a)
  475. {
  476. int64_t bytes_skipped;
  477. struct ar* ar;
  478. ar = (struct ar *)(a->format->data);
  479. bytes_skipped = __archive_read_consume(a,
  480. ar->entry_bytes_remaining + ar->entry_padding
  481. + ar->entry_bytes_unconsumed);
  482. if (bytes_skipped < 0)
  483. return (ARCHIVE_FATAL);
  484. ar->entry_bytes_remaining = 0;
  485. ar->entry_bytes_unconsumed = 0;
  486. ar->entry_padding = 0;
  487. return (ARCHIVE_OK);
  488. }
  489. static int
  490. ar_parse_gnu_filename_table(struct archive_read *a)
  491. {
  492. struct ar *ar;
  493. char *p;
  494. size_t size;
  495. ar = (struct ar*)(a->format->data);
  496. size = ar->strtab_size;
  497. for (p = ar->strtab; p < ar->strtab + size - 1; ++p) {
  498. if (*p == '/') {
  499. *p++ = '\0';
  500. if (*p != '\n')
  501. goto bad_string_table;
  502. *p = '\0';
  503. }
  504. }
  505. /*
  506. * GNU ar always pads the table to an even size.
  507. * The pad character is either '\n' or '`'.
  508. */
  509. if (p != ar->strtab + size && *p != '\n' && *p != '`')
  510. goto bad_string_table;
  511. /* Enforce zero termination. */
  512. ar->strtab[size - 1] = '\0';
  513. return (ARCHIVE_OK);
  514. bad_string_table:
  515. archive_set_error(&a->archive, EINVAL,
  516. "Invalid string table");
  517. free(ar->strtab);
  518. ar->strtab = NULL;
  519. return (ARCHIVE_FATAL);
  520. }
  521. static uint64_t
  522. ar_atol8(const char *p, unsigned char_cnt)
  523. {
  524. uint64_t l, limit, last_digit_limit;
  525. unsigned int digit, base;
  526. base = 8;
  527. limit = UINT64_MAX / base;
  528. last_digit_limit = UINT64_MAX % base;
  529. while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
  530. p++;
  531. l = 0;
  532. digit = *p - '0';
  533. while (*p >= '0' && digit < base && char_cnt-- > 0) {
  534. if (l>limit || (l == limit && digit > last_digit_limit)) {
  535. l = UINT64_MAX; /* Truncate on overflow. */
  536. break;
  537. }
  538. l = (l * base) + digit;
  539. digit = *++p - '0';
  540. }
  541. return (l);
  542. }
  543. static uint64_t
  544. ar_atol10(const char *p, unsigned char_cnt)
  545. {
  546. uint64_t l, limit, last_digit_limit;
  547. unsigned int base, digit;
  548. base = 10;
  549. limit = UINT64_MAX / base;
  550. last_digit_limit = UINT64_MAX % base;
  551. while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
  552. p++;
  553. l = 0;
  554. digit = *p - '0';
  555. while (*p >= '0' && digit < base && char_cnt-- > 0) {
  556. if (l > limit || (l == limit && digit > last_digit_limit)) {
  557. l = UINT64_MAX; /* Truncate on overflow. */
  558. break;
  559. }
  560. l = (l * base) + digit;
  561. digit = *++p - '0';
  562. }
  563. return (l);
  564. }