src/bitstream.c

filter: Statementstmt 82.7% 91 / 110 Branchbran 62.0% 36 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.6% 127 / 168 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/QRCode.xs ↑ Summary src/qrspec.c »
1 /*
2 * qrencode - QR Code encoder
3 *
4 * Binary sequence class.
5 * Copyright (C) 2006-2011 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 48 BitStream *BitStream_new(void)
32 {
33 BitStream *bstream;
34  
35 48 bstream = (BitStream *)malloc(sizeof(BitStream));
36 48 ▶ if(bstream == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(bstream == NULL) return NULL; 0 48 48
37  
38 48 bstream->length = 0;
39 48 bstream->data = NULL;
40  
41 48 return bstream;
42 }
43  
44 48 static int BitStream_allocate(BitStream *bstream, int length)
45 {
46 unsigned char *data;
47  
48 48 ▶ if(bstream == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(bstream == NULL) { 0 48 48
49 0 return -1;
50 }
51  
52 48 data = (unsigned char *)malloc(length);
53 48 ▶ if(data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) { 0 48 48
54 0 return -1;
55 }
56  
57 48 ▶ if(bstream->data) {
Branch: 1 branch
BranchTrueFalseTotal
if(bstream->data) { 0 48 48
58 0 free(bstream->data);
59 }
60 48 bstream->length = length;
61 48 bstream->data = data;
62  
63 48 return 0;
64 }
65  
66 18 static BitStream *BitStream_newFromNum(int bits, unsigned int num)
67 {
68 unsigned int mask;
69 int i;
70 unsigned char *p;
71 BitStream *bstream;
72  
73 18 bstream = BitStream_new();
74 18 ▶ if(bstream == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(bstream == NULL) return NULL; 0 18 18
75  
76 18 ▶ if(BitStream_allocate(bstream, bits)) {
Branch: 1 branch
BranchTrueFalseTotal
if(BitStream_allocate(bstream, bits)) { 0 18 18
77 0 BitStream_free(bstream);
78 0 return NULL;
79 }
80  
81 18 p = bstream->data;
82 18 mask = 1 << (bits - 1);
83 114 ▶ for(i=0; i<bits; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<bits; i++) { 96 18 114
84 96 ▶ if(num & mask) {
Branch: 1 branch
BranchTrueFalseTotal
if(num & mask) { 21 75 96
85 21 *p = 1;
86 } else {
87 75 *p = 0;
88 }
89 96 p++;
90 96 mask = mask >> 1;
91 }
92  
93 18 return bstream;
94 }
95  
96 12 static BitStream *BitStream_newFromBytes(int size, unsigned char *data)
97 {
98 unsigned char mask;
99 int i, j;
100 unsigned char *p;
101 BitStream *bstream;
102  
103 12 bstream = BitStream_new();
104 12 ▶ if(bstream == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(bstream == NULL) return NULL; 0 12 12
105  
106 12 ▶ if(BitStream_allocate(bstream, size * 8)) {
Branch: 1 branch
BranchTrueFalseTotal
if(BitStream_allocate(bstream, size * 8)) { 0 12 12
107 0 BitStream_free(bstream);
108 0 return NULL;
109 }
110  
111 12 p = bstream->data;
112 515 ▶ for(i=0; i<size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<size; i++) { 503 12 515
113 503 mask = 0x80;
114 4527 ▶ for(j=0; j<8; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for(j=0; j<8; j++) { 4024 503 4527
115 4024 ▶ if(data[i] & mask) {
Branch: 1 branch
BranchTrueFalseTotal
if(data[i] & mask) { 1729 2295 4024
116 1729 *p = 1;
117 } else {
118 2295 *p = 0;
119 }
120 4024 p++;
121 4024 mask = mask >> 1;
122 }
123 }
124  
125 12 return bstream;
126 }
127  
128 42 int BitStream_append(BitStream *bstream, BitStream *arg)
129 {
130 unsigned char *data;
131  
132 42 ▶ if(arg == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(arg == NULL) { 0 42 42
133 0 return -1;
134 }
135 42 ▶ if(arg->length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(arg->length == 0) { 0 42 42
136 0 return 0;
137 }
138 42 ▶ if(bstream->length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(bstream->length == 0) { 18 24 42
139 18 ▶ if(BitStream_allocate(bstream, arg->length)) {
Branch: 1 branch
BranchTrueFalseTotal
if(BitStream_allocate(bstream, arg->length)) { 0 18 18
140 0 return -1;
141 }
142 18 memcpy(bstream->data, arg->data, arg->length);
143 18 return 0;
144 }
145  
146 24 data = (unsigned char *)malloc(bstream->length + arg->length);
147 24 ▶ if(data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) { 0 24 24
148 0 return -1;
149 }
150 24 memcpy(data, bstream->data, bstream->length);
151 24 memcpy(data + bstream->length, arg->data, arg->length);
152  
153 24 free(bstream->data);
154 24 bstream->length += arg->length;
155 24 bstream->data = data;
156  
157 24 return 0;
158 }
159  
160 18 int BitStream_appendNum(BitStream *bstream, int bits, unsigned int num)
161 {
162 BitStream *b;
163 int ret;
164  
165 18 ▶ if(bits == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(bits == 0) return 0; 0 18 18
166  
167 18 b = BitStream_newFromNum(bits, num);
168 18 ▶ if(b == NULL) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(b == NULL) return -1; 0 18 18
169  
170 18 ret = BitStream_append(bstream, b);
171 18 BitStream_free(b);
172  
173 18 return ret;
174 }
175  
176 12 int BitStream_appendBytes(BitStream *bstream, int size, unsigned char *data)
177 {
178 BitStream *b;
179 int ret;
180  
181 12 ▶ if(size == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(size == 0) return 0; 0 12 12
182  
183 12 b = BitStream_newFromBytes(size, data);
184 12 ▶ if(b == NULL) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(b == NULL) return -1; 0 12 12
185  
186 12 ret = BitStream_append(bstream, b);
187 12 BitStream_free(b);
188  
189 12 return ret;
190 }
191  
192 6 unsigned char *BitStream_toByte(BitStream *bstream)
193 {
194 int i, j, size, bytes;
195 unsigned char *data, v;
196 unsigned char *p;
197  
198 6 size = BitStream_size(bstream);
199 6 ▶ if(size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(size == 0) { 0 6 6
200 0 return NULL;
201 }
202 6 data = (unsigned char *)malloc((size + 7) / 8);
203 6 ▶ if(data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) { 0 6 6
204 0 return NULL;
205 }
206  
207 6 bytes = size / 8;
208  
209 6 p = bstream->data;
210 521 ▶ for(i=0; i<bytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i<bytes; i++) { 515 6 521
211 515 v = 0;
212 4635 ▶ for(j=0; j<8; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for(j=0; j<8; j++) { 4120 515 4635
213 4120 v = v << 1;
214 4120 v |= *p;
215 4120 p++;
216 }
217 515 data[i] = v;
218 }
219 6 ▶ if(size & 7) {
Branch: 1 branch
BranchTrueFalseTotal
if(size & 7) { 0 6 6
220 0 v = 0;
221 0 ▶ for(j=0; j<(size & 7); j++) {
Branch: 1 branch
BranchTrueFalseTotal
for(j=0; j<(size & 7); j++) { 0 0 0
222 0 v = v << 1;
223 0 v |= *p;
224 0 p++;
225 }
226 0 data[bytes] = v;
227 }
228  
229 6 return data;
230 }
231  
232 48 void BitStream_free(BitStream *bstream)
233 {
234 48 ▶ if(bstream != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(bstream != NULL) { 48 0 48
235 48 free(bstream->data);
236 48 free(bstream);
237 }
238 48 }
« src/QRCode.xs ↑ Summary src/qrspec.c »