kexec-tools: fix compile error
[openwrt/openwrt.git] / package / boot / kexec-tools / patches / 0001-Fix-zlib-lzma-decompression.patch
1 From d606837b56d46eb7f815b5d85f07fcc3f1555d00 Mon Sep 17 00:00:00 2001
2 From: Yousong Zhou <yszhou4tech@gmail.com>
3 Date: Sun, 1 Feb 2015 00:10:07 +0800
4 Subject: [PATCH 1/5] Fix zlib/lzma decompression.
5
6 Let {zlib,lzma}_decompress_file() return NULL if anything wrong happened
7 to allow the other method to have a chance to run.
8
9 Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
10 Signed-off-by: Simon Horman <horms@verge.net.au>
11 ---
12 kexec/lzma.c | 33 ++++++++++++++++++++++-----------
13 kexec/zlib.c | 57 +++++++++++++++++++++++++++++++++++----------------------
14 2 files changed, 57 insertions(+), 33 deletions(-)
15
16 diff --git a/kexec/lzma.c b/kexec/lzma.c
17 index 939aeb3..5bfccb7 100644
18 --- a/kexec/lzma.c
19 +++ b/kexec/lzma.c
20 @@ -162,13 +162,16 @@ char *lzma_decompress_file(const char *filename, off_t *r_size)
21 off_t size, allocated;
22 ssize_t result;
23
24 - if (!filename) {
25 - *r_size = 0;
26 - return 0;
27 - }
28 + dbgprintf("Try LZMA decompression.\n");
29 +
30 + *r_size = 0;
31 + if (!filename)
32 + return NULL;
33 +
34 fp = lzopen(filename, "rb");
35 if (fp == 0) {
36 - die("Cannot open `%s'\n", filename);
37 + dbgprintf("Cannot open `%s'\n", filename);
38 + return NULL;
39 }
40 size = 0;
41 allocated = 65536;
42 @@ -183,17 +186,25 @@ char *lzma_decompress_file(const char *filename, off_t *r_size)
43 if ((errno == EINTR) || (errno == EAGAIN))
44 continue;
45
46 - die ("read on %s of %ld bytes failed\n",
47 - filename, (allocated - size) + 0UL);
48 + dbgprintf("%s: read on %s of %ld bytes failed\n",
49 + __func__, filename, (allocated - size) + 0UL);
50 + break;
51 }
52 size += result;
53 - } while(result > 0);
54 - result = lzclose(fp);
55 - if (result != LZMA_OK) {
56 - die ("Close of %s failed\n", filename);
57 + } while (result > 0);
58 +
59 + if (lzclose(fp) != LZMA_OK) {
60 + dbgprintf("%s: Close of %s failed\n", __func__, filename);
61 + goto fail;
62 }
63 + if (result < 0)
64 + goto fail;
65 +
66 *r_size = size;
67 return buf;
68 +fail:
69 + free(buf);
70 + return NULL;
71 }
72 #else
73 char *lzma_decompress_file(const char *UNUSED(filename), off_t *UNUSED(r_size))
74 diff --git a/kexec/zlib.c b/kexec/zlib.c
75 index d44df12..7170ac3 100644
76 --- a/kexec/zlib.c
77 +++ b/kexec/zlib.c
78 @@ -15,29 +15,39 @@
79 #include <ctype.h>
80 #include <zlib.h>
81
82 +static void _gzerror(gzFile fp, int *errnum, const char **errmsg)
83 +{
84 + *errmsg = gzerror(fp, errnum);
85 + if (*errnum == Z_ERRNO) {
86 + *errmsg = strerror(*errnum);
87 + }
88 +}
89 +
90 char *zlib_decompress_file(const char *filename, off_t *r_size)
91 {
92 gzFile fp;
93 int errnum;
94 const char *msg;
95 char *buf;
96 - off_t size, allocated;
97 + off_t size = 0, allocated;
98 ssize_t result;
99
100 + dbgprintf("Try gzip decompression.\n");
101 +
102 + *r_size = 0;
103 if (!filename) {
104 - *r_size = 0;
105 - return 0;
106 + return NULL;
107 }
108 fp = gzopen(filename, "rb");
109 if (fp == 0) {
110 - msg = gzerror(fp, &errnum);
111 - if (errnum == Z_ERRNO) {
112 - msg = strerror(errno);
113 - }
114 - fprintf(stderr, "Cannot open `%s': %s\n", filename, msg);
115 + _gzerror(fp, &errnum, &msg);
116 + dbgprintf("Cannot open `%s': %s\n", filename, msg);
117 + return NULL;
118 + }
119 + if (gzdirect(fp)) {
120 + /* It's not in gzip format */
121 return NULL;
122 }
123 - size = 0;
124 allocated = 65536;
125 buf = xmalloc(allocated);
126 do {
127 @@ -49,25 +59,28 @@ char *zlib_decompress_file(const char *filename, off_t *r_size)
128 if (result < 0) {
129 if ((errno == EINTR) || (errno == EAGAIN))
130 continue;
131 -
132 - msg = gzerror(fp, &errnum);
133 - if (errnum == Z_ERRNO) {
134 - msg = strerror(errno);
135 - }
136 - die ("read on %s of %ld bytes failed: %s\n",
137 - filename, (allocated - size) + 0UL, msg);
138 + _gzerror(fp, &errnum, &msg);
139 + dbgprintf("Read on %s of %ld bytes failed: %s\n",
140 + filename, (allocated - size) + 0UL, msg);
141 + size = 0;
142 + goto fail;
143 }
144 size += result;
145 } while(result > 0);
146 +
147 +fail:
148 result = gzclose(fp);
149 if (result != Z_OK) {
150 - msg = gzerror(fp, &errnum);
151 - if (errnum == Z_ERRNO) {
152 - msg = strerror(errno);
153 - }
154 - die ("Close of %s failed: %s\n", filename, msg);
155 + _gzerror(fp, &errnum, &msg);
156 + dbgprintf(" Close of %s failed: %s\n", filename, msg);
157 + }
158 +
159 + if (size > 0) {
160 + *r_size = size;
161 + } else {
162 + free(buf);
163 + buf = NULL;
164 }
165 - *r_size = size;
166 return buf;
167 }
168 #else
169 --
170 1.7.10.4
171