~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to extra/yassl/taocrypt/src/file.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2000-2007 MySQL AB
 
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.cpp implements File Sources and Sinks
 
20
*/
 
21
 
 
22
#include "runtime.hpp"
 
23
#include "file.hpp"
 
24
 
 
25
 
 
26
namespace TaoCrypt {
 
27
 
 
28
 
 
29
FileSource::FileSource(const char* fname, Source& source)
 
30
{
 
31
    file_ = fopen(fname, "rb");
 
32
    if (file_) get(source);
 
33
}
 
34
 
 
35
 
 
36
FileSource::~FileSource()
 
37
{
 
38
    if (file_)
 
39
        fclose(file_);
 
40
}
 
41
 
 
42
 
 
43
 
 
44
// return size of source from beginning or current position
 
45
word32 FileSource::size(bool use_current)
 
46
{
 
47
    long current = ftell(file_);
 
48
    long begin   = current;
 
49
 
 
50
    if (!use_current) {
 
51
        fseek(file_, 0, SEEK_SET);
 
52
        begin = ftell(file_);
 
53
    }
 
54
 
 
55
    fseek(file_, 0, SEEK_END);
 
56
    long end = ftell(file_);
 
57
 
 
58
    fseek(file_, current, SEEK_SET);
 
59
 
 
60
    return end - begin;
 
61
}
 
62
 
 
63
 
 
64
word32 FileSource::size_left()
 
65
{
 
66
    return size(true);
 
67
}
 
68
 
 
69
 
 
70
// fill file source from source
 
71
word32 FileSource::get(Source& source)
 
72
{
 
73
    word32 sz(size());
 
74
    if (source.size() < sz)
 
75
        source.grow(sz);
 
76
 
 
77
    size_t bytes = fread(source.buffer_.get_buffer(), 1, sz, file_);
 
78
 
 
79
    if (bytes == 1)
 
80
        return sz;
 
81
    else
 
82
        return 0;
 
83
}
 
84
 
 
85
 
 
86
FileSink::FileSink(const char* fname, Source& source)
 
87
{
 
88
    file_ = fopen(fname, "wb");
 
89
    if (file_) put(source);
 
90
}
 
91
 
 
92
 
 
93
FileSink::~FileSink()
 
94
{
 
95
    if (file_)
 
96
        fclose(file_);
 
97
}
 
98
 
 
99
 
 
100
// fill source from file sink
 
101
void FileSink::put(Source& source)
 
102
{
 
103
    fwrite(source.get_buffer(), 1, source.size(), file_);
 
104
}
 
105
 
 
106
 
 
107
// swap with other and reset to beginning
 
108
void Source::reset(ByteBlock& otherBlock)
 
109
{
 
110
    buffer_.Swap(otherBlock);   
 
111
    current_ = 0;
 
112
}
 
113
 
 
114
 
 
115
}  // namespace