File: binio.c

package info (click to toggle)
tcltrf 2.1.4-dfsg3-8
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 9,656 kB
  • sloc: ansic: 73,139; sh: 3,155; tcl: 1,343; makefile: 182; exp: 22
file content (1016 lines) | stat: -rw-r--r-- 26,016 bytes parent folder | download | duplicates (6)
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016

/*
 * binio.c --
 *
 *	Implementation of a binary input and output.
 *
 * Copyright (c) Jan 1997, Andreas Kupries (andreas_kupries@users.sourceforge.net)
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL I BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
 * SOFTWARE AND ITS DOCUMENTATION, EVEN IF I HAVE BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * I SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
 * I HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 * ENHANCEMENTS, OR MODIFICATIONS.
 *
 * CVS: $Id: binio.c,v 1.11 2009/05/07 04:57:27 andreas_kupries Exp $
 */


#include "transformInt.h"

#ifdef ENABLE_BINIO
#include <limits.h>

/*
 * Forward declarations of internal procedures.
 */

static int CopyCmd   _ANSI_ARGS_((Tcl_Interp *interp, int argc, char** argv));
static int PackCmd   _ANSI_ARGS_((Tcl_Interp *interp, int argc, char** argv));
static int UnpackCmd _ANSI_ARGS_((Tcl_Interp *interp, int argc, char** argv));
static int BinioCmd  _ANSI_ARGS_((ClientData notUsed, Tcl_Interp* interp, int argc, char** argv));

static void	ReorderBytes _ANSI_ARGS_ ((char* buf, int len /*2,4,8*/));

static int	GetHex   _ANSI_ARGS_ ((Tcl_Interp* interp, char* text, long int* result));
static int	GetOctal _ANSI_ARGS_ ((Tcl_Interp* interp, char* text, long int* result));

/*
 * Return at most this number of bytes in one call to Tcl_Read:
 */

#define KILO 1024
#ifndef READ_CHUNK_SIZE
#define	READ_CHUNK_SIZE	(16*KILO)
#endif

/*
 * Union to overlay the different possible types used in 'pack', 'unpack'.
 */

typedef union {
  double         d;
  float          f;

  long int       li;
  unsigned long  ul;

  int            i;
  unsigned int   ui;

  short int      si;
  unsigned short us;

  char           c;
  unsigned char  uc;
} conversion;

/*
 *------------------------------------------------------*
 *
 *	CopyCmd --
 *
 *	------------------------------------------------*
 *	This procedure realizes the 'binio copy' command.
 *	See the manpages for details on what it does.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		See user documentation.
 *
 *	Result:
 *		A standard tcl error code.
 *
 *------------------------------------------------------*
 */
	/* ARGSUSED */
