se505-buildsystem/tools/untrx.c
2007-06-15 15:11:23 +02:00

91 lines
1.7 KiB
C

/*
* extract file from trx file
*
* (c) 01/2005 Olaf Rempel <razzor at kopf minus tisch dot de>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include "trxhdr.h"
#define BUFSIZE 32768
int main(int argc, char *argv[]) {
struct trx_header trx;
int cnt;
FILE *fp;
char *buf;
if (argc <= 1) {
printf("USAGE: untrx file.trx > file\n");
exit(-1);
}
// open file
if ((fp = fopen(argv[1], "r")) < 0) {
perror(argv[1]);
exit(-1);
}
// read header
if ((cnt = fread(&trx, 1, sizeof(struct trx_header), fp)) < 0) {
perror(argv[1]);
fclose(fp);
exit(-1);
} else if (cnt < sizeof(struct trx_header)) {
fprintf(stderr, "Truncated file: %s\n", argv[1]);
fclose(fp);
exit(-1);
} else if (trx.magic == TRX_MAGIC) {
if (trx.len > TRX_MAX_LEN || trx.len < sizeof(struct trx_header)) {
fprintf(stderr, "Bad TRX Header: %s\n", argv[1]);
fclose(fp);
exit(-1);
}
} else if (trx.magic == PMON_MAGIC) {
if (trx.len > PMON_MAX_LEN || trx.len < sizeof(struct trx_header)) {
fprintf(stderr, "Bad PMON Header: %s\n", argv[1]);
fclose(fp);
exit(-1);
}
} else if (trx.magic == NVAR_MAGIC) {
if (trx.len > NVRAM_MAX_LEN || trx.len < sizeof(struct trx_header)) {
fprintf(stderr, "Bad NVRAM Header: %s\n", argv[1]);
fclose(fp);
exit(-1);
}
} else {
fprintf(stderr, "Unknown Header Type: %s\n", argv[1]);
fclose(fp);
exit(-1);
}
trx.len -= sizeof(struct trx_header);
if ((buf = malloc(BUFSIZE)) == NULL) {
perror("malloc");
exit(-1);
}
while (trx.len > 0 && !feof(fp)) {
cnt = fread(buf, 1, BUFSIZE, fp);
write(1, buf, cnt);
trx.len -= cnt;
}
free(buf);
fclose(fp);
return 0;
}