Skip to content

Commit

Permalink
Update for new zlib compressed files
Browse files Browse the repository at this point in the history
  • Loading branch information
AMGarkin authored May 14, 2018
1 parent 2f70150 commit d77bd2f
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 65 deletions.
105 changes: 71 additions & 34 deletions BDO_decrypt.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include "ice.h"
#include <cwchar>
#include "zlib.h"

const wchar_t CHAR_LF = 0x000A;
const unsigned long MAX_BUFF_SIZE = 4096;


int main(int argc, char **argv)
{
if (argc != 3)
{
printf("BDO_decrypt <encrypted file> <output file>");
if (argc != 3){
printf("BDO_decrypt <source file> <output file>");
return 1;
}

Expand All @@ -18,42 +22,75 @@ int main(int argc, char **argv)
printf("File not found: %s", srcFileName);
return -1;
}
FILE* tmpFile = tmpfile();
FILE* outFile = fopen(outFileName, "wb");

size_t dataSize, fileSize;
fseek(srcFile, 0, SEEK_END);
dataSize = ftell(srcFile);
fseek(srcFile, 0, SEEK_SET);

unsigned char *ctext = (unsigned char *) calloc(dataSize, sizeof(unsigned char));
unsigned char *ptext = (unsigned char *) calloc(dataSize, sizeof(unsigned char));

fread(ctext, 1, dataSize, srcFile);

const unsigned char *bdo_ice_key = (const unsigned char*)"\x51\xF3\x0F\x11\x04\x24\x6A\x00";

ICE_KEY *ik = ice_key_create(0);
ice_key_set(ik, bdo_ice_key);
///Decompress source to temporary file
unsigned long compressedSize = 0;
unsigned long uncompressedSize = 0;

size_t cuts = dataSize/8;
while (cuts--){
ice_key_decrypt(ik, ctext, ptext);

ctext +=8;
ptext +=8;
}

ptext -= dataSize;

fileSize = dataSize;

while (*(ptext + fileSize) == 0){
fileSize--;
fseek(srcFile, 0, SEEK_END);
compressedSize = ftell(srcFile) - 4; // 1st 4 bytes holds information about uncompressed data size
rewind(srcFile);

fread(&uncompressedSize, 4, 1, srcFile);

unsigned char *pCompressedData = (unsigned char *) calloc(compressedSize, sizeof(unsigned char));
unsigned char *pUncompressedData = (unsigned char *) calloc(uncompressedSize, sizeof(unsigned char));

fread(pCompressedData, 1, compressedSize, srcFile);

int result = uncompress(pUncompressedData, &uncompressedSize, pCompressedData, compressedSize);

if (result == Z_OK) {
fwrite(pUncompressedData, 1, uncompressedSize, tmpFile);

///Convert .bss data from temporary file to text file
unsigned long strSize;
unsigned long strType;
unsigned long strID1;
unsigned short strID2;
unsigned char strID3;
unsigned char strID4;
int a, b;
wchar_t tmp;
wchar_t strBuff[MAX_BUFF_SIZE];

rewind(tmpFile);

while (1){
if (fread(&strSize, 4, 1, tmpFile) != 1) break;
if (fread(&strType, 4, 1, tmpFile) != 1) break;
if (fread(&strID1, 4, 1, tmpFile) != 1) break;
if (fread(&strID2, 2, 1, tmpFile) != 1) break;
if (fread(&strID3, 1, 1, tmpFile) != 1) break;
if (fread(&strID4, 1, 1, tmpFile) != 1) break;
b = 0;
for (a = 0; a < strSize + 2; a++) {
if (fread(&tmp, 2, 1, tmpFile) != 1) break;
if (tmp == CHAR_LF) {
strBuff[b] = L'\\';
b++;
strBuff[b] = L'n';
b++;
} else {
strBuff[b] = tmp;
b++;
}
}

fwprintf(outFile, L"%u\t%u\t%u\t%u\t%u\t\"%s\"\r\n", strType, strID1, strID2, strID3, strID4, strBuff);
}
} else if (result == Z_MEM_ERROR) {
printf("ERROR: Not enough memory.");
} else if (result == Z_BUF_ERROR) {
printf("ERROR: Output buffer is too small.");
} else if (result == Z_DATA_ERROR) {
printf("ERROR: Input data are corrupted or incomplete.");
}

fwrite(ptext, 1, fileSize + 1, outFile);

fclose(srcFile);
fclose(tmpFile);
fclose(outFile);

return 0;
Expand Down
98 changes: 77 additions & 21 deletions BDO_encrypt.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
#include "ice.h"
#include <cwchar>
#include "zlib.h"

const wchar_t CHAR_NULL = 0x0000;
const wchar_t CHAR_CR = 0x000D;
const wchar_t CHAR_LF = 0x000A;
const unsigned long MAX_BUFF_SIZE = 4096;


int main(int argc, char **argv)
{
Expand All @@ -18,38 +25,87 @@ int main(int argc, char **argv)
printf("File not found: %s", srcFileName);
return -1;
}
FILE* tmpFile = tmpfile();
FILE* outFile = fopen(outFileName, "wb");

size_t fileSize, dataSize;
fseek(srcFile, 0, SEEK_END);
fileSize = ftell(srcFile);
fseek(srcFile, 0, SEEK_SET);

dataSize = (fileSize % 8) == 0 ? fileSize : fileSize + 8 - (fileSize % 8);

unsigned char *ptext = (unsigned char *) calloc(dataSize, sizeof(unsigned char));
unsigned char *ctext = (unsigned char *) calloc(dataSize, sizeof(unsigned char));

fread(ptext, 1, fileSize, srcFile);
///Convert .txt source file into temporary .bss file
unsigned long strSize;
unsigned long strType;
unsigned long strID1;
unsigned long strID2; //for some reason "short" doesn't work well with fwscanf()
unsigned char strID3;
unsigned char strID4;
int a;
wchar_t strBuff[MAX_BUFF_SIZE];

const unsigned char *bdo_ice_key = (const unsigned char*)"\x51\xF3\x0F\x11\x04\x24\x6A\x00";
while (1){
wmemset(strBuff, CHAR_NULL, MAX_BUFF_SIZE);

ICE_KEY *ik = ice_key_create(0);
ice_key_set(ik, bdo_ice_key);
if (fwscanf(srcFile, L"%u\t%u\t%u\t%u\t%u\t", &strType, &strID1, &strID2, &strID3, &strID4) < 5) break; //this pattern "eats" leading white space from next string, so I had to enclose strings in double quotes (in bss -> txt conversion)

fseek(srcFile, 2, SEEK_CUR); //skip leading double quotes

for (a = 0; a < MAX_BUFF_SIZE; a++) {
fread(&strBuff[a], 2, 1, srcFile);
if (a > 0) {
if (strBuff[a] == CHAR_LF && strBuff[a-1] == CHAR_CR) {
if (strBuff[a-2] == L'"') {
strBuff[a-2] = CHAR_NULL;
}
strBuff[a-1] = CHAR_NULL;
strBuff[a] = CHAR_NULL;
break;
}
if (strBuff[a] == L'n' && strBuff[a-1] == L'\\') {
a--;
strBuff[a] = CHAR_LF;
}
}
}

size_t cuts = dataSize/8;
while (cuts--){
ice_key_encrypt(ik, ptext, ctext); // key, in (decrypted text), out (encrypted text)
strSize = wcslen(strBuff);

ptext +=8;
ctext +=8;
fwrite(&strSize, 4, 1, tmpFile);
fwrite(&strType, 4, 1, tmpFile);
fwrite(&strID1, 4, 1, tmpFile);
fwrite(&strID2, 2, 1, tmpFile);
fwrite(&strID3, 1, 1, tmpFile);
fwrite(&strID4, 1, 1, tmpFile);
fwrite(&strBuff, 2, strSize, tmpFile);
fputwc(CHAR_NULL, tmpFile);
fputwc(CHAR_NULL, tmpFile);
}

ctext -= dataSize; // reset the pointer back to the beginning.

fwrite(ctext, 1, dataSize, outFile);
///Compress temporary .bss file to .loc
unsigned long compressedSize = 0;
unsigned long uncompressedSize = 0;

fseek(tmpFile, 0, SEEK_END);
uncompressedSize = ftell(tmpFile);
rewind(tmpFile);

compressedSize = compressBound(uncompressedSize);

unsigned char *pCompressedData = (unsigned char *) calloc(compressedSize, sizeof(unsigned char));
unsigned char *pUncompressedData = (unsigned char *) calloc(uncompressedSize, sizeof(unsigned char));

fread(pUncompressedData, 1, uncompressedSize, tmpFile);

int result = compress2(pCompressedData, &compressedSize, pUncompressedData, uncompressedSize, Z_BEST_SPEED);

if (result == Z_OK) {
fwrite(&uncompressedSize, 1, 4, outFile);
fwrite(pCompressedData, 1, compressedSize, outFile);
} else if (result == Z_MEM_ERROR) {
printf("ERROR: Not enough memory.");
} else if (result == Z_BUF_ERROR) {
printf("ERROR: Output buffer is too small.");
}

fclose(srcFile);
fclose(tmpFile);
fclose(outFile);

return 0;
Expand Down
9 changes: 4 additions & 5 deletions build-x64.cmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@mkdir obj\x64 2>nul
x86_64-w64-mingw32-g++.exe -O3 -c ice.c -o obj/x64/ice.o
x86_64-w64-mingw32-g++.exe -O3 -c BDO_encrypt.c -o obj/x64/BDO_encrypt.o
x86_64-w64-mingw32-g++.exe -O3 -c BDO_decrypt.c -o obj/x64/BDO_decrypt.o
x86_64-w64-mingw32-g++.exe -O3 -c BDO_encrypt.c -o obj/x64/BDO_encrypt.o -lz
x86_64-w64-mingw32-g++.exe -O3 -c BDO_decrypt.c -o obj/x64/BDO_decrypt.o -lz
@mkdir bin\x64 2>nul
x86_64-w64-mingw32-g++.exe -o bin/x64/BDO_encrypt.exe obj/x64/BDO_encrypt.o obj/x64/ice.o -s -static
x86_64-w64-mingw32-g++.exe -o bin/x64/BDO_decrypt.exe obj/x64/BDO_decrypt.o obj/x64/ice.o -s -static
x86_64-w64-mingw32-g++.exe -o bin/x64/BDO_encrypt.exe obj/x64/BDO_encrypt.o -s -static -lz
x86_64-w64-mingw32-g++.exe -o bin/x64/BDO_decrypt.exe obj/x64/BDO_decrypt.o -s -static -lz
9 changes: 4 additions & 5 deletions build-x86.cmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@mkdir obj\x86 2>nul
i686-w64-mingw32-g++.exe -O3 -c ice.c -o obj/x86/ice.o
i686-w64-mingw32-g++.exe -O3 -c BDO_encrypt.c -o obj/x86/BDO_encrypt.o
i686-w64-mingw32-g++.exe -O3 -c BDO_decrypt.c -o obj/x86/BDO_decrypt.o
i686-w64-mingw32-g++.exe -O3 -c BDO_encrypt.c -o obj/x86/BDO_encrypt.o -lz
i686-w64-mingw32-g++.exe -O3 -c BDO_decrypt.c -o obj/x86/BDO_decrypt.o -lz
@mkdir bin\x86 2>nul
i686-w64-mingw32-g++.exe -o bin/x86/BDO_encrypt.exe obj/x86/BDO_encrypt.o obj/x86/ice.o -s -static
i686-w64-mingw32-g++.exe -o bin/x86/BDO_decrypt.exe obj/x86/BDO_decrypt.o obj/x86/ice.o -s -static
i686-w64-mingw32-g++.exe -o bin/x86/BDO_encrypt.exe obj/x86/BDO_encrypt.o -s -static -lz
i686-w64-mingw32-g++.exe -o bin/x86/BDO_decrypt.exe obj/x86/BDO_decrypt.o -s -static -lz

0 comments on commit d77bd2f

Please sign in to comment.