Skip to content

Commit

Permalink
Support for BOM
Browse files Browse the repository at this point in the history
Added support for BOM (byte order mark), it will fix issue with opening decrypted file in text editors.
  • Loading branch information
AMGarkin authored May 15, 2018
1 parent c975261 commit adc67ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 5 additions & 1 deletion BDO_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "zlib.h"

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


Expand Down Expand Up @@ -32,7 +33,7 @@ int main(int argc, char **argv)
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));
Expand All @@ -58,6 +59,9 @@ int main(int argc, char **argv)

rewind(tmpFile);

//write BOM (byte order mark) 0xFEFF = UTF-16 little-endian
fwrite(&BOM_UTF16LE, 2, 1, outFile);

while (1){
if (fread(&strSize, 4, 1, tmpFile) != 1) break;
if (fread(&strType, 4, 1, tmpFile) != 1) break;
Expand Down
20 changes: 13 additions & 7 deletions BDO_encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const wchar_t CHAR_NULL = 0x0000;
const wchar_t CHAR_CR = 0x000D;
const wchar_t CHAR_LF = 0x000A;
const wchar_t BOM_UTF16LE = 0xFEFF;
const unsigned long MAX_BUFF_SIZE = 4096;


Expand Down Expand Up @@ -39,21 +40,26 @@ int main(int argc, char **argv)
int a;
wchar_t strBuff[MAX_BUFF_SIZE];

//Handle BOM (if present, skip it)
fread(&strBuff[0], 2, 1, srcFile);
if (strBuff[0] != BOM_UTF16LE) {
rewind(srcFile);
}

while (1){
wmemset(strBuff, CHAR_NULL, MAX_BUFF_SIZE);

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;
if (strBuff[a] == CHAR_LF || strBuff[a] == CHAR_CR) {
if (strBuff[a-1] == L'"') {
strBuff[a-1] = CHAR_NULL;
}
strBuff[a-1] = CHAR_NULL;
strBuff[a] = CHAR_NULL;
break;
}
Expand Down Expand Up @@ -85,7 +91,7 @@ int main(int argc, char **argv)
fseek(tmpFile, 0, SEEK_END);
uncompressedSize = ftell(tmpFile);
rewind(tmpFile);

compressedSize = compressBound(uncompressedSize);

unsigned char *pCompressedData = (unsigned char *) calloc(compressedSize, sizeof(unsigned char));
Expand Down

0 comments on commit adc67ae

Please sign in to comment.