1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/7zip/Common/FileStreams.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,251 @@ 1.4 +// FileStreams.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 + 1.8 +#ifndef _WIN32 1.9 +#include <fcntl.h> 1.10 +#include <unistd.h> 1.11 +#include <errno.h> 1.12 +#endif 1.13 + 1.14 +#include "FileStreams.h" 1.15 + 1.16 +static inline HRESULT ConvertBoolToHRESULT(bool result) 1.17 +{ 1.18 + // return result ? S_OK: E_FAIL; 1.19 + #ifdef _WIN32 1.20 + return result ? S_OK: (::GetLastError()); 1.21 + #else 1.22 + return result ? S_OK: E_FAIL; 1.23 + #endif 1.24 +} 1.25 + 1.26 +bool CInFileStream::Open(LPCTSTR fileName) 1.27 +{ 1.28 + return File.Open(fileName); 1.29 +} 1.30 + 1.31 +#ifdef _WIN32 1.32 +#ifndef _UNICODE 1.33 +bool CInFileStream::Open(LPCWSTR fileName) 1.34 +{ 1.35 + return File.Open(fileName); 1.36 +} 1.37 +#endif 1.38 +#endif 1.39 + 1.40 +STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize) 1.41 +{ 1.42 + #ifdef _WIN32 1.43 + 1.44 + UInt32 realProcessedSize; 1.45 + bool result = File.ReadPart(data, size, realProcessedSize); 1.46 + if(processedSize != NULL) 1.47 + *processedSize = realProcessedSize; 1.48 + return ConvertBoolToHRESULT(result); 1.49 + 1.50 + #else 1.51 + 1.52 + if(processedSize != NULL) 1.53 + *processedSize = 0; 1.54 + ssize_t res = File.Read(data, (size_t)size); 1.55 + if (res == -1) 1.56 + return E_FAIL; 1.57 + if(processedSize != NULL) 1.58 + *processedSize = (UInt32)res; 1.59 + return S_OK; 1.60 + 1.61 + #endif 1.62 +} 1.63 + 1.64 +#ifndef _WIN32_WCE 1.65 +STDMETHODIMP CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize) 1.66 +{ 1.67 + #ifdef _WIN32 1.68 + UInt32 realProcessedSize; 1.69 + BOOL res = ::ReadFile(GetStdHandle(STD_INPUT_HANDLE), 1.70 + data, size, (DWORD *)&realProcessedSize, NULL); 1.71 + if(processedSize != NULL) 1.72 + *processedSize = realProcessedSize; 1.73 + if (res == FALSE && GetLastError() == ERROR_BROKEN_PIPE) 1.74 + return S_OK; 1.75 + return ConvertBoolToHRESULT(res != FALSE); 1.76 + 1.77 + #else 1.78 + 1.79 + if(processedSize != NULL) 1.80 + *processedSize = 0; 1.81 + ssize_t res; 1.82 + do 1.83 + { 1.84 + res = read(0, data, (size_t)size); 1.85 + } 1.86 + while (res < 0 && (errno == EINTR)); 1.87 + if (res == -1) 1.88 + return E_FAIL; 1.89 + if(processedSize != NULL) 1.90 + *processedSize = (UInt32)res; 1.91 + return S_OK; 1.92 + 1.93 + #endif 1.94 +} 1.95 + 1.96 +#endif 1.97 + 1.98 +STDMETHODIMP CInFileStream::Seek(Int64 offset, UInt32 seekOrigin, 1.99 + UInt64 *newPosition) 1.100 +{ 1.101 + if(seekOrigin >= 3) 1.102 + return STG_E_INVALIDFUNCTION; 1.103 + 1.104 + #ifdef _WIN32 1.105 + 1.106 + UInt64 realNewPosition; 1.107 + bool result = File.Seek(offset, seekOrigin, realNewPosition); 1.108 + if(newPosition != NULL) 1.109 + *newPosition = realNewPosition; 1.110 + return ConvertBoolToHRESULT(result); 1.111 + 1.112 + #else 1.113 + 1.114 + off_t res = File.Seek(offset, seekOrigin); 1.115 + if (res == -1) 1.116 + return E_FAIL; 1.117 + if(newPosition != NULL) 1.118 + *newPosition = (UInt64)res; 1.119 + return S_OK; 1.120 + 1.121 + #endif 1.122 +} 1.123 + 1.124 +STDMETHODIMP CInFileStream::GetSize(UInt64 *size) 1.125 +{ 1.126 + return ConvertBoolToHRESULT(File.GetLength(*size)); 1.127 +} 1.128 + 1.129 + 1.130 +////////////////////////// 1.131 +// COutFileStream 1.132 + 1.133 +bool COutFileStream::Create(LPCTSTR fileName, bool createAlways) 1.134 +{ 1.135 + return File.Create(fileName, createAlways); 1.136 +} 1.137 + 1.138 +#ifdef _WIN32 1.139 +#ifndef _UNICODE 1.140 +bool COutFileStream::Create(LPCWSTR fileName, bool createAlways) 1.141 +{ 1.142 + return File.Create(fileName, createAlways); 1.143 +} 1.144 +#endif 1.145 +#endif 1.146 + 1.147 +STDMETHODIMP COutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize) 1.148 +{ 1.149 + #ifdef _WIN32 1.150 + 1.151 + UInt32 realProcessedSize; 1.152 + bool result = File.WritePart(data, size, realProcessedSize); 1.153 + if(processedSize != NULL) 1.154 + *processedSize = realProcessedSize; 1.155 + return ConvertBoolToHRESULT(result); 1.156 + 1.157 + #else 1.158 + 1.159 + if(processedSize != NULL) 1.160 + *processedSize = 0; 1.161 + ssize_t res = File.Write(data, (size_t)size); 1.162 + if (res == -1) 1.163 + return E_FAIL; 1.164 + if(processedSize != NULL) 1.165 + *processedSize = (UInt32)res; 1.166 + return S_OK; 1.167 + 1.168 + #endif 1.169 +} 1.170 + 1.171 +STDMETHODIMP COutFileStream::Seek(Int64 offset, UInt32 seekOrigin, 1.172 + UInt64 *newPosition) 1.173 +{ 1.174 + if(seekOrigin >= 3) 1.175 + return STG_E_INVALIDFUNCTION; 1.176 + #ifdef _WIN32 1.177 + 1.178 + UInt64 realNewPosition; 1.179 + bool result = File.Seek(offset, seekOrigin, realNewPosition); 1.180 + if(newPosition != NULL) 1.181 + *newPosition = realNewPosition; 1.182 + return ConvertBoolToHRESULT(result); 1.183 + 1.184 + #else 1.185 + 1.186 + off_t res = File.Seek(offset, seekOrigin); 1.187 + if (res == -1) 1.188 + return E_FAIL; 1.189 + if(newPosition != NULL) 1.190 + *newPosition = (UInt64)res; 1.191 + return S_OK; 1.192 + 1.193 + #endif 1.194 +} 1.195 + 1.196 +STDMETHODIMP COutFileStream::SetSize(Int64 newSize) 1.197 +{ 1.198 + #ifdef _WIN32 1.199 + UInt64 currentPos; 1.200 + if(!File.Seek(0, FILE_CURRENT, currentPos)) 1.201 + return E_FAIL; 1.202 + bool result = File.SetLength(newSize); 1.203 + UInt64 currentPos2; 1.204 + result = result && File.Seek(currentPos, currentPos2); 1.205 + return result ? S_OK : E_FAIL; 1.206 + #else 1.207 + return E_FAIL; 1.208 + #endif 1.209 +} 1.210 + 1.211 +#ifndef _WIN32_WCE 1.212 +STDMETHODIMP CStdOutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize) 1.213 +{ 1.214 + if(processedSize != NULL) 1.215 + *processedSize = 0; 1.216 + 1.217 + #ifdef _WIN32 1.218 + UInt32 realProcessedSize; 1.219 + BOOL res = TRUE; 1.220 + if (size > 0) 1.221 + { 1.222 + // Seems that Windows doesn't like big amounts writing to stdout. 1.223 + // So we limit portions by 32KB. 1.224 + UInt32 sizeTemp = (1 << 15); 1.225 + if (sizeTemp > size) 1.226 + sizeTemp = size; 1.227 + res = ::WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), 1.228 + data, sizeTemp, (DWORD *)&realProcessedSize, NULL); 1.229 + size -= realProcessedSize; 1.230 + data = (const void *)((const Byte *)data + realProcessedSize); 1.231 + if(processedSize != NULL) 1.232 + *processedSize += realProcessedSize; 1.233 + } 1.234 + return ConvertBoolToHRESULT(res != FALSE); 1.235 + 1.236 + #else 1.237 + 1.238 + ssize_t res; 1.239 + do 1.240 + { 1.241 + res = write(1, data, (size_t)size); 1.242 + } 1.243 + while (res < 0 && (errno == EINTR)); 1.244 + if (res == -1) 1.245 + return E_FAIL; 1.246 + if(processedSize != NULL) 1.247 + *processedSize = (UInt32)res; 1.248 + return S_OK; 1.249 + 1.250 + return S_OK; 1.251 + #endif 1.252 +} 1.253 + 1.254 +#endif