Skip to content

Commit

Permalink
Improved HCA encoding and decoding with Encryption
Browse files Browse the repository at this point in the history
Made an improvised port of VGAudio, and also added in the better HCA encrypting and decrypting methods, now they work in C.
I also changed the PCM code a bit, as well as fixing bugs in the IO handlers.

I do have an improved ADX code ready, but would need me to test it to push it eventually. TODO for now.
  • Loading branch information
Youjose authored Mar 29, 2023
1 parent 16efaae commit 77b7962
Show file tree
Hide file tree
Showing 10 changed files with 1,091 additions and 1,268 deletions.
2 changes: 1 addition & 1 deletion CriCodecs/BitWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class BitWriter{
public:
unsigned char* buffer;
unsigned int len; // This would give maximum file buffer to be 512 megabytes (0xFFFFFFFF * 8).
unsigned long long len; // This would give maximum file buffer to be 512 megabytes (0xFFFFFFFF * 8).
unsigned int pos;

int get_remaining(){
Expand Down
1 change: 1 addition & 0 deletions CriCodecs/CriCodecs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static struct PyMethodDef Codec_methods[] = {
{ "CriLaylaCompress", (PyCFunction)CriLaylaCompress, METH_VARARGS, nullptr },
{ "HcaDecode", (PyCFunction)HcaDecode, METH_VARARGS, nullptr },
{ "HcaEncode", (PyCFunction)HcaEncode, METH_VARARGS, nullptr },
{ "HcaCrypt", (PyCFunction)HcaCrypt, METH_VARARGS, nullptr },
{ nullptr, nullptr, 0, nullptr }
};

Expand Down
31 changes: 18 additions & 13 deletions CriCodecs/IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,28 @@ void BitReader::AlignPosition(int multiple){
}

int BitReader::PeekInt(int BitCount){
unsigned long long Remaining = GetBitsRemaining();
if(BitCount == 32 && Remaining >= 32)
return ReadIntBE(Buffer);
else if(BitCount == 16 && Remaining >= 16)
return ReadShortBE(Buffer);
else if(BitCount == 8 && Remaining >= 8)
return *Buffer;
int ByteIndex = Position / 8;
int BitIndex = Position % 8;
if (BitCount <= 9 && GetBitsRemaining() >= 16){
if (BitCount <= 9 && Remaining >= 16){
int value = Buffer[ByteIndex] << 8 | Buffer[ByteIndex + 1];
value &= 0xFFFF >> BitIndex;
value >>= 16 - BitCount - BitIndex;
return value;
}
if (BitCount <= 17 && GetBitsRemaining() >= 24){
if (BitCount <= 17 && Remaining >= 24){
int value = Buffer[ByteIndex] << 16 | Buffer[ByteIndex + 1] << 8 | Buffer[ByteIndex + 2];
value &= 0xFFFFFF >> BitIndex;
value >>= 24 - BitCount - BitIndex;
return value;
}
if (BitCount <= 25 && GetBitsRemaining() >= 32){
if (BitCount <= 25 && Remaining >= 32){
int value = Buffer[ByteIndex] << 24 | Buffer[ByteIndex + 1] << 16 | Buffer[ByteIndex + 2] << 8 | Buffer[ByteIndex + 3];
value &= (int)(0xFFFFFFFF >> BitIndex);
value >>= 32 - BitCount - BitIndex;
Expand Down Expand Up @@ -121,25 +128,23 @@ unsigned long long BitWriter::GetBitsRemaining(){
}

void BitWriter::Write(int value, int BitCount){
if (BitCount < 0 || BitCount > 32){
unsigned long long Remaining = GetBitsRemaining();
unsigned int ByteIndex = Position / 8;
unsigned int BitIndex = Position % 8;
if(BitCount < 0 || BitCount > 32)
return;
}
if (BitCount > GetBitsRemaining()){
else if(BitCount > Remaining)
return;
}
int ByteIndex = Position / 8;
int BitIndex = Position % 8;

if (BitCount <= 9 && GetBitsRemaining() >= 16){
if(BitCount <= 9 && GetBitsRemaining() >= 16){
unsigned int outValue = ((value << (16 - BitCount)) & 0xFFFF) >> BitIndex;
Buffer[ByteIndex] |= (unsigned char)(outValue >> 8);
Buffer[ByteIndex + 1] = (unsigned char)outValue;
}else if (BitCount <= 17 && GetBitsRemaining() >= 24){
}else if(BitCount <= 17 && GetBitsRemaining() >= 24){
int outValue = ((value << (24 - BitCount)) & 0xFFFFFF) >> BitIndex;
Buffer[ByteIndex] |= (unsigned char)(outValue >> 16);
Buffer[ByteIndex + 1] = (unsigned char)(outValue >> 8);
Buffer[ByteIndex + 2] = (unsigned char)outValue;
}else if (BitCount <= 25 && GetBitsRemaining() >= 32){
}else if(BitCount <= 25 && GetBitsRemaining() >= 32){
unsigned int outValue = (unsigned int)(((value << (32 - BitCount)) & 0xFFFFFFFF) >> BitIndex);
Buffer[ByteIndex] |= (unsigned char)(outValue >> 24);
Buffer[ByteIndex + 1] = (unsigned char)(outValue >> 16);
Expand Down
Loading

0 comments on commit 77b7962

Please sign in to comment.