/src/openssl32/crypto/asn1/a_i2d_fp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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, const void *x) |
19 | 0 | { |
20 | 0 | BIO *b; |
21 | 0 | int ret; |
22 | |
|
23 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
24 | 0 | ERR_raise(ERR_LIB_ASN1, 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, const void *x) |
35 | 28.4k | { |
36 | 28.4k | char *b; |
37 | 28.4k | unsigned char *p; |
38 | 28.4k | int i, j = 0, n, ret = 1; |
39 | | |
40 | 28.4k | n = i2d(x, NULL); |
41 | 28.4k | if (n <= 0) |
42 | 0 | return 0; |
43 | | |
44 | 28.4k | b = OPENSSL_malloc(n); |
45 | 28.4k | if (b == NULL) |
46 | 0 | return 0; |
47 | | |
48 | 28.4k | p = (unsigned char *)b; |
49 | 28.4k | i2d(x, &p); |
50 | | |
51 | 28.4k | for (;;) { |
52 | 28.4k | i = BIO_write(out, &(b[j]), n); |
53 | 28.4k | if (i == n) |
54 | 28.4k | break; |
55 | 0 | if (i <= 0) { |
56 | 0 | ret = 0; |
57 | 0 | break; |
58 | 0 | } |
59 | 0 | j += i; |
60 | 0 | n -= i; |
61 | 0 | } |
62 | 28.4k | OPENSSL_free(b); |
63 | 28.4k | return ret; |
64 | 28.4k | } |
65 | | |
66 | | #endif |
67 | | |
68 | | #ifndef OPENSSL_NO_STDIO |
69 | | int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, const void *x) |
70 | 0 | { |
71 | 0 | BIO *b; |
72 | 0 | int ret; |
73 | |
|
74 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
75 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB); |
76 | 0 | return 0; |
77 | 0 | } |
78 | 0 | BIO_set_fp(b, out, BIO_NOCLOSE); |
79 | 0 | ret = ASN1_item_i2d_bio(it, b, x); |
80 | 0 | BIO_free(b); |
81 | 0 | return ret; |
82 | 0 | } |
83 | | #endif |
84 | | |
85 | | int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, const void *x) |
86 | 3.24k | { |
87 | 3.24k | unsigned char *b = NULL; |
88 | 3.24k | int i, j = 0, n, ret = 1; |
89 | | |
90 | 3.24k | n = ASN1_item_i2d(x, &b, it); |
91 | 3.24k | if (b == NULL) { |
92 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | 3.24k | for (;;) { |
97 | 3.24k | i = BIO_write(out, &(b[j]), n); |
98 | 3.24k | if (i == n) |
99 | 3.24k | break; |
100 | 0 | if (i <= 0) { |
101 | 0 | ret = 0; |
102 | 0 | break; |
103 | 0 | } |
104 | 0 | j += i; |
105 | 0 | n -= i; |
106 | 0 | } |
107 | 3.24k | OPENSSL_free(b); |
108 | 3.24k | return ret; |
109 | 3.24k | } |
110 | | |
111 | | BIO *ASN1_item_i2d_mem_bio(const ASN1_ITEM *it, const ASN1_VALUE *val) |
112 | 0 | { |
113 | 0 | BIO *res; |
114 | |
|
115 | 0 | if (it == NULL || val == NULL) { |
116 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER); |
117 | 0 | return NULL; |
118 | 0 | } |
119 | | |
120 | 0 | if ((res = BIO_new(BIO_s_mem())) == NULL) |
121 | 0 | return NULL; |
122 | 0 | if (ASN1_item_i2d_bio(it, res, val) <= 0) { |
123 | 0 | BIO_free(res); |
124 | 0 | res = NULL; |
125 | 0 | } |
126 | 0 | return res; |
127 | 0 | } |