|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Bjorn Andersson <[email protected]> |
| 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 are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | + * may be used to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | +#define _FILE_OFFSET_BITS 64 |
| 32 | +#include <ctype.h> |
| 33 | +#include <endian.h> |
| 34 | +#include <errno.h> |
| 35 | +#include <inttypes.h> |
| 36 | +#include <stdbool.h> |
| 37 | +#include <stdint.h> |
| 38 | +#include <stdio.h> |
| 39 | +#include <string.h> |
| 40 | +#include <unistd.h> |
| 41 | + |
| 42 | +#include "sparse.h" |
| 43 | + |
| 44 | +int sparse_header_parse(int fd, sparse_header_t* sparse_header) |
| 45 | +{ |
| 46 | + lseek(fd, 0, SEEK_SET); |
| 47 | + |
| 48 | + if (read(fd, sparse_header, sizeof(sparse_header_t)) != sizeof(sparse_header_t)) { |
| 49 | + fprintf(stderr, "[SPARSE] Unable to read sparse header\n"); |
| 50 | + return -EINVAL; |
| 51 | + } |
| 52 | + |
| 53 | + if (htole32(sparse_header->magic) != htole32(SPARSE_HEADER_MAGIC)) { |
| 54 | + fprintf(stderr, "[SPARSE] Invalid magic in sparse header\n"); |
| 55 | + return -EINVAL; |
| 56 | + } |
| 57 | + |
| 58 | + if (htole16(sparse_header->major_version) != htole16(SPARSE_HEADER_MAJOR_VER)) { |
| 59 | + fprintf(stderr, "[SPARSE] Invalid major version in sparse header\n"); |
| 60 | + return -EINVAL; |
| 61 | + } |
| 62 | + |
| 63 | + if (htole16(sparse_header->minor_version) != htole16(SPARSE_HEADER_MINOR_VER)) { |
| 64 | + fprintf(stderr, "[SPARSE] Invalid minor version in sparse header\n"); |
| 65 | + return -EINVAL; |
| 66 | + } |
| 67 | + |
| 68 | + if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) |
| 69 | + lseek(fd, sparse_header->file_hdr_sz - sizeof(sparse_header_t), SEEK_CUR); |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +int sparse_chunk_header_parse(int fd, sparse_header_t* sparse_header, unsigned *chunk_size, unsigned *sparse_file_offset) |
| 75 | +{ |
| 76 | + chunk_header_t chunk_header; |
| 77 | + |
| 78 | + *chunk_size = 0; |
| 79 | + *sparse_file_offset = 0; |
| 80 | + |
| 81 | + if (read(fd, &chunk_header, sizeof(chunk_header_t)) != sizeof(chunk_header_t)) { |
| 82 | + fprintf(stderr, "[SPARSE] Unable to read sparse chunk header\n"); |
| 83 | + return -EINVAL; |
| 84 | + } |
| 85 | + |
| 86 | + if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) |
| 87 | + lseek(fd, sparse_header->chunk_hdr_sz - sizeof(chunk_header_t), SEEK_CUR); |
| 88 | + |
| 89 | + if (htole16(chunk_header.chunk_type) == htole16(CHUNK_TYPE_RAW)) { |
| 90 | + |
| 91 | + *chunk_size = chunk_header.chunk_sz * sparse_header->blk_sz; |
| 92 | + |
| 93 | + if (chunk_header.total_sz != (sparse_header->chunk_hdr_sz + *chunk_size)) { |
| 94 | + fprintf(stderr, "[SPARSE] Bogus chunk size, type Raw\n"); |
| 95 | + return -EINVAL; |
| 96 | + } |
| 97 | + |
| 98 | + *sparse_file_offset = lseek(fd, 0, SEEK_CUR); |
| 99 | + lseek(fd, *chunk_size, SEEK_CUR); |
| 100 | + } |
| 101 | + else if (htole16(chunk_header.chunk_type) == htole16(CHUNK_TYPE_DONT_CARE)) { |
| 102 | + |
| 103 | + *chunk_size = chunk_header.chunk_sz * sparse_header->blk_sz; |
| 104 | + if (chunk_header.total_sz != sparse_header->chunk_hdr_sz) { |
| 105 | + fprintf(stderr, "[SPARSE] Bogus chunk size, type Don't Care\n"); |
| 106 | + return -EINVAL; |
| 107 | + } |
| 108 | + } |
| 109 | + else if (htole16(chunk_header.chunk_type) == htole16(CHUNK_TYPE_FILL)) { |
| 110 | + fprintf(stderr, "[SPARSE] Fill chunk size doesn't support\n"); |
| 111 | + return -EINVAL; |
| 112 | + } |
| 113 | + else { |
| 114 | + fprintf(stderr, "[SPARSE] Unknown chunk\n"); |
| 115 | + return -EINVAL; |
| 116 | + } |
| 117 | + |
| 118 | + return 0; |
| 119 | +} |
0 commit comments