Add migration guide for 3.0
[openssl.git] / doc / man3 / BIO_read.pod
1 =pod
2
3 =head1 NAME
4
5 BIO_read_ex, BIO_write_ex, BIO_read, BIO_write,
6 BIO_gets, BIO_get_line, BIO_puts
7 - BIO I/O functions
8
9 =head1 SYNOPSIS
10
11  #include <openssl/bio.h>
12
13  int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes);
14  int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written);
15
16  int BIO_read(BIO *b, void *data, int dlen);
17  int BIO_gets(BIO *b, char *buf, int size);
18  int BIO_get_line(BIO *b, char *buf, int size);
19  int BIO_write(BIO *b, const void *data, int dlen);
20  int BIO_puts(BIO *b, const char *buf);
21
22 =head1 DESCRIPTION
23
24 BIO_read_ex() attempts to read B<dlen> bytes from BIO B<b> and places the data
25 in B<data>. If any bytes were successfully read then the number of bytes read is
26 stored in B<*readbytes>.
27
28 BIO_write_ex() attempts to write B<dlen> bytes from B<data> to BIO B<b>. If
29 successful then the number of bytes written is stored in B<*written>.
30
31 BIO_read() attempts to read B<len> bytes from BIO B<b> and places
32 the data in B<buf>.
33
34 BIO_gets() performs the BIOs "gets" operation and places the data
35 in B<buf>. Usually this operation will attempt to read a line of data
36 from the BIO of maximum length B<size-1>. There are exceptions to this,
37 however; for example, BIO_gets() on a digest BIO will calculate and
38 return the digest and other BIOs may not support BIO_gets() at all.
39 The returned string is always NUL-terminated and the '\n' is preserved
40 if present in the input data.
41 On binary input there may be NUL characters within the string;
42 in this case the return value (if nonnegative) may give an incorrect length.
43
44 BIO_get_line() attempts to read from BIO <b> a line of data up to the next '\n'
45 or the maximum length B<size-1> is reached and places the data in B<buf>.
46 The returned string is always NUL-terminated and the '\n' is preserved
47 if present in the input data.
48 On binary input there may be NUL characters within the string;
49 in this case the return value (if nonnegative) gives the actual length read.
50 For implementing this, unfortunately the data needs to be read byte-by-byte.
51
52 BIO_write() attempts to write B<len> bytes from B<buf> to BIO B<b>.
53
54 BIO_puts() attempts to write a NUL-terminated string B<buf> to BIO B<b>.
55
56 =head1 RETURN VALUES
57
58 BIO_read_ex() and BIO_write_ex() return 1 if data was successfully read or
59 written, and 0 otherwise.
60
61 BIO_gets() returns -2 if the "gets" operation is not implemented by the BIO
62 or -1 on other errors.
63 Otherwise it typically returns the amount of data read,
64 but depending on the implementation it may return only the length up to
65 the first NUL character contained in the data read.
66 In any case the trailing NUL that is added after the data read
67 is not included in the length returned.
68
69 All other functions return either the amount of data successfully read or
70 written (if the return value is positive) or that no data was successfully
71 read or written if the result is 0 or -1. If the return value is -2 then
72 the operation is not implemented in the specific BIO type.
73
74 =head1 NOTES
75
76 A 0 or -1 return is not necessarily an indication of an error. In
77 particular when the source/sink is nonblocking or of a certain type
78 it may merely be an indication that no data is currently available and that
79 the application should retry the operation later.
80
81 One technique sometimes used with blocking sockets is to use a system call
82 (such as select(), poll() or equivalent) to determine when data is available
83 and then call read() to read the data. The equivalent with BIOs (that is call
84 select() on the underlying I/O structure and then call BIO_read() to
85 read the data) should B<not> be used because a single call to BIO_read()
86 can cause several reads (and writes in the case of SSL BIOs) on the underlying
87 I/O structure and may block as a result. Instead select() (or equivalent)
88 should be combined with non blocking I/O so successive reads will request
89 a retry instead of blocking.
90
91 See L<BIO_should_retry(3)> for details of how to
92 determine the cause of a retry and other I/O issues.
93
94 If the "gets" method is not supported by a BIO then BIO_get_line() can be used.
95 It is also possible to make BIO_gets() usable even if the "gets" method is not
96 supported by adding a buffering BIO L<BIO_f_buffer(3)> to the chain.
97
98 =head1 SEE ALSO
99
100 L<BIO_should_retry(3)>
101
102 =head1 HISTORY
103
104 BIO_gets() on 1.1.0 and older when called on BIO_fd() based BIO did not
105 keep the '\n' at the end of the line in the buffer.
106
107 BIO_get_line() was added in OpenSSL 3.0.
108
109 =head1 COPYRIGHT
110
111 Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
112
113 Licensed under the Apache License 2.0 (the "License").  You may not use
114 this file except in compliance with the License.  You can obtain a copy
115 in the file LICENSE in the source distribution or at
116 L<https://www.openssl.org/source/license.html>.
117
118 =cut