Diff
Not logged in

Differences From Artifact [19e1e17f8e]:

To Artifact [4d728227ea]:


1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12

13
14
15
16
17
18
19
20












-
+







/*
 * tclBinary.c --
 *
 *	This file contains the implementation of the "binary" Tcl built-in
 *	command and the Tcl binary data object.
 *
 * Copyright (c) 1997 by Sun Microsystems, Inc.
 * Copyright (c) 1998-1999 by Scriptics Corporation.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tclBinary.c,v 1.13.4.21 2008/07/29 20:13:28 dgp Exp $
 * RCS: @(#) $Id: tclBinary.c,v 1.13.4.22 2008/10/11 03:37:26 dgp Exp $
 */

#include "tclInt.h"
#include "tommath.h"

#include <math.h>

2237
2238
2239
2240
2241
2242
2243
2244
2245
2246



2247
2248
2249


2250
2251
2252
2253
2254



2255
2256
2257
2258
2259
2260
2261
2262
2263


2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276





2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294

2295
2296
2297
2298
2299
2300
2301
2237
2238
2239
2240
2241
2242
2243



2244
2245
2246
2247


2248
2249

2250



2251
2252
2253
2254
2255
2256
2257
2258
2259
2260


2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273


2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295

2296
2297
2298
2299
2300
2301
2302
2303







-
-
-
+
+
+

-
-
+
+
-

-
-
-
+
+
+







-
-
+
+











-
-
+
+
+
+
+

















-
+







}

/*
 * ----------------------------------------------------------------------
 *
 * NOTES --
 *
 *	Some measurements show that it is faster to use a table to 
 *	to perform uuencode and base64 value encoding than to calculate
 *	the output (at least on intel P4 arch).
 *	Some measurements show that it is faster to use a table to to perform
 *	uuencode and base64 value encoding than to calculate the output (at
 *	least on intel P4 arch).
 *
 *	Conversely using a lookup table for the decoding is slower than
 *	just calculating the values. We therefore use the fastest of
 *	Conversely using a lookup table for the decoding is slower than just
 *	calculating the values. We therefore use the fastest of each method.
 *	each method.
 *
 *	Presumably this has to do with the size of the tables. The
 *	base64 decode table is 255 bytes while the encode table is only
 *	65 bytes. The choice likely depends on CPU memory cache sizes.
 *	Presumably this has to do with the size of the tables. The base64
 *	decode table is 255 bytes while the encode table is only 65 bytes. The
 *	choice likely depends on CPU memory cache sizes.
 */

/*
 *----------------------------------------------------------------------
 *
 * BinaryEncodeHex --
 *
 *	Implement the [binary encode hex] binary encoding.
 *	clientData must be a table to convert values to hexadecimal digits.
 *	Implement the [binary encode hex] binary encoding. clientData must be
 *	a table to convert values to hexadecimal digits.
 *
 * Results:
 *	Interp result set to an encoded byte array object 
 *
 * Side effects:
 *	None
 *
 *----------------------------------------------------------------------
 */

static int
BinaryEncodeHex(ClientData clientData, Tcl_Interp *interp, 
    int objc, Tcl_Obj *const objv[])
BinaryEncodeHex(
    ClientData clientData,
    Tcl_Interp *interp, 
    int objc,
    Tcl_Obj *const objv[])
{
    Tcl_Obj *resultObj = NULL;
    unsigned char *data = NULL;
    unsigned char *cursor = NULL;
    const char *digits = clientData;
    int offset = 0, count = 0;
	
    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "data");
	return TCL_ERROR;
    }

    TclNewObj(resultObj);
    data = Tcl_GetByteArrayFromObj(objv[1], &count);
    cursor = Tcl_SetByteArrayLength(resultObj, count * 2);
    for (offset = 0; offset < count; ++offset) {
	*cursor++ = digits[((data[offset] >> 4) & 0x0f)];
	*cursor++ = digits[( data[offset]       & 0x0f)];
	*cursor++ = digits[(data[offset] & 0x0f)];
    }
    Tcl_SetObjResult(interp, resultObj);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
2310
2311
2312
2313
2314
2315
2316
2317
2318





