File: ArchivePool.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (158 lines) | stat: -rw-r--r-- 3,205 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
#include "StdAfx.h"
#include "ArchivePool.h"
#include "FileSystem.h"
#include <algorithm>
#include <stdexcept>
#include "Util.h"
#include "mmgr.h"
#include "LogOutput.h"

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


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 len)
{
	unsigned offset = 0;

	while (true) {
		int i = gzread(file, ((char *)buf)+offset, len-offset);
		if (i == -1) return false;
		offset += i;
		if (offset == len) return true;
	}
}

CArchivePool::CArchivePool(const std::string& name):
	CArchiveBuffered(name),
	isOpen(false)

{
	char c_name[255];
	unsigned char c_md5[16];
	unsigned char c_crc32[4];
	unsigned char c_size[4];

	gzFile in = gzopen (name.c_str(), "rb");
	if (in == NULL) {
		LogObject() << "Error opening " << name;
		return;
	}

	while (true) {
		if (gzeof(in)) {
			isOpen = true;
			break;
		}

		int length = gzgetc(in);
		if (length == -1) 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);
		fileMap[f->name] = f;
	}
	gzclose(in);
}

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

unsigned int CArchivePool::GetCrc32 (const std::string& fileName)
{
	std::string lower = StringToLower(fileName);
	FileData *f = fileMap[lower];
	return f->crc32;
}

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

ABOpenFile_t* CArchivePool::GetEntireFileImpl(const std::string& fName)
{

	if (!isOpen) return NULL;

	std::string fileName = StringToLower(fName);
	if (fileMap.find(fileName) == fileMap.end()) {
		return NULL;
	}

	FileData *f = fileMap[fileName];

	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 = filesystem.LocateFile(rpath);
	gzFile in = gzopen (path.c_str(), "rb");
	if (in == NULL) return NULL;

	ABOpenFile_t* of = new ABOpenFile_t;
	of->size = f->size;
	of->pos = 0;
	of->data = (char*) malloc(of->size);

	int j, i = 0;
	while (true) {
		j = gzread(in, of->data + i, of->size - i);
		if (j == 0) break;
		if (j == -1) {
			gzclose(in);
			delete of;
			return NULL;
		}
		i += j;
	}

	gzclose(in);
	return of;
}

int CArchivePool::FindFiles(int cur, std::string* name, int* size)
{
	if (cur < 0 || static_cast<size_t>(cur) >= files.size()) {
		return 0;
	} else {
		*name = files[cur]->name;
		*size = files[cur]->size;
		return cur + 1;
	}
}