static int
CopyCmd (interp, argc, argv)
Tcl_Interp* interp;     /* The interpreter we are working in */
int         argc;	/* # arguments */
char**      argv;	/* trailing arguments */
{
  /*
   * Allowed syntax:
   * 	inChannel outChannel ?count?
   *
   * code taken from 'unsupported0'.
   */

  Tcl_Channel inChan, outChan;
  int requested;
  char *bufPtr;
  int actuallyRead, actuallyWritten, totalRead, toReadNow, mode;
    
  /*
   * Assume we want to copy the entire channel.
   */
  
  requested = INT_MAX;

  if ((argc < 2) || (argc > 3)) {
    Tcl_AppendResult(interp,
		     "wrong # args: should be \"binio copy inChannel outChannel ?chunkSize?\"",
		     (char *) NULL);
    return TCL_ERROR;
  }

  inChan = Tcl_GetChannel(interp, argv[0], &mode);
  if (inChan == (Tcl_Channel) NULL) {
    return TCL_ERROR;
  }

  if ((mode & TCL_READABLE) == 0) {
    Tcl_AppendResult(interp, "channel \"", argv[0],
		     "\" wasn't opened for reading", (char *) NULL);
    return TCL_ERROR;
  }

  outChan = Tcl_GetChannel(interp, argv[1], &mode);
  if (outChan == (Tcl_Channel) NULL) {
    return TCL_ERROR;
  }

  if ((mode & TCL_WRITABLE) == 0) {
    Tcl_AppendResult(interp, "channel \"", argv[1],
		     "\" wasn't opened for writing", (char *) NULL);
    return TCL_ERROR;
  }
    
  if (argc == 3) {
    if (Tcl_GetInt(interp, argv[2], &requested) != TCL_OK) {
      return TCL_ERROR;
    }
    if (requested < 0) {
      requested = INT_MAX;
    }
  }

  bufPtr = ckalloc((unsigned) READ_CHUNK_SIZE);

  for (totalRead = 0;
       requested > 0;
       totalRead += actuallyRead, requested -= actuallyRead) {

    toReadNow = requested;
    if (toReadNow > READ_CHUNK_SIZE) {
      toReadNow = READ_CHUNK_SIZE;
    }

    actuallyRead = Tcl_Read(inChan, bufPtr, toReadNow);

    if (actuallyRead < 0) {
      ckfree (bufPtr);
      Tcl_AppendResult(interp, argv[0], ": ", Tcl_GetChannelName(inChan),
		       Tcl_PosixError(interp), (char *) NULL);
      return TCL_ERROR;
    } else if (actuallyRead == 0) {
      ckfree (bufPtr);
      sprintf(interp->result, "%d", totalRead);
      return TCL_OK;
    }

    actuallyWritten = Tcl_Write(outChan, bufPtr, actuallyRead);
    if (actuallyWritten < 0) {
      ckfree (bufPtr);
      Tcl_AppendResult(interp, argv[0], ": ", Tcl_GetChannelName(outChan),
		       Tcl_PosixError(interp), (char *) NULL);
      return TCL_ERROR;
    }
  }

  ckfree(bufPtr);
    
  sprintf(interp->result, "%d", totalRead);
  return TCL_OK;
}

/*
 *------------------------------------------------------*
 *
 *	PackCmd --
 *
 *	------------------------------------------------*
 *	This procedure realizes the 'binio pack' command.
 *	See the manpages for details on what it does.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		See user documentation.
 *
 *	Result:
 *		A standard tcl error code.
 *
 *------------------------------------------------------*
 */
	/* ARGSUSED */
