summaryrefslogtreecommitdiff
path: root/src/pso/PSOV3Encryption.cpp
blob: 40d80e1f3b95d7290f8ca099a876bc2c5963c4f7 (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
#include <global_types.h>
#include <string.h>
#include "pso/macros.h"
#include "pso/PSOV3Encryption.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;
	m_buffer_start = m_buffer;
	ptr = m_buffer;
	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 = (sizeof(m_buffer)/sizeof(*m_buffer))-1;
	m_buffer_end = &m_buffer[size+1];
	m_buffer_start = m_buffer;
	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;

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

	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[size-1];
}

PSOV3Encryption::~PSOV3Encryption() {
	memset(m_buffer, 0, sizeof(m_buffer));
	m_buffer_start = nullptr;
	m_buffer_end = nullptr;
}

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


PSOEncryption::~PSOEncryption() {

}

PSOEncryption::PSOEncryption() {

}