blob: b883c13f2a2bf9ca87d703e183a63458dabf799c [file] [log] [blame]
Colin Cross28fa5bc2012-05-20 13:28:05 -07001/*
2 * Copyright (C) 2010 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
17#define _FILE_OFFSET_BITS 64
18#define _LARGEFILE64_SOURCE 1
19
cfig946da7c2019-08-15 10:07:07 +080020#include <algorithm>
Colin Cross28fa5bc2012-05-20 13:28:05 -070021#include <fcntl.h>
Elliott Hughesccecf142014-01-16 10:53:11 -080022#include <inttypes.h>
Colin Cross1e17b312012-05-21 16:35:45 -070023#include <limits.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070024#include <stdbool.h>
Colin Crossb4cd2672012-05-18 14:49:50 -070025#include <stddef.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070026#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <unistd.h>
31#include <zlib.h>
32
Mark Salyzyn031a7482014-02-27 16:56:15 -080033#include "defs.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070034#include "output_file.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070035#include "sparse_crc32.h"
Mark Salyzyn8116c8c2014-05-01 09:15:02 -070036#include "sparse_format.h"
Colin Cross28fa5bc2012-05-20 13:28:05 -070037
Elliott Hughesc44f50c2020-05-14 16:52:18 -070038#include <android-base/mapped_file.h>
39
Elliott Hughes34a4f0b2016-10-05 09:37:18 -070040#ifndef _WIN32
Colin Cross28fa5bc2012-05-20 13:28:05 -070041#define O_BINARY 0
Colin Crossb4cd2672012-05-18 14:49:50 -070042#else
43#define ftruncate64 ftruncate
Colin Cross28fa5bc2012-05-20 13:28:05 -070044#endif
45
46#if defined(__APPLE__) && defined(__MACH__)
47#define lseek64 lseek
48#define ftruncate64 ftruncate
Colin Cross28fa5bc2012-05-20 13:28:05 -070049#define off64_t off_t
50#endif
51
Colin Cross28fa5bc2012-05-20 13:28:05 -070052#define SPARSE_HEADER_MAJOR_VER 1
53#define SPARSE_HEADER_MINOR_VER 0
Jerry Zhang7b444f02018-06-12 16:42:09 -070054#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
Colin Cross28fa5bc2012-05-20 13:28:05 -070055#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
56
Jerry Zhang7b444f02018-06-12 16:42:09 -070057#define container_of(inner, outer_t, elem) ((outer_t*)((char*)(inner)-offsetof(outer_t, elem)))
Colin Crossb4cd2672012-05-18 14:49:50 -070058
Colin Cross28fa5bc2012-05-20 13:28:05 -070059struct output_file_ops {
Jerry Zhang7b444f02018-06-12 16:42:09 -070060 int (*open)(struct output_file*, int fd);
61 int (*skip)(struct output_file*, int64_t);
62 int (*pad)(struct output_file*, int64_t);
63 int (*write)(struct output_file*, void*, size_t);
64 void (*close)(struct output_file*);
Colin Cross28fa5bc2012-05-20 13:28:05 -070065};
66
Colin Crossb55dcee2012-04-24 23:07:49 -070067struct sparse_file_ops {
Jerry Zhang7b444f02018-06-12 16:42:09 -070068 int (*write_data_chunk)(struct output_file* out, unsigned int len, void* data);
69 int (*write_fill_chunk)(struct output_file* out, unsigned int len, uint32_t fill_val);
70 int (*write_skip_chunk)(struct output_file* out, int64_t len);
71 int (*write_end_chunk)(struct output_file* out);
Colin Crossb55dcee2012-04-24 23:07:49 -070072};
73
Colin Cross28fa5bc2012-05-20 13:28:05 -070074struct output_file {
Jerry Zhang7b444f02018-06-12 16:42:09 -070075 int64_t cur_out_ptr;
76 unsigned int chunk_cnt;
77 uint32_t crc32;
78 struct output_file_ops* ops;
79 struct sparse_file_ops* sparse_ops;
80 int use_crc;
81 unsigned int block_size;
82 int64_t len;
83 char* zero_buf;
84 uint32_t* fill_buf;
85 char* buf;
Colin Cross28fa5bc2012-05-20 13:28:05 -070086};
87
Colin Crossb4cd2672012-05-18 14:49:50 -070088struct output_file_gz {
Jerry Zhang7b444f02018-06-12 16:42:09 -070089 struct output_file out;
90 gzFile gz_fd;
Colin Crossb4cd2672012-05-18 14:49:50 -070091};
92
Jerry Zhang7b444f02018-06-12 16:42:09 -070093#define to_output_file_gz(_o) container_of((_o), struct output_file_gz, out)
Colin Crossb4cd2672012-05-18 14:49:50 -070094
95struct output_file_normal {
Jerry Zhang7b444f02018-06-12 16:42:09 -070096 struct output_file out;
97 int fd;
Colin Crossb4cd2672012-05-18 14:49:50 -070098};
99
Jerry Zhang7b444f02018-06-12 16:42:09 -0700100#define to_output_file_normal(_o) container_of((_o), struct output_file_normal, out)
Colin Crossb4cd2672012-05-18 14:49:50 -0700101
Colin Cross1e17b312012-05-21 16:35:45 -0700102struct output_file_callback {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700103 struct output_file out;
104 void* priv;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000105 int (*write)(void* priv, const void* buf, size_t len);
Colin Cross1e17b312012-05-21 16:35:45 -0700106};
107
Jerry Zhang7b444f02018-06-12 16:42:09 -0700108#define to_output_file_callback(_o) container_of((_o), struct output_file_callback, out)
Colin Cross1e17b312012-05-21 16:35:45 -0700109
Jerry Zhang7b444f02018-06-12 16:42:09 -0700110static int file_open(struct output_file* out, int fd) {
111 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700112
Jerry Zhang7b444f02018-06-12 16:42:09 -0700113 outn->fd = fd;
114 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700115}
116
Jerry Zhang7b444f02018-06-12 16:42:09 -0700117static int file_skip(struct output_file* out, int64_t cnt) {
118 off64_t ret;
119 struct output_file_normal* outn = to_output_file_normal(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700120
Jerry Zhang7b444f02018-06-12 16:42:09 -0700121 ret = lseek64(outn->fd, cnt, SEEK_CUR);
122 if (ret < 0) {
123 error_errno("lseek64");
124 return -1;
125 }
126 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700127}
128
Jerry Zhang7b444f02018-06-12 16:42:09 -0700129static int file_pad(struct output_file* out, int64_t len) {
130 int ret;
131 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700132
Jerry Zhang7b444f02018-06-12 16:42:09 -0700133 ret = ftruncate64(outn->fd, len);
134 if (ret < 0) {
135 return -errno;
136 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700137
Jerry Zhang7b444f02018-06-12 16:42:09 -0700138 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700139}
140
Jerry Zhang7b444f02018-06-12 16:42:09 -0700141static int file_write(struct output_file* out, void* data, size_t len) {
142 ssize_t ret;
143 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700144
Jerry Zhang7b444f02018-06-12 16:42:09 -0700145 while (len > 0) {
146 ret = write(outn->fd, data, len);
147 if (ret < 0) {
148 if (errno == EINTR) {
149 continue;
150 }
151 error_errno("write");
152 return -1;
153 }
Jeremy Compostellafca594c2016-09-30 14:54:25 +0200154
Jerry Zhang7b444f02018-06-12 16:42:09 -0700155 data = (char*)data + ret;
156 len -= ret;
157 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700158
Jerry Zhang7b444f02018-06-12 16:42:09 -0700159 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700160}
161
Jerry Zhang7b444f02018-06-12 16:42:09 -0700162static void file_close(struct output_file* out) {
163 struct output_file_normal* outn = to_output_file_normal(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700164
Jerry Zhang7b444f02018-06-12 16:42:09 -0700165 free(outn);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700166}
167
Colin Cross28fa5bc2012-05-20 13:28:05 -0700168static struct output_file_ops file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700169 .open = file_open,
170 .skip = file_skip,
171 .pad = file_pad,
172 .write = file_write,
173 .close = file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700174};
175
Jerry Zhang7b444f02018-06-12 16:42:09 -0700176static int gz_file_open(struct output_file* out, int fd) {
177 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700178
Jerry Zhang7b444f02018-06-12 16:42:09 -0700179 outgz->gz_fd = gzdopen(fd, "wb9");
180 if (!outgz->gz_fd) {
181 error_errno("gzopen");
182 return -errno;
183 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700184
Jerry Zhang7b444f02018-06-12 16:42:09 -0700185 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700186}
187
Jerry Zhang7b444f02018-06-12 16:42:09 -0700188static int gz_file_skip(struct output_file* out, int64_t cnt) {
189 off64_t ret;
190 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700191
Jerry Zhang7b444f02018-06-12 16:42:09 -0700192 ret = gzseek(outgz->gz_fd, cnt, SEEK_CUR);
193 if (ret < 0) {
194 error_errno("gzseek");
195 return -1;
196 }
197 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700198}
199
Jerry Zhang7b444f02018-06-12 16:42:09 -0700200static int gz_file_pad(struct output_file* out, int64_t len) {
201 off64_t ret;
202 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700203
Jerry Zhang7b444f02018-06-12 16:42:09 -0700204 ret = gztell(outgz->gz_fd);
205 if (ret < 0) {
206 return -1;
207 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700208
Jerry Zhang7b444f02018-06-12 16:42:09 -0700209 if (ret >= len) {
210 return 0;
211 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700212
Jerry Zhang7b444f02018-06-12 16:42:09 -0700213 ret = gzseek(outgz->gz_fd, len - 1, SEEK_SET);
214 if (ret < 0) {
215 return -1;
216 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700217
Jerry Zhang7b444f02018-06-12 16:42:09 -0700218 gzwrite(outgz->gz_fd, "", 1);
Colin Crossb4cd2672012-05-18 14:49:50 -0700219
Jerry Zhang7b444f02018-06-12 16:42:09 -0700220 return 0;
Colin Crossb4cd2672012-05-18 14:49:50 -0700221}
222
Jerry Zhang7b444f02018-06-12 16:42:09 -0700223static int gz_file_write(struct output_file* out, void* data, size_t len) {
224 int ret;
225 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700226
Jerry Zhang7b444f02018-06-12 16:42:09 -0700227 while (len > 0) {
cfig946da7c2019-08-15 10:07:07 +0800228 ret = gzwrite(outgz->gz_fd, data, std::min<unsigned int>(len, (unsigned int)INT_MAX));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700229 if (ret == 0) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700230 error("gzwrite %s", gzerror(outgz->gz_fd, nullptr));
Jerry Zhang7b444f02018-06-12 16:42:09 -0700231 return -1;
232 }
233 len -= ret;
234 data = (char*)data + ret;
235 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700236
Jerry Zhang7b444f02018-06-12 16:42:09 -0700237 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700238}
239
Jerry Zhang7b444f02018-06-12 16:42:09 -0700240static void gz_file_close(struct output_file* out) {
241 struct output_file_gz* outgz = to_output_file_gz(out);
Colin Crossb4cd2672012-05-18 14:49:50 -0700242
Jerry Zhang7b444f02018-06-12 16:42:09 -0700243 gzclose(outgz->gz_fd);
244 free(outgz);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700245}
246
247static struct output_file_ops gz_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700248 .open = gz_file_open,
249 .skip = gz_file_skip,
250 .pad = gz_file_pad,
251 .write = gz_file_write,
252 .close = gz_file_close,
Colin Cross28fa5bc2012-05-20 13:28:05 -0700253};
254
Jerry Zhang7b444f02018-06-12 16:42:09 -0700255static int callback_file_open(struct output_file* out __unused, int fd __unused) {
256 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700257}
258
Jerry Zhang7b444f02018-06-12 16:42:09 -0700259static int callback_file_skip(struct output_file* out, int64_t off) {
260 struct output_file_callback* outc = to_output_file_callback(out);
261 int to_write;
262 int ret;
Colin Cross1e17b312012-05-21 16:35:45 -0700263
Jerry Zhang7b444f02018-06-12 16:42:09 -0700264 while (off > 0) {
cfig946da7c2019-08-15 10:07:07 +0800265 to_write = std::min(off, (int64_t)INT_MAX);
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000266 ret = outc->write(outc->priv, nullptr, to_write);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700267 if (ret < 0) {
268 return ret;
269 }
270 off -= to_write;
271 }
Colin Cross1e17b312012-05-21 16:35:45 -0700272
Jerry Zhang7b444f02018-06-12 16:42:09 -0700273 return 0;
Colin Cross1e17b312012-05-21 16:35:45 -0700274}
275
Jerry Zhang7b444f02018-06-12 16:42:09 -0700276static int callback_file_pad(struct output_file* out __unused, int64_t len __unused) {
277 return -1;
Colin Cross1e17b312012-05-21 16:35:45 -0700278}
279
Jerry Zhang7b444f02018-06-12 16:42:09 -0700280static int callback_file_write(struct output_file* out, void* data, size_t len) {
281 struct output_file_callback* outc = to_output_file_callback(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700282
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000283 return outc->write(outc->priv, data, len);
Colin Cross1e17b312012-05-21 16:35:45 -0700284}
285
Jerry Zhang7b444f02018-06-12 16:42:09 -0700286static void callback_file_close(struct output_file* out) {
287 struct output_file_callback* outc = to_output_file_callback(out);
Colin Cross1e17b312012-05-21 16:35:45 -0700288
Jerry Zhang7b444f02018-06-12 16:42:09 -0700289 free(outc);
Colin Cross1e17b312012-05-21 16:35:45 -0700290}
291
292static struct output_file_ops callback_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700293 .open = callback_file_open,
294 .skip = callback_file_skip,
295 .pad = callback_file_pad,
296 .write = callback_file_write,
297 .close = callback_file_close,
Colin Cross1e17b312012-05-21 16:35:45 -0700298};
299
Jerry Zhang7b444f02018-06-12 16:42:09 -0700300int read_all(int fd, void* buf, size_t len) {
301 size_t total = 0;
302 int ret;
303 char* ptr = reinterpret_cast<char*>(buf);
Colin Cross13a56062012-06-19 16:45:48 -0700304
Jerry Zhang7b444f02018-06-12 16:42:09 -0700305 while (total < len) {
306 ret = read(fd, ptr, len - total);
Colin Cross13a56062012-06-19 16:45:48 -0700307
Jerry Zhang7b444f02018-06-12 16:42:09 -0700308 if (ret < 0) return -errno;
Colin Cross13a56062012-06-19 16:45:48 -0700309
Jerry Zhang7b444f02018-06-12 16:42:09 -0700310 if (ret == 0) return -EINVAL;
Colin Cross13a56062012-06-19 16:45:48 -0700311
Jerry Zhang7b444f02018-06-12 16:42:09 -0700312 ptr += ret;
313 total += ret;
314 }
Colin Cross13a56062012-06-19 16:45:48 -0700315
Jerry Zhang7b444f02018-06-12 16:42:09 -0700316 return 0;
Colin Cross13a56062012-06-19 16:45:48 -0700317}
318
Jerry Zhang7b444f02018-06-12 16:42:09 -0700319static int write_sparse_skip_chunk(struct output_file* out, int64_t skip_len) {
320 chunk_header_t chunk_header;
321 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700322
Jerry Zhang7b444f02018-06-12 16:42:09 -0700323 if (skip_len % out->block_size) {
324 error("don't care size %" PRIi64 " is not a multiple of the block size %u", skip_len,
325 out->block_size);
326 return -1;
327 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700328
Jerry Zhang7b444f02018-06-12 16:42:09 -0700329 /* We are skipping data, so emit a don't care chunk. */
330 chunk_header.chunk_type = CHUNK_TYPE_DONT_CARE;
331 chunk_header.reserved1 = 0;
332 chunk_header.chunk_sz = skip_len / out->block_size;
333 chunk_header.total_sz = CHUNK_HEADER_LEN;
334 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
335 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700336
Jerry Zhang7b444f02018-06-12 16:42:09 -0700337 out->cur_out_ptr += skip_len;
338 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700339
Jerry Zhang7b444f02018-06-12 16:42:09 -0700340 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700341}
342
Jerry Zhang7b444f02018-06-12 16:42:09 -0700343static int write_sparse_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
344 chunk_header_t chunk_header;
345 int rnd_up_len, count;
346 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700347
Jerry Zhang7b444f02018-06-12 16:42:09 -0700348 /* Round up the fill length to a multiple of the block size */
349 rnd_up_len = ALIGN(len, out->block_size);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700350
Jerry Zhang7b444f02018-06-12 16:42:09 -0700351 /* Finally we can safely emit a chunk of data */
352 chunk_header.chunk_type = CHUNK_TYPE_FILL;
353 chunk_header.reserved1 = 0;
354 chunk_header.chunk_sz = rnd_up_len / out->block_size;
355 chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
356 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700357
Jerry Zhang7b444f02018-06-12 16:42:09 -0700358 if (ret < 0) return -1;
359 ret = out->ops->write(out, &fill_val, sizeof(fill_val));
360 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700361
Jerry Zhang7b444f02018-06-12 16:42:09 -0700362 if (out->use_crc) {
363 count = out->block_size / sizeof(uint32_t);
364 while (count--) out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
365 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700366
Jerry Zhang7b444f02018-06-12 16:42:09 -0700367 out->cur_out_ptr += rnd_up_len;
368 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700369
Jerry Zhang7b444f02018-06-12 16:42:09 -0700370 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700371}
372
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000373static int write_sparse_data_chunk(struct output_file* out, unsigned int len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700374 chunk_header_t chunk_header;
375 int rnd_up_len, zero_len;
376 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700377
Jerry Zhang7b444f02018-06-12 16:42:09 -0700378 /* Round up the data length to a multiple of the block size */
379 rnd_up_len = ALIGN(len, out->block_size);
380 zero_len = rnd_up_len - len;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700381
Jerry Zhang7b444f02018-06-12 16:42:09 -0700382 /* Finally we can safely emit a chunk of data */
383 chunk_header.chunk_type = CHUNK_TYPE_RAW;
384 chunk_header.reserved1 = 0;
385 chunk_header.chunk_sz = rnd_up_len / out->block_size;
386 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
387 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700388
Jerry Zhang7b444f02018-06-12 16:42:09 -0700389 if (ret < 0) return -1;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000390 ret = out->ops->write(out, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700391 if (ret < 0) return -1;
392 if (zero_len) {
393 ret = out->ops->write(out, out->zero_buf, zero_len);
394 if (ret < 0) return -1;
395 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700396
Jerry Zhang7b444f02018-06-12 16:42:09 -0700397 if (out->use_crc) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000398 out->crc32 = sparse_crc32(out->crc32, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700399 if (zero_len) out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len);
400 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700401
Jerry Zhang7b444f02018-06-12 16:42:09 -0700402 out->cur_out_ptr += rnd_up_len;
403 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700404
Jerry Zhang7b444f02018-06-12 16:42:09 -0700405 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700406}
407
Jerry Zhang7b444f02018-06-12 16:42:09 -0700408int write_sparse_end_chunk(struct output_file* out) {
409 chunk_header_t chunk_header;
410 int ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700411
Jerry Zhang7b444f02018-06-12 16:42:09 -0700412 if (out->use_crc) {
413 chunk_header.chunk_type = CHUNK_TYPE_CRC32;
414 chunk_header.reserved1 = 0;
415 chunk_header.chunk_sz = 0;
416 chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
Colin Crossb55dcee2012-04-24 23:07:49 -0700417
Jerry Zhang7b444f02018-06-12 16:42:09 -0700418 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
419 if (ret < 0) {
420 return ret;
421 }
422 out->ops->write(out, &out->crc32, 4);
423 if (ret < 0) {
424 return ret;
425 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700426
Jerry Zhang7b444f02018-06-12 16:42:09 -0700427 out->chunk_cnt++;
428 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700429
Jerry Zhang7b444f02018-06-12 16:42:09 -0700430 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700431}
432
433static struct sparse_file_ops sparse_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700434 .write_data_chunk = write_sparse_data_chunk,
435 .write_fill_chunk = write_sparse_fill_chunk,
436 .write_skip_chunk = write_sparse_skip_chunk,
437 .write_end_chunk = write_sparse_end_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700438};
439
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000440static int write_normal_data_chunk(struct output_file* out, unsigned int len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700441 int ret;
442 unsigned int rnd_up_len = ALIGN(len, out->block_size);
Colin Crossb55dcee2012-04-24 23:07:49 -0700443
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000444 ret = out->ops->write(out, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700445 if (ret < 0) {
446 return ret;
447 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700448
Jerry Zhang7b444f02018-06-12 16:42:09 -0700449 if (rnd_up_len > len) {
450 ret = out->ops->skip(out, rnd_up_len - len);
451 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700452
Jerry Zhang7b444f02018-06-12 16:42:09 -0700453 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700454}
455
Jerry Zhang7b444f02018-06-12 16:42:09 -0700456static int write_normal_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000457 int ret;
458 unsigned int i;
459 unsigned int write_len;
460
461 /* Initialize fill_buf with the fill_val */
462 for (i = 0; i < out->block_size / sizeof(uint32_t); i++) {
463 out->fill_buf[i] = fill_val;
464 }
465
466 while (len) {
cfig946da7c2019-08-15 10:07:07 +0800467 write_len = std::min(len, out->block_size);
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000468 ret = out->ops->write(out, out->fill_buf, write_len);
469 if (ret < 0) {
470 return ret;
471 }
472
473 len -= write_len;
474 }
475
476 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700477}
478
Jerry Zhang7b444f02018-06-12 16:42:09 -0700479static int write_normal_skip_chunk(struct output_file* out, int64_t len) {
480 return out->ops->skip(out, len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700481}
482
Jerry Zhang7b444f02018-06-12 16:42:09 -0700483int write_normal_end_chunk(struct output_file* out) {
484 return out->ops->pad(out, out->len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700485}
486
487static struct sparse_file_ops normal_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700488 .write_data_chunk = write_normal_data_chunk,
489 .write_fill_chunk = write_normal_fill_chunk,
490 .write_skip_chunk = write_normal_skip_chunk,
491 .write_end_chunk = write_normal_end_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700492};
493
Jerry Zhang7b444f02018-06-12 16:42:09 -0700494void output_file_close(struct output_file* out) {
495 out->sparse_ops->write_end_chunk(out);
Robin Hsu48c9f612019-11-07 13:44:08 +0800496 free(out->zero_buf);
497 free(out->fill_buf);
498 out->zero_buf = nullptr;
499 out->fill_buf = nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700500 out->ops->close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700501}
502
Jerry Zhang7b444f02018-06-12 16:42:09 -0700503static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
504 int chunks, bool crc) {
505 int ret;
Colin Crossb4cd2672012-05-18 14:49:50 -0700506
Jerry Zhang7b444f02018-06-12 16:42:09 -0700507 out->len = len;
508 out->block_size = block_size;
Chih-Hung Hsieh5d08f632018-12-11 10:38:27 -0800509 out->cur_out_ptr = 0LL;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700510 out->chunk_cnt = 0;
511 out->crc32 = 0;
512 out->use_crc = crc;
Colin Crossb4cd2672012-05-18 14:49:50 -0700513
Jerry Zhang7b444f02018-06-12 16:42:09 -0700514 out->zero_buf = reinterpret_cast<char*>(calloc(block_size, 1));
515 if (!out->zero_buf) {
516 error_errno("malloc zero_buf");
517 return -ENOMEM;
518 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700519
Jerry Zhang7b444f02018-06-12 16:42:09 -0700520 out->fill_buf = reinterpret_cast<uint32_t*>(calloc(block_size, 1));
521 if (!out->fill_buf) {
522 error_errno("malloc fill_buf");
523 ret = -ENOMEM;
524 goto err_fill_buf;
525 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700526
Jerry Zhang7b444f02018-06-12 16:42:09 -0700527 if (sparse) {
528 out->sparse_ops = &sparse_file_ops;
529 } else {
530 out->sparse_ops = &normal_file_ops;
531 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700532
Jerry Zhang7b444f02018-06-12 16:42:09 -0700533 if (sparse) {
534 sparse_header_t sparse_header = {
535 .magic = SPARSE_HEADER_MAGIC,
536 .major_version = SPARSE_HEADER_MAJOR_VER,
537 .minor_version = SPARSE_HEADER_MINOR_VER,
538 .file_hdr_sz = SPARSE_HEADER_LEN,
539 .chunk_hdr_sz = CHUNK_HEADER_LEN,
540 .blk_sz = out->block_size,
541 .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)),
542 .total_chunks = static_cast<unsigned>(chunks),
543 .image_checksum = 0};
Colin Cross28fa5bc2012-05-20 13:28:05 -0700544
Jerry Zhang7b444f02018-06-12 16:42:09 -0700545 if (out->use_crc) {
546 sparse_header.total_chunks++;
547 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700548
Jerry Zhang7b444f02018-06-12 16:42:09 -0700549 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
550 if (ret < 0) {
551 goto err_write;
552 }
553 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700554
Jerry Zhang7b444f02018-06-12 16:42:09 -0700555 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700556
557err_write:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700558 free(out->fill_buf);
Colin Crossb55dcee2012-04-24 23:07:49 -0700559err_fill_buf:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700560 free(out->zero_buf);
561 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700562}
563
Jerry Zhang7b444f02018-06-12 16:42:09 -0700564static struct output_file* output_file_new_gz(void) {
565 struct output_file_gz* outgz =
566 reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
567 if (!outgz) {
568 error_errno("malloc struct outgz");
Yi Kong17ba95e2018-07-23 16:31:11 -0700569 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700570 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700571
Jerry Zhang7b444f02018-06-12 16:42:09 -0700572 outgz->out.ops = &gz_file_ops;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700573
Jerry Zhang7b444f02018-06-12 16:42:09 -0700574 return &outgz->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700575}
576
Jerry Zhang7b444f02018-06-12 16:42:09 -0700577static struct output_file* output_file_new_normal(void) {
578 struct output_file_normal* outn =
579 reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
580 if (!outn) {
581 error_errno("malloc struct outn");
Yi Kong17ba95e2018-07-23 16:31:11 -0700582 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700583 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700584
Jerry Zhang7b444f02018-06-12 16:42:09 -0700585 outn->out.ops = &file_ops;
Colin Crossb4cd2672012-05-18 14:49:50 -0700586
Jerry Zhang7b444f02018-06-12 16:42:09 -0700587 return &outn->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700588}
589
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000590struct output_file* output_file_open_callback(int (*write)(void*, const void*, size_t), void* priv,
591 unsigned int block_size, int64_t len, int gz __unused,
592 int sparse, int chunks, int crc) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700593 int ret;
594 struct output_file_callback* outc;
Colin Cross1e17b312012-05-21 16:35:45 -0700595
Jerry Zhang7b444f02018-06-12 16:42:09 -0700596 outc =
597 reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
598 if (!outc) {
599 error_errno("malloc struct outc");
Yi Kong17ba95e2018-07-23 16:31:11 -0700600 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700601 }
Colin Cross1e17b312012-05-21 16:35:45 -0700602
Jerry Zhang7b444f02018-06-12 16:42:09 -0700603 outc->out.ops = &callback_file_ops;
604 outc->priv = priv;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000605 outc->write = write;
Colin Cross1e17b312012-05-21 16:35:45 -0700606
Jerry Zhang7b444f02018-06-12 16:42:09 -0700607 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
608 if (ret < 0) {
609 free(outc);
Yi Kong17ba95e2018-07-23 16:31:11 -0700610 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700611 }
Colin Cross1e17b312012-05-21 16:35:45 -0700612
Jerry Zhang7b444f02018-06-12 16:42:09 -0700613 return &outc->out;
Colin Cross1e17b312012-05-21 16:35:45 -0700614}
615
Jerry Zhang7b444f02018-06-12 16:42:09 -0700616struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
617 int sparse, int chunks, int crc) {
618 int ret;
619 struct output_file* out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700620
Jerry Zhang7b444f02018-06-12 16:42:09 -0700621 if (gz) {
622 out = output_file_new_gz();
623 } else {
624 out = output_file_new_normal();
625 }
626 if (!out) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700627 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700628 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700629
Jerry Zhang7b444f02018-06-12 16:42:09 -0700630 out->ops->open(out, fd);
Colin Crossb4cd2672012-05-18 14:49:50 -0700631
Jerry Zhang7b444f02018-06-12 16:42:09 -0700632 ret = output_file_init(out, block_size, len, sparse, chunks, crc);
633 if (ret < 0) {
634 free(out);
Yi Kong17ba95e2018-07-23 16:31:11 -0700635 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700636 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700637
Jerry Zhang7b444f02018-06-12 16:42:09 -0700638 return out;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700639}
640
Colin Cross28fa5bc2012-05-20 13:28:05 -0700641/* Write a contiguous region of data blocks from a memory buffer */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700642int write_data_chunk(struct output_file* out, unsigned int len, void* data) {
643 return out->sparse_ops->write_data_chunk(out, len, data);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700644}
645
646/* Write a contiguous region of data blocks with a fill value */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700647int write_fill_chunk(struct output_file* out, unsigned int len, uint32_t fill_val) {
648 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700649}
650
Jerry Zhang7b444f02018-06-12 16:42:09 -0700651int write_fd_chunk(struct output_file* out, unsigned int len, int fd, int64_t offset) {
Elliott Hughesc44f50c2020-05-14 16:52:18 -0700652 auto m = android::base::MappedFile::FromFd(fd, offset, len, PROT_READ);
653 if (!m) return -errno;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000654
Elliott Hughesc44f50c2020-05-14 16:52:18 -0700655 return out->sparse_ops->write_data_chunk(out, m->size(), m->data());
Colin Cross9e1f17e2012-04-25 18:31:39 -0700656}
657
658/* Write a contiguous region of data blocks from a file */
Jerry Zhang7b444f02018-06-12 16:42:09 -0700659int write_file_chunk(struct output_file* out, unsigned int len, const char* file, int64_t offset) {
660 int ret;
Colin Cross9e1f17e2012-04-25 18:31:39 -0700661
Jerry Zhang7b444f02018-06-12 16:42:09 -0700662 int file_fd = open(file, O_RDONLY | O_BINARY);
663 if (file_fd < 0) {
664 return -errno;
665 }
Colin Cross9e1f17e2012-04-25 18:31:39 -0700666
Jerry Zhang7b444f02018-06-12 16:42:09 -0700667 ret = write_fd_chunk(out, len, file_fd, offset);
Colin Cross9e1f17e2012-04-25 18:31:39 -0700668
Jerry Zhang7b444f02018-06-12 16:42:09 -0700669 close(file_fd);
Colin Crossb55dcee2012-04-24 23:07:49 -0700670
Jerry Zhang7b444f02018-06-12 16:42:09 -0700671 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700672}
673
Jerry Zhang7b444f02018-06-12 16:42:09 -0700674int write_skip_chunk(struct output_file* out, int64_t len) {
675 return out->sparse_ops->write_skip_chunk(out, len);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700676}