source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/bio/bf_nbio.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 4.0 KB
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 <errno.h>
12#include "bio_lcl.h"
13#include "internal/cryptlib.h"
14#include <openssl/rand.h>
15
16/*
17 * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
18 */
19
20static int nbiof_write(BIO *h, const char *buf, int num);
21static int nbiof_read(BIO *h, char *buf, int size);
22static int nbiof_puts(BIO *h, const char *str);
23static int nbiof_gets(BIO *h, char *str, int size);
24static long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
25static int nbiof_new(BIO *h);
26static int nbiof_free(BIO *data);
27static long nbiof_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
28typedef struct nbio_test_st {
29 /* only set if we sent a 'should retry' error */
30 int lrn;
31 int lwn;
32} NBIO_TEST;
33
34static const BIO_METHOD methods_nbiof = {
35 BIO_TYPE_NBIO_TEST,
36 "non-blocking IO test filter",
37 nbiof_write,
38 nbiof_read,
39 nbiof_puts,
40 nbiof_gets,
41 nbiof_ctrl,
42 nbiof_new,
43 nbiof_free,
44 nbiof_callback_ctrl,
45};
46
47const BIO_METHOD *BIO_f_nbio_test(void)
48{
49 return (&methods_nbiof);
50}
51
52static int nbiof_new(BIO *bi)
53{
54 NBIO_TEST *nt;
55
56 if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
57 return (0);
58 nt->lrn = -1;
59 nt->lwn = -1;
60 bi->ptr = (char *)nt;
61 bi->init = 1;
62 return (1);
63}
64
65static int nbiof_free(BIO *a)
66{
67 if (a == NULL)
68 return (0);
69 OPENSSL_free(a->ptr);
70 a->ptr = NULL;
71 a->init = 0;
72 a->flags = 0;
73 return (1);
74}
75
76static int nbiof_read(BIO *b, char *out, int outl)
77{
78 int ret = 0;
79 int num;
80 unsigned char n;
81
82 if (out == NULL)
83 return (0);
84 if (b->next_bio == NULL)
85 return (0);
86
87 BIO_clear_retry_flags(b);
88 if (RAND_bytes(&n, 1) <= 0)
89 return -1;
90 num = (n & 0x07);
91
92 if (outl > num)
93 outl = num;
94
95 if (num == 0) {
96 ret = -1;
97 BIO_set_retry_read(b);
98 } else {
99 ret = BIO_read(b->next_bio, out, outl);
100 if (ret < 0)
101 BIO_copy_next_retry(b);
102 }
103 return (ret);
104}
105
106static int nbiof_write(BIO *b, const char *in, int inl)
107{
108 NBIO_TEST *nt;
109 int ret = 0;
110 int num;
111 unsigned char n;
112
113 if ((in == NULL) || (inl <= 0))
114 return (0);
115 if (b->next_bio == NULL)
116 return (0);
117 nt = (NBIO_TEST *)b->ptr;
118
119 BIO_clear_retry_flags(b);
120
121 if (nt->lwn > 0) {
122 num = nt->lwn;
123 nt->lwn = 0;
124 } else {
125 if (RAND_bytes(&n, 1) <= 0)
126 return -1;
127 num = (n & 7);
128 }
129
130 if (inl > num)
131 inl = num;
132
133 if (num == 0) {
134 ret = -1;
135 BIO_set_retry_write(b);
136 } else {
137 ret = BIO_write(b->next_bio, in, inl);
138 if (ret < 0) {
139 BIO_copy_next_retry(b);
140 nt->lwn = inl;
141 }
142 }
143 return (ret);
144}
145
146static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
147{
148 long ret;
149
150 if (b->next_bio == NULL)
151 return (0);
152 switch (cmd) {
153 case BIO_C_DO_STATE_MACHINE:
154 BIO_clear_retry_flags(b);
155 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
156 BIO_copy_next_retry(b);
157 break;
158 case BIO_CTRL_DUP:
159 ret = 0L;
160 break;
161 default:
162 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
163 break;
164 }
165 return (ret);
166}
167
168static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
169{
170 long ret = 1;
171
172 if (b->next_bio == NULL)
173 return (0);
174 switch (cmd) {
175 default:
176 ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
177 break;
178 }
179 return (ret);
180}
181
182static int nbiof_gets(BIO *bp, char *buf, int size)
183{
184 if (bp->next_bio == NULL)
185 return (0);
186 return (BIO_gets(bp->next_bio, buf, size));
187}
188
189static int nbiof_puts(BIO *bp, const char *str)
190{
191 if (bp->next_bio == NULL)
192 return (0);
193 return (BIO_puts(bp->next_bio, str));
194}
Note: See TracBrowser for help on using the repository browser.