summaryrefslogtreecommitdiff
path: root/src/pso/PSOV3Encryption.cpp
blob: 38225bf47d311adaef21a3e901bb53e20b26b500 (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
#include <global_types.h>
#include <string.h>
#include "pso/macros.h"
#include "pso/PSOV3Encryption.h"
#include "pso/TArray.h"

u32 PSOV3Encryption::next() {
	if (++m_buffer_start == m_buffer_end) {
		update_stream();
	}
	return *m_buffer_start;
}

void PSOV3Encryption::update_stream() {
	u32 *ptr;
	u32 *part1;
	u32 *part2;

	part2 = m_buffer.start();
	m_buffer_start = m_buffer.start();
	ptr = m_buffer.start();
	part1 = &ptr[489];

	for (; part1 != m_buffer_end; *ptr++ ^= *part1++);
	for (; ptr != m_buffer_end; *ptr++ ^= *part2++);

}

void PSOV3Encryption::init(u32 seed) {
	const size_t size = m_buffer.size();
	u32 thing;
	m_buffer_end = &m_buffer[size];
	m_buffer_start = m_buffer.start();
	u32 value = 0;

	for (int i = 0; i <= 16; ++i, *m_buffer_start++ = value) {
		for (int j = 32; j; --j) {
			seed *= 0x5d588b65;
			value = (++seed & (1 << 31)) | (value >> 1);
		}
	}

	--m_buffer_start;
	thing = m_buffer[0xf];
	*m_buffer_start = (m_buffer[0] >> 9) ^ (*m_buffer_start << 23) ^ thing;

	for (u32 *buf_val = &m_buffer[0], *next_buf_val = &m_buffer[1], *buf = m_buffer_start++; m_buffer_start != m_buffer_end;) {
		*m_buffer_start++ = (*buf_val++ << 23) ^ (*next_buf_val++ >> 9) ^ *buf++;
	}

	update_stream();
	update_stream();
	update_stream();
	m_buffer_start = m_buffer.end();
}

PSOV3Encryption::~PSOV3Encryption() {
	m_buffer.fill(0);
	m_buffer_start = nullptr;
	m_buffer_end = nullptr;
}

PSOV3Encryption::PSOV3Encryption() : PSOEncryption() {
	init(0);
}


PSOEncryption::~PSOEncryption() {

}

PSOEncryption::PSOEncryption() {

}