]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_iobuf.c
e71f4791203913c01c7fa8bd1dfd6b822d6d2acc
[xonotic/d0_blind_id.git] / d0_iobuf.c
1 /*
2  * FILE:        d0_iobuf.c
3  * AUTHOR:      Rudolf Polzer - divVerent@xonotic.org
4  * 
5  * Copyright (c) 2010, Rudolf Polzer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Format:commit %H$
33  */
34
35 #include "d0_iobuf.h"
36
37 #include <string.h>
38
39 struct d0_iobuf_s
40 {
41         const unsigned char *inbuf;
42         unsigned char *outbuf;
43         size_t inpos, outpos, inbuflen, outbuflen;
44         BOOL ok;
45 };
46
47 d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len)
48 {
49         d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
50         b->inbuf = (const unsigned char *) buf;
51         b->outbuf = NULL;
52         b->inpos = b->outpos = 0;
53         b->inbuflen = len;
54         b->outbuflen = 0;
55         b->ok = 1;
56         return b;
57 }
58
59 d0_iobuf_t *d0_iobuf_open_write(void *buf, size_t len)
60 {
61         d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
62         b->inbuf = (const unsigned char *) buf;
63         b->outbuf = (unsigned char *) buf;
64         b->inpos = b->outpos = 0;
65         b->inbuflen = len;
66         b->outbuflen = len;
67         b->ok = 1;
68         return b;
69 }
70
71 BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len)
72 {
73         BOOL r = buf->ok;
74         if(len)
75                 *len = buf->outpos;
76         d0_free(buf);
77         return r;
78 }
79
80 size_t d0_iobuf_write_raw(d0_iobuf_t *buf, const void *s, size_t n)
81 {
82         size_t nreal = n;
83         if(nreal > buf->outbuflen - buf->outpos)
84         {
85                 buf->ok = 0;
86                 nreal = buf->outbuflen - buf->outpos;
87         }
88         memcpy(buf->outbuf + buf->outpos, s, nreal);
89         buf->outpos += nreal;
90         return nreal;
91 }
92
93 size_t d0_iobuf_read_raw(d0_iobuf_t *buf, void *s, size_t n)
94 {
95         size_t nreal = n;
96         if(nreal > buf->inbuflen - buf->inpos)
97         {
98                 buf->ok = 0;
99                 nreal = buf->inbuflen - buf->inpos;
100         }
101         memcpy(s, buf->inbuf + buf->inpos, nreal);
102         buf->inpos += nreal;
103         return nreal;
104 }
105
106 BOOL d0_iobuf_write_packet(d0_iobuf_t *buf, const void *s, size_t n)
107 {
108         unsigned char c;
109         size_t nn = n;
110         while(nn)
111         {
112                 c = nn & 0x7F;
113                 nn >>= 7;
114                 if(nn)
115                         c |= 0x80;
116                 if(d0_iobuf_write_raw(buf, &c, 1) != 1)
117                         return 0;
118         }
119         if(d0_iobuf_write_raw(buf, s, n) != n)
120                 return 0;
121         return 1;
122 }
123
124 BOOL d0_iobuf_read_packet(d0_iobuf_t *buf, void *s, size_t *np)
125 {
126         unsigned char c;
127         size_t n = 0;
128         size_t nn = 1;
129         do
130         {
131                 if(d0_iobuf_read_raw(buf, &c, 1) != 1)
132                         return 0;
133                 n |= nn * (c & 0x7F);
134                 nn <<= 7;
135         }
136         while(c & 0x80);
137         if(n > *np)
138                 return 0;
139         if(d0_iobuf_read_raw(buf, s, n) != n)
140                 return 0;
141         *np = n;
142         return 1;
143 }
144
145 BOOL d0_iobuf_conv_base64_in(d0_iobuf_t *buf)
146 {
147         // compand the in-buffer
148         return 0;
149 }
150
151 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
152 static void base64_3to4(const unsigned char *in, unsigned char *out, int bytes)
153 {
154         unsigned char i0 = (bytes > 0) ? in[0] : 0;
155         unsigned char i1 = (bytes > 1) ? in[1] : 0;
156         unsigned char i2 = (bytes > 2) ? in[2] : 0;
157         unsigned char o0 = base64[i0 >> 2];
158         unsigned char o1 = base64[((i0 << 4) | (i1 >> 4)) & 077];
159         unsigned char o2 = base64[((i1 << 2) | (i2 >> 6)) & 077];
160         unsigned char o3 = base64[i2 & 077];
161         out[0] = (bytes > 0) ? o0 : '?';
162         out[1] = (bytes > 0) ? o1 : '?';
163         out[2] = (bytes > 1) ? o2 : '=';
164         out[3] = (bytes > 2) ? o3 : '=';
165 }
166
167 BOOL d0_iobuf_conv_base64_out(d0_iobuf_t *buf)
168 {
169         size_t blocks, i;
170         // expand the out-buffer
171         blocks = ((buf->outpos + 2) / 3);
172         if(blocks*4 > buf->outbuflen)
173                 return 0;
174         for(i = blocks; i > 0; )
175         {
176                 --i;
177                 base64_3to4(buf->outbuf + 3*i, buf->outbuf + 4*i, buf->outpos - 3*i);
178         }
179         buf->outpos = blocks * 4;
180         return 1;
181 }