2 * Copyright (c) 2016 FUKAUMI Naoki <naobsd@gmail.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
19 #include "buffalo-lib.h"
21 #define DHP_HEADER_SIZE 20
23 static char *progname
;
29 fprintf(stderr
, "usage: %s <in> <out>\n", progname
);
34 main(int argc
, char *argv
[])
47 if ((in
= open(argv
[1], O_RDONLY
)) == -1)
48 err(EXIT_FAILURE
, "%s", argv
[1]);
50 if (fstat(in
, &in_st
) == -1)
51 err(EXIT_FAILURE
, "%s", argv
[1]);
53 size
= DHP_HEADER_SIZE
+ in_st
.st_size
;
55 if ((buf
= malloc(size
)) == NULL
)
56 err(EXIT_FAILURE
, "malloc");
58 memset(buf
, 0, DHP_HEADER_SIZE
);
63 buf
[0xc] = (size
>> 24) & 0xff;
64 buf
[0xd] = (size
>> 16) & 0xff;
65 buf
[0xe] = (size
>> 8) & 0xff;
66 buf
[0xf] = size
& 0xff;
68 read(in
, &buf
[DHP_HEADER_SIZE
], in_st
.st_size
);
71 crc
= buffalo_crc(buf
, size
);
72 buf
[0x10] = (crc
>> 24) & 0xff;
73 buf
[0x11] = (crc
>> 16) & 0xff;
74 buf
[0x12] = (crc
>> 8) & 0xff;
75 buf
[0x13] = crc
& 0xff;
77 if ((out
= open(argv
[2], O_WRONLY
| O_CREAT
| O_TRUNC
, 0644)) == -1)
78 err(EXIT_FAILURE
, "%s", argv
[2]);
79 write(out
, buf
, size
);