PyCriCodecsEx 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ #define PY_SSIZE_T_CLEAN
2
+ #pragma once
3
+ #include <Python.h>
4
+ #include "adx.cpp"
5
+ #include "crilayla.cpp"
6
+ #include "hca.cpp"
7
+
8
+ static struct PyMethodDef Codec_methods[] = {
9
+ { "AdxDecode", (PyCFunction)AdxDecode, METH_O, nullptr },
10
+ { "AdxEncode", (PyCFunction)AdxEncode, METH_VARARGS, nullptr },
11
+ { "CriLaylaDecompress", (PyCFunction)CriLaylaDecompress, METH_O, nullptr },
12
+ { "CriLaylaCompress", (PyCFunction)CriLaylaCompress, METH_VARARGS, nullptr },
13
+ { "HcaDecode", (PyCFunction)HcaDecode, METH_VARARGS, nullptr },
14
+ { "HcaEncode", (PyCFunction)HcaEncode, METH_VARARGS, nullptr },
15
+ { "HcaCrypt", (PyCFunction)HcaCrypt, METH_VARARGS, nullptr },
16
+ { nullptr, nullptr, 0, nullptr }
17
+ };
18
+
19
+ static PyModuleDef CriCodecsEx_module = {
20
+ PyModuleDef_HEAD_INIT,
21
+ "CriCodecs",
22
+ "Faster Decoding and Encoding with C++",
23
+ 0,
24
+ Codec_methods
25
+ };
26
+
27
+ PyMODINIT_FUNC PyInit_CriCodecsEx() {
28
+ return PyModule_Create(&CriCodecsEx_module);
29
+ }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * @file IO.cpp
3
+ * @brief Read/Write handler for cross-platform endiannes.
4
+ * Most of what's here is ports from other libraries. VGAudio and VGMStream to name a few.
5
+ */
6
+
7
+ /* Includes */
8
+ #include "IO.hpp"
9
+ #include <algorithm>
10
+
11
+ void WriteChar(unsigned char* Buffer, unsigned char Value){
12
+ Buffer[0] = Value;
13
+ }
14
+
15
+ void WriteShortLE(unsigned char* Buffer, unsigned short Value){
16
+ Buffer[0] = Value & 0xFF;
17
+ Buffer[1] = Value >> 8;
18
+ }
19
+
20
+ void WriteShortBE(unsigned char* Buffer, unsigned short Value){
21
+ Buffer[0] = Value >> 8;
22
+ Buffer[1] = Value & 0xFF;
23
+ }
24
+
25
+ void WriteIntLE(unsigned char* Buffer, unsigned int Value){
26
+ Buffer[0] = (unsigned char)(Value & 0xFF);
27
+ Buffer[1] = (unsigned char)((Value >> 8) & 0xFF);
28
+ Buffer[2] = (unsigned char)((Value >> 16) & 0xFF);
29
+ Buffer[3] = (unsigned char)((Value >> 24) & 0xFF);
30
+ }
31
+
32
+ void WriteIntBE(unsigned char* Buffer, unsigned int Value){
33
+ Buffer[0] = (unsigned char)((Value >> 24) & 0xFF);
34
+ Buffer[1] = (unsigned char)((Value >> 16) & 0xFF);
35
+ Buffer[2] = (unsigned char)((Value >> 8) & 0xFF);
36
+ Buffer[3] = (unsigned char)(Value & 0xFF);
37
+ }
38
+
39
+ unsigned long long BitReader::GetBitsRemaining(){
40
+ return Length - Position;
41
+ }
42
+
43
+ void BitReader::SetBuffer(unsigned char* buffer, unsigned int size){
44
+ Buffer = buffer;
45
+ Length = size * 8;
46
+ Position = 0;
47
+ }
48
+
49
+ int BitReader::ReadInt(int BitCount){
50
+ int value = PeekInt(BitCount);
51
+ Position += BitCount;
52
+ return value;
53
+ }
54
+
55
+ int BitReader::ReadSignedInt(int BitCount){
56
+ int value = PeekInt(BitCount);
57
+ Position += BitCount;
58
+ return SignExtend(value, BitCount);
59
+ }
60
+
61
+ template<typename T> T BitReader::SignExtend(T value, int bits){
62
+ int shift = sizeof(T) * 8 - bits;
63
+ return (value << shift) >> shift;
64
+ }
65
+
66
+ void BitReader::AlignPosition(int multiple){
67
+ Position = GetNextMultiple(Position, multiple);
68
+ }
69
+
70
+ int BitReader::PeekInt(int BitCount){
71
+ unsigned long long Remaining = GetBitsRemaining();
72
+ if (BitCount > Remaining){
73
+ if (Position >= Length) return 0;
74
+ int ExtraBits = BitCount - Remaining;
75
+ return PeekIntFallback(Remaining) << ExtraBits;
76
+ }
77
+ int ByteIndex = Position / 8;
78
+ int BitIndex = Position % 8;
79
+ if (BitCount <= 9 && Remaining >= 16){
80
+ int value = Buffer[ByteIndex] << 8 | Buffer[ByteIndex + 1];
81
+ value &= 0xFFFF >> BitIndex;
82
+ value >>= 16 - BitCount - BitIndex;
83
+ return value;
84
+ }
85
+ if (BitCount <= 17 && Remaining >= 24){
86
+ int value = Buffer[ByteIndex] << 16 | Buffer[ByteIndex + 1] << 8 | Buffer[ByteIndex + 2];
87
+ value &= 0xFFFFFF >> BitIndex;
88
+ value >>= 24 - BitCount - BitIndex;
89
+ return value;
90
+ }
91
+ if (BitCount <= 25 && Remaining >= 32){
92
+ int value = Buffer[ByteIndex] << 24 | Buffer[ByteIndex + 1] << 16 | Buffer[ByteIndex + 2] << 8 | Buffer[ByteIndex + 3];
93
+ value &= (int)(0xFFFFFFFF >> BitIndex);
94
+ value >>= 32 - BitCount - BitIndex;
95
+ return value;
96
+ }
97
+ return PeekIntFallback(BitCount);
98
+ }
99
+
100
+ int BitReader::PeekIntFallback(int BitCount){
101
+ int value = 0;
102
+ int ByteIndex = Position / 8;
103
+ int BitIndex = Position % 8;
104
+ while (BitCount > 0){
105
+ if (BitIndex >= 8){
106
+ BitIndex = 0;
107
+ ByteIndex++;
108
+ }
109
+ int bitsToRead = std::min(BitCount, 8 - BitIndex);
110
+ int mask = 0xFF >> BitIndex;
111
+ int currentByte = (mask & Buffer[ByteIndex]) >> (8 - BitIndex - bitsToRead);
112
+ value = (value << bitsToRead) | currentByte;
113
+ BitIndex += bitsToRead;
114
+ BitCount -= bitsToRead;
115
+ }
116
+ return value;
117
+ }
118
+
119
+ void BitWriter::SetBuffer(unsigned char* buffer, unsigned int size){
120
+ Buffer = buffer;
121
+ Length = size * 8;
122
+ Position = 0;
123
+ }
124
+
125
+ unsigned long long BitWriter::GetBitsRemaining(){
126
+ return Length - Position;
127
+ }
128
+
129
+ void BitWriter::Write(int value, int BitCount){
130
+ unsigned long long Remaining = GetBitsRemaining();
131
+ if(BitCount < 0 || BitCount > 32)
132
+ return;
133
+ else if(BitCount > Remaining)
134
+ return;
135
+ int ByteIndex = Position / 8;
136
+ int BitIndex = Position % 8;
137
+ if (BitCount <= 9 && GetBitsRemaining() >= 16){
138
+ unsigned int outValue = ((value << (16 - BitCount)) & 0xFFFF) >> BitIndex;
139
+ Buffer[ByteIndex] |= (unsigned char)(outValue >> 8);
140
+ Buffer[ByteIndex + 1] = (unsigned char)outValue;
141
+ }else if (BitCount <= 17 && GetBitsRemaining() >= 24){
142
+ int outValue = ((value << (24 - BitCount)) & 0xFFFFFF) >> BitIndex;
143
+ Buffer[ByteIndex] |= (unsigned char)(outValue >> 16);
144
+ Buffer[ByteIndex + 1] = (unsigned char)(outValue >> 8);
145
+ Buffer[ByteIndex + 2] = (unsigned char)outValue;
146
+ }else if (BitCount <= 25 && GetBitsRemaining() >= 32){
147
+ unsigned int outValue = (unsigned int)(((value << (32 - BitCount)) & 0xFFFFFFFF) >> BitIndex);
148
+ Buffer[ByteIndex] |= (unsigned char)(outValue >> 24);
149
+ Buffer[ByteIndex + 1] = (unsigned char)(outValue >> 16);
150
+ Buffer[ByteIndex + 2] = (unsigned char)(outValue >> 8);
151
+ Buffer[ByteIndex + 3] = (unsigned char)outValue;
152
+ }else{
153
+ WriteFallback(value, BitCount);
154
+ }
155
+ Position += BitCount;
156
+ }
157
+
158
+ void BitWriter::AlignPosition(int multiple){
159
+ int newPosition = GetNextMultiple(Position, multiple);
160
+ int bits = newPosition - Position;
161
+ Write(0, bits);
162
+ }
163
+
164
+ void BitWriter::WriteFallback(int value, int BitCount){
165
+ int ByteIndex = Position / 8;
166
+ int BitIndex = Position % 8;
167
+ while(BitCount > 0){
168
+ if(BitIndex >= 8){
169
+ BitIndex = 0;
170
+ ByteIndex++;
171
+ }
172
+ int toShift = 8 - BitIndex - BitCount;
173
+ int shifted = toShift < 0 ? value >> -toShift : value << toShift;
174
+ int bitsToWrite = std::min(BitCount, 8 - BitIndex);
175
+ int mask = ((1 << bitsToWrite) - 1) << 8 - BitIndex - bitsToWrite;
176
+ int outByte = Buffer[ByteIndex] & ~mask;
177
+ outByte |= shifted & mask;
178
+ Buffer[ByteIndex] = (unsigned char)outByte;
179
+ BitIndex += bitsToWrite;
180
+ BitCount -= bitsToWrite;
181
+ }
182
+ }
@@ -0,0 +1,77 @@
1
+ static inline short ReadShortBE(const unsigned char* ptr){
2
+ return (ptr[0] << 8) | ptr[1];
3
+ }
4
+ static inline short ReadShortLE(const unsigned char* ptr){
5
+ return ptr[0] | (ptr[1] << 8);
6
+ }
7
+ static inline int ReadIntBE(const unsigned char* ptr){
8
+ return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
9
+ }
10
+ static inline int ReadIntLE(const unsigned char* ptr){
11
+ return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
12
+ }
13
+ static inline long long ReadLongLongBE(const unsigned char* ptr) {
14
+ return (unsigned long long)(((unsigned long long)ptr[0] << 56) | ((unsigned long long)ptr[1] << 48) | ((unsigned long long)ptr[2] << 40) | ((unsigned long long)ptr[3] << 32) | ((unsigned long long)ptr[4] << 24) | ((unsigned long long)ptr[5] << 16) | ((unsigned long long)ptr[6] << 8) | (unsigned long long)ptr[7]);
15
+ }
16
+ static inline long long ReadLongLongLE(const unsigned char* ptr) {
17
+ return (unsigned long long)((unsigned long long)ptr[0] | ((unsigned long long)ptr[1] << 8) | ((unsigned long long)ptr[2] << 16) | ((unsigned long long)ptr[3] << 24) | ((unsigned long long)ptr[4] << 32) | ((unsigned long long)ptr[5] << 40) | ((unsigned long long)ptr[6] << 48) | ((unsigned long long)ptr[7] << 56));
18
+ }
19
+ static inline unsigned short ReadUnsignedShortBE(const unsigned char* ptr){return (unsigned short)ReadShortBE(ptr);}
20
+ static inline unsigned short ReadUnsignedShortLE(const unsigned char* ptr){return (unsigned short)ReadShortLE(ptr);}
21
+ static inline unsigned int ReadUnsignedIntBE(const unsigned char* ptr){return (unsigned int)ReadIntBE(ptr);}
22
+ static inline unsigned int ReadUnsignedIntLE(const unsigned char* ptr){return (unsigned int)ReadIntLE(ptr);}
23
+ static inline unsigned long long ReadUnsignedLongLongBE(const unsigned char* ptr){return (unsigned long long)ReadLongLongBE(ptr);}
24
+ static inline unsigned long long ReadUnsignedLongLongLE(const unsigned char* ptr){return (unsigned long long)ReadLongLongLE(ptr);}
25
+
26
+ static inline int GetNextMultiple(int value, int multiple){
27
+ if (multiple <= 0)
28
+ return value;
29
+ if (value % multiple == 0)
30
+ return value;
31
+ return value + multiple - value % multiple;
32
+ }
33
+
34
+ void WriteChar(unsigned char* Buffer, unsigned char Value);
35
+ void WriteShortLE(unsigned char* Buffer, unsigned short Value);
36
+ void WriteShortBE(unsigned char* Buffer, unsigned short Value);
37
+ void WriteIntLE(unsigned char* Buffer, unsigned int Value);
38
+ void WriteIntBE(unsigned char* Buffer, unsigned int Value);
39
+
40
+ struct BitReader{
41
+ unsigned char* Buffer;
42
+ unsigned long long Length;
43
+ unsigned int Position;
44
+
45
+ unsigned long long GetBitsRemaining();
46
+ void SetBuffer(unsigned char* buffer, unsigned int size);
47
+ int ReadInt(int BitCount);
48
+ int ReadSignedInt(int BitCount);
49
+ template<typename T> T SignExtend(T value, int bits);
50
+ void AlignPosition(int multiple);
51
+ int PeekInt(int BitCount);
52
+ int PeekIntFallback(int BitCount);
53
+ };
54
+
55
+ struct BitWriter{
56
+ unsigned char* Buffer;
57
+ unsigned long long Length;
58
+ unsigned int Position;
59
+ void SetBuffer(unsigned char *buffer, unsigned int size);
60
+ unsigned long long GetBitsRemaining();
61
+ void Write(int value, int BitCount);
62
+ void AlignPosition(int multiple);
63
+ void WriteFallback(int value, int BitCount);
64
+ };
65
+
66
+ struct GUID{
67
+ unsigned int guid1;
68
+ unsigned short guid2;
69
+ unsigned short guid3;
70
+ unsigned long long guid4;
71
+ inline void loadGUID(const unsigned char *data){
72
+ guid1 = ReadUnsignedIntLE(data+0);
73
+ guid2 = ReadUnsignedShortLE(data+4);
74
+ guid3 = ReadUnsignedShortLE(data+6);
75
+ guid4 = ReadUnsignedLongLongLE(data+8);
76
+ }
77
+ };