summaryrefslogtreecommitdiff
path: root/libobjc/archive.c
blob: 10106c1145e7f2880c4929a4031821cd81944b51 (plain)
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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
/* GNU Objective C Runtime archiving
   Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
   Contributed by Kresten Krab Thorup

This file is part of GNU CC.

GNU CC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version.

GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with
GNU CC; see the file COPYING.  If not, write to the Free Software
Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* As a special exception, if you link this library with files compiled with
   GCC to produce an executable, this does not cause the resulting executable
   to be covered by the GNU General Public License. This exception does not
   however invalidate any other reasons why the executable file might be
   covered by the GNU General Public License.  */

#include "tconfig.h"
#include "runtime.h"
#include "typedstream.h"
#include "encoding.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

extern int fflush(FILE*);

#define ROUND(V, A) \
  ({ typeof(V) __v=(V); typeof(A) __a=(A);  \
     __a*((__v+__a-1)/__a); })

#define PTR2LONG(P) (((char*)(P))-(char*)0)
#define LONG2PTR(L) (((char*)0)+(L))

/* Declare some functions... */

static int
objc_read_class (struct objc_typed_stream* stream, Class* class);

int objc_sizeof_type(const char* type);

static int
objc_write_use_common (struct objc_typed_stream* stream, unsigned long key);

static int
objc_write_register_common (struct objc_typed_stream* stream,
			    unsigned long key);

static int 
objc_write_class (struct objc_typed_stream* stream,
			 struct objc_class* class);

const char* objc_skip_type (const char* type);

static void __objc_finish_write_root_object(struct objc_typed_stream*);
static void __objc_finish_read_root_object(struct objc_typed_stream*);

static __inline__ int
__objc_code_unsigned_char (unsigned char* buf, unsigned char val)
{
  if ((val&_B_VALUE) == val)
    {
      buf[0] = val|_B_SINT;
      return 1;
    }
  else
    {
      buf[0] = _B_NINT|0x01;
      buf[1] = val;
      return 2;
    }
}

int
objc_write_unsigned_char (struct objc_typed_stream* stream,
			  unsigned char value)
{
  unsigned char buf[sizeof (unsigned char)+1];
  int len = __objc_code_unsigned_char (buf, value);
  return (*stream->write)(stream->physical, buf, len);
}

static __inline__ int
__objc_code_char (unsigned char* buf, signed char val)
{
  if (val >= 0)
    return __objc_code_unsigned_char (buf, val);
  else
    {
      buf[0] = _B_NINT|_B_SIGN|0x01;
      buf[1] = -val;
      return 2;
    }
}

int
objc_write_char (struct objc_typed_stream* stream, signed char value)
{
  unsigned char buf[sizeof (char)+1];
  int len = __objc_code_char (buf, value);
  return (*stream->write)(stream->physical, buf, len);
}

static __inline__ int
__objc_code_unsigned_short (unsigned char* buf, unsigned short val)
{
  if ((val&_B_VALUE) == val)
    {
      buf[0] = val|_B_SINT;
      return 1;
    }
  else 
    {
      int c, b;

      buf[0] = _B_NINT;

      for (c= sizeof(short); c != 0; c -= 1)
	if (((val>>(8*(c-1)))%0x100) != 0)
	  break;

      buf[0] |= c;

      for (b = 1; c != 0; c--, b++)
	{
	  buf[b] = (val >> (8*(c-1)))%0x100;
	}

      return b;
    }
}

int
objc_write_unsigned_short (struct objc_typed_stream* stream, 
			   unsigned short value)
{
  unsigned char buf[sizeof (unsigned short)+1];
  int len = __objc_code_unsigned_short (buf, value);
  return (*stream->write)(stream->physical, buf, len);
}
      
static __inline__ int
__objc_code_short (unsigned char* buf, short val)
{
  int sign = (val < 0);
  int size = __objc_code_unsigned_short (buf, sign ? -val : val);
  if (sign)
    buf[0] |= _B_SIGN;
  return size;
}

int
objc_write_short (struct objc_typed_stream* stream, short value)
{
  unsigned char buf[sizeof (short)+1];
  int len = __objc_code_short (buf, value);
  return (*stream->write)(stream->physical, buf, len);
}
      

static __inline__ int
__objc_code_unsigned_int (unsigned char* buf, unsigned int val)
{
  if ((val&_B_VALUE) == val)
    {
      buf[0] = val|_B_SINT;
      return 1;
    }
  else 
    {
      int c, b;

      buf[0] = _B_NINT;

      for (c= sizeof(int); c != 0; c -= 1)
	if (((val>>(8*(c-1)))%0x100) != 0)
	  break;

      buf[0] |= c;

      for (b = 1; c != 0; c--, b++)
	{
	  buf[b] = (val >> (8*(c-1)))%0x100;
	}

      return b;
    }
}

int
objc_write_unsigned_int (struct objc_typed_stream* stream, unsigned int value)
{
  unsigned char buf[sizeof(unsigned int)+1];
  int len = __objc_code_unsigned_int (buf, value);
  return (*stream->write)(stream->physical, buf, len);
}

static __inline__ int
__objc_code_int (unsigned char* buf, int val)
{
  int sign = (val < 0);
  int size = __objc_code_unsigned_int (buf, sign ? -val : val);
  if (sign)
    buf[0] |= _B_SIGN;
  return size;
}

int
objc_write_int (struct objc_typed_stream* stream, int value)
{
  unsigned char buf[sizeof(int)+1];
  int len = __objc_code_int (buf, value);
  return (*stream->write)(stream->physical, buf, len);
}

