]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blobdiff - d0_iobuf.c
on G*nt**, initialized globals seem to be made of fail and crash
[xonotic/d0_blind_id.git] / d0_iobuf.c
index e71f4791203913c01c7fa8bd1dfd6b822d6d2acc..4fa37e2e1455de52a892fc8acbee4e228a010e44 100644 (file)
@@ -30,6 +30,7 @@
  * SUCH DAMAGE.
  *
  * $Format:commit %H$
+ * $Id$
  */
 
 #include "d0_iobuf.h"
@@ -40,8 +41,10 @@ struct d0_iobuf_s
 {
        const unsigned char *inbuf;
        unsigned char *outbuf;
+       unsigned char **outbufp;
        size_t inpos, outpos, inbuflen, outbuflen;
-       BOOL ok;
+       D0_BOOL ok;
+       D0_BOOL pdata;
 };
 
 d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len)
@@ -49,6 +52,7 @@ d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len)
        d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
        b->inbuf = (const unsigned char *) buf;
        b->outbuf = NULL;
+       b->outbufp = NULL;
        b->inpos = b->outpos = 0;
        b->inbuflen = len;
        b->outbuflen = 0;
@@ -61,16 +65,30 @@ d0_iobuf_t *d0_iobuf_open_write(void *buf, size_t len)
        d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
        b->inbuf = (const unsigned char *) buf;
        b->outbuf = (unsigned char *) buf;
+       b->outbufp = NULL;
        b->inpos = b->outpos = 0;
-       b->inbuflen = len;
+       b->inbuflen = 0;
+       b->outbuflen = len;
+       b->ok = 1;
+       return b;
+}
+
+d0_iobuf_t *d0_iobuf_open_write_p(void **buf, size_t len)
+{
+       d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
+       b->inbuf = (const unsigned char *) *buf;
+       b->outbuf = (unsigned char *) *buf;
+       b->outbufp = (unsigned char **) buf;
+       b->inpos = b->outpos = 0;
+       b->inbuflen = 0;
        b->outbuflen = len;
        b->ok = 1;
        return b;
 }
 
-BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len)
+D0_BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len)
 {
-       BOOL r = buf->ok;
+       D0_BOOL r = buf->ok;
        if(len)
                *len = buf->outpos;
        d0_free(buf);
@@ -80,6 +98,27 @@ BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len)
 size_t d0_iobuf_write_raw(d0_iobuf_t *buf, const void *s, size_t n)
 {
        size_t nreal = n;
+
+       // if packet doesn't fit, expand buffer
+       if(buf->outbufp && nreal > buf->outbuflen - buf->outpos)
+       {
+               size_t newsize = 1;
+               while(nreal + buf->outpos > newsize)
+                       newsize <<= 1;
+
+               {
+                       char *newbuf = d0_malloc(newsize);
+                       if(buf->outbuf)
+                       {
+                               memcpy(newbuf, buf->outbuf, buf->outbuflen);
+                               d0_free(buf->outbuf);
+                       }
+                       buf->outbuf = newbuf;
+                       *buf->outbufp = newbuf;
+                       buf->outbuflen = newsize;
+               }
+       }
+
        if(nreal > buf->outbuflen - buf->outpos)
        {
                buf->ok = 0;
@@ -87,6 +126,7 @@ size_t d0_iobuf_write_raw(d0_iobuf_t *buf, const void *s, size_t n)
        }
        memcpy(buf->outbuf + buf->outpos, s, nreal);
        buf->outpos += nreal;
+       buf->inbuflen = buf->outpos;
        return nreal;
 }
 
@@ -103,7 +143,7 @@ size_t d0_iobuf_read_raw(d0_iobuf_t *buf, void *s, size_t n)
        return nreal;
 }
 
-BOOL d0_iobuf_write_packet(d0_iobuf_t *buf, const void *s, size_t n)
+D0_BOOL d0_iobuf_write_packet(d0_iobuf_t *buf, const void *s, size_t n)
 {
        unsigned char c;
        size_t nn = n;
@@ -121,7 +161,7 @@ BOOL d0_iobuf_write_packet(d0_iobuf_t *buf, const void *s, size_t n)
        return 1;
 }
 
-BOOL d0_iobuf_read_packet(d0_iobuf_t *buf, void *s, size_t *np)
+D0_BOOL d0_iobuf_read_packet(d0_iobuf_t *buf, void *s, size_t *np)
 {
        unsigned char c;
        size_t n = 0;
@@ -142,7 +182,7 @@ BOOL d0_iobuf_read_packet(d0_iobuf_t *buf, void *s, size_t *np)
        return 1;
 }
 
-BOOL d0_iobuf_conv_base64_in(d0_iobuf_t *buf)
+D0_BOOL d0_iobuf_conv_base64_in(d0_iobuf_t *buf)
 {
        // compand the in-buffer
        return 0;
@@ -164,7 +204,7 @@ static void base64_3to4(const unsigned char *in, unsigned char *out, int bytes)
        out[3] = (bytes > 2) ? o3 : '=';
 }
 
-BOOL d0_iobuf_conv_base64_out(d0_iobuf_t *buf)
+D0_BOOL d0_iobuf_conv_base64_out(d0_iobuf_t *buf)
 {
        size_t blocks, i;
        // expand the out-buffer