yEnc encoder

Share your advanced PureBasic knowledge/code with the community.
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

yEnc encoder

Post by xorc1zt »

hi

here is my first pb code: a simple yEnc encoder. (http://www.yenc.org/)

Code: Select all

; --------------
; YENC ENCODER
; x0rc1zt
; v1.0
; jully 2011
; --------------

#Sourcefilebuff = 0
#Destfilebuff = 1

If OpenConsole()
  
  PrintN("* yEnc Encoder")
  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
    
    destfile$ = sourcefile$ + ".ntx"
   
  	If CreateFile(#Destfilebuff, destfile$) ; Create the destination file
  	  WriteStringN(#Destfilebuff, "=ybegin line=128 size="+Str(sourcefilesize)+ " name="+sourcefile$ ) ; Open the yEnc section
  	  
  	  counter.i = 1
  	  While Eof(#Sourcefilebuff) = 0  	 ; Start yEnc encodage
  	    
  	    If counter>=128 ; Line size must be 128
  	      counter=0
  	      WriteAsciiCharacter(#Destfilebuff, 13)
  	      WriteAsciiCharacter(#Destfilebuff, 10)
  	    EndIf
  	    
  	    counter = counter + 1
  	    
  	    to_be_encoded.a = ReadAsciiCharacter(#Sourcefilebuff)
  	    encoded.a = to_be_encoded + 42 ; yEnc formula
  	    
  	    Select encoded ; check if the encoded char is legit if not we must write "=" then encoded+64
  	      Case 0, 9, 10, 13, '=', '.':
  	        WriteAsciiCharacter(#Destfilebuff,'=') ;
  	        WriteAsciiCharacter(#Destfilebuff,encoded+64)
  	        counter = counter + 1
  	      Default:
  	        WriteAsciiCharacter(#Destfilebuff,encoded)
  	    EndSelect
  	
  	  Wend
  	  
  	  WriteAsciiCharacter(#Destfilebuff, 10)
  	  result.s = Hex(CRC32FileFingerprint(sourcefile$)) ; CRC32 of the source file
  	  
  	  WriteStringN(#Destfilebuff, "=yend size="+Str(sourcefilesize)+" crc32="+result+" ") ; Close the yEnc section
  	
  	  CloseFile(#Destfilebuff) ; Close the destination file
  	Else
      PrintN("Couldn't create the file")
  	EndIf
  	
  	CloseFile(#Sourcefilebuff) ; Close the source file
  	
  	PrintN("Done!");
  	
  Else
  	PrintN("Couldn't open the file!")
  EndIf
  
  Print("Press enter to exit")
  Input()
EndIf

todo: multipart support.