static __inline__ int
__objc_code_unsigned_long (unsigned char* buf, unsigned long val)
{
  if ((val&_B_VALUE) == val)
    {
      buf[0] = val|_B_SINT;
      return 1;
    }
  else 
    {
      int c, b;

      buf[0] = _B_NINT;

      for (c= sizeof(long); c != 0; c -= 1)
	if (((val>>(8*(c-1)))%0x100) != 0)
	  break;

      buf[0] |= c;

      for (b = 1; c != 0; c--, b++)
	{
	  buf[b] = (val >> (8*(c-1)))%0x100;
	}

      return b;
    }
}

int
objc_write_unsigned_long (struct objc_typed_stream* stream, 
			  unsigned long value)
{
  unsigned char buf[sizeof(unsigned long)+1];
  int len = __objc_code_unsigned_long (buf, value);
  return (*stream->write)(stream->physical, buf, len);
}

static __inline__ int
__objc_code_long (unsigned char* buf, long val)
{
  int sign = (val < 0);
  int size = __objc_code_unsigned_long (buf, sign ? -val : val);
  if (sign)
    buf[0] |= _B_SIGN;
  return size;
}

int
objc_write_long (struct objc_typed_stream* stream, long value)
{
  unsigned char buf[sizeof(long)+1];
  int len = __objc_code_long (buf, value);
  return (*stream->write)(stream->physical, buf, len);
}


int
objc_write_string (struct objc_typed_stream* stream,
		   const unsigned char* string, unsigned int nbytes)
{
  unsigned char buf[sizeof(unsigned int)+1];
  int len = __objc_code_unsigned_int (buf, nbytes);
  
  if ((buf[0]&_B_CODE) == _B_SINT)
    buf[0] = (buf[0]&_B_VALUE)|_B_SSTR;

  else /* _B_NINT */
    buf[0] = (buf[0]&_B_VALUE)|_B_NSTR;

  if ((*stream->write)(stream->physical, buf, len) != 0)
    return (*stream->write)(stream->physical, string, nbytes);
  else
    return 0;
}

int
objc_write_string_atomic (struct objc_typed_stream* stream,
			  unsigned char* string, unsigned int nbytes)
{
  unsigned long key;
  if ((key = PTR2LONG(hash_value_for_key (stream->stream_table, string))))
    return objc_write_use_common (stream, key);
  else
    {
      int length;
      hash_add (&stream->stream_table, LONG2PTR(key=PTR2LONG(string)), string);
      if ((length = objc_write_register_common (stream, key)))
	return objc_write_string (stream, string, nbytes);
      return length;
    }
}

static int
objc_write_register_common (struct objc_typed_stream* stream, 
			    unsigned long key)
{
  unsigned char buf[sizeof (unsigned long)+2];
  int len = __objc_code_unsigned_long (buf+1, key);
  if (len == 1)
    {
      buf[0] = _B_RCOMM|0x01;
      buf[1] &= _B_VALUE;
      return (*stream->write)(stream->physical, buf, len+1);
    }
  else
    {
      buf[1] = (buf[1]&_B_VALUE)|_B_RCOMM;
      return (*stream->write)(stream->physical, buf+1, len);
    }
}

static int
objc_write_use_common (struct objc_typed_stream* stream, unsigned long key)
{
  unsigned char buf[sizeof (unsigned long)+2];
  int len = __objc_code_unsigned_long (buf+1, key);
  if (len == 1)
    {
      buf[0] = _B_UCOMM|0x01;
      buf[1] &= _B_VALUE;
      return (*stream->write)(stream->physical, buf, 2);
    }
  else
    {
      buf[1] = (buf[1]&_B_VALUE)|_B_UCOMM;
      return (*stream->write)(stream->physical, buf+1, len);
    }
}

static __inline__ int
__objc_write_extension (struct objc_typed_stream* stream, unsigned char code)
{
  if (code <= _B_VALUE)
    {
      unsigned char buf = code|_B_EXT;
      return (*stream->write)(stream->physical, &buf, 1);
    }
  else 
    {
      objc_error(nil, OBJC_ERR_BAD_OPCODE,
		 "__objc_write_extension: bad opcode %c\n", code);
      return -1;
    }
}

__inline__ int
__objc_write_object (struct objc_typed_stream* stream, id object)
{
  unsigned char buf = '\0';
  SEL write_sel = sel_get_any_uid ("write:");
  if (object)
    {
      __objc_write_extension (stream, _BX_OBJECT);
      objc_write_class (stream, object->class_pointer);
      (*objc_msg_lookup(object, write_sel))(object, write_sel, stream);
      return (*stream->write)(stream->physical, &buf, 1);
    }
  else
    return objc_write_use_common(stream, 0);
}

int 
objc_write_object_reference (struct objc_typed_stream* stream, id object)
{
  unsigned long key;
  if ((key = PTR2LONG(hash_value_for_key (stream->object_table, object))))
    return objc_write_use_common (stream, key);

  __objc_write_extension (stream, _BX_OBJREF);
  return objc_write_unsigned_long (stream, PTR2LONG (object));
}

int 
objc_write_root_object (struct objc_typed_stream* stream, id object)
{
  int len = 0;
  if (stream->writing_root_p)
    objc_error (nil, OBJC_ERR_RECURSE_ROOT, 
		"objc_write_root_object called recursively");
  else
    {
      stream->writing_root_p = 1;
      __objc_write_extension (stream, _BX_OBJROOT);
      if((len = objc_write_object (stream, object)))
	__objc_finish_write_root_object(stream);
      stream->writing_root_p = 0;
    }
  return len;
}

