From 5a755077958dd4f7fb668a125b57dea8d85cd9a8 Mon Sep 17 00:00:00 2001 From: Jerry Zhang Date: Tue, 12 Jun 2018 16:18:53 -0700 Subject: [PATCH] libsparse: Change source files to cpp Bug: 78793464 Test: compiles Change-Id: Ib8b933fe3ccb8dfa49a77f7955891678bf0df086 --- libsparse/Android.bp | 18 ++++++------- libsparse/{append2simg.c => append2simg.cpp} | 1 - .../{backed_block.c => backed_block.cpp} | 13 ++++----- libsparse/{img2simg.c => img2simg.cpp} | 0 libsparse/{output_file.c => output_file.cpp} | 27 ++++++++++--------- libsparse/{simg2img.c => simg2img.cpp} | 0 libsparse/{simg2simg.c => simg2simg.cpp} | 1 - libsparse/{sparse.c => sparse.cpp} | 6 ++--- .../{sparse_crc32.c => sparse_crc32.cpp} | 5 ++-- libsparse/sparse_crc32.h | 8 ------ libsparse/{sparse_err.c => sparse_err.cpp} | 0 11 files changed, 37 insertions(+), 42 deletions(-) rename libsparse/{append2simg.c => append2simg.cpp} (99%) rename libsparse/{backed_block.c => backed_block.cpp} (93%) rename libsparse/{img2simg.c => img2simg.cpp} (100%) rename libsparse/{output_file.c => output_file.cpp} (94%) rename libsparse/{simg2img.c => simg2img.cpp} (100%) rename libsparse/{simg2simg.c => simg2simg.cpp} (99%) rename libsparse/{sparse.c => sparse.cpp} (97%) rename libsparse/{sparse_crc32.c => sparse_crc32.cpp} (97%) rename libsparse/{sparse_err.c => sparse_err.cpp} (100%) diff --git a/libsparse/Android.bp b/libsparse/Android.bp index c7c089f49..8ad339f5f 100644 --- a/libsparse/Android.bp +++ b/libsparse/Android.bp @@ -6,11 +6,11 @@ cc_library { recovery_available: true, unique_host_soname: true, srcs: [ - "backed_block.c", - "output_file.c", - "sparse.c", - "sparse_crc32.c", - "sparse_err.c", + "backed_block.cpp", + "output_file.cpp", + "sparse.cpp", + "sparse_crc32.cpp", + "sparse_err.cpp", "sparse_read.cpp", ], cflags: ["-Werror"], @@ -31,8 +31,8 @@ cc_binary { name: "simg2img", host_supported: true, srcs: [ - "simg2img.c", - "sparse_crc32.c", + "simg2img.cpp", + "sparse_crc32.cpp", ], static_libs: [ "libsparse", @@ -46,7 +46,7 @@ cc_binary { cc_binary { name: "img2simg", host_supported: true, - srcs: ["img2simg.c"], + srcs: ["img2simg.cpp"], static_libs: [ "libsparse", "libz", @@ -58,7 +58,7 @@ cc_binary { cc_binary_host { name: "append2simg", - srcs: ["append2simg.c"], + srcs: ["append2simg.cpp"], static_libs: [ "libsparse", "libz", diff --git a/libsparse/append2simg.c b/libsparse/append2simg.cpp similarity index 99% rename from libsparse/append2simg.c rename to libsparse/append2simg.cpp index eef876400..83450a0d9 100644 --- a/libsparse/append2simg.c +++ b/libsparse/append2simg.cpp @@ -16,7 +16,6 @@ #define _FILE_OFFSET_BITS 64 #define _LARGEFILE64_SOURCE 1 -#define _GNU_SOURCE #include #include diff --git a/libsparse/backed_block.c b/libsparse/backed_block.cpp similarity index 93% rename from libsparse/backed_block.c rename to libsparse/backed_block.cpp index 794cd6b17..fb26d1a6e 100644 --- a/libsparse/backed_block.c +++ b/libsparse/backed_block.cpp @@ -122,7 +122,8 @@ void backed_block_destroy(struct backed_block *bb) struct backed_block_list *backed_block_list_new(unsigned int block_size) { - struct backed_block_list *b = calloc(sizeof(struct backed_block_list), 1); + struct backed_block_list *b = reinterpret_cast( + calloc(sizeof(struct backed_block_list), 1)); b->block_size = block_size; return b; } @@ -292,7 +293,7 @@ static int queue_bb(struct backed_block_list *bbl, struct backed_block *new_bb) int backed_block_add_fill(struct backed_block_list *bbl, unsigned int fill_val, unsigned int len, unsigned int block) { - struct backed_block *bb = calloc(1, sizeof(struct backed_block)); + struct backed_block *bb = reinterpret_cast(calloc(1, sizeof(struct backed_block))); if (bb == NULL) { return -ENOMEM; } @@ -310,7 +311,7 @@ int backed_block_add_fill(struct backed_block_list *bbl, unsigned int fill_val, int backed_block_add_data(struct backed_block_list *bbl, void *data, unsigned int len, unsigned int block) { - struct backed_block *bb = calloc(1, sizeof(struct backed_block)); + struct backed_block *bb = reinterpret_cast(calloc(1, sizeof(struct backed_block))); if (bb == NULL) { return -ENOMEM; } @@ -328,7 +329,7 @@ int backed_block_add_data(struct backed_block_list *bbl, void *data, int backed_block_add_file(struct backed_block_list *bbl, const char *filename, int64_t offset, unsigned int len, unsigned int block) { - struct backed_block *bb = calloc(1, sizeof(struct backed_block)); + struct backed_block *bb = reinterpret_cast(calloc(1, sizeof(struct backed_block))); if (bb == NULL) { return -ENOMEM; } @@ -347,7 +348,7 @@ int backed_block_add_file(struct backed_block_list *bbl, const char *filename, int backed_block_add_fd(struct backed_block_list *bbl, int fd, int64_t offset, unsigned int len, unsigned int block) { - struct backed_block *bb = calloc(1, sizeof(struct backed_block)); + struct backed_block *bb = reinterpret_cast(calloc(1, sizeof(struct backed_block))); if (bb == NULL) { return -ENOMEM; } @@ -373,7 +374,7 @@ int backed_block_split(struct backed_block_list *bbl, struct backed_block *bb, return 0; } - new_bb = malloc(sizeof(struct backed_block)); + new_bb = reinterpret_cast(malloc(sizeof(struct backed_block))); if (new_bb == NULL) { return -ENOMEM; } diff --git a/libsparse/img2simg.c b/libsparse/img2simg.cpp similarity index 100% rename from libsparse/img2simg.c rename to libsparse/img2simg.cpp diff --git a/libsparse/output_file.c b/libsparse/output_file.cpp similarity index 94% rename from libsparse/output_file.c rename to libsparse/output_file.cpp index 002ad2746..8d915508c 100644 --- a/libsparse/output_file.c +++ b/libsparse/output_file.cpp @@ -326,7 +326,7 @@ int read_all(int fd, void *buf, size_t len) { size_t total = 0; int ret; - char *ptr = buf; + char *ptr = reinterpret_cast(buf); while (total < len) { ret = read(fd, ptr, len - total); @@ -350,7 +350,7 @@ static int write_sparse_skip_chunk(struct output_file *out, int64_t skip_len) int ret; if (skip_len % out->block_size) { - error("don't care size %"PRIi64" is not a multiple of the block size %u", + error("don't care size %" PRIi64 " is not a multiple of the block size %u", skip_len, out->block_size); return -1; } @@ -557,13 +557,13 @@ static int output_file_init(struct output_file *out, int block_size, out->crc32 = 0; out->use_crc = crc; - out->zero_buf = calloc(block_size, 1); + out->zero_buf = reinterpret_cast(calloc(block_size, 1)); if (!out->zero_buf) { error_errno("malloc zero_buf"); return -ENOMEM; } - out->fill_buf = calloc(block_size, 1); + out->fill_buf = reinterpret_cast(calloc(block_size, 1)); if (!out->fill_buf) { error_errno("malloc fill_buf"); ret = -ENOMEM; @@ -584,8 +584,8 @@ static int output_file_init(struct output_file *out, int block_size, .file_hdr_sz = SPARSE_HEADER_LEN, .chunk_hdr_sz = CHUNK_HEADER_LEN, .blk_sz = out->block_size, - .total_blks = DIV_ROUND_UP(out->len, out->block_size), - .total_chunks = chunks, + .total_blks = static_cast(DIV_ROUND_UP(out->len, out->block_size)), + .total_chunks = static_cast(chunks), .image_checksum = 0 }; @@ -610,7 +610,8 @@ err_fill_buf: static struct output_file *output_file_new_gz(void) { - struct output_file_gz *outgz = calloc(1, sizeof(struct output_file_gz)); + struct output_file_gz *outgz = reinterpret_cast( + calloc(1, sizeof(struct output_file_gz))); if (!outgz) { error_errno("malloc struct outgz"); return NULL; @@ -623,7 +624,8 @@ static struct output_file *output_file_new_gz(void) static struct output_file *output_file_new_normal(void) { - struct output_file_normal *outn = calloc(1, sizeof(struct output_file_normal)); + struct output_file_normal *outn = reinterpret_cast( + calloc(1, sizeof(struct output_file_normal))); if (!outn) { error_errno("malloc struct outn"); return NULL; @@ -642,7 +644,8 @@ struct output_file *output_file_open_callback( int ret; struct output_file_callback *outc; - outc = calloc(1, sizeof(struct output_file_callback)); + outc = reinterpret_cast( + calloc(1, sizeof(struct output_file_callback))); if (!outc) { error_errno("malloc struct outc"); return NULL; @@ -716,15 +719,15 @@ int write_fd_chunk(struct output_file *out, unsigned int len, #ifndef _WIN32 if (buffer_size > SIZE_MAX) return -E2BIG; - char *data = mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, - aligned_offset); + char *data = reinterpret_cast(mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, + aligned_offset)); if (data == MAP_FAILED) { return -errno; } ptr = data + aligned_diff; #else off64_t pos; - char *data = malloc(len); + char *data = reinterpret_cast(malloc(len)); if (!data) { return -errno; } diff --git a/libsparse/simg2img.c b/libsparse/simg2img.cpp similarity index 100% rename from libsparse/simg2img.c rename to libsparse/simg2img.cpp diff --git a/libsparse/simg2simg.c b/libsparse/simg2simg.cpp similarity index 99% rename from libsparse/simg2simg.c rename to libsparse/simg2simg.cpp index 5f9ccf678..6f02f4fcc 100644 --- a/libsparse/simg2simg.c +++ b/libsparse/simg2simg.cpp @@ -16,7 +16,6 @@ #define _FILE_OFFSET_BITS 64 #define _LARGEFILE64_SOURCE 1 -#define _GNU_SOURCE #include #include diff --git a/libsparse/sparse.c b/libsparse/sparse.cpp similarity index 97% rename from libsparse/sparse.c rename to libsparse/sparse.cpp index 466435f7b..992945f44 100644 --- a/libsparse/sparse.c +++ b/libsparse/sparse.cpp @@ -29,7 +29,7 @@ struct sparse_file *sparse_file_new(unsigned int block_size, int64_t len) { - struct sparse_file *s = calloc(sizeof(struct sparse_file), 1); + struct sparse_file *s = reinterpret_cast(calloc(sizeof(struct sparse_file), 1)); if (!s) { return NULL; } @@ -209,7 +209,7 @@ struct chunk_data { static int foreach_chunk_write(void *priv, const void *data, size_t len) { - struct chunk_data *chk = priv; + struct chunk_data *chk = reinterpret_cast(priv); return chk->write(chk->priv, data, len, chk->block, chk->nr_blocks); } @@ -252,7 +252,7 @@ int sparse_file_foreach_chunk(struct sparse_file *s, bool sparse, bool crc, static int out_counter_write(void *priv, const void *data __unused, size_t len) { - int64_t *count = priv; + int64_t *count = reinterpret_cast(priv); *count += len; return 0; } diff --git a/libsparse/sparse_crc32.c b/libsparse/sparse_crc32.cpp similarity index 97% rename from libsparse/sparse_crc32.c rename to libsparse/sparse_crc32.cpp index 38bfe4aa0..97c5a1001 100644 --- a/libsparse/sparse_crc32.c +++ b/libsparse/sparse_crc32.cpp @@ -44,6 +44,7 @@ /* Code taken from FreeBSD 8 */ #include +#include static uint32_t crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, @@ -98,9 +99,9 @@ static uint32_t crc32_tab[] = { * in sys/libkern.h, where it can be inlined. */ -uint32_t sparse_crc32(uint32_t crc_in, const void *buf, int size) +uint32_t sparse_crc32(uint32_t crc_in, const void *buf, size_t size) { - const uint8_t *p = buf; + const uint8_t *p = reinterpret_cast(buf); uint32_t crc; crc = crc_in ^ ~0U; diff --git a/libsparse/sparse_crc32.h b/libsparse/sparse_crc32.h index 50cd9e989..e42c1eb98 100644 --- a/libsparse/sparse_crc32.h +++ b/libsparse/sparse_crc32.h @@ -19,14 +19,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - uint32_t sparse_crc32(uint32_t crc, const void *buf, size_t size); -#ifdef __cplusplus -} -#endif - #endif diff --git a/libsparse/sparse_err.c b/libsparse/sparse_err.cpp similarity index 100% rename from libsparse/sparse_err.c rename to libsparse/sparse_err.cpp