3 * AUTHOR: Rudolf Polzer - divVerent@xonotic.org
5 * Copyright (c) 2010, Rudolf Polzer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
42 const unsigned char *inbuf;
43 unsigned char *outbuf;
44 unsigned char **outbufp;
45 size_t inpos, outpos, inbuflen, outbuflen;
50 d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len)
52 d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
53 b->inbuf = (const unsigned char *) buf;
56 b->inpos = b->outpos = 0;
63 d0_iobuf_t *d0_iobuf_open_write(void *buf, size_t len)
65 d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
66 b->inbuf = (const unsigned char *) buf;
67 b->outbuf = (unsigned char *) buf;
69 b->inpos = b->outpos = 0;
76 d0_iobuf_t *d0_iobuf_open_write_p(void **buf, size_t len)
78 d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
79 b->inbuf = (const unsigned char *) *buf;
80 b->outbuf = (unsigned char *) *buf;
81 b->outbufp = (unsigned char **) buf;
82 b->inpos = b->outpos = 0;
89 D0_BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len)
98 size_t d0_iobuf_write_raw(d0_iobuf_t *buf, const void *s, size_t n)
102 // if packet doesn't fit, expand buffer
103 if(buf->outbufp && nreal > buf->outbuflen - buf->outpos)
106 while(nreal + buf->outpos > newsize)
110 char *newbuf = d0_malloc(newsize);
113 memcpy(newbuf, buf->outbuf, buf->outbuflen);
114 d0_free(buf->outbuf);
116 buf->outbuf = newbuf;
117 *buf->outbufp = newbuf;
118 buf->outbuflen = newsize;
122 if(nreal > buf->outbuflen - buf->outpos)
125 nreal = buf->outbuflen - buf->outpos;
127 memcpy(buf->outbuf + buf->outpos, s, nreal);
128 buf->outpos += nreal;
129 buf->inbuflen = buf->outpos;
133 size_t d0_iobuf_read_raw(d0_iobuf_t *buf, void *s, size_t n)
136 if(nreal > buf->inbuflen - buf->inpos)
139 nreal = buf->inbuflen - buf->inpos;
141 memcpy(s, buf->inbuf + buf->inpos, nreal);
146 D0_BOOL d0_iobuf_write_packet(d0_iobuf_t *buf, const void *s, size_t n)
156 if(d0_iobuf_write_raw(buf, &c, 1) != 1)
159 if(d0_iobuf_write_raw(buf, s, n) != n)
164 D0_BOOL d0_iobuf_read_packet(d0_iobuf_t *buf, void *s, size_t *np)
171 if(d0_iobuf_read_raw(buf, &c, 1) != 1)
173 n |= nn * (c & 0x7F);
179 if(d0_iobuf_read_raw(buf, s, n) != n)
185 D0_BOOL d0_iobuf_conv_base64_in(d0_iobuf_t *buf)
187 // compand the in-buffer
191 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
192 static void base64_3to4(const unsigned char *in, unsigned char *out, int bytes)
194 unsigned char i0 = (bytes > 0) ? in[0] : 0;
195 unsigned char i1 = (bytes > 1) ? in[1] : 0;
196 unsigned char i2 = (bytes > 2) ? in[2] : 0;
197 unsigned char o0 = base64[i0 >> 2];
198 unsigned char o1 = base64[((i0 << 4) | (i1 >> 4)) & 077];
199 unsigned char o2 = base64[((i1 << 2) | (i2 >> 6)) & 077];
200 unsigned char o3 = base64[i2 & 077];
201 out[0] = (bytes > 0) ? o0 : '?';
202 out[1] = (bytes > 0) ? o1 : '?';
203 out[2] = (bytes > 1) ? o2 : '=';
204 out[3] = (bytes > 2) ? o3 : '=';
207 D0_BOOL d0_iobuf_conv_base64_out(d0_iobuf_t *buf)
210 // expand the out-buffer
211 blocks = ((buf->outpos + 2) / 3);
212 if(blocks*4 > buf->outbuflen)
214 for(i = blocks; i > 0; )
217 base64_3to4(buf->outbuf + 3*i, buf->outbuf + 4*i, buf->outpos - 3*i);
219 buf->outpos = blocks * 4;