int 
objc_write_object (struct objc_typed_stream* stream, id object)
{
  unsigned long key;
  if ((key = PTR2LONG(hash_value_for_key (stream->object_table, object))))
    return objc_write_use_common (stream, key);

  else if (object == nil)
    return objc_write_use_common(stream, 0);

  else
    {
      int length;
      hash_add (&stream->object_table, LONG2PTR(key=PTR2LONG(object)), object);
      if ((length = objc_write_register_common (stream, key)))
	return __objc_write_object (stream, object);
      return length;
    }
}

__inline__ int
__objc_write_class (struct objc_typed_stream* stream, struct objc_class* class)
{
  __objc_write_extension (stream, _BX_CLASS);
  objc_write_string_atomic(stream, (char*)class->name,
			   strlen((char*)class->name));
  return objc_write_unsigned_long (stream, class->version);
}


static int 
objc_write_class (struct objc_typed_stream* stream,
			 struct objc_class* class)
{
  unsigned long key;
  if ((key = PTR2LONG(hash_value_for_key (stream->stream_table, class))))
    return objc_write_use_common (stream, key);
  else
    {
      int length;
      hash_add (&stream->stream_table, LONG2PTR(key=PTR2LONG(class)), class);
      if ((length = objc_write_register_common (stream, key)))
	return __objc_write_class (stream, class);
      return length;
    }
}


__inline__ int 
__objc_write_selector (struct objc_typed_stream* stream, SEL selector)
{
  const char* sel_name;
  __objc_write_extension (stream, _BX_SEL);
  /* to handle NULL selectors */
  if ((SEL)0 == selector)
    return objc_write_string (stream, "", 0);
  sel_name = sel_get_name (selector);
  return objc_write_string (stream, sel_name, strlen ((char*)sel_name));
}

int 
objc_write_selector (struct objc_typed_stream* stream, SEL selector)
{
  const char* sel_name;
  unsigned long key;

  /* to handle NULL selectors */
  if ((SEL)0 == selector)
    return __objc_write_selector (stream, selector);

  sel_name = sel_get_name (selector);
  if ((key = PTR2LONG(hash_value_for_key (stream->stream_table, sel_name))))
    return objc_write_use_common (stream, key);
  else
    {
      int length;
      hash_add (&stream->stream_table, 
		LONG2PTR(key=PTR2LONG(sel_name)), (char*)sel_name);
      if ((length = objc_write_register_common (stream, key)))
	return __objc_write_selector (stream, selector);
      return length;
    }
}



/*
** Read operations 
*/

__inline__ int
objc_read_char (struct objc_typed_stream* stream, char* val)
{
  unsigned char buf;
  int len;
  len = (*stream->read)(stream->physical, &buf, 1);
  if (len != 0)
    {
      if ((buf & _B_CODE) == _B_SINT)
	(*val) = (buf & _B_VALUE);

      else if ((buf & _B_NUMBER) == 1)
	{
	  len = (*stream->read)(stream->physical, val, 1);
	  if (buf&_B_SIGN)
	    (*val) = -1*(*val);
	}

      else
	objc_error(nil, OBJC_ERR_BAD_DATA,
		   "expected 8bit signed int, got %dbit int",
		   (int)(buf&_B_NUMBER)*8);
    }
  return len;
}


__inline__ int
objc_read_unsigned_char (struct objc_typed_stream* stream, unsigned char* val)
{
  unsigned char buf;
  int len;
  if ((len = (*stream->read)(stream->physical, &buf, 1)))
    {
      if ((buf & _B_CODE) == _B_SINT)
	(*val) = (buf & _B_VALUE);

      else if ((buf & _B_NUMBER) == 1)
	len = (*stream->read)(stream->physical, val, 1);

      else
	objc_error(nil, OBJC_ERR_BAD_DATA,
		   "expected 8bit unsigned int, got %dbit int",
		   (int)(buf&_B_NUMBER)*8);
    }
  return len;
}

__inline__ int
objc_read_short (struct objc_typed_stream* stream, short* value)
{
  unsigned char buf[sizeof(short)+1];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      if ((buf[0] & _B_CODE) == _B_SINT)
	(*value) = (buf[0] & _B_VALUE);

      else
	{
	  int pos = 1;
	  int nbytes = buf[0] & _B_NUMBER;
	  if (nbytes > sizeof (short))
	    objc_error(nil, OBJC_ERR_BAD_DATA,
		       "expected short, got bigger (%dbits)", nbytes*8);
	  len = (*stream->read)(stream->physical, buf+1, nbytes);
	  (*value) = 0;
	  while (pos <= nbytes)
	    (*value) = ((*value)*0x100) + buf[pos++];
	  if (buf[0] & _B_SIGN)
	    (*value) = -(*value);
	}
    }
  return len;
}

__inline__ int
objc_read_unsigned_short (struct objc_typed_stream* stream,
			  unsigned short* value)
{
  unsigned char buf[sizeof(unsigned short)+1];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      if ((buf[0] & _B_CODE) == _B_SINT)
	(*value) = (buf[0] & _B_VALUE);

      else
	{
	  int pos = 1;
	  int nbytes = buf[0] & _B_NUMBER;
	  if (nbytes > sizeof (short))
	    objc_error(nil, OBJC_ERR_BAD_DATA,
		       "expected short, got int or bigger");
	  len = (*stream->read)(stream->physical, buf+1, nbytes);
	  (*value) = 0;
	  while (pos <= nbytes)
	    (*value) = ((*value)*0x100) + buf[pos++];
	}
    }
  return len;
}


