Codebase list jimtcl / HEAD jim-pack.c
HEAD

Tree @HEAD (Download .tar.gz)

jim-pack.c @HEADraw · history · blame

  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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include <string.h>
#include <jim.h>

/* Provides the [pack] and [unpack] commands to pack and unpack
 * a binary string to/from arbitrary width integers and strings.
 *
 * This may be used to implement the [binary] command.
 */

/**
 * Big endian bit test.
 *
 * Considers 'bitvect' as a big endian bit stream and returns
 * bit 'b' as zero or non-zero.
 */
static int JimTestBitBigEndian(const unsigned char *bitvec, int b)
{
    div_t pos = div(b, 8);
    return bitvec[pos.quot] & (1 << (7 - pos.rem));
}

/**
 * Little endian bit test.
 *
 * Considers 'bitvect' as a little endian bit stream and returns
 * bit 'b' as zero or non-zero.
 */
static int JimTestBitLittleEndian(const unsigned char *bitvec, int b)
{
    div_t pos = div(b, 8);
    return bitvec[pos.quot] & (1 << pos.rem);
}

/**
 * Sign extends the given value, 'n' of width 'width' bits.
 *
 * For example, sign extending 0x80 with a width of 8, produces -128
 */
static jim_wide JimSignExtend(jim_wide n, int width)
{
    if (width == sizeof(jim_wide) * 8) {
        /* Can't sign extend the maximum size integer */
        return n;
    }
    if (n & ((jim_wide)1 << (width - 1))) {
        /* Need to extend */
        n -= ((jim_wide)1 << width);
    }

    return n;
}

/**
 * Big endian integer extraction.
 *
 * Considers 'bitvect' as a big endian bit stream.
 * Returns an integer of the given width (in bits)
 * starting at the given position (in bits).
 *
 * The pos/width must represent bits inside bitvec,
 * and the width be no more than the width of jim_wide.
 */
static jim_wide JimBitIntBigEndian(const unsigned char *bitvec, int pos, int width)
{
    jim_wide result = 0;
    int i;

    /* Aligned, byte extraction */
    if (pos % 8 == 0 && width % 8 == 0) {
        for (i = 0; i < width; i += 8) {
            result = (result << 8) + bitvec[(pos + i) / 8];
        }
        return result;
    }

    /* Unaligned */
    for (i = 0; i < width; i++) {
        if (JimTestBitBigEndian(bitvec, pos + width - i - 1)) {
            result |= ((jim_wide)1 << i);
        }
    }

    return result;
}

/**
 * Little endian integer extraction.
 *
 * Like JimBitIntBigEndian() but considers 'bitvect' as a little endian bit stream.
 */
static jim_wide JimBitIntLittleEndian(const unsigned char *bitvec, int pos, int width)
{
    jim_wide result = 0;
    int i;

    /* Aligned, byte extraction */
    if (pos % 8 == 0 && width % 8 == 0) {
        for (i = 0; i < width; i += 8) {
            result += (jim_wide)bitvec[(pos + i) / 8] << i;
        }
        return result;
    }

    /* Unaligned */
    for (i = 0; i < width; i++) {
        if (JimTestBitLittleEndian(bitvec, pos + i)) {
            result |= ((jim_wide)1 << i);
        }
    }

    return result;
}

/**
 * Big endian bit set.
 *
 * Considers 'bitvect' as a big endian bit stream and sets
 * bit 'b' to 'bit'
 */
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));
    }
    else {
        bitvec[pos.quot] &= ~(1 << (7 - pos.rem));
    }
}

/**
 * Little endian bit set.
 *
 * Considers 'bitvect' as a little endian bit stream and sets
 * bit 'b' to 'bit'
 */
static void JimSetBitLittleEndian(unsigned char *bitvec, int b, int bit)
{
    div_t pos = div(b, 8);
    if (bit) {
        bitvec[pos.quot] |= (1 << pos.rem);
    }
    else {
        bitvec[pos.quot] &= ~(1 << pos.rem);
    }
}

/**
 * Big endian integer packing.
 *
 * Considers 'bitvect' as a big endian bit stream.
 * Packs integer 'value' of the given width (in bits)
 * starting at the given position (in bits).
 *
 * The pos/width must represent bits inside bitvec,
 * and the width be no more than the width of jim_wide.
 */
