File: PoolArchive.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (159 lines) | stat: -rwxr-xr-x 3,308 bytes parent folder | download
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "PoolArchive.h"

#include <algorithm>
#include <stdexcept>
#include <sstream>
#include <string>
#include <cstring>
#include <iostream>

#include "DataDirsAccess.h"
#include "FileSystem.h"
#include "System/Util.h"
#include "System/mmgr.h"
#include "System/Log/ILog.h"


CPoolArchiveFactory::CPoolArchiveFactory()
	: IArchiveFactory("sdp")
{
}

IArchive* CPoolArchiveFactory::DoCreateArchive(const std::string& filePath) const
{
	return new CPoolArchive(filePath);
}


static unsigned int parse_int32(unsigned char c[4])
{
	unsigned int i = 0;
	i = c[0] << 24 | i;
	i = c[1] << 16 | i;
	i = c[2] << 8  | i;
	i = c[3] << 0  | i;
	return i;
}

static bool gz_really_read(gzFile file, voidp buf, unsigned int len)
{
	return gzread(file, (char*)buf, len) == len;
}

CPoolArchive::CPoolArchive(const std::string& name)
	: CBufferedArchive(name)
	, isOpen(false)
{
	char c_name[255];
	unsigned char c_md5[16];
	unsigned char c_crc32[4];
	unsigned char c_size[4];
	unsigned char length;

	gzFile in = gzopen(name.c_str(), "rb");
	if (in == NULL) {
		LOG_L(L_ERROR, "couldn't open %s", name.c_str());
		return;
	}

	while (true) {

		if (!gz_really_read(in, &length, 1)) {
			if (gzeof(in)) {
				isOpen = true;
			}
			break;
		}
		if (!gz_really_read(in, &c_name, length)) break;
		if (!gz_really_read(in, &c_md5, 16)) break;
		if (!gz_really_read(in, &c_crc32, 4)) break;
		if (!gz_really_read(in, &c_size, 4)) break;

		FileData* f = new FileData;
		f->name = std::string(c_name, length);
		std::memcpy(&f->md5, &c_md5, 16);
		f->crc32 = parse_int32(c_crc32);
		f->size = parse_int32(c_size);

		files.push_back(f);
		lcNameIndex[f->name] = files.size() - 1;
	}
	gzclose(in);
}

CPoolArchive::~CPoolArchive()
{
	std::vector<FileData*>::iterator fi;
	for (fi = files.begin(); fi < files.end(); ++fi) {
		delete *fi;
	}
}

bool CPoolArchive::IsOpen()
{
	return isOpen;
}

unsigned int CPoolArchive::NumFiles() const
{
	return files.size();
}

void CPoolArchive::FileInfo(unsigned int fid, std::string& name, int& size) const
{
	assert(IsFileId(fid));
	name = files[fid]->name;
	size = files[fid]->size;
}

unsigned int CPoolArchive::GetCrc32(unsigned int fid)
{
	assert(IsFileId(fid));
	return files[fid]->crc32;
}


bool CPoolArchive::GetFileImpl(unsigned int fid, std::vector<boost::uint8_t>& buffer)
{
	assert(IsFileId(fid));

	FileData* f = files[fid];

	char table[] = "0123456789abcdef";
	char c_hex[32];
	for (int i = 0; i < 16; ++i) {
		c_hex[2 * i]     = table[(f->md5[i] >> 4) & 0xf];
		c_hex[2 * i + 1] = table[ f->md5[i]       & 0xf];
	}
	std::string prefix(c_hex,      2);
	std::string postfix(c_hex + 2, 30);

	std::ostringstream accu;
	accu << "pool/" << prefix << "/" << postfix << ".gz";
	std::string rpath = accu.str();

	FileSystem::FixSlashes(rpath);
	std::string path = dataDirsAccess.LocateFile(rpath);
	gzFile in = gzopen(path.c_str(), "rb");
	if (in == NULL){
		LOG_L(L_ERROR, "couldn't open %s", path.c_str());
		return false;
	}

	unsigned int len = f->size;
	buffer.resize(len);

	int bytesread = (len == 0) ? 0 : gzread(in, (char *)&buffer[0], len);
	gzclose(in);

	if (bytesread != len) {
		LOG_L(L_ERROR, "couldn't read %s", path.c_str());
		buffer.clear();
		return false;
	}

	return true;
}