File: ZipMemFile.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 (110 lines) | stat: -rw-r--r-- 3,028 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
////////////////////////////////////////////////////////////////////////////////
// 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 "ZipMemFile.h"
#include "ZipException.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

void CZipMemFile::Grow(size_t nGrowTo)
{
	if (m_nBufSize < (UINT)nGrowTo)
	{
		if (m_nGrowBy == 0)
			CZipException::Throw(CZipException::memError);
		size_t nNewSize = m_nBufSize;
		while (nNewSize < nGrowTo)
			nNewSize += m_nGrowBy;
		BYTE* lpNew;
		if (m_lpBuf)
			lpNew = (BYTE*)realloc((void*) m_lpBuf, nNewSize);
		else
			lpNew = (BYTE*)malloc(nNewSize);

		if (!lpNew)
			CZipException::Throw(CZipException::memError);
		m_nBufSize = nNewSize;
		m_lpBuf = lpNew;
	}
} 

void CZipMemFile::SetLength(ZIP_FILE_USIZE nNewLen)
{
	if (m_nBufSize < (size_t)nNewLen)
		Grow((size_t)nNewLen);
	else
		m_nPos = (size_t)nNewLen;
	m_nDataSize = (size_t)nNewLen;
}

UINT CZipMemFile::Read(void *lpBuf, UINT nCount)
{
	if (m_nPos >= m_nDataSize)
		return 0;
	UINT nToRead = (m_nPos + nCount > m_nDataSize) ? (UINT)(m_nDataSize - m_nPos) : nCount;
	memcpy(lpBuf, m_lpBuf + m_nPos, nToRead);
	m_nPos += nToRead;
	return nToRead;

}

void CZipMemFile::Write(const void *lpBuf, UINT nCount)
{
	if (!nCount)
		return;

	if (m_nPos + nCount > m_nBufSize)
		Grow(m_nPos + nCount);
	memcpy(m_lpBuf + m_nPos, lpBuf, nCount);
	m_nPos += nCount;
	if (m_nPos > m_nDataSize)
		m_nDataSize = m_nPos;
}
ZIP_FILE_USIZE CZipMemFile::Seek(ZIP_FILE_SIZE lOff, int nFrom)
{
	ZIP_FILE_USIZE lNew = m_nPos;

	if (nFrom == CZipAbstractFile::begin)
	{
		if (lOff < 0)
			CZipException::Throw(CZipException::memError);
		lNew = lOff;
	}
	else if (nFrom == CZipAbstractFile::current)
	{
		if (lOff < 0 && (ZIP_FILE_USIZE)(-lOff) > lNew)
			CZipException::Throw(CZipException::memError);
		lNew += lOff;
	}
	else if (nFrom == CZipAbstractFile::end)
	{
		if (lOff < 0 && ZIP_FILE_USIZE(-lOff) > m_nDataSize)
			CZipException::Throw(CZipException::memError);
		lNew = m_nDataSize + lOff;
	}
	else
		return lNew;

	// assumption that size_t is always signed
	if (lNew > (size_t)(-1)) // max of size_t
		CZipException::Throw(CZipException::memError);
	if (lNew > m_nDataSize)
		Grow((size_t)lNew);
	
	m_nPos = (size_t)lNew;
	return lNew;
}