Share your advanced PureBasic knowledge/code with the community.
-
xorc1zt
- Enthusiast

- Posts: 276
- Joined: Sat Jul 09, 2011 7:57 am
Post
by xorc1zt »
I wrote a yEnc encoder here :
http://www.purebasic.fr/english/viewtop ... 12&t=46849
There is the decoder :
Code: Select all
; --------------
; YENC DECODER
; x0rc1zt
; v1.0
; jully 2011
; --------------
#Sourcefilebuff = 0
#Destfilebuff = 1
If OpenConsole()
PrintN("* yEnc Decoder")
PrintN("* Version 1.0")
PrintN("* x0rc1zt")
PrintN("")
Print("Enter the file name: ")
sourcefile$ = Input()
sourcefilesize.q = FileSize(sourcefile$)
If ReadFile(#Sourcefilebuff, sourcefile$) ; Open the source file
While Eof(#Sourcefilebuff) = 0
;get the name of our file
buff.s = ReadString(#Sourcefilebuff)
position= FindString(buff, "name=", 1)
If position
name.s = Mid(buff, position+5)
EndIf
;create our file
If CreateFile(#Destfilebuff, name) ; Create the destination file
buff = ReadString(#Sourcefilebuff)
; Read each strings till "=yend"
While Not FindString(buff, "=yend", 1)
*Buffer.character = AllocateMemory(1000)
*Pointer = *Buffer
CopyMemoryString(buff, @*Pointer)
myC.a = 0
; Decode each ascii char of our string
While Not *Buffer\c = #Null
myC = PeekA(*Buffer)
*Buffer = *Buffer + 1
If myC = '='
myC = PeekA(*Buffer)
*Buffer = *Buffer + 1
WriteAsciiCharacter(#Destfilebuff,myC-106)
Else
WriteAsciiCharacter(#Destfilebuff,myC-42)
EndIf
Wend
buff = ReadString(#Sourcefilebuff)
Wend
;FreeMemory(*Buffer)
;get crc32
position= FindString(buff, "crc32=", 1)
If position
checkcrc32.s = Mid(buff, position+6,8)
EndIf
CloseFile(#Destfilebuff) ; Close the destination file
destcrc32.s = Hex(CRC32FileFingerprint(name))
If CompareMemoryString(@checkcrc32,@destcrc32) = #PB_String_Equal
PrintN("CRC32 is okay.")
Else
PrintN("CRC32 failed")
EndIf
Else
PrintN("Couldn't create the file")
EndIf
Wend
CloseFile(#Sourcefilebuff) ; Close the source file
Else
PrintN("Couldn't open the file!")
EndIf
Print("Press enter to exit")
Input()
EndIf
-
DoctorLove
- User

- Posts: 85
- Joined: Sat Mar 06, 2010 2:55 pm
Post
by DoctorLove »
COOL!!!!
Thnx a lot! do you have a working example of multi-file too?
-
xorc1zt
- Enthusiast

- Posts: 276
- Joined: Sat Jul 09, 2011 7:57 am
Post
by xorc1zt »
this code work fine with the multi part example file on yenc.org (
http://www.yenc.org/yenc2.zip)
Code: Select all
; --------------
; YENC DECODER
; x0rc1zt
; v1.0
; jully 2011
; --------------
#Sourcefilebuff = 0
#Destfilebuff = 1
Procedure decode(sourcefile.s, destfile.s, multipart.i)
part.i = 0
buff.s = ""
myC.a = 0
position.i = 0
If CreateFile(#Destfilebuff, destfile)
Repeat
If ReadFile(#Sourcefilebuff, sourcefile)
While Eof(#Sourcefilebuff) = 0
buff = ReadString(#Sourcefilebuff)
position= FindString(buff, "name=", 1)
; if we found the yenc section we read the next line
If position
buff = ReadString(#Sourcefilebuff)
If multipart ; if multi part we must read the next line again
buff = ReadString(#Sourcefilebuff)
EndIf
EndIf
While Not FindString(buff, "=yend", 1) And position
*Buffer.character = AllocateMemory(1000)
*Pointer = *Buffer
CopyMemoryString(buff, @*Pointer)
; Decode each ascii char of our string
While Not *Buffer\c = #Null
myC = PeekA(*Buffer)
*Buffer = *Buffer + 1
If myC = '='
myC = PeekA(*Buffer)
*Buffer = *Buffer + 1
WriteAsciiCharacter(#Destfilebuff,myC-106)
Else
WriteAsciiCharacter(#Destfilebuff,myC-42)
EndIf
Wend
buff = ReadString(#Sourcefilebuff)
Wend
position=0
Wend
CloseFile(#Sourcefilebuff) ; Close the source file
part+1
stringpart.s = Str(part) + ".ntx"
sourcefile = Mid(sourcefile, 1,Len(sourcefile)-Len(stringpart)) + stringpart
Else
multipart=0
EndIf
Until Not multipart
Else
PrintN("Error file " + destfile )
EndIf
CloseFile(#Destfilebuff) ;
EndProcedure
If OpenConsole()
PrintN("* yEnc Decoder")
PrintN("* Version 1.0")
PrintN("* x0rc1zt")
PrintN("")
Print("Enter the file name: ")
sourcefile.s = Input()
sourcefilesize.q = FileSize(sourcefile)
checkcrc32.s = ""
destcrc32.s = ""
crc32found = 0
If ReadFile(#Sourcefilebuff, sourcefile) ; Open the source file
While Eof(#Sourcefilebuff) = 0
;get the name of our file
buff.s = ReadString(#Sourcefilebuff)
position= FindString(buff, "name=", 1)
If position
name.s = Mid(buff, position+5)
EndIf
position= FindString(buff, "part=", 1)
If position
multipart = 1
buff = ReadString(#Sourcefilebuff)
EndIf
position= FindString(buff, "crc32=", 1)
If position
checkcrc32.s = Mid(buff, position+6,8)
crc32found = 1
EndIf
Wend
Else
PrintN("Couldn't open the file!")
EndIf
CloseFile(#Sourcefilebuff) ; Close the source file
decode(sourcefile, name, multipart)
; check crc32
destcrc32.s = Hex(CRC32FileFingerprint(name))
If CompareMemoryString(@checkcrc32,@destcrc32,#PB_String_NoCase) = #PB_String_Equal
PrintN("CRC32 is okay.")
ElseIf crc32found
PrintN("CRC32 failed")
Else
PrintN("CRC32 not found")
EndIf
Print("Press enter to exit")
Input()
EndIf