forked from Siudya/Nanhu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupby-4byte.c
69 lines (59 loc) · 2.13 KB
/
groupby-4byte.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
* Copyright (c) 2020-2021 Peng Cheng Laboratory
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
int main(int argc, char *argv[]) {
assert(argc == 3);
FILE *in = fopen(argv[1], "rb");
assert(in != NULL);
FILE *out = fopen(argv[2], "w");
assert(out != NULL);
char line[128];
uint32_t addr;
union {
uint8_t _8[4];
uint32_t _32;
} data[4];
while (fgets(line, 128, in) != NULL) {
if (line[0] == '@') {
sscanf(line + 1, "%x", &addr);
assert(addr % 4 == 0);
fprintf(out, "@%08x\n", addr / 4);
}
else {
int ret = sscanf(line,
"%hhx%hhx%hhx%hhx"
"%hhx%hhx%hhx%hhx"
"%hhx%hhx%hhx%hhx"
"%hhx%hhx%hhx%hhx",
&data[0]._8[0], &data[0]._8[1], &data[0]._8[2], &data[0]._8[3],
&data[1]._8[0], &data[1]._8[1], &data[1]._8[2], &data[1]._8[3],
&data[2]._8[0], &data[2]._8[1], &data[2]._8[2], &data[2]._8[3],
&data[3]._8[0], &data[3]._8[1], &data[3]._8[2], &data[3]._8[3]);
assert(ret == EOF || ret == 4 || ret == 8 || ret == 12 || ret == 16);
if (ret == EOF) continue;
if (ret >= 4) fprintf(out, "%08x ", data[0]._32);
if (ret >= 8) fprintf(out, "%08x ", data[1]._32);
if (ret >= 12) fprintf(out, "%08x ", data[2]._32);
if (ret >= 16) fprintf(out, "%08x ", data[3]._32);
fprintf(out, "\n");
}
}
fclose(in);
fclose(out);
return 0;
}