monotone

monotone Mtn Source Tree

Root/botan/data_src.cpp

1/*************************************************
2* DataSource Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/data_src.h>
7#include <fstream>
8#include <algorithm>
9
10namespace Botan {
11
12/*************************************************
13* Read a single byte from the DataSource *
14*************************************************/
15u32bit DataSource::read_byte(byte& out)
16 {
17 return read(&out, 1);
18 }
19
20/*************************************************
21* Peek a single byte from the DataSource *
22*************************************************/
23u32bit DataSource::peek_byte(byte& out) const
24 {
25 return peek(&out, 1, 0);
26 }
27
28/*************************************************
29* Discard the next N bytes of the data *
30*************************************************/
31u32bit DataSource::discard_next(u32bit n)
32 {
33 u32bit discarded = 0;
34 byte dummy;
35 for(u32bit j = 0; j != n; ++j)
36 discarded = read_byte(dummy);
37 return discarded;
38 }
39
40/*************************************************
41* Read from a memory buffer *
42*************************************************/
43u32bit DataSource_Memory::read(byte out[], u32bit length)
44 {
45 u32bit got = std::min(source.size() - offset, length);
46 copy_mem(out, source + offset, got);
47 offset += got;
48 return got;
49 }
50
51/*************************************************
52* Peek into a memory buffer *
53*************************************************/
54u32bit DataSource_Memory::peek(byte out[], u32bit length,
55 u32bit peek_offset) const
56 {
57 const u32bit bytes_left = source.size() - offset;
58 if(peek_offset >= bytes_left) return 0;
59
60 u32bit got = std::min(bytes_left - peek_offset, length);
61 copy_mem(out, source + offset + peek_offset, got);
62 return got;
63 }
64
65/*************************************************
66* Check if the memory buffer is empty *
67*************************************************/
68bool DataSource_Memory::end_of_data() const
69 {
70 return (offset == source.size());
71 }
72
73/*************************************************
74* DataSource_Memory Constructor *
75*************************************************/
76DataSource_Memory::DataSource_Memory(const byte in[], u32bit length)
77 {
78 source.set(in, length);
79 offset = 0;
80 }
81
82/*************************************************
83* DataSource_Memory Constructor *
84*************************************************/
85DataSource_Memory::DataSource_Memory(const MemoryRegion<byte>& in)
86 {
87 source = in;
88 offset = 0;
89 }
90
91/*************************************************
92* DataSource_Memory Constructor *
93*************************************************/
94DataSource_Memory::DataSource_Memory(const std::string& in)
95 {
96 source.set((const byte*)in.c_str(), in.length());
97 offset = 0;
98 }
99
100/*************************************************
101* Read from a stream *
102*************************************************/
103u32bit DataSource_Stream::read(byte out[], u32bit length)
104 {
105 source->read((char*)out, length);
106 if(source->bad())
107 throw Stream_IO_Error("DataSource_Stream::read: Source failure");
108
109 u32bit got = source->gcount();
110 total_read += got;
111 return (u32bit)got;
112 }
113
114/*************************************************
115* Peek into a stream *
116*************************************************/
117u32bit DataSource_Stream::peek(byte out[], u32bit length, u32bit offset) const
118 {
119 if(end_of_data())
120 throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
121
122 u32bit got = 0;
123
124 if(offset)
125 {
126 SecureVector<byte> buf(offset);
127 source->read((char*)buf.begin(), buf.size());
128 if(source->bad())
129 throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
130 got = source->gcount();
131 }
132
133 if(got == offset)
134 {
135 source->read((char*)out, length);
136 if(source->bad())
137 throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
138 got = source->gcount();
139 }
140
141 if(source->eof())
142 source->clear();
143 source->seekg(total_read, std::ios::beg);
144
145 return got;
146 }
147
148/*************************************************
149* Check if the stream is empty or in error *
150*************************************************/
151bool DataSource_Stream::end_of_data() const
152 {
153 return (!source->good());
154 }
155
156/*************************************************
157* Return a human-readable ID for this stream *
158*************************************************/
159std::string DataSource_Stream::id() const
160 {
161 return fsname;
162 }
163
164/*************************************************
165* DataSource_Stream Constructor *
166*************************************************/
167DataSource_Stream::DataSource_Stream(const std::string& file,
168 bool use_binary) : fsname(file)
169 {
170 if(use_binary)
171 source = new std::ifstream(fsname.c_str(), std::ios::binary);
172 else
173 source = new std::ifstream(fsname.c_str());
174
175 if(!source->good())
176 throw Stream_IO_Error("DataSource_Stream: Failure opening " + fsname);
177 total_read = 0;
178 }
179
180/*************************************************
181* DataSource_Stream Destructor *
182*************************************************/
183DataSource_Stream::~DataSource_Stream()
184 {
185 delete source;
186 }
187
188}

Archive Download this file

Branches

Tags

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