2319
2320
2321
2322

2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333


2334
2335
2336
2337
2338
2339



2340
2341
2342
2343
2344
2345


2346
2347
2348
2349
2350
2351
2352

2353
2354
2355



2356
2357
2358
2359
2360




2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382

2383
2384
2385
2386
2387







2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398




2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419















2420
2421
2422
2423





2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437

2438
2439
2440
2441
2442


2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454











2455
2456
2457
2458
2459
2460
2461
2462
2463

2464



2465
2466
2467
2468
2469
2470

2471

2472

2473

2474
2475
2476
2477
2478
2479
2480
2481

2482
2483
2484
2485
2486
2487
2488
2312
2313
2314
2315
2316
2317
2318


2319
2320
2321
2322
2323
2324
2325
2326

2327
2328
2329
2330
2331
2332
2333
2334
2335
2336


2337
2338
2339
2340
2341



2342
2343
2344
2345
2346
2347
2348


2349
2350
2351
2352
2353
2354
2355


2356
2357


2358
2359
2360
2361




2362
2363
2364
2365



2366

2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380

2381

2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401




2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415











2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432


2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450

2451
2452
2453
2454


2455
2456
2457
2458
2459









2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480

2481
2482
2483
2484
2485
2486
2487
2488
2489
2490

2491
2492
2493

2494
2495
2496
2497
2498
2499
2500
2501

2502
2503
2504
2505
2506
2507
2508
2509







-
-
+
+
+
+
+



-
+









-
-
+
+



-
-
-
+
+
+




-
-
+
+





-
-
+

-
-
+
+
+

-
-
-
-
+
+
+
+
-
-
-

-














-

-
+





+
+
+
+
+
+
+







-
-
-
-
+
+
+
+










-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-
+
+
+
+
+













-
+



-
-
+
+



-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+









+
-
+
+
+






+
-
+

+
-
+







-
+







 * Side effects:
 *	None
 *
 *----------------------------------------------------------------------
 */

static int
BinaryDecodeHex(ClientData clientData, Tcl_Interp *interp, 
    int objc, Tcl_Obj *const objv[])
