-
Notifications
You must be signed in to change notification settings - Fork 43
/
laps_dump.c
186 lines (159 loc) · 5.31 KB
/
laps_dump.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Dump files with each byte interpreted in several formats */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HELLO "Aldebaro Klautau - www.laps.ufpa.br - March 10, 2009"
#define SWAP_SHORT(Var) Var = *(short*) SwapEndian((void*)&Var, sizeof(short))
#define SWAP_INT(Var) Var = *(int*) SwapEndian((void*)&Var, sizeof(int))
#define SWAP_LONG(Var) Var = *(long*) SwapEndian((void*)&Var, sizeof(long))
#define SWAP_FLOAT(Var) Var = *(float*) SwapEndian((void*)&Var, sizeof(float))
#define SWAP_DOUBLE(Var) Var = *(double*) SwapEndian((void*)&Var, sizeof(double))
void *SwapEndian(void* Addr, const int Nb);
int main(int argc, char* argv[]) {
FILE *filePointer;
int counter=0;
if(argc!=3) {
printf("Usage: <input file> <swap|not>\n");
puts("Use 'swap' in the second argument to swap the bytes or 'not' for not swapping");
exit(1);
}
puts(HELLO);
puts("Each columns represents:");
puts("<byte counter> <decimal char> <hexa char> <(ASCII)> <short> <int> <long int> <float> <double>\n");
filePointer=fopen(argv[1],"rb");
if(filePointer==NULL) {
printf("Error opening file %s",argv[1]);
exit(1);
}
if (strcmp(argv[2],"swap")==0) {
counter = readDataSwappingBytes(filePointer);
} else {
counter = readDataWithoutSwapping(filePointer);
}
fclose(filePointer);
printf("\n\n%d bytes read from file %s\n",counter,argv[1]);
return 1;
}
int readDataWithoutSwapping(FILE *filePointer) {
int k;
unsigned char xc;
short xs;
int xi;
long int xl;
float xf;
double xd;
int counter=1;
do {
k=fread(&xc,sizeof(unsigned char),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(unsigned char), SEEK_CUR);
printf("%d %d %x (%c) ",counter,(short int)xc,(short int)xc,xc);
k=fread(&xs,sizeof(short),1,filePointer);
if(k != 1) { puts("End of file."); break;}
fseek(filePointer, - (int) sizeof(short), SEEK_CUR);
printf("%d ",xs);
k=fread(&xi,sizeof(int),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(int), SEEK_CUR);
printf("%d ",xi);
k=fread(&xl,sizeof(long int),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(long int), SEEK_CUR);
printf("%d ",xl);
k=fread(&xf,sizeof(float),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(float), SEEK_CUR);
printf("%e ",xf);
k=fread(&xd,sizeof(double),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(double), SEEK_CUR);
printf("%e\n",xd);
fseek(filePointer, sizeof(unsigned char), SEEK_CUR);
counter++;
} while(1);
return counter;
}
int readDataSwappingBytes(FILE *filePointer) {
int k;
unsigned char xc;
short xs;
int xi;
long int xl;
float xf;
double xd;
int counter=1;
do {
k=fread(&xc,sizeof(unsigned char),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(unsigned char), SEEK_CUR);
printf("%d %d %x (%c) ",counter,(short int)xc,(short int)xc,xc);
k=fread(&xs,sizeof(short),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(short), SEEK_CUR);
printf("%d ",SWAP_SHORT(xs));
k=fread(&xi,sizeof(int),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(int), SEEK_CUR);
printf("%d ",SWAP_INT(xi));
k=fread(&xl,sizeof(long int),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(long int), SEEK_CUR);
printf("%d ",SWAP_LONG(xl));
k=fread(&xf,sizeof(float),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(float), SEEK_CUR);
printf("%e ",SWAP_FLOAT(xf));
k=fread(&xd,sizeof(double),1,filePointer);
if(k != 1) break;
fseek(filePointer, - (int) sizeof(double), SEEK_CUR);
printf("%e\n",SWAP_DOUBLE(xd));
fseek(filePointer, sizeof(unsigned char), SEEK_CUR);
counter++;
} while(1);
return counter;
}
/******************************************************************************
FUNCTION: SwapEndian
PURPOSE: Swap the byte order of primitive types
EXAMPLE: float F=123.456; SWAP_FLOAT(F);
**********************'********************************************************/
void *SwapEndian(void* Addr, const int Nb) {
static char Swapped[16];
switch (Nb) {
case 2: Swapped[0]=*((char*)Addr+1);
Swapped[1]=*((char*)Addr );
break;
case 4: Swapped[0]=*((char*)Addr+3);
Swapped[1]=*((char*)Addr+2);
Swapped[2]=*((char*)Addr+1);
Swapped[3]=*((char*)Addr );
break;
case 8: Swapped[0]=*((char*)Addr+7);
Swapped[1]=*((char*)Addr+6);
Swapped[2]=*((char*)Addr+5);
Swapped[3]=*((char*)Addr+4);
Swapped[4]=*((char*)Addr+3);
Swapped[5]=*((char*)Addr+2);
Swapped[6]=*((char*)Addr+1);
Swapped[7]=*((char*)Addr );
break;
case 16:Swapped[0]=*((char*)Addr+15);
Swapped[1]=*((char*)Addr+14);
Swapped[2]=*((char*)Addr+13);
Swapped[3]=*((char*)Addr+12);
Swapped[4]=*((char*)Addr+11);
Swapped[5]=*((char*)Addr+10);
Swapped[6]=*((char*)Addr+9);
Swapped[7]=*((char*)Addr+8);
Swapped[8]=*((char*)Addr+7);
Swapped[9]=*((char*)Addr+6);
Swapped[10]=*((char*)Addr+5);
Swapped[11]=*((char*)Addr+4);
Swapped[12]=*((char*)Addr+3);
Swapped[13]=*((char*)Addr+2);
Swapped[14]=*((char*)Addr+1);
Swapped[15]=*((char*)Addr );
break;
}
return (void*)Swapped;
}