1 /* 2 Copyright (C) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; version 2 of the License. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; see the file COPYING. If not, write to the 15 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 16 MA 02110-1301 USA. 17 */ 18 19 /* file.hpp provies File Sources and Sinks 20 */ 21 22 23 #ifndef TAO_CRYPT_FILE_HPP 24 #define TAO_CRYPT_FILE_HPP 25 26 #include "misc.hpp" 27 #include "block.hpp" 28 #include "error.hpp" 29 #include <stdio.h> 30 31 namespace TaoCrypt { 32 33 34 class Source { 35 ByteBlock buffer_; 36 word32 current_; 37 Error error_; 38 public: Source(word32 sz=0)39 explicit Source(word32 sz = 0) : buffer_(sz), current_(0) {} Source(const byte * b,word32 sz)40 Source(const byte* b, word32 sz) : buffer_(b, sz), current_(0) {} 41 remaining()42 word32 remaining() { if (GetError().What()) return 0; 43 else return buffer_.size() - current_; } size() const44 word32 size() const { return buffer_.size(); } grow(word32 sz)45 void grow(word32 sz) { buffer_.CleanGrow(sz); } 46 IsLeft(word32 sz)47 bool IsLeft(word32 sz) { if (remaining() >= sz) return true; 48 else { SetError(CONTENT_E); return false; } } 49 get_buffer() const50 const byte* get_buffer() const { return buffer_.get_buffer(); } get_current() const51 const byte* get_current() const { return &buffer_[current_]; } get_index() const52 word32 get_index() const { return current_; } set_index(word32 i)53 void set_index(word32 i) { if (i < size()) current_ = i; } 54 operator [](word32 i)55 byte operator[] (word32 i) { current_ = i; return next(); } next()56 byte next() { if (IsLeft(1)) return buffer_[current_++]; else return 0; } prev()57 byte prev() { if (current_) return buffer_[--current_]; else return 0; } 58 add(const byte * data,word32 len)59 void add(const byte* data, word32 len) 60 { 61 if (IsLeft(len)) { 62 memcpy(buffer_.get_buffer() + current_, data, len); 63 current_ += len; 64 } 65 } 66 advance(word32 i)67 void advance(word32 i) { if (IsLeft(i)) current_ += i; } 68 void reset(ByteBlock&); 69 GetError()70 Error GetError() { return error_; } SetError(ErrorNumber w)71 void SetError(ErrorNumber w) { error_.SetError(w); } 72 73 friend class FileSource; // for get() 74 Source(const Source & that)75 Source(const Source& that) 76 : buffer_(that.buffer_), current_(that.current_) {} 77 operator =(const Source & that)78 Source& operator=(const Source& that) 79 { 80 Source tmp(that); 81 Swap(tmp); 82 return *this; 83 } 84 Swap(Source & other)85 void Swap(Source& other) 86 { 87 buffer_.Swap(other.buffer_); 88 STL::swap(current_, other.current_); 89 } 90 91 }; 92 93 94 // File Source 95 class FileSource { 96 FILE* file_; 97 public: 98 FileSource(const char* fname, Source& source); 99 ~FileSource(); 100 101 word32 size(bool use_current = false); 102 private: 103 word32 get(Source&); 104 word32 size_left(); 105 106 FileSource(const FileSource&); // hide 107 FileSource& operator=(const FileSource&); // hide 108 }; 109 110 111 // File Sink 112 class FileSink { 113 FILE* file_; 114 public: 115 FileSink(const char* fname, Source& source); 116 ~FileSink(); 117 118 word32 size(bool use_current = false); 119 private: 120 void put(Source&); 121 122 FileSink(const FileSink&); // hide 123 FileSink& operator=(const FileSink&); // hide 124 }; 125 126 127 128 } // namespace 129 130 #endif // TAO_CRYPT_FILE_HPP 131