diff options
Diffstat (limited to 'libsparse')
-rw-r--r-- | libsparse/Android.bp | 15 | ||||
-rw-r--r-- | libsparse/output_file.h | 8 | ||||
-rw-r--r-- | libsparse/sparse_file.h | 7 | ||||
-rw-r--r-- | libsparse/sparse_format.h | 8 | ||||
-rw-r--r-- | libsparse/sparse_read.cpp (renamed from libsparse/sparse_read.c) | 75 |
5 files changed, 60 insertions, 53 deletions
diff --git a/libsparse/Android.bp b/libsparse/Android.bp index dd8b5fd1d..6ec0991c7 100644 --- a/libsparse/Android.bp +++ b/libsparse/Android.bp | |||
@@ -10,17 +10,23 @@ cc_library { | |||
10 | "sparse.c", | 10 | "sparse.c", |
11 | "sparse_crc32.c", | 11 | "sparse_crc32.c", |
12 | "sparse_err.c", | 12 | "sparse_err.c", |
13 | "sparse_read.c", | 13 | "sparse_read.cpp", |
14 | ], | 14 | ], |
15 | cflags: ["-Werror"], | 15 | cflags: ["-Werror"], |
16 | local_include_dirs: ["include"], | 16 | local_include_dirs: ["include"], |
17 | export_include_dirs: ["include"], | 17 | export_include_dirs: ["include"], |
18 | target: { | 18 | target: { |
19 | host: { | 19 | host: { |
20 | shared_libs: ["libz-host"], | 20 | shared_libs: [ |
21 | "libz-host", | ||
22 | "libbase", | ||
23 | ], | ||
21 | }, | 24 | }, |
22 | android: { | 25 | android: { |
23 | shared_libs: ["libz"], | 26 | shared_libs: [ |
27 | "libz", | ||
28 | "libbase", | ||
29 | ], | ||
24 | }, | 30 | }, |
25 | windows: { | 31 | windows: { |
26 | enabled: true, | 32 | enabled: true, |
@@ -38,6 +44,7 @@ cc_binary { | |||
38 | static_libs: [ | 44 | static_libs: [ |
39 | "libsparse", | 45 | "libsparse", |
40 | "libz", | 46 | "libz", |
47 | "libbase", | ||
41 | ], | 48 | ], |
42 | 49 | ||
43 | cflags: ["-Werror"], | 50 | cflags: ["-Werror"], |
@@ -50,6 +57,7 @@ cc_binary { | |||
50 | static_libs: [ | 57 | static_libs: [ |
51 | "libsparse", | 58 | "libsparse", |
52 | "libz", | 59 | "libz", |
60 | "libbase", | ||
53 | ], | 61 | ], |
54 | 62 | ||
55 | cflags: ["-Werror"], | 63 | cflags: ["-Werror"], |
@@ -61,6 +69,7 @@ cc_binary_host { | |||
61 | static_libs: [ | 69 | static_libs: [ |
62 | "libsparse", | 70 | "libsparse", |
63 | "libz", | 71 | "libz", |
72 | "libbase", | ||
64 | ], | 73 | ], |
65 | 74 | ||
66 | cflags: ["-Werror"], | 75 | cflags: ["-Werror"], |
diff --git a/libsparse/output_file.h b/libsparse/output_file.h index 474c1fc21..b67e94ecb 100644 --- a/libsparse/output_file.h +++ b/libsparse/output_file.h | |||
@@ -17,6 +17,10 @@ | |||
17 | #ifndef _OUTPUT_FILE_H_ | 17 | #ifndef _OUTPUT_FILE_H_ |
18 | #define _OUTPUT_FILE_H_ | 18 | #define _OUTPUT_FILE_H_ |
19 | 19 | ||
20 | #ifdef __cplusplus | ||
21 | extern "C" { | ||
22 | #endif | ||
23 | |||
20 | #include <sparse/sparse.h> | 24 | #include <sparse/sparse.h> |
21 | 25 | ||
22 | struct output_file; | 26 | struct output_file; |
@@ -38,4 +42,8 @@ void output_file_close(struct output_file *out); | |||
38 | 42 | ||
39 | int read_all(int fd, void *buf, size_t len); | 43 | int read_all(int fd, void *buf, size_t len); |
40 | 44 | ||
45 | #ifdef __cplusplus | ||
46 | } | ||
47 | #endif | ||
48 | |||
41 | #endif | 49 | #endif |
diff --git a/libsparse/sparse_file.h b/libsparse/sparse_file.h index 91a12e678..763f43f67 100644 --- a/libsparse/sparse_file.h +++ b/libsparse/sparse_file.h | |||
@@ -17,6 +17,10 @@ | |||
17 | #ifndef _LIBSPARSE_SPARSE_FILE_H_ | 17 | #ifndef _LIBSPARSE_SPARSE_FILE_H_ |
18 | #define _LIBSPARSE_SPARSE_FILE_H_ | 18 | #define _LIBSPARSE_SPARSE_FILE_H_ |
19 | 19 | ||
20 | #ifdef __cplusplus | ||
21 | extern "C" { | ||
22 | #endif | ||
23 | |||
20 | #include <sparse/sparse.h> | 24 | #include <sparse/sparse.h> |
21 | 25 | ||
22 | struct sparse_file { | 26 | struct sparse_file { |
@@ -28,5 +32,8 @@ struct sparse_file { | |||
28 | struct output_file *out; | 32 | struct output_file *out; |
29 | }; | 33 | }; |
30 | 34 | ||
35 | #ifdef __cplusplus | ||
36 | } | ||
37 | #endif | ||
31 | 38 | ||
32 | #endif /* _LIBSPARSE_SPARSE_FILE_H_ */ | 39 | #endif /* _LIBSPARSE_SPARSE_FILE_H_ */ |
diff --git a/libsparse/sparse_format.h b/libsparse/sparse_format.h index c41f12a54..779e0385d 100644 --- a/libsparse/sparse_format.h +++ b/libsparse/sparse_format.h | |||
@@ -18,6 +18,10 @@ | |||
18 | #define _LIBSPARSE_SPARSE_FORMAT_H_ | 18 | #define _LIBSPARSE_SPARSE_FORMAT_H_ |
19 | #include "sparse_defs.h" | 19 | #include "sparse_defs.h" |
20 | 20 | ||
21 | #ifdef __cplusplus | ||
22 | extern "C" { | ||
23 | #endif | ||
24 | |||
21 | typedef struct sparse_header { | 25 | typedef struct sparse_header { |
22 | __le32 magic; /* 0xed26ff3a */ | 26 | __le32 magic; /* 0xed26ff3a */ |
23 | __le16 major_version; /* (0x1) - reject images with higher major versions */ | 27 | __le16 major_version; /* (0x1) - reject images with higher major versions */ |
@@ -52,4 +56,8 @@ typedef struct chunk_header { | |||
52 | * For a CRC32 chunk, it's 4 bytes of CRC32 | 56 | * For a CRC32 chunk, it's 4 bytes of CRC32 |
53 | */ | 57 | */ |
54 | 58 | ||
59 | #ifdef __cplusplus | ||
60 | } | ||
61 | #endif | ||
62 | |||
55 | #endif | 63 | #endif |
diff --git a/libsparse/sparse_read.c b/libsparse/sparse_read.cpp index a18820277..bd668735a 100644 --- a/libsparse/sparse_read.c +++ b/libsparse/sparse_read.cpp | |||
@@ -14,10 +14,10 @@ | |||
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | 16 | ||
17 | #define _GNU_SOURCE | ||
18 | #define _FILE_OFFSET_BITS 64 | 17 | #define _FILE_OFFSET_BITS 64 |
19 | #define _LARGEFILE64_SOURCE 1 | 18 | #define _LARGEFILE64_SOURCE 1 |
20 | 19 | ||
20 | #include <algorithm> | ||
21 | #include <inttypes.h> | 21 | #include <inttypes.h> |
22 | #include <fcntl.h> | 22 | #include <fcntl.h> |
23 | #include <stdarg.h> | 23 | #include <stdarg.h> |
@@ -25,17 +25,19 @@ | |||
25 | #include <stdint.h> | 25 | #include <stdint.h> |
26 | #include <stdio.h> | 26 | #include <stdio.h> |
27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
28 | #include <string.h> | 28 | #include <string> |
29 | #include <unistd.h> | 29 | #include <unistd.h> |
30 | 30 | ||
31 | #include <sparse/sparse.h> | 31 | #include <sparse/sparse.h> |
32 | 32 | ||
33 | #include "android-base/stringprintf.h" | ||
33 | #include "defs.h" | 34 | #include "defs.h" |
34 | #include "output_file.h" | 35 | #include "output_file.h" |
35 | #include "sparse_crc32.h" | 36 | #include "sparse_crc32.h" |
36 | #include "sparse_file.h" | 37 | #include "sparse_file.h" |
37 | #include "sparse_format.h" | 38 | #include "sparse_format.h" |
38 | 39 | ||
40 | |||
39 | #if defined(__APPLE__) && defined(__MACH__) | 41 | #if defined(__APPLE__) && defined(__MACH__) |
40 | #define lseek64 lseek | 42 | #define lseek64 lseek |
41 | #define off64_t off_t | 43 | #define off64_t off_t |
@@ -45,57 +47,30 @@ | |||
45 | #define SPARSE_HEADER_LEN (sizeof(sparse_header_t)) | 47 | #define SPARSE_HEADER_LEN (sizeof(sparse_header_t)) |
46 | #define CHUNK_HEADER_LEN (sizeof(chunk_header_t)) | 48 | #define CHUNK_HEADER_LEN (sizeof(chunk_header_t)) |
47 | 49 | ||
48 | #define COPY_BUF_SIZE (1024U*1024U) | 50 | static constexpr int64_t COPY_BUF_SIZE = 1024 * 1024; |
49 | static char *copybuf; | 51 | static char *copybuf; |
50 | 52 | ||
51 | #define min(a, b) \ | 53 | static std::string ErrorString(int err) |
52 | ({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; }) | 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 | } | ||
53 | 60 | ||
54 | static void verbose_error(bool verbose, int err, const char *fmt, ...) | 61 | static void verbose_error(bool verbose, int err, const char *fmt, ...) |
55 | { | 62 | { |
56 | char *s = ""; | 63 | if (!verbose) return; |
57 | char *at = ""; | 64 | |
65 | std::string msg = ErrorString(err); | ||
58 | if (fmt) { | 66 | if (fmt) { |
67 | msg += " at "; | ||
59 | va_list argp; | 68 | va_list argp; |
60 | int size; | ||
61 | |||
62 | va_start(argp, fmt); | 69 | va_start(argp, fmt); |
63 | size = vsnprintf(NULL, 0, fmt, argp); | 70 | android::base::StringAppendV(&msg, fmt, argp); |
64 | va_end(argp); | 71 | va_end(argp); |
65 | |||
66 | if (size < 0) { | ||
67 | return; | ||
68 | } | ||
69 | |||
70 | at = malloc(size + 1); | ||
71 | if (at == NULL) { | ||
72 | return; | ||
73 | } | ||
74 | |||
75 | va_start(argp, fmt); | ||
76 | vsnprintf(at, size, fmt, argp); | ||
77 | va_end(argp); | ||
78 | at[size] = 0; | ||
79 | s = " at "; | ||
80 | } | ||
81 | if (verbose) { | ||
82 | #ifndef _WIN32 | ||
83 | if (err == -EOVERFLOW) { | ||
84 | sparse_print_verbose("EOF while reading file%s%s\n", s, at); | ||
85 | } else | ||
86 | #endif | ||
87 | if (err == -EINVAL) { | ||
88 | sparse_print_verbose("Invalid sparse file format%s%s\n", s, at); | ||
89 | } else if (err == -ENOMEM) { | ||
90 | sparse_print_verbose("Failed allocation while reading file%s%s\n", | ||
91 | s, at); | ||
92 | } else { | ||
93 | sparse_print_verbose("Unknown error %d%s%s\n", err, s, at); | ||
94 | } | ||
95 | } | ||
96 | if (fmt) { | ||
97 | free(at); | ||
98 | } | 72 | } |
73 | sparse_print_verbose("%s\n", msg.c_str()); | ||
99 | } | 74 | } |
100 | 75 | ||
101 | static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size, | 76 | static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size, |
@@ -104,7 +79,7 @@ static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size, | |||
104 | { | 79 | { |
105 | int ret; | 80 | int ret; |
106 | int chunk; | 81 | int chunk; |
107 | unsigned int len = blocks * s->block_size; | 82 | int64_t len = blocks * s->block_size; |
108 | 83 | ||
109 | if (chunk_size % s->block_size != 0) { | 84 | if (chunk_size % s->block_size != 0) { |
110 | return -EINVAL; | 85 | return -EINVAL; |
@@ -121,7 +96,7 @@ static int process_raw_chunk(struct sparse_file *s, unsigned int chunk_size, | |||
121 | 96 | ||
122 | if (crc32) { | 97 | if (crc32) { |
123 | while (len) { | 98 | while (len) { |
124 | chunk = min(len, COPY_BUF_SIZE); | 99 | chunk = std::min(len, COPY_BUF_SIZE); |
125 | ret = read_all(fd, copybuf, chunk); | 100 | ret = read_all(fd, copybuf, chunk); |
126 | if (ret < 0) { | 101 | if (ret < 0) { |
127 | return ret; | 102 | return ret; |
@@ -168,7 +143,7 @@ static int process_fill_chunk(struct sparse_file *s, unsigned int chunk_size, | |||
168 | } | 143 | } |
169 | 144 | ||
170 | while (len) { | 145 | while (len) { |
171 | chunk = min(len, COPY_BUF_SIZE); | 146 | chunk = std::min(len, COPY_BUF_SIZE); |
172 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); | 147 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); |
173 | len -= chunk; | 148 | len -= chunk; |
174 | } | 149 | } |
@@ -190,7 +165,7 @@ static int process_skip_chunk(struct sparse_file *s, unsigned int chunk_size, | |||
190 | memset(copybuf, 0, COPY_BUF_SIZE); | 165 | memset(copybuf, 0, COPY_BUF_SIZE); |
191 | 166 | ||
192 | while (len) { | 167 | while (len) { |
193 | int chunk = min(len, COPY_BUF_SIZE); | 168 | int chunk = std::min(len, COPY_BUF_SIZE); |
194 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); | 169 | *crc32 = sparse_crc32(*crc32, copybuf, chunk); |
195 | len -= chunk; | 170 | len -= chunk; |
196 | } | 171 | } |
@@ -284,7 +259,7 @@ static int sparse_file_read_sparse(struct sparse_file *s, int fd, bool crc) | |||
284 | off64_t offset; | 259 | off64_t offset; |
285 | 260 | ||
286 | if (!copybuf) { | 261 | if (!copybuf) { |
287 | copybuf = malloc(COPY_BUF_SIZE); | 262 | copybuf = (char *)malloc(COPY_BUF_SIZE); |
288 | } | 263 | } |
289 | 264 | ||
290 | if (!copybuf) { | 265 | if (!copybuf) { |
@@ -357,7 +332,7 @@ static int sparse_file_read_sparse(struct sparse_file *s, int fd, bool crc) | |||
357 | static int sparse_file_read_normal(struct sparse_file *s, int fd) | 332 | static int sparse_file_read_normal(struct sparse_file *s, int fd) |
358 | { | 333 | { |
359 | int ret; | 334 | int ret; |
360 | uint32_t *buf = malloc(s->block_size); | 335 | uint32_t *buf = (uint32_t *)malloc(s->block_size); |
361 | unsigned int block = 0; | 336 | unsigned int block = 0; |
362 | int64_t remain = s->len; | 337 | int64_t remain = s->len; |
363 | int64_t offset = 0; | 338 | int64_t offset = 0; |
@@ -370,7 +345,7 @@ static int sparse_file_read_normal(struct sparse_file *s, int fd) | |||
370 | } | 345 | } |
371 | 346 | ||
372 | while (remain > 0) { | 347 | while (remain > 0) { |
373 | to_read = min(remain, s->block_size); | 348 | to_read = std::min(remain, (int64_t)(s->block_size)); |
374 | ret = read_all(fd, buf, to_read); | 349 | ret = read_all(fd, buf, to_read); |
375 | if (ret < 0) { | 350 | if (ret < 0) { |
376 | error("failed to read sparse file"); | 351 | error("failed to read sparse file"); |