Welcome to mirror list, hosted at ThFree Co, Russian Federation.

File.cpp - github.com/mpc-hc/rarfilesource.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4bc3c835aab4736077b00765def830fd6a008389 (plain)
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
/*
 * Copyright (C) 2008-2012, OctaneSnail <os@v12pwr.com>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <windows.h>
#include <streams.h>

#include "File.h"
#include "Utils.h"

static int compare (const void *pos, const void *part)
{
	if (*((LONGLONG *) pos) < ((CRFSFilePart *) part)->in_file_offset)
		return -1;

	if (*((LONGLONG *) pos) >= ((CRFSFilePart *) part)->in_file_offset + ((CRFSFilePart *) part)->size)
		return 1;

	return 0;
}

int CRFSFile::FindStartPart (LONGLONG position)
{
	if (position > size)
		return -1;

	// Check if the previous lookup up still matches.
	if (m_prev_part && !compare (&position, m_prev_part))
		return (int) (m_prev_part - array);

	m_prev_part = (CRFSFilePart *) bsearch (&position, array, parts, sizeof (CRFSFilePart), compare);

	if (!m_prev_part)
		return -1;

	return (int) (m_prev_part - array);
}

HRESULT CRFSFile::SyncRead (LONGLONG llPosition, DWORD lLength, BYTE* pBuffer, LONG *cbActual)
{
	OVERLAPPED o;
	LARGE_INTEGER offset;
	DWORD to_read, read, acc = 0;
	LONGLONG offset2;
	int pos;
#ifdef _DEBUG
	static int last_pos = -1;
#endif

	if (!pBuffer)
		return E_POINTER;

	pos = FindStartPart (llPosition);
	if (pos == -1)
	{
		DbgLog((LOG_TRACE, 2, L"FindStartPart bailed length = %lu, pos = %lld", lLength, llPosition));
		return S_FALSE;
	}

#ifdef _DEBUG
	if (pos != last_pos)
	{
		DbgLog((LOG_TRACE, 2, L"Now reading volume %d.", pos));
		last_pos = pos;
	}
#endif
	CRFSFilePart *part = array + pos;

	offset2 = llPosition - part->in_file_offset;
	offset.QuadPart = part->in_rar_offset + offset2;

	memset (&o, 0, sizeof (o));

	if (!(o.hEvent = CreateEvent (NULL, FALSE, FALSE, NULL)))
	{
		ErrorMsg (GetLastError (), L"CRFSOutputPin::SyncRead - CreateEvent");
		return S_FALSE;
	}

	while (true)
	{
		read = 0;
		to_read = min (lLength, (DWORD) (part->size - offset2));

		o.Offset = offset.LowPart;
		o.OffsetHigh = offset.HighPart;

		if (!ReadFile (part->file, pBuffer + acc, to_read, NULL, &o))
		{
			DWORD err = GetLastError ();

			if (err != ERROR_IO_PENDING)
			{
				ErrorMsg (err, L"CRFSOutputPin::SyncRead - ReadFile");
				break;
			}
		}
		if (!GetOverlappedResult (part->file, &o, &read, TRUE))
		{
			ErrorMsg (GetLastError (), L"CRFSOutputPin::SyncRead - GetOverlappedResult");
			break;
		}
		lLength -= read;
		acc += read;

		if (lLength == 0)
		{
			CloseHandle (o.hEvent);
			if (cbActual)
				*cbActual = acc;
			return S_OK;
		}

		pos ++;

		if (pos >= parts)
			break;

		part ++;
		offset2 = 0;
		offset.QuadPart = part->in_rar_offset;
	}

	CloseHandle (o.hEvent);
	if (cbActual)
		*cbActual = acc;
	return S_FALSE;
}