Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/cpio.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
| 19 | #include <stdint.h> |
| 20 | |
Andrew Walbran | 4a53ba6 | 2019-03-05 17:26:12 +0000 | [diff] [blame] | 21 | #include "hf/arch/std.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 22 | |
| 23 | #pragma pack(push, 1) |
| 24 | struct cpio_header { |
| 25 | uint16_t magic; |
| 26 | uint16_t dev; |
| 27 | uint16_t ino; |
| 28 | uint16_t mode; |
| 29 | uint16_t uid; |
| 30 | uint16_t gid; |
| 31 | uint16_t nlink; |
| 32 | uint16_t rdev; |
| 33 | uint16_t mtime[2]; |
| 34 | uint16_t namesize; |
| 35 | uint16_t filesize[2]; |
| 36 | }; |
| 37 | #pragma pack(pop) |
| 38 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 39 | /** |
| 40 | * Retrieves the next file stored in the cpio archive stored in the cpio, and |
| 41 | * advances the iterator such that another call to this function would return |
| 42 | * the following file. |
| 43 | */ |
| 44 | bool cpio_next(struct memiter *iter, const char **name, const void **contents, |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 45 | size_t *size) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 46 | { |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 47 | size_t len; |
| 48 | struct memiter lit = *iter; |
| 49 | const struct cpio_header *h = (const struct cpio_header *)lit.next; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 50 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 51 | if (!memiter_advance(&lit, sizeof(struct cpio_header))) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 52 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 53 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 54 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 55 | *name = lit.next; |
| 56 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 57 | /* TODO: Check magic. */ |
| 58 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 59 | len = (h->namesize + 1) & ~1; |
| 60 | if (!memiter_advance(&lit, len)) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 61 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 62 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 63 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 64 | *contents = lit.next; |
| 65 | |
| 66 | len = (size_t)h->filesize[0] << 16 | h->filesize[1]; |
| 67 | if (!memiter_advance(&lit, (len + 1) & ~1)) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 68 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 69 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 70 | |
| 71 | /* TODO: Check that string is null-terminated. */ |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 72 | |
| 73 | /* Stop enumerating files when we hit the end marker. */ |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 74 | if (!strcmp(*name, "TRAILER!!!")) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 75 | return false; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 76 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 77 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 78 | *size = len; |
| 79 | *iter = lit; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 80 | |
| 81 | return true; |
| 82 | } |