BinaryDecodeHex(
    ClientData clientData,
    Tcl_Interp *interp, 
    int objc,
    Tcl_Obj *const objv[])
{
    Tcl_Obj *resultObj = NULL;
    unsigned char *data, *datastart, *dataend;
    unsigned char *begin, *cursor;
    unsigned char *begin, *cursor, c;
    int i, index, value, size, count = 0, cut = 0, strict = 0;
    enum {OPT_STRICT };
    static const char *optStrings[] = { "-strict", NULL };
	
    if (objc < 2 || objc > 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "data");
	return TCL_ERROR;
    }
    for (i = 1; i < objc-1; ++i) {
	if (Tcl_GetIndexFromObj(interp, objv[i], optStrings, 
		"option", TCL_EXACT, &index) != TCL_OK) {
	if (Tcl_GetIndexFromObj(interp, objv[i], optStrings, "option",
		TCL_EXACT, &index) != TCL_OK) {
	    return TCL_ERROR;
	}
	switch (index) {
	    case OPT_STRICT: 
		strict = 1;
		break;
	case OPT_STRICT: 
	    strict = 1;
	    break;
	}
    }

    TclNewObj(resultObj);
    datastart = data = (unsigned char *) TclGetStringFromObj(objv[objc-1],
	    &count);
    datastart = data = (unsigned char *)
	    TclGetStringFromObj(objv[objc-1], &count);
    dataend = data + count;
    size = (count + 1) / 2;
    begin = cursor = Tcl_SetByteArrayLength(resultObj, size);
    while (data < dataend) {
	value = 0;
	i = 0;
	while (i < 2) {
	for (i=0 ; i<2 ; i++) {
	    if (data < dataend) {
		unsigned char c = *data++;
		if (!isxdigit((char)c)) {
		c = *data++;

		if (!isxdigit((int) c)) {
		    if (strict) {
			char sz[2] = {0, 0}, pos[TCL_INTEGER_SPACE];
			sz[0] = c;
			sprintf(pos, "%d", (int)(data - datastart - 1));
			TclDecrRefCount(resultObj);
			goto badChar;
		    } else {
			i--;
			continue;
			Tcl_AppendResult(interp, "invalid hexadecimal digit \"", 
			    sz, "\" at position ", pos, NULL);
			return TCL_ERROR;
		    }
		    continue;
		}
		value <<= 4;
		c -= '0';
		if (c > 9) {
		    c += ('0' - 'A') + 10;
		}
		if (c > 16) {
		    c += ('A' - 'a');
		}
		value |= (c & 0xf);
	    } else {
		value <<= 4;
		++cut;
	    }
	    ++i;
	}
	*cursor++ = (unsigned char) value;
	*cursor++ = UCHAR(value);
	value = 0;
    }
    Tcl_SetByteArrayLength(resultObj, cursor - begin - cut);
    Tcl_SetObjResult(interp, resultObj);
    return TCL_OK;

  badChar:
    TclDecrRefCount(resultObj);
    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "invalid hexadecimal digit \"%c\" at position %d",
	    c, (int) (data - datastart - 1)));
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * BinaryEncode64 --
 *
 *	This implements a generic 6 bit binary encoding. Input is broken
 *	into 6 bit chunks and a lookup table passed in via clientData is
 *	used to turn these values into output characters. This is used
 *	to implement base64 and uuencode binary encodings.
 *	This implements a generic 6 bit binary encoding. Input is broken into
 *	6 bit chunks and a lookup table passed in via clientData is used to
 *	turn these values into output characters. This is used to implement
 *	base64 and uuencode binary encodings.
 *
 * Results:
 *	Interp result set to an encoded byte array object 
 *
 * Side effects:
 *	None
 *
 *----------------------------------------------------------------------
 */

#define OUTPUT(c)					\
    *cursor++ = (c);					\
    ++outindex;						\
    if (maxlen > 0 && cursor != limit) {		\
	if (outindex == maxlen) {			\
	    memcpy(cursor, wrapchar, wrapcharlen);	\
	    cursor += wrapcharlen;			\
	    outindex = 0;				\
	}						\
    }							\
    if (cursor > limit) Tcl_Panic("limit hit\n");
#define OUTPUT(c) \
    do {						\
	*cursor++ = (c);				\
	++outindex;					\
	if (maxlen > 0 && cursor != limit) {		\
	    if (outindex == maxlen) {			\
		memcpy(cursor, wrapchar, wrapcharlen);	\
		cursor += wrapcharlen;			\
		outindex = 0;				\
	    }						\
	}						\
	if (cursor > limit) {				\
	    Tcl_Panic("limit hit\n");			\
	}						\
    } while (0)

static int
BinaryEncode64(ClientData clientData, Tcl_Interp *interp,
    int objc, Tcl_Obj *const objv[])
BinaryEncode64(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Tcl_Obj *resultObj;
    unsigned char *data, *cursor, *limit;
    const char *digits = clientData;
    int maxlen = 0;
    const char *wrapchar = "\n";
    int wrapcharlen = 1;
    int offset, i, index, size, outindex = 0, count = 0;
    enum {OPT_MAXLEN, OPT_WRAPCHAR };
    static const char *optStrings[] = { "-maxlen", "-wrapchar", NULL };

    if (objc < 2 || objc%2 != 0) {
	Tcl_WrongNumArgs(interp, 1, objv, 
	    "?-maxlen len? ?-wrapchar char? data");
		"?-maxlen len? ?-wrapchar char? data");
	return TCL_ERROR;
    }
    for (i = 1; i < objc-1; i += 2) {
	if (Tcl_GetIndexFromObj(interp, objv[i], optStrings, 
		"option", TCL_EXACT, &index) != TCL_OK) {
	if (Tcl_GetIndexFromObj(interp, objv[i], optStrings, "option",
		TCL_EXACT, &index) != TCL_OK) {
	    return TCL_ERROR;
	}
	switch (index) {
	    case OPT_MAXLEN: 
		if (Tcl_GetIntFromObj(interp, objv[i+1], &maxlen) != TCL_OK)
		    return TCL_ERROR;
		break;
	    case OPT_WRAPCHAR:
		wrapchar = Tcl_GetStringFromObj(objv[i+1], NULL);
		wrapcharlen = strlen(wrapchar);
		if (wrapcharlen == 0) maxlen = 0;
		break;
	case OPT_MAXLEN: 
	    if (Tcl_GetIntFromObj(interp, objv[i+1], &maxlen) != TCL_OK) {
		return TCL_ERROR;
	    }
	    break;
	case OPT_WRAPCHAR:
	    wrapchar = Tcl_GetStringFromObj(objv[i+1], &wrapcharlen);
	    if (wrapcharlen == 0) {
		maxlen = 0;
	    }
	    break;
	}
    }

    resultObj = Tcl_NewObj();
    data = Tcl_GetByteArrayFromObj(objv[objc-1], &count);
    if (count > 0) {
	size = (((count * 4) / 3) + 3) & ~3; /* ensure 4 byte chunks */
	if (maxlen > 0 && size > maxlen) {
	    int adjusted = size + (wrapcharlen * (size / maxlen));

	    if (size % maxlen == 0) adjusted -= wrapcharlen;
	    if (size % maxlen == 0) {
		adjusted -= wrapcharlen;
	    }
	    size = adjusted;
	}
	cursor = Tcl_SetByteArrayLength(resultObj, size);
	limit = cursor + size;
	for (offset = 0; offset < count; offset+=3) {
	    unsigned char d[3] = {0, 0, 0};

	    for (i = 0; i < 3 && offset+i < count; ++i)
	    for (i = 0; i < 3 && offset+i < count; ++i) {
		d[i] = data[offset + i];
	    }
	    OUTPUT(digits[  d[0] >> 2]);
	    OUTPUT(digits[d[0] >> 2]);
	    OUTPUT(digits[((d[0] & 0x03) << 4) | (d[1] >> 4)]);
	    if (offset+1 < count) {
		OUTPUT(digits[((d[1] & 0x0f) << 2) | (d[2] >> 6)]);
	    } else {
		OUTPUT(digits[64]);
	    }
	    if (offset+2 < count) {
		OUTPUT(digits[  d[2] & 0x3f]);
		OUTPUT(digits[d[2] & 0x3f]);
	    } else {
		OUTPUT(digits[64]);
	    }
	}
    }
    Tcl_SetObjResult(interp, resultObj);
    return TCL_OK;
2502
2503
2504
2505
2506
2507
2508
2509
2510





2511
2512
2513
2514
2515

2516
2517
2518
2519
2520
2521
2522
2523
2524
2525


2526
2527
2528
2529
2530
2531



2532
2533
2534
2535
2536
2537


2538
2539
2540
2541
2542
2543
2544


2545
2546
2547


2548
2549
2550
2551
2552




2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566






2567
2568
2569
2570







2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591





2592
2593
2594

2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608


2609
2610
2611
2612
2613
2614



2615
2616
2617
2618
2619
2620


2621
2622
2623
2624
2625

2626
2627


2628
2629


2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642



2643
2644
2645
2646

2647
2648
2649
2650
2651
2652

2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663



2664
2665
2666
2667
2668









2669
2670
2671
2672
2673
2674
2675
2523
2524
2525
2526
2527
2528
2529


2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548


2549
2550
2551
2552
2553



2554
2555
2556
2557
2558
2559
2560


2561
2562
2563
2564
2565
2566
2567


2568
2569
2570


2571
2572
2573




2574
2575
2576
2577



2578

2579
2580
2581
2582

2583



2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619


2620
2621
2622
2623
2624
2625
2626

2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639


2640
2641
2642
2643
2644



2645
2646
2647
2648
2649
2650
2651


2652
2653
2654
2655
2656
2657

2658
2659

2660
2661
2662

2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676

2677
2678
2679
2680
2681


2682





2683
2684
2685
2686
2687
2688
2689
2690

2691



2692
2693
2694
2695
2696
2697
2698

2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714







-
-
+
+
+
+
+





+








-
-
+
+



-
-
-
+
+
+




-
-
+
+





-
-
+
+

-
-
+
+

-
-
-
-
+
+
+
+
-
-
-

-




-

-
-
-
+
+
+
+
+
+




+
+
+
+
+
+
+



















-
-
+
+
+
+
+


-
+












-
-
+
+



-
-
-
+
+
+




-
-
+
+




-
+

-
+
+

-
+
+












-
+
+
+


-
-
+
-
-
-
-
-

+






-

-
-
-
+
+
+




-
+
+
+
+
+
+
+
+
+







 * Side effects:
 *	None
 *
 *----------------------------------------------------------------------
 */

static int
BinaryDecodeUu(ClientData clientData, Tcl_Interp *interp,
    int objc, Tcl_Obj *const objv[])
BinaryDecodeUu(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Tcl_Obj *resultObj = NULL;
    unsigned char *data, *datastart, *dataend;
    unsigned char *begin, *cursor;
    int i, index, size, count = 0, cut = 0, strict = 0;
    char c;
    enum {OPT_STRICT };
    static const char *optStrings[] = { "-strict", NULL };
	
    if (objc < 2 || objc > 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "data");
	return TCL_ERROR;
    }
    for (i = 1; i < objc-1; ++i) {
	if (Tcl_GetIndexFromObj(interp, objv[i], optStrings, 
		"option", TCL_EXACT, &index) != TCL_OK) {
	if (Tcl_GetIndexFromObj(interp, objv[i], optStrings, "option",
		TCL_EXACT, &index) != TCL_OK) {
	    return TCL_ERROR;
	}
	switch (index) {
	    case OPT_STRICT: 
		strict = 1;
		break;
	case OPT_STRICT: 
	    strict = 1;
	    break;
	}
    }

    TclNewObj(resultObj);
    datastart = data = (unsigned char *) TclGetStringFromObj(objv[objc-1],
	    &count);
    datastart = data = (unsigned char *)
	    TclGetStringFromObj(objv[objc-1], &count);
    dataend = data + count;
    size = ((count + 3) & ~3) * 3 / 4;
    begin = cursor = Tcl_SetByteArrayLength(resultObj, size);
    while (data < dataend) {
	char d[4] = {0, 0, 0, 0};
	i = 0;
	while (i < 4) {

	for (i=0 ; i<4 ; i++) {
	    if (data < dataend) {
		d[i] = *data++;
		if (d[i] < 33 || d[i] > 96) {
		d[i] = c = *data++;
		if (c < 33 || c > 96) {
		    if (strict) {
			char sz[2] = {0, 0}, pos[TCL_INTEGER_SPACE];
			sz[0] = d[i];
			sprintf(pos, "%d", (int)(data - datastart - 1));
			TclDecrRefCount(resultObj);
			goto badUu;
		    } else {
			i--;
			continue;
			Tcl_AppendResult(interp, "invalid uuencode character \"", 
			    sz, "\" at position ", pos, NULL);
			return TCL_ERROR;
		    }
		    continue;
		}
	    } else {
		++cut;
	    }
	    ++i;
	}
	*cursor++ = (((d[0] - 0x20) & 0x3f) << 2) | (((d[1] - 0x20) & 0x3f) >> 4);
	*cursor++ = (((d[1] - 0x20) & 0x3f) << 4) | (((d[2] - 0x20) & 0x3f) >> 2);
	*cursor++ = (((d[2] - 0x20) & 0x3f) << 6) | (((d[3] - 0x20) & 0x3f) );
	*cursor++ = (((d[0] - 0x20) & 0x3f) << 2)
		| (((d[1] - 0x20) & 0x3f) >> 4);
	*cursor++ = (((d[1] - 0x20) & 0x3f) << 4)
		| (((d[2] - 0x20) & 0x3f) >> 2);
	*cursor++ = (((d[2] - 0x20) & 0x3f) << 6)
		| (((d[3] - 0x20) & 0x3f));
    }
    Tcl_SetByteArrayLength(resultObj, cursor - begin - cut);
    Tcl_SetObjResult(interp, resultObj);
    return TCL_OK;

  badUu:
    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "invalid uuencode character \"%c\" at position %d",
	    c, (int) (data - datastart - 1)));
    TclDecrRefCount(resultObj);
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * BinaryDecode64 --
 *
 *	Decode a base64 encoded string.
 *
 * Results:
 *	Interp result set to an byte array object 
 *
 * Side effects:
 *	None
 *
 *----------------------------------------------------------------------
 */

static int
BinaryDecode64(ClientData clientData, Tcl_Interp *interp,
    int objc, Tcl_Obj *const objv[])
BinaryDecode64(
    ClientData clientData,
    Tcl_Interp *interp,
    int objc,
    Tcl_Obj *const objv[])
{
    Tcl_Obj *resultObj = NULL;
    unsigned char *data, *datastart, *dataend;
    unsigned char *data, *datastart, *dataend, c;
    unsigned char *begin = NULL;
    unsigned char *cursor = NULL;
    int strict = 0;
    int i, index, size, cut = 0, count = 0;
    enum {OPT_STRICT };
    static const char *optStrings[] = { "-strict", NULL };
	
    if (objc < 2 || objc > 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "data");
	return TCL_ERROR;
    }
    for (i = 1; i < objc-1; ++i) {
	if (Tcl_GetIndexFromObj(interp, objv[i], optStrings, 
		"option", TCL_EXACT, &index) != TCL_OK) {
	if (Tcl_GetIndexFromObj(interp, objv[i], optStrings, "option",
		TCL_EXACT, &index) != TCL_OK) {
	    return TCL_ERROR;
	}
	switch (index) {
	    case OPT_STRICT: 
		strict = 1;
		break;
	case OPT_STRICT: 
	    strict = 1;
	    break;
	}
    }

    TclNewObj(resultObj);
    datastart = data = (unsigned char *) TclGetStringFromObj(objv[objc-1],
	&count);
    datastart = data = (unsigned char *)
	    TclGetStringFromObj(objv[objc-1], &count);
    dataend = data + count;
    size = ((count + 3) & ~3) * 3 / 4;
    begin = cursor = Tcl_SetByteArrayLength(resultObj, size);
    while (data < dataend) {
	int i = 0;
	int i;
	unsigned long value = 0;
	while (i < 4) {

	for (i=0 ; i<4 ; i++) {
	    if (data < dataend) {
		unsigned char c = *data++;
		c = *data++;

		if (c >= 'A' && c <= 'Z') {
		    value = (value << 6) | ((c - 'A') & 0x3f);
		} else if (c >= 'a' && c <= 'z') {
		    value = (value << 6) | ((c - 'a' + 26) & 0x3f);
		} else if (c >= '0' && c <= '9') {
		    value = (value << 6) | ((c - '0' + 52) & 0x3f);
		} else if (c == '+') {
		    value = (value << 6) | 0x3e;
		} else if (c == '/') {
		    value = (value << 6) | 0x3f;
		} else if (c == '=') {
		    value <<= 6;
		    if (cut < 2) ++cut;
		    if (cut < 2) {
			++cut;
		    }
		} else {
		    if (strict) {
			char sz[2] = {0, 0}, pos[TCL_INTEGER_SPACE];
			sz[0] = c;
			goto bad64;
			sprintf(pos, "%d", (int)(data - datastart - 1));
			TclDecrRefCount(resultObj);
			Tcl_AppendResult(interp, "invalid base64 character \"", 
			    sz, "\" at position ", pos, NULL);
			return TCL_ERROR;
		    }
		    i--;
		    continue;
		}
	    } else {
		value <<= 6;
		++cut;
	    }
	    ++i;
	}
	*cursor++ = (unsigned char)((value >> 16) & 0xff);
	*cursor++ = (unsigned char)((value >> 8) & 0xff);
	*cursor++ = (unsigned char)(value & 0xff);
	*cursor++ = UCHAR((value >> 16) & 0xff);
	*cursor++ = UCHAR((value >> 8) & 0xff);
	*cursor++ = UCHAR(value & 0xff);
    }
    Tcl_SetByteArrayLength(resultObj, cursor - begin - cut);
    Tcl_SetObjResult(interp, resultObj);
    return TCL_OK;
}

  bad64:
    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "invalid base64 character \"%c\" at position %d",
	    (char) c, (int) (data - datastart - 1)));
    TclDecrRefCount(resultObj);
    return TCL_ERROR;
}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */