Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / bio / bio_mem.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56
57 #include <openssl/bio.h>
58
59 #include <limits.h>
60
61 #include <openssl/buf.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64
65
66 BIO *BIO_new_mem_buf(void *buf, int len) {
67   BIO *ret;
68   BUF_MEM *b;
69   const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
70
71   if (!buf && len != 0) {
72     OPENSSL_PUT_ERROR(BIO, BIO_new_mem_buf, BIO_R_NULL_PARAMETER);
73     return NULL;
74   }
75
76   ret = BIO_new(BIO_s_mem());
77   if (ret == NULL) {
78     return NULL;
79   }
80
81   b = (BUF_MEM *)ret->ptr;
82   b->data = buf;
83   b->length = size;
84   b->max = size;
85
86   ret->flags |= BIO_FLAGS_MEM_RDONLY;
87
88   /* |num| is used to store the value that this BIO will return when it runs
89    * out of data. If it's negative then the retry flags will also be set. Since
90    * this is static data, retrying wont help */
91   ret->num = 0;
92
93   return ret;
94 }
95
96 static int mem_new(BIO *bio) {
97   BUF_MEM *b;
98
99   b = BUF_MEM_new();
100   if (b == NULL) {
101     return 0;
102   }
103
104   /* |shutdown| is used to store the close flag: whether the BIO has ownership
105    * of the BUF_MEM. */
106   bio->shutdown = 1;
107   bio->init = 1;
108   bio->num = -1;
109   bio->ptr = (char *)b;
110
111   return 1;
112 }
113
114 static int mem_free(BIO *bio) {
115   BUF_MEM *b;
116
117   if (bio == NULL) {
118     return 0;
119   }
120
121   if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
122     return 1;
123   }
124
125   b = (BUF_MEM *)bio->ptr;
126   if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
127     b->data = NULL;
128   }
129   BUF_MEM_free(b);
130   bio->ptr = NULL;
131   return 1;
132 }
133
134 static int mem_read(BIO *bio, char *out, int outl) {
135   int ret;
136   BUF_MEM *b = (BUF_MEM*) bio->ptr;
137
138   BIO_clear_retry_flags(bio);
139   ret = outl;
140   if (b->length < INT_MAX && ret > (int)b->length) {
141     ret = b->length;
142   }
143
144   if (ret > 0) {
145     memcpy(out, b->data, ret);
146     b->length -= ret;
147     if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
148       b->data += ret;
149     } else {
150       memmove(b->data, &b->data[ret], b->length);
151     }
152   } else if (b->length == 0) {
153     ret = bio->num;
154     if (ret != 0) {
155       BIO_set_retry_read(bio);
156     }
157   }
158   return ret;
159 }
160
161 static int mem_write(BIO *bio, const char *in, int inl) {
162   int ret = -1;
163   int blen;
164   BUF_MEM *b;
165
166   b = (BUF_MEM *)bio->ptr;
167
168   if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
169     OPENSSL_PUT_ERROR(BIO, mem_write, BIO_R_WRITE_TO_READ_ONLY_BIO);
170     goto err;
171   }
172
173   BIO_clear_retry_flags(bio);
174   blen = b->length;
175   if (INT_MAX - blen < inl) {
176     goto err;
177   }
178   if (BUF_MEM_grow_clean(b, blen + inl) != (blen + inl)) {
179     goto err;
180   }
181   memcpy(&b->data[blen], in, inl);
182   ret = inl;
183
184 err:
185   return ret;
186 }
187
188 static int mem_puts(BIO *bp, const char *str) {
189   return mem_write(bp, str, strlen(str));
190 }
191
192 static int mem_gets(BIO *bio, char *buf, int size) {
193   int i, j;
194   char *p;
195   BUF_MEM *b = (BUF_MEM *)bio->ptr;
196
197   BIO_clear_retry_flags(bio);
198   j = b->length;
199   if (size - 1 < j) {
200     j = size - 1;
201   }
202   if (j <= 0) {
203     if (size > 0) {
204       *buf = 0;
205     }
206     return 0;
207   }
208
209   p = b->data;
210   for (i = 0; i < j; i++) {
211     if (p[i] == '\n') {
212       i++;
213       break;
214     }
215   }
216
217   /* i is now the max num of bytes to copy, either j or up to and including the
218    * first newline */
219
220   i = mem_read(bio, buf, i);
221   if (i > 0) {
222     buf[i] = '\0';
223   }
224   return i;
225 }
226
227 static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
228   long ret = 1;
229   char **pptr;
230
231   BUF_MEM *b = (BUF_MEM *)bio->ptr;
232
233   switch (cmd) {
234     case BIO_CTRL_RESET:
235       if (b->data != NULL) {
236         /* For read only case reset to the start again */
237         if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
238           b->data -= b->max - b->length;
239           b->length = b->max;
240         } else {
241           memset(b->data, 0, b->max);
242           b->length = 0;
243         }
244       }
245       break;
246     case BIO_CTRL_EOF:
247       ret = (long)(b->length == 0);
248       break;
249     case BIO_C_SET_BUF_MEM_EOF_RETURN:
250       bio->num = (int)num;
251       break;
252     case BIO_CTRL_INFO:
253       ret = (long)b->length;
254       if (ptr != NULL) {
255         pptr = (char **)ptr;
256         *pptr = (char *)&b->data[0];
257       }
258       break;
259     case BIO_C_SET_BUF_MEM:
260       mem_free(bio);
261       bio->shutdown = (int)num;
262       bio->ptr = ptr;
263       break;
264     case BIO_C_GET_BUF_MEM_PTR:
265       if (ptr != NULL) {
266         pptr = (char **)ptr;
267         *pptr = (char *)b;
268       }
269       break;
270     case BIO_CTRL_GET_CLOSE:
271       ret = (long)bio->shutdown;
272       break;
273     case BIO_CTRL_SET_CLOSE:
274       bio->shutdown = (int)num;
275       break;
276
277     case BIO_CTRL_WPENDING:
278       ret = 0L;
279       break;
280     case BIO_CTRL_PENDING:
281       ret = (long)b->length;
282       break;
283     case BIO_CTRL_FLUSH:
284       ret = 1;
285       break;
286     default:
287       ret = 0;
288       break;
289   }
290   return ret;
291 }
292
293 static const BIO_METHOD mem_method = {
294     BIO_TYPE_MEM, "memory buffer", mem_write, mem_read, mem_puts,
295     mem_gets,     mem_ctrl,        mem_new,   mem_free, NULL, };
296
297 const BIO_METHOD *BIO_s_mem(void) { return &mem_method; }
298
299 int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents,
300                      size_t *out_len) {
301   const BUF_MEM *b;
302   if (bio->method != &mem_method) {
303     return 0;
304   }
305
306   b = (BUF_MEM *)bio->ptr;
307   *out_contents = (uint8_t *)b->data;
308   *out_len = b->length;
309   return 1;
310 }
311
312 long BIO_get_mem_data(BIO *bio, char **contents) {
313   return BIO_ctrl(bio, BIO_CTRL_INFO, 0, (char *) contents);
314 }
315
316 int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) {
317   return BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, (char *) out);
318 }
319
320 int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) {
321   return BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, (char *) b);
322 }
323
324 int BIO_set_mem_eof_return(BIO *bio, int eof_value) {
325   return BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL);
326 }