monotone

monotone Mtn Source Tree

Root/botan/data_src.cpp

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

Archive Download this file

Branches

Tags

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