static int
PackCmd (interp, argc, argv)
Tcl_Interp* interp;     /* The interpreter we are working in */
int         argc;	/* # arguments */
char**      argv;	/* trailing arguments */
{
  Tcl_Channel outChan;	/* The channel to write to */
  char*       format;
  conversion  cvt;
  char        buffer [50];
  char*       bufPtr = (char*) NULL;
  int         bufLen = 0;
  int         packed, actuallyWritten, reorder, mode;

  /*
   * Allowed syntax:
   * 	outChannel format ?data1 data2 ...?
   */

  if (argc < 2) {
    Tcl_AppendResult(interp,
		     "wrong # args: should be \"binio pack outChannel format ?data1 data2 ...?\"",
		     (char *) NULL);
    return TCL_ERROR;
  }

  outChan = Tcl_GetChannel(interp, argv[0], &mode);
  if (outChan == (Tcl_Channel) NULL) {
    return TCL_ERROR;
  }

  if ((mode & TCL_WRITABLE) == 0) {
    Tcl_AppendResult(interp, "channel \"", argv[0],
		     "\" wasn't opened for writing", (char *) NULL);
    return TCL_ERROR;
  }

  format = argv [1];
  argc  -= 2;
  argv  += 2;

  for (packed = 0 ; format [0] != '\0'; format += 2, argc --, argv ++, packed ++) {
    if (format [0] != '%') {
      char buf [3];
      buf [0] = format [0];
      buf [1] = format [1];
      buf [2] = '\0';

      Tcl_AppendResult (interp, "unknown format specification '", buf, "'", (char*) NULL);
      return TCL_ERROR;
    }

    if (argc == 0) {
      Tcl_AppendResult (interp, "more format specifiers than data items", (char*) NULL);
      return TCL_ERROR;      
    }

    reorder = 1; /* prepare for usual case */

    /*
     * Possible specifications:
     * - %d specifies that the corresponding value is a four byte signed int.
     * - %u specifies that the corresponding value is a four byte unsigned int.
     * - %o specifies that the corresponding value is a four byte octal signed int.
     * - %x specifies that the corresponding value is a four byte hexadecimal signed int.
     * - %l specifies that the corresponding value is an eight byte signed int.
     * - %L specifies that the corresponding value is an eight byte unsigned int.
     * - %D specifies that the corresponding value is a two byte signed int.
     * - %U specifies that the corresponding value is a two byte unsigned int.
     * - %O specifies that the corresponding value is a two byte octal signed int.
     * - %X specifies that the corresponding value is a two byte hexadecimal signed int.
     * - %c specifies that the corresponding value is a one byte signed int (char).
     * - %C specifies that the corresponding value is a one byte unsigned int.
     * - %f specifies that the corresponding value is a four byte floating point number.
     * - %F specifies that the corresponding value is an eight byte floating point number.
     * - %s specifies that the corresponding value is a NULL terminated string.
     */

    switch (format [1]) {
    case 'd':
    case 'u':
    case 'l':
    case 'L':
    case 'D':
    case 'U':
    case 'c':
    case 'C':
      if (TCL_OK != Tcl_GetInt (interp, argv [0], &cvt.i)) {
	return TCL_ERROR;
      }

      switch (format [1]) {
      case 'd':
	bufPtr = (char*) &cvt.i;
	bufLen = sizeof (int);
	break;

      case 'u':
	cvt.ui = (unsigned int) cvt.i;
	bufPtr = (char*) &cvt.ui;
	bufLen = sizeof (unsigned int);
	break;

      case 'l':
	cvt.li = (long int) cvt.i;
	bufPtr = (char*) &cvt.li;
	bufLen = sizeof (long int);
	break;

      case 'L':
	cvt.ul = (unsigned long) cvt.i;
	bufPtr = (char*) &cvt.ul;
	bufLen = sizeof (unsigned long);
	break;

      case 'D':
	cvt.si = (short int) cvt.i;
	bufPtr = (char*) &cvt.si;
	bufLen = sizeof (short int);
	break;

      case 'U':
	cvt.us = (short int) cvt.i;
	bufPtr = (char*) &cvt.us;
	bufLen = sizeof (unsigned short);
	break;

      case 'c':
	cvt.c = (char) cvt.i;
	bufPtr = (char*) &cvt.c;
	bufLen = sizeof (char);
	break;

      case 'C':
	cvt.uc = (unsigned char) cvt.i;
	bufPtr = (char*) &cvt.uc;
	bufLen = sizeof (unsigned char);
	break;
      } /* switch */
      break;

    case 'o':
    case 'O':
      if (TCL_OK != GetOctal (interp, argv [0], &cvt.li)) {
	return TCL_ERROR;
      }

      if (format [1] == 'O') {
	cvt.si = (short int) cvt.i;
	bufPtr = (char*) &cvt.si;
	bufLen = sizeof (short int);
      } else {
	cvt.i  = (int) cvt.li;
	bufPtr = (char*) &cvt.i;
	bufLen = sizeof (int);
      }
      break;

    case 'x':
    case 'X':
      if (TCL_OK != GetHex (interp, argv [0], &cvt.li)) {
	return TCL_ERROR;
      }

      if (format [1] == 'X') {
	cvt.si = (short int) cvt.i;
	bufPtr = (char*) &cvt.si;
	bufLen = sizeof (short int);
      } else {
	cvt.i  = (int) cvt.li;
	bufPtr = (char*) &cvt.i;
	bufLen = sizeof (int);
      }
      break;

    case 'f':
    case 'F':
      if (TCL_OK != Tcl_GetDouble (interp, argv [0], &cvt.d)) {
	return TCL_ERROR;
      }

      if (format [1] == 'f') {
	cvt.f = (float) cvt.d;
	bufPtr = (char*) &cvt.f;
	bufLen = sizeof (float);
      } else {
	bufPtr = (char*) &cvt.d;
	bufLen = sizeof (double);
      }
      break;

    case 's':
      bufPtr  = argv [0];
      bufLen  = strlen (argv [0]);
      reorder = 0;
      break;
    } /* switch */


    /* check, wether reordering is required or not.
     * upon answer `yes` do the reordering here too.
     */
    if ((bufLen > 1) && reorder &&
	(Tcl_GetHostByteorder () != Tcl_GetChannelByteorder (outChan))) {
      ReorderBytes (bufPtr, bufLen);
    }

    actuallyWritten = Tcl_Write (outChan, bufPtr, bufLen);
    if (actuallyWritten < 0) {
      Tcl_AppendResult(interp, "binio pack: ", Tcl_GetChannelName(outChan),
		       Tcl_PosixError(interp), (char *) NULL);
      return TCL_ERROR;
    }
  } /* for (format) */

  /* return number of packed items */
  sprintf (buffer, "%d", packed);
  Tcl_AppendResult (interp, buffer, (char*) NULL);
  return TCL_OK;
}

