* zlib compressed case: * Build the required header and follow it with the compressed image. Detect * whether the input is an elf image, and if so, compress only the progbits. * * header * 0 +------+-------+-------+-------+ * | | | | | * +----------------------+-------+ * | | size | | | * +----------------------+-------+ *
| 157 | * n +------------------------------+ |
| 158 | */ |
| 159 | static void do_process(char *outf, char *inf, size_t max_size, bool do_compress) |
| 160 | { |
| 161 | int out_fd, in_fd; |
| 162 | struct buffer inbf, outbf; |
| 163 | int err; |
| 164 | |
| 165 | in_fd = do_file(inf, &inbf.size, O_RDONLY); |
| 166 | if (in_fd < 0) { |
| 167 | printf("\tError opening input file %s\n", inf); |
| 168 | err = 1; |
| 169 | goto out; |
| 170 | } |
| 171 | |
| 172 | out_fd = do_file(outf, 0, O_CREAT | O_WRONLY); |
| 173 | if (out_fd < 0) { |
| 174 | printf("\tError opening output file %s\n", outf); |
| 175 | err = 1; |
| 176 | goto out_close_in; |
| 177 | } |
| 178 | |
| 179 | inbf.data = calloc(inbf.size, 1); |
| 180 | if (!inbf.data) { |
| 181 | printf("\tError allocating 0x%zx bytes for input buffer\n", inbf.size); |
| 182 | err = 1; |
| 183 | goto out_close_out; |
| 184 | } |
| 185 | |
| 186 | if (read(in_fd, inbf.data, inbf.size) != (ssize_t)inbf.size) { |
| 187 | printf("\tError reading input file %s\n", inf); |
| 188 | err = 1; |
| 189 | goto out_free_in; |
| 190 | } |
| 191 | |
| 192 | if (iself(inbf.data)) { |
| 193 | if (convert_elf(&inbf)) { |
| 194 | err = 1; |
| 195 | goto out_free_in; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (max_size && inbf.size > max_size) { |
| 200 | printf("\tError - size (%zx) exceeds specified max_size (%zx)\n", |
| 201 | inbf.size, max_size); |
| 202 | err = 1; |
| 203 | goto out_free_in; |
| 204 | } |
| 205 | |
| 206 | if (!do_compress) { |
| 207 | /* |
| 208 | * When no zlib compression is being used there's also no 256 byte header. |
| 209 | * Simply copy the input to the output. |
| 210 | */ |
| 211 | err = 0; |
| 212 | if (write(out_fd, inbf.data, inbf.size) != (ssize_t)(inbf.size)) { |
| 213 | printf("\tError writing to %s\n", outf); |
| 214 | err = 1; |
| 215 | } |
| 216 | goto out_free_in; |