static void JimSetBitsIntBigEndian(unsigned char *bitvec, jim_wide value, int pos, int width)
{
    int i;

    /* Common fast option */
    if (pos % 8 == 0 && width == 8) {
        bitvec[pos / 8] = value;
        return;
    }

    for (i = 0; i < width; i++) {
        int bit = !!(value & ((jim_wide)1 << i));
        JimSetBitBigEndian(bitvec, pos + width - i - 1, bit);
    }
}

/**
 * Little endian version of JimSetBitsIntBigEndian()
 */
static void JimSetBitsIntLittleEndian(unsigned char *bitvec, jim_wide value, int pos, int width)
{
    int i;

    /* Common fast option */
    if (pos % 8 == 0 && width == 8) {
        bitvec[pos / 8] = value;
        return;
    }

    for (i = 0; i < width; i++) {
        int bit = !!(value & ((jim_wide)1 << i));
        JimSetBitLittleEndian(bitvec, pos + i, bit);
    }
}

/**
 * Binary conversion of jim_wide integer to float
 *
 * Considers the least significant bits of
 * jim_wide 'value' as a IEEE float.
 *
 * Should work for both little- and big-endian platforms.
 */
static float JimIntToFloat(jim_wide value)
{
    int offs;
    float val;

    /* Skip offs to get to least significant bytes */
    offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(float)) : 0;

    memcpy(&val, (unsigned char *) &value + offs, sizeof(float));
    return val;
}

/**
 * Binary conversion of jim_wide integer to double
 *
 * Double precision version of JimIntToFloat
 */
static double JimIntToDouble(jim_wide value)
{
    int offs;
    double val;

    /* Skip offs to get to least significant bytes */
    offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(double)) : 0;

    memcpy(&val, (unsigned char *) &value + offs, sizeof(double));
    return val;
}

/**
 * Binary conversion of float to jim_wide integer
 *
 * Considers the bits of IEEE float 'value' as integer.
 * The integer is zero-extended to jim_wide.
 *
 * Should work for both little- and big-endian platforms.
 */
static jim_wide JimFloatToInt(float value)
{
    int offs;
    jim_wide val = 0;

    /* Skip offs to get to least significant bytes */
    offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(float)) : 0;

    memcpy((unsigned char *) &val + offs, &value, sizeof(float));
    return val;
}

/**
 * Binary conversion of double to jim_wide integer
 *
 * Double precision version of JimFloatToInt
 */
static jim_wide JimDoubleToInt(double value)
{
    int offs;
    jim_wide val = 0;

    /* Skip offs to get to least significant bytes */
    offs = Jim_IsBigEndian() ? (sizeof(jim_wide) - sizeof(double)) : 0;

    memcpy((unsigned char *) &val + offs, &value, sizeof(double));
    return val;
}

/**
 * [unpack]
 *
 * Usage: unpack binvalue -intbe|-intle|-uintbe|-uintle|-floatbe|-floatle|-str bitpos bitwidth
 *
 * Unpacks bits from $binvalue at bit position $bitpos and with $bitwidth.
 * Interprets the value according to the type and returns it.
 */
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 (Jim_GetEnum(interp, argv[2], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
        return JIM_ERR;
    }

    if (Jim_GetWideExpr(interp, argv[3], &pos) != JIM_OK) {
        return JIM_ERR;
    }
    if (pos < 0 || (option == OPT_STR && pos % 8)) {
        Jim_SetResultFormatted(interp, "bad bitoffset: %#s", argv[3]);
        return JIM_ERR;
    }
    if (Jim_GetWideExpr(interp, argv[4], &width) != JIM_OK) {
        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)) {
        Jim_SetResultFormatted(interp, "bad bitwidth: %#s", argv[4]);
        return JIM_ERR;
    }

    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;
            }
            Jim_SetResultString(interp, str + pos / 8, width / 8);
        }
        return JIM_OK;
    }
    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 (option == OPT_INTBE || option == OPT_UINTBE || option == OPT_FLOATBE) {
                result = JimBitIntBigEndian(str, pos, width);
            }
            else {
                result = JimBitIntLittleEndian(str, pos, width);
            }
            if (option == OPT_INTBE || option == OPT_INTLE) {
                result = JimSignExtend(result, width);
            }

        }

        if (option == OPT_FLOATBE || option == OPT_FLOATLE) {
            double fresult;
            if (width == 32) {
                fresult = (double) JimIntToFloat(result);
            } else {
                fresult = JimIntToDouble(result);
            }
            Jim_SetResult(interp, Jim_NewDoubleObj(interp, fresult));
        } else {
            Jim_SetResultInt(interp, result);
        }
        return JIM_OK;
    }
}