__inline__ int
objc_read_int (struct objc_typed_stream* stream, int* value)
{
  unsigned char buf[sizeof(int)+1];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      if ((buf[0] & _B_CODE) == _B_SINT)
	(*value) = (buf[0] & _B_VALUE);

      else
	{
	  int pos = 1;
	  int nbytes = buf[0] & _B_NUMBER;
	  if (nbytes > sizeof (int))
	    objc_error(nil, OBJC_ERR_BAD_DATA, "expected int, got bigger");
	  len = (*stream->read)(stream->physical, buf+1, nbytes);
	  (*value) = 0;
	  while (pos <= nbytes)
	    (*value) = ((*value)*0x100) + buf[pos++];
	  if (buf[0] & _B_SIGN)
	    (*value) = -(*value);
	}
    }
  return len;
}

__inline__ int
objc_read_long (struct objc_typed_stream* stream, long* value)
{
  unsigned char buf[sizeof(long)+1];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      if ((buf[0] & _B_CODE) == _B_SINT)
	(*value) = (buf[0] & _B_VALUE);

      else
	{
	  int pos = 1;
	  int nbytes = buf[0] & _B_NUMBER;
	  if (nbytes > sizeof (long))
	    objc_error(nil, OBJC_ERR_BAD_DATA, "expected long, got bigger");
	  len = (*stream->read)(stream->physical, buf+1, nbytes);
	  (*value) = 0;
	  while (pos <= nbytes)
	    (*value) = ((*value)*0x100) + buf[pos++];
	  if (buf[0] & _B_SIGN)
	    (*value) = -(*value);
	}
    }
  return len;
}

__inline__ int
__objc_read_nbyte_uint (struct objc_typed_stream* stream,
		       unsigned int nbytes, unsigned int* val)
{
  int len, pos = 0;
  unsigned char buf[sizeof(unsigned int)+1];

  if (nbytes > sizeof (int))
    objc_error(nil, OBJC_ERR_BAD_DATA, "expected int, got bigger");

  len = (*stream->read)(stream->physical, buf, nbytes);
  (*val) = 0;
  while (pos < nbytes)
    (*val) = ((*val)*0x100) + buf[pos++];
  return len;
}
  

__inline__ int
objc_read_unsigned_int (struct objc_typed_stream* stream,
			unsigned int* value)
{
  unsigned char buf[sizeof(unsigned int)+1];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      if ((buf[0] & _B_CODE) == _B_SINT)
	(*value) = (buf[0] & _B_VALUE);

      else
	len = __objc_read_nbyte_uint (stream, (buf[0] & _B_VALUE), value);

    }
  return len;
}

int
__objc_read_nbyte_ulong (struct objc_typed_stream* stream,
		       unsigned int nbytes, unsigned long* val)
{
  int len, pos = 0;
  unsigned char buf[sizeof(unsigned long)+1];

  if (nbytes > sizeof (long))
    objc_error(nil, OBJC_ERR_BAD_DATA, "expected long, got bigger");

  len = (*stream->read)(stream->physical, buf, nbytes);
  (*val) = 0;
  while (pos < nbytes)
    (*val) = ((*val)*0x100) + buf[pos++];
  return len;
}
  

__inline__ int
objc_read_unsigned_long (struct objc_typed_stream* stream,
			unsigned long* value)
{
  unsigned char buf[sizeof(unsigned long)+1];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      if ((buf[0] & _B_CODE) == _B_SINT)
	(*value) = (buf[0] & _B_VALUE);

      else
	len = __objc_read_nbyte_ulong (stream, (buf[0] & _B_VALUE), value);

    }
  return len;
}

__inline__ int
objc_read_string (struct objc_typed_stream* stream,
		  char** string)
{
  unsigned char buf[sizeof(unsigned int)+1];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      unsigned long key = 0;

      if ((buf[0]&_B_CODE) == _B_RCOMM)	/* register following */
	{
	  len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
	  len = (*stream->read)(stream->physical, buf, 1);
	}

      switch (buf[0]&_B_CODE) {
      case _B_SSTR:
	{
	  int length = buf[0]&_B_VALUE;
	  (*string) = (char*)objc_malloc(length+1);
	  if (key)
	    hash_add (&stream->stream_table, LONG2PTR(key), *string);
	  len = (*stream->read)(stream->physical, *string, length);
	  (*string)[length] = '\0';
	}
	break;

      case _B_UCOMM:
	{
	  char *tmp;
	  len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
	  tmp = hash_value_for_key (stream->stream_table, LONG2PTR (key));
	  *string = objc_malloc (strlen(tmp) + 1);
	  strcpy (*string, tmp);
	}
	break;

      case _B_NSTR:
	{
	  unsigned int nbytes = buf[0]&_B_VALUE;
	  len = __objc_read_nbyte_uint(stream, nbytes, &nbytes);
	  if (len) {
	    (*string) = (char*)objc_malloc(nbytes+1);
	    if (key)
	      hash_add (&stream->stream_table, LONG2PTR(key), *string);
	    len = (*stream->read)(stream->physical, *string, nbytes);
	    (*string)[nbytes] = '\0';
	  }
	}
	break;
	
      default:
	objc_error(nil, OBJC_ERR_BAD_DATA,
		   "expected string, got opcode %c\n", (buf[0]&_B_CODE));
      }
    }

  return len;
}


