[63a045c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jiří Zárevúcky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <align.h>
|
---|
| 30 | #include <tar.h>
|
---|
| 31 |
|
---|
| 32 | static int _digit_val(char c)
|
---|
| 33 | {
|
---|
| 34 | if (c >= '0' && c <= '9') {
|
---|
| 35 | return c - '0';
|
---|
| 36 | }
|
---|
| 37 | if (c >= 'a' && c <= 'z') {
|
---|
| 38 | return c - 'a' + 10;
|
---|
| 39 | }
|
---|
| 40 | if (c >= 'A' && c <= 'Z') {
|
---|
| 41 | return c - 'A' + 10;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | // TODO: error message
|
---|
| 45 | return 0;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | static int64_t _parse_size(const char *s, size_t len, int base)
|
---|
| 49 | {
|
---|
| 50 | int64_t val = 0;
|
---|
| 51 |
|
---|
| 52 | for (size_t i = 0; i < len; i++) {
|
---|
| 53 | if (*s == 0)
|
---|
| 54 | return val;
|
---|
| 55 |
|
---|
| 56 | if (*s == ' ') {
|
---|
| 57 | s++;
|
---|
| 58 | continue;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | if ((INT64_MAX - 64) / base <= val) {
|
---|
| 62 | // TODO: error message
|
---|
| 63 | return INT64_MAX;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | val *= base;
|
---|
| 67 | val += _digit_val(*s);
|
---|
| 68 | s++;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | return val;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | bool tar_info(const uint8_t *start, const uint8_t *end,
|
---|
| 75 | const char **name, size_t *length)
|
---|
| 76 | {
|
---|
| 77 | if (end - start < TAR_BLOCK_SIZE) {
|
---|
| 78 | // TODO: error message
|
---|
| 79 | return false;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | const tar_header_raw_t *h = (const tar_header_raw_t *) start;
|
---|
| 83 | if (h->filename[0] == '\0')
|
---|
| 84 | return false;
|
---|
| 85 |
|
---|
| 86 | ptrdiff_t len = _parse_size(h->size, sizeof(h->size), 8);
|
---|
| 87 |
|
---|
| 88 | if (end - start < TAR_BLOCK_SIZE + len) {
|
---|
| 89 | // TODO: error message
|
---|
| 90 | return false;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | *name = h->filename;
|
---|
| 94 | *length = len;
|
---|
| 95 | return true;
|
---|
| 96 | }
|
---|