static const char *title =\
"Broadcom Consumer Router Firmware Header Editor"
;
-static const float VERSION = 1.2f;
+static const float VERSION = 1.3f;
static const char *copyright = \
"Copyright 2015-2016 TJ <hacker@iam.tj>\n"
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
+#include <stdint.h>
+#include "heap_reap.h"
static unsigned int MESSAGE_SIZE = 1024;
static unsigned int TAG_VER_LEN = 4;
if (usage) pr_usage(usage);
+ heap_and_reap(NULL-1, 0, 0);
exit(EXIT_FAILURE);
}
main(int argc, char **argv)
{
unsigned int arg, crc;
- FILE *fdata = NULL;
char *filename = NULL;
int fd, fd_mode;
unsigned char *buffer = NULL;
fprintf(stderr, "%s\nVersion: %0.2f\n%s\n", title, VERSION, copyright);
- if ((model_id = calloc(MODEL_ID_LEN, 1)) == NULL) {
+ if ((model_id = heap_and_reap(NULL, MODEL_ID_LEN, 1)) == NULL) {
pr_error_exit(0, "Unable to allocate memory (%d bytes)\n", MODEL_ID_LEN);
}
memcpy(model_id, match_model_id, MODEL_ID_LEN);
break;
case 'h': // help
pr_usage(1);
- exit(0);
+ goto end;
}
} else {
pr_error_exit(0, "cannot read data from stdin; provide a filename");
if (filename) {
if ((fd = open(filename, fd_mode)) > 0) {
- if ( (buffer = calloc(header_len + sizeof(crc), 1)) != NULL) {
+ if ( (buffer = heap_and_reap(NULL, header_len + sizeof(crc), 1)) != NULL) {
ssize_t qty;
if ( (qty = read(fd, buffer, header_len + sizeof(crc))) < header_len) {
if (!opt_quiet)
memcpy(buffer + header_crc_offset, &tmp, sizeof(tmp));
if (!opt_simulate) {
+ ssize_t write_len = header_len > header_crc_offset + sizeof(tmp) ? header_len : header_crc_offset + sizeof(tmp);
lseek(fd, 0, SEEK_SET);
- write(fd, buffer, header_len);
+ write(fd, buffer, write_len);
}
printf( format_spec,
);
}
- free(buffer);
+ heap_and_reap(buffer, 0, 0);
close(fd);
} else {
fprintf(stderr, "Unable to open for %s (%s)\n", opt_write ? "writing" : "reading" , filename );
pr_usage(0);
}
+end:
+ heap_and_reap(NULL-1, 0, 0);
return 0;
}