cc_bignum.c

filter: Statementstmt 91.3% 126 / 138 Branchbran 75.8% 47 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.5% 173 / 200 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« swap.h ↑ Summary decode.c »
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include <stdint.h>
5 #include "cc_bignum.h"
6  
7 /* I needed a bignum library but couldn't use GMP because I can't assume it's installed everywhere.
8 Since the amount of things I need to do is really small, I rolled my own. */
9  
10 4 void cc_bignum_init_bytes(struct cc_bignum *bn, unsigned char *bytes, size_t length)
11 {
12 4 ▶ if (length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length > 0) { 4 0 4
13 4 bn->number = malloc(length);
14 4 bn->length = length;
15 4 memcpy(bn->number, bytes, length);
16 4 ▶ if (bn->number[length-1] & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (bn->number[length-1] & 0x80) { 1 3 4
17 int i;
18 12 ▶ for (i = 0; i < bn->length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < bn->length; i++) { 11 1 12
19 11 bn->number[i] = ~bn->number[i];
20 }
21 1 bn->is_negative = 1;
22 1 cc_bignum_add(bn, 1);
23 } else {
24 3 bn->is_negative = 0;
25 }
26 } else {
27 0 bn->number = calloc(1, 1);
28 0 bn->length = 1;
29 0 bn->is_negative = 0;
30 }
31 4 }
32  
33 5 void cc_bignum_init_string(struct cc_bignum *bn, char *string, size_t length)
34 {
35 5 size_t pos = 0;
36  
37 5 bn->number = calloc(1, 1);
38 5 bn->length = 1;
39 5 bn->is_negative = 0;
40  
41 5 ▶ if (string[pos] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (string[pos] == '-') { 1 4 5
42 1 pos++;
43 1 bn->is_negative = 1;
44 4 ▶ } else if (string[pos] == '+') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (string[pos] == '+') { 0 4 4
45 0 pos++;
46 }
47  
48 224 ▶ for (; pos < length; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; pos < length; pos++) { 219 5 224
49 219 cc_bignum_mul(bn, 10);
50 219 cc_bignum_add(bn, string[pos]-'0');
51 }
52 5 }
53  
54 170 void cc_bignum_destroy(struct cc_bignum *bn)
55 {
56 170 ▶ if (bn->number)
Branch: 1 branch
BranchTrueFalseTotal
if (bn->number) 170 0 170
57 170 free(bn->number);
58 170 bn->number = NULL;
59 170 }
60  
61 9 void cc_bignum_copy(struct cc_bignum *out, struct cc_bignum *in)
62 {
63 9 out->length = in->length;
64 9 out->number = calloc(1, in->length);
65 9 memcpy(out->number, in->number, in->length);
66 9 out->is_negative = in->is_negative;
67 9 }
68  
69 152 void cc_bignum_move(struct cc_bignum *out, struct cc_bignum *in)
70 {
71 152 out->length = in->length;
72 152 out->number = in->number;
73 152 out->is_negative = in->is_negative;
74 152 in->number = NULL;
75 152 in->length = 0;
76 152 in->is_negative = 0;
77 152 }
78  
79 /* https://stackoverflow.com/a/10525503 */
80 152 uint32_t cc_bignum_divide_8bit(struct cc_bignum *n, uint8_t d, struct cc_bignum *out)
81 {
82 size_t i;
83 uint32_t temp;
84  
85 152 temp = 0;
86 152 out->number = calloc(1, n->length);
87 152 i = n->length;
88 3103 ▶ while (i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (i > 0) { 2951 152 3103
89 2951 i--;
90 2951 temp <<= 8;
91 2951 temp |= n->number[i];
92 2951 out->number[i] = temp / d;
93 2951 temp -= out->number[i] * d;
94 }
95  
96 152 out->length = n->length;
97 152 out->is_negative = n->is_negative;
98  
99 /* Probably not correct when the number is negative. But good enough for our use cases. */
100 152 return temp;
101 }
102  
103 219 void cc_bignum_mul(struct cc_bignum *n, uint8_t mul)
104 {
105 size_t i;
106 uint32_t temp;
107  
108 219 temp = 0;
109 219 i = 0;
110 2576 ▶ while (i < n->length) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n->length) { 2357 219 2576
111 2357 temp += (n->number[i] * mul);
112 2357 n->number[i] = temp % 256;
113 2357 temp >>= 8;
114 2357 i++;
115 }
116 219 ▶ if (temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (temp) { 86 133 219
117 86 ▶ assert(temp < 256);
Branch: 1 branch
BranchTrueFalseTotal
assert(temp < 256); 0 86 86
118 86 n->length++;
119 86 n->number = realloc(n->number, n->length);
120 86 n->number[i] = temp;
121 }
122 219 }
123  
124 221 void cc_bignum_add(struct cc_bignum *n, uint8_t howmuch)
125 {
126 int i;
127 uint8_t carry;
128 221 carry = howmuch;
129 221 ▶ if (!carry)
Branch: 1 branch
BranchTrueFalseTotal
if (!carry) 195 26 221
130 195 return;
131 32 ▶ for (i = 0; i < n->length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->length; i++) { 32 0 32
132 32 ▶ if (n->number[i] < 256-carry) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->number[i] < 256-carry) { 26 6 32
133 26 n->number[i] += carry;
134 26 return;
135 } else {
136 6 n->number[i] += carry;
137 6 carry = 1;
138 }
139 }
140 0 n->number = realloc(n->number, n->length+1);
141 0 n->length++;
142 0 n->number[i] = carry;
143 }
144  
145 160 int cc_bignum_is_zero(struct cc_bignum *n)
146 {
147 int i;
148 160 ▶ if (n->length == 1 && n->number[0] == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (n->length == 1 && n->number[0] == 0) 0 160 160
if (n->length == 1 && n->number[0] == 0) 0 0 0
149 0 return 1;
150 549 ▶ for (i = 0; i < n->length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->length; i++) { 545 4 549
151 545 ▶ if (n->number[i] != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n->number[i] != 0) 156 389 545
152 156 return 0;
153 }
154 4 return 1;
155 }
156  
157 /*
158 void cc_bignum_dump(struct cc_bignum *bn)
159 {
160 int i;
161 printf("BN: ");
162 for (i = 0; i < bn->length; i++) {
163 printf("%.2x ", bn->number[i]);
164 }
165 printf("\n");
166 }
167 */
168  
169 4 void cc_bignum_stringify(struct cc_bignum *bn, char *out, size_t outlen)
170 {
171 struct cc_bignum cur;
172 size_t i, j, tmp_buf_len;
173 char *tmp_buf;
174  
175 4 ▶ if (cc_bignum_is_zero(bn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (cc_bignum_is_zero(bn)) { 0 4 4
176 0 out[0] = '0';
177 0 out[1] = 0;
178 0 ▶ assert(outlen >= 2);
Branch: 1 branch
BranchTrueFalseTotal
assert(outlen >= 2); 0 0 0
179 0 return;
180 }
181  
182 4 tmp_buf_len = bn->length*4 + 2;
183 4 tmp_buf = calloc(1, tmp_buf_len);
184  
185 4 cc_bignum_copy(&cur, bn);
186 4 i = 0;
187  
188 156 ▶ while (!cc_bignum_is_zero(&cur)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!cc_bignum_is_zero(&cur)) { 152 4 156
189 struct cc_bignum new;
190 152 uint8_t remain = cc_bignum_divide_8bit(&cur, 10, &new);
191 152 cc_bignum_destroy(&cur);
192 152 cc_bignum_move(&cur, &new);
193 152 tmp_buf[i++] = '0' + remain;
194 152 ▶ assert(i < tmp_buf_len);
Branch: 1 branch
BranchTrueFalseTotal
assert(i < tmp_buf_len); 0 152 152
195 }
196  
197 4 ▶ if (bn->is_negative)
Branch: 1 branch
BranchTrueFalseTotal
if (bn->is_negative) 1 3 4
198 1 tmp_buf[i++] = '-';
199  
200 4 ▶ assert(i < outlen);
Branch: 1 branch
BranchTrueFalseTotal
assert(i < outlen); 0 4 4
201  
202 157 ▶ for (j = 0; j < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) { 153 4 157
203 153 out[j] = tmp_buf[i-j-1];
204 }
205 4 out[i] = 0;
206  
207 4 free(tmp_buf);
208 4 cc_bignum_destroy(&cur);
209 }
210  
211 5 size_t cc_bignum_byteify(struct cc_bignum *bn, unsigned char *out, size_t outlen)
212 {
213 struct cc_bignum copy;
214 size_t needed_bytes;
215 5 cc_bignum_copy(&copy, bn);
216  
217 5 ▶ assert(copy.length < outlen);
Branch: 1 branch
BranchTrueFalseTotal
assert(copy.length < outlen); 0 5 5
218  
219 5 ▶ if (copy.is_negative) {
Branch: 1 branch
BranchTrueFalseTotal
if (copy.is_negative) { 1 4 5
220 int i;
221 11 ▶ for (i = 0; i < copy.length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < copy.length; i++) { 10 1 11
222 10 copy.number[i] = ~copy.number[i];
223 }
224 1 cc_bignum_add(&copy, 1);
225 1 out[copy.length] = 0xff;
226 } else {
227 4 out[copy.length] = 0;
228 }
229  
230 5 memcpy(out, copy.number, copy.length);
231  
232 5 needed_bytes = copy.length+1;
233 8 ▶ while (needed_bytes > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (needed_bytes > 1) { 8 0 8
234 8 ▶ if (out[needed_bytes-1] == out[copy.length]) {
Branch: 1 branch
BranchTrueFalseTotal
if (out[needed_bytes-1] == out[copy.length]) { 5 3 8
235 5 ▶ if ((out[needed_bytes-1]&0x80) == (out[needed_bytes-2]&0x80)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((out[needed_bytes-1]&0x80) == (out[needed_bytes-2]&0x80)) { 3 2 5
236 3 needed_bytes--;
237 3 continue;
238 }
239 }
240 5 break;
241 }
242  
243 5 cc_bignum_destroy(&copy);
244  
245 5 return needed_bytes;
246 }
« swap.h ↑ Summary decode.c »