]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_i2d_fp.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / asn1 / a_i2d_fp.c
CommitLineData
2039c421
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822 12#include <openssl/buffer.h>
9d6b1ce6 13#include <openssl/asn1.h>
d02b48c6 14
4e1209eb
DSH
15#ifndef NO_OLD_ASN1
16
0f113f3e 17# ifndef OPENSSL_NO_STDIO
9fdcc21f 18int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, const void *x)
0f113f3e
MC
19{
20 BIO *b;
21 int ret;
d02b48c6 22
0f113f3e 23 if ((b = BIO_new(BIO_s_file())) == NULL) {
9311d0c4 24 ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
26a7d938 25 return 0;
0f113f3e
MC
26 }
27 BIO_set_fp(b, out, BIO_NOCLOSE);
28 ret = ASN1_i2d_bio(i2d, b, x);
29 BIO_free(b);
26a7d938 30 return ret;
0f113f3e
MC
31}
32# endif
d02b48c6 33
9fdcc21f 34int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, const void *x)
0f113f3e
MC
35{
36 char *b;
37 unsigned char *p;
38 int i, j = 0, n, ret = 1;
39
40 n = i2d(x, NULL);
a6f622bc
PK
41 if (n <= 0)
42 return 0;
43
b196e7d9 44 b = OPENSSL_malloc(n);
0f113f3e 45 if (b == NULL) {
9311d0c4 46 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
26a7d938 47 return 0;
0f113f3e 48 }
d02b48c6 49
0f113f3e
MC
50 p = (unsigned char *)b;
51 i2d(x, &p);
d02b48c6 52
0f113f3e
MC
53 for (;;) {
54 i = BIO_write(out, &(b[j]), n);
55 if (i == n)
56 break;
57 if (i <= 0) {
58 ret = 0;
59 break;
60 }
61 j += i;
62 n -= i;
63 }
64 OPENSSL_free(b);
26a7d938 65 return ret;
0f113f3e 66}
4e1209eb
DSH
67
68#endif
69
4b618848 70#ifndef OPENSSL_NO_STDIO
9fdcc21f 71int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, const void *x)
0f113f3e
MC
72{
73 BIO *b;
74 int ret;
4e1209eb 75
0f113f3e 76 if ((b = BIO_new(BIO_s_file())) == NULL) {
9311d0c4 77 ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
26a7d938 78 return 0;
0f113f3e
MC
79 }
80 BIO_set_fp(b, out, BIO_NOCLOSE);
81 ret = ASN1_item_i2d_bio(it, b, x);
82 BIO_free(b);
26a7d938 83 return ret;
0f113f3e 84}
4e1209eb
DSH
85#endif
86
9fdcc21f 87int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, const void *x)
0f113f3e
MC
88{
89 unsigned char *b = NULL;
90 int i, j = 0, n, ret = 1;
4e1209eb 91
0f113f3e
MC
92 n = ASN1_item_i2d(x, &b, it);
93 if (b == NULL) {
9311d0c4 94 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
26a7d938 95 return 0;
0f113f3e 96 }
4e1209eb 97
0f113f3e
MC
98 for (;;) {
99 i = BIO_write(out, &(b[j]), n);
100 if (i == n)
101 break;
102 if (i <= 0) {
103 ret = 0;
104 break;
105 }
106 j += i;
107 n -= i;
108 }
109 OPENSSL_free(b);
26a7d938 110 return ret;
0f113f3e 111}