summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/x_long.c
diff options
context:
space:
mode:
authorjsing <>2014-04-18 11:20:32 +0000
committerjsing <>2014-04-18 11:20:32 +0000
commit6d8ab3f51c14ff4e0ce4f0aaafede903671056b3 (patch)
tree80f89d5dfdd4e6eef26aea54fe2399d237045354 /src/lib/libcrypto/asn1/x_long.c
parent3a5e199af72e13c8924435aeb9126166854a7c00 (diff)
downloadopenbsd-6d8ab3f51c14ff4e0ce4f0aaafede903671056b3.tar.gz
openbsd-6d8ab3f51c14ff4e0ce4f0aaafede903671056b3.tar.bz2
openbsd-6d8ab3f51c14ff4e0ce4f0aaafede903671056b3.zip
More KNF.
Diffstat (limited to 'src/lib/libcrypto/asn1/x_long.c')
-rw-r--r--src/lib/libcrypto/asn1/x_long.c73
1 files changed, 45 insertions, 28 deletions
diff --git a/src/lib/libcrypto/asn1/x_long.c b/src/lib/libcrypto/asn1/x_long.c
index 1417284d3c..81dcc4fc29 100644
--- a/src/lib/libcrypto/asn1/x_long.c
+++ b/src/lib/libcrypto/asn1/x_long.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -84,25 +84,29 @@ static ASN1_PRIMITIVE_FUNCS long_pf = {
84}; 84};
85 85
86ASN1_ITEM_start(LONG) 86ASN1_ITEM_start(LONG)
87 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG" 87ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG"
88ASN1_ITEM_end(LONG) 88ASN1_ITEM_end(LONG)
89 89
90ASN1_ITEM_start(ZLONG) 90ASN1_ITEM_start(ZLONG)
91 ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG" 91ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG"
92ASN1_ITEM_end(ZLONG) 92ASN1_ITEM_end(ZLONG)
93 93
94static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 94static int
95long_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
95{ 96{
96 *(long *)pval = it->size; 97 *(long *)pval = it->size;
97 return 1; 98 return 1;
98} 99}
99 100
100static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 101static void
102long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
101{ 103{
102 *(long *)pval = it->size; 104 *(long *)pval = it->size;
103} 105}
104 106
105static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it) 107static int
108long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
109 const ASN1_ITEM *it)
106{ 110{
107 long ltmp; 111 long ltmp;
108 unsigned long utmp; 112 unsigned long utmp;
@@ -113,58 +117,70 @@ static int long_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const A
113 /* use memcpy, because we may not be long aligned */ 117 /* use memcpy, because we may not be long aligned */
114 memcpy(&ltmp, cp, sizeof(long)); 118 memcpy(&ltmp, cp, sizeof(long));
115 119
116 if(ltmp == it->size) return -1; 120 if (ltmp == it->size)
121 return -1;
117 /* Convert the long to positive: we subtract one if negative so 122 /* Convert the long to positive: we subtract one if negative so
118 * we can cleanly handle the padding if only the MSB of the leading 123 * we can cleanly handle the padding if only the MSB of the leading
119 * octet is set. 124 * octet is set.
120 */ 125 */
121 if(ltmp < 0) utmp = -ltmp - 1; 126 if (ltmp < 0)
122 else utmp = ltmp; 127 utmp = -ltmp - 1;
128 else
129 utmp = ltmp;
123 clen = BN_num_bits_word(utmp); 130 clen = BN_num_bits_word(utmp);
124 /* If MSB of leading octet set we need to pad */ 131 /* If MSB of leading octet set we need to pad */
125 if(!(clen & 0x7)) pad = 1; 132 if (!(clen & 0x7))
126 else pad = 0; 133 pad = 1;
134 else
135 pad = 0;
127 136
128 /* Convert number of bits to number of octets */ 137 /* Convert number of bits to number of octets */
129 clen = (clen + 7) >> 3; 138 clen = (clen + 7) >> 3;
130 139
131 if(cont) { 140 if (cont) {
132 if(pad) *cont++ = (ltmp < 0) ? 0xff : 0; 141 if (pad)
133 for(i = clen - 1; i >= 0; i--) { 142 *cont++ = (ltmp < 0) ? 0xff : 0;
143 for (i = clen - 1; i >= 0; i--) {
134 cont[i] = (unsigned char)(utmp & 0xff); 144 cont[i] = (unsigned char)(utmp & 0xff);
135 if(ltmp < 0) cont[i] ^= 0xff; 145 if (ltmp < 0)
146 cont[i] ^= 0xff;
136 utmp >>= 8; 147 utmp >>= 8;
137 } 148 }
138 } 149 }
139 return clen + pad; 150 return clen + pad;
140} 151}
141 152
142static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 153static int
143 int utype, char *free_cont, const ASN1_ITEM *it) 154long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype,
155 char *free_cont, const ASN1_ITEM *it)
144{ 156{
145 int neg, i; 157 int neg, i;
146 long ltmp; 158 long ltmp;
147 unsigned long utmp = 0; 159 unsigned long utmp = 0;
148 char *cp = (char *)pval; 160 char *cp = (char *)pval;
149 if(len > (int)sizeof(long)) { 161 if (len > (int)sizeof(long)) {
150 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); 162 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
151 return 0; 163 return 0;
152 } 164 }
153 /* Is it negative? */ 165 /* Is it negative? */
154 if(len && (cont[0] & 0x80)) neg = 1; 166 if (len && (cont[0] & 0x80))
155 else neg = 0; 167 neg = 1;
168 else
169 neg = 0;
156 utmp = 0; 170 utmp = 0;
157 for(i = 0; i < len; i++) { 171 for (i = 0; i < len; i++) {
158 utmp <<= 8; 172 utmp <<= 8;
159 if(neg) utmp |= cont[i] ^ 0xff; 173 if (neg)
160 else utmp |= cont[i]; 174 utmp |= cont[i] ^ 0xff;
175 else
176 utmp |= cont[i];
161 } 177 }
162 ltmp = (long)utmp; 178 ltmp = (long)utmp;
163 if(neg) { 179 if (neg) {
164 ltmp++; 180 ltmp++;
165 ltmp = -ltmp; 181 ltmp = -ltmp;
166 } 182 }
167 if(ltmp == it->size) { 183 if (ltmp == it->size) {
168 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG); 184 ASN1err(ASN1_F_LONG_C2I, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
169 return 0; 185 return 0;
170 } 186 }
@@ -172,8 +188,9 @@ static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
172 return 1; 188 return 1;
173} 189}
174 190
175static int long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, 191static int
176 int indent, const ASN1_PCTX *pctx) 192long_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent,
193 const ASN1_PCTX *pctx)
177{ 194{
178 return BIO_printf(out, "%ld\n", *(long *)pval); 195 return BIO_printf(out, "%ld\n", *(long *)pval);
179} 196}