OpenCoverage

a_bitstr.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/asn1/a_bitstr.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.-
3 *-
4 * Licensed under the OpenSSL license (the "License"). You may not use-
5 * this file except in compliance with the License. You can obtain a copy-
6 * in the file LICENSE in the source distribution or at-
7 * https://www.openssl.org/source/license.html-
8 */-
9-
10#include <limits.h>-
11#include <stdio.h>-
12#include "internal/cryptlib.h"-
13#include <openssl/asn1.h>-
14#include "asn1_locl.h"-
15-
16int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)-
17{-
18 return ASN1_STRING_set(x, d, len);
executed 21 times by 1 test: return ASN1_STRING_set(x, d, len);
Executed by:
  • libcrypto.so.1.1
21
19}-
20-
21int i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)-
22{-
23 int ret, j, bits, len;-
24 unsigned char *p, *d;-
25-
26 if (a == NULL)
a == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 309577 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-309577
27 return 0;
never executed: return 0;
0
28-
29 len = a->length;-
30-
31 if (len > 0) {
len > 0Description
TRUEevaluated 176496 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 133081 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
133081-176496
32 if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
a->flags & 0x08Description
TRUEevaluated 176466 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 30 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
30-176466
33 bits = (int)a->flags & 0x07;-
34 } else {
executed 176466 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
176466
35 for (; len > 0; len--) {
len > 0Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-30
36 if (a->data[len - 1])
a->data[len - 1]Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-30
37 break;
executed 30 times by 1 test: break;
Executed by:
  • libcrypto.so.1.1
30
38 }
never executed: end of block
0
39 j = a->data[len - 1];-
40 if (j & 0x01)
j & 0x01Description
TRUEnever evaluated
FALSEevaluated 30 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-30
41 bits = 0;
never executed: bits = 0;
0
42 else if (j & 0x02)
j & 0x02Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 18 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
12-18
43 bits = 1;
executed 12 times by 1 test: bits = 1;
Executed by:
  • libcrypto.so.1.1
12
44 else if (j & 0x04)
j & 0x04Description
TRUEnever evaluated
FALSEevaluated 18 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-18
45 bits = 2;
never executed: bits = 2;
0
46 else if (j & 0x08)
j & 0x08Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
6-12
47 bits = 3;
executed 6 times by 1 test: bits = 3;
Executed by:
  • libcrypto.so.1.1
6
48 else if (j & 0x10)
j & 0x10Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-12
49 bits = 4;
never executed: bits = 4;
0
50 else if (j & 0x20)
j & 0x20Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 6 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
6
51 bits = 5;
executed 6 times by 1 test: bits = 5;
Executed by:
  • libcrypto.so.1.1
6
52 else if (j & 0x40)
j & 0x40Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-6
53 bits = 6;
executed 6 times by 1 test: bits = 6;
Executed by:
  • libcrypto.so.1.1
6
54 else if (j & 0x80)
j & 0x80Description
TRUEnever evaluated
FALSEnever evaluated
0
55 bits = 7;
never executed: bits = 7;
0
56 else-
57 bits = 0; /* should not happen */
never executed: bits = 0;
0
58 }-
59 } else-
60 bits = 0;
executed 133081 times by 1 test: bits = 0;
Executed by:
  • libcrypto.so.1.1
