Botan  1.10.9
pipe_rw.cpp
Go to the documentation of this file.
1 /*
2 * Pipe Reading/Writing
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/pipe.h>
9 #include <botan/internal/out_buf.h>
10 #include <botan/secqueue.h>
11 
12 namespace Botan {
13 
14 /*
15 * Look up the canonical ID for a queue
16 */
17 Pipe::message_id Pipe::get_message_no(const std::string& func_name,
18  message_id msg) const
19  {
20  if(msg == DEFAULT_MESSAGE)
21  msg = default_msg();
22  else if(msg == LAST_MESSAGE)
23  msg = message_count() - 1;
24 
25  if(msg >= message_count())
26  throw Invalid_Message_Number(func_name, msg);
27 
28  return msg;
29  }
30 
31 /*
32 * Write into a Pipe
33 */
34 void Pipe::write(const byte input[], size_t length)
35  {
36  if(!inside_msg)
37  throw Invalid_State("Cannot write to a Pipe while it is not processing");
38  pipe->write(input, length);
39  }
40 
41 /*
42 * Write into a Pipe
43 */
44 void Pipe::write(const MemoryRegion<byte>& input)
45  {
46  write(&input[0], input.size());
47  }
48 
49 /*
50 * Write a string into a Pipe
51 */
52 void Pipe::write(const std::string& str)
53  {
54  write(reinterpret_cast<const byte*>(str.data()), str.size());
55  }
56 
57 /*
58 * Write a single byte into a Pipe
59 */
60 void Pipe::write(byte input)
61  {
62  write(&input, 1);
63  }
64 
65 /*
66 * Write the contents of a DataSource into a Pipe
67 */
68 void Pipe::write(DataSource& source)
69  {
70  SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
71  while(!source.end_of_data())
72  {
73  size_t got = source.read(&buffer[0], buffer.size());
74  write(&buffer[0], got);
75  }
76  }
77 
78 /*
79 * Read some data from the pipe
80 */
81 size_t Pipe::read(byte output[], size_t length, message_id msg)
82  {
83  return outputs->read(output, length, get_message_no("read", msg));
84  }
85 
86 /*
87 * Read some data from the pipe
88 */
89 size_t Pipe::read(byte output[], size_t length)
90  {
91  return read(output, length, DEFAULT_MESSAGE);
92  }
93 
94 /*
95 * Read a single byte from the pipe
96 */
97 size_t Pipe::read(byte& out, message_id msg)
98  {
99  return read(&out, 1, msg);
100  }
101 
102 /*
103 * Return all data in the pipe
104 */
106  {
107  msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
108  SecureVector<byte> buffer(remaining(msg));
109  size_t got = read(&buffer[0], buffer.size(), msg);
110  buffer.resize(got);
111  return buffer;
112  }
113 
114 /*
115 * Return all data in the pipe as a string
116 */
118  {
119  msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
120  SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
121  std::string str;
122  str.reserve(remaining(msg));
123 
124  while(true)
125  {
126  size_t got = read(&buffer[0], buffer.size(), msg);
127  if(got == 0)
128  break;
129  str.append(reinterpret_cast<const char*>(&buffer[0]), got);
130  }
131 
132  return str;
133  }
134 
135 /*
136 * Find out how many bytes are ready to read
137 */
138 size_t Pipe::remaining(message_id msg) const
139  {
140  return outputs->remaining(get_message_no("remaining", msg));
141  }
142 
143 /*
144 * Peek at some data in the pipe
145 */
146 size_t Pipe::peek(byte output[], size_t length,
147  size_t offset, message_id msg) const
148  {
149  return outputs->peek(output, length, offset, get_message_no("peek", msg));
150  }
151 
152 /*
153 * Peek at some data in the pipe
154 */
155 size_t Pipe::peek(byte output[], size_t length, size_t offset) const
156  {
157  return peek(output, length, offset, DEFAULT_MESSAGE);
158  }
159 
160 /*
161 * Peek at a byte in the pipe
162 */
163 size_t Pipe::peek(byte& out, size_t offset, message_id msg) const
164  {
165  return peek(&out, 1, offset, msg);
166  }
167 
168 }
size_t read(byte[], size_t, Pipe::message_id)
Definition: out_buf.cpp:17
void resize(size_t n)
Definition: secmem.h:211
virtual void write(const byte input[], size_t length)=0
virtual size_t read(byte out[], size_t length)=0
size_t read(byte output[], size_t length)
Definition: pipe_rw.cpp:89
size_t peek(byte[], size_t, size_t, Pipe::message_id) const
Definition: out_buf.cpp:29
std::string read_all_as_string(message_id=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:117
void write(const byte in[], size_t length)
Definition: pipe_rw.cpp:34
size_t message_id
Definition: pipe.h:31
size_t remaining(message_id msg=DEFAULT_MESSAGE) const
Definition: pipe_rw.cpp:138
static const message_id DEFAULT_MESSAGE
Definition: pipe.h:57
unsigned char byte
Definition: types.h:22
SecureVector< byte > read_all(message_id msg=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:105
static const message_id LAST_MESSAGE
Definition: pipe.h:52
virtual bool end_of_data() const =0
size_t size() const
Definition: secmem.h:29
size_t remaining(Pipe::message_id) const
Definition: out_buf.cpp:42
size_t default_msg() const
Definition: pipe.h:206
size_t peek(byte output[], size_t length, size_t offset) const
Definition: pipe_rw.cpp:155
message_id message_count() const
Definition: pipe.cpp:282