Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 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 | * http://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 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 17 | #define _FILE_OFFSET_BITS 64 |
| 18 | #define _LARGEFILE64_SOURCE 1 |
| 19 | |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 20 | #include <algorithm> |
Daniel Micay | af090a6 | 2015-10-13 16:18:45 -0400 | [diff] [blame] | 21 | #include <inttypes.h> |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <stdarg.h> |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Xiaolei Yu | c39caaf | 2017-10-11 14:46:29 +0800 | [diff] [blame] | 27 | #include <string.h> |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 28 | #include <string> |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <sparse/sparse.h> |
| 32 | |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 33 | #include "android-base/stringprintf.h" |
Mark Salyzyn | 031a748 | 2014-02-27 16:56:15 -0800 | [diff] [blame] | 34 | #include "defs.h" |
| 35 | #include "output_file.h" |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 36 | #include "sparse_crc32.h" |
| 37 | #include "sparse_file.h" |
| 38 | #include "sparse_format.h" |
| 39 | |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 40 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 41 | #if defined(__APPLE__) && defined(__MACH__) |
| 42 | #define lseek64 lseek |
| 43 | #define off64_t off_t |
| 44 | #endif |
| 45 | |
| 46 | #define SPARSE_HEADER_MAJOR_VER 1 |
| 47 | #define SPARSE_HEADER_LEN (sizeof(sparse_header_t)) |
| 48 | #define CHUNK_HEADER_LEN (sizeof(chunk_header_t)) |
| 49 | |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 50 | static constexpr int64_t COPY_BUF_SIZE = 1024 * 1024; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 51 | static char *copybuf; |
| 52 | |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 53 | static std::string ErrorString(int err) |
| 54 | { |
| 55 | if (err == -EOVERFLOW) return "EOF while reading file"; |
| 56 | if (err == -EINVAL) return "Invalid sparse file format"; |
| 57 | if (err == -ENOMEM) return "Failed allocation while reading file"; |
| 58 | return android::base::StringPrintf("Unknown error %d", err); |
| 59 | } |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 60 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 61 | class SparseFileSource { |
| 62 | public: |
| 63 | /* Seeks the source ahead by the given offset. */ |
| 64 | virtual void Seek(int64_t offset) = 0; |
| 65 | |
| 66 | /* Return the current offset. */ |
| 67 | virtual int64_t GetOffset() = 0; |
| 68 | |
| 69 | /* Set the current offset. Return 0 if successful. */ |
| 70 | virtual int SetOffset(int64_t offset) = 0; |
| 71 | |
| 72 | /* Adds the given length from the current offset of the source to the file at the given |
| 73 | * block. Return 0 if successful. */ |
| 74 | virtual int AddToSparseFile(struct sparse_file *s, int64_t len, unsigned int block) = 0; |
| 75 | |
| 76 | /* Get data of fixed size from the current offset and seek len bytes. |
| 77 | * Return 0 if successful. */ |
| 78 | virtual int ReadValue(void *ptr, int len) = 0; |
| 79 | |
| 80 | /* Find the crc32 of the next len bytes and seek ahead len bytes. Return 0 if successful. */ |
| 81 | virtual int GetCrc32(uint32_t *crc32, int64_t len) = 0; |
| 82 | |
| 83 | virtual ~SparseFileSource() {}; |
| 84 | }; |
| 85 | |
| 86 | class SparseFileFdSource : public SparseFileSource { |
| 87 | private: |
| 88 | int fd; |
| 89 | public: |
| 90 | SparseFileFdSource(int fd) : fd(fd) {} |
| 91 | ~SparseFileFdSource() override {} |
| 92 | |
| 93 | void Seek(int64_t off) override { |
| 94 | lseek64(fd, off, SEEK_CUR); |
| 95 | } |
| 96 | |
| 97 | int64_t GetOffset() override { |
| 98 | return lseek64(fd, 0, SEEK_CUR); |
| 99 | } |
| 100 | |
| 101 | int SetOffset(int64_t offset) override { |
| 102 | return lseek64(fd, offset, SEEK_SET) == offset ? 0 : -errno; |
| 103 | } |
| 104 | |
| 105 | int AddToSparseFile(struct sparse_file *s, int64_t len, unsigned int block) override { |
| 106 | return sparse_file_add_fd(s, fd, GetOffset(), len, block); |
| 107 | } |
| 108 | |
| 109 | int ReadValue(void *ptr, int len) override { |
| 110 | return read_all(fd, ptr, len); |
| 111 | } |
| 112 | |
| 113 | int GetCrc32(uint32_t *crc32, int64_t len) override { |
| 114 | int chunk; |
| 115 | int ret; |
| 116 | while (len) { |
| 117 | chunk = std::min(len, COPY_BUF_SIZE); |
| 118 | ret = read_all(fd, copybuf, chunk); |
| 119 | if (ret < 0) { |
| 120 | return ret; |
| 121 | } |
| 122 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); |
| 123 | len -= chunk; |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | class SparseFileBufSource : public SparseFileSource { |
| 130 | private: |
| 131 | char *buf; |
| 132 | int64_t offset; |
| 133 | public: |
| 134 | SparseFileBufSource(char *buf) : buf(buf), offset(0) {} |
| 135 | ~SparseFileBufSource() override {} |
| 136 | |
| 137 | void Seek(int64_t off) override { |
| 138 | buf += off; |
| 139 | offset += off; |
| 140 | } |
| 141 | |
| 142 | int64_t GetOffset() override { |
| 143 | return offset; |
| 144 | } |
| 145 | |
| 146 | int SetOffset(int64_t off) override { |
| 147 | buf += off - offset; |
| 148 | offset = off; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int AddToSparseFile(struct sparse_file *s, int64_t len, unsigned int block) override { |
| 153 | return sparse_file_add_data(s, buf, len, block); |
| 154 | } |
| 155 | |
| 156 | int ReadValue(void *ptr, int len) override { |
| 157 | memcpy(ptr, buf, len); |
| 158 | Seek(len); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int GetCrc32(uint32_t *crc32, int64_t len) override { |
| 163 | *crc32 = sparse_crc32(*crc32, buf, len); |
| 164 | Seek(len); |
| 165 | return 0; |
| 166 | } |
| 167 | }; |
| 168 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 169 | static void verbose_error(bool verbose, int err, const char *fmt, ...) |
| 170 | { |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 171 | if (!verbose) return; |
| 172 | |
| 173 | std::string msg = ErrorString(err); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 174 | if (fmt) { |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 175 | msg += " at "; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 176 | va_list argp; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 177 | va_start(argp, fmt); |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 178 | android::base::StringAppendV(&msg, fmt, argp); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 179 | va_end(argp); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 180 | } |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 181 | sparse_print_verbose("%s\n", msg.c_str()); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size, |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 185 | SparseFileSource *source, unsigned int blocks, unsigned int block, |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 186 | uint32_t *crc32) |
| 187 | { |
| 188 | int ret; |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 189 | int64_t len = blocks * s->block_size; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 190 | |
| 191 | if (chunk_size % s->block_size != 0) { |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
| 195 | if (chunk_size / s->block_size != blocks) { |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 199 | ret = source->AddToSparseFile(s, len, block); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 200 | if (ret < 0) { |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | if (crc32) { |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 205 | ret = source->GetCrc32(crc32, len); |
| 206 | if (ret < 0) { |
| 207 | return ret; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 208 | } |
| 209 | } else { |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 210 | source->Seek(len); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int process_fill_chunk(struct sparse_file *s, unsigned int chunk_size, |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 217 | SparseFileSource *source, unsigned int blocks, unsigned int block, uint32_t *crc32) |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 218 | { |
| 219 | int ret; |
| 220 | int chunk; |
| 221 | int64_t len = (int64_t)blocks * s->block_size; |
| 222 | uint32_t fill_val; |
| 223 | uint32_t *fillbuf; |
| 224 | unsigned int i; |
| 225 | |
| 226 | if (chunk_size != sizeof(fill_val)) { |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 230 | ret = source->ReadValue(&fill_val, sizeof(fill_val)); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 231 | if (ret < 0) { |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | ret = sparse_file_add_fill(s, fill_val, len, block); |
| 236 | if (ret < 0) { |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | if (crc32) { |
| 241 | /* Fill copy_buf with the fill value */ |
| 242 | fillbuf = (uint32_t *)copybuf; |
| 243 | for (i = 0; i < (COPY_BUF_SIZE / sizeof(fill_val)); i++) { |
| 244 | fillbuf[i] = fill_val; |
| 245 | } |
| 246 | |
| 247 | while (len) { |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 248 | chunk = std::min(len, COPY_BUF_SIZE); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 249 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); |
| 250 | len -= chunk; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int process_skip_chunk(struct sparse_file *s, unsigned int chunk_size, |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 258 | SparseFileSource *source __unused, unsigned int blocks, |
Mark Salyzyn | 031a748 | 2014-02-27 16:56:15 -0800 | [diff] [blame] | 259 | unsigned int block __unused, uint32_t *crc32) |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 260 | { |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 261 | if (chunk_size != 0) { |
| 262 | return -EINVAL; |
| 263 | } |
| 264 | |
| 265 | if (crc32) { |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 266 | int64_t len = (int64_t)blocks * s->block_size; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 267 | memset(copybuf, 0, COPY_BUF_SIZE); |
| 268 | |
| 269 | while (len) { |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 270 | int chunk = std::min(len, COPY_BUF_SIZE); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 271 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); |
| 272 | len -= chunk; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 279 | static int process_crc32_chunk(SparseFileSource *source, unsigned int chunk_size, uint32_t *crc32) |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 280 | { |
| 281 | uint32_t file_crc32; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 282 | |
| 283 | if (chunk_size != sizeof(file_crc32)) { |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 287 | int ret = source->ReadValue(&file_crc32, sizeof(file_crc32)); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 288 | if (ret < 0) { |
| 289 | return ret; |
| 290 | } |
| 291 | |
Colin Cross | 1eb743b | 2016-02-01 11:15:30 -0800 | [diff] [blame] | 292 | if (crc32 != NULL && file_crc32 != *crc32) { |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 299 | static int process_chunk(struct sparse_file *s, SparseFileSource *source, |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 300 | unsigned int chunk_hdr_sz, chunk_header_t *chunk_header, |
| 301 | unsigned int cur_block, uint32_t *crc_ptr) |
| 302 | { |
| 303 | int ret; |
| 304 | unsigned int chunk_data_size; |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 305 | int64_t offset = source->GetOffset(); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 306 | |
| 307 | chunk_data_size = chunk_header->total_sz - chunk_hdr_sz; |
| 308 | |
| 309 | switch (chunk_header->chunk_type) { |
| 310 | case CHUNK_TYPE_RAW: |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 311 | ret = process_raw_chunk(s, chunk_data_size, source, |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 312 | chunk_header->chunk_sz, cur_block, crc_ptr); |
| 313 | if (ret < 0) { |
Daniel Micay | af090a6 | 2015-10-13 16:18:45 -0400 | [diff] [blame] | 314 | verbose_error(s->verbose, ret, "data block at %" PRId64, offset); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 315 | return ret; |
| 316 | } |
| 317 | return chunk_header->chunk_sz; |
| 318 | case CHUNK_TYPE_FILL: |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 319 | ret = process_fill_chunk(s, chunk_data_size, source, |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 320 | chunk_header->chunk_sz, cur_block, crc_ptr); |
| 321 | if (ret < 0) { |
Daniel Micay | af090a6 | 2015-10-13 16:18:45 -0400 | [diff] [blame] | 322 | verbose_error(s->verbose, ret, "fill block at %" PRId64, offset); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 323 | return ret; |
| 324 | } |
| 325 | return chunk_header->chunk_sz; |
| 326 | case CHUNK_TYPE_DONT_CARE: |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 327 | ret = process_skip_chunk(s, chunk_data_size, source, |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 328 | chunk_header->chunk_sz, cur_block, crc_ptr); |
| 329 | if (chunk_data_size != 0) { |
| 330 | if (ret < 0) { |
Daniel Micay | af090a6 | 2015-10-13 16:18:45 -0400 | [diff] [blame] | 331 | verbose_error(s->verbose, ret, "skip block at %" PRId64, offset); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 332 | return ret; |
| 333 | } |
| 334 | } |
| 335 | return chunk_header->chunk_sz; |
| 336 | case CHUNK_TYPE_CRC32: |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 337 | ret = process_crc32_chunk(source, chunk_data_size, crc_ptr); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 338 | if (ret < 0) { |
Daniel Micay | af090a6 | 2015-10-13 16:18:45 -0400 | [diff] [blame] | 339 | verbose_error(s->verbose, -EINVAL, "crc block at %" PRId64, |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 340 | offset); |
| 341 | return ret; |
| 342 | } |
| 343 | return 0; |
| 344 | default: |
Daniel Micay | af090a6 | 2015-10-13 16:18:45 -0400 | [diff] [blame] | 345 | verbose_error(s->verbose, -EINVAL, "unknown block %04X at %" PRId64, |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 346 | chunk_header->chunk_type, offset); |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 352 | static int sparse_file_read_sparse(struct sparse_file *s, SparseFileSource *source, bool crc) |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 353 | { |
| 354 | int ret; |
| 355 | unsigned int i; |
| 356 | sparse_header_t sparse_header; |
| 357 | chunk_header_t chunk_header; |
| 358 | uint32_t crc32 = 0; |
| 359 | uint32_t *crc_ptr = 0; |
| 360 | unsigned int cur_block = 0; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 361 | |
| 362 | if (!copybuf) { |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 363 | copybuf = (char *)malloc(COPY_BUF_SIZE); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | if (!copybuf) { |
| 367 | return -ENOMEM; |
| 368 | } |
| 369 | |
| 370 | if (crc) { |
| 371 | crc_ptr = &crc32; |
| 372 | } |
| 373 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 374 | ret = source->ReadValue(&sparse_header, sizeof(sparse_header)); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 375 | if (ret < 0) { |
| 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | if (sparse_header.magic != SPARSE_HEADER_MAGIC) { |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) { |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | |
| 387 | if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) { |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | if (sparse_header.chunk_hdr_sz < sizeof(chunk_header)) { |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
| 395 | if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) { |
| 396 | /* Skip the remaining bytes in a header that is longer than |
| 397 | * we expected. |
| 398 | */ |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 399 | source->Seek(sparse_header.file_hdr_sz - SPARSE_HEADER_LEN); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | for (i = 0; i < sparse_header.total_chunks; i++) { |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 403 | ret = source->ReadValue(&chunk_header, sizeof(chunk_header)); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 404 | if (ret < 0) { |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) { |
| 409 | /* Skip the remaining bytes in a header that is longer than |
| 410 | * we expected. |
| 411 | */ |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 412 | source->Seek(sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 415 | ret = process_chunk(s, source, sparse_header.chunk_hdr_sz, &chunk_header, |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 416 | cur_block, crc_ptr); |
| 417 | if (ret < 0) { |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | cur_block += ret; |
| 422 | } |
| 423 | |
| 424 | if (sparse_header.total_blks != cur_block) { |
| 425 | return -EINVAL; |
| 426 | } |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int sparse_file_read_normal(struct sparse_file *s, int fd) |
| 432 | { |
| 433 | int ret; |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 434 | uint32_t *buf = (uint32_t *)malloc(s->block_size); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 435 | unsigned int block = 0; |
| 436 | int64_t remain = s->len; |
| 437 | int64_t offset = 0; |
| 438 | unsigned int to_read; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 439 | unsigned int i; |
| 440 | bool sparse_block; |
| 441 | |
| 442 | if (!buf) { |
| 443 | return -ENOMEM; |
| 444 | } |
| 445 | |
| 446 | while (remain > 0) { |
Chris Fries | a7eeb22 | 2017-04-17 21:53:16 -0500 | [diff] [blame] | 447 | to_read = std::min(remain, (int64_t)(s->block_size)); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 448 | ret = read_all(fd, buf, to_read); |
| 449 | if (ret < 0) { |
| 450 | error("failed to read sparse file"); |
Colin Cross | 1eb743b | 2016-02-01 11:15:30 -0800 | [diff] [blame] | 451 | free(buf); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | if (to_read == s->block_size) { |
| 456 | sparse_block = true; |
| 457 | for (i = 1; i < s->block_size / sizeof(uint32_t); i++) { |
| 458 | if (buf[0] != buf[i]) { |
| 459 | sparse_block = false; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | } else { |
| 464 | sparse_block = false; |
| 465 | } |
| 466 | |
| 467 | if (sparse_block) { |
| 468 | /* TODO: add flag to use skip instead of fill for buf[0] == 0 */ |
| 469 | sparse_file_add_fill(s, buf[0], to_read, block); |
| 470 | } else { |
| 471 | sparse_file_add_fd(s, fd, offset, to_read, block); |
| 472 | } |
| 473 | |
| 474 | remain -= to_read; |
| 475 | offset += to_read; |
| 476 | block++; |
| 477 | } |
| 478 | |
Colin Cross | 1eb743b | 2016-02-01 11:15:30 -0800 | [diff] [blame] | 479 | free(buf); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | int sparse_file_read(struct sparse_file *s, int fd, bool sparse, bool crc) |
| 484 | { |
| 485 | if (crc && !sparse) { |
| 486 | return -EINVAL; |
| 487 | } |
| 488 | |
| 489 | if (sparse) { |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 490 | SparseFileFdSource source(fd); |
| 491 | return sparse_file_read_sparse(s, &source, crc); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 492 | } else { |
| 493 | return sparse_file_read_normal(s, fd); |
| 494 | } |
| 495 | } |
| 496 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 497 | int sparse_file_read_buf(struct sparse_file *s, char *buf, bool crc) |
| 498 | { |
| 499 | SparseFileBufSource source(buf); |
| 500 | return sparse_file_read_sparse(s, &source, crc); |
| 501 | } |
| 502 | |
| 503 | static struct sparse_file *sparse_file_import_source(SparseFileSource *source, bool verbose, bool crc) |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 504 | { |
| 505 | int ret; |
| 506 | sparse_header_t sparse_header; |
| 507 | int64_t len; |
| 508 | struct sparse_file *s; |
| 509 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 510 | ret = source->ReadValue(&sparse_header, sizeof(sparse_header)); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 511 | if (ret < 0) { |
| 512 | verbose_error(verbose, ret, "header"); |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | if (sparse_header.magic != SPARSE_HEADER_MAGIC) { |
| 517 | verbose_error(verbose, -EINVAL, "header magic"); |
| 518 | return NULL; |
| 519 | } |
| 520 | |
| 521 | if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) { |
| 522 | verbose_error(verbose, -EINVAL, "header major version"); |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) { |
| 527 | return NULL; |
| 528 | } |
| 529 | |
| 530 | if (sparse_header.chunk_hdr_sz < sizeof(chunk_header_t)) { |
| 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | len = (int64_t)sparse_header.total_blks * sparse_header.blk_sz; |
| 535 | s = sparse_file_new(sparse_header.blk_sz, len); |
| 536 | if (!s) { |
| 537 | verbose_error(verbose, -EINVAL, NULL); |
| 538 | return NULL; |
| 539 | } |
| 540 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 541 | ret = source->SetOffset(0); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 542 | if (ret < 0) { |
| 543 | verbose_error(verbose, ret, "seeking"); |
| 544 | sparse_file_destroy(s); |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | s->verbose = verbose; |
| 549 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 550 | ret = sparse_file_read_sparse(s, source, crc); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 551 | if (ret < 0) { |
| 552 | sparse_file_destroy(s); |
| 553 | return NULL; |
| 554 | } |
| 555 | |
| 556 | return s; |
| 557 | } |
| 558 | |
Jerry Zhang | 50e6029 | 2018-06-05 11:44:52 -0700 | [diff] [blame] | 559 | struct sparse_file *sparse_file_import(int fd, bool verbose, bool crc) { |
| 560 | SparseFileFdSource source(fd); |
| 561 | return sparse_file_import_source(&source, verbose, crc); |
| 562 | } |
| 563 | |
| 564 | struct sparse_file *sparse_file_import_buf(char* buf, bool verbose, bool crc) { |
| 565 | SparseFileBufSource source(buf); |
| 566 | return sparse_file_import_source(&source, verbose, crc); |
| 567 | } |
| 568 | |
Mohamad Ayyash | 80cc1f6 | 2015-03-31 12:09:29 -0700 | [diff] [blame] | 569 | struct sparse_file *sparse_file_import_auto(int fd, bool crc, bool verbose) |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 570 | { |
| 571 | struct sparse_file *s; |
| 572 | int64_t len; |
| 573 | int ret; |
| 574 | |
Mohamad Ayyash | 80cc1f6 | 2015-03-31 12:09:29 -0700 | [diff] [blame] | 575 | s = sparse_file_import(fd, verbose, crc); |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 576 | if (s) { |
| 577 | return s; |
| 578 | } |
| 579 | |
| 580 | len = lseek64(fd, 0, SEEK_END); |
| 581 | if (len < 0) { |
| 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | lseek64(fd, 0, SEEK_SET); |
| 586 | |
| 587 | s = sparse_file_new(4096, len); |
| 588 | if (!s) { |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | ret = sparse_file_read_normal(s, fd); |
| 593 | if (ret < 0) { |
| 594 | sparse_file_destroy(s); |
| 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | return s; |
| 599 | } |