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 <openssl/asn1.h>
11
12 #include <openssl/bio.h>
13
i2a_ASN1_INTEGER(BIO * bp,const ASN1_INTEGER * a)14 int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) {
15 int i, n = 0;
16 static const char *h = "0123456789ABCDEF";
17 char buf[2];
18
19 if (a == NULL) {
20 return 0;
21 }
22
23 if (a->type & V_ASN1_NEG) {
24 if (BIO_write(bp, "-", 1) != 1) {
25 goto err;
26 }
27 n = 1;
28 }
29
30 if (a->length == 0) {
31 if (BIO_write(bp, "00", 2) != 2) {
32 goto err;
33 }
34 n += 2;
35 } else {
36 for (i = 0; i < a->length; i++) {
37 if ((i != 0) && (i % 35 == 0)) {
38 if (BIO_write(bp, "\\\n", 2) != 2) {
39 goto err;
40 }
41 n += 2;
42 }
43 buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
44 buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
45 if (BIO_write(bp, buf, 2) != 2) {
46 goto err;
47 }
48 n += 2;
49 }
50 }
51 return n;
52 err:
53 return -1;
54 }
55
i2a_ASN1_ENUMERATED(BIO * bp,const ASN1_ENUMERATED * a)56 int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a) {
57 return i2a_ASN1_INTEGER(bp, a);
58 }
59