diff options
Diffstat (limited to 'src/lib/libcrypto/bio/bss_mem.c')
-rw-r--r-- | src/lib/libcrypto/bio/bss_mem.c | 130 |
1 files changed, 75 insertions, 55 deletions
diff --git a/src/lib/libcrypto/bio/bss_mem.c b/src/lib/libcrypto/bio/bss_mem.c index 40c4e39f02..28ff7582bf 100644 --- a/src/lib/libcrypto/bio/bss_mem.c +++ b/src/lib/libcrypto/bio/bss_mem.c | |||
@@ -59,26 +59,15 @@ | |||
59 | #include <stdio.h> | 59 | #include <stdio.h> |
60 | #include <errno.h> | 60 | #include <errno.h> |
61 | #include "cryptlib.h" | 61 | #include "cryptlib.h" |
62 | #include "bio.h" | 62 | #include <openssl/bio.h> |
63 | 63 | ||
64 | #ifndef NOPROTO | 64 | static int mem_write(BIO *h, const char *buf, int num); |
65 | static int mem_write(BIO *h,char *buf,int num); | 65 | static int mem_read(BIO *h, char *buf, int size); |
66 | static int mem_read(BIO *h,char *buf,int size); | 66 | static int mem_puts(BIO *h, const char *str); |
67 | static int mem_puts(BIO *h,char *str); | 67 | static int mem_gets(BIO *h, char *str, int size); |
68 | static int mem_gets(BIO *h,char *str,int size); | 68 | static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
69 | static long mem_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
70 | static int mem_new(BIO *h); | 69 | static int mem_new(BIO *h); |
71 | static int mem_free(BIO *data); | 70 | static int mem_free(BIO *data); |
72 | #else | ||
73 | static int mem_write(); | ||
74 | static int mem_read(); | ||
75 | static int mem_puts(); | ||
76 | static int mem_gets(); | ||
77 | static long mem_ctrl(); | ||
78 | static int mem_new(); | ||
79 | static int mem_free(); | ||
80 | #endif | ||
81 | |||
82 | static BIO_METHOD mem_method= | 71 | static BIO_METHOD mem_method= |
83 | { | 72 | { |
84 | BIO_TYPE_MEM, | 73 | BIO_TYPE_MEM, |
@@ -90,15 +79,38 @@ static BIO_METHOD mem_method= | |||
90 | mem_ctrl, | 79 | mem_ctrl, |
91 | mem_new, | 80 | mem_new, |
92 | mem_free, | 81 | mem_free, |
82 | NULL, | ||
93 | }; | 83 | }; |
94 | 84 | ||
95 | BIO_METHOD *BIO_s_mem() | 85 | /* bio->num is used to hold the value to return on 'empty', if it is |
86 | * 0, should_retry is not set */ | ||
87 | |||
88 | BIO_METHOD *BIO_s_mem(void) | ||
96 | { | 89 | { |
97 | return(&mem_method); | 90 | return(&mem_method); |
98 | } | 91 | } |
99 | 92 | ||
100 | static int mem_new(bi) | 93 | BIO *BIO_new_mem_buf(void *buf, int len) |
101 | BIO *bi; | 94 | { |
95 | BIO *ret; | ||
96 | BUF_MEM *b; | ||
97 | if (!buf) { | ||
98 | BIOerr(BIO_F_BIO_NEW_MEM_BUF,BIO_R_NULL_PARAMETER); | ||
99 | return NULL; | ||
100 | } | ||
101 | if(len == -1) len = strlen(buf); | ||
102 | if(!(ret = BIO_new(BIO_s_mem())) ) return NULL; | ||
103 | b = (BUF_MEM *)ret->ptr; | ||
104 | b->data = buf; | ||
105 | b->length = len; | ||
106 | b->max = len; | ||
107 | ret->flags |= BIO_FLAGS_MEM_RDONLY; | ||
108 | /* Since this is static data retrying wont help */ | ||
109 | ret->num = 0; | ||
110 | return ret; | ||
111 | } | ||
112 | |||
113 | static int mem_new(BIO *bi) | ||
102 | { | 114 | { |
103 | BUF_MEM *b; | 115 | BUF_MEM *b; |
104 | 116 | ||
@@ -106,30 +118,29 @@ BIO *bi; | |||
106 | return(0); | 118 | return(0); |
107 | bi->shutdown=1; | 119 | bi->shutdown=1; |
108 | bi->init=1; | 120 | bi->init=1; |
109 | bi->num=0; | 121 | bi->num= -1; |
110 | bi->ptr=(char *)b; | 122 | bi->ptr=(char *)b; |
111 | return(1); | 123 | return(1); |
112 | } | 124 | } |
113 | 125 | ||
114 | static int mem_free(a) | 126 | static int mem_free(BIO *a) |
115 | BIO *a; | ||
116 | { | 127 | { |
117 | if (a == NULL) return(0); | 128 | if (a == NULL) return(0); |
118 | if (a->shutdown) | 129 | if (a->shutdown) |
119 | { | 130 | { |
120 | if ((a->init) && (a->ptr != NULL)) | 131 | if ((a->init) && (a->ptr != NULL)) |
121 | { | 132 | { |
122 | BUF_MEM_free((BUF_MEM *)a->ptr); | 133 | BUF_MEM *b; |
134 | b = (BUF_MEM *)a->ptr; | ||
135 | if(a->flags & BIO_FLAGS_MEM_RDONLY) b->data = NULL; | ||
136 | BUF_MEM_free(b); | ||
123 | a->ptr=NULL; | 137 | a->ptr=NULL; |
124 | } | 138 | } |
125 | } | 139 | } |
126 | return(1); | 140 | return(1); |
127 | } | 141 | } |
128 | 142 | ||
129 | static int mem_read(b,out,outl) | 143 | static int mem_read(BIO *b, char *out, int outl) |
130 | BIO *b; | ||
131 | char *out; | ||
132 | int outl; | ||
133 | { | 144 | { |
134 | int ret= -1; | 145 | int ret= -1; |
135 | BUF_MEM *bm; | 146 | BUF_MEM *bm; |
@@ -139,28 +150,27 @@ int outl; | |||
139 | bm=(BUF_MEM *)b->ptr; | 150 | bm=(BUF_MEM *)b->ptr; |
140 | BIO_clear_retry_flags(b); | 151 | BIO_clear_retry_flags(b); |
141 | ret=(outl > bm->length)?bm->length:outl; | 152 | ret=(outl > bm->length)?bm->length:outl; |
142 | if ((out != NULL) && (ret > 0)) | 153 | if ((out != NULL) && (ret > 0)) { |
143 | { | ||
144 | memcpy(out,bm->data,ret); | 154 | memcpy(out,bm->data,ret); |
145 | bm->length-=ret; | 155 | bm->length-=ret; |
146 | /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */ | 156 | /* memmove(&(bm->data[0]),&(bm->data[ret]), bm->length); */ |
147 | from=(char *)&(bm->data[ret]); | 157 | if(b->flags & BIO_FLAGS_MEM_RDONLY) bm->data += ret; |
148 | to=(char *)&(bm->data[0]); | 158 | else { |
149 | for (i=0; i<bm->length; i++) | 159 | from=(char *)&(bm->data[ret]); |
150 | to[i]=from[i]; | 160 | to=(char *)&(bm->data[0]); |
161 | for (i=0; i<bm->length; i++) | ||
162 | to[i]=from[i]; | ||
151 | } | 163 | } |
152 | else if (bm->length == 0) | 164 | } else if (bm->length == 0) |
153 | { | 165 | { |
154 | BIO_set_retry_read(b); | 166 | ret = b->num; |
155 | ret= -1; | 167 | if (ret != 0) |
168 | BIO_set_retry_read(b); | ||
156 | } | 169 | } |
157 | return(ret); | 170 | return(ret); |
158 | } | 171 | } |
159 | 172 | ||
160 | static int mem_write(b,in,inl) | 173 | static int mem_write(BIO *b, const char *in, int inl) |
161 | BIO *b; | ||
162 | char *in; | ||
163 | int inl; | ||
164 | { | 174 | { |
165 | int ret= -1; | 175 | int ret= -1; |
166 | int blen; | 176 | int blen; |
@@ -173,6 +183,11 @@ int inl; | |||
173 | goto end; | 183 | goto end; |
174 | } | 184 | } |
175 | 185 | ||
186 | if(b->flags & BIO_FLAGS_MEM_RDONLY) { | ||
187 | BIOerr(BIO_F_MEM_WRITE,BIO_R_WRITE_TO_READ_ONLY_BIO); | ||
188 | goto end; | ||
189 | } | ||
190 | |||
176 | BIO_clear_retry_flags(b); | 191 | BIO_clear_retry_flags(b); |
177 | blen=bm->length; | 192 | blen=bm->length; |
178 | if (BUF_MEM_grow(bm,blen+inl) != (blen+inl)) | 193 | if (BUF_MEM_grow(bm,blen+inl) != (blen+inl)) |
@@ -183,11 +198,7 @@ end: | |||
183 | return(ret); | 198 | return(ret); |
184 | } | 199 | } |
185 | 200 | ||
186 | static long mem_ctrl(b,cmd,num,ptr) | 201 | static long mem_ctrl(BIO *b, int cmd, long num, void *ptr) |
187 | BIO *b; | ||
188 | int cmd; | ||
189 | long num; | ||
190 | char *ptr; | ||
191 | { | 202 | { |
192 | long ret=1; | 203 | long ret=1; |
193 | char **pptr; | 204 | char **pptr; |
@@ -198,12 +209,26 @@ char *ptr; | |||
198 | { | 209 | { |
199 | case BIO_CTRL_RESET: | 210 | case BIO_CTRL_RESET: |
200 | if (bm->data != NULL) | 211 | if (bm->data != NULL) |
201 | memset(bm->data,0,bm->max); | 212 | { |
202 | bm->length=0; | 213 | /* For read only case reset to the start again */ |
214 | if(b->flags & BIO_FLAGS_MEM_RDONLY) | ||
215 | { | ||
216 | bm->data -= bm->max - bm->length; | ||
217 | bm->length = bm->max; | ||
218 | } | ||
219 | else | ||
220 | { | ||
221 | memset(bm->data,0,bm->max); | ||
222 | bm->length=0; | ||
223 | } | ||
224 | } | ||
203 | break; | 225 | break; |
204 | case BIO_CTRL_EOF: | 226 | case BIO_CTRL_EOF: |
205 | ret=(long)(bm->length == 0); | 227 | ret=(long)(bm->length == 0); |
206 | break; | 228 | break; |
229 | case BIO_C_SET_BUF_MEM_EOF_RETURN: | ||
230 | b->num=(int)num; | ||
231 | break; | ||
207 | case BIO_CTRL_INFO: | 232 | case BIO_CTRL_INFO: |
208 | ret=(long)bm->length; | 233 | ret=(long)bm->length; |
209 | if (ptr != NULL) | 234 | if (ptr != NULL) |
@@ -250,10 +275,7 @@ char *ptr; | |||
250 | return(ret); | 275 | return(ret); |
251 | } | 276 | } |
252 | 277 | ||
253 | static int mem_gets(bp,buf,size) | 278 | static int mem_gets(BIO *bp, char *buf, int size) |
254 | BIO *bp; | ||
255 | char *buf; | ||
256 | int size; | ||
257 | { | 279 | { |
258 | int i,j; | 280 | int i,j; |
259 | int ret= -1; | 281 | int ret= -1; |
@@ -283,9 +305,7 @@ int size; | |||
283 | return(ret); | 305 | return(ret); |
284 | } | 306 | } |
285 | 307 | ||
286 | static int mem_puts(bp,str) | 308 | static int mem_puts(BIO *bp, const char *str) |
287 | BIO *bp; | ||
288 | char *str; | ||
289 | { | 309 | { |
290 | int n,ret; | 310 | int n,ret; |
291 | 311 | ||