Mercurial > vba-clojure
diff src/win32/7zip/7zipstreams.h @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/7zip/7zipstreams.h Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,315 @@ 1.4 +// This file is (modified) from 1.5 +// FCEUX (2009) 1.6 +// FCE Ultra - NES/Famicom Emulator 1.7 +// Copyright (C) 2003 Xodnizel 1.8 +// 1.9 +// This program is free software; you can redistribute it and/or modify 1.10 +// it under the terms of the GNU General Public License as published by 1.11 +// the Free Software Foundation; either version 2 of the License, or 1.12 +// (at your option) any later version. 1.13 +// 1.14 +// This program is distributed in the hope that it will be useful, 1.15 +// but WITHOUT ANY WARRANTY; without even the implied warranty of 1.16 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.17 +// GNU General Public License for more details. 1.18 +// 1.19 +// You should have received a copy of the GNU General Public License 1.20 +// along with this program; if not, write to the Free Software 1.21 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1.22 + 1.23 +#ifndef _7ZIPSTREAMS_HEADER 1.24 +#define _7ZIPSTREAMS_HEADER 1.25 + 1.26 +#include "7z/CPP/Common/MyCom.h" 1.27 + 1.28 +class ICountedSequentialOutStream : public ISequentialOutStream 1.29 +{ 1.30 +public: 1.31 + virtual UINT32 Size() const = 0; 1.32 +}; 1.33 + 1.34 +class SeqMemoryOutStream : public ICountedSequentialOutStream, private CMyUnknownImp 1.35 +{ 1.36 + UINT8* const output; 1.37 + UINT32 pos; 1.38 + const UINT32 size; 1.39 + ULONG refCount; 1.40 + 1.41 + HRESULT STDMETHODCALLTYPE QueryInterface(REFGUID, void**) 1.42 + { 1.43 + return E_NOINTERFACE; 1.44 + } 1.45 + 1.46 + HRESULT STDMETHODCALLTYPE Write(const void* data, UInt32 length, UInt32* bytesWritten) 1.47 + { 1.48 + if (data != NULL || size == 0) 1.49 + { 1.50 + //assert(length <= size - pos); 1.51 + 1.52 + if (length > size - pos) 1.53 + length = size - pos; 1.54 + 1.55 + if(data) 1.56 + memcpy(output + pos, data, length); 1.57 + pos += length; 1.58 + 1.59 + if (bytesWritten) 1.60 + *bytesWritten = length; 1.61 + 1.62 + return S_OK; 1.63 + } 1.64 + else 1.65 + { 1.66 + return E_INVALIDARG; 1.67 + } 1.68 + } 1.69 + 1.70 + MY_ADDREF_RELEASE 1.71 + 1.72 +public: 1.73 + 1.74 + SeqMemoryOutStream(void* d, UINT32 s) : output((UINT8*)d), pos(0), size(s), refCount(0) {} 1.75 + 1.76 + virtual ~SeqMemoryOutStream() 1.77 + { 1.78 + int a = 0; 1.79 + } 1.80 + 1.81 + UINT32 Size() const 1.82 + { 1.83 + return pos; 1.84 + } 1.85 +}; 1.86 + 1.87 +class SeqFileOutStream : public ICountedSequentialOutStream, private CMyUnknownImp 1.88 +{ 1.89 + FILE* file; 1.90 + UINT32 pos; 1.91 + ULONG refCount; 1.92 + 1.93 + HRESULT STDMETHODCALLTYPE QueryInterface(REFGUID, void**) 1.94 + { 1.95 + return E_NOINTERFACE; 1.96 + } 1.97 + 1.98 + HRESULT STDMETHODCALLTYPE Write(const void* data, UInt32 length, UInt32* bytesWritten) 1.99 + { 1.100 + if(!file) 1.101 + return E_FAIL; 1.102 + 1.103 + if (data != NULL) 1.104 + { 1.105 + int written = 0; 1.106 + if(data) 1.107 + written = fwrite(data, 1, length, file); 1.108 + 1.109 + pos += written; 1.110 + if (bytesWritten) 1.111 + *bytesWritten = written; 1.112 + 1.113 + return S_OK; 1.114 + } 1.115 + else 1.116 + { 1.117 + return E_INVALIDARG; 1.118 + } 1.119 + } 1.120 + 1.121 + MY_ADDREF_RELEASE 1.122 + 1.123 +public: 1.124 + 1.125 + SeqFileOutStream(const char* outFilename) : pos(0), refCount(0) 1.126 + { 1.127 + file = fopen(outFilename, "wb"); 1.128 + } 1.129 + virtual ~SeqFileOutStream() 1.130 + { 1.131 + if(file) 1.132 + fclose(file); 1.133 + } 1.134 + 1.135 + UINT32 Size() const 1.136 + { 1.137 + return pos; 1.138 + } 1.139 +}; 1.140 + 1.141 + 1.142 +class OutStream : public IArchiveExtractCallback, private CMyUnknownImp 1.143 +{ 1.144 + ICountedSequentialOutStream* seqStream; 1.145 + const UINT32 index; 1.146 + ULONG refCount; 1.147 + 1.148 + HRESULT STDMETHODCALLTYPE QueryInterface(REFGUID, void**) 1.149 + { 1.150 + return E_NOINTERFACE; 1.151 + } 1.152 + 1.153 + HRESULT STDMETHODCALLTYPE PrepareOperation(Int32) 1.154 + { 1.155 + return S_OK; 1.156 + } 1.157 + 1.158 + HRESULT STDMETHODCALLTYPE SetTotal(UInt64) 1.159 + { 1.160 + return S_OK; 1.161 + } 1.162 + 1.163 + HRESULT STDMETHODCALLTYPE SetCompleted(const UInt64*) 1.164 + { 1.165 + return S_OK; 1.166 + } 1.167 + 1.168 + HRESULT STDMETHODCALLTYPE SetOperationResult(Int32) 1.169 + { 1.170 + return S_OK; 1.171 + } 1.172 + 1.173 + HRESULT STDMETHODCALLTYPE GetStream(UInt32 id, ISequentialOutStream** ptr, Int32 mode) 1.174 + { 1.175 + switch (mode) 1.176 + { 1.177 + case NArchive::NExtract::NAskMode::kExtract: 1.178 + case NArchive::NExtract::NAskMode::kTest: 1.179 + 1.180 + if (id != index || ptr == NULL) 1.181 + return S_FALSE; 1.182 + else 1.183 + *ptr = seqStream; 1.184 + // fall through 1.185 + case NArchive::NExtract::NAskMode::kSkip: 1.186 + return S_OK; 1.187 + 1.188 + default: 1.189 + return E_INVALIDARG; 1.190 + } 1.191 + } 1.192 + 1.193 + MY_ADDREF_RELEASE 1.194 + 1.195 +public: 1.196 + 1.197 + OutStream(UINT32 index, void* data, UINT32 size) : index(index), refCount(0) 1.198 + { 1.199 + seqStream = new SeqMemoryOutStream(data, size); 1.200 + seqStream->AddRef(); 1.201 + } 1.202 + OutStream(UINT32 index, const char* outFilename) : index(index), refCount(0) 1.203 + { 1.204 + seqStream = new SeqFileOutStream(outFilename); 1.205 + seqStream->AddRef(); 1.206 + } 1.207 + virtual ~OutStream() 1.208 + { 1.209 + //seqStream->Release(); // commented out because apparently IInArchive::Extract() calls Release one more time than it calls AddRef 1.210 + } 1.211 + UINT32 Size() const 1.212 + { 1.213 + return seqStream->Size(); 1.214 + } 1.215 +}; 1.216 + 1.217 +class InStream : public IInStream, private IStreamGetSize, private CMyUnknownImp 1.218 +{ 1.219 + ULONG refCount; 1.220 + 1.221 + HRESULT STDMETHODCALLTYPE QueryInterface(REFGUID, void**) 1.222 + { 1.223 + return E_NOINTERFACE; 1.224 + } 1.225 + 1.226 + HRESULT STDMETHODCALLTYPE GetSize(UInt64* outSize) 1.227 + { 1.228 + if (outSize) 1.229 + { 1.230 + *outSize = size; 1.231 + return S_OK; 1.232 + } 1.233 + else 1.234 + { 1.235 + return E_INVALIDARG; 1.236 + } 1.237 + } 1.238 + 1.239 + MY_ADDREF_RELEASE 1.240 + 1.241 +protected: 1.242 + 1.243 + UINT32 size; 1.244 + 1.245 +public: 1.246 + 1.247 + explicit InStream() : refCount(0) {} 1.248 + virtual ~InStream() {} 1.249 +}; 1.250 + 1.251 + 1.252 +class InFileStream : public InStream 1.253 +{ 1.254 +public: 1.255 + 1.256 + virtual ~InFileStream() 1.257 + { 1.258 + if(file) 1.259 + fclose(file); 1.260 + } 1.261 + 1.262 + FILE* file; 1.263 + 1.264 + InFileStream(const char* fname) : file(NULL) 1.265 + { 1.266 + file = fopen(fname, "rb"); 1.267 + if(file) 1.268 + { 1.269 + fseek(file, 0, SEEK_END); 1.270 + size = ftell(file); 1.271 + fseek(file, 0, SEEK_SET); 1.272 + } 1.273 + } 1.274 + 1.275 + HRESULT STDMETHODCALLTYPE Read(void* data, UInt32 length, UInt32* bytesRead) 1.276 + { 1.277 + if(!file) 1.278 + return E_FAIL; 1.279 + 1.280 + if (data != NULL || length == 0) 1.281 + { 1.282 + int read = fread(data, 1, length, file); 1.283 + 1.284 + if (bytesRead) 1.285 + *bytesRead = read; 1.286 + 1.287 + return S_OK; 1.288 + } 1.289 + else 1.290 + { 1.291 + return E_INVALIDARG; 1.292 + } 1.293 + } 1.294 + 1.295 + HRESULT STDMETHODCALLTYPE Seek(Int64 offset, UInt32 origin, UInt64* pos) 1.296 + { 1.297 + if(!file) 1.298 + return E_FAIL; 1.299 + 1.300 + if (origin < 3) 1.301 + { 1.302 + fseek(file, (long)offset, origin); 1.303 + origin = ftell(file); 1.304 + 1.305 + if (pos) 1.306 + *pos = origin; 1.307 + 1.308 + return S_OK; 1.309 + } 1.310 + else 1.311 + { 1.312 + return E_INVALIDARG; 1.313 + } 1.314 + 1.315 + } 1.316 +}; 1.317 + 1.318 +#endif