1/*
2Copyright (C) 2005-2006 NSRT Team ( http://nsrt.edgeemu.com )
3Copyright (C) 2002 Andrea Mazzoleni ( http://advancemame.sf.net )
4Copyright (C) 2001-4 Igor Pavlov ( http://www.7-zip.org )
5
6This library is free software; you can redistribute it and/or
7modify it under the terms of the GNU Lesser General Public
8License version 2.1 as published by the Free Software Foundation.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public
16License along with this library; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20#include "portable.h"
21#include "iiostrm.h"
22#include "crc32.h"
23
24HRESULT ISequentialInStream_Array::Read(void *aData, UINT32 aSize, UINT32 *aProcessedSize)
25{
26 if (aSize > size)
27 {
28 aSize = size;
29 }
30
31 *aProcessedSize = aSize;
32 memcpy(aData, data, aSize);
33 size -= aSize;
34 data += aSize;
35 return(S_OK);
36}
37
38HRESULT ISequentialOutStream_Array::Write(const void *aData, UINT32 aSize, UINT32 *aProcessedSize)
39{
40 if (aSize > size)
41 {
42 overflow = true;
43 aSize = size;
44 }
45
46 *aProcessedSize = aSize;
47 memcpy(data, aData, aSize);
48 size -= aSize;
49 data += aSize;
50 total += aSize;
51 return(S_OK);
52}
53
54HRESULT ISequentialInStream_String::Read(void *aData, UINT32 aSize, UINT32 *aProcessedSize)
55{
56 if (aSize > data.size())
57 {
58 aSize = data.size();
59 }
60
61 *aProcessedSize = aSize;
62 memcpy(aData, data.c_str(), aSize);
63 data.erase(0, aSize);
64 return(S_OK);
65}
66
67HRESULT ISequentialOutStream_String::Write(const void *aData, UINT32 aSize, UINT32 *aProcessedSize)
68{
69 *aProcessedSize = aSize;
70 data.append((const char *)aData, aSize);
71 total += aSize;
72 return(S_OK);
73}
74
75HRESULT ISequentialInStream_Istream::Read(void *aData, UINT32 aSize, UINT32 *aProcessedSize)
76{
77 data.read((char *)aData, aSize);
78 *aProcessedSize = data.gcount();
79 return(S_OK);
80}
81
82HRESULT ISequentialOutStream_Ostream::Write(const void *aData, UINT32 aSize, UINT32 *aProcessedSize)
83{
84 *aProcessedSize = aSize;
85 data.write((char *)aData, aSize);
86 total += aSize;
87 return(S_OK);
88}
89
90
91
92HRESULT ISequentialInStreamCRC32_Array::Read(void *aData, UINT32 aSize, UINT32 *aProcessedSize)
93{
94 ISequentialInStream_Array::Read(aData, aSize, aProcessedSize);
95 crc32 = CRC32lib::CRC32((const unsigned char *)aData, *aProcessedSize, ~crc32);
96 return(S_OK);
97}
98
99HRESULT ISequentialOutStreamCRC32_Array::Write(const void *aData, UINT32 aSize, UINT32 *aProcessedSize)
100{
101 ISequentialOutStream_Array::Write(aData, aSize, aProcessedSize);
102 crc32 = CRC32lib::CRC32((const unsigned char *)aData, *aProcessedSize, ~crc32);
103 return(S_OK);
104}
105
106HRESULT ISequentialInStreamCRC32_String::Read(void *aData, UINT32 aSize, UINT32 *aProcessedSize)
107{
108 ISequentialInStream_String::Read(aData, aSize, aProcessedSize);
109 crc32 = CRC32lib::CRC32((const unsigned char *)aData, *aProcessedSize, ~crc32);
110 return(S_OK);
111}
112
113HRESULT ISequentialOutStreamCRC32_String::Write(const void *aData, UINT32 aSize, UINT32 *aProcessedSize)
114{
115 ISequentialOutStream_String::Write(aData, aSize, aProcessedSize);
116 crc32 = CRC32lib::CRC32((const unsigned char *)aData, *aProcessedSize, ~crc32);
117 return(S_OK);
118}
119
120HRESULT ISequentialInStreamCRC32_Istream::Read(void *aData, UINT32 aSize, UINT32 *aProcessedSize)
121{
122 ISequentialInStream_Istream::Read(aData, aSize, aProcessedSize);
123 crc32 = CRC32lib::CRC32((const unsigned char *)aData, *aProcessedSize, ~crc32);
124 return(S_OK);
125}
126
127HRESULT ISequentialOutStreamCRC32_Ostream::Write(const void *aData, UINT32 aSize, UINT32 *aProcessedSize)
128{
129 ISequentialOutStream_Ostream::Write(aData, aSize, aProcessedSize);
130 crc32 = CRC32lib::CRC32((const unsigned char *)aData, *aProcessedSize, ~crc32);
131 return(S_OK);
132}
133