/*
 *------------------------------------------------------*
 *
 *	UnpackCmd --
 *
 *	------------------------------------------------*
 *	This procedure realizes the 'binio unpack' command.
 *	See the manpages for details on what it does.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		See user documentation.
 *
 *	Result:
 *		A standard tcl error code.
 *
 *------------------------------------------------------*
 */
	/* ARGSUSED */
static int
UnpackCmd (interp, argc, argv)
Tcl_Interp* interp;     /* The interpreter we are working in */
int         argc;	/* # arguments */
char**      argv;	/* trailing arguments */
{
  Tcl_Channel inChan;		/* The channel to read from */
  conversion  cvt;
  int         mode, unpacked, actuallyRead;
  int         length;   /* length of single item,
			 * < 0 => variable length (string)
			 * 0 is illegal.
			 */
  char        buffer [50];   /* to hold most of the read information (and its conversion) */
  char*       format;
  char*       val;


  /*
   * Allowed syntax:
   * 	format ?var1 var2 ...?
   */

  if (argc < 2) {
    Tcl_AppendResult(interp,
		     "wrong # args: should be \"binio unpack outChannel format ?var1 var2 ...?\"",
		     (char *) NULL);
    return TCL_ERROR;
  }

  inChan = Tcl_GetChannel(interp, argv[0], &mode);
  if (inChan == (Tcl_Channel) NULL) {
    return TCL_ERROR;
  }

  if ((mode & TCL_READABLE) == 0) {
    Tcl_AppendResult(interp, "channel \"", argv[0],
		     "\" wasn't opened for reading", (char *) NULL);
    return TCL_ERROR;
  }

  if (Tcl_Eof (inChan)) {
    /*
     * cannot convert behind end of channel.
     * no error, just unpack nothing !
     */

    Tcl_AppendResult (interp, "0", (char*) NULL);
    return TCL_OK;
  }

  format = argv [1];
  argc  -= 2;
  argv  += 2;

  for (unpacked = 0 ; format [0] != '\0'; format += 2, argc --, argv ++, unpacked ++) {
    if (format [0] != '%') {
      char buf [3];
      buf [0] = format [0];
      buf [1] = format [1];
      buf [2] = '\0';

      Tcl_AppendResult (interp, "unknown format specification '", buf, "'", (char*) NULL);
      return TCL_ERROR;
    }

    if (argc == 0) {
      Tcl_AppendResult (interp, "more format specifiers than variables", (char*) NULL);
      return TCL_ERROR;      
    }

    length  = 0; /* illegal marker, to catch missing cases later */

    /*
     * Possible specifications:
     * - %d specifies that the corresponding value is a four byte signed int.
     * - %u specifies that the corresponding value is a four byte unsigned int.
     * - %o specifies that the corresponding value is a four byte octal signed int.
     * - %x specifies that the corresponding value is a four byte hexadecimal signed int.
     * - %f specifies that the corresponding value is a four byte floating point number.
     *
     * - %l specifies that the corresponding value is an eight byte signed int.
     * - %L specifies that the corresponding value is an eight byte unsigned int.
     * - %F specifies that the corresponding value is an eight byte floating point number.
     *
     * - %D specifies that the corresponding value is a two byte signed int.
     * - %U specifies that the corresponding value is a two byte unsigned int.
     * - %O specifies that the corresponding value is a two byte octal signed int.
     * - %X specifies that the corresponding value is a two byte hexadecimal signed int.
     *
     * - %c specifies that the corresponding value is a one byte signed int (char).
     * - %C specifies that the corresponding value is a one byte unsigned int.
     *
     * - %s specifies that the corresponding value is a NULL terminated string.
     */

    /* first: determine number of bytes required, then read these.
     * at last do the conversion and write into the associated variable.
     */

    switch (format [1]) {
    case 'l':
    case 'L':
#if SIZEOF_LONG_INT != 8
      Tcl_AppendResult (interp, "binio unpack: %l / %L not supported, no 8byte integers here", NULL);
      return TCL_ERROR;
#endif
    case 'F':
      length = 8;
      break;

    case 'd':
    case 'u':
    case 'o':
    case 'x':
    case 'f':
      length = 4;
      break;

    case 'D':
    case 'U':
    case 'O':
    case 'X':
      length = 2;
      break;

    case 'c':
    case 'C':
      length = 1;
      break;

    case 's':
      length = -1; /* variable length, string terminated by '\0'. */
      break;
    }

    if (length == 0) {
      format [2] = '\0';
      Tcl_AppendResult (interp, "binio unpack: internal error, missing case for format ", format, NULL);
      return TCL_ERROR;
    } else if (length < 0) {
      /* variable length, string terminated by '\0'. (%s) */

      Tcl_DString data;
      Tcl_DStringInit (&data);

      while (! Tcl_Eof (inChan)) {
	actuallyRead = Tcl_Read (inChan, buffer, 1);

	if (actuallyRead < 0) {
	  Tcl_AppendResult(interp, "binio unpack: ", Tcl_GetChannelName(inChan),
			   Tcl_PosixError(interp), (char *) NULL);
	  return TCL_ERROR;
	} else if (actuallyRead > 0) {
	  Tcl_DStringAppend (&data, buffer, 1);
	  if (buffer [0] == '\0') {
	    break;
	  }
	}
      } /* while */

      val = Tcl_SetVar (interp, argv [0], data.string, TCL_LEAVE_ERR_MSG);
      Tcl_DStringFree (&data);

      if (val == NULL) {
	return TCL_ERROR;
      }
    } else {
      /* handle item with fixed lengths */


      actuallyRead = Tcl_Read (inChan, buffer, length);
      if (actuallyRead < 0) {
	Tcl_AppendResult(interp, "binio unpack: ", Tcl_GetChannelName(inChan),
			 Tcl_PosixError(interp), (char *) NULL);
	return TCL_ERROR;
      }

      /* check, wether reordering is required or not.
       * upon answer `yes` do the reordering here too.
       */

      if ((length > 1) &&
	  (Tcl_GetHostByteorder () != Tcl_GetChannelByteorder (inChan))) {
	ReorderBytes (buffer, length);
      }

      switch (format [1]) {
      case 'd':
#if SIZEOF_INT == 4
	/* 'int' is our 4 byte integer on this machine */
	memcpy ((VOID*) &cvt.i, (VOID*) buffer, length);
	sprintf (buffer, "%d", cvt.i);
#else
	/* 'int' seems to be equal to 'short' (2 byte), so use 'long int' instead */
	memcpy ((VOID*) &cvt.li, (VOID*) buffer, length);
	sprintf (buffer, "%ld", cvt.li);
#endif
	break;

      case 'o':
#if SIZEOF_INT == 4
	/* 'int' is our 4 byte integer on this machine */
	memcpy ((VOID*) &cvt.i, (VOID*) buffer, length);
	sprintf (buffer, "%o", cvt.i);
#else
	/* 'int' seems to be equal to 'short' (2 byte), so use 'long int' instead */
	memcpy ((VOID*) &cvt.li, (VOID*) buffer, length);
	sprintf (buffer, "%lo", cvt.li);
#endif
	break;

      case 'x':
#if SIZEOF_INT == 4
	/* 'int' is our 4 byte integer on this machine */
	memcpy ((VOID*) &cvt.i, (VOID*) buffer, length);
	sprintf (buffer, "%08x", cvt.i);
#else
	/* 'int' seems to be equal to 'short' (2 byte), so use 'long int' instead */
	memcpy ((VOID*) &cvt.li, (VOID*) buffer, length);
	sprintf (buffer, "%08lx", cvt.li);
#endif
	break;

      case 'u':
#if SIZEOF_INT == 4
	/* 'unsigned int' is our 4 byte integer on this machine */
	memcpy ((VOID*) &cvt.ui, (VOID*) buffer, length);
	sprintf (buffer, "%u", cvt.ui);
#else
	/* 'int' seems to be equal to 'short' (2 byte), so use 'unsigned long' instead */
	memcpy ((VOID*) &cvt.ul, (VOID*) buffer, length);
	sprintf (buffer, "%lu", cvt.ul);
#endif
	break;

      case 'D':
	/* 'short int' is our 2 byte integer on this machine */
	memcpy ((VOID*) &cvt.si, (VOID*) buffer, length);
	sprintf (buffer, "%d", cvt.si);
	break;

      case 'O':
	/* 'short int' is our 2 byte integer on this machine */
	memcpy ((VOID*) &cvt.si, (VOID*) buffer, length);
	sprintf (buffer, "%o", cvt.si);
	break;

      case 'X':
	/* 'short int' is our 2 byte integer on this machine */
	memcpy ((VOID*) &cvt.si, (VOID*) buffer, length);
	sprintf (buffer, "%04x", cvt.si);
	break;

      case 'U':
	/* 'unsigned short' is our 2 byte integer on this machine */
	memcpy ((VOID*) &cvt.us, (VOID*) buffer, length);
	sprintf (buffer, "%u", cvt.us);
	break;

      case 'l':
	/* assume SIZEOF_LONG_INT == 8 */
	memcpy ((VOID*) &cvt.li, (VOID*) buffer, length);
	sprintf (buffer, "%ld", cvt.li);
	break;

      case 'L':
	/* assume SIZEOF_LONG_INT == 8 */
	memcpy ((VOID*) &cvt.ul, (VOID*) buffer, length);
	sprintf (buffer, "%lu", cvt.ul);
	break;

      case 'c':
	memcpy ((VOID*) &cvt.c, (VOID*) buffer, length);
	cvt.i = cvt.c;
	sprintf (buffer, "%d", cvt.i);
	break;

      case 'C':
	memcpy ((VOID*) &cvt.uc, (VOID*) buffer, length);
	cvt.ui = cvt.uc;
	sprintf (buffer, "%u", cvt.ui);
	break;

      case 'f':
	memcpy ((VOID*) &cvt.f, (VOID*) buffer, length);
	sprintf (buffer, "%f", cvt.f);
	break;

      case 'F':
	memcpy ((VOID*) &cvt.d, (VOID*) buffer, length);
	sprintf (buffer, "%f", cvt.d);
	break;

      case 's':
	Tcl_AppendResult (interp, "binio unpack: internal error, wrong branch for %s", NULL);
	return TCL_ERROR;
	break;
      } /* switch */

      val = Tcl_SetVar (interp, argv [0], buffer, TCL_LEAVE_ERR_MSG);
      if (val == NULL) {
	return TCL_ERROR;
      }      
    } /* if (length < 0) */
  } /* for (format) */

  /* return number of unpacked items */
  sprintf (buffer, "%d", unpacked);
  Tcl_AppendResult (interp, buffer, (char*) NULL);
  return TCL_OK;
}

