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

RAR.cpp - github.com/mpc-hc/rarfilesource.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aa13ba6b3eada082ece55fa9cdc95941c660a7e5 (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, 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; version 2 of the License.
 *
 * 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 "rar.h"
#include "utils.h"

#define READ_ITEM(item) READ_ITEM2(&item, sizeof(item))

#define READ_ITEM2(item, size) { \
	if (!ReadFile (file, item, size, &read, NULL)) \
	{ \
		ErrorMsg (GetLastError (), L"Could not read RAR header"); \
		return S_FALSE; \
	} \
	if (read < size) return ERROR_HANDLE_EOF; \
	acc += read; }

DWORD ReadHeader (HANDLE file, rar_header_t *dest)
{
	fixed_header_t fh;
	fixed_file_header_t ffh;
	DWORD read, dword;
	LONGLONG acc = 0;

	// Read fixed archive header.
	READ_ITEM (fh);

	dest->ch.crc = fh.crc;
	dest->ch.flags = fh.flags;
	dest->ch.type = fh.type;

	switch (fh.type)
	{
	case HEADER_TYPE_FILE:
		READ_ITEM (ffh);

		dest->ch.size.QuadPart = (LONGLONG) ffh.packedSize + fh.size;
		dest->fh.size.LowPart = ffh.size;
		dest->fh.os = ffh.os;
		dest->fh.crc = ffh.crc;
		dest->fh.timestamp = ffh.timestamp;
		dest->fh.version = ffh.version;
		dest->fh.method = ffh.method;
		dest->fh.name_len = ffh.name_len;
		dest->fh.attributes = ffh.attributes;

		if (fh.flags & LHD_LARGE)
		{
			READ_ITEM (dword); // Packed size high dword
			dest->ch.size.HighPart += dword;
			READ_ITEM (dword); // Unpacked size high dword
			dest->fh.size.HighPart = dword;
		}
		else
			dest->fh.size.HighPart = 0;

		dest->fh.filename = new char [dest->fh.name_len + 1];
		if (!dest->fh.filename)
		{
			ErrorMsg (0, L"Out of memory while reading RAR header.");
			return ERROR_OUTOFMEMORY;
		}
		READ_ITEM2 (dest->fh.filename, dest->fh.name_len);
		dest->fh.filename [dest->fh.name_len] = 0;

		if (fh.flags & LHD_SALT)
			READ_ITEM2 (dest->fh.salt, 8);

		if (fh.flags & LHD_EXTTIME)
		{
			WORD flags;
			READ_ITEM (flags);
			for (int i= 0 ; i < 4; i++)
			{
				DWORD rmode = flags >> (3 - i) * 4;
				if (!(rmode & 8))
					continue;

				if (i != 0)
				{
					DWORD dosTime;
					READ_ITEM (dosTime);
				}

				int count = rmode & 3;
				for (int j = 0; j < count; j++)
				{
					BYTE byte;
					READ_ITEM (byte);
				}
			}
		}
		break;

	default:
		if (fh.flags & LONG_BLOCK)
		{
			READ_ITEM (dword);
			dest->ch.size.QuadPart = (LONGLONG) dword + fh.size;
		}
		else
		{
			dest->ch.size.HighPart = 0;
			dest->ch.size.LowPart = fh.size;
		}
	}

	if (acc > dest->ch.size.QuadPart)
	{
		ErrorMsg (0, L"Overrun while reading RAR header.");
		return S_FALSE;
	}

	if (fh.type != HEADER_TYPE_FILE && acc < dest->ch.size.QuadPart)
	{
		LARGE_INTEGER li;
		li.QuadPart = dest->ch.size.QuadPart - acc;
		SetFilePointerEx (file, li, NULL, FILE_CURRENT);
		dest->bytesRemaining.QuadPart = 0;
	}
	else
		dest->bytesRemaining.QuadPart = dest->ch.size.QuadPart - acc;

	return ERROR_SUCCESS;
}