int
objc_read_object (struct objc_typed_stream* stream, id* object)
{
  unsigned char buf[sizeof (unsigned int)];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      SEL read_sel = sel_get_any_uid ("read:");
      unsigned long key = 0;

      if ((buf[0]&_B_CODE) == _B_RCOMM)	/* register common */
	{
	  len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
	  len = (*stream->read)(stream->physical, buf, 1);
	}

      if (buf[0] == (_B_EXT | _BX_OBJECT))
	{
	  Class class;

	  /* get class */
	  len = objc_read_class (stream, &class);

	  /* create instance */
	  (*object) = class_create_instance(class);

	  /* register? */
	  if (key)
	    hash_add (&stream->object_table, LONG2PTR(key), *object);

	  /* send -read: */
	  if (__objc_responds_to (*object, read_sel))
	    (*get_imp(class, read_sel))(*object, read_sel, stream);

	  /* check null-byte */
	  len = (*stream->read)(stream->physical, buf, 1);
	  if (buf[0] != '\0')
	    objc_error(nil, OBJC_ERR_BAD_DATA,
		       "expected null-byte, got opcode %c", buf[0]);
	}

      else if ((buf[0]&_B_CODE) == _B_UCOMM)
	{
	  if (key)
	    objc_error(nil, OBJC_ERR_BAD_KEY, "cannot register use upcode...");
	  len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
	  (*object) = hash_value_for_key (stream->object_table, LONG2PTR(key));
	}

      else if (buf[0] == (_B_EXT | _BX_OBJREF))	/* a forward reference */
	{
	  struct objc_list* other;
	  len = objc_read_unsigned_long (stream, &key);
	  other = (struct objc_list*)hash_value_for_key (stream->object_refs, 
							 LONG2PTR(key));
	  hash_add (&stream->object_refs, LONG2PTR(key), 
		    (void*)list_cons(object, other));
	}

      else if (buf[0] == (_B_EXT | _BX_OBJROOT)) /* a root object */
	{
	  if (key)
	    objc_error(nil, OBJC_ERR_BAD_KEY,
		       "cannot register root object...");
	  len = objc_read_object (stream, object);
	  __objc_finish_read_root_object (stream);
	}

      else
	objc_error(nil, OBJC_ERR_BAD_DATA,
		   "expected object, got opcode %c", buf[0]);
    }
  return len;
}

static int
objc_read_class (struct objc_typed_stream* stream, Class* class)
{
  unsigned char buf[sizeof (unsigned int)];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      unsigned long key = 0;

      if ((buf[0]&_B_CODE) == _B_RCOMM)	/* register following */
	{
	  len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
	  len = (*stream->read)(stream->physical, buf, 1);
	}

      if (buf[0] == (_B_EXT | _BX_CLASS))
	{
	  char* class_name;
	  unsigned long version;

	  /* get class */
	  len = objc_read_string (stream, &class_name);
	  (*class) = objc_get_class(class_name);
	  objc_free(class_name);

	  /* register */
	  if (key)
	    hash_add (&stream->stream_table, LONG2PTR(key), *class);

	  objc_read_unsigned_long(stream, &version);
	  hash_add (&stream->class_table, (*class)->name, (void*)version);
	}

      else if ((buf[0]&_B_CODE) == _B_UCOMM)
	{
	  if (key)
	    objc_error(nil, OBJC_ERR_BAD_KEY, "cannot register use upcode...");
	  len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
	  (*class) = hash_value_for_key (stream->stream_table, LONG2PTR(key));
	  if (!*class)
	    objc_error(nil, OBJC_ERR_BAD_CLASS,
		       "cannot find class for key %lu", key);
	}

      else
	objc_error(nil, OBJC_ERR_BAD_DATA,
		   "expected class, got opcode %c", buf[0]);
    }
  return len;
}

int
objc_read_selector (struct objc_typed_stream* stream, SEL* selector)
{
  unsigned char buf[sizeof (unsigned int)];
  int len;
  if ((len = (*stream->read)(stream->physical, buf, 1)))
    {
      unsigned long key = 0;

      if ((buf[0]&_B_CODE) == _B_RCOMM)	/* register following */
	{
	  len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
	  len = (*stream->read)(stream->physical, buf, 1);
	}

      if (buf[0] == (_B_EXT|_BX_SEL)) /* selector! */
	{
	  char* selector_name;

	  /* get selector */
	  len = objc_read_string (stream, &selector_name);
	  /* To handle NULL selectors */
	  if (0 == strlen(selector_name))
	    {
	      (*selector) = (SEL)0;
	      return 0;
	    }
	  else 
	    (*selector) = sel_get_any_uid(selector_name);
	  objc_free(selector_name);

	  /* register */
	  if (key)
	    hash_add (&stream->stream_table, LONG2PTR(key), (void*)*selector);
	}

      else if ((buf[0]&_B_CODE) == _B_UCOMM)
	{
	  if (key)
	    objc_error(nil, OBJC_ERR_BAD_KEY, "cannot register use upcode...");
	  len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
	  (*selector) = hash_value_for_key (stream->stream_table, 
					    LONG2PTR(key));
	}

      else
	objc_error(nil, OBJC_ERR_BAD_DATA,
		   "expected selector, got opcode %c", buf[0]);
    }
  return len;
}

/*
** USER LEVEL FUNCTIONS
*/

/*
** Write one object, encoded in TYPE and pointed to by DATA to the
** typed stream STREAM.  
*/

