aboutsummaryrefslogtreecommitdiffstats
path: root/include/data_src.h
blob: a64141ef93eeb3e35f1feef31a611b04a4d110ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*************************************************
* DataSource Header File                         *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#ifndef BOTAN_DATA_SRC_H__
#define BOTAN_DATA_SRC_H__

#include <botan/base.h>
#include <iosfwd>

namespace Botan {

/*************************************************
* Generic DataSource Interface                   *
*************************************************/
class DataSource
   {
   public:
      virtual u32bit read(byte[], u32bit) = 0;
      virtual u32bit peek(byte[], u32bit, u32bit) const = 0;
      virtual bool end_of_data() const = 0;
      virtual std::string id() const { return ""; }

      u32bit read_byte(byte&);
      u32bit peek_byte(byte&) const;
      u32bit discard_next(u32bit);

      DataSource() {}
      virtual ~DataSource() {}
   private:
      DataSource& operator=(const DataSource&) { return (*this); }
      DataSource(const DataSource&);
   };

/*************************************************
* Memory-Based DataSource                        *
*************************************************/
class DataSource_Memory : public DataSource
   {
   public:
      u32bit read(byte[], u32bit);
      u32bit peek(byte[], u32bit, u32bit) const;
      bool end_of_data() const;

      DataSource_Memory(const std::string&);
      DataSource_Memory(const byte[], u32bit);
      DataSource_Memory(const MemoryRegion<byte>&);
   private:
      SecureVector<byte> source;
      u32bit offset;
   };

/*************************************************
* Stream-Based DataSource                        *
*************************************************/
class DataSource_Stream : public DataSource
   {
   public:
      u32bit read(byte[], u32bit);
      u32bit peek(byte[], u32bit, u32bit) const;
      bool end_of_data() const;
      std::string id() const;

      DataSource_Stream(std::istream&, const std::string& id = "");
      DataSource_Stream(const std::string&, bool = false);
      ~DataSource_Stream();
   private:
      const std::string identifier;
      const bool owner;

      std::istream* source;
      u32bit total_read;
   };

}

#endif