source: mainline/boot/generic/src/tar.c@ 39916d6

Last change on this file since 39916d6 was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2018 Jiří Zárevúcky
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <align.h>
8#include <tar.h>
9
10static int _digit_val(char c)
11{
12 if (c >= '0' && c <= '9') {
13 return c - '0';
14 }
15 if (c >= 'a' && c <= 'z') {
16 return c - 'a' + 10;
17 }
18 if (c >= 'A' && c <= 'Z') {
19 return c - 'A' + 10;
20 }
21
22 // TODO: error message
23 return 0;
24}
25
26static int64_t _parse_size(const char *s, size_t len, int base)
27{
28 int64_t val = 0;
29
30 for (size_t i = 0; i < len; i++) {
31 if (*s == 0)
32 return val;
33
34 if (*s == ' ') {
35 s++;
36 continue;
37 }
38
39 if ((INT64_MAX - 64) / base <= val) {
40 // TODO: error message
41 return INT64_MAX;
42 }
43
44 val *= base;
45 val += _digit_val(*s);
46 s++;
47 }
48
49 return val;
50}
51
52bool tar_info(const uint8_t *start, const uint8_t *end,
53 const char **name, size_t *length)
54{
55 if (end - start < TAR_BLOCK_SIZE) {
56 // TODO: error message
57 return false;
58 }
59
60 const tar_header_raw_t *h = (const tar_header_raw_t *) start;
61 if (h->filename[0] == '\0')
62 return false;
63
64 ptrdiff_t len = _parse_size(h->size, sizeof(h->size), 8);
65
66 if (end - start < TAR_BLOCK_SIZE + len) {
67 // TODO: error message
68 return false;
69 }
70
71 *name = h->filename;
72 *length = len;
73 return true;
74}
Note: See TracBrowser for help on using the repository browser.