forked from ansell/camp2ascii
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes_processing.c
418 lines (351 loc) · 12.9 KB
/
types_processing.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* Copyright SLF/WSL, 03/2007 - www.slf.ch *
**************************************************************************/
/* Here, we put all functions related to the details of reading a given data type */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> /* for time calculations */
#include <math.h>
#include <inttypes.h> /* C99 portable data types */
#include "limits.h"
#include "types_processing.h"
#include <stdint.h>
/* Functions for swapping byte order. */
/* Inspired from http://www.gamedev.net/reference/articles/article2091.asp (GPL)*/
/* Itself inspired by Quake2 source code */
int16_t ReadInt2(frame_structure *frame) {
/* This function reads an int coded on 2 bytes */
/* It also updates the char index to point to the next byte */
int16_t result;
#ifdef BIGENDIAN
result=(int16_t)((frame->raw[frame->cindex+0] << 8) + frame->raw[frame->cindex+1]);
#else
result=(int16_t)((frame->raw[frame->cindex+1] << 8) + frame->raw[frame->cindex+0]);
#endif
frame->cindex += 2;
return result;
}
uint16_t ReadUInt2(frame_structure *frame) {
/* This function reads an int coded on 2 bytes */
/* It also updates the char index to point to the next byte */
uint16_t result;
#ifdef BIGENDIAN
result=(uint16_t)((frame->raw[frame->cindex+0] << 8) + frame->raw[frame->cindex+1]);
#else
result=(uint16_t)((frame->raw[frame->cindex+1] << 8) + frame->raw[frame->cindex+0]);
#endif
frame->cindex += 2;
return result;
}
int16_t ReadInt2Swap(frame_structure *frame) {
/* This function reads an int coded on 2 bytes */
/* It also updates the char index to point to the next byte */
int16_t result;
#ifndef BIGENDIAN
result=(int16_t)((frame->raw[frame->cindex+0] << 8) + frame->raw[frame->cindex+1]);
#else
result=(int16_t)((frame->raw[frame->cindex+1] << 8) + frame->raw[frame->cindex+0]);
#endif
frame->cindex += 2;
return result;
}
uint16_t ReadUInt2Swap(frame_structure *frame) {
/* This function reads an int coded on 2 bytes */
/* It also updates the char index to point to the next byte */
uint16_t result;
#ifndef BIGENDIAN
result=(uint16_t)((frame->raw[frame->cindex+0] << 8) + frame->raw[frame->cindex+1]);
#else
result=(uint16_t)((frame->raw[frame->cindex+1] << 8) + frame->raw[frame->cindex+0]);
#endif
frame->cindex += 2;
return result;
}
int32_t ReadInt4(frame_structure *frame) {
/* This function reads an int coded on 4 bytes */
/* It also updates the char index to point to the next byte */
int32_t result;
#ifdef BIGENDIAN
result=(int32_t)( frame->raw[frame->cindex+3] + (frame->raw[frame->cindex+2] << 8) + (frame->raw[frame->cindex+1] << 16) + (frame->raw[frame->cindex+0] << 24) );
#else
result=(int32_t)( frame->raw[frame->cindex+0] + (frame->raw[frame->cindex+1] << 8) + (frame->raw[frame->cindex+2] << 16) + (frame->raw[frame->cindex+3] << 24) );
#endif
frame->cindex += 4;
return result;
}
uint32_t ReadUInt4(frame_structure *frame) {
/* This function reads an int coded on 4 bytes */
/* It also updates the char index to point to the next byte */
uint32_t result;
#ifdef BIGENDIAN
result=(uint32_t)( frame->raw[frame->cindex+3] + (frame->raw[frame->cindex+2] << 8) + (frame->raw[frame->cindex+1] << 16) + (frame->raw[frame->cindex+0] << 24) );
#else
result=(uint32_t)( frame->raw[frame->cindex+0] + (frame->raw[frame->cindex+1] << 8) + (frame->raw[frame->cindex+2] << 16) + (frame->raw[frame->cindex+3] << 24) );
#endif
frame->cindex += 4;
return result;
}
int32_t ReadInt4Swap(frame_structure *frame) {
/* This function reads an int coded on 4 bytes */
/* It also updates the char index to point to the next byte */
int32_t result;
#ifdef BIGENDIAN
result=(int32_t)( frame->raw[frame->cindex+0] + (frame->raw[frame->cindex+1] << 8) + (frame->raw[frame->cindex+2] << 16) + (frame->raw[frame->cindex+3] << 24) );
#else
result=(int32_t)( frame->raw[frame->cindex+3] + (frame->raw[frame->cindex+2] << 8) + (frame->raw[frame->cindex+1] << 16) + (frame->raw[frame->cindex+0] << 24) );
#endif
frame->cindex += 4;
return result;
}
uint32_t ReadUInt4Swap(frame_structure *frame) {
/* This function reads an int coded on 4 bytes */
/* It also updates the char index to point to the next byte */
uint32_t result;
#ifdef BIGENDIAN
result=(uint32_t)( frame->raw[frame->cindex+0] + (frame->raw[frame->cindex+1] << 8) + (frame->raw[frame->cindex+2] << 16) + (frame->raw[frame->cindex+3] << 24) );
#else
result=(uint32_t)( frame->raw[frame->cindex+3] + (frame->raw[frame->cindex+2] << 8) + (frame->raw[frame->cindex+1] << 16) + (frame->raw[frame->cindex+0] << 24) );
#endif
frame->cindex += 4;
return result;
}
float32_t ReadFloat4(frame_structure *frame) {
/* This function reads a float coded on 4 bytes */
/* It also updates the char index to point to the next byte */
union
{
float32_t f;
uint8_t b[4];
} dat2;
#ifndef BIGENDIAN
dat2.b[0] = frame->raw[frame->cindex];
dat2.b[1] = frame->raw[frame->cindex+1];
dat2.b[2] = frame->raw[frame->cindex+2];
dat2.b[3] = frame->raw[frame->cindex+3];
#else
dat2.b[0] = frame->raw[frame->cindex+3];
dat2.b[1] = frame->raw[frame->cindex+2];
dat2.b[2] = frame->raw[frame->cindex+1];
dat2.b[3] = frame->raw[frame->cindex];
#endif
frame->cindex += 4;
return dat2.f;
}
float32_t ReadFloat4Swap(frame_structure *frame) {
/* This function reads a float coded on 4 bytes */
/* It also updates the char index to point to the next byte */
union
{
float32_t f;
uint8_t b[4];
} dat2;
#ifdef BIGENDIAN
dat2.b[0] = frame->raw[frame->cindex];
dat2.b[1] = frame->raw[frame->cindex+1];
dat2.b[2] = frame->raw[frame->cindex+2];
dat2.b[3] = frame->raw[frame->cindex+3];
#else
dat2.b[0] = frame->raw[frame->cindex+3];
dat2.b[1] = frame->raw[frame->cindex+2];
dat2.b[2] = frame->raw[frame->cindex+1];
dat2.b[3] = frame->raw[frame->cindex];
#endif
frame->cindex += 4;
return dat2.f;
}
/////////////////////////////////////////////////////////////////////////////////////////
//////////////////// higher level functions, adapted to Cambell types ///////////////////
/////////////////////////////////////////////////////////////////////////////////////////
// SPECIAL FLOATING POINTS
int Read_FP2(frame_structure *frame,const config_structure *config) {
//2 bytes float
SIresult=(int16_t)ReadInt2Swap(frame);
sign = ((0x8000 & SIresult) >> 15 );
exponent = ((0x6000 & SIresult) >> 13 );
mantissa = ((0x1FFF & SIresult) );
switch(exponent) {
case 0: Fresult=(float)mantissa;
break;
case 1: Fresult=(float)mantissa*1e-1;
break;
case 2: Fresult=(float)mantissa*1e-2;
break;
default: Fresult=(float)mantissa*1e-3;
}
if(Fresult>=frame->fp2_nan || isnan(Fresult))
fprintf(config->output,config->NANs);
else {
if(sign ==0)
fprintf(config->output,config->fp2_format,Fresult);
else
fprintf(config->output,config->fp2_format,-Fresult);
}
return EXIT_SUCCESS;
}
int Read_FP4(frame_structure *frame,const config_structure *config) {
//in progress... but it should work! see Appendix C of CR10X manual
Iresult=ReadInt4(frame);
sign = ((0x80000000 & Iresult) >> 31 );
exponent = ((0x7F000000 & Iresult) >> 24 );//isn't it 23 instead?
mantissa = ((0x00FFFFFF & Iresult) );
Fresult=(float)mantissa/16777216.0*(float)pow(2.0,(double)(exponent-64));//TODO: optimize...(exponent is INT)
#ifdef DEBUG
fprintf(stderr,"0x%0x->",Iresult);
fprintf(stderr,"(%d)_%d*10^(%d)->%g\n",sign,mantissa,exponent,Fresult);
#endif
fprintf(config->output,"\"FP4\"");
/*if(Fresult>=frame->fp4_nan || isnan(Fresult))
fprintf(config->output,config->NANs,config->separator);
else {
if(sign ==0)
fprintf(config->output,config->fp4_format,config->separator,Fresult);
else
fprintf(config->output,config->fp4_format,config->separator,-Fresult);
}*/
return EXIT_SUCCESS;
}
//IEEE4 FLOATING POINTS
int Read_IEEE4(frame_structure *frame,const config_structure *config) {
//IEEE4 & L -> Little endian LSB
Fresult=ReadFloat4(frame);
if(isnan(Fresult))
fprintf(config->output,config->NANs);
else
fprintf(config->output,config->floats_format,Fresult);
return EXIT_SUCCESS;
}
int Read_IEEE4B(frame_structure *frame,const config_structure *config) {
//IEEE4, Big endian version MSB
Fresult=ReadFloat4Swap(frame);
if(isnan(Fresult))
fprintf(config->output,config->NANs);
else
fprintf(config->output,config->floats_format,Fresult);
return EXIT_SUCCESS;
}
//INTEGERS
int Read_USHORT(frame_structure *frame,const config_structure *config) {
//2 bytes unsigned int LSB
Iresult=(int32_t)ReadUInt2(frame);
if(Iresult>=frame->uint2_nan) // || isnan(Iresult))
fprintf(config->output,config->NANs);
else {
fprintf(config->output,config->ints_format,Iresult);
}
return EXIT_SUCCESS;
}
int Read_SHORT(frame_structure *frame,const config_structure *config) {
//2 bytes signed int LSB
Iresult=(int32_t)ReadInt2(frame);
if(Iresult>=frame->uint2_nan) //|| isnan(Iresult)) // Iresult isn't a float, so this code is faulty
fprintf(config->output,config->NANs);
else {
fprintf(config->output,config->ints_format,Iresult);
}
return EXIT_SUCCESS;
}
int Read_UINT2(frame_structure *frame,const config_structure *config) {
//2 bytes unsigned int MSB
Iresult=(int32_t)ReadUInt2Swap(frame);
if(Iresult>=frame->uint2_nan) // || isnan(Iresult)) // Iresult isn't a float, so this code is faulty
fprintf(config->output,config->NANs);
else {
fprintf(config->output,config->ints_format,Iresult);
}
return EXIT_SUCCESS;
}
int Read_INT2(frame_structure *frame,const config_structure *config) {
//2 bytes signed int MSB
Iresult=(int32_t)ReadInt2Swap(frame);
if(SIresult>=frame->uint2_nan) // || isnan(Iresult))
fprintf(config->output,config->NANs);
else {
fprintf(config->output,config->ints_format,Iresult);
}
return EXIT_SUCCESS;
}
int Read_UINT4(frame_structure *frame,const config_structure *config) {
//4 bytes unsigned int MSB
fprintf(config->output,config->ints_format,ReadUInt4Swap(frame));
return EXIT_SUCCESS;
}
int Read_INT4(frame_structure *frame,const config_structure *config) {
//4 bytes signed int MSB
fprintf(config->output,config->ints_format,ReadInt4Swap(frame));
return EXIT_SUCCESS;
}
int Read_ULONG(frame_structure *frame,const config_structure *config) {
//4 bytes unsigned int LSB
fprintf(config->output,config->ints_format,ReadUInt4(frame));
return EXIT_SUCCESS;
}
int Read_LONG(frame_structure *frame,const config_structure *config) {
//4 bytes signed int LSB
fprintf(config->output,config->ints_format,ReadInt4(frame));
return EXIT_SUCCESS;
}
//BOOLEANS
int Read_BOOL(frame_structure *frame,const config_structure *config) {
//1 byte boolean: either FF or 00
if((int16_t)(frame->raw[frame->cindex]) == 0)
fprintf(config->output,config->bool_false);
else
fprintf(config->output,config->bool_true);
frame->cindex += 1;
return EXIT_SUCCESS;
}
int Read_BOOL2(frame_structure *frame,const config_structure *config) {
//2 bytes boolean: either FFFF or 0000 - TODO: check
if(ReadInt2Swap(frame)==0)
fprintf(config->output,config->bool_false);
else
fprintf(config->output,config->bool_true);
return EXIT_SUCCESS;
}
int Read_BOOL4(frame_structure *frame,const config_structure *config) {
//4 bytes boolean: either FFFFFFFF or 00000000
if(ReadInt4Swap(frame)==0)
fprintf(config->output,config->bool_false);
else
fprintf(config->output,config->bool_true);
return EXIT_SUCCESS;
}
//TIMESTAMPS
int Read_NSec(frame_structure *frame,const config_structure *config) {
//2x4 bytes (2xUINT4), timestamps and nanoseconds MSB
time_offset = ReadUInt4Swap(frame)+TO_EPOCH;
strftime(time_str,MAX_FIELD,config->time_format,gmtime(&time_offset));
//we print as {separator}{timestamp}.{nanoseconds}
fprintf(config->output,config->nsec_format,time_str,ReadUInt4Swap(frame));
return EXIT_SUCCESS;
}
int Read_SecNano(frame_structure *frame,const config_structure *config) {
//2x4 bytes (2xULONG), timestamps and nanoseconds LSB
time_offset = ReadUInt4(frame)+TO_EPOCH;
strftime(time_str,MAX_FIELD,config->time_format,gmtime(&time_offset));
//we print as {separator}{timestamp}.{nanoseconds}
fprintf(config->output,config->nsec_format,time_str,ReadUInt4(frame));
return EXIT_SUCCESS;
}
//ASCII
int Read_ASCII(frame_structure *frame,const config_structure *config) {
//this is for the ASCII strings, each char on 1 byte
count=frame->field_options[field_index];
fprintf(config->output,config->strings_beg);
for(;count>0;count--) {
Cresult=frame->raw[frame->cindex++];
if(Cresult == 0) {//the end of the string has been reached
frame->cindex += count-1;
count=0;
} else
fprintf(config->output,"%c",Cresult);
}
fprintf(config->output,config->strings_end);
return EXIT_SUCCESS;
}