int
objc_write_type(TypedStream* stream, const char* type, const void* data)
{
  switch(*type) {
  case _C_ID:
    return objc_write_object (stream, *(id*)data);
    break;

  case _C_CLASS:
    return objc_write_class (stream, *(Class*)data);
    break;

  case _C_SEL:
    return objc_write_selector (stream, *(SEL*)data);
    break;

  case _C_CHR:
    return objc_write_char(stream, *(signed char*)data);
    break;
    
  case _C_UCHR:
    return objc_write_unsigned_char(stream, *(unsigned char*)data);
    break;

  case _C_SHT:
    return objc_write_short(stream, *(short*)data);
    break;

  case _C_USHT:
    return objc_write_unsigned_short(stream, *(unsigned short*)data);
    break;

  case _C_INT:
    return objc_write_int(stream, *(int*)data);
    break;

  case _C_UINT:
    return objc_write_unsigned_int(stream, *(unsigned int*)data);
    break;

  case _C_LNG:
    return objc_write_long(stream, *(long*)data);
    break;

  case _C_ULNG:
    return objc_write_unsigned_long(stream, *(unsigned long*)data);
    break;

  case _C_CHARPTR:
    return objc_write_string (stream, *(char**)data, strlen(*(char**)data));
    break;

  case _C_ATOM:
    return objc_write_string_atomic (stream, *(char**)data, 
				     strlen(*(char**)data));
    break;

  case _C_ARY_B:
    {
      int len = atoi(type+1);
      while (isdigit(*++type))
	;
      return objc_write_array (stream, type, len, data);
    }
    break; 

  case _C_STRUCT_B:
    {
      int acc_size = 0;
      int align;
      while (*type != _C_STRUCT_E && *type++ != '=')
	; /* skip "<name>=" */
      while (*type != _C_STRUCT_E)
	{
	  align = objc_alignof_type (type);       /* padd to alignment */
	  acc_size += ROUND (acc_size, align);
	  objc_write_type (stream, type, ((char*)data)+acc_size);
	  acc_size += objc_sizeof_type (type);   /* add component size */
	  type = objc_skip_typespec (type);	 /* skip component */
	}
      return 1;
    }

  default:
    {
      objc_error(nil, OBJC_ERR_BAD_TYPE,
		 "objc_write_type: cannot parse typespec: %s\n", type);
      return 0;
    }
  }
}

/*
** Read one object, encoded in TYPE and pointed to by DATA to the
** typed stream STREAM.  DATA specifies the address of the types to
** read.  Expected type is checked against the type actually present
** on the stream. 
*/

int
objc_read_type(TypedStream* stream, const char* type, void* data)
{
  char c;
  switch(c = *type) {
  case _C_ID:
    return objc_read_object (stream, (id*)data);
    break;

  case _C_CLASS:
    return objc_read_class (stream, (Class*)data);
    break;

  case _C_SEL:
    return objc_read_selector (stream, (SEL*)data);
    break;

  case _C_CHR:
    return objc_read_char (stream, (char*)data);
    break;
    
  case _C_UCHR:
    return objc_read_unsigned_char (stream, (unsigned char*)data);
    break;

  case _C_SHT:
    return objc_read_short (stream, (short*)data);
    break;

  case _C_USHT:
    return objc_read_unsigned_short (stream, (unsigned short*)data);
    break;

  case _C_INT:
    return objc_read_int (stream, (int*)data);
    break;

  case _C_UINT:
    return objc_read_unsigned_int (stream, (unsigned int*)data);
    break;

  case _C_LNG:
    return objc_read_long (stream, (long*)data);
    break;

  case _C_ULNG:
    return objc_read_unsigned_long (stream, (unsigned long*)data);
    break;

  case _C_CHARPTR:
  case _C_ATOM:
    return objc_read_string (stream, (char**)data);
    break;

  case _C_ARY_B:
    {
      int len = atoi(type+1);
      while (isdigit(*++type))
	;
      return objc_read_array (stream, type, len, data);
    }
    break; 

  case _C_STRUCT_B:
    {
      int acc_size = 0;
      int align;
      while (*type != _C_STRUCT_E && *type++ != '=')
	; /* skip "<name>=" */
      while (*type != _C_STRUCT_E)
	{
	  align = objc_alignof_type (type);       /* padd to alignment */
	  acc_size += ROUND (acc_size, align);
	  objc_read_type (stream, type, ((char*)data)+acc_size);
	  acc_size += objc_sizeof_type (type);   /* add component size */
	  type = objc_skip_typespec (type);	 /* skip component */
	}
      return 1;
    }

  default:
    {
      objc_error(nil, OBJC_ERR_BAD_TYPE,
		 "objc_read_type: cannot parse typespec: %s\n", type);
      return 0;
    }
  }
}

/*
** Write the object specified by the template TYPE to STREAM.  Last
** arguments specify addresses of values to be written.  It might 
** seem surprising to specify values by address, but this is extremely
** convenient for copy-paste with objc_read_types calls.  A more
** down-to-the-earth cause for this passing of addresses is that values
** of arbitrary size is not well supported in ANSI C for functions with
** variable number of arguments.
*/

int 
objc_write_types (TypedStream* stream, const char* type, ...)
{
  va_list args;
  const char *c;
  int res = 0;

  va_start(args, type);

  for (c = type; *c; c = objc_skip_typespec (c))
    {
      switch(*c) {
      case _C_ID:
	res = objc_write_object (stream, *va_arg (args, id*));
	break;

      case _C_CLASS:
	res = objc_write_class (stream, *va_arg(args, Class*));
	break;

      case _C_SEL:
	res = objc_write_selector (stream, *va_arg(args, SEL*));
	break;
	
      case _C_CHR:
	res = objc_write_char (stream, *va_arg (args, char*));
	break;
	
      case _C_UCHR:
	res = objc_write_unsigned_char (stream,
					*va_arg (args, unsigned char*));
	break;
	
      case _C_SHT:
	res = objc_write_short (stream, *va_arg(args, short*));
	break;

      case _C_USHT:
	res = objc_write_unsigned_short (stream,
					 *va_arg(args, unsigned short*));
	break;

      case _C_INT:
	res = objc_write_int(stream, *va_arg(args, int*));
	break;
	
      case _C_UINT:
	res = objc_write_unsigned_int(stream, *va_arg(args, unsigned int*));
	break;

      case _C_LNG:
	res = objc_write_long(stream, *va_arg(args, long*));
	break;
	
      case _C_ULNG:
	res = objc_write_unsigned_long(stream, *va_arg(args, unsigned long*));
	break;

      case _C_CHARPTR:
	{
	  char** str = va_arg(args, char**);
	  res = objc_write_string (stream, *str, strlen(*str));
	}
	break;

      case _C_ATOM:
	{
	  char** str = va_arg(args, char**);
	  res = objc_write_string_atomic (stream, *str, strlen(*str));
	}
	break;

      case _C_ARY_B:
	{
	  int len = atoi(c+1);
	  const char* t = c;
	  while (isdigit(*++t))
	    ;
	  res = objc_write_array (stream, t, len, va_arg(args, void*));
	  t = objc_skip_typespec (t);
	  if (*t != _C_ARY_E)
	    objc_error(nil, OBJC_ERR_BAD_TYPE, "expected `]', got: %s", t);
	}
	break; 
	
      default:
	objc_error(nil, OBJC_ERR_BAD_TYPE, 
		   "objc_write_types: cannot parse typespec: %s\n", type);
      }
    }
  va_end(args);
  return res;
}


