1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/asn1.h>
16 
17 #include <openssl/bio.h>
18 
i2a_ASN1_STRING(BIO * bp,const ASN1_STRING * a,int type)19 int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type) {
20   int i, n = 0;
21   static const char *h = "0123456789ABCDEF";
22   char buf[2];
23 
24   if (a == NULL) {
25     return 0;
26   }
27 
28   if (a->length == 0) {
29     if (BIO_write(bp, "0", 1) != 1) {
30       goto err;
31     }
32     n = 1;
33   } else {
34     for (i = 0; i < a->length; i++) {
35       if ((i != 0) && (i % 35 == 0)) {
36         if (BIO_write(bp, "\\\n", 2) != 2) {
37           goto err;
38         }
39         n += 2;
40       }
41       buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
42       buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
43       if (BIO_write(bp, buf, 2) != 2) {
44         goto err;
45       }
46       n += 2;
47     }
48   }
49   return n;
50 err:
51   return -1;
52 }
53