File: ZipException.cpp

package info (click to toggle)
tuxcmd-modules 0.6.70%2Bds-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 1,872 kB
  • ctags: 2,807
  • sloc: cpp: 11,743; ansic: 8,064; makefile: 245
file content (279 lines) | stat: -rw-r--r-- 7,489 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2007 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ZipException.h"
#include "zlib/zlib.h"
#include <errno.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#if defined _MFC_VER && defined ZIP_ARCHIVE_MFC
	IMPLEMENT_DYNAMIC( CZipException, CException)
#endif

CZipException::CZipException(int iCause, LPCTSTR lpszZipName)
#ifdef _MFC_VER
	:CException(TRUE)
#endif
{
	m_iCause = iCause;

	if (lpszZipName)
		m_szFileName = lpszZipName;	
}

CZipException::~CZipException() throw()
{

}

// inline void CZipException::Throw(int iZipError, LPCTSTR lpszZipName)
// {
// #ifdef _MFC_VER
// 	throw new CZipException(iZipError, lpszZipName);
// #else
// 	CZipException e(iZipError, lpszZipName);
// 	throw e;
// #endif
// MSVC++: ignore "Unreachable code" warning here, it's due to 
// optimizations
// }


#ifdef ZIP_ENABLE_ERROR_DESCRIPTION

ZBOOL CZipException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
	UINT* )

{
	if (!lpszError || !nMaxError)
		return FALSE;
	CZipString sz = GetErrorDescription();
	if (sz.IsEmpty())
		return FALSE;
	UINT iLen = sz.GetLength();
	if (nMaxError - 1 < iLen)
		iLen = nMaxError - 1;
	LPTSTR lpsz = sz.GetBuffer(iLen);
#if _MSC_VER >= 1400
	#ifdef _UNICODE	
		wcsncpy_s(lpszError, nMaxError, lpsz, iLen);
	#else
		strncpy_s(lpszError, nMaxError, lpsz, iLen);
	#endif
#else
	#ifdef _UNICODE	
		wcsncpy(lpszError, lpsz, iLen);
	#else
		strncpy(lpszError, lpsz, iLen);
	#endif
#endif

	lpszError[iLen] = _T('\0');
	return TRUE;
}


CZipString CZipException::GetErrorDescription()
{
	return GetInternalErrorDescription(m_iCause);
}


CZipString CZipException::GetSystemErrorDescription()
{
#ifdef WIN32
	DWORD x = GetLastError();
	if (x)
	{
		LPVOID lpMsgBuf;
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,    
			          NULL, x, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
				      (LPTSTR) &lpMsgBuf, 0, NULL);
		CZipString sz = (LPCTSTR)lpMsgBuf;
		LocalFree(lpMsgBuf);
		return sz;
	}
#endif
	return GetInternalErrorDescription(errno == 0 ? genericError : errno, true);
}

CZipString CZipException::GetInternalErrorDescription(int iCause, bool bNoLoop)
{
	CZipString sz;
	switch (iCause)
	{
		case EROFS:
			sz = _T("Read-only file system.");
			break;
		case ESPIPE:
			sz = _T("Illegal seek.");
			break;
		case ENOSPC:
			sz = _T("No space left on device.");
			break;
		case EFBIG:
			sz = _T("File too large.");
			break;
		case EMFILE:
			sz = _T("Too many open files.");
			break;
		case ENFILE:
			sz = _T("File table overflow.");
			break;
		case EINVAL:
			sz = _T("Invalid argument.");
			break;
		case EISDIR:
			sz = _T("Is a directory.");
			break;
		case ENOTDIR:
			sz = _T("Not a directory.");
			break;
		case ENODEV:
			sz = _T("No such device.");
			break;
		case EXDEV:
			sz = _T("Cross-device link.");
			break;
		case EEXIST:
			sz = _T("File exists.");
			break;
		case EFAULT:
			sz = _T("Bad address.");
			break;
		case EACCES:
			sz = _T("Permission denied.");
			break;
		case ENOMEM:
			sz = _T("Not enough space.");
			break;
		case EBADF:
			sz = _T("Bad file number.");
			break;
		case ENXIO:
			sz = _T("No such device or address.");
			break;
		case EIO:
			sz = _T("I/O error.");
			break;
		case EINTR:
			sz = _T("Interrupted system call.");
			break;
		case ENOENT:
			sz = _T("No such file or directory.");
			break;
		case EPERM:
			sz = _T("Not super-user.");
			break;
		case badZipFile:
			sz = _T("Damaged or not a zip file.");
			break;
		case badCrc:
			sz = _T("Crc is mismatched.");
			break;
		case noCallback:
			sz = _T("There is no spanned archive callback object set.");
			break;
		case aborted:
			sz = _T("Volume change aborted in a segmented archive.");
			break;
		case abortedAction:
			sz = _T("Action aborted.");
			break;
		case abortedSafely:
			sz = _T("Action aborted safely.");
			break;
		case nonRemovable:
			sz = _T("The device selected for the spanned archive is not removable.");
			break;
		case tooManyVolumes:
			sz = _T("The limit of the maximum volumes reached.");
			break;
		case tooManyFiles:
			sz = _T("The limit of the maximum files in an archive reached.");
			break;
		case tooLongData:
			sz = _T("The filename, the comment or the local or central extra field of the file added to the archive is too long.");
			break;
		case tooBigSize:
			sz = _T("The file size is too large to be supported.");
			break;
		case badPassword:
			sz = _T("An incorrect password set for the file being decrypted.");
			break;
		case dirWithSize:
			sz = _T("The directory with a non-zero size found while testing.");
			break;
		case internalError:
			sz = _T("An internal error.");
			break;
		case notRemoved:
			sz.Format(_T("%s (%s)."), _T("Error while removing a file"), (LPCTSTR)GetSystemErrorDescription());
			break;
		case notRenamed:
			sz.Format(_T("%s (%s)."), _T("Error while renaming a file"), (LPCTSTR)GetSystemErrorDescription());
			break;
		case platfNotSupp:
			sz = _T("Cannot create a file for the specified platform.");
			break;
		case cdirNotFound:
			sz = _T("The central directory was not found in the archive (or you were trying to open not the last volume of a segmented archive).");
			break;
		case noZip64:
			sz = _T("The Zip64 format has not been enabled for the library, but is required to use the archive.");
			break;
		case noAES:
			sz = _T("WinZip AES encryption has not been enabled for the library, but is required to decompress the archive.");
			break;
#ifdef ZIP_ARCHIVE_STL
			case outOfBounds:
			sz = _T("The collection is empty and the bounds do not exist.");
			break;
#endif
#ifdef ZIP_ARCHIVE_USE_LOCKING
		case mutexError:
			sz = _T("Locking or unlocking resources access was unsuccessful.");
			break;
#endif
		case streamEnd:
			sz = _T("Zlib library error (end of stream).");
			break;
		case errNo:
			sz = GetInternalErrorDescription(errno != errNo ? errno : genericError);
			break;
		case streamError:
			sz = _T("Zlib library error (stream error).");
			break;
		case dataError:
			sz = _T("Zlib library error (data error).");
			break;
		case memError:
			sz = _T("Not enough memory.");
			break;
		case bufError:
			sz = _T("Zlib library error (buffer error).");
			break;
		case versionError:
			sz = _T("Zlib library error (version error).");
			break;
		default:
			sz = bNoLoop ? _T("Unknown error") :(LPCTSTR) GetSystemErrorDescription();
	}
	return sz;
}

#endif //ZIP_ENABLE_ERROR_DESCRIPTION