/**
 * [pack]
 *
 * Usage: pack varname value -intbe|-intle|-floatle|-floatbe|-str width ?bitoffset?
 *
 * Packs the binary representation of 'value' into the variable of the given name.
 * The value is packed according to the given type, width and bitoffset.
 * The variable is created if necessary (like [append])
 * The variable is expanded if necessary
 */
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 (Jim_GetEnum(interp, argv[3], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
        return JIM_ERR;
    }
    if ((option == OPT_LE || option == OPT_BE) &&
            Jim_GetWideExpr(interp, argv[2], &value) != JIM_OK) {
        return JIM_ERR;
    }
    if ((option == OPT_FLOATLE || option == OPT_FLOATBE) &&
            Jim_GetDouble(interp, argv[2], &fvalue) != JIM_OK) {
        return JIM_ERR;
    }
    if (Jim_GetWideExpr(interp, argv[4], &width) != JIM_OK) {
        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)) {
        Jim_SetResultFormatted(interp, "bad bitwidth: %#s", argv[4]);
        return JIM_ERR;
    }
    if (argc == 6) {
        if (Jim_GetWideExpr(interp, argv[5], &pos) != JIM_OK) {
            return JIM_ERR;
        }
        if (pos < 0 || (option == OPT_STR && pos % 8)) {
            Jim_SetResultFormatted(interp, "bad bitoffset: %#s", argv[5]);
            return JIM_ERR;
        }
    }

    stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED);
    if (!stringObjPtr) {
        /* Create the string if it doesn't exist */
        stringObjPtr = Jim_NewEmptyStringObj(interp);
        freeobj = 1;
    }
    else if (Jim_IsShared(stringObjPtr)) {
        freeobj = 1;
        stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr);
    }

    len = Jim_Length(stringObjPtr) * 8;

    /* Extend the string as necessary first */
    while (len < pos + width) {
        Jim_AppendString(interp, stringObjPtr, "", 1);
        len += 8;
    }

    Jim_SetResultInt(interp, pos + width);

    /* Now set the bits. Note that the the string *must* have no non-string rep
     * since we are writing the bytes directly.
     */
    Jim_AppendString(interp, stringObjPtr, "", 0);

    /* Convert floating point to integer if necessary */
    if (option == OPT_FLOATLE || option == OPT_FLOATBE) {
        /* Note that the following is slightly incompatible with Tcl behaviour.
         * In Tcl floating overflow gives FLT_MAX (cf. test binary-13.13).
         * In Jim Tcl it gives Infinity. This behavior may change.
         */
        value = (width == 32) ? JimFloatToInt((float)fvalue) : JimDoubleToInt(fvalue);
    }

    if (option == OPT_BE || option == OPT_FLOATBE) {
        JimSetBitsIntBigEndian((unsigned char *)stringObjPtr->bytes, value, pos, width);
    }
    else if (option == OPT_LE || option == OPT_FLOATLE) {
        JimSetBitsIntLittleEndian((unsigned char *)stringObjPtr->bytes, value, pos, width);
    }
    else {
        pos /= 8;
        width /= 8;

        if (width > Jim_Length(argv[2])) {
            width = Jim_Length(argv[2]);
        }
        memcpy(stringObjPtr->bytes + pos, Jim_String(argv[2]), width);
        /* No padding is needed since the string is already extended */
    }

    if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) {
        if (freeobj) {
            Jim_FreeNewObj(interp, stringObjPtr);
            return JIM_ERR;
        }
    }
    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;
}