pastebin

Paste #gXI -- näytä pelkkänä tekstinä -- uusi tämän pohjalta

Värjäys: Tyyli: ensimmäinen rivinumero: Tabin korvaus:

 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
#define MTHASHSET_FILE_SIGNATURE 0x4d544853 // 'MTHS'

/*
        Minetest Hashset File Format

        All values are stored in big-endian byte order.
        [u32] signature: 'MTHS'
        [u16] version: 1
        For each hash in set:
                [u8*20] SHA1 hash

        Version changes:
        1 - Initial version
*/

bool ClientMediaDownloader::loadHashSet(const std::string &data,
		std::set<std::string> &result)
{
	if (data.size() % 20 != 6) {
                errorstream << "loadHashSet: invalid hash set file"
			<< std::endl;
                return false;
	}

        u32 signature = readU32(&data[0]);
        if (signature != MTHASHSET_FILE_SIGNATURE) {
                errorstream << "loadHashSet: invalid hash set file"
                        << std::endl;
                return false;
        }

        u16 version = readU16(&data[4]);
        if (version != 1) {
                errorstream << "loadHashSet: unsupported hash set "
                        "file version" << std::endl;
                return false;
        }

	for (u32 pos = 6; pos < data.size(); pos += 20) {
		result.push_back(data.substr(pos, 20));
	}
	return true;
}

bool ClientMediaDownloader::saveHashSet(const std::set<std::string> &hashset,
		std::ostream &os)
{
	writeU32(os, MTHASHSET_FILE_SIGNATURE); // signature
	writeU16(os, 1);                        // version

	// list of hashes
	for (std::set<std::string>::const_iterator it = hashset.begin();
			it != hashset.end(); ++it) {
		assert(it->size() == 20);
		os << (*it);
	}

	return true;
}