2 Blind-ID library for user identification using RSA blind signatures
3 Copyright (C) 2010 Rudolf Polzer
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 const unsigned char *inbuf;
27 unsigned char *outbuf;
28 size_t inpos, outpos, inbuflen, outbuflen;
32 d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len)
34 d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
35 b->inbuf = (const unsigned char *) buf;
37 b->inpos = b->outpos = 0;
44 d0_iobuf_t *d0_iobuf_open_write(void *buf, size_t len)
46 d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t));
47 b->inbuf = (const unsigned char *) buf;
48 b->outbuf = (unsigned char *) buf;
49 b->inpos = b->outpos = 0;
56 BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len)
65 size_t d0_iobuf_write_raw(d0_iobuf_t *buf, const void *s, size_t n)
68 if(nreal > buf->outbuflen - buf->outpos)
71 nreal = buf->outbuflen - buf->outpos;
73 memcpy(buf->outbuf + buf->outpos, s, nreal);
78 size_t d0_iobuf_read_raw(d0_iobuf_t *buf, void *s, size_t n)
81 if(nreal > buf->inbuflen - buf->inpos)
84 nreal = buf->inbuflen - buf->inpos;
86 memcpy(s, buf->inbuf + buf->inpos, nreal);
91 BOOL d0_iobuf_write_packet(d0_iobuf_t *buf, const void *s, size_t n)
99 if(d0_iobuf_write_raw(buf, &c, 1) != 1)
103 if(d0_iobuf_write_raw(buf, &c, 1) != 1)
105 if(d0_iobuf_write_raw(buf, s, n) != n)
110 BOOL d0_iobuf_read_packet(d0_iobuf_t *buf, void *s, size_t *np)
117 if(d0_iobuf_read_raw(buf, &c, 1) != 1)
125 if(d0_iobuf_read_raw(buf, s, n) != n)
131 BOOL d0_iobuf_conv_base64_in(d0_iobuf_t *buf)
133 // compand the in-buffer
137 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
138 static void base64_3to4(const unsigned char *in, unsigned char *out, int bytes)
140 unsigned char i0 = (bytes > 0) ? in[0] : 0;
141 unsigned char i1 = (bytes > 1) ? in[1] : 0;
142 unsigned char i2 = (bytes > 2) ? in[2] : 0;
143 unsigned char o0 = base64[i0 >> 2];
144 unsigned char o1 = base64[((i0 << 4) | (i1 >> 4)) & 077];
145 unsigned char o2 = base64[((i1 << 2) | (i2 >> 6)) & 077];
146 unsigned char o3 = base64[i2 & 077];
147 out[0] = (bytes > 0) ? o0 : '?';
148 out[1] = (bytes > 0) ? o1 : '?';
149 out[2] = (bytes > 1) ? o2 : '=';
150 out[3] = (bytes > 2) ? o3 : '=';
153 BOOL d0_iobuf_conv_base64_out(d0_iobuf_t *buf)
156 // expand the out-buffer
157 blocks = ((buf->outpos + 2) / 3);
158 if(blocks*4 > buf->outbuflen)
160 for(i = blocks; i > 0; )
163 base64_3to4(buf->outbuf + 3*i, buf->outbuf + 4*i, buf->outpos - 3*i);
165 buf->outpos = blocks * 4;