]> 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 968f938362c423d93585a300859dee03b514f454..4fa37e2e1455de52a892fc8acbee4e228a010e44 100644 (file)
@@ -1,21 +1,37 @@
 /*
-Blind-ID library for user identification using RSA blind signatures
-Copyright (C) 2010  Rudolf Polzer
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Lesser General Public
-License as published by the Free Software Foundation; either
-version 2.1 of the License, or (at your option) any later version.
-
-This library 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
-Lesser General Public License for more details.
-
-You should have received a copy of the GNU Lesser General Public
-License along with this library; if not, write to the Free Software
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-*/
+ * FILE:       d0_iobuf.c
+ * AUTHOR:     Rudolf Polzer - divVerent@xonotic.org
+ * 
+ * Copyright (c) 2010, Rudolf Polzer
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Format:commit %H$
+ * $Id$
+ */
 
 #include "d0_iobuf.h"
 
@@ -25,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)
@@ -34,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;
@@ -46,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;
 }
 
-BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len)
+d0_iobuf_t *d0_iobuf_open_write_p(void **buf, size_t len)
 {
-       BOOL r = buf->ok;
+       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;
+}
+
+D0_BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len)
+{
+       D0_BOOL r = buf->ok;
        if(len)
                *len = buf->outpos;
        d0_free(buf);
@@ -65,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;
@@ -72,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;
 }
 
@@ -88,26 +143,25 @@ 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;
        while(nn)
        {
-               c = nn & 255;
-               nn >>= 8;
+               c = nn & 0x7F;
+               nn >>= 7;
+               if(nn)
+                       c |= 0x80;
                if(d0_iobuf_write_raw(buf, &c, 1) != 1)
                        return 0;
        }
-       c = 0;
-       if(d0_iobuf_write_raw(buf, &c, 1) != 1)
-               return 0;
        if(d0_iobuf_write_raw(buf, s, n) != n)
                return 0;
        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;
@@ -116,10 +170,10 @@ BOOL d0_iobuf_read_packet(d0_iobuf_t *buf, void *s, size_t *np)
        {
                if(d0_iobuf_read_raw(buf, &c, 1) != 1)
                        return 0;
-               n |= nn * c;
-               nn <<= 8;
+               n |= nn * (c & 0x7F);
+               nn <<= 7;
        }
-       while(c);
+       while(c & 0x80);
        if(n > *np)
                return 0;
        if(d0_iobuf_read_raw(buf, s, n) != n)
@@ -128,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;
@@ -150,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