$OpenBSD: patch-base_src_common_SDR_c,v 1.3 2002/09/04 16:21:43 todd Exp $
--- base/src/common/SDR.c.orig	Mon Jul 29 22:48:31 2002
+++ base/src/common/SDR.c	Tue Sep  3 23:34:47 2002
@@ -39,6 +39,7 @@
  */
 
 #include <opencm.h>
+#include <sys/types.h>
 
 #define BUFBSZ 1024
 #define STRING_GROW_UNIT 1024
@@ -76,14 +77,14 @@ struct SDR_stream {
 struct serializer {
   void           (*onCreate)(SDR_stream *);
 
-  void           (*w_u8)(const char *, SDR_stream *, unsigned char);
-  unsigned char  (*r_u8)(const char *, SDR_stream *);
+  void           (*w_u8)(const char *, SDR_stream *, u_int8_t);
+  u_int8_t       (*r_u8)(const char *, SDR_stream *);
 
-  void           (*w_u16)(const char *, SDR_stream *, unsigned short);
-  unsigned short (*r_u16)(const char *, SDR_stream *);
+  void           (*w_u16)(const char *, SDR_stream *, u_int16_t);
+  u_int16_t      (*r_u16)(const char *, SDR_stream *);
 
-  void           (*w_u32)(const char *, SDR_stream *, unsigned long);
-  unsigned long  (*r_u32)(const char *, SDR_stream *);
+  void           (*w_u32)(const char *, SDR_stream *, u_int32_t);
+  u_int32_t      (*r_u32)(const char *, SDR_stream *);
 
   void           (*w_u64)(const char *, SDR_stream *, 
 			  oc_uint64_t);
@@ -92,8 +93,8 @@ struct serializer {
   void           (*w_buffer)(const char *elem, SDR_stream *, const Buffer*);
   Buffer *       (*r_buffer)(const char *elem, SDR_stream *, ocmoff_t len);
 
-  void           (*w_bytes)(const char *, SDR_stream *, unsigned long len, const void *);
-  void *         (*r_bytes)(const char *, SDR_stream *, unsigned long len);
+  void           (*w_bytes)(const char *, SDR_stream *, u_int32_t len, const void *);
+  void *         (*r_bytes)(const char *, SDR_stream *, u_int32_t len);
 
   void           (*w_string)(const char *, SDR_stream *, const char *);
   char *         (*r_string)(const char *, SDR_stream *);
@@ -116,13 +117,15 @@ file_stream_putc(SDR_stream *s, int c)
   s->pos++;
   s->len++;
   s->bound++;
-  return fputc(c, s->state);
+  return fputc(c, (FILE*)s->state);
 }
 
 static ocmoff_t
 file_stream_puts(SDR_stream *s, const void *vp, ocmoff_t len)
 {
-  size_t wlen = fwrite(vp, 1, len, s->state);
+  size_t wlen = fwrite(vp, 1, len, (FILE*)s->state);
+  if(len != wlen || ferror((FILE*)s->state))
+    THROW(ExTruncated, format("Writing to %s was truncated\n", s->name));
   s->pos += wlen;
   s->len += wlen;
   s->bound += wlen;
@@ -142,7 +145,7 @@ file_stream_getc(SDR_stream *s)
 {
   int c;
 
-  c = fgetc(s->state);
+  c = fgetc((FILE*)s->state);
   s->pos++;
   return c;
 }
@@ -150,7 +153,7 @@ file_stream_getc(SDR_stream *s)
 static ocmoff_t
 file_stream_gets(SDR_stream *s, void *vp, ocmoff_t len)
 {
-  size_t rlen = fread(vp, 1, len, s->state);
+  size_t rlen = fread(vp, 1, len, (FILE*)s->state);
   s->pos += rlen;
   
   return rlen;
@@ -161,8 +164,8 @@ file_stream_reread(SDR_stream *s)
 {
   assert(s->state);
 
-  fflush(s->state);
-  rewind(s->state);
+  fflush((FILE*)s->state);
+  rewind((FILE*)s->state);
   s->pos = 0;
 
   s->mode = MODE_RSTREAM;
@@ -175,7 +178,7 @@ static void
 file_stream_close(SDR_stream *s)
 {
   if (s->state) {
-    fflush(s->state);
+    fflush((FILE*)s->state);
     xfclose((FILE *) s->state);
   }
   s->state = 0;
@@ -568,8 +571,8 @@ stream_ungetc(SDR_stream *strm, int c)
 size_t
 stream_vprintf(SDR_stream *s, const char *fmt, va_list ap)
 {
-  unsigned long len;
-  unsigned long width = 0;
+  u_int32_t len;
+  u_int32_t width = 0;
   OC_bool sign;
   OC_bool rightAdjust;
   char fillchar;
@@ -584,7 +587,7 @@ stream_vprintf(SDR_stream *s, const char
       continue;
     }
 
-    /* largest thing we might convert fits in 20 digits (unsigned long
+    /* largest thing we might convert fits in 20 digits (u_int32_t
      * long as decimal */
     
     pend = &buf[20];
@@ -647,7 +650,7 @@ stream_vprintf(SDR_stream *s, const char
     case 'd':
       {
 	long l;
-	unsigned long ul;
+	u_int32_t ul;
 
 	l = va_arg(ap, long);
 	      
@@ -661,7 +664,7 @@ stream_vprintf(SDR_stream *s, const char
 	  ul = (l < 0) ? (unsigned) -l : (unsigned) l;
 
 	  if (l == LONG_MIN)
-	    ul = ((unsigned long) LONG_MAX) + 1ul;
+	    ul = ((u_int32_t) LONG_MAX) + 1ul;
 
 	  while(ul) {
 	    *(--p) = '0' + (ul % 10);
@@ -675,9 +678,9 @@ stream_vprintf(SDR_stream *s, const char
       }
     case 'u':
       {
-	unsigned long ul;
+	u_int32_t ul;
 
-	ul = va_arg(ap, unsigned long);
+	ul = va_arg(ap, u_int32_t);
 	      
 	if (ul == 0) {
 	  *(--p) = '0';
@@ -692,11 +695,11 @@ stream_vprintf(SDR_stream *s, const char
       }
     case 'x':
       {
-	unsigned long ul;
+	u_int32_t ul;
 
 	char *hexout = "0123456789abcdef";
 
-	ul = va_arg(ap, unsigned long);
+	ul = va_arg(ap, u_int32_t);
 	      
 	if (ul == 0) {
 	  *(--p) = '0';
@@ -826,7 +829,7 @@ stream_scanf(SDR_stream *s, const char *
     case 'u':
     case 'U':
       {
-	unsigned long ull = 0;
+	u_int32_t ull = 0;
 
 	do {
 	  c = stream_getc(s);
@@ -852,10 +855,10 @@ stream_scanf(SDR_stream *s, const char *
 	--input_count;
 
 	if (*fmt == 'u') {
-	  unsigned long *ulp;
+	  u_int32_t *ulp;
 
-	  ulp = va_arg(ap, unsigned long *);
-	  *ulp = (unsigned long) ull;
+	  ulp = va_arg(ap, u_int32_t *);
+	  *ulp = (u_int32_t) ull;
 	  assert(ull <= ULONG_MAX);
 	}
 	else {
@@ -869,10 +872,10 @@ stream_scanf(SDR_stream *s, const char *
       }
     case 'x':
       {
-	unsigned long ul = 0;
-	unsigned long *ulp;
+	u_int32_t ul = 0;
+	u_int32_t *ulp;
 
-	ulp = va_arg(ap, unsigned long *);
+	ulp = va_arg(ap, u_int32_t *);
 
 	do {
 	  c = stream_getc(s);
@@ -1028,7 +1031,7 @@ stream_autodetect_format(SDR_stream *str
       /* Texty format starts with TEXTY\n
        *
        * The newline isn't really necessary, but otherwise the file
-       * will look kind of ugly, with stupid stuff like Tobtype ...
+       * will look kind of ugly, with stupid stuff like TEXTYobtype 5...
        */
       const char* expecting = "EXTY\n";
 
@@ -1046,6 +1049,26 @@ stream_autodetect_format(SDR_stream *str
 
       break;
     }
+  case 'D':
+    {
+      /* Texty format starts with DTEXTY\n
+       */
+      const char* expecting = "TEXTY\n";
+
+      do {
+        c = stream_getc(stream);
+        if(c != *expecting)
+          THROW(ExMalformed,
+                format("Bad stream format (thought it was DTEXTY); "
+                       "source \"%s\", starts with char %x\n",
+                       stream->name, c));
+        expecting++;
+      } while (c != '\n');
+
+      stream->format = SDR_DTEXTY;
+
+      break;
+    }
   default:
     {
       THROW(ExMalformed, 
@@ -1317,13 +1340,13 @@ sdr_show(const void *ob)
 
 /* GENERIC WRAPPERS */
 void
-sdr_w_u8(const char *elem, SDR_stream *strm, unsigned char u)
+sdr_w_u8(const char *elem, SDR_stream *strm, u_int8_t u)
 {
   assert(sermodes[strm->format].w_u8);
   sermodes[strm->format].w_u8(elem, strm, u);  
 }
 
-unsigned char
+u_int8_t
 sdr_r_u8(const char *elem, SDR_stream *strm)
 {
   assert(sermodes[strm->format].r_u8);
@@ -1331,13 +1354,13 @@ sdr_r_u8(const char *elem, SDR_stream *s
 }
 
 void
-sdr_w_u16(const char *elem, SDR_stream *strm, unsigned short u)
+sdr_w_u16(const char *elem, SDR_stream *strm, u_int16_t u)
 {
   assert(sermodes[strm->format].w_u16);
   sermodes[strm->format].w_u16(elem, strm, u);  
 }
 
-unsigned short
+u_int16_t
 sdr_r_u16(const char *elem, SDR_stream *strm)
 {
   assert(sermodes[strm->format].r_u16);
@@ -1345,13 +1368,13 @@ sdr_r_u16(const char *elem, SDR_stream *
 }
 
 void
-sdr_w_u32(const char *elem, SDR_stream *strm, unsigned long u)
+sdr_w_u32(const char *elem, SDR_stream *strm, u_int32_t u)
 {
   assert(sermodes[strm->format].w_u32);
   sermodes[strm->format].w_u32(elem, strm, u);  
 }
 
-unsigned long
+u_int32_t
 sdr_r_u32(const char *elem, SDR_stream *strm)
 {
   assert(sermodes[strm->format].r_u32);
@@ -1387,14 +1410,14 @@ sdr_r_buffer(const char *elem, SDR_strea
 }
 
 void
-sdr_w_bytes(const char *elem, SDR_stream *strm, unsigned long len, const void *v)
+sdr_w_bytes(const char *elem, SDR_stream *strm, u_int32_t len, const void *v)
 {
   assert(sermodes[strm->format].w_bytes);
   sermodes[strm->format].w_bytes(elem, strm, len, v);  
 }
 
 void *
-sdr_r_bytes(const char *elem, SDR_stream *strm, unsigned long len)
+sdr_r_bytes(const char *elem, SDR_stream *strm, u_int32_t len)
 {
   assert(sermodes[strm->format].r_bytes);
   return sermodes[strm->format].r_bytes(elem, strm, len);  
@@ -1450,16 +1473,16 @@ bin_onCreate(SDR_stream * strm)
 }
 
 static void
-bin_w_u8(const char *elem, SDR_stream *strm, unsigned char u)
+bin_w_u8(const char *elem, SDR_stream *strm, u_int8_t u)
 {
   if (stream_write(strm, &u, sizeof(u)) < sizeof(u))
     THROW(ExTruncated, "Serialization write error");
 }
 
-static unsigned char
+static u_int8_t
 bin_r_u8(const char *elem, SDR_stream *strm)
 {
-  unsigned char u;
+  u_int8_t u;
 
   if (stream_read(strm, &u, sizeof(u)) < sizeof(u))
     THROW(ExTruncated, "Serialization read error");
@@ -1468,7 +1491,7 @@ bin_r_u8(const char *elem, SDR_stream *s
 }
 
 static void
-bin_w_u16(const char *elem, SDR_stream *strm, unsigned short u)
+bin_w_u16(const char *elem, SDR_stream *strm, u_int16_t u)
 {
   int i;
   unsigned char bytes[sizeof(u)];
@@ -1482,11 +1505,11 @@ bin_w_u16(const char *elem, SDR_stream *
     THROW(ExTruncated, "Serialization write error");
 }
 
-static unsigned short
+static u_int16_t
 bin_r_u16(const char *elem, SDR_stream *strm)
 {
   int i;
-  unsigned short u;
+  u_int16_t u;
   unsigned char bytes[sizeof(u)];
 
   if (stream_read(strm, bytes, sizeof(u)) < sizeof(u))
@@ -1496,7 +1519,7 @@ bin_r_u16(const char *elem, SDR_stream *
   u = 0;
 
   do {
-    unsigned long bu;
+    u_int32_t bu;
     
     i--;
     u = u << 8;
@@ -1508,7 +1531,7 @@ bin_r_u16(const char *elem, SDR_stream *
 }
 
 static void
-bin_w_u32(const char *elem, SDR_stream *strm, unsigned long u)
+bin_w_u32(const char *elem, SDR_stream *strm, u_int32_t u)
 {
   int i;
   unsigned char bytes[sizeof(u)];
@@ -1522,11 +1545,11 @@ bin_w_u32(const char *elem, SDR_stream *
     THROW(ExTruncated, "Serialization write error");
 }
 
-static unsigned long
+static u_int32_t
 bin_r_u32(const char *elem, SDR_stream *strm)
 {
   int i;
-  unsigned long u;
+  u_int32_t u;
   unsigned char bytes[sizeof(u)];
 
   if (stream_read(strm, bytes, sizeof(u)) < sizeof(u))
@@ -1536,7 +1559,7 @@ bin_r_u32(const char *elem, SDR_stream *
   u = 0;
 
   do {
-    unsigned long bu;
+    u_int32_t bu;
     
     i--;
     u = u << 8;
@@ -1589,7 +1612,7 @@ bin_r_u64(const char *elem, SDR_stream *
 
 /* Note that these do NOT read/write the actual length! */
 static void
-bin_w_bytes(const char *elem, SDR_stream *strm, unsigned long len, 
+bin_w_bytes(const char *elem, SDR_stream *strm, u_int32_t len, 
 	    const void *v)
 {
   const char *s = (const char *) v;
@@ -1599,7 +1622,7 @@ bin_w_bytes(const char *elem, SDR_stream
 }
 
 static void *
-bin_r_bytes(const char *elem, SDR_stream *strm, unsigned long len)
+bin_r_bytes(const char *elem, SDR_stream *strm, u_int32_t len)
 {
   char * s = (char *) GC_MALLOC_ATOMIC(sizeof(char) * (len+1));
   s[len] = 0;
@@ -1921,41 +1944,41 @@ texty_get_field(const char *name, char t
 }
 
 static void
-texty_w_u8(const char *elem, SDR_stream *strm, unsigned char num)
+texty_w_u8(const char *elem, SDR_stream *strm, u_int8_t num)
 {
   texty_do_indent(strm);
   stream_printf(strm, "%s I %u\n", elem, num);
 }
 
 static void
-texty_w_u16(const char *elem, SDR_stream *strm, unsigned short num)
+texty_w_u16(const char *elem, SDR_stream *strm, u_int16_t num)
 {
   texty_do_indent(strm);
   stream_printf(strm, "%s I %u\n", elem, num);
 }
 
 static void
-texty_w_u32(const char *elem, SDR_stream *strm, unsigned long num)
+texty_w_u32(const char *elem, SDR_stream *strm, u_int32_t num)
 {
   texty_do_indent(strm);
   stream_printf(strm, "%s I %u\n", elem, num);
 }
 
-static unsigned char
+static u_int8_t
 texty_r_u8(const char *elem, SDR_stream *strm)
 {
   texty_field *fld = texty_get_field(elem, 'I', strm);
   return fld->value;
 }
 
-static unsigned short
+static u_int16_t
 texty_r_u16(const char *elem, SDR_stream *strm)
 {
   texty_field *fld = texty_get_field(elem, 'I', strm);
   return fld->value;
 }
 
-static unsigned long
+static u_int32_t
 texty_r_u32(const char *elem, SDR_stream *strm)
 {
   texty_field *fld = texty_get_field(elem, 'I', strm);
@@ -1978,7 +2001,7 @@ texty_r_u64(const char *elem, SDR_stream
 
 static void
 texty_w_bytestring(const char *elem, SDR_stream *strm, const char ty, 
-		   unsigned long len, const void *vp)
+		   u_int32_t len, const void *vp)
 {
   texty_do_indent(strm);
 
@@ -2019,14 +2042,14 @@ texty_r_buffer(const char *elem, SDR_str
 }
 
 static void
-texty_w_bytes(const char *elem, SDR_stream *strm, unsigned long len,
+texty_w_bytes(const char *elem, SDR_stream *strm, u_int32_t len,
               const void *obj)
 {
   texty_w_bytestring(elem, strm, 'B', len, obj);
 }
 
 static void *
-texty_r_bytes(const char *elem, SDR_stream *strm, unsigned long len)
+texty_r_bytes(const char *elem, SDR_stream *strm, u_int32_t len)
 {
   texty_field *fld = texty_get_field(elem, 'B', strm);
   return fld->rep;
@@ -2125,6 +2148,11 @@ isSafeChar(unsigned char c)
   return (isprint(c) && c != ENCODE_CHAR) || isspace(c);
 }
 
+static void dtexty_onCreate(SDR_stream *strm)
+{
+  stream_printf(strm, "DTEXTY\n");
+}
+
 static ocmoff_t
 encodedLength(const void *vp, ocmoff_t len)
 {
@@ -2169,7 +2197,7 @@ encodeString(const void *vp, ocmoff_t le
 
 static void
 dtexty_w_bytestring(const char *elem, SDR_stream *strm, const char ty, 
-                    unsigned long len, const void *vp)
+                    u_int32_t len, const void *vp)
 {
   ocmoff_t elen = len;		/* encoded length */
   const void *evp = vp;		/* encoded string */
@@ -2207,7 +2235,6 @@ dtexty_get_field(const char *name, char 
   {
     /* Do an in-place conversion to safe form on the string that came
        back: */
-    
     unsigned char *base = fld->rep;
     unsigned char *end = base + fld->value;
     unsigned char *to = fld->rep;
@@ -2240,8 +2267,7 @@ dtexty_get_field(const char *name, char 
 	  THROW(ExMalformed, "Mis-encoded or malformed input string");
 
       }
-      else
-	*to++ = c;
+      *to++ = c;
     }
 
     fld->value = to - base;	/* len excludes terminating NUL */
@@ -2291,7 +2317,7 @@ dtexty_get_field(const char *name, char 
         {
           char* decoded;
           char hex[2];
-          unsigned int len = 2;
+          u_int32_t len = 2;
           encoded_rep++;
           hex[0] = *encoded_rep++;
           hex[1] = *encoded_rep++;
@@ -2334,14 +2360,14 @@ dtexty_get_field(const char *name, char 
 #if 0
 static void
 dtexty_w_bytestring(const char *elem, SDR_stream *strm, const char ty, 
-                    unsigned long len, const void *vp)
+                    u_int32_t len, const void *vp)
 {
   texty_do_indent(strm);
 
   if(vp)
   {
     const unsigned char* dat = (const unsigned char*)vp;
-    unsigned int j;
+    u_int32_t j;
 
     stream_printf(strm, "%s %c %u", elem, ty, len+1);
     if (ty == 'N' || ty == 'O')
@@ -2415,14 +2441,14 @@ dtexty_r_buffer(const char *elem, SDR_st
    dtexty_w_bytestring and dtexty_get_field. Yuck.
 */
 static void
-dtexty_w_bytes(const char *elem, SDR_stream *strm, unsigned long len,
+dtexty_w_bytes(const char *elem, SDR_stream *strm, u_int32_t len,
                const void *s)
 {
   dtexty_w_bytestring(elem, strm, 'B', len, s);
 }
 
 static void *
-dtexty_r_bytes(const char *elem, SDR_stream *strm, unsigned long len)
+dtexty_r_bytes(const char *elem, SDR_stream *strm, u_int32_t len)
 {
   texty_field *fld = dtexty_get_field(elem, 'B', strm);
   return fld->rep;
@@ -2539,7 +2565,7 @@ static struct serializer sermodes[] = {
     texty_w_obname, texty_r_obname,
     texty_write, texty_read },
   /* DTEXTY streams */
-  { texty_onCreate,
+  { dtexty_onCreate,
     texty_w_u8, texty_r_u8,
     texty_w_u16, texty_r_u16,
     texty_w_u32, texty_r_u32,
