/src/openssl/crypto/asn1/a_i2d_fp.c
Line | Count | Source (jump to first uncovered line) |
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 <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/buffer.h> |
13 | | #include <openssl/asn1.h> |
14 | | |
15 | | #ifndef NO_OLD_ASN1 |
16 | | |
17 | | # ifndef OPENSSL_NO_STDIO |
18 | | int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, void *x) |
19 | 0 | { |
20 | 0 | BIO *b; |
21 | 0 | int ret; |
22 | 0 |
|
23 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
24 | 0 | ASN1err(ASN1_F_ASN1_I2D_FP, ERR_R_BUF_LIB); |
25 | 0 | return 0; |
26 | 0 | } |
27 | 0 | BIO_set_fp(b, out, BIO_NOCLOSE); |
28 | 0 | ret = ASN1_i2d_bio(i2d, b, x); |
29 | 0 | BIO_free(b); |
30 | 0 | return ret; |
31 | 0 | } |
32 | | # endif |
33 | | |
34 | | int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, unsigned char *x) |
35 | 0 | { |
36 | 0 | char *b; |
37 | 0 | unsigned char *p; |
38 | 0 | int i, j = 0, n, ret = 1; |
39 | 0 |
|
40 | 0 | n = i2d(x, NULL); |
41 | 0 | if (n <= 0) |
42 | 0 | return 0; |
43 | 0 | |
44 | 0 | b = OPENSSL_malloc(n); |
45 | 0 | if (b == NULL) { |
46 | 0 | ASN1err(ASN1_F_ASN1_I2D_BIO, ERR_R_MALLOC_FAILURE); |
47 | 0 | return 0; |
48 | 0 | } |
49 | 0 |
|
50 | 0 | p = (unsigned char *)b; |
51 | 0 | i2d(x, &p); |
52 | 0 |
|
53 | 0 | for (;;) { |
54 | 0 | i = BIO_write(out, &(b[j]), n); |
55 | 0 | if (i == n) |
56 | 0 | break; |
57 | 0 | if (i <= 0) { |
58 | 0 | ret = 0; |
59 | 0 | break; |
60 | 0 | } |
61 | 0 | j += i; |
62 | 0 | n -= i; |
63 | 0 | } |
64 | 0 | OPENSSL_free(b); |
65 | 0 | return ret; |
66 | 0 | } |
67 | | |
68 | | #endif |
69 | | |
70 | | #ifndef OPENSSL_NO_STDIO |
71 | | int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x) |
72 | 0 | { |
73 | 0 | BIO *b; |
74 | 0 | int ret; |
75 | 0 |
|
76 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
77 | 0 | ASN1err(ASN1_F_ASN1_ITEM_I2D_FP, ERR_R_BUF_LIB); |
78 | 0 | return 0; |
79 | 0 | } |
80 | 0 | BIO_set_fp(b, out, BIO_NOCLOSE); |
81 | 0 | ret = ASN1_item_i2d_bio(it, b, x); |
82 | 0 | BIO_free(b); |
83 | 0 | return ret; |
84 | 0 | } |
85 | | #endif |
86 | | |
87 | | int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x) |
88 | 0 | { |
89 | 0 | unsigned char *b = NULL; |
90 | 0 | int i, j = 0, n, ret = 1; |
91 | 0 |
|
92 | 0 | n = ASN1_item_i2d(x, &b, it); |
93 | 0 | if (b == NULL) { |
94 | 0 | ASN1err(ASN1_F_ASN1_ITEM_I2D_BIO, ERR_R_MALLOC_FAILURE); |
95 | 0 | return 0; |
96 | 0 | } |
97 | 0 |
|
98 | 0 | for (;;) { |
99 | 0 | i = BIO_write(out, &(b[j]), n); |
100 | 0 | if (i == n) |
101 | 0 | break; |
102 | 0 | if (i <= 0) { |
103 | 0 | ret = 0; |
104 | 0 | break; |
105 | 0 | } |
106 | 0 | j += i; |
107 | 0 | n -= i; |
108 | 0 | } |
109 | 0 | OPENSSL_free(b); |
110 | 0 | return ret; |
111 | 0 | } |