archive.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:39k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. /* GNU Objective C Runtime archiving
  2.    Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
  3.    Contributed by Kresten Krab Thorup
  4. This file is part of GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation; either version 2, or (at your option) any later version.
  8. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  11. details.
  12. You should have received a copy of the GNU General Public License along with
  13. GNU CC; see the file COPYING.  If not, write to the Free Software
  14. Foundation, 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.  */
  16. /* As a special exception, if you link this library with files compiled with
  17.    GCC to produce an executable, this does not cause the resulting executable
  18.    to be covered by the GNU General Public License. This exception does not
  19.    however invalidate any other reasons why the executable file might be
  20.    covered by the GNU General Public License.  */
  21. #include "runtime.h"
  22. #include "typedstream.h"
  23. #include "encoding.h"
  24. #ifdef HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #include <string.h> /* strcpy, strlen */ 
  28. extern int fflush(FILE*);
  29. #define ROUND(V, A) 
  30.   ({ typeof(V) __v=(V); typeof(A) __a=(A);  
  31.      __a*((__v+__a-1)/__a); })
  32. #define PTR2LONG(P) (((char*)(P))-(char*)0)
  33. #define LONG2PTR(L) (((char*)0)+(L))
  34. /* Declare some functions... */
  35. static int
  36. objc_read_class (struct objc_typed_stream* stream, Class* class);
  37. int objc_sizeof_type(const char* type);
  38. static int
  39. objc_write_use_common (struct objc_typed_stream* stream, unsigned long key);
  40. static int
  41. objc_write_register_common (struct objc_typed_stream* stream,
  42.     unsigned long key);
  43. static int 
  44. objc_write_class (struct objc_typed_stream* stream,
  45.  struct objc_class* class);
  46. const char* objc_skip_type (const char* type);
  47. static void __objc_finish_write_root_object(struct objc_typed_stream*);
  48. static void __objc_finish_read_root_object(struct objc_typed_stream*);
  49. static __inline__ int
  50. __objc_code_unsigned_char (unsigned char* buf, unsigned char val)
  51. {
  52.   if ((val&_B_VALUE) == val)
  53.     {
  54.       buf[0] = val|_B_SINT;
  55.       return 1;
  56.     }
  57.   else
  58.     {
  59.       buf[0] = _B_NINT|0x01;
  60.       buf[1] = val;
  61.       return 2;
  62.     }
  63. }
  64. int
  65. objc_write_unsigned_char (struct objc_typed_stream* stream,
  66.   unsigned char value)
  67. {
  68.   unsigned char buf[sizeof (unsigned char)+1];
  69.   int len = __objc_code_unsigned_char (buf, value);
  70.   return (*stream->write)(stream->physical, buf, len);
  71. }
  72. static __inline__ int
  73. __objc_code_char (unsigned char* buf, char val)
  74. {
  75.   if (val >= 0)
  76.     return __objc_code_unsigned_char (buf, val);
  77.   else
  78.     {
  79.       buf[0] = _B_NINT|_B_SIGN|0x01;
  80.       buf[1] = -val;
  81.       return 2;
  82.     }
  83. }
  84. int
  85. objc_write_char (struct objc_typed_stream* stream, char value)
  86. {
  87.   unsigned char buf[sizeof (char)+1];
  88.   int len = __objc_code_char (buf, value);
  89.   return (*stream->write)(stream->physical, buf, len);
  90. }
  91. static __inline__ int
  92. __objc_code_unsigned_short (unsigned char* buf, unsigned short val)
  93. {
  94.   if ((val&_B_VALUE) == val)
  95.     {
  96.       buf[0] = val|_B_SINT;
  97.       return 1;
  98.     }
  99.   else 
  100.     {
  101.       int c, b;
  102.       buf[0] = _B_NINT;
  103.       for (c= sizeof(short); c != 0; c -= 1)
  104. if (((val>>(8*(c-1)))%0x100) != 0)
  105.   break;
  106.       buf[0] |= c;
  107.       for (b = 1; c != 0; c--, b++)
  108. {
  109.   buf[b] = (val >> (8*(c-1)))%0x100;
  110. }
  111.       return b;
  112.     }
  113. }
  114. int
  115. objc_write_unsigned_short (struct objc_typed_stream* stream, 
  116.    unsigned short value)
  117. {
  118.   unsigned char buf[sizeof (unsigned short)+1];
  119.   int len = __objc_code_unsigned_short (buf, value);
  120.   return (*stream->write)(stream->physical, buf, len);
  121. }
  122.       
  123. static __inline__ int
  124. __objc_code_short (unsigned char* buf, short val)
  125. {
  126.   int sign = (val < 0);
  127.   int size = __objc_code_unsigned_short (buf, sign ? -val : val);
  128.   if (sign)
  129.     buf[0] |= _B_SIGN;
  130.   return size;
  131. }
  132. int
  133. objc_write_short (struct objc_typed_stream* stream, short value)
  134. {
  135.   unsigned char buf[sizeof (short)+1];
  136.   int len = __objc_code_short (buf, value);
  137.   return (*stream->write)(stream->physical, buf, len);
  138. }
  139.       
  140. static __inline__ int
  141. __objc_code_unsigned_int (unsigned char* buf, unsigned int val)
  142. {
  143.   if ((val&_B_VALUE) == val)
  144.     {
  145.       buf[0] = val|_B_SINT;
  146.       return 1;
  147.     }
  148.   else 
  149.     {
  150.       int c, b;
  151.       buf[0] = _B_NINT;
  152.       for (c= sizeof(int); c != 0; c -= 1)
  153. if (((val>>(8*(c-1)))%0x100) != 0)
  154.   break;
  155.       buf[0] |= c;
  156.       for (b = 1; c != 0; c--, b++)
  157. {
  158.   buf[b] = (val >> (8*(c-1)))%0x100;
  159. }
  160.       return b;
  161.     }
  162. }
  163. int
  164. objc_write_unsigned_int (struct objc_typed_stream* stream, unsigned int value)
  165. {
  166.   unsigned char buf[sizeof(unsigned int)+1];
  167.   int len = __objc_code_unsigned_int (buf, value);
  168.   return (*stream->write)(stream->physical, buf, len);
  169. }
  170. static __inline__ int
  171. __objc_code_int (unsigned char* buf, int val)
  172. {
  173.   int sign = (val < 0);
  174.   int size = __objc_code_unsigned_int (buf, sign ? -val : val);
  175.   if (sign)
  176.     buf[0] |= _B_SIGN;
  177.   return size;
  178. }
  179. int
  180. objc_write_int (struct objc_typed_stream* stream, int value)
  181. {
  182.   unsigned char buf[sizeof(int)+1];
  183.   int len = __objc_code_int (buf, value);
  184.   return (*stream->write)(stream->physical, buf, len);
  185. }
  186. static __inline__ int
  187. __objc_code_unsigned_long (unsigned char* buf, unsigned long val)
  188. {
  189.   if ((val&_B_VALUE) == val)
  190.     {
  191.       buf[0] = val|_B_SINT;
  192.       return 1;
  193.     }
  194.   else 
  195.     {
  196.       int c, b;
  197.       buf[0] = _B_NINT;
  198.       for (c= sizeof(long); c != 0; c -= 1)
  199. if (((val>>(8*(c-1)))%0x100) != 0)
  200.   break;
  201.       buf[0] |= c;
  202.       for (b = 1; c != 0; c--, b++)
  203. {
  204.   buf[b] = (val >> (8*(c-1)))%0x100;
  205. }
  206.       return b;
  207.     }
  208. }
  209. int
  210. objc_write_unsigned_long (struct objc_typed_stream* stream, 
  211.   unsigned long value)
  212. {
  213.   unsigned char buf[sizeof(unsigned long)+1];
  214.   int len = __objc_code_unsigned_long (buf, value);
  215.   return (*stream->write)(stream->physical, buf, len);
  216. }
  217. static __inline__ int
  218. __objc_code_long (unsigned char* buf, long val)
  219. {
  220.   int sign = (val < 0);
  221.   int size = __objc_code_unsigned_long (buf, sign ? -val : val);
  222.   if (sign)
  223.     buf[0] |= _B_SIGN;
  224.   return size;
  225. }
  226. int
  227. objc_write_long (struct objc_typed_stream* stream, long value)
  228. {
  229.   unsigned char buf[sizeof(long)+1];
  230.   int len = __objc_code_long (buf, value);
  231.   return (*stream->write)(stream->physical, buf, len);
  232. }
  233. int
  234. objc_write_string (struct objc_typed_stream* stream,
  235.    const unsigned char* string, unsigned int nbytes)
  236. {
  237.   unsigned char buf[sizeof(unsigned int)+1];
  238.   int len = __objc_code_unsigned_int (buf, nbytes);
  239.   
  240.   if ((buf[0]&_B_CODE) == _B_SINT)
  241.     buf[0] = (buf[0]&_B_VALUE)|_B_SSTR;
  242.   else /* _B_NINT */
  243.     buf[0] = (buf[0]&_B_VALUE)|_B_NSTR;
  244.   if ((*stream->write)(stream->physical, buf, len) != 0)
  245.     return (*stream->write)(stream->physical, string, nbytes);
  246.   else
  247.     return 0;
  248. }
  249. int
  250. objc_write_string_atomic (struct objc_typed_stream* stream,
  251.   unsigned char* string, unsigned int nbytes)
  252. {
  253.   unsigned long key;
  254.   if ((key = PTR2LONG(hash_value_for_key (stream->stream_table, string))))
  255.     return objc_write_use_common (stream, key);
  256.   else
  257.     {
  258.       int length;
  259.       hash_add (&stream->stream_table, LONG2PTR(key=PTR2LONG(string)), string);
  260.       if ((length = objc_write_register_common (stream, key)))
  261. return objc_write_string (stream, string, nbytes);
  262.       return length;
  263.     }
  264. }
  265. static int
  266. objc_write_register_common (struct objc_typed_stream* stream, 
  267.     unsigned long key)
  268. {
  269.   unsigned char buf[sizeof (unsigned long)+2];
  270.   int len = __objc_code_unsigned_long (buf+1, key);
  271.   if (len == 1)
  272.     {
  273.       buf[0] = _B_RCOMM|0x01;
  274.       buf[1] &= _B_VALUE;
  275.       return (*stream->write)(stream->physical, buf, len+1);
  276.     }
  277.   else
  278.     {
  279.       buf[1] = (buf[1]&_B_VALUE)|_B_RCOMM;
  280.       return (*stream->write)(stream->physical, buf+1, len);
  281.     }
  282. }
  283. static int
  284. objc_write_use_common (struct objc_typed_stream* stream, unsigned long key)
  285. {
  286.   unsigned char buf[sizeof (unsigned long)+2];
  287.   int len = __objc_code_unsigned_long (buf+1, key);
  288.   if (len == 1)
  289.     {
  290.       buf[0] = _B_UCOMM|0x01;
  291.       buf[1] &= _B_VALUE;
  292.       return (*stream->write)(stream->physical, buf, 2);
  293.     }
  294.   else
  295.     {
  296.       buf[1] = (buf[1]&_B_VALUE)|_B_UCOMM;
  297.       return (*stream->write)(stream->physical, buf+1, len);
  298.     }
  299. }
  300. static __inline__ int
  301. __objc_write_extension (struct objc_typed_stream* stream, unsigned char code)
  302. {
  303.   if (code <= _B_VALUE)
  304.     {
  305.       unsigned char buf = code|_B_EXT;
  306.       return (*stream->write)(stream->physical, &buf, 1);
  307.     }
  308.   else 
  309.     {
  310.       objc_error(nil, OBJC_ERR_BAD_OPCODE,
  311.  "__objc_write_extension: bad opcode %cn", code);
  312.       return -1;
  313.     }
  314. }
  315. __inline__ int
  316. __objc_write_object (struct objc_typed_stream* stream, id object)
  317. {
  318.   unsigned char buf = '';
  319.   SEL write_sel = sel_get_any_uid ("write:");
  320.   if (object)
  321.     {
  322.       __objc_write_extension (stream, _BX_OBJECT);
  323.       objc_write_class (stream, object->class_pointer);
  324.       (*objc_msg_lookup(object, write_sel))(object, write_sel, stream);
  325.       return (*stream->write)(stream->physical, &buf, 1);
  326.     }
  327.   else
  328.     return objc_write_use_common(stream, 0);
  329. }
  330. int 
  331. objc_write_object_reference (struct objc_typed_stream* stream, id object)
  332. {
  333.   unsigned long key;
  334.   if ((key = PTR2LONG(hash_value_for_key (stream->object_table, object))))
  335.     return objc_write_use_common (stream, key);
  336.   __objc_write_extension (stream, _BX_OBJREF);
  337.   return objc_write_unsigned_long (stream, PTR2LONG (object));
  338. }
  339. int 
  340. objc_write_root_object (struct objc_typed_stream* stream, id object)
  341. {
  342.   int len = 0;
  343.   if (stream->writing_root_p)
  344.     objc_error (nil, OBJC_ERR_RECURSE_ROOT, 
  345. "objc_write_root_object called recursively");
  346.   else
  347.     {
  348.       stream->writing_root_p = 1;
  349.       __objc_write_extension (stream, _BX_OBJROOT);
  350.       if((len = objc_write_object (stream, object)))
  351. __objc_finish_write_root_object(stream);
  352.       stream->writing_root_p = 0;
  353.     }
  354.   return len;
  355. }
  356. int 
  357. objc_write_object (struct objc_typed_stream* stream, id object)
  358. {
  359.   unsigned long key;
  360.   if ((key = PTR2LONG(hash_value_for_key (stream->object_table, object))))
  361.     return objc_write_use_common (stream, key);
  362.   else if (object == nil)
  363.     return objc_write_use_common(stream, 0);
  364.   else
  365.     {
  366.       int length;
  367.       hash_add (&stream->object_table, LONG2PTR(key=PTR2LONG(object)), object);
  368.       if ((length = objc_write_register_common (stream, key)))
  369. return __objc_write_object (stream, object);
  370.       return length;
  371.     }
  372. }
  373. __inline__ int
  374. __objc_write_class (struct objc_typed_stream* stream, struct objc_class* class)
  375. {
  376.   __objc_write_extension (stream, _BX_CLASS);
  377.   objc_write_string_atomic(stream, (char*)class->name,
  378.    strlen((char*)class->name));
  379.   return objc_write_unsigned_long (stream, class->version);
  380. }
  381. static int 
  382. objc_write_class (struct objc_typed_stream* stream,
  383.  struct objc_class* class)
  384. {
  385.   unsigned long key;
  386.   if ((key = PTR2LONG(hash_value_for_key (stream->stream_table, class))))
  387.     return objc_write_use_common (stream, key);
  388.   else
  389.     {
  390.       int length;
  391.       hash_add (&stream->stream_table, LONG2PTR(key=PTR2LONG(class)), class);
  392.       if ((length = objc_write_register_common (stream, key)))
  393. return __objc_write_class (stream, class);
  394.       return length;
  395.     }
  396. }
  397. __inline__ int 
  398. __objc_write_selector (struct objc_typed_stream* stream, SEL selector)
  399. {
  400.   const char* sel_name;
  401.   __objc_write_extension (stream, _BX_SEL);
  402.   /* to handle NULL selectors */
  403.   if ((SEL)0 == selector)
  404.     return objc_write_string (stream, "", 0);
  405.   sel_name = sel_get_name (selector);
  406.   return objc_write_string (stream, sel_name, strlen ((char*)sel_name));
  407. }
  408. int 
  409. objc_write_selector (struct objc_typed_stream* stream, SEL selector)
  410. {
  411.   const char* sel_name;
  412.   unsigned long key;
  413.   /* to handle NULL selectors */
  414.   if ((SEL)0 == selector)
  415.     return __objc_write_selector (stream, selector);
  416.   sel_name = sel_get_name (selector);
  417.   if ((key = PTR2LONG(hash_value_for_key (stream->stream_table, sel_name))))
  418.     return objc_write_use_common (stream, key);
  419.   else
  420.     {
  421.       int length;
  422.       hash_add (&stream->stream_table, 
  423. LONG2PTR(key=PTR2LONG(sel_name)), (char*)sel_name);
  424.       if ((length = objc_write_register_common (stream, key)))
  425. return __objc_write_selector (stream, selector);
  426.       return length;
  427.     }
  428. }
  429. /*
  430. ** Read operations 
  431. */
  432. __inline__ int
  433. objc_read_char (struct objc_typed_stream* stream, char* val)
  434. {
  435.   unsigned char buf;
  436.   int len;
  437.   len = (*stream->read)(stream->physical, &buf, 1);
  438.   if (len != 0)
  439.     {
  440.       if ((buf & _B_CODE) == _B_SINT)
  441. (*val) = (buf & _B_VALUE);
  442.       else if ((buf & _B_NUMBER) == 1)
  443. {
  444.   len = (*stream->read)(stream->physical, val, 1);
  445.   if (buf&_B_SIGN)
  446.     (*val) = -1*(*val);
  447. }
  448.       else
  449. objc_error(nil, OBJC_ERR_BAD_DATA,
  450.    "expected 8bit signed int, got %dbit int",
  451.    (int)(buf&_B_NUMBER)*8);
  452.     }
  453.   return len;
  454. }
  455. __inline__ int
  456. objc_read_unsigned_char (struct objc_typed_stream* stream, unsigned char* val)
  457. {
  458.   unsigned char buf;
  459.   int len;
  460.   if ((len = (*stream->read)(stream->physical, &buf, 1)))
  461.     {
  462.       if ((buf & _B_CODE) == _B_SINT)
  463. (*val) = (buf & _B_VALUE);
  464.       else if ((buf & _B_NUMBER) == 1)
  465. len = (*stream->read)(stream->physical, val, 1);
  466.       else
  467. objc_error(nil, OBJC_ERR_BAD_DATA,
  468.    "expected 8bit unsigned int, got %dbit int",
  469.    (int)(buf&_B_NUMBER)*8);
  470.     }
  471.   return len;
  472. }
  473. __inline__ int
  474. objc_read_short (struct objc_typed_stream* stream, short* value)
  475. {
  476.   unsigned char buf[sizeof(short)+1];
  477.   int len;
  478.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  479.     {
  480.       if ((buf[0] & _B_CODE) == _B_SINT)
  481. (*value) = (buf[0] & _B_VALUE);
  482.       else
  483. {
  484.   int pos = 1;
  485.   int nbytes = buf[0] & _B_NUMBER;
  486.   if (nbytes > sizeof (short))
  487.     objc_error(nil, OBJC_ERR_BAD_DATA,
  488.        "expected short, got bigger (%dbits)", nbytes*8);
  489.   len = (*stream->read)(stream->physical, buf+1, nbytes);
  490.   (*value) = 0;
  491.   while (pos <= nbytes)
  492.     (*value) = ((*value)*0x100) + buf[pos++];
  493.   if (buf[0] & _B_SIGN)
  494.     (*value) = -(*value);
  495. }
  496.     }
  497.   return len;
  498. }
  499. __inline__ int
  500. objc_read_unsigned_short (struct objc_typed_stream* stream,
  501.   unsigned short* value)
  502. {
  503.   unsigned char buf[sizeof(unsigned short)+1];
  504.   int len;
  505.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  506.     {
  507.       if ((buf[0] & _B_CODE) == _B_SINT)
  508. (*value) = (buf[0] & _B_VALUE);
  509.       else
  510. {
  511.   int pos = 1;
  512.   int nbytes = buf[0] & _B_NUMBER;
  513.   if (nbytes > sizeof (short))
  514.     objc_error(nil, OBJC_ERR_BAD_DATA,
  515.        "expected short, got int or bigger");
  516.   len = (*stream->read)(stream->physical, buf+1, nbytes);
  517.   (*value) = 0;
  518.   while (pos <= nbytes)
  519.     (*value) = ((*value)*0x100) + buf[pos++];
  520. }
  521.     }
  522.   return len;
  523. }
  524. __inline__ int
  525. objc_read_int (struct objc_typed_stream* stream, int* value)
  526. {
  527.   unsigned char buf[sizeof(int)+1];
  528.   int len;
  529.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  530.     {
  531.       if ((buf[0] & _B_CODE) == _B_SINT)
  532. (*value) = (buf[0] & _B_VALUE);
  533.       else
  534. {
  535.   int pos = 1;
  536.   int nbytes = buf[0] & _B_NUMBER;
  537.   if (nbytes > sizeof (int))
  538.     objc_error(nil, OBJC_ERR_BAD_DATA, "expected int, got bigger");
  539.   len = (*stream->read)(stream->physical, buf+1, nbytes);
  540.   (*value) = 0;
  541.   while (pos <= nbytes)
  542.     (*value) = ((*value)*0x100) + buf[pos++];
  543.   if (buf[0] & _B_SIGN)
  544.     (*value) = -(*value);
  545. }
  546.     }
  547.   return len;
  548. }
  549. __inline__ int
  550. objc_read_long (struct objc_typed_stream* stream, long* value)
  551. {
  552.   unsigned char buf[sizeof(long)+1];
  553.   int len;
  554.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  555.     {
  556.       if ((buf[0] & _B_CODE) == _B_SINT)
  557. (*value) = (buf[0] & _B_VALUE);
  558.       else
  559. {
  560.   int pos = 1;
  561.   int nbytes = buf[0] & _B_NUMBER;
  562.   if (nbytes > sizeof (long))
  563.     objc_error(nil, OBJC_ERR_BAD_DATA, "expected long, got bigger");
  564.   len = (*stream->read)(stream->physical, buf+1, nbytes);
  565.   (*value) = 0;
  566.   while (pos <= nbytes)
  567.     (*value) = ((*value)*0x100) + buf[pos++];
  568.   if (buf[0] & _B_SIGN)
  569.     (*value) = -(*value);
  570. }
  571.     }
  572.   return len;
  573. }
  574. __inline__ int
  575. __objc_read_nbyte_uint (struct objc_typed_stream* stream,
  576.        unsigned int nbytes, unsigned int* val)
  577. {
  578.   int len, pos = 0;
  579.   unsigned char buf[sizeof(unsigned int)+1];
  580.   if (nbytes > sizeof (int))
  581.     objc_error(nil, OBJC_ERR_BAD_DATA, "expected int, got bigger");
  582.   len = (*stream->read)(stream->physical, buf, nbytes);
  583.   (*val) = 0;
  584.   while (pos < nbytes)
  585.     (*val) = ((*val)*0x100) + buf[pos++];
  586.   return len;
  587. }
  588.   
  589. __inline__ int
  590. objc_read_unsigned_int (struct objc_typed_stream* stream,
  591. unsigned int* value)
  592. {
  593.   unsigned char buf[sizeof(unsigned int)+1];
  594.   int len;
  595.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  596.     {
  597.       if ((buf[0] & _B_CODE) == _B_SINT)
  598. (*value) = (buf[0] & _B_VALUE);
  599.       else
  600. len = __objc_read_nbyte_uint (stream, (buf[0] & _B_VALUE), value);
  601.     }
  602.   return len;
  603. }
  604. int
  605. __objc_read_nbyte_ulong (struct objc_typed_stream* stream,
  606.        unsigned int nbytes, unsigned long* val)
  607. {
  608.   int len, pos = 0;
  609.   unsigned char buf[sizeof(unsigned long)+1];
  610.   if (nbytes > sizeof (long))
  611.     objc_error(nil, OBJC_ERR_BAD_DATA, "expected long, got bigger");
  612.   len = (*stream->read)(stream->physical, buf, nbytes);
  613.   (*val) = 0;
  614.   while (pos < nbytes)
  615.     (*val) = ((*val)*0x100) + buf[pos++];
  616.   return len;
  617. }
  618.   
  619. __inline__ int
  620. objc_read_unsigned_long (struct objc_typed_stream* stream,
  621. unsigned long* value)
  622. {
  623.   unsigned char buf[sizeof(unsigned long)+1];
  624.   int len;
  625.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  626.     {
  627.       if ((buf[0] & _B_CODE) == _B_SINT)
  628. (*value) = (buf[0] & _B_VALUE);
  629.       else
  630. len = __objc_read_nbyte_ulong (stream, (buf[0] & _B_VALUE), value);
  631.     }
  632.   return len;
  633. }
  634. __inline__ int
  635. objc_read_string (struct objc_typed_stream* stream,
  636.   char** string)
  637. {
  638.   unsigned char buf[sizeof(unsigned int)+1];
  639.   int len;
  640.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  641.     {
  642.       unsigned long key = 0;
  643.       if ((buf[0]&_B_CODE) == _B_RCOMM) /* register following */
  644. {
  645.   len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
  646.   len = (*stream->read)(stream->physical, buf, 1);
  647. }
  648.       switch (buf[0]&_B_CODE) {
  649.       case _B_SSTR:
  650. {
  651.   int length = buf[0]&_B_VALUE;
  652.   (*string) = (char*)objc_malloc(length+1);
  653.   if (key)
  654.     hash_add (&stream->stream_table, LONG2PTR(key), *string);
  655.   len = (*stream->read)(stream->physical, *string, length);
  656.   (*string)[length] = '';
  657. }
  658. break;
  659.       case _B_UCOMM:
  660. {
  661.   char *tmp;
  662.   len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
  663.   tmp = hash_value_for_key (stream->stream_table, LONG2PTR (key));
  664.   *string = objc_malloc (strlen(tmp) + 1);
  665.   strcpy (*string, tmp);
  666. }
  667. break;
  668.       case _B_NSTR:
  669. {
  670.   unsigned int nbytes = buf[0]&_B_VALUE;
  671.   len = __objc_read_nbyte_uint(stream, nbytes, &nbytes);
  672.   if (len) {
  673.     (*string) = (char*)objc_malloc(nbytes+1);
  674.     if (key)
  675.       hash_add (&stream->stream_table, LONG2PTR(key), *string);
  676.     len = (*stream->read)(stream->physical, *string, nbytes);
  677.     (*string)[nbytes] = '';
  678.   }
  679. }
  680. break;
  681.       default:
  682. objc_error(nil, OBJC_ERR_BAD_DATA,
  683.    "expected string, got opcode %cn", (buf[0]&_B_CODE));
  684.       }
  685.     }
  686.   return len;
  687. }
  688. int
  689. objc_read_object (struct objc_typed_stream* stream, id* object)
  690. {
  691.   unsigned char buf[sizeof (unsigned int)];
  692.   int len;
  693.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  694.     {
  695.       SEL read_sel = sel_get_any_uid ("read:");
  696.       unsigned long key = 0;
  697.       if ((buf[0]&_B_CODE) == _B_RCOMM) /* register common */
  698. {
  699.   len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
  700.   len = (*stream->read)(stream->physical, buf, 1);
  701. }
  702.       if (buf[0] == (_B_EXT | _BX_OBJECT))
  703. {
  704.   Class class;
  705.   /* get class */
  706.   len = objc_read_class (stream, &class);
  707.   /* create instance */
  708.   (*object) = class_create_instance(class);
  709.   /* register? */
  710.   if (key)
  711.     hash_add (&stream->object_table, LONG2PTR(key), *object);
  712.   /* send -read: */
  713.   if (__objc_responds_to (*object, read_sel))
  714.     (*get_imp(class, read_sel))(*object, read_sel, stream);
  715.   /* check null-byte */
  716.   len = (*stream->read)(stream->physical, buf, 1);
  717.   if (buf[0] != '')
  718.     objc_error(nil, OBJC_ERR_BAD_DATA,
  719.        "expected null-byte, got opcode %c", buf[0]);
  720. }
  721.       else if ((buf[0]&_B_CODE) == _B_UCOMM)
  722. {
  723.   if (key)
  724.     objc_error(nil, OBJC_ERR_BAD_KEY, "cannot register use upcode...");
  725.   len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
  726.   (*object) = hash_value_for_key (stream->object_table, LONG2PTR(key));
  727. }
  728.       else if (buf[0] == (_B_EXT | _BX_OBJREF)) /* a forward reference */
  729. {
  730.   struct objc_list* other;
  731.   len = objc_read_unsigned_long (stream, &key);
  732.   other = (struct objc_list*)hash_value_for_key (stream->object_refs, 
  733.  LONG2PTR(key));
  734.   hash_add (&stream->object_refs, LONG2PTR(key), 
  735.     (void*)list_cons(object, other));
  736. }
  737.       else if (buf[0] == (_B_EXT | _BX_OBJROOT)) /* a root object */
  738. {
  739.   if (key)
  740.     objc_error(nil, OBJC_ERR_BAD_KEY,
  741.        "cannot register root object...");
  742.   len = objc_read_object (stream, object);
  743.   __objc_finish_read_root_object (stream);
  744. }
  745.       else
  746. objc_error(nil, OBJC_ERR_BAD_DATA,
  747.    "expected object, got opcode %c", buf[0]);
  748.     }
  749.   return len;
  750. }
  751. static int
  752. objc_read_class (struct objc_typed_stream* stream, Class* class)
  753. {
  754.   unsigned char buf[sizeof (unsigned int)];
  755.   int len;
  756.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  757.     {
  758.       unsigned long key = 0;
  759.       if ((buf[0]&_B_CODE) == _B_RCOMM) /* register following */
  760. {
  761.   len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
  762.   len = (*stream->read)(stream->physical, buf, 1);
  763. }
  764.       if (buf[0] == (_B_EXT | _BX_CLASS))
  765. {
  766.   char* class_name;
  767.   unsigned long version;
  768.   /* get class */
  769.   len = objc_read_string (stream, &class_name);
  770.   (*class) = objc_get_class(class_name);
  771.   objc_free(class_name);
  772.   /* register */
  773.   if (key)
  774.     hash_add (&stream->stream_table, LONG2PTR(key), *class);
  775.   objc_read_unsigned_long(stream, &version);
  776.   hash_add (&stream->class_table, (*class)->name, (void*)version);
  777. }
  778.       else if ((buf[0]&_B_CODE) == _B_UCOMM)
  779. {
  780.   if (key)
  781.     objc_error(nil, OBJC_ERR_BAD_KEY, "cannot register use upcode...");
  782.   len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
  783.   (*class) = hash_value_for_key (stream->stream_table, LONG2PTR(key));
  784.   if (!*class)
  785.     objc_error(nil, OBJC_ERR_BAD_CLASS,
  786.        "cannot find class for key %lu", key);
  787. }
  788.       else
  789. objc_error(nil, OBJC_ERR_BAD_DATA,
  790.    "expected class, got opcode %c", buf[0]);
  791.     }
  792.   return len;
  793. }
  794. int
  795. objc_read_selector (struct objc_typed_stream* stream, SEL* selector)
  796. {
  797.   unsigned char buf[sizeof (unsigned int)];
  798.   int len;
  799.   if ((len = (*stream->read)(stream->physical, buf, 1)))
  800.     {
  801.       unsigned long key = 0;
  802.       if ((buf[0]&_B_CODE) == _B_RCOMM) /* register following */
  803. {
  804.   len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
  805.   len = (*stream->read)(stream->physical, buf, 1);
  806. }
  807.       if (buf[0] == (_B_EXT|_BX_SEL)) /* selector! */
  808. {
  809.   char* selector_name;
  810.   /* get selector */
  811.   len = objc_read_string (stream, &selector_name);
  812.   /* To handle NULL selectors */
  813.   if (0 == strlen(selector_name))
  814.     {
  815.       (*selector) = (SEL)0;
  816.       return 0;
  817.     }
  818.   else 
  819.     (*selector) = sel_get_any_uid(selector_name);
  820.   objc_free(selector_name);
  821.   /* register */
  822.   if (key)
  823.     hash_add (&stream->stream_table, LONG2PTR(key), (void*)*selector);
  824. }
  825.       else if ((buf[0]&_B_CODE) == _B_UCOMM)
  826. {
  827.   if (key)
  828.     objc_error(nil, OBJC_ERR_BAD_KEY, "cannot register use upcode...");
  829.   len = __objc_read_nbyte_ulong(stream, (buf[0] & _B_VALUE), &key);
  830.   (*selector) = hash_value_for_key (stream->stream_table, 
  831.     LONG2PTR(key));
  832. }
  833.       else
  834. objc_error(nil, OBJC_ERR_BAD_DATA,
  835.    "expected selector, got opcode %c", buf[0]);
  836.     }
  837.   return len;
  838. }
  839. /*
  840. ** USER LEVEL FUNCTIONS
  841. */
  842. /*
  843. ** Write one object, encoded in TYPE and pointed to by DATA to the
  844. ** typed stream STREAM.  
  845. */
  846. int
  847. objc_write_type(TypedStream* stream, const char* type, const void* data)
  848. {
  849.   switch(*type) {
  850.   case _C_ID:
  851.     return objc_write_object (stream, *(id*)data);
  852.     break;
  853.   case _C_CLASS:
  854.     return objc_write_class (stream, *(Class*)data);
  855.     break;
  856.   case _C_SEL:
  857.     return objc_write_selector (stream, *(SEL*)data);
  858.     break;
  859.   case _C_CHR:
  860.     return objc_write_char(stream, *(char*)data);
  861.     break;
  862.     
  863.   case _C_UCHR:
  864.     return objc_write_unsigned_char(stream, *(unsigned char*)data);
  865.     break;
  866.   case _C_SHT:
  867.     return objc_write_short(stream, *(short*)data);
  868.     break;
  869.   case _C_USHT:
  870.     return objc_write_unsigned_short(stream, *(unsigned short*)data);
  871.     break;
  872.   case _C_INT:
  873.     return objc_write_int(stream, *(int*)data);
  874.     break;
  875.   case _C_UINT:
  876.     return objc_write_unsigned_int(stream, *(unsigned int*)data);
  877.     break;
  878.   case _C_LNG:
  879.     return objc_write_long(stream, *(long*)data);
  880.     break;
  881.   case _C_ULNG:
  882.     return objc_write_unsigned_long(stream, *(unsigned long*)data);
  883.     break;
  884.   case _C_CHARPTR:
  885.     return objc_write_string (stream, *(char**)data, strlen(*(char**)data));
  886.     break;
  887.   case _C_ATOM:
  888.     return objc_write_string_atomic (stream, *(char**)data, 
  889.      strlen(*(char**)data));
  890.     break;
  891.   case _C_ARY_B:
  892.     {
  893.       int len = atoi (type+1);
  894.       while (isDigit (*++type))
  895. ;
  896.       return objc_write_array (stream, type, len, data);
  897.     }
  898.     break; 
  899.   case _C_STRUCT_B:
  900.     {
  901.       int acc_size = 0;
  902.       int align;
  903.       while (*type != _C_STRUCT_E && *type++ != '=')
  904. ; /* skip "<name>=" */
  905.       while (*type != _C_STRUCT_E)
  906. {
  907.   align = objc_alignof_type (type);       /* padd to alignment */
  908.   acc_size += ROUND (acc_size, align);
  909.   objc_write_type (stream, type, ((char*)data)+acc_size);
  910.   acc_size += objc_sizeof_type (type);   /* add component size */
  911.   type = objc_skip_typespec (type);  /* skip component */
  912. }
  913.       return 1;
  914.     }
  915.   default:
  916.     {
  917.       objc_error(nil, OBJC_ERR_BAD_TYPE,
  918.  "objc_write_type: cannot parse typespec: %sn", type);
  919.       return 0;
  920.     }
  921.   }
  922. }
  923. /*
  924. ** Read one object, encoded in TYPE and pointed to by DATA to the
  925. ** typed stream STREAM.  DATA specifies the address of the types to
  926. ** read.  Expected type is checked against the type actually present
  927. ** on the stream. 
  928. */
  929. int
  930. objc_read_type(TypedStream* stream, const char* type, void* data)
  931. {
  932.   char c;
  933.   switch(c = *type) {
  934.   case _C_ID:
  935.     return objc_read_object (stream, (id*)data);
  936.     break;
  937.   case _C_CLASS:
  938.     return objc_read_class (stream, (Class*)data);
  939.     break;
  940.   case _C_SEL:
  941.     return objc_read_selector (stream, (SEL*)data);
  942.     break;
  943.   case _C_CHR:
  944.     return objc_read_char (stream, (char*)data);
  945.     break;
  946.     
  947.   case _C_UCHR:
  948.     return objc_read_unsigned_char (stream, (unsigned char*)data);
  949.     break;
  950.   case _C_SHT:
  951.     return objc_read_short (stream, (short*)data);
  952.     break;
  953.   case _C_USHT:
  954.     return objc_read_unsigned_short (stream, (unsigned short*)data);
  955.     break;
  956.   case _C_INT:
  957.     return objc_read_int (stream, (int*)data);
  958.     break;
  959.   case _C_UINT:
  960.     return objc_read_unsigned_int (stream, (unsigned int*)data);
  961.     break;
  962.   case _C_LNG:
  963.     return objc_read_long (stream, (long*)data);
  964.     break;
  965.   case _C_ULNG:
  966.     return objc_read_unsigned_long (stream, (unsigned long*)data);
  967.     break;
  968.   case _C_CHARPTR:
  969.   case _C_ATOM:
  970.     return objc_read_string (stream, (char**)data);
  971.     break;
  972.   case _C_ARY_B:
  973.     {
  974.       int len = atoi(type+1);
  975.       while (isDigit (*++type))
  976. ;
  977.       return objc_read_array (stream, type, len, data);
  978.     }
  979.     break; 
  980.   case _C_STRUCT_B:
  981.     {
  982.       int acc_size = 0;
  983.       int align;
  984.       while (*type != _C_STRUCT_E && *type++ != '=')
  985. ; /* skip "<name>=" */
  986.       while (*type != _C_STRUCT_E)
  987. {
  988.   align = objc_alignof_type (type);       /* padd to alignment */
  989.   acc_size += ROUND (acc_size, align);
  990.   objc_read_type (stream, type, ((char*)data)+acc_size);
  991.   acc_size += objc_sizeof_type (type);   /* add component size */
  992.   type = objc_skip_typespec (type);  /* skip component */
  993. }
  994.       return 1;
  995.     }
  996.   default:
  997.     {
  998.       objc_error(nil, OBJC_ERR_BAD_TYPE,
  999.  "objc_read_type: cannot parse typespec: %sn", type);
  1000.       return 0;
  1001.     }
  1002.   }
  1003. }
  1004. /*
  1005. ** Write the object specified by the template TYPE to STREAM.  Last
  1006. ** arguments specify addresses of values to be written.  It might 
  1007. ** seem surprising to specify values by address, but this is extremely
  1008. ** convenient for copy-paste with objc_read_types calls.  A more
  1009. ** down-to-the-earth cause for this passing of addresses is that values
  1010. ** of arbitrary size is not well supported in ANSI C for functions with
  1011. ** variable number of arguments.
  1012. */
  1013. int 
  1014. objc_write_types (TypedStream* stream, const char* type, ...)
  1015. {
  1016.   va_list args;
  1017.   const char *c;
  1018.   int res = 0;
  1019.   va_start(args, type);
  1020.   for (c = type; *c; c = objc_skip_typespec (c))
  1021.     {
  1022.       switch(*c) {
  1023.       case _C_ID:
  1024. res = objc_write_object (stream, *va_arg (args, id*));
  1025. break;
  1026.       case _C_CLASS:
  1027. res = objc_write_class (stream, *va_arg(args, Class*));
  1028. break;
  1029.       case _C_SEL:
  1030. res = objc_write_selector (stream, *va_arg(args, SEL*));
  1031. break;
  1032.       case _C_CHR:
  1033. res = objc_write_char (stream, *va_arg (args, char*));
  1034. break;
  1035.       case _C_UCHR:
  1036. res = objc_write_unsigned_char (stream,
  1037. *va_arg (args, unsigned char*));
  1038. break;
  1039.       case _C_SHT:
  1040. res = objc_write_short (stream, *va_arg(args, short*));
  1041. break;
  1042.       case _C_USHT:
  1043. res = objc_write_unsigned_short (stream,
  1044.  *va_arg(args, unsigned short*));
  1045. break;
  1046.       case _C_INT:
  1047. res = objc_write_int(stream, *va_arg(args, int*));
  1048. break;
  1049.       case _C_UINT:
  1050. res = objc_write_unsigned_int(stream, *va_arg(args, unsigned int*));
  1051. break;
  1052.       case _C_LNG:
  1053. res = objc_write_long(stream, *va_arg(args, long*));
  1054. break;
  1055.       case _C_ULNG:
  1056. res = objc_write_unsigned_long(stream, *va_arg(args, unsigned long*));
  1057. break;
  1058.       case _C_CHARPTR:
  1059. {
  1060.   char** str = va_arg(args, char**);
  1061.   res = objc_write_string (stream, *str, strlen(*str));
  1062. }
  1063. break;
  1064.       case _C_ATOM:
  1065. {
  1066.   char** str = va_arg(args, char**);
  1067.   res = objc_write_string_atomic (stream, *str, strlen(*str));
  1068. }
  1069. break;
  1070.       case _C_ARY_B:
  1071. {
  1072.   int len = atoi(c+1);
  1073.   const char* t = c;
  1074.   while (isDigit (*++t))
  1075.     ;
  1076.   res = objc_write_array (stream, t, len, va_arg(args, void*));
  1077.   t = objc_skip_typespec (t);
  1078.   if (*t != _C_ARY_E)
  1079.     objc_error(nil, OBJC_ERR_BAD_TYPE, "expected `]', got: %s", t);
  1080. }
  1081. break; 
  1082.       default:
  1083. objc_error(nil, OBJC_ERR_BAD_TYPE, 
  1084.    "objc_write_types: cannot parse typespec: %sn", type);
  1085.       }
  1086.     }
  1087.   va_end(args);
  1088.   return res;
  1089. }
  1090. /* 
  1091. ** Last arguments specify addresses of values to be read.  Expected
  1092. ** type is checked against the type actually present on the stream. 
  1093. */
  1094. int 
  1095. objc_read_types(TypedStream* stream, const char* type, ...)
  1096. {
  1097.   va_list args;
  1098.   const char *c;
  1099.   int res = 0;
  1100.   va_start(args, type);
  1101.   for (c = type; *c; c = objc_skip_typespec(c))
  1102.     {
  1103.       switch(*c) {
  1104.       case _C_ID:
  1105. res = objc_read_object(stream, va_arg(args, id*));
  1106. break;
  1107.       case _C_CLASS:
  1108. res = objc_read_class(stream, va_arg(args, Class*));
  1109. break;
  1110.       case _C_SEL:
  1111. res = objc_read_selector(stream, va_arg(args, SEL*));
  1112. break;
  1113.       case _C_CHR:
  1114. res = objc_read_char(stream, va_arg(args, char*));
  1115. break;
  1116.       case _C_UCHR:
  1117. res = objc_read_unsigned_char(stream, va_arg(args, unsigned char*));
  1118. break;
  1119.       case _C_SHT:
  1120. res = objc_read_short(stream, va_arg(args, short*));
  1121. break;
  1122.       case _C_USHT:
  1123. res = objc_read_unsigned_short(stream, va_arg(args, unsigned short*));
  1124. break;
  1125.       case _C_INT:
  1126. res = objc_read_int(stream, va_arg(args, int*));
  1127. break;
  1128.       case _C_UINT:
  1129. res = objc_read_unsigned_int(stream, va_arg(args, unsigned int*));
  1130. break;
  1131.       case _C_LNG:
  1132. res = objc_read_long(stream, va_arg(args, long*));
  1133. break;
  1134.       case _C_ULNG:
  1135. res = objc_read_unsigned_long(stream, va_arg(args, unsigned long*));
  1136. break;
  1137.       case _C_CHARPTR:
  1138.       case _C_ATOM:
  1139. {
  1140.   char** str = va_arg(args, char**);
  1141.   res = objc_read_string (stream, str);
  1142. }
  1143. break;
  1144.       case _C_ARY_B:
  1145. {
  1146.   int len = atoi(c+1);
  1147.   const char* t = c;
  1148.   while (isDigit (*++t))
  1149.     ;
  1150.   res = objc_read_array (stream, t, len, va_arg(args, void*));
  1151.   t = objc_skip_typespec (t);
  1152.   if (*t != _C_ARY_E)
  1153.     objc_error(nil, OBJC_ERR_BAD_TYPE, "expected `]', got: %s", t);
  1154. }
  1155. break; 
  1156.       default:
  1157. objc_error(nil, OBJC_ERR_BAD_TYPE, 
  1158.    "objc_read_types: cannot parse typespec: %sn", type);
  1159.       }
  1160.     }
  1161.   va_end(args);
  1162.   return res;
  1163. }
  1164. /*
  1165. ** Write an array of COUNT elements of TYPE from the memory address DATA.
  1166. ** This is equivalent of objc_write_type (stream, "[N<type>]", data)
  1167. */
  1168. int
  1169. objc_write_array (TypedStream* stream, const char* type,
  1170.   int count, const void* data)
  1171. {
  1172.   int off = objc_sizeof_type(type);
  1173.   const char* where = data;
  1174.   while (count-- > 0)
  1175.     {
  1176.       objc_write_type(stream, type, where);
  1177.       where += off;
  1178.     }
  1179.   return 1;
  1180. }
  1181. /*
  1182. ** Read an array of COUNT elements of TYPE into the memory address
  1183. ** DATA.  The memory pointed to by data is supposed to be allocated
  1184. ** by the callee.  This is equivalent of 
  1185. **   objc_read_type (stream, "[N<type>]", data)
  1186. */
  1187. int
  1188. objc_read_array (TypedStream* stream, const char* type,
  1189.  int count, void* data)
  1190. {
  1191.   int off = objc_sizeof_type(type);
  1192.   char* where = (char*)data;
  1193.   while (count-- > 0)
  1194.     {
  1195.       objc_read_type(stream, type, where);
  1196.       where += off;
  1197.     }
  1198.   return 1;
  1199. }
  1200. static int 
  1201. __objc_fread(FILE* file, char* data, int len)
  1202. {
  1203.   return fread(data, len, 1, file);
  1204. }
  1205. static int 
  1206. __objc_fwrite(FILE* file, char* data, int len)
  1207. {
  1208.   return fwrite(data, len, 1, file);
  1209. }
  1210. static int
  1211. __objc_feof(FILE* file)
  1212. {
  1213.   return feof(file);
  1214. }
  1215. static int 
  1216. __objc_no_write(FILE* file, char* data, int len)
  1217. {
  1218.   objc_error (nil, OBJC_ERR_NO_WRITE, "TypedStream not open for writing");
  1219.   return 0;
  1220. }
  1221. static int 
  1222. __objc_no_read(FILE* file, char* data, int len)
  1223. {
  1224.   objc_error (nil, OBJC_ERR_NO_READ, "TypedStream not open for reading");
  1225.   return 0;
  1226. }
  1227. static int
  1228. __objc_read_typed_stream_signature (TypedStream* stream)
  1229. {
  1230.   char buffer[80];
  1231.   int pos = 0;
  1232.   do
  1233.     (*stream->read)(stream->physical, buffer+pos, 1);
  1234.   while (buffer[pos++] != '')
  1235.     ;
  1236.   sscanf (buffer, "GNU TypedStream %d", &stream->version);
  1237.   if (stream->version != OBJC_TYPED_STREAM_VERSION)
  1238.     objc_error (nil, OBJC_ERR_STREAM_VERSION,
  1239. "cannot handle TypedStream version %d", stream->version);
  1240.   return 1;
  1241. }
  1242. static int
  1243. __objc_write_typed_stream_signature (TypedStream* stream)
  1244. {
  1245.   char buffer[80];
  1246.   sprintf(buffer, "GNU TypedStream %d", OBJC_TYPED_STREAM_VERSION);
  1247.   stream->version = OBJC_TYPED_STREAM_VERSION;
  1248.   (*stream->write)(stream->physical, buffer, strlen(buffer)+1);
  1249.   return 1;
  1250. }
  1251. static void __objc_finish_write_root_object(struct objc_typed_stream* stream)
  1252. {
  1253.   hash_delete (stream->object_table);
  1254.   stream->object_table = hash_new(64,
  1255.   (hash_func_type)hash_ptr,
  1256.   (compare_func_type)compare_ptrs);
  1257. }
  1258. static void __objc_finish_read_root_object(struct objc_typed_stream* stream)
  1259. {
  1260.   node_ptr node;
  1261.   SEL awake_sel = sel_get_any_uid ("awake");
  1262.   cache_ptr free_list = hash_new (64,
  1263.   (hash_func_type) hash_ptr,
  1264.   (compare_func_type) compare_ptrs);
  1265.   /* resolve object forward references */
  1266.   for (node = hash_next (stream->object_refs, NULL); node;
  1267.        node = hash_next (stream->object_refs, node))
  1268.     {
  1269.       struct objc_list* reflist = node->value;
  1270.       const void* key = node->key;
  1271.       id object = hash_value_for_key (stream->object_table, key);
  1272.       while(reflist)
  1273. {
  1274.   *((id*)reflist->head) = object;
  1275.   if (hash_value_for_key (free_list,reflist) == NULL)
  1276.     hash_add (&free_list,reflist,reflist);
  1277.   reflist = reflist->tail;
  1278. }
  1279.     }
  1280.     
  1281.   /* apply __objc_free to all objects stored in free_list */
  1282.   for (node = hash_next (free_list, NULL); node;
  1283.        node = hash_next (free_list, node))
  1284.     objc_free ((void *) node->key);
  1285.   hash_delete (free_list);
  1286.   /* empty object reference table */
  1287.   hash_delete (stream->object_refs);
  1288.   stream->object_refs = hash_new(8, (hash_func_type)hash_ptr,
  1289.  (compare_func_type)compare_ptrs);
  1290.   
  1291.   /* call -awake for all objects read  */
  1292.   if (awake_sel)
  1293.     {
  1294.       for (node = hash_next (stream->object_table, NULL); node;
  1295.    node = hash_next (stream->object_table, node))
  1296. {
  1297.   id object = node->value;
  1298.   if (__objc_responds_to (object, awake_sel))
  1299.     (*objc_msg_lookup(object, awake_sel))(object, awake_sel);
  1300. }
  1301.     }
  1302.   /* empty object table */
  1303.   hash_delete (stream->object_table);
  1304.   stream->object_table = hash_new(64,
  1305.   (hash_func_type)hash_ptr,
  1306.   (compare_func_type)compare_ptrs);
  1307. }
  1308. /*
  1309. ** Open the stream PHYSICAL in MODE
  1310. */
  1311. TypedStream* 
  1312. objc_open_typed_stream (FILE* physical, int mode)
  1313. {
  1314.   TypedStream* s = (TypedStream*)objc_malloc(sizeof(TypedStream));
  1315.   s->mode = mode;
  1316.   s->physical = physical;
  1317.   s->stream_table = hash_new(64,
  1318.      (hash_func_type)hash_ptr,
  1319.      (compare_func_type)compare_ptrs);
  1320.   s->object_table = hash_new(64,
  1321.      (hash_func_type)hash_ptr,
  1322.      (compare_func_type)compare_ptrs);
  1323.   s->eof = (objc_typed_eof_func)__objc_feof;
  1324.   s->flush = (objc_typed_flush_func)fflush;
  1325.   s->writing_root_p = 0;
  1326.   if (mode == OBJC_READONLY)
  1327.     {
  1328.       s->class_table = hash_new(8, (hash_func_type)hash_string,
  1329. (compare_func_type)compare_strings);
  1330.       s->object_refs = hash_new(8, (hash_func_type)hash_ptr,
  1331. (compare_func_type)compare_ptrs);
  1332.       s->read = (objc_typed_read_func)__objc_fread;
  1333.       s->write = (objc_typed_write_func)__objc_no_write;
  1334.       __objc_read_typed_stream_signature (s);
  1335.     }
  1336.   else if (mode == OBJC_WRITEONLY)
  1337.     {
  1338.       s->class_table = 0;
  1339.       s->object_refs = 0;
  1340.       s->read = (objc_typed_read_func)__objc_no_read;
  1341.       s->write = (objc_typed_write_func)__objc_fwrite;
  1342.       __objc_write_typed_stream_signature (s);
  1343.     }      
  1344.   else
  1345.     {
  1346.       objc_close_typed_stream (s);
  1347.       return NULL;
  1348.     }
  1349.   s->type = OBJC_FILE_STREAM;
  1350.   return s;
  1351. }
  1352. /*
  1353. ** Open the file named by FILE_NAME in MODE
  1354. */
  1355. TypedStream*
  1356. objc_open_typed_stream_for_file (const char* file_name, int mode)
  1357. {
  1358.   FILE* file = NULL;
  1359.   TypedStream* s;
  1360.   if (mode == OBJC_READONLY)
  1361.     file = fopen (file_name, "r");
  1362.   else
  1363.     file = fopen (file_name, "w");
  1364.   if (file)
  1365.     {
  1366.       s = objc_open_typed_stream (file, mode);
  1367.       if (s)
  1368. s->type |= OBJC_MANAGED_STREAM;
  1369.       return s;
  1370.     }
  1371.   else
  1372.     return NULL;
  1373. }
  1374. /*
  1375. ** Close STREAM freeing the structure it self.  If it was opened with 
  1376. ** objc_open_typed_stream_for_file, the file will also be closed.
  1377. */
  1378. void
  1379. objc_close_typed_stream (TypedStream* stream)
  1380. {
  1381.   if (stream->mode == OBJC_READONLY)
  1382.     {
  1383.       __objc_finish_read_root_object (stream); /* Just in case... */
  1384.       hash_delete (stream->class_table);
  1385.       hash_delete (stream->object_refs);
  1386.     }
  1387.   hash_delete (stream->stream_table);
  1388.   hash_delete (stream->object_table);
  1389.   if (stream->type == (OBJC_MANAGED_STREAM | OBJC_FILE_STREAM))
  1390.     fclose ((FILE*)stream->physical);
  1391.   objc_free(stream);
  1392. }
  1393. BOOL
  1394. objc_end_of_typed_stream (TypedStream* stream)
  1395. {
  1396.   return (*stream->eof)(stream->physical);
  1397. }
  1398. void
  1399. objc_flush_typed_stream (TypedStream* stream)
  1400. {
  1401.   (*stream->flush)(stream->physical);
  1402. }
  1403. long
  1404. objc_get_stream_class_version (TypedStream* stream, Class class)
  1405. {
  1406.   if (stream->class_table)
  1407.     return PTR2LONG(hash_value_for_key (stream->class_table, class->name));
  1408.   else
  1409.     return class_get_version (class);
  1410. }