FLCodec algorithm rewritten in VB 6
-
I don’t know if anyone will find this useful, but I rewrote the algorithm to decode .fl save files in VB 6.
'Credits to Sherlog <[email protected]>for finding out the algorithm 'Rewritten from C Public Function FLCDecode(strCharFilename As String, strOut As String) As Boolean Dim Gene(0 To 3) As Byte Gene(0) = &H47 'G Gene(1) = &H65 'e Gene(2) = &H6E 'n Gene(3) = &H65 'e Dim out() As Byte Dim playerBytes() As Byte Dim fileLength As Long Dim iFileNo As Integer iFileNo = FreeFile() Open strCharFilename For Binary As #iFileNo Dim strFile As String strFile = Input$(4, iFileNo) If strFile <> "FLS1" Then 'Charfile not encoded strOut = strFile & Input$(LOF(iFileNo) - 4, iFileNo) Close #iFileNo FLCDecode = False Exit Function End If fileLength = LOF(iFileNo) ReDim playerBytes(0 To fileLength - 1) ReDim out(0 To (fileLength - 5) * 2 + 1) Get #iFileNo, 1, playerBytes Close #iFileNo Dim i As Long, l As Long i = 0 l = fileLength - 4 Do While i < l Dim c As Byte, k As Byte, r As Byte c = playerBytes(i + 4) k = (Gene(i Mod 4) + i) Mod 256 r = c Xor (k Or &H80) out(i * 2) = r i = i + 1 Loop strOut = out FLCDecode = True End Function Public Function FLCEncode(strIn As String, strCharFilename As String) Dim Gene(0 To 3) As Byte Gene(0) = &H47 'G Gene(1) = &H65 'e Gene(2) = &H6E 'n Gene(3) = &H65 'e Dim byteIn() As Byte byteIn = strIn Dim iFileNo As Integer iFileNo = FreeFile() Open strCharFilename For Binary Access Write As #iFileNo Put #iFileNo, , "FLS1" Dim i As Long, l As Long i = 0 l = Len(strIn) Do While i < l Dim c As Byte, k As Byte, r As Byte c = byteIn(i * 2) k = (Gene(i Mod 4) + i) Mod 256 r = c Xor (k Or &H80) Put #iFileNo, , r i = i + 1 Loop Close #iFileNo End Function</[email protected]>
Feel free to use for whatever. Â
EDIT: I recently rewrote the encode part of the algorithm, so it’s included in the above code now.
I also fixed a bug that wouldn’t show up unless you had more than one file open at once in the decode algorithm (only present in my VB6 rewrite).EDIT2: Tweaked the code some more, now both the decode and encode functions are significantly faster than the original ones. The decode function also checks if the input file is encoded before trying to decode it - if it isn’t encoded it just reads the file without doing any decoding ;).
-
I know I’d have a use for it. Would you mind if, at one point, I included this in FLDev? Credits given, of course. I’d just rewrite it in C++ first, but it’d still be your code.
-
someone just got an idea ^_^
-
I know I’d have a use for it. Would you mind if, at one point, I included this in FLDev? Credits given, of course. I’d just rewrite it in C++ first, but it’d still be your code.
Than i will recode it to Pascal and cobolt. Ok?
-
@ FF
Catch me in chat at home and I will give ya a decoder in C++, wrote it over 2 years ago… -
I know I’d have a use for it. Would you mind if, at one point, I included this in FLDev? Credits given, of course. I’d just rewrite it in C++ first, but it’d still be your code.
Wouldn’t it be better to just use the original FLCodec code, written in C, for use in a C++ project?
It wouldn’t be hard to rewrite it to simply return the entire decoded file as a string instead of write it back out to a file. I already modified it to do so…kinda mixed in the C++ standard lib basic_string to do it, but it works ;Dconst char* __stdcall flc_decode(const char *ifile) { int ifd, i, l, len, rc; char *mem, *buff, c, k, r; std::basic_string <char>scOut = ""; ifd = open(ifile,O_RDONLY|_O_BINARY); if (ifd == -1) return ""; len = lseek(ifd, 0, SEEK_END); lseek(ifd, 0, SEEK_SET); mem = (char*)malloc(len + 1); if(mem == NULL) { close(ifd); return ""; } rc = read(ifd, mem, len); /* if(rc != len) { free(mem); close(ifd); return false; } */ close(ifd); if (strncmp(mem, "FLS1", 4) != 0) { free(mem); return ""; } /* skip FLS1 */ buff = mem + 4; l = len - 4; i = 0; while (i < l) { c = buff[i]; k = (gene[i] + i) % 256; r = c ^ (k | 0x80); scOut += r; i++; } free(mem); return scOut.c_str(); } This is based off the flcodec.cpp file included in FLHook. It had the following includes defined (I added <string>) [code]#include <sys types.h=""> #include <sys stat.h=""> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <stdio.h> #include <io.h> #include <string> [/code] I also made a DLL that exports two functions: rewritten flc_encode and flc_decode methods that accept and return the contents of the file in a string instead of another file, which may be useful to those working in other languages. You can grab it [url]here[/url], and it also includes the source. As always, feel free to use this in any of your projects and bug me when it doesn't work right. You can still of course use my VB code to rewrite the algorithm in C++, FriendlyFire, ..just seems like you're working too hard ;).[/i][/i]</string></io.h></stdio.h></errno.h></string.h></stdlib.h></fcntl.h></sys></sys></string></char>
-
Wouldn’t be the first time I’d be recoding something (just thinking about the DLL read/write functions I had to redo, despite FLEd-IDS having those hidden in its code), but I just wasn’t aware of the fact the source for the decode/encode functions was readily available in C. I’ll have to take a look at that, in that case. When it ain’t broken, don’t fix it, eh?
Thanks though… Might save me quite a bit of work