-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataDecode.c
108 lines (92 loc) · 1.88 KB
/
dataDecode.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#define _GNU_SOURCE
#define __USE_GNU
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <byteswap.h>
#include <string.h>
#ifndef DATADECODE
#define DATADECODE fa250DataDecode
#endif
char progName[255];
void usage();
int
main(int argc, char *argv[])
{
int iarg = 1;
int byteswap = 0;
FILE *f;
uint32_t data = 0;
char *user_input = NULL;
size_t input_size = 0;
size_t line_len = 0;
int bytes_read, bytes_skip = 0;
/* Evaluate the command line arguments */
strncpy((char *) &progName, argv[0], 255);
if(argc > 1)
{
/* help */
if ((strcmp(argv[iarg],"-h") == 0) || (strcmp(argv[iarg],"--help") == 0))
{
usage();
return 0;
}
/* byteswapping specified */
if ((strcmp(argv[iarg],"-b") == 0) || (strcmp(argv[iarg],"--byteswap") == 0))
{
byteswap = 1;
++iarg;
}
/* Filename included */
if (iarg == argc-1)
{
f = fopen(argv[iarg], "r");
if(f)
{
printf("\n");
while(fscanf(f, "%x", &data) > 0)
{
if(byteswap)
data = bswap_32(data);
DATADECODE(data);
}
}
else
perror(argv[1]);
return 0;
}
}
/* User data entry */
printf("\n");
while(1)
{
line_len = getline(&user_input, &input_size, stdin);
if(line_len > 1)
{
while(sscanf(user_input + bytes_skip, "%x%n", &data,
&bytes_read) == 1)
{
if(byteswap)
data = bswap_32(data);
DATADECODE(data);
bytes_skip += bytes_read;
}
bytes_skip = 0;
}
else
break;
}
return 0;
}
void
usage()
{
printf("\nUsage: ");
printf("%s [OPTION] [FILENAME]\n", progName);
printf("If [FILENAME] is not specified, user input is accepted.\n");
printf("\n");
printf("OPTIONs:\n");
printf(" -h, --help This help page.\n");
printf(" -b, --byteswap Byteswap (32bit) before decoding.\n");
printf("\n\n");
}