00001
00002
00003
00004
00005
00006
00007
00008 #include <botan/pipe.h>
00009 #include <botan/internal/out_buf.h>
00010 #include <botan/secqueue.h>
00011
00012 namespace Botan {
00013
00014
00015
00016
00017 Pipe::message_id Pipe::get_message_no(const std::string& func_name,
00018 message_id msg) const
00019 {
00020 if(msg == DEFAULT_MESSAGE)
00021 msg = default_msg();
00022 else if(msg == LAST_MESSAGE)
00023 msg = message_count() - 1;
00024
00025 if(msg >= message_count())
00026 throw Invalid_Message_Number(func_name, msg);
00027
00028 return msg;
00029 }
00030
00031
00032
00033
00034 void Pipe::write(const byte input[], u32bit length)
00035 {
00036 if(!inside_msg)
00037 throw Invalid_State("Cannot write to a Pipe while it is not processing");
00038 pipe->write(input, length);
00039 }
00040
00041
00042
00043
00044 void Pipe::write(const MemoryRegion<byte>& input)
00045 {
00046 write(input.begin(), input.size());
00047 }
00048
00049
00050
00051
00052 void Pipe::write(const std::string& str)
00053 {
00054 write(reinterpret_cast<const byte*>(str.data()), str.size());
00055 }
00056
00057
00058
00059
00060 void Pipe::write(byte input)
00061 {
00062 write(&input, 1);
00063 }
00064
00065
00066
00067
00068 void Pipe::write(DataSource& source)
00069 {
00070 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
00071 while(!source.end_of_data())
00072 {
00073 u32bit got = source.read(buffer, buffer.size());
00074 write(buffer, got);
00075 }
00076 }
00077
00078
00079
00080
00081 u32bit Pipe::read(byte output[], u32bit length, message_id msg)
00082 {
00083 return outputs->read(output, length, get_message_no("read", msg));
00084 }
00085
00086
00087
00088
00089 u32bit Pipe::read(byte output[], u32bit length)
00090 {
00091 return read(output, length, DEFAULT_MESSAGE);
00092 }
00093
00094
00095
00096
00097 u32bit Pipe::read(byte& out, message_id msg)
00098 {
00099 return read(&out, 1, msg);
00100 }
00101
00102
00103
00104
00105 SecureVector<byte> Pipe::read_all(message_id msg)
00106 {
00107 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
00108 SecureVector<byte> buffer(remaining(msg));
00109 read(buffer, buffer.size(), msg);
00110 return buffer;
00111 }
00112
00113
00114
00115
00116 std::string Pipe::read_all_as_string(message_id msg)
00117 {
00118 msg = ((msg != DEFAULT_MESSAGE) ? msg : default_msg());
00119 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);
00120 std::string str;
00121 str.reserve(remaining(msg));
00122
00123 while(true)
00124 {
00125 u32bit got = read(buffer, buffer.size(), msg);
00126 if(got == 0)
00127 break;
00128 str.append(reinterpret_cast<const char*>(buffer.begin()), got);
00129 }
00130
00131 return str;
00132 }
00133
00134
00135
00136
00137 u32bit Pipe::remaining(message_id msg) const
00138 {
00139 return outputs->remaining(get_message_no("remaining", msg));
00140 }
00141
00142
00143
00144
00145 u32bit Pipe::peek(byte output[], u32bit length,
00146 u32bit offset, message_id msg) const
00147 {
00148 return outputs->peek(output, length, offset, get_message_no("peek", msg));
00149 }
00150
00151
00152
00153
00154 u32bit Pipe::peek(byte output[], u32bit length, u32bit offset) const
00155 {
00156 return peek(output, length, offset, DEFAULT_MESSAGE);
00157 }
00158
00159
00160
00161
00162 u32bit Pipe::peek(byte& out, u32bit offset, message_id msg) const
00163 {
00164 return peek(&out, 1, offset, msg);
00165 }
00166
00167 }