/* 
** Last arguments specify addresses of values to be read.  Expected
** type is checked against the type actually present on the stream. 
*/

int 
objc_read_types(TypedStream* stream, const char* type, ...)
{
  va_list args;
  const char *c;
  int res = 0;

  va_start(args, type);

  for (c = type; *c; c = objc_skip_typespec(c))
    {
      switch(*c) {
      case _C_ID:
	res = objc_read_object(stream, va_arg(args, id*));
	break;

      case _C_CLASS:
	res = objc_read_class(stream, va_arg(args, Class*));
	break;

      case _C_SEL:
	res = objc_read_selector(stream, va_arg(args, SEL*));
	break;
	
      case _C_CHR:
	res = objc_read_char(stream, va_arg(args, char*));
	break;
	
      case _C_UCHR:
	res = objc_read_unsigned_char(stream, va_arg(args, unsigned char*));
	break;
	
      case _C_SHT:
	res = objc_read_short(stream, va_arg(args, short*));
	break;

      case _C_USHT:
	res = objc_read_unsigned_short(stream, va_arg(args, unsigned short*));
	break;

      case _C_INT:
	res = objc_read_int(stream, va_arg(args, int*));
	break;
	
      case _C_UINT:
	res = objc_read_unsigned_int(stream, va_arg(args, unsigned int*));
	break;

      case _C_LNG:
	res = objc_read_long(stream, va_arg(args, long*));
	break;
	
      case _C_ULNG:
	res = objc_read_unsigned_long(stream, va_arg(args, unsigned long*));
	break;

      case _C_CHARPTR:
      case _C_ATOM:
	{
	  char** str = va_arg(args, char**);
	  res = objc_read_string (stream, str);
	}
	break;

      case _C_ARY_B:
	{
	  int len = atoi(c+1);
	  const char* t = c;
	  while (isdigit(*++t))
	    ;
	  res = objc_read_array (stream, t, len, va_arg(args, void*));
	  t = objc_skip_typespec (t);
	  if (*t != _C_ARY_E)
	    objc_error(nil, OBJC_ERR_BAD_TYPE, "expected `]', got: %s", t);
	}
	break; 
	
      default:
	objc_error(nil, OBJC_ERR_BAD_TYPE, 
		   "objc_read_types: cannot parse typespec: %s\n", type);
      }
    }
  va_end(args);
  return res;
}

/*
** Write an array of COUNT elements of TYPE from the memory address DATA.
** This is equivalent of objc_write_type (stream, "[N<type>]", data)
*/

int
objc_write_array (TypedStream* stream, const char* type,
		  int count, const void* data)
{
  int off = objc_sizeof_type(type);
  const char* where = data;

  while (count-- > 0)
    {
      objc_write_type(stream, type, where);
      where += off;
    }
  return 1;
}

/*
** Read an array of COUNT elements of TYPE into the memory address
** DATA.  The memory pointed to by data is supposed to be allocated
** by the callee.  This is equivalent of 
**   objc_read_type (stream, "[N<type>]", data)
*/

int
objc_read_array (TypedStream* stream, const char* type,
		 int count, void* data)
{
  int off = objc_sizeof_type(type);
  char* where = (char*)data;

  while (count-- > 0)
    {
      objc_read_type(stream, type, where);
      where += off;
    }
  return 1;
}

static int 
__objc_fread(FILE* file, char* data, int len)
{
  return fread(data, len, 1, file);
}

static int 
__objc_fwrite(FILE* file, char* data, int len)
{
  return fwrite(data, len, 1, file);
}

static int
__objc_feof(FILE* file)
{
  return feof(file);
}

static int 
__objc_no_write(FILE* file, char* data, int len)
{
  objc_error (nil, OBJC_ERR_NO_WRITE, "TypedStream not open for writing");
  return 0;
}

static int 
__objc_no_read(FILE* file, char* data, int len)
{
  objc_error (nil, OBJC_ERR_NO_READ, "TypedStream not open for reading");
  return 0;
}

static int
__objc_read_typed_stream_signature (TypedStream* stream)
{
  char buffer[80];
  int pos = 0;
  do
    (*stream->read)(stream->physical, buffer+pos, 1);
  while (buffer[pos++] != '\0')
    ;
  sscanf (buffer, "GNU TypedStream %d", &stream->version);
  if (stream->version != OBJC_TYPED_STREAM_VERSION)
    objc_error (nil, OBJC_ERR_STREAM_VERSION,
		"cannot handle TypedStream version %d", stream->version);
  return 1;
}

