1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2011 Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include <sys/types.h>
30 /* A simple replacement program for (
31 * dd if=$file1 of=$file2 bs=1 skip=$offset count=$size
33 * Written because byte-size operations with dd are unbearably slow.
38 fprintf(stderr
, "Usage: extract_section <romfile> <outfile> <offset> <byte count>\n");
42 void die(const char* fmt
, ...)
46 vfprintf(stderr
, fmt
, ap
);
51 int main(int argc
, const char* argv
[])
57 ssize_t size
= atol(argv
[4]);
58 long skip
= atol(argv
[3]);
61 die("invalid byte count\n");
63 ifd
= open(argv
[1], O_RDONLY
);
65 die("Could not open %s for reading!\n", argv
[1]);
67 ofd
= open(argv
[2], O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
69 die("Could not create %s\n", argv
[2]);
71 void *buf
= malloc(size
);
72 if (!buf
) die("OOM\n");
74 lseek(ifd
, skip
, SEEK_SET
);
75 lseek(ofd
, 0, SEEK_SET
);
76 if (read(ifd
, buf
, size
) != size
)
78 if (write(ofd
, buf
, size
) != size
)
79 die("write failed\n");