/*
 *------------------------------------------------------*
 *
 *	ReorderBytes --
 *
 *	------------------------------------------------*
 *	This procedure reorders the bytes in a buffer to
 *	match real and intended byteorder.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		See above.
 *
 *	Result:
 *		The incoming buffer 'buf' contains the
 *		reorderd bytes.
 *
 *------------------------------------------------------*
 */

static void
ReorderBytes (buf, len)
char* buf;
int   len;
{
#define FLIP(a,b) c = buf [a]; buf [a] = buf [b]; buf [b] = c;

  char c;

  if (len == 2) {
    FLIP (0,1);
  } else if (len == 4) {
    FLIP (0,3);
    FLIP (1,2);
  } else if (len == 8) {
    FLIP (0,7);
    FLIP (1,6);
    FLIP (2,5);
    FLIP (3,4);
  } else {
    Tcl_Panic ("unknown buffer size %d", len);
  }
}

/*
 *------------------------------------------------------*
 *
 *	GetHex --
 *
 *	------------------------------------------------*
 *	Read a string containing a number in hexadecimal
 *	representation and convert it into a long integer.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		See above.
 *
 *	Result:
 *		'result' contains the conversion result.
 *		A standard tcl error code.
 *
 *------------------------------------------------------*
 */

static int
GetHex (interp, text, result)
Tcl_Interp* interp;
char*       text;
long int*   result;
{
  int match;
  match = sscanf (text, "%lx", result);

  if (match != 1) {
    Tcl_AppendResult (interp, "expected hexadecimal integer, but got \"",
		      text, "\"", (char*) NULL);
    return TCL_ERROR;
  }

  return TCL_OK;
}