133081
61-
62 ret = 1 + len;-
63 if (pp == NULL)
pp == ((void *)0)Description
TRUEevaluated 235356 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 74221 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
74221-235356
64 return ret;
executed 235356 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
235356
65-
66 p = *pp;-
67-
68 *(p++) = (unsigned char)bits;-
69 d = a->data;-
70 if (len > 0) {
len > 0Description
TRUEevaluated 42006 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 32215 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
32215-42006
71 memcpy(p, d, len);-
72 p += len;-
73 p[-1] &= (0xff << bits);-
74 }
executed 42006 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
42006
75 *pp = p;-
76 return ret;
executed 74221 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
74221
77}-
78-
79ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,-
80 const unsigned char **pp, long len)-
81{-
82 ASN1_BIT_STRING *ret = NULL;-
83 const unsigned char *p;-
84 unsigned char *s;-
85 int i;-
86-
87 if (len < 1) {
len < 1Description
TRUEevaluated 1244 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 146638 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1244-146638
88 i = ASN1_R_STRING_TOO_SHORT;-
89 goto err;
executed 1244 times by 1 test: goto err;
Executed by:
  • libcrypto.so.1.1
1244
90 }-
91-
92 if (len > INT_MAX) {
len > 0x7fffffffDescription
TRUEnever evaluated
FALSEevaluated 146638 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-146638
93 i = ASN1_R_STRING_TOO_LONG;-
94 goto err;
never executed: goto err;
0
95 }-
96-
97 if ((a == NULL) || ((*a) == NULL)) {
(a == ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 146638 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
((*a) == ((void *)0) )Description
TRUEevaluated 22982 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 123656 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-146638
98 if ((ret = ASN1_BIT_STRING_new()) == NULL)
(ret = ASN1_BI...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 22982 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-22982
99 return NULL;
never executed: return ((void *)0) ;
0
100 } else
executed 22982 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
22982
101 ret = (*a);
executed 123656 times by 1 test: ret = (*a);
Executed by:
  • libcrypto.so.1.1
123656
102-
103 p = *pp;-
104 i = *(p++);-
105 if (i > 7) {
i > 7Description
TRUEevaluated 626 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 146012 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
626-146012
106 i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;-
107 goto err;
executed 626 times by 1 test: goto err;
Executed by:
  • libcrypto.so.1.1
626
108 }-
109 /*-
110 * We do this to preserve the settings. If we modify the settings, via-
111 * the _set_bit function, we will recalculate on output-
112 */-
113 ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */-
114 ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */-
115-
116 if (len-- > 1) { /* using one because of the bits left byte */
len-- > 1Description
TRUEevaluated 92146 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 53866 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
53866-92146
117 s = OPENSSL_malloc((int)len);-
118 if (s == NULL) {
s == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 92146 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-92146
119 i = ERR_R_MALLOC_FAILURE;-
120 goto err;
never executed: goto err;
0
121 }-
122 memcpy(s, p, (int)len);-
123 s[len - 1] &= (0xff << i);-
124 p += len;-
125 } else
executed 92146 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
92146
126 s = NULL;
executed 53866 times by 1 test: s = ((void *)0) ;
Executed by:
  • libcrypto.so.1.1
53866
127-
128 ret->length = (int)len;-
129 OPENSSL_free(ret->data);-
130 ret->data = s;-
131 ret->type = V_ASN1_BIT_STRING;-
132 if (a != NULL)
a != ((void *)0)Description
TRUEevaluated 146012 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-146012
133 (*a) = ret;
executed 146012 times by 1 test: (*a) = ret;
Executed by:
  • libcrypto.so.1.1
146012
134 *pp = p;-
135 return ret;
executed 146012 times by 1 test: return ret;
Executed by:
  • libcrypto.so.1.1
146012
136 err:-
137 ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);-
138 if ((a == NULL) || (*a != ret))
(a == ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 1870 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(*a != ret)Description
TRUEevaluated 807 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1063 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-1870
139 ASN1_BIT_STRING_free(ret);
executed 807 times by 1 test: ASN1_BIT_STRING_free(ret);
Executed by:
  • libcrypto.so.1.1
807
140 return NULL;
executed 1870 times by 1 test: return ((void *)0) ;
Executed by:
  • libcrypto.so.1.1
1870
141}-
142-
143/*-
144 * These next 2 functions from Goetz Babin-Ebell.-
145 */-
146int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)-
147{-
148 int w, v, iv;-
149 unsigned char *c;-
150-
151 w = n / 8;-
152 v = 1 << (7 - (n & 0x07));-
153 iv = ~v;-
154 if (!value)
!valueDescription
TRUEnever evaluated
FALSEevaluated 24 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-24
155 v = 0;
never executed: v = 0;
0
156-
157 if (a == NULL)
a == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 24 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-24
158 return 0;
never executed: return 0;
0
159-
160 a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */-
161-
162 if ((a->length < (w + 1)) || (a->data == NULL)) {
(a->length < (w + 1))Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 14 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(a->data == ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 14 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-14
163 if (!value)
!valueDescription
TRUEnever evaluated
FALSEevaluated 10 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-10
164 return 1; /* Don't need to set */
never executed: return 1;
0
165 c = OPENSSL_clear_realloc(a->data, a->length, w + 1);-
166 if (c == NULL) {
c == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 10 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-10
167 ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);-
168 return 0;
never executed: return 0;
0
169 }-
170 if (w + 1 - a->length > 0)
w + 1 - a->length > 0Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-10
171 memset(c + a->length, 0, w + 1 - a->length);
executed 10 times by 1 test: memset(c + a->length, 0, w + 1 - a->length);
Executed by:
  • libcrypto.so.1.1
10
172 a->data = c;-
173 a->length = w + 1;-
174 }
executed 10 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
10
175 a->data[w] = ((a->data[w]) & iv) | v;-
176 while ((a->length > 0) && (a->data[a->length - 1] == 0))
(a->length > 0)Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
(a->data[a->length - 1] == 0)Description
TRUEnever evaluated
FALSEevaluated 24 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-24
177 a->length--;
never executed: a->length--;
0
178 return 1;
executed 24 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
24
179}-
180-
181int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)-
182{-
183 int w, v;-
184-
185 w = n / 8;-
186 v = 1 << (7 - (n & 0x07));-
187 if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
(a == ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 47413 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(a->length < (w + 1))Description
TRUEevaluated 5893 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 41520 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
(a->data == ((void *)0) )Description
TRUEnever evaluated
FALSEevaluated 41520 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-47413
188 return 0;
executed 5893 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
5893
189 return ((a->data[w] & v) != 0);
executed 41520 times by 1 test: return ((a->data[w] & v) != 0);
Executed by:
  • libcrypto.so.1.1
41520
190}-
191-
192/*-
193 * Checks if the given bit string contains only bits specified by-
194 * the flags vector. Returns 0 if there is at least one bit set in 'a'-
195 * which is not specified in 'flags', 1 otherwise.-
196 * 'len' is the length of 'flags'.-
197 */-
198int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,-
199 const unsigned char *flags, int flags_len)-
200{-
201 int i, ok;-
202 /* Check if there is one bit set at all. */-
203 if (!a || !a->data)
!aDescription
TRUEnever evaluated
FALSEnever evaluated
!a->dataDescription
TRUEnever evaluated
FALSEnever evaluated
0
204 return 1;
never executed: return 1;
0
205-
206 /*-
207 * Check each byte of the internal representation of the bit string.-
208 */-
209 ok = 1;-
210 for (i = 0; i < a->length && ok; ++i) {
i < a->lengthDescription
TRUEnever evaluated
FALSEnever evaluated
okDescription
TRUEnever evaluated
FALSEnever evaluated
0
211 unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
i < flags_lenDescription
TRUEnever evaluated
FALSEnever evaluated
0
212 /* We are done if there is an unneeded bit set. */-
213 ok = (a->data[i] & mask) == 0;-
214 }
never executed: end of block
0
215 return ok;
never executed: return ok;
0
216}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2