1
2
3
4
9
10
16
21
22
28
33
34
39
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
75
76
77
82
83
84
85
86
91
92
93
94
95
96
97
103
104
105
110
111
112
113
114
120
130
131
137
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
167
168
172
173
174
177
178
179
180
181
182
186
187
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
280
281
282
283
284
289
292
293
296
300
303
308
309
321
322
323
324
325
326
341
342
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
372
373
374
375
376
377
378
379
380
381
386
389
393
397
400
405
414
415
416
421
425
426
427
428
429
433
434
435
436
439
440
441
442
449
450
453
456
457
458
459
460
463
464
465
466
467
473
474
475
476
#include <string.h>
#include <jim.h>
/* ... */
/* ... */
static int JimTestBitBigEndian(const unsigned char *bitvec, int b)
{
div_t pos = div(b, 8);
return bitvec[pos.quot] & (1 << (7 - pos.rem));
}{ ... }
/* ... */
static int JimTestBitLittleEndian(const unsigned char *bitvec, int b)
{
div_t pos = div(b, 8);
return bitvec[pos.quot] & (1 << pos.rem);
}{ ... }
/* ... */
static jim_wide JimSignExtend(jim_wide n, int width)
{
if (width == sizeof(jim_wide) * 8) {
return n;
}if (width == sizeof(jim_wide) * 8) { ... }
if (n & ((jim_wide)1 << (width - 1))) {
n -= ((jim_wide)1 << width);
}if (n & ((jim_wide)1 << (width - 1))) { ... }
return n;
}{ ... }
/* ... */
static jim_wide JimBitIntBigEndian(const unsigned char *bitvec, int pos, int width)
{
jim_wide result = 0;
int i;
if (pos % 8 == 0 && width % 8 == 0) {
for (i = 0; i < width; i += 8) {
result = (result << 8) + bitvec[(pos + i) / 8];
}for (i = 0; i < width; i += 8) { ... }
return result;
}if (pos % 8 == 0 && width % 8 == 0) { ... }
for (i = 0; i < width; i++) {
if (JimTestBitBigEndian(bitvec, pos + width - i - 1)) {
result |= ((jim_wide)1 << i);
}if (JimTestBitBigEndian(bitvec, pos + width - i - 1)) { ... }
}for (i = 0; i < width; i++) { ... }
return result;
}{ ... }
/* ... */
static jim_wide JimBitIntLittleEndian(const unsigned char *bitvec, int pos, int width)
{
jim_wide result = 0;
int i;
if (pos % 8 == 0 && width % 8 == 0) {
for (i = 0; i < width; i += 8) {
result += (jim_wide)bitvec[(pos + i) / 8] << i;
}for (i = 0; i < width; i += 8) { ... }
return result;
}if (pos % 8 == 0 && width % 8 == 0) { ... }
for (i = 0; i < width; i++) {
if (JimTestBitLittleEndian(bitvec, pos + i)) {
result |= ((jim_wide)1 << i);
}if (JimTestBitLittleEndian(bitvec, pos + i)) { ... }
}for (i = 0; i < width; i++) { ... }
return result;
}{ ... }
/* ... */
static void JimSetBitBigEndian(unsigned char *bitvec, int b, int bit)
{
div_t pos = div(b, 8);
if (bit) {
bitvec[pos.quot] |= (1 << (7 - pos.rem));
}if (bit) { ... }
else {
bitvec[pos.quot] &= ~(1 << (7 - pos.rem));
}else { ... }
}{ ... }
/* ... */
static void JimSetBitLittleEndian(unsigned char *bitvec, int b, int bit)
{
div_t pos = div(b, 8);
if (bit) {
bitvec[pos.quot] |= (1 << pos.rem);
}if (bit) { ... }
else {
bitvec[pos.quot] &= ~(1 << pos.rem);
}else { ... }
}{ ... }
/* ... */
static void JimSetBitsIntBigEndian(unsigned char *bitvec, jim_wide value, int pos, int width)
{
int i;
if (pos % 8 == 0 && width == 8) {
bitvec[pos / 8] = value;
return;
}if (pos % 8 == 0 && width == 8) { ... }
for (i = 0; i < width; i++) {
int bit = !!(value & ((jim_wide)1 << i));
JimSetBitBigEndian(bitvec, pos + width - i - 1, bit);
}for (i = 0; i < width; i++) { ... }
}{ ... }
/* ... */
static void JimSetBitsIntLittleEndian(unsigned char *bitvec, jim_wide value, int pos, int width)
{
int i;
if (pos % 8 == 0 && width == 8) {
bitvec[pos / 8] = value;
return;
}if (pos % 8 == 0 && width == 8) { ... }
for (i = 0; i < width; i++) {
int bit = !!(value & ((jim_wide)1 << i));
JimSetBitLittleEndian(bitvec, pos + i, bit);
}for (i = 0; i < width; i++) { ... }
}{ ... }
/* ... */
static float JimIntToFloat(jim_wide value)
{
int offs;
float val;
offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(float)) : 0;
memcpy(&val, (unsigned char *) &value + offs, sizeof(float));
return val;
}{ ... }
/* ... */
static double JimIntToDouble(jim_wide value)
{
int offs;
double val;
offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(double)) : 0;
memcpy(&val, (unsigned char *) &value + offs, sizeof(double));
return val;
}{ ... }
/* ... */
static jim_wide JimFloatToInt(float value)
{
int offs;
jim_wide val = 0;
offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(float)) : 0;
memcpy((unsigned char *) &val + offs, &value, sizeof(float));
return val;
}{ ... }
/* ... */
static jim_wide JimDoubleToInt(double value)
{
int offs;
jim_wide val = 0;
offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(double)) : 0;
memcpy((unsigned char *) &val + offs, &value, sizeof(double));
return val;
}{ ... }
/* ... */
static int Jim_UnpackCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
int option;
static const char * const options[] = { "-intbe", "-intle", "-uintbe", "-uintle",
"-floatbe", "-floatle", "-str", NULL ...};
enum { OPT_INTBE, OPT_INTLE, OPT_UINTBE, OPT_UINTLE, OPT_FLOATBE, OPT_FLOATLE, OPT_STR, };
jim_wide pos;
jim_wide width;
if (argc != 5) {
Jim_WrongNumArgs(interp, 1, argv,
"binvalue -intbe|-intle|-uintbe|-uintle|-floatbe|-floatle|-str bitpos bitwidth");
return JIM_ERR;
}if (argc != 5) { ... }
if (Jim_GetEnum(interp, argv[2], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetEnum(interp, argv[2], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { ... }
if (Jim_GetWideExpr(interp, argv[3], &pos) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetWideExpr(interp, argv[3], &pos) != JIM_OK) { ... }
if (pos < 0 || (option == OPT_STR && pos % 8)) {
Jim_SetResultFormatted(interp, "bad bitoffset: %#s", argv[3]);
return JIM_ERR;
}if (pos < 0 || (option == OPT_STR && pos % 8)) { ... }
if (Jim_GetWideExpr(interp, argv[4], &width) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetWideExpr(interp, argv[4], &width) != JIM_OK) { ... }
if (width < 0 || (option == OPT_STR && width % 8) || (option != OPT_STR && width > sizeof(jim_wide) * 8) ||
((option == OPT_FLOATLE || option == OPT_FLOATBE) && width != 32 && width != 64)) {
Jim_SetResultFormatted(interp, "bad bitwidth: %#s", argv[4]);
return JIM_ERR;
}if (width < 0 || (option == OPT_STR && width % 8) || (option != OPT_STR && width > sizeof(jim_wide) * 8) || ((option == OPT_FLOATLE || option == OPT_FLOATBE) && width != 32 && width != 64)) { ... }
if (option == OPT_STR) {
int len;
const char *str = Jim_GetString(argv[1], &len);
if (pos < len * 8) {
if (pos + width > len * 8) {
width = len * 8 - pos;
}if (pos + width > len * 8) { ... }
Jim_SetResultString(interp, str + pos / 8, width / 8);
}if (pos < len * 8) { ... }
return JIM_OK;
}if (option == OPT_STR) { ... }
else {
int len;
const unsigned char *str = (const unsigned char *)Jim_GetString(argv[1], &len);
jim_wide result = 0;
if (pos < len * 8) {
if (pos + width > len * 8) {
width = len * 8 - pos;
}if (pos + width > len * 8) { ... }
if (option == OPT_INTBE || option == OPT_UINTBE || option == OPT_FLOATBE) {
result = JimBitIntBigEndian(str, pos, width);
}if (option == OPT_INTBE || option == OPT_UINTBE || option == OPT_FLOATBE) { ... }
else {
result = JimBitIntLittleEndian(str, pos, width);
}else { ... }
if (option == OPT_INTBE || option == OPT_INTLE) {
result = JimSignExtend(result, width);
}if (option == OPT_INTBE || option == OPT_INTLE) { ... }
}if (pos < len * 8) { ... }
if (option == OPT_FLOATBE || option == OPT_FLOATLE) {
double fresult;
if (width == 32) {
fresult = (double) JimIntToFloat(result);
}if (width == 32) { ... } else {
fresult = JimIntToDouble(result);
}else { ... }
Jim_SetResult(interp, Jim_NewDoubleObj(interp, fresult));
}if (option == OPT_FLOATBE || option == OPT_FLOATLE) { ... } else {
Jim_SetResultInt(interp, result);
}else { ... }
return JIM_OK;
}else { ... }
}{ ... }
/* ... */
static int Jim_PackCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
int option;
static const char * const options[] = { "-intle", "-intbe", "-floatle", "-floatbe",
"-str", NULL ...};
enum { OPT_LE, OPT_BE, OPT_FLOATLE, OPT_FLOATBE, OPT_STR };
jim_wide pos = 0;
jim_wide width;
jim_wide value;
double fvalue;
Jim_Obj *stringObjPtr;
int len;
int freeobj = 0;
if (argc != 5 && argc != 6) {
Jim_WrongNumArgs(interp, 1, argv,
"varName value -intle|-intbe|-floatle|-floatbe|-str bitwidth ?bitoffset?");
return JIM_ERR;
}if (argc != 5 && argc != 6) { ... }
if (Jim_GetEnum(interp, argv[3], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetEnum(interp, argv[3], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { ... }
if ((option == OPT_LE || option == OPT_BE) &&
Jim_GetWideExpr(interp, argv[2], &value) != JIM_OK) {
return JIM_ERR;
}if ((option == OPT_LE || option == OPT_BE) && Jim_GetWideExpr(interp, argv[2], &value) != JIM_OK) { ... }
if ((option == OPT_FLOATLE || option == OPT_FLOATBE) &&
Jim_GetDouble(interp, argv[2], &fvalue) != JIM_OK) {
return JIM_ERR;
}if ((option == OPT_FLOATLE || option == OPT_FLOATBE) && Jim_GetDouble(interp, argv[2], &fvalue) != JIM_OK) { ... }
if (Jim_GetWideExpr(interp, argv[4], &width) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetWideExpr(interp, argv[4], &width) != JIM_OK) { ... }
if (width <= 0 || (option == OPT_STR && width % 8) || (option != OPT_STR && width > sizeof(jim_wide) * 8) ||
((option == OPT_FLOATLE || option == OPT_FLOATBE) && width != 32 && width != 64)) {
Jim_SetResultFormatted(interp, "bad bitwidth: %#s", argv[4]);
return JIM_ERR;
}if (width <= 0 || (option == OPT_STR && width % 8) || (option != OPT_STR && width > sizeof(jim_wide) * 8) || ((option == OPT_FLOATLE || option == OPT_FLOATBE) && width != 32 && width != 64)) { ... }
if (argc == 6) {
if (Jim_GetWideExpr(interp, argv[5], &pos) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetWideExpr(interp, argv[5], &pos) != JIM_OK) { ... }
if (pos < 0 || (option == OPT_STR && pos % 8)) {
Jim_SetResultFormatted(interp, "bad bitoffset: %#s", argv[5]);
return JIM_ERR;
}if (pos < 0 || (option == OPT_STR && pos % 8)) { ... }
}if (argc == 6) { ... }
stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED);
if (!stringObjPtr) {
stringObjPtr = Jim_NewEmptyStringObj(interp);
freeobj = 1;
}if (!stringObjPtr) { ... }
else if (Jim_IsShared(stringObjPtr)) {
freeobj = 1;
stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr);
}else if (Jim_IsShared(stringObjPtr)) { ... }
len = Jim_Length(stringObjPtr) * 8;
while (len < pos + width) {
Jim_AppendString(interp, stringObjPtr, "", 1);
len += 8;
}while (len < pos + width) { ... }
Jim_SetResultInt(interp, pos + width);
/* ... */
Jim_AppendString(interp, stringObjPtr, "", 0);
if (option == OPT_FLOATLE || option == OPT_FLOATBE) {
/* ... */
value = (width == 32) ? JimFloatToInt((float)fvalue) : JimDoubleToInt(fvalue);
}if (option == OPT_FLOATLE || option == OPT_FLOATBE) { ... }
if (option == OPT_BE || option == OPT_FLOATBE) {
JimSetBitsIntBigEndian((unsigned char *)stringObjPtr->bytes, value, pos, width);
}if (option == OPT_BE || option == OPT_FLOATBE) { ... }
else if (option == OPT_LE || option == OPT_FLOATLE) {
JimSetBitsIntLittleEndian((unsigned char *)stringObjPtr->bytes, value, pos, width);
}else if (option == OPT_LE || option == OPT_FLOATLE) { ... }
else {
pos /= 8;
width /= 8;
if (width > Jim_Length(argv[2])) {
width = Jim_Length(argv[2]);
}if (width > Jim_Length(argv[2])) { ... }
memcpy(stringObjPtr->bytes + pos, Jim_String(argv[2]), width);
}else { ... }
if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) {
if (freeobj) {
Jim_FreeNewObj(interp, stringObjPtr);
return JIM_ERR;
}if (freeobj) { ... }
}if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) { ... }
return JIM_OK;
}{ ... }
int Jim_packInit(Jim_Interp *interp)
{
Jim_PackageProvideCheck(interp, "pack");
Jim_CreateCommand(interp, "unpack", Jim_UnpackCmd, NULL, NULL);
Jim_CreateCommand(interp, "pack", Jim_PackCmd, NULL, NULL);
return JIM_OK;
}{ ... }