/*
 *------------------------------------------------------*
 *
 *	GetOctal --
 *
 *	------------------------------------------------*
 *	Read a string containing a number in octal
 *	representation and convert it into a long integer.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		See above.
 *
 *	Result:
 *		'result' contains the conversion result.
 *		A standard tcl error code.
 *
 *------------------------------------------------------*
 */

static int
GetOctal (interp, text, result)
Tcl_Interp* interp;
char*       text;
long int*   result;
{
  int match;
  match = sscanf (text, "%lo", result);

  if (match != 1) {
    Tcl_AppendResult (interp, "expected octal integer, but got \"",
		      text, "\"", (char*) NULL);
    return TCL_ERROR;
  }

  return TCL_OK;
}

/*
 *------------------------------------------------------*
 *
 *	BinioCmd --
 *
 *	------------------------------------------------*
 *	This procedure realizes the 'binio' command.
 *	See the manpages for details on what it does.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		See the user documentation.
 *
 *	Result:
 *		A standard Tcl result.
 *
 *------------------------------------------------------*
 */
	/* ARGSUSED */
static int
BinioCmd (notUsed, interp, argc, argv)
ClientData  notUsed;		/* Not used. */
Tcl_Interp* interp;		/* Current interpreter. */
int         argc;		/* Number of arguments. */
char**      argv;		/* Argument strings. */
{
  /*
   * Allowed syntax:
   *
   * binio copy   inChannel outChannel ?count?
   * binio pack   outChannel format ?data1 data2 ...?
   * binio unpack inChannel  format ?var1 var2 ...?
   */

  int len;
  char c;
  Tcl_Channel a;
  int mode;

  if (argc < 3) {
    Tcl_AppendResult (interp,
		      "wrong # args: should be \"binio option channel ?arg arg...?\"",
		      (char*) NULL);
    return TCL_ERROR;
  }

  c = argv [1][0];
  len = strlen (argv [1]);

  a = Tcl_GetChannel (interp, argv [2], &mode);

  if (a == (Tcl_Channel) NULL) {
    Tcl_ResetResult (interp);
    Tcl_AppendResult (interp,
		      "binio ", argv [1],
		      ": channel expected as 2nd argument, got \"",
		      argv [2], "\"", (char*) NULL);

    return TCL_ERROR;
  }

  switch (c) {
  case 'c':
    if (0 == strncmp (argv [1], "copy", len)) {
      return CopyCmd (interp, argc - 2, argv + 2);
    } else
      goto unknown_option;
    break;

  case 'p':
    if (0 == strncmp (argv [1], "pack", len)) {
      return PackCmd (interp, argc - 2, argv + 2);
    } else
      goto unknown_option;
    break;

  case 'u':
    if (0 == strncmp (argv [1], "unpack", len)) {
      return UnpackCmd (interp, argc - 2, argv + 2);
    } else
      goto unknown_option;
    break;

  default:
  unknown_option:
    Tcl_AppendResult (interp,
		      "binio: bad option \"", argv [1],
		      "\": should be one of copy, pack or unpack",
		      (char*) NULL);
    return TCL_ERROR;
  }

  return TCL_OK;
}
#endif /* ENABLE_BINIO */

/*
 *------------------------------------------------------*
 *
 *	TrfInit_Binio --
 *
 *	------------------------------------------------*
 *	Initializes this command.
 *	------------------------------------------------*
 *
 *	Sideeffects:
 *		As of 'Tcl_CreateCommand'.
 *
 *	Result:
 *		A standard Tcl error code.
 *
 *------------------------------------------------------*
 */

int
TrfInit_Binio (interp)
Tcl_Interp* interp;
{
#ifdef ENABLE_BINIO
  Tcl_CreateCommand (interp, "binio", BinioCmd,
		     (ClientData) NULL,
		     (Tcl_CmdDeleteProc *) NULL);
#endif /* ENABLE_BINIO */
  return TCL_OK;
}