blob: b2c5407e169f0eb7546a619038e34b855db2e9a9 [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 {
Hyeongseok Kime8d02c52020-08-10 12:11:57 +090068 int (*write_data_chunk)(struct output_file* out, uint64_t len, void* data);
69 int (*write_fill_chunk)(struct output_file* out, uint64_t len, uint32_t fill_val);
70 int (*write_skip_chunk)(struct output_file* out, uint64_t len);
Jerry Zhang7b444f02018-06-12 16:42:09 -070071 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
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900319static int write_sparse_skip_chunk(struct output_file* out, uint64_t skip_len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700320 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
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900343static int write_sparse_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700344 chunk_header_t chunk_header;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900345 uint64_t rnd_up_len;
346 int count;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700347 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700348
Jerry Zhang7b444f02018-06-12 16:42:09 -0700349 /* Round up the fill length to a multiple of the block size */
350 rnd_up_len = ALIGN(len, out->block_size);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700351
Jerry Zhang7b444f02018-06-12 16:42:09 -0700352 /* Finally we can safely emit a chunk of data */
353 chunk_header.chunk_type = CHUNK_TYPE_FILL;
354 chunk_header.reserved1 = 0;
355 chunk_header.chunk_sz = rnd_up_len / out->block_size;
356 chunk_header.total_sz = CHUNK_HEADER_LEN + sizeof(fill_val);
357 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700358
Jerry Zhang7b444f02018-06-12 16:42:09 -0700359 if (ret < 0) return -1;
360 ret = out->ops->write(out, &fill_val, sizeof(fill_val));
361 if (ret < 0) return -1;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700362
Jerry Zhang7b444f02018-06-12 16:42:09 -0700363 if (out->use_crc) {
364 count = out->block_size / sizeof(uint32_t);
365 while (count--) out->crc32 = sparse_crc32(out->crc32, &fill_val, sizeof(uint32_t));
366 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700367
Jerry Zhang7b444f02018-06-12 16:42:09 -0700368 out->cur_out_ptr += rnd_up_len;
369 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700370
Jerry Zhang7b444f02018-06-12 16:42:09 -0700371 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700372}
373
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900374static int write_sparse_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700375 chunk_header_t chunk_header;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900376 uint64_t rnd_up_len, zero_len;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700377 int ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700378
Jerry Zhang7b444f02018-06-12 16:42:09 -0700379 /* Round up the data length to a multiple of the block size */
380 rnd_up_len = ALIGN(len, out->block_size);
381 zero_len = rnd_up_len - len;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700382
Jerry Zhang7b444f02018-06-12 16:42:09 -0700383 /* Finally we can safely emit a chunk of data */
384 chunk_header.chunk_type = CHUNK_TYPE_RAW;
385 chunk_header.reserved1 = 0;
386 chunk_header.chunk_sz = rnd_up_len / out->block_size;
387 chunk_header.total_sz = CHUNK_HEADER_LEN + rnd_up_len;
388 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
Colin Cross28fa5bc2012-05-20 13:28:05 -0700389
Jerry Zhang7b444f02018-06-12 16:42:09 -0700390 if (ret < 0) return -1;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000391 ret = out->ops->write(out, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700392 if (ret < 0) return -1;
393 if (zero_len) {
394 ret = out->ops->write(out, out->zero_buf, zero_len);
395 if (ret < 0) return -1;
396 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700397
Jerry Zhang7b444f02018-06-12 16:42:09 -0700398 if (out->use_crc) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000399 out->crc32 = sparse_crc32(out->crc32, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700400 if (zero_len) out->crc32 = sparse_crc32(out->crc32, out->zero_buf, zero_len);
401 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700402
Jerry Zhang7b444f02018-06-12 16:42:09 -0700403 out->cur_out_ptr += rnd_up_len;
404 out->chunk_cnt++;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700405
Jerry Zhang7b444f02018-06-12 16:42:09 -0700406 return 0;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700407}
408
Jerry Zhang7b444f02018-06-12 16:42:09 -0700409int write_sparse_end_chunk(struct output_file* out) {
410 chunk_header_t chunk_header;
411 int ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700412
Jerry Zhang7b444f02018-06-12 16:42:09 -0700413 if (out->use_crc) {
414 chunk_header.chunk_type = CHUNK_TYPE_CRC32;
415 chunk_header.reserved1 = 0;
416 chunk_header.chunk_sz = 0;
417 chunk_header.total_sz = CHUNK_HEADER_LEN + 4;
Colin Crossb55dcee2012-04-24 23:07:49 -0700418
Jerry Zhang7b444f02018-06-12 16:42:09 -0700419 ret = out->ops->write(out, &chunk_header, sizeof(chunk_header));
420 if (ret < 0) {
421 return ret;
422 }
423 out->ops->write(out, &out->crc32, 4);
424 if (ret < 0) {
425 return ret;
426 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700427
Jerry Zhang7b444f02018-06-12 16:42:09 -0700428 out->chunk_cnt++;
429 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700430
Jerry Zhang7b444f02018-06-12 16:42:09 -0700431 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700432}
433
434static struct sparse_file_ops sparse_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700435 .write_data_chunk = write_sparse_data_chunk,
436 .write_fill_chunk = write_sparse_fill_chunk,
437 .write_skip_chunk = write_sparse_skip_chunk,
438 .write_end_chunk = write_sparse_end_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700439};
440
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900441static int write_normal_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700442 int ret;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900443 uint64_t rnd_up_len = ALIGN(len, out->block_size);
Colin Crossb55dcee2012-04-24 23:07:49 -0700444
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000445 ret = out->ops->write(out, data, len);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700446 if (ret < 0) {
447 return ret;
448 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700449
Jerry Zhang7b444f02018-06-12 16:42:09 -0700450 if (rnd_up_len > len) {
451 ret = out->ops->skip(out, rnd_up_len - len);
452 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700453
Jerry Zhang7b444f02018-06-12 16:42:09 -0700454 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700455}
456
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900457static int write_normal_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000458 int ret;
459 unsigned int i;
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900460 uint64_t write_len;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000461
462 /* Initialize fill_buf with the fill_val */
463 for (i = 0; i < out->block_size / sizeof(uint32_t); i++) {
464 out->fill_buf[i] = fill_val;
465 }
466
467 while (len) {
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900468 write_len = std::min(len, (uint64_t)out->block_size);
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000469 ret = out->ops->write(out, out->fill_buf, write_len);
470 if (ret < 0) {
471 return ret;
472 }
473
474 len -= write_len;
475 }
476
477 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700478}
479
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900480static int write_normal_skip_chunk(struct output_file* out, uint64_t len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700481 return out->ops->skip(out, len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700482}
483
Jerry Zhang7b444f02018-06-12 16:42:09 -0700484int write_normal_end_chunk(struct output_file* out) {
485 return out->ops->pad(out, out->len);
Colin Crossb55dcee2012-04-24 23:07:49 -0700486}
487
488static struct sparse_file_ops normal_file_ops = {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700489 .write_data_chunk = write_normal_data_chunk,
490 .write_fill_chunk = write_normal_fill_chunk,
491 .write_skip_chunk = write_normal_skip_chunk,
492 .write_end_chunk = write_normal_end_chunk,
Colin Crossb55dcee2012-04-24 23:07:49 -0700493};
494
Jerry Zhang7b444f02018-06-12 16:42:09 -0700495void output_file_close(struct output_file* out) {
496 out->sparse_ops->write_end_chunk(out);
Robin Hsu48c9f612019-11-07 13:44:08 +0800497 free(out->zero_buf);
498 free(out->fill_buf);
499 out->zero_buf = nullptr;
500 out->fill_buf = nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700501 out->ops->close(out);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700502}
503
Jerry Zhang7b444f02018-06-12 16:42:09 -0700504static int output_file_init(struct output_file* out, int block_size, int64_t len, bool sparse,
505 int chunks, bool crc) {
506 int ret;
Colin Crossb4cd2672012-05-18 14:49:50 -0700507
Jerry Zhang7b444f02018-06-12 16:42:09 -0700508 out->len = len;
509 out->block_size = block_size;
Chih-Hung Hsieh5d08f632018-12-11 10:38:27 -0800510 out->cur_out_ptr = 0LL;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700511 out->chunk_cnt = 0;
512 out->crc32 = 0;
513 out->use_crc = crc;
Colin Crossb4cd2672012-05-18 14:49:50 -0700514
Jerry Zhang7b444f02018-06-12 16:42:09 -0700515 out->zero_buf = reinterpret_cast<char*>(calloc(block_size, 1));
516 if (!out->zero_buf) {
517 error_errno("malloc zero_buf");
518 return -ENOMEM;
519 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700520
Jerry Zhang7b444f02018-06-12 16:42:09 -0700521 out->fill_buf = reinterpret_cast<uint32_t*>(calloc(block_size, 1));
522 if (!out->fill_buf) {
523 error_errno("malloc fill_buf");
524 ret = -ENOMEM;
525 goto err_fill_buf;
526 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700527
Jerry Zhang7b444f02018-06-12 16:42:09 -0700528 if (sparse) {
529 out->sparse_ops = &sparse_file_ops;
530 } else {
531 out->sparse_ops = &normal_file_ops;
532 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700533
Jerry Zhang7b444f02018-06-12 16:42:09 -0700534 if (sparse) {
535 sparse_header_t sparse_header = {
536 .magic = SPARSE_HEADER_MAGIC,
537 .major_version = SPARSE_HEADER_MAJOR_VER,
538 .minor_version = SPARSE_HEADER_MINOR_VER,
539 .file_hdr_sz = SPARSE_HEADER_LEN,
540 .chunk_hdr_sz = CHUNK_HEADER_LEN,
541 .blk_sz = out->block_size,
542 .total_blks = static_cast<unsigned>(DIV_ROUND_UP(out->len, out->block_size)),
543 .total_chunks = static_cast<unsigned>(chunks),
544 .image_checksum = 0};
Colin Cross28fa5bc2012-05-20 13:28:05 -0700545
Jerry Zhang7b444f02018-06-12 16:42:09 -0700546 if (out->use_crc) {
547 sparse_header.total_chunks++;
548 }
Colin Crossb55dcee2012-04-24 23:07:49 -0700549
Jerry Zhang7b444f02018-06-12 16:42:09 -0700550 ret = out->ops->write(out, &sparse_header, sizeof(sparse_header));
551 if (ret < 0) {
552 goto err_write;
553 }
554 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700555
Jerry Zhang7b444f02018-06-12 16:42:09 -0700556 return 0;
Colin Crossb55dcee2012-04-24 23:07:49 -0700557
558err_write:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700559 free(out->fill_buf);
Colin Crossb55dcee2012-04-24 23:07:49 -0700560err_fill_buf:
Jerry Zhang7b444f02018-06-12 16:42:09 -0700561 free(out->zero_buf);
562 return ret;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700563}
564
Jerry Zhang7b444f02018-06-12 16:42:09 -0700565static struct output_file* output_file_new_gz(void) {
566 struct output_file_gz* outgz =
567 reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
568 if (!outgz) {
569 error_errno("malloc struct outgz");
Yi Kong17ba95e2018-07-23 16:31:11 -0700570 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700571 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700572
Jerry Zhang7b444f02018-06-12 16:42:09 -0700573 outgz->out.ops = &gz_file_ops;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700574
Jerry Zhang7b444f02018-06-12 16:42:09 -0700575 return &outgz->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700576}
577
Jerry Zhang7b444f02018-06-12 16:42:09 -0700578static struct output_file* output_file_new_normal(void) {
579 struct output_file_normal* outn =
580 reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
581 if (!outn) {
582 error_errno("malloc struct outn");
Yi Kong17ba95e2018-07-23 16:31:11 -0700583 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700584 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700585
Jerry Zhang7b444f02018-06-12 16:42:09 -0700586 outn->out.ops = &file_ops;
Colin Crossb4cd2672012-05-18 14:49:50 -0700587
Jerry Zhang7b444f02018-06-12 16:42:09 -0700588 return &outn->out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700589}
590
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000591struct output_file* output_file_open_callback(int (*write)(void*, const void*, size_t), void* priv,
592 unsigned int block_size, int64_t len, int gz __unused,
593 int sparse, int chunks, int crc) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700594 int ret;
595 struct output_file_callback* outc;
Colin Cross1e17b312012-05-21 16:35:45 -0700596
Jerry Zhang7b444f02018-06-12 16:42:09 -0700597 outc =
598 reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
599 if (!outc) {
600 error_errno("malloc struct outc");
Yi Kong17ba95e2018-07-23 16:31:11 -0700601 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700602 }
Colin Cross1e17b312012-05-21 16:35:45 -0700603
Jerry Zhang7b444f02018-06-12 16:42:09 -0700604 outc->out.ops = &callback_file_ops;
605 outc->priv = priv;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000606 outc->write = write;
Colin Cross1e17b312012-05-21 16:35:45 -0700607
Jerry Zhang7b444f02018-06-12 16:42:09 -0700608 ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
609 if (ret < 0) {
610 free(outc);
Yi Kong17ba95e2018-07-23 16:31:11 -0700611 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700612 }
Colin Cross1e17b312012-05-21 16:35:45 -0700613
Jerry Zhang7b444f02018-06-12 16:42:09 -0700614 return &outc->out;
Colin Cross1e17b312012-05-21 16:35:45 -0700615}
616
Jerry Zhang7b444f02018-06-12 16:42:09 -0700617struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t len, int gz,
618 int sparse, int chunks, int crc) {
619 int ret;
620 struct output_file* out;
Colin Crossb4cd2672012-05-18 14:49:50 -0700621
Jerry Zhang7b444f02018-06-12 16:42:09 -0700622 if (gz) {
623 out = output_file_new_gz();
624 } else {
625 out = output_file_new_normal();
626 }
627 if (!out) {
Yi Kong17ba95e2018-07-23 16:31:11 -0700628 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700629 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700630
Jerry Zhang7b444f02018-06-12 16:42:09 -0700631 out->ops->open(out, fd);
Colin Crossb4cd2672012-05-18 14:49:50 -0700632
Jerry Zhang7b444f02018-06-12 16:42:09 -0700633 ret = output_file_init(out, block_size, len, sparse, chunks, crc);
634 if (ret < 0) {
635 free(out);
Yi Kong17ba95e2018-07-23 16:31:11 -0700636 return nullptr;
Jerry Zhang7b444f02018-06-12 16:42:09 -0700637 }
Colin Crossb4cd2672012-05-18 14:49:50 -0700638
Jerry Zhang7b444f02018-06-12 16:42:09 -0700639 return out;
Colin Cross28fa5bc2012-05-20 13:28:05 -0700640}
641
Colin Cross28fa5bc2012-05-20 13:28:05 -0700642/* Write a contiguous region of data blocks from a memory buffer */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900643int write_data_chunk(struct output_file* out, uint64_t len, void* data) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700644 return out->sparse_ops->write_data_chunk(out, len, data);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700645}
646
647/* Write a contiguous region of data blocks with a fill value */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900648int write_fill_chunk(struct output_file* out, uint64_t len, uint32_t fill_val) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700649 return out->sparse_ops->write_fill_chunk(out, len, fill_val);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700650}
651
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900652int write_fd_chunk(struct output_file* out, uint64_t len, int fd, int64_t offset) {
Elliott Hughesc44f50c2020-05-14 16:52:18 -0700653 auto m = android::base::MappedFile::FromFd(fd, offset, len, PROT_READ);
654 if (!m) return -errno;
Tobias Thiererfca4a9c2018-07-26 03:13:45 +0000655
Elliott Hughesc44f50c2020-05-14 16:52:18 -0700656 return out->sparse_ops->write_data_chunk(out, m->size(), m->data());
Colin Cross9e1f17e2012-04-25 18:31:39 -0700657}
658
659/* Write a contiguous region of data blocks from a file */
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900660int write_file_chunk(struct output_file* out, uint64_t len, const char* file, int64_t offset) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700661 int ret;
Colin Cross9e1f17e2012-04-25 18:31:39 -0700662
Jerry Zhang7b444f02018-06-12 16:42:09 -0700663 int file_fd = open(file, O_RDONLY | O_BINARY);
664 if (file_fd < 0) {
665 return -errno;
666 }
Colin Cross9e1f17e2012-04-25 18:31:39 -0700667
Jerry Zhang7b444f02018-06-12 16:42:09 -0700668 ret = write_fd_chunk(out, len, file_fd, offset);
Colin Cross9e1f17e2012-04-25 18:31:39 -0700669
Jerry Zhang7b444f02018-06-12 16:42:09 -0700670 close(file_fd);
Colin Crossb55dcee2012-04-24 23:07:49 -0700671
Jerry Zhang7b444f02018-06-12 16:42:09 -0700672 return ret;
Colin Crossb55dcee2012-04-24 23:07:49 -0700673}
674
Hyeongseok Kime8d02c52020-08-10 12:11:57 +0900675int write_skip_chunk(struct output_file* out, uint64_t len) {
Jerry Zhang7b444f02018-06-12 16:42:09 -0700676 return out->sparse_ops->write_skip_chunk(out, len);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700677}