-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfix.cpp
More file actions
executable file
·450 lines (391 loc) · 13.4 KB
/
bfix.cpp
File metadata and controls
executable file
·450 lines (391 loc) · 13.4 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
*======================================================================================================================
* File: bfix.c
*
* Description:
* These routines are used to insert and extract bit fields from an array of characters pointed to by an unsigned
* char* pointer.
*
*======================================================================================================================
*/
/*
* Notes:
* a. in the following notes any annotation of the form n/m means n for 32 bit systems and m for 64 bit systems.
* operation on 32 or 64 bit systems should be transparent.
*
* b. normally, no checking for reasonable argument values is done. if BFIX_DEBUG is defined then reasonable
* values for bit_offset and bit_len are checked. a reasonable value for bit_len is dependent on the starting
* bit_offset value as explained in note b. a check if both BFIX_BIG_ENDIAN and BFIX_LITTLE_ENDIAN are
* defined at the same time is made. see the code for BFIX_DEBUG inclusions.
*
* c. bit_len should be <=32/64 to 25/57. it depends on the value of bit_offset. the method always uses a memmove()
* of 4/8 bytes to a long temporary storage in which logical operations can be performed. this means that the
* bit_len can be at most 4/8 bytes, but in a case in which the start bit is not at the beginning of a byte,
* then the bit_len can only extend until the end of the 4/8'th byte. if the start bit is the last bit of a
* byte this will limit bit_len to 25/57 bits - the last bit of the first byte plus the next 3/7 bytes.
* a bit_len of zero is ok - with bfx() returning 0 and bfi() inserting nothing.
*
* d. and bit_offset+bit_len should not overrun the array.
*
* e. value should not be too long to fit into the bit field. if it is, the high order bits in front of the low
* order bit_len bits will be truncated.
*
* f. all bit_len bits will be set and no bit outside the bit field will be changed.
*
* g. value may be negative and prefix 2,s complement sign bits are truncated to fit into bit_len bits.
*
* h. 4/8 bytes are always read from the unsigned char array, modified and then written back. this means that if
* you set the last bit of the array, then the next 3/7 bytes will be read and written back, thus seemingly
* overrunning the array. this may or may not be a problem. if the 4/8 bytes does not overrun the array
* then no bits beyond the end of the array will be changed. if the 4/8 bytes does overrun the array
* some provision must be made to deal with this possibility. the array could be padded by 3/7 extra bytes.
*
* i. there are three ways to handle endianness. by default endianness is determined at run time. this, however,
* introduces some run time penalty. if BFIX_BIG_ENDIAN or BFIX_LITTLE_ENDIAN(but not both) is defined in
* bfix.h then that particular behavior will be compiled in.
*/
/*
*----------------------------------------------------------------------------------------------------------------------
* include files
*----------------------------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "bfix.h"
/*
*----------------------------------------------------------------------------------------------------------------------
* constants
*----------------------------------------------------------------------------------------------------------------------
*/
/* comment in one of these for compiled in endianness - default to run time endian check */
/* #define BFIX_BIG_ENDIAN */
/* #define BFIX_LITTLE_ENDIAN */
/*
*----------------------------------------------------------------------------------------------------------------------
* data types
*----------------------------------------------------------------------------------------------------------------------
*/
/*
*----------------------------------------------------------------------------------------------------------------------
* global variables
*----------------------------------------------------------------------------------------------------------------------
*/
/*
*----------------------------------------------------------------------------------------------------------------------
* local function prototypes
*----------------------------------------------------------------------------------------------------------------------
*/
/*
*----------------------------------------------------------------------------------------------------------------------
* functions
*----------------------------------------------------------------------------------------------------------------------
*/
/*
*----------------------------------------------------------------------------------------------------------------------
* bfi:
* bit field insertion
*
* Arguments:
* Bit field insert:
* bfi(cptr, bit_offset, bit_len, value);
*
* cptr - pointer to unsigned char array
* bit_offset - bit offset(starting from 1) from the start of
* the char array
* bit_len - bit length of field to insert
* value - value to be inserted
*
* Returns:
* None if !BFIX_DEBUG
* int 1 if fail and 0 on success
*----------------------------------------------------------------------------------------------------------------------
*/
#ifdef BFIX_DEBUG
int
#else
void
#endif
bfi(
unsigned char *cptr,
unsigned long bit_offset,
unsigned long bit_len,
long value)
{
/* machine dependencies */
const unsigned int BITS_PER_BYTE = 8;
unsigned int BYTES_PER_LONG;
unsigned int BITS_PER_LONG;
unsigned long l;
#if !defined(BFIX_BIG_ENDIAN) && !defined(BFIX_LITTLE_ENDIAN)
unsigned long m;
#endif
#if !defined(BFIX_BIG_ENDIAN)
unsigned int i, j, size;
unsigned char* c = (unsigned char*)&l;
unsigned char tmp;
#endif
unsigned long mask;
unsigned long byte_offset;
unsigned long pre_shift;
unsigned long post_shift;
BYTES_PER_LONG = sizeof(unsigned long);
BITS_PER_LONG = BYTES_PER_LONG * BITS_PER_BYTE;
for (i=0 ; i<BYTES_PER_LONG ; i++)
{
((unsigned char *)&mask)[i] = 0xff;
}
#ifdef BFIX_DEBUG
#if defined(BFIX_BIG_ENDIAN) && defined(BFIX_LITTLE_ENDIAN)
#error bfi(): Both BFIX_BIG_ENDIAN and BFIX_LITTLE_ENDIAN defined at the same time.
#endif
if (bit_offset < 1)
{
fprintf(stderr, "bfi: arg #2, bit_offset = %ld is < 1.\n", bit_offset);
return 1;
}
if (bit_len < 0)
{
fprintf(stderr, "bfi: arg #3, bit_len = %ld is < 0.\n", bit_len);
return 1;
}
#endif
/*
* calculate byte offset(first byte=0) of start of
* BYTES_PER_LONG bytes containing bit field
*/
byte_offset = (bit_offset-1)/BITS_PER_BYTE;
/*
* calculate how many bits to shift bit field left
* to clear bits above bit field
*/
pre_shift = bit_offset - byte_offset*BITS_PER_BYTE - 1;
#ifdef BFIX_DEBUG
if (bit_len > (BITS_PER_LONG-pre_shift))
{
fprintf(stderr, "bfi: arg #3, bit_len = %ld to long.\n", bit_len);
return 1;
}
#endif
/*
* calculate how many bits to shift bit field left
* to clear bits below bit field
*/
post_shift = BITS_PER_LONG - bit_len - pre_shift;
/*
* move bit field into position over bits to set in l
* corresponding to correct bits in cptr
*/
value <<= post_shift;
/* zero out mask bits after bit field */
mask <<= post_shift;
/* zero out mask bits before bit field */
mask <<= pre_shift;
mask >>= pre_shift;
/* zero out value bits before and after bit field */
value &= mask;
/* move BYTES_PER_LONG bytes to tmp storage */
memmove((unsigned char *)&l, &cptr[byte_offset], BYTES_PER_LONG);
#if defined(BFIX_BIG_ENDIAN)
#elif defined(BFIX_LITTLE_ENDIAN)
size = sizeof(l);
for (i=0;i < size/2; i++)
{
j = size - i - 1;
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
#else
m = l;
l = 1;
if (c[0])
{
/* little endian */
l = m;
size = sizeof(l);
for (i=0;i < size/2; i++)
{
j = size - i - 1;
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
for (i=0;i < size; i++)
{
}
}
else
{
/* big endian */
l = m;
}
#endif
/* zero out bit field bits and then or value bits into them */
l = (l & (~mask)) | value;
#if defined(BFIX_BIG_ENDIAN)
#elif defined(BFIX_LITTLE_ENDIAN)
size = sizeof(l);
for (i=0;i < size/2; i++)
{
j = size - i - 1;
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
#else
m = l;
l = 1;
if (c[0])
{
/* little endian */
l = m;
size = sizeof(l);
for (i=0;i < size/2; i++)
{
j = size - i - 1;
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
for (i=0;i < size; i++)
{
}
}
else
{
/* big endian */
l = m;
}
#endif
/* move tmp storage back to cptr array */
memmove(&cptr[byte_offset], (unsigned char *)&l, BYTES_PER_LONG);
#ifdef BFIX_DEBUG
return 0;
#endif
}
/*
*------------------------------------------------------------------------------
* bfx:
* bit field extraction
*
* Arguments:
* Bit field extract:
* l = bfx(cptr, bit_offset, bit_len);
*
* cptr - pointer to unsigned char array
* bit_offset - bit offset(starting from 1) from the start of
* the char array
* bit_len - bit length of field to extract
*
* Returns:
* on success unsigned long extracted bit field, 0 on failure
*------------------------------------------------------------------------------
*/
unsigned long
bfx(
const unsigned char *cptr,
unsigned long bit_offset,
unsigned long bit_len)
{
/* machine dependencies */
const unsigned int BITS_PER_BYTE = 8;
unsigned int BYTES_PER_LONG;
unsigned int BITS_PER_LONG;
unsigned long l;
#if !defined(BFIX_BIG_ENDIAN) && !defined(BFIX_LITTLE_ENDIAN)
unsigned long m;
#endif
#if !defined(BFIX_BIG_ENDIAN)
unsigned int i, j, size;
unsigned char* c = (unsigned char*)&l;
unsigned char tmp;
#endif
unsigned long byte_offset;
unsigned long left_shift;
unsigned long right_shift;
BYTES_PER_LONG = sizeof(unsigned long);
BITS_PER_LONG = BYTES_PER_LONG * BITS_PER_BYTE;
#ifdef BFIX_DEBUG
#if defined(BFIX_BIG_ENDIAN) && defined(BFIX_LITTLE_ENDIAN)
#error bfx(): Both BFIX_BIG_ENDIAN and BFIX_LITTLE_ENDIAN defined at the same time.
#endif
if (bit_offset < 1)
{
fprintf(stderr, "bfx: arg #2, bit_offset < 1.\n");
return 0;
}
if (bit_len < 0)
{
fprintf(stderr, "bfx: arg #3, bit_len < 0.\n");
return 0;
}
#endif
if (bit_len == 0)
{
return 0;
}
/*
* calculate byte offset(first byte=0) of start of
* BYTES_PER_LONG bytes containing bit field
*/
byte_offset = (bit_offset-1)/BITS_PER_BYTE;
/*
* calculate how many bits to shift bit field left
* to clear bits above bit field
*/
left_shift = bit_offset - byte_offset*BITS_PER_BYTE - 1;
#ifdef BFIX_DEBUG
if (bit_len > (BITS_PER_LONG-left_shift))
{
fprintf(stderr, "bfx: arg #3, bit_len to long.\n");
return 0;
}
#endif
/*
* calculate how many bits to shift bit field right
* to right justify bit field
*/
right_shift = BITS_PER_LONG - bit_len;
/* move BYTES_PER_LONG bytes to tmp storage */
#if defined(BFIX_BIG_ENDIAN)
memmove((unsigned char *)&l, &cptr[byte_offset], BYTES_PER_LONG);
#elif defined(BFIX_LITTLE_ENDIAN)
memmove((unsigned char *)&l, &cptr[byte_offset], BYTES_PER_LONG);
size = sizeof(l);
for (i=0;i < size/2; i++)
{
j = size - i - 1;
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
#else
m = l;
l = 1;
if (c[0])
{
/* little endian */
l = m;
memmove((unsigned char *)&l, &cptr[byte_offset], BYTES_PER_LONG);
size = sizeof(l);
for (i=0;i < size/2; i++)
{
j = size - i - 1;
tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
}
else
{
/* big endian */
l = m;
memmove((unsigned char *)&l, &cptr[byte_offset], BYTES_PER_LONG);
}
#endif
/*
* clear bits above bit field, right justify bit
* field, and return this value
*/
return (l << left_shift) >> right_shift;
}