qrencode  4.1.1
About: libqrencode is a fast and compact library and command-line utility for encoding data in a QR Code symbol.
  Fossies Dox: qrencode-4.1.1.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

bitstream.c
Go to the documentation of this file.
1/*
2 * qrencode - QR Code encoder
3 *
4 * Binary sequence class.
5 * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#if HAVE_CONFIG_H
23# include "config.h"
24#endif
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include "bitstream.h"
30
31#define DEFAULT_BUFSIZE (128)
32
34{
35 BitStream *bstream;
36
37 bstream = (BitStream *)malloc(sizeof(BitStream));
38 if(bstream == NULL) return NULL;
39
40 bstream->length = 0;
41 bstream->data = (unsigned char *)malloc(DEFAULT_BUFSIZE);
42 if(bstream->data == NULL) {
43 free(bstream);
44 return NULL;
45 }
46 bstream->datasize = DEFAULT_BUFSIZE;
47
48 return bstream;
49}
50
51#ifdef WITH_TESTS
52BitStream *BitStream_newWithBits(size_t size, unsigned char *bits)
53{
54 BitStream *bstream;
55
56 if(size == 0) return BitStream_new();
57
58 bstream = (BitStream *)malloc(sizeof(BitStream));
59 if(bstream == NULL) return NULL;
60
61 bstream->data = (unsigned char *)malloc(size);
62 if(bstream->data == NULL) {
63 free(bstream);
64 return NULL;
65 }
66
67 bstream->length = size;
68 bstream->datasize = size;
69 memcpy(bstream->data, bits, size);
70
71 return bstream;
72}
73#endif
74
75static int BitStream_expand(BitStream *bstream)
76{
77 unsigned char *data;
78
79 data = (unsigned char *)realloc(bstream->data, bstream->datasize * 2);
80 if(data == NULL) {
81 return -1;
82 }
83
84 bstream->data = data;
85 bstream->datasize *= 2;
86
87 return 0;
88}
89
90static void BitStream_writeNum(unsigned char *dest, size_t bits, unsigned int num)
91{
92 unsigned int mask;
93 size_t i;
94 unsigned char *p;
95
96 p = dest;
97 mask = 1U << (bits - 1);
98 for(i = 0; i < bits; i++) {
99 if(num & mask) {
100 *p = 1;
101 } else {
102 *p = 0;
103 }
104 p++;
105 mask = mask >> 1;
106 }
107}
108
109static void BitStream_writeBytes(unsigned char *dest, size_t size, unsigned char *data)
110{
111 unsigned char mask;
112 size_t i, j;
113 unsigned char *p;
114
115 p = dest;
116 for(i = 0; i < size; i++) {
117 mask = 0x80;
118 for(j = 0; j < 8; j++) {
119 if(data[i] & mask) {
120 *p = 1;
121 } else {
122 *p = 0;
123 }
124 p++;
125 mask = mask >> 1;
126 }
127 }
128}
129
131{
132 int ret;
133
134 if(arg == NULL) {
135 return -1;
136 }
137 if(arg->length == 0) {
138 return 0;
139 }
140
141 while(bstream->length + arg->length > bstream->datasize) {
142 ret = BitStream_expand(bstream);
143 if(ret < 0) return ret;
144 }
145
146 memcpy(bstream->data + bstream->length, arg->data, arg->length);
147 bstream->length += arg->length;
148
149 return 0;
150}
151
152int BitStream_appendNum(BitStream *bstream, size_t bits, unsigned int num)
153{
154 int ret;
155
156 if(bits == 0) return 0;
157
158 while(bstream->datasize - bstream->length < bits) {
159 ret = BitStream_expand(bstream);
160 if(ret < 0) return ret;
161 }
162 BitStream_writeNum(bstream->data + bstream->length, bits, num);
163 bstream->length += bits;
164
165 return 0;
166}
167
168int BitStream_appendBytes(BitStream *bstream, size_t size, unsigned char *data)
169{
170 int ret;
171
172 if(size == 0) return 0;
173
174 while(bstream->datasize - bstream->length < size * 8) {
175 ret = BitStream_expand(bstream);
176 if(ret < 0) return ret;
177 }
178 BitStream_writeBytes(bstream->data + bstream->length, size, data);
179 bstream->length += size * 8;
180
181 return 0;
182}
183
184unsigned char *BitStream_toByte(BitStream *bstream)
185{
186 size_t i, j, size, bytes, oddbits;
187 unsigned char *data, v;
188 unsigned char *p;
189
190 size = BitStream_size(bstream);
191 if(size == 0) {
192 return NULL;
193 }
194 data = (unsigned char *)malloc((size + 7) / 8);
195 if(data == NULL) {
196 return NULL;
197 }
198
199 bytes = size / 8;
200
201 p = bstream->data;
202 for(i = 0; i < bytes; i++) {
203 v = 0;
204 for(j = 0; j < 8; j++) {
205 v = (unsigned char)(v << 1);
206 v |= *p;
207 p++;
208 }
209 data[i] = v;
210 }
211 oddbits = size & 7;
212 if(oddbits > 0) {
213 v = 0;
214 for(j = 0; j < oddbits; j++) {
215 v = (unsigned char)(v << 1);
216 v |= *p;
217 p++;
218 }
219 data[bytes] = (unsigned char)(v << (8 - oddbits));
220 }
221
222 return data;
223}
224
226{
227 if(bstream != NULL) {
228 free(bstream->data);
229 free(bstream);
230 }
231}
static void BitStream_writeNum(unsigned char *dest, size_t bits, unsigned int num)
Definition: bitstream.c:90
void BitStream_free(BitStream *bstream)
Definition: bitstream.c:225
BitStream * BitStream_new(void)
Definition: bitstream.c:33
static int BitStream_expand(BitStream *bstream)
Definition: bitstream.c:75
#define DEFAULT_BUFSIZE
Definition: bitstream.c:31
unsigned char * BitStream_toByte(BitStream *bstream)
Definition: bitstream.c:184
int BitStream_appendNum(BitStream *bstream, size_t bits, unsigned int num)
Definition: bitstream.c:152
static void BitStream_writeBytes(unsigned char *dest, size_t size, unsigned char *data)
Definition: bitstream.c:109
int BitStream_append(BitStream *bstream, BitStream *arg)
Definition: bitstream.c:130
int BitStream_appendBytes(BitStream *bstream, size_t size, unsigned char *data)
Definition: bitstream.c:168
#define BitStream_size(__bstream__)
Definition: bitstream.h:38
static int size
Definition: qrenc.c:41
size_t datasize
Definition: bitstream.h:27
size_t length
Definition: bitstream.h:26
unsigned char * data
Definition: bitstream.h:28