1/*
2 * Copyright (c) 2011-12 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _OSSL_BIO_H_
25#define _OSSL_BIO_H_
26
27/* symbol renaming */
28#define BIO_new				ossl_BIO_new
29#define BIO_s_mem			ossl_BIO_s_mem
30#define BIO_free			ossl_BIO_free
31#define BIO_new_mem_buf			ossl_BIO_new_mem_buf
32#define BIO_get_mem_data		ossl_BIO_get_mem_data
33#define BIO_set_flags			ossl_BIO_set_flags
34#define BIO_clear_flags			ossl_BIO_clear_flags
35#define BIO_set				ossl_BIO_set
36#define BIO_ctrl			ossl_BIO_ctrl
37#define BIO_gets			ossl_BIO_gets
38#define BIO_write			ossl_BIO_write
39
40#define BIO_new_file			ossl_BIO_new_file
41#define BIO_new_fp			ossl_BIO_new_fp
42#define BIO_printf			ossl_BIO_printf
43#define BIO_s_file			ossl_BIO_s_file
44#define BIO_set_fp			ossl_BIO_set_fp
45#define BIO_snprintf			ossl_BIO_snprintf
46#define BIO_vprintf			ossl_BIO_vprintf
47#define BIO_vsnprinf			ossl_BIO_vsnprintf
48
49/* BIO_METHOD types */
50#define BIO_TYPE_NONE			0
51#define BIO_TYPE_MEM			(1|0x0400)
52#define BIO_TYPE_FILE			(2|0x0400)
53
54/* BIO flags */
55#define BIO_FLAGS_UPLINK		0
56#define BIO_FLAGS_READ			0x01
57#define BIO_FLAGS_WRITE			0x02
58#define BIO_FLAGS_IO_SPECIAL		0x04
59#define BIO_FLAGS_RWS			(BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL)
60#define BIO_FLAGS_SHOULD_RETRY		0x08
61
62/* BIO_ctrl() commands */
63#define BIO_CTRL_RESET			1       /* opt - rewind/zero etc */
64#define BIO_CTRL_EOF			2       /* opt - are we at the eof */
65#define BIO_CTRL_INFO			3       /* opt - extra tit-bits */
66#define BIO_CTRL_SET			4       /* man - set the 'IO' type */
67#define BIO_CTRL_GET			5       /* man - get the 'IO' type */
68#define BIO_CTRL_PUSH			6       /* opt - internal, used to signify change */
69#define BIO_CTRL_POP			7       /* opt - internal, used to signify change */
70#define BIO_CTRL_GET_CLOSE		8       /* man - set the 'close' on free */
71#define BIO_CTRL_SET_CLOSE		9       /* man - set the 'close' on free */
72#define BIO_CTRL_PENDING		10      /* opt - is their more data buffered */
73#define BIO_CTRL_FLUSH			11      /* opt - 'flush' buffered output */
74#define BIO_CTRL_DUP			12      /* man - extra stuff for 'duped' BIO */
75#define BIO_CTRL_WPENDING		13      /* opt - number of bytes still to write */
76
77
78#define BIO_FLAGS_MEM_RDONLY		0x200
79
80#define BIO_C_SET_FD			104
81#define BIO_C_GET_FD			105
82#define BIO_C_SET_FILE_PTR		106
83#define BIO_C_GET_FILE_PTR		107
84#define BIO_C_SET_FILENAME		108
85#define BIO_C_FILE_SEEK			128
86#define BIO_C_FILE_TELL			133
87
88#define BIO_C_SET_BUF_MEM		114
89#define BIO_C_GET_BUF_MEM_PTR		115
90#define BIO_C_SET_BUF_MEM_EOF_RETURN	130
91
92/* BIO callback */
93#define BIO_CB_FREE			0x01
94#define BIO_CB_READ			0x02
95#define BIO_CB_WRITE			0x03
96#define BIO_CB_PUTS			0x04
97#define BIO_CB_GETS			0x05
98#define BIO_CB_CTRL			0x06
99#define BIO_CB_RETURN			0x80
100
101#define BIO_NOCLOSE			0x00
102#define BIO_CLOSE			0x01
103#define BIO_FP_READ			0x02
104#define BIO_FP_WRITE			0x04
105#define BIO_FP_APPEND			0x08
106#define BIO_FP_TEXT			0x10
107
108struct bio_st;
109typedef struct bio_st	BIO;
110
111typedef void		bio_info_cb (struct bio_st *, int, const char *, int, long, long);
112
113typedef struct bio_method_st {
114	int		type;
115	const char *	name;
116	int (*bwrite)(BIO *, const char *, int);
117	int (*bread)(BIO *, char *, int);
118	int (*bputs)(BIO *, const char *);
119	int (*bgets)(BIO *, char *, int);
120	long (*ctrl)(BIO *, int, long, void *);
121	int (*create)(BIO *);
122	int (*destroy)(BIO *);
123	long (*callback_ctrl)(BIO *, int, bio_info_cb *);
124} BIO_METHOD;
125
126struct bio_st {
127	BIO_METHOD *	method;
128	/* bio, mode, argp, argi, argl, ret */
129	long		(*callback)(struct bio_st *, int, const char *, int, long, long);
130	char *		cb_arg; /* first argument for the callback */
131
132	int		init;
133	int		shutdown;
134	int		flags; /* extra storage */
135	int		retry_reason;
136	int		num;
137	void *		ptr;
138	struct bio_st * next_bio;       /* used by filter BIOs */
139	struct bio_st * prev_bio;       /* used by filter BIOs */
140	int		references;
141	unsigned long	num_read;
142	unsigned long	num_write;
143#if 0
144	CRYPTO_EX_DATA	ex_data;
145#endif
146};
147
148BIO *BIO_new(BIO_METHOD *);
149BIO_METHOD *BIO_s_mem(void);
150int BIO_free(BIO *a);
151BIO *BIO_new_mem_buf(void *buf, int len);
152long BIO_get_mem_data(BIO *b, void *p);
153void BIO_set_flags(BIO *b, int flags);
154void BIO_clear_flags(BIO *b, int flags);
155int BIO_set(BIO *bio, BIO_METHOD *method);
156long BIO_ctrl(BIO *b, int cmd, long larg, void *parg);
157int BIO_gets(BIO *bp, char *buf, int size);
158int BIO_write(BIO *b, const void *in, int inl);
159
160BIO_METHOD *BIO_s_file(void);
161BIO *BIO_new_fp(FILE *stream, int close_flag);
162BIO *BIO_new_file(const char *filename, const char *mode);
163long BIO_set_fp(BIO *b, FILE *fp, long c);
164
165int BIO_printf(BIO *bio, const char *format, ...);
166int BIO_snprintf(char *buf, size_t n, const char *format, ...);
167int BIO_vprintf(BIO *bio, const char *format, va_list args);
168int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args);
169
170#endif /* _OSSL_BIO_H_ */
171