1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 FUKAUMI Naoki <naobsd@gmail.com>
15 #include "buffalo-lib.h"
17 #define DHP_HEADER_SIZE 20
19 static char *progname
;
25 fprintf(stderr
, "usage: %s <in> <out>\n", progname
);
30 main(int argc
, char *argv
[])
43 if ((in
= open(argv
[1], O_RDONLY
)) == -1)
44 err(EXIT_FAILURE
, "%s", argv
[1]);
46 if (fstat(in
, &in_st
) == -1)
47 err(EXIT_FAILURE
, "%s", argv
[1]);
49 size
= DHP_HEADER_SIZE
+ in_st
.st_size
;
51 if ((buf
= malloc(size
)) == NULL
)
52 err(EXIT_FAILURE
, "malloc");
54 memset(buf
, 0, DHP_HEADER_SIZE
);
59 buf
[0xc] = (size
>> 24) & 0xff;
60 buf
[0xd] = (size
>> 16) & 0xff;
61 buf
[0xe] = (size
>> 8) & 0xff;
62 buf
[0xf] = size
& 0xff;
64 read(in
, &buf
[DHP_HEADER_SIZE
], in_st
.st_size
);
67 crc
= buffalo_crc(buf
, size
);
68 buf
[0x10] = (crc
>> 24) & 0xff;
69 buf
[0x11] = (crc
>> 16) & 0xff;
70 buf
[0x12] = (crc
>> 8) & 0xff;
71 buf
[0x13] = crc
& 0xff;
73 if ((out
= open(argv
[2], O_WRONLY
| O_CREAT
| O_TRUNC
, 0644)) == -1)
74 err(EXIT_FAILURE
, "%s", argv
[2]);
75 write(out
, buf
, size
);