static int
__objc_write_typed_stream_signature (TypedStream* stream)
{
  char buffer[80];
  sprintf(buffer, "GNU TypedStream %d", OBJC_TYPED_STREAM_VERSION);
  stream->version = OBJC_TYPED_STREAM_VERSION;
  (*stream->write)(stream->physical, buffer, strlen(buffer)+1);
  return 1;
}

static void __objc_finish_write_root_object(struct objc_typed_stream* stream)
{
  hash_delete (stream->object_table);
  stream->object_table = hash_new(64,
				  (hash_func_type)hash_ptr,
				  (compare_func_type)compare_ptrs);
}

static void __objc_finish_read_root_object(struct objc_typed_stream* stream)
{
  node_ptr node;
  SEL awake_sel = sel_get_any_uid ("awake");
  cache_ptr free_list = hash_new (64,
				  (hash_func_type) hash_ptr,
				  (compare_func_type) compare_ptrs);

  /* resolve object forward references */
  for (node = hash_next (stream->object_refs, NULL); node;
       node = hash_next (stream->object_refs, node))
    {
      struct objc_list* reflist = node->value;
      const void* key = node->key;
      id object = hash_value_for_key (stream->object_table, key);
      while(reflist)
	{
	  *((id*)reflist->head) = object;
	  if (hash_value_for_key (free_list,reflist) == NULL)
	    hash_add (&free_list,reflist,reflist);

	  reflist = reflist->tail;
	}
    }
    
  /* apply __objc_free to all objects stored in free_list */
  for (node = hash_next (free_list, NULL); node;
       node = hash_next (free_list, node))
    objc_free ((void *) node->key);

  hash_delete (free_list);

  /* empty object reference table */
  hash_delete (stream->object_refs);
  stream->object_refs = hash_new(8, (hash_func_type)hash_ptr,
				 (compare_func_type)compare_ptrs);
  
  /* call -awake for all objects read  */
  if (awake_sel)
    {
      for (node = hash_next (stream->object_table, NULL); node;
	   node = hash_next (stream->object_table, node))
	{
	  id object = node->value;
	  if (__objc_responds_to (object, awake_sel))
	    (*objc_msg_lookup(object, awake_sel))(object, awake_sel);
	}
    }

  /* empty object table */
  hash_delete (stream->object_table);
  stream->object_table = hash_new(64,
				  (hash_func_type)hash_ptr,
				  (compare_func_type)compare_ptrs);
}

/*
** Open the stream PHYSICAL in MODE
*/

TypedStream* 
objc_open_typed_stream (FILE* physical, int mode)
{
  TypedStream* s = (TypedStream*)objc_malloc(sizeof(TypedStream));

  s->mode = mode;
  s->physical = physical;
  s->stream_table = hash_new(64,
			     (hash_func_type)hash_ptr,
			     (compare_func_type)compare_ptrs);
  s->object_table = hash_new(64,
			     (hash_func_type)hash_ptr,
			     (compare_func_type)compare_ptrs);
  s->eof = (objc_typed_eof_func)__objc_feof;
  s->flush = (objc_typed_flush_func)fflush;
  s->writing_root_p = 0;
  if (mode == OBJC_READONLY)
    {
      s->class_table = hash_new(8, (hash_func_type)hash_string,
				(compare_func_type)compare_strings);
      s->object_refs = hash_new(8, (hash_func_type)hash_ptr,
				(compare_func_type)compare_ptrs);
      s->read = (objc_typed_read_func)__objc_fread;
      s->write = (objc_typed_write_func)__objc_no_write;
      __objc_read_typed_stream_signature (s);
    }
  else if (mode == OBJC_WRITEONLY)
    {
      s->class_table = 0;
      s->object_refs = 0;
      s->read = (objc_typed_read_func)__objc_no_read;
      s->write = (objc_typed_write_func)__objc_fwrite;
      __objc_write_typed_stream_signature (s);
    }      
  else
    {
      objc_close_typed_stream (s);
      return NULL;
    }
  s->type = OBJC_FILE_STREAM;
  return s;
}

/*
** Open the file named by FILE_NAME in MODE
*/

TypedStream*
objc_open_typed_stream_for_file (const char* file_name, int mode)
{
  FILE* file = NULL;
  TypedStream* s;

  if (mode == OBJC_READONLY)
    file = fopen (file_name, "r");
  else
    file = fopen (file_name, "w");

  if (file)
    {
      s = objc_open_typed_stream (file, mode);
      if (s)
	s->type |= OBJC_MANAGED_STREAM;
      return s;
    }
  else
    return NULL;
}

/*
** Close STREAM freeing the structure it self.  If it was opened with 
** objc_open_typed_stream_for_file, the file will also be closed.
*/

void
objc_close_typed_stream (TypedStream* stream)
{
  if (stream->mode == OBJC_READONLY)
    {
      __objc_finish_read_root_object (stream); /* Just in case... */
      hash_delete (stream->class_table);
      hash_delete (stream->object_refs);
    }

  hash_delete (stream->stream_table);
  hash_delete (stream->object_table);

  if (stream->type == (OBJC_MANAGED_STREAM | OBJC_FILE_STREAM))
    fclose ((FILE*)stream->physical);

  objc_free(stream);
}

BOOL
objc_end_of_typed_stream (TypedStream* stream)
{
  return (*stream->eof)(stream->physical);
}

void
objc_flush_typed_stream (TypedStream* stream)
{
  (*stream->flush)(stream->physical);
}

long
objc_get_stream_class_version (TypedStream* stream, Class class)
{
  if (stream->class_table)
    return PTR2LONG(hash_value_for_key (stream->class_table, class->name));
  else
    return class_get_version (class);
}