monotone

monotone Mtn Source Tree

Root/botan/pipe_rw.cpp

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status