Data_Tool_InCode_BF - Data outside Data Section + AES + Compression - Modules

Share your advanced PureBasic knowledge/code with the community.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Data_Tool_InCode_BF - Data outside Data Section + AES + Compression - Modules

Post by Saki »

The data rows are stored directly in the code and the PB data section is then not needed.

It is possible to include data rows directly in the code.

The embedded data is allways automatically encrypted.

There are two variants.
The first with a hard-wired automatic encryption,
which does not require a password.
However, an integer number can be set as the password.
The second variant uses AES encryption which can be passed a password.

Also variants with LZMA compression available.

To test the module, proceed as follows:
Select the Encoder.
Enter an image in the file requester that appears.
Your clipboard now contains the image you selected as a finished data section.
Replace the data lines in the demo part from the decoder code with the content of the clipboard.
Now your new image is embedded in the code.
Start the decoder code and your new image should now be displayed.

The Encoder use String_Fields_BF and can generate more as 700,000 lines in one second.
The Decoder is also extremely fast,
but it is very much slowed down by the PB internal string processing here.
Therefore do not use this version for images that generate several thousand lines of code.
For very large images use the DataSection based variant of this code, its decoder works lightning fast.
You found here :
viewtopic.php?f=12&t=77248&p=570097#p570097

Encoder :

Code: Select all

DeclareModule Data_Tool_InCode_Encoder_BF
  EnableExplicit
  ; Sakis data InCode Generator © - Encoder - Using StringFields_BF - Incredible fast
  ; This code is free for use, changing and enhancing
  ; character_offset - Base character offset - As sample 30, 160, 230 - Try what ever you want
  ; Seed - set as a integer - This works as password for the integrated data crypter
  Declare Data_InCode_Encoder_BF(path$="", data_name$="Str_01$+", length_data_line=100, character_offset=230, seed=0)
EndDeclareModule

Module Data_Tool_InCode_Encoder_BF
  ; StringField_Tool_BF - By Saki - Unicode - This code is free for using and enhancing
  Global NewList index(), NewList indexes.s(), empty_fields.q, start_index.q=1, end_index.q=-1
  AddElement(index()) : AddElement(indexes())
  Procedure StringFields_BF(string$, separator$, mode=1, ignore_empty_fields=1)
    Select mode
      Case 0 : Protected add_first=1 : Case 2 : Protected add_last=1 : Case 3 : add_first=1 : add_last=1
    EndSelect
    Protected skip_first : If start_index<1 : start_index=1 : EndIf
    Protected i, ii, iii, iiii, pos_1, pos_2, length_result, comp, count_index, amount_indexes
    Protected len_separator=StringByteLength(separator$), skip_last, *string=@string$
    Protected *separator=@separator$, *pointer.word, byte_pos_last, result$ 
    If end_index=-1 : end_index=1e18 : EndIf : end_index-1
    If start_index>end_index : start_index=end_index+1 : EndIf : If Not PeekW(*string) : ProcedureReturn 0 : EndIf
    ClearList(index()) : AddElement(index()) : ClearList(indexes()) : AddElement(indexes())
    If comp=CompareMemory(*string, *separator, len_separator) : end_index+1 : count_index+1 : skip_first=1 : EndIf
    If add_first
      If skip_first And start_index : start_index-2 : end_index-1 : EndIf
    Else
      If skip_first And start_index : start_index-1 : EndIf 
    EndIf
    i=-2
    Repeat
      i+2 : comp=CompareMemory(*string+i, *separator, len_separator)
      If comp
        iii=i : count_index+1 : ii+1 : i+len_separator-2 : amount_indexes+1 : AddElement(index()) : index()=i+2
      EndIf 
      *pointer=*string+i+len_separator
    Until count_index>end_index Or Not *pointer\w
    iiii=i : byte_pos_last=iii+len_separator
    If comp=CompareMemory(*string+i-len_separator-1, *separator, len_separator) : skip_last=1 : EndIf
    If end_index>count_index : end_index=count_index : EndIf : amount_indexes=ii : i=skip_first
    If start_index>amount_indexes : start_index=amount_indexes-1 : EndIf : i+start_index+skip_first+skip_last
    If skip_first : i-1 : EndIf : If skip_last : i-1 : EndIf
    If amount_indexes
      Repeat 
        If ListSize(index())>i : SelectElement(index(), i) : pos_1=index() : EndIf
        If ListSize(index())>i+1 : SelectElement(index(), i+1) : pos_2=index() : EndIf
        length_result=(pos_2-pos_1-len_separator)>>1
        If pos_2-pos_1>0
          If length_result>0
            result$=PeekS(*string+pos_1, length_result) : AddElement(indexes()) : indexes()=result$ 
          Else 
            empty_fields+1
            If ignore_empty_fields : result$=#Null$ : Else : result$="" : AddElement(indexes()) : EndIf         
          EndIf
        EndIf
        i+1
      Until i>end_index Or i=amount_indexes
      If add_last And skip_last
        result$=PeekS(*string+byte_pos_last, (iiii-byte_pos_last+len_separator)>>1)
        AddElement(indexes()) : indexes()=result$ 
      EndIf
    EndIf
    ProcedureReturn 1
  EndProcedure
  
  Procedure.s GetStringFields_BF(field)
    If field <1 : ProcedureReturn "" : EndIf
    SelectElement(indexes(), field) : If field<ListSize(indexes()) : ProcedureReturn indexes() : EndIf
  EndProcedure
  
  Procedure FreeAllStringFields_BF()
    ClearList(index()) : AddElement(index()) : ClearList(indexes()) : AddElement(indexes())
    empty_fields.q=0 : start_index.q=1 : end_index.q=-1 : ProcedureReturn 1
  EndProcedure
  
  Procedure Data_InCode_Encoder_BF(path$="", data_name$="Str_01$+", length_data_line=100, character_offset=230, seed=0)
    ; Sakis data Generator - This code put the generated data in your Clipboard - You see nothing
    Protected i, ii, iii, breaks, *buffer, len_buffer, file, lof_file, len_source.q, padding=3
    Protected string$, new_string$, temp_string$, last$, length$
    Macro Create_length_string
      length$=LSet(Chr(character_offset<<8), 6, Chr(character_offset<<8))
      ii=0 : For i=0 To 5 : PokeA(@length$+ii, PeekA(@len_source+i)) : ii+2 : Next i
    EndMacro
    If path$=""
      path$=OpenFileRequester("Select source", "", "", 0) : If path$="" : ProcedureReturn 0 : EndIf
    EndIf
    If FileSize(path$)<1 : ProcedureReturn 0 : EndIf
    file=ReadFile(#PB_Any, path$) : If Not file : ProcedureReturn 0 : EndIf
    lof_file=Lof(file) : *buffer=AllocateMemory(lof_file)
    If Not ReadData(file, *buffer, lof_file) : CloseFile(file) : ProcedureReturn 0 : EndIf : CloseFile(file)
    len_buffer=lof_file+padding : len_source.q=len_buffer-3 : RandomSeed(seed)
    breaks=len_buffer/length_data_line : breaks+Bool(len_buffer%length_data_line) : string$=Space(len_buffer+breaks-1)
    For i=0 To len_buffer-1 : ii+1 : iii+1
      PokeA(@string$+i+ii, character_offset) : PokeA(@string$+i+ii-1, PeekA(*buffer+i)+Random(254, 1))
      If Not iii%length_data_line : ii+1 : PokeW(@string$+i+ii, 10) : ii+1 : EndIf
    Next 
    If breaks<2
      Create_length_string : SetClipboardText(data_name$+Chr(34)+string$+Chr(34)+"+"+Chr(34)+length$+Chr(34))
      ProcedureReturn 1
    EndIf
    iii=(length_data_line+Len(data_name$)+3) : new_string$=Space(breaks*iii) : ii=0 : iii*2
    StringFields_BF(string$, #LF$, 3)
    For i=1 To breaks
      temp_string$=data_name$+Chr(34)+GetStringFields_BF(i)+Chr(34)+#LF$
      CopyMemory(@temp_string$, @new_string$+ii, iii) : ii+iii 
    Next
    new_string$+data_name$+Chr(34) : Create_length_string : new_string$+length$+Chr(34)
    SetClipboardText(new_string$) : FreeAllStringFields_BF()
    ProcedureReturn 1
  EndProcedure
EndModule
UseModule Data_Tool_InCode_Encoder_BF

; #################### Encode - You found the data in your Clipboard ########################
CompilerIf #PB_Compiler_IsMainFile
  ; Data_InCode_Encoder_BF() ; Preset
  Define len_line=100
  Data_InCode_Encoder_BF("", "Str_01$+", len_line)
  MessageRequester("Encoder", "Encode finished"+#LF$+#LF$+ "The data are now in your Clipboard")
CompilerEndIf

Decoder :

Code: Select all

DeclareModule Data_Tool_InCode_Decoder_BF
  EnableExplicit
  ; Sakis data InCode Generator © - Decoder - This code is free For use, changing And enhancing
  ; character_offset - Base character offset - As sample 30, 160, 230 - Try what ever you want
  ; Seed - set as a integer - This works as password for the integrated data crypter
  Declare Data_InCode_Decoder_BF(Data_BF$, seed=0)
  Declare ChatchImageInCode_BF(Data_01$) ; Catch a image
EndDeclareModule

Module Data_Tool_InCode_Decoder_BF
  ; You can remove what you not want
  UsePNGImageDecoder() : UseJPEGImageDecoder() : UseGIFImageDecoder() : UseTIFFImageDecoder()
  
  Procedure Data_InCode_Decoder_BF(Data_BF$, seed=0)
    ; Sakis data Generator - This function give back a pointer to a allocated memory with your data
    Protected *data_start_pointer=@Data_BF$, len_line=Len(Data_BF$), len_data.q, data_size=len_line*2-2
    Protected i, ii, offset, *buffer, *catch_buffer, len_buffer=data_size/2 , *catch_buffer_new
    *buffer=AllocateMemory(data_size) : CopyMemory(*data_start_pointer, *buffer, data_size)
    *catch_buffer=AllocateMemory(data_size/2-offset) : RandomSeed(seed)
    For i=0 To len_buffer-offset-1 : If Not i%len_line And i : ii+2 : EndIf
      PokeA(*catch_buffer+i, PeekA(*buffer+ii)-Random(254, 1)) : ii+2
    Next
    ii=12 : For i=0 To 5 : PokeA(@len_data+i, PeekA(*data_start_pointer+StringByteLength(Data_BF$)-ii)) : ii-2 : Next
    *catch_buffer_new=AllocateMemory(len_data)
    CopyMemory(*catch_buffer, *catch_buffer_new, MemorySize(*catch_buffer_new))
    FreeMemory(*catch_buffer) : FreeMemory(*buffer)
    ProcedureReturn *catch_buffer_new
  EndProcedure
  Procedure ChatchImageInCode_BF(Str_01$)
    If Str_01$="" : ProcedureReturn 0 : EndIf
    Protected *catch_buffer=Data_InCode_Decoder_BF(Str_01$)
    If *catch_buffer : Protected image_ID=CatchImage(#PB_Any, *catch_buffer) : FreeMemory(*catch_buffer)
    ProcedureReturn image_ID : EndIf
  EndProcedure
EndModule
UseModule Data_Tool_InCode_Decoder_BF

; #################### Decoder Demo part ########################
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
  
  Define Str_01$
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  
  ; Standard procedre
  ; Define *catch_buffer=Data_InCode_Decoder_BF(Str_01$) 
  ; Define image_ID=CatchImage(#PB_Any, *catch_buffer)
  ; FreeMemory(*catch_buffer)
  
  ; Simplificated
  Define image_ID=ChatchImageInCode_BF(Str_01$)
  
  Define window_ID=OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If IsImage(image_ID)
    Define image_gadget_ID=ImageGadget(#PB_Any,
                                       WindowWidth(window_ID)/2-ImageWidth(image_ID)/2,
                                       WindowHeight(window_ID)/2-ImageHeight(image_ID)/2,
                                       0,
                                       0,
                                       ImageID(image_ID))
  Else
    CloseWindow(window_ID)
    MessageRequester("Hint", "Embedded image not usable")
    End
  EndIf
  
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
  
CompilerEndIf

Encoder with AES encryption and crypt randomized IV

Code: Select all

DeclareModule Data_Tool_InCode_Encoder_AES_BF
  EnableExplicit
  ; Sakis data InCode Generator © - Encoder - Using StringFields_BF - Incredible fast
  ; Variant with integrated AES encryption and automatically crypt randomized IV
  ; This code is free for use, changing and enhancing
  ; character_offset - Base character offset - As sample 30, 160, 230 - Try what ever you want
  ; Seed - set as a integer - This works as password for the integrated data crypter
  Declare Data_InCode_Encoder_AES_BF(path$="", data_name$="Str_01$+", length_data_line=100, character_offset=230, password$="")
EndDeclareModule

Module Data_Tool_InCode_Encoder_AES_BF
  UseMD5Fingerprint()
  ; StringField_Tool_BF - By Saki - Unicode - This code is free for using and enhancing
  Global NewList index(), NewList indexes.s(), empty_fields.q, skip_first, start_index.q=1, end_index.q=-1
  AddElement(index()) : AddElement(indexes())
  Procedure StringFields_BF(string$, separator$, mode=1, ignore_empty_fields=1)
    Select mode
      Case 0 : Protected add_first=1 : Case 2 : Protected add_last=1 : Case 3 : add_first=1 : add_last=1
    EndSelect
    Protected skip_first=0 : If start_index<1 : start_index=1 : EndIf
    Protected i, ii, iii, iiii, pos_1, pos_2, length_result, comp, count_index, amount_indexes
    Protected len_separator=StringByteLength(separator$), skip_last, *string=@string$
    Protected *separator=@separator$, *pointer.word, byte_pos_last, result$ 
    If end_index=-1 : end_index=1e18 : EndIf : end_index-1
    If start_index>end_index : start_index=end_index+1 : EndIf : If Not PeekW(*string) : ProcedureReturn 0 : EndIf
    ClearList(index()) : AddElement(index()) : ClearList(indexes()) : AddElement(indexes())
    If comp=CompareMemory(*string, *separator, len_separator) : end_index+1 : count_index+1 : skip_first=1 : EndIf
    If add_first
      If skip_first And start_index : start_index-2 : end_index-1 : EndIf
    Else
      If skip_first And start_index : start_index-1 : EndIf 
    EndIf
    i=-2
    Repeat
      i+2 : comp=CompareMemory(*string+i, *separator, len_separator)
      If comp
        iii=i : count_index+1 : ii+1 : i+len_separator-2 : amount_indexes+1 : AddElement(index()) : index()=i+2
      EndIf 
      *pointer=*string+i+len_separator
    Until count_index>end_index Or Not *pointer\w
    iiii=i : byte_pos_last=iii+len_separator
    If comp=CompareMemory(*string+i-len_separator-1, *separator, len_separator) : skip_last=1 : EndIf
    If end_index>count_index : end_index=count_index : EndIf : amount_indexes=ii : i=skip_first
    If start_index>amount_indexes : start_index=amount_indexes-1 : EndIf : i+start_index+skip_first+skip_last
    If skip_first : i-1 : EndIf : If skip_last : i-1 : EndIf
    If amount_indexes
      Repeat 
        If ListSize(index())>i : SelectElement(index(), i) : pos_1=index() : EndIf
        If ListSize(index())>i+1 : SelectElement(index(), i+1) : pos_2=index() : EndIf
        length_result=(pos_2-pos_1-len_separator)>>1
        If pos_2-pos_1>0
          If length_result>0
            result$=PeekS(*string+pos_1, length_result) : AddElement(indexes()) : indexes()=result$ 
          Else 
            empty_fields+1
            If ignore_empty_fields : result$=#Null$ : Else : result$="" : AddElement(indexes()) : EndIf         
          EndIf
        EndIf
        skip_first=0 : i+1
      Until i>end_index Or i=amount_indexes
      If add_last And skip_last
        result$=PeekS(*string+byte_pos_last, (iiii-byte_pos_last+len_separator)>>1)
        AddElement(indexes()) : indexes()=result$ 
      EndIf
    EndIf
    ProcedureReturn 1
  EndProcedure
  
  Procedure.s GetStringFields_BF(field)
    If field <1 : ProcedureReturn "" : EndIf
    SelectElement(indexes(), field) : If field<ListSize(indexes()) : ProcedureReturn indexes() : EndIf
  EndProcedure
  
  Procedure FreeAllStringFields_BF()
    ClearList(index()) : AddElement(index()) : ClearList(indexes()) : AddElement(indexes()) : empty_fields=0
    ProcedureReturn 1
  EndProcedure
  
  Procedure Data_InCode_Encoder_AES_BF(path$="", data_name$="Str_01$+", length_data_line=100, character_offset=230, password$="")
    ; Sakis data Generator - This code put the generated data in your Clipboard - You see nothing
    Protected i, ii, iii, i1, depth, breaks, *buffer, len_buffer, file, lof_file, len_source.q, padding=3
    Protected string$, new_string$, temp_string$, last$, length$, iv$
    Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_MD5) : Dim register.q(3)
    Repeat ; 16 Bytes
      PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
    Until ii=StringByteLength(fixed$) : i=0 : ii=0
    If OpenCryptRandom() : CryptRandomData(@register(2), 16) : Else : RandomData(@register(2), 16) : EndIf
    register(0)!register(2) : register(1)!register(3) 
    Macro Create_length_string
      length$=LSet(Chr(character_offset<<8), 6, Chr(character_offset<<8))
      ii=0 : For i=0 To 5 : PokeA(@length$+ii, PeekA(@len_source+i)) : ii+2 : Next
    EndMacro
    Macro Create_string
      iv$=LSet(Chr(character_offset<<8), 16, Chr(character_offset<<8))
      ii=0 : For i=0 To 15 : PokeA(@iv$+ii, PeekA(@register(2)+i)) : ii+2 : Next
    EndMacro
    If path$="" : path$=OpenFileRequester("Select source", "", "", 0) : If path$="" : ProcedureReturn 0 : EndIf : EndIf
    If FileSize(path$)<1 : ProcedureReturn 0 : EndIf
    file=ReadFile(#PB_Any, path$) : If Not file : ProcedureReturn 0 : EndIf
    lof_file=Lof(file) : *buffer=AllocateMemory(lof_file)
    If Not ReadData(file, *buffer, lof_file) : CloseFile(file) : ProcedureReturn 0 : EndIf : CloseFile(file)
    len_buffer=lof_file+padding : len_source.q=len_buffer-3
    breaks=len_buffer/length_data_line : breaks+Bool(len_buffer%length_data_line) : string$=Space(len_buffer+breaks-1)
    For i=0 To len_buffer-1 : ii+1 : iii+1
      Repeat
        If Not i1 : AESEncoder(@register(0), @register(0), 16, @register(0), 128, 0, #PB_Cipher_ECB) : EndIf
        depth=PeekA(@register(0)+i1) : i1+1 : If i1>15 : i1=0 : EndIf
      Until depth
      PokeA(@string$+i+ii, character_offset) : PokeA(@string$+i+ii-1, PeekA(*buffer+i)+depth)
      If Not iii%length_data_line : ii+1 : PokeW(@string$+i+ii, 10) : ii+1 : EndIf
    Next 
    If breaks<2
      Create_length_string : Create_string
      SetClipboardText(data_name$+Chr(34)+string$+Chr(34)+"+"+Chr(34)+iv$+length$+Chr(34))
      ProcedureReturn 1
    EndIf
    iii=(length_data_line+Len(data_name$)+3) : new_string$=Space(breaks*iii) : ii=0 : iii*2
    StringFields_BF(string$, #LF$, 3)
    For i=1 To breaks
      temp_string$=data_name$+Chr(34)+GetStringFields_BF(i)+Chr(34)+#LF$
      CopyMemory(@temp_string$, @new_string$+ii, iii) : ii+iii 
    Next
    new_string$+data_name$+Chr(34) : Create_length_string : Create_string : new_string$+iv$+length$+Chr(34)
    SetClipboardText(new_string$) : FreeAllStringFields_BF()
    ProcedureReturn 1
  EndProcedure
EndModule
UseModule Data_Tool_InCode_Encoder_AES_BF

; #################### Encode - You found the data in your Clipboard ########################
CompilerIf #PB_Compiler_IsMainFile
  ; Data_InCode_Encoder_AES_BF() ; Preset
  Define password$=""
  Define len_line=100
  Define character_offset=230
  Data_InCode_Encoder_AES_BF("", "Str_01$+", len_line, character_offset, password$)
  MessageRequester("Encoder", "Encode finished"+#LF$+#LF$+ "The data are now in your Clipboard")
CompilerEndIf

Decoder with AES encryption and crypt randomized IV

Code: Select all

DeclareModule Data_Tool_InCode_Decoder_AES_BF
  EnableExplicit
  ; Sakis data InCode Generator © - Decoder - This code is free for use, changing and enhancing
  ; Variant with integrated AES encryption and automatically crypt randomized IV
  ; character_offset - Base character offset - As sample 30, 160, 230 - Try what ever you want
  ; Seed - set as a integer - This works as password for the integrated data crypter
  Declare Data_InCode_Decoder_AES_BF(Data_BF$, password$="")
  Declare ChatchImageInCode_BF(Data_01$, password$="") ; Catch a image
EndDeclareModule

Module Data_Tool_InCode_Decoder_AES_BF
  ; You can remove what you not want
  UsePNGImageDecoder() : UseJPEGImageDecoder() : UseGIFImageDecoder() : UseTIFFImageDecoder() : UseMD5Fingerprint()
  
  Procedure Data_InCode_Decoder_AES_BF(Data_BF$, password$="")
    ; Sakis data Generator - This function give back a pointer to a allocated memory with your data
    Protected *data_start_pointer=@Data_BF$, len_line=Len(Data_BF$), len_data.q, data_size=len_line*2-2
    Protected i, ii, i1, depth, offset, *buffer, *catch_buffer, len_buffer, *catch_buffer_new
    Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_MD5) : Dim register.q(3)
    Repeat ; 16 Bytes
      PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
    Until ii=StringByteLength(fixed$) : i=0 : ii=0 : len_buffer=data_size/2
    *buffer=AllocateMemory(data_size) : CopyMemory(*data_start_pointer, *buffer, data_size) : i=0
    *catch_buffer=AllocateMemory(data_size/2-offset)
    ii=44 : For i=0 To 15 : PokeA(@register(2)+i, PeekA(*data_start_pointer+StringByteLength(Data_BF$)-ii)) : ii-2 : Next
    ii=0 : register(0)!register(2) : register(1)!register(3) 
    For i=0 To len_buffer-offset-1 : If Not i%len_line And i : ii+2 : EndIf
      Repeat
        If Not i1 : AESEncoder(@register(0), @register(0), 16, @register(0), 128, 0, #PB_Cipher_ECB) : EndIf
        depth=PeekA(@register(0)+i1) : i1+1 : If i1>15 : i1=0 : EndIf
      Until depth
      PokeA(*catch_buffer+i, PeekA(*buffer+ii)-depth) : ii+2
    Next
    ii=12 : For i=0 To 5 : PokeA(@len_data+i, PeekA(*data_start_pointer+StringByteLength(Data_BF$)-ii)) : ii-2 : Next
    *catch_buffer_new=AllocateMemory(len_data)
    CopyMemory(*catch_buffer, *catch_buffer_new, MemorySize(*catch_buffer_new))
    FreeMemory(*catch_buffer) : FreeMemory(*buffer)
    ProcedureReturn *catch_buffer_new
  EndProcedure
  Procedure ChatchImageInCode_BF(Str_01$, password$="")
    If Str_01$="" : ProcedureReturn 0 : EndIf
    Protected *catch_buffer=Data_InCode_Decoder_AES_BF(Str_01$, password$)
    If *catch_buffer : Protected image_ID=CatchImage(#PB_Any, *catch_buffer) : FreeMemory(*catch_buffer)
    ProcedureReturn image_ID : EndIf
  EndProcedure
EndModule
UseModule Data_Tool_InCode_Decoder_AES_BF

; #################### Decoder Demo part ########################
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
  
  Define Str_01$
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  
  ; Standard procedre
  ; Define *catch_buffer=Data_InCode_Decoder_AES_BF(Str_01$) 
  ; Define image_ID=CatchImage(#PB_Any, *catch_buffer)
  ; FreeMemory(*catch_buffer)
  
  ; Simplificated
  
  Define password$=""
  Define image_ID=ChatchImageInCode_BF(Str_01$, password$)
  
  Define window_ID=OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If IsImage(image_ID)
    Define image_gadget_ID=ImageGadget(#PB_Any,
                                       WindowWidth(window_ID)/2-ImageWidth(image_ID)/2,
                                       WindowHeight(window_ID)/2-ImageHeight(image_ID)/2,
                                       0,
                                       0,
                                       ImageID(image_ID))
  Else
    CloseWindow(window_ID)
    MessageRequester("Hint", "Embedded image not usable")
    End
  EndIf
  
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
  
CompilerEndIf

Encoder with LZMA compression

Code: Select all

DeclareModule Data_Tool_InCode_Encoder_BF
  EnableExplicit
  ; Sakis data InCode Generator © - Encoder - Using StringFields_BF - Incredible fast - With compression
  ; This code is free for use, changing and enhancing
  ; character_offset - Base character offset - As sample 30, 160, 230 - Try what ever you want
  ; Seed - set as a integer - This works as password for the integrated data crypter
  Declare Data_InCode_Encoder_BF(path$="", data_name$="Str_01$+", length_data_line=100, character_offset=230, seed=0)
  Declare CompressionPercent_ICE_BF() ; This function show the actual compression as percent
EndDeclareModule

Module Data_Tool_InCode_Encoder_BF
  ; StringField_Tool_BF - By Saki - Unicode - This code is free for using and enhancing
  Global NewList index(), NewList indexes.s(), empty_fields.q, start_index.q=1, end_index.q=-1, compression_percent.d
  AddElement(index()) : AddElement(indexes())
  Procedure StringFields_BF(string$, separator$, mode=1, ignore_empty_fields=1)
    Select mode
      Case 0 : Protected add_first=1 : Case 2 : Protected add_last=1 : Case 3 : add_first=1 : add_last=1
    EndSelect
    Protected skip_first : If start_index<1 : start_index=1 : EndIf
    Protected i, ii, iii, iiii, pos_1, pos_2, length_result, comp, count_index, amount_indexes
    Protected len_separator=StringByteLength(separator$), skip_last, *string=@string$
    Protected *separator=@separator$, *pointer.word, byte_pos_last, result$ 
    If end_index=-1 : end_index=1e18 : EndIf : end_index-1
    If start_index>end_index : start_index=end_index+1 : EndIf : If Not PeekW(*string) : ProcedureReturn 0 : EndIf
    ClearList(index()) : AddElement(index()) : ClearList(indexes()) : AddElement(indexes())
    If comp=CompareMemory(*string, *separator, len_separator) : end_index+1 : count_index+1 : skip_first=1 : EndIf
    If add_first
      If skip_first And start_index : start_index-2 : end_index-1 : EndIf
    Else
      If skip_first And start_index : start_index-1 : EndIf 
    EndIf
    i=-2
    Repeat
      i+2 : comp=CompareMemory(*string+i, *separator, len_separator)
      If comp
        iii=i : count_index+1 : ii+1 : i+len_separator-2 : amount_indexes+1 : AddElement(index()) : index()=i+2
      EndIf 
      *pointer=*string+i+len_separator
    Until count_index>end_index Or Not *pointer\w
    iiii=i : byte_pos_last=iii+len_separator
    If comp=CompareMemory(*string+i-len_separator-1, *separator, len_separator) : skip_last=1 : EndIf
    If end_index>count_index : end_index=count_index : EndIf : amount_indexes=ii : i=skip_first
    If start_index>amount_indexes : start_index=amount_indexes-1 : EndIf : i+start_index+skip_first+skip_last
    If skip_first : i-1 : EndIf : If skip_last : i-1 : EndIf
    If amount_indexes
      Repeat 
        If ListSize(index())>i : SelectElement(index(), i) : pos_1=index() : EndIf
        If ListSize(index())>i+1 : SelectElement(index(), i+1) : pos_2=index() : EndIf
        length_result=(pos_2-pos_1-len_separator)>>1
        If pos_2-pos_1>0
          If length_result>0
            result$=PeekS(*string+pos_1, length_result) : AddElement(indexes()) : indexes()=result$ 
          Else 
            empty_fields+1
            If ignore_empty_fields : result$=#Null$ : Else : result$="" : AddElement(indexes()) : EndIf         
          EndIf
        EndIf
        i+1
      Until i>end_index Or i=amount_indexes
      If add_last And skip_last
        result$=PeekS(*string+byte_pos_last, (iiii-byte_pos_last+len_separator)>>1)
        AddElement(indexes()) : indexes()=result$ 
      EndIf
    EndIf
    ProcedureReturn 1
  EndProcedure
  
  Procedure.s GetStringFields_BF(field)
    If field <1 : ProcedureReturn "" : EndIf
    SelectElement(indexes(), field) : If field<ListSize(indexes()) : ProcedureReturn indexes() : EndIf
  EndProcedure
  
  Procedure FreeAllStringFields_BF()
    ClearList(index()) : AddElement(index()) : ClearList(indexes()) : AddElement(indexes())
    empty_fields.q=0 : start_index.q=1 : end_index.q=-1 : ProcedureReturn 1
  EndProcedure
  
  Procedure Data_InCode_Encoder_BF(path$="", data_name$="Str_01$+", length_data_line=100, character_offset=230, seed=0)
    ; Sakis data Generator - This code put the generated data in your Clipboard - You see nothing
    Protected i, ii, iii, breaks, *buffer, len_buffer, file, lof_file, len_source.q, padding=3
    Protected string$, new_string$, temp_string$, last$, length$
    Macro Create_length_string
      length$=LSet(Chr(character_offset<<8), 6, Chr(character_offset<<8))
      ii=0 : For i=0 To 5 : PokeA(@length$+ii, PeekA(@len_source+i)) : ii+2 : Next i
    EndMacro
    If path$=""
      path$=OpenFileRequester("Select source", "", "", 0) : If path$="" : ProcedureReturn 0 : EndIf
    EndIf
    If FileSize(path$)<1 : ProcedureReturn 0 : EndIf
    file=ReadFile(#PB_Any, path$) : If Not file : ProcedureReturn 0 : EndIf
    lof_file=Lof(file) : *buffer=AllocateMemory(lof_file)
    If Not ReadData(file, *buffer, lof_file) : CloseFile(file) : ProcedureReturn 0 : EndIf : CloseFile(file)
    RandomSeed(seed)
    UseLZMAPacker()
    Protected length_uncompressed$, length_uncompressed.q=lof_file
    Protected *buffer_compressed=AllocateMemory(lof_file*1.5)
    Protected size_compressed=CompressMemory(*buffer, lof_file, *buffer_compressed,
                                             MemorySize(*buffer_compressed), #PB_PackerPlugin_Lzma)
    If size_compressed<lof_file
      compression_percent=100-100/length_uncompressed*size_compressed
      CopyMemory(*buffer_compressed, *buffer, size_compressed)
      len_buffer=size_compressed+padding
    Else
      compression_percent=-1
      len_buffer=lof_file+padding
    EndIf
    FreeMemory(*buffer_compressed)
    len_source.q=len_buffer-3
    Macro Create_length_uncompressed
      length_uncompressed$=LSet(Chr(character_offset<<8), 6, Chr(character_offset<<8))
      ii=0 : For i=0 To 5 : PokeA(@length_uncompressed$+ii, PeekA(@length_uncompressed+i)) : ii+2 : Next i
    EndMacro
    
    breaks=len_buffer/length_data_line : breaks+Bool(len_buffer%length_data_line) : string$=Space(len_buffer+breaks-1)
    For i=0 To len_buffer-1 : ii+1 : iii+1
      PokeA(@string$+i+ii, character_offset) : PokeA(@string$+i+ii-1, PeekA(*buffer+i)+Random(254, 1))
      If Not iii%length_data_line : ii+1 : PokeW(@string$+i+ii, 10) : ii+1 : EndIf
    Next 
    If breaks<2
      Create_length_string : Create_length_uncompressed
      SetClipboardText(data_name$+Chr(34)+string$+Chr(34)+"+"+Chr(34)+length_uncompressed$+length$+Chr(34))
      ProcedureReturn 1
    EndIf
    iii=(length_data_line+Len(data_name$)+3) : new_string$=Space(breaks*iii) : ii=0 : iii*2
    StringFields_BF(string$, #LF$, 3)
    For i=1 To breaks
      temp_string$=data_name$+Chr(34)+GetStringFields_BF(i)+Chr(34)+#LF$
      CopyMemory(@temp_string$, @new_string$+ii, iii) : ii+iii 
    Next
    new_string$+data_name$+Chr(34) : Create_length_string : Create_length_uncompressed
    new_string$+length_uncompressed$+length$+Chr(34)
    SetClipboardText(new_string$) : FreeAllStringFields_BF()
    ProcedureReturn 1
  EndProcedure
  
  Procedure CompressionPercent_ICE_BF() : ProcedureReturn compression_percent : EndProcedure
  
EndModule
UseModule Data_Tool_InCode_Encoder_BF

; #################### Encode - You found the data in your Clipboard ########################
CompilerIf #PB_Compiler_IsMainFile
  ; Data_InCode_Encoder_BF() ; Preset
  Define len_line=100
  Data_InCode_Encoder_BF("", "Str_01$+", len_line)
  MessageRequester("Encoder", "Encode finished"+#LF$+#LF$+ "The data are now in your Clipboard"+#LF$+#LF$+
                              Str(CompressionPercent_ICE_BF())+" % compression")
CompilerEndIf

Decoder with LZMA compression

Code: Select all

DeclareModule Data_Tool_InCode_Decoder_BF
  EnableExplicit
  ; Sakis data InCode Generator © - Decoder - With compression
  ; This code is free for use, changing and enhancing
  ; character_offset - Base character offset - As sample 30, 160, 230 - Try what ever you want
  ; Seed - set as a integer - This works as password for the integrated data crypter
  Declare Data_InCode_Decoder_BF(Data_BF$, seed=0)
  Declare ChatchImageInCode_BF(Data_01$) ; Catch a image
  Declare CompressionPercent_ICD_BF()    ; This function show the actual compression as percent
EndDeclareModule

Module Data_Tool_InCode_Decoder_BF
  ; You can remove what you not want
  UsePNGImageDecoder() : UseJPEGImageDecoder() : UseGIFImageDecoder() : UseTIFFImageDecoder()
  Global compression_percent.d
  
  Procedure Data_InCode_Decoder_BF(Data_BF$, seed=0)
    ; Sakis data Generator - This function give back a pointer to a allocated memory with your data
    Protected *data_start_pointer=@Data_BF$, len_line=Len(Data_BF$), len_data.q, data_size=len_line*2-2
    Protected i, ii, offset, *buffer, *catch_buffer, len_buffer=data_size/2 , *catch_buffer_new
    *buffer=AllocateMemory(data_size) : CopyMemory(*data_start_pointer, *buffer, data_size)
    *catch_buffer=AllocateMemory(data_size/2-offset) : RandomSeed(seed)
    For i=0 To len_buffer-offset-1 : If Not i%len_line And i : ii+2 : EndIf
      PokeA(*catch_buffer+i, PeekA(*buffer+ii)-Random(254, 1)) : ii+2
    Next
    
    Protected len_uncompressed
    ii=12 : For i=0 To 5 : PokeA(@len_data+i, PeekA(*data_start_pointer+StringByteLength(Data_BF$)-ii)) : ii-2 : Next
    ii=24 : For i=0 To 5 : PokeA(@len_uncompressed+i, PeekA(*data_start_pointer+StringByteLength(Data_BF$)-ii)) : ii-2 : Next
    *catch_buffer_new=AllocateMemory(len_data)
    CopyMemory(*catch_buffer, *catch_buffer_new, MemorySize(*catch_buffer_new))
    FreeMemory(*catch_buffer) : FreeMemory(*buffer)
    UseLZMAPacker()
    Protected *buffer_uncompressed=AllocateMemory(len_uncompressed)
    Protected size_uncompressed=UncompressMemory(*catch_buffer_new, MemorySize(*catch_buffer_new),
                                                 *buffer_uncompressed, MemorySize(*buffer_uncompressed),
                                                 #PB_PackerPlugin_Lzma)
    
    If size_uncompressed<>-1
      Swap *buffer_uncompressed, *catch_buffer_new
      compression_percent=100-100/size_uncompressed*len_data
    Else
      compression_percent=-1
    EndIf
    FreeMemory(*buffer_uncompressed)
    
    ProcedureReturn *catch_buffer_new
  EndProcedure
  Procedure ChatchImageInCode_BF(Str_01$)
    If Str_01$="" : ProcedureReturn 0 : EndIf
    Protected *catch_buffer=Data_InCode_Decoder_BF(Str_01$)
    If *catch_buffer : Protected image_ID=CatchImage(#PB_Any, *catch_buffer) : FreeMemory(*catch_buffer)
    ProcedureReturn image_ID : EndIf
  EndProcedure
  
  Procedure CompressionPercent_ICD_BF() : ProcedureReturn compression_percent : EndProcedure
  
EndModule
UseModule Data_Tool_InCode_Decoder_BF

; #################### Decoder Demo part ########################
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
  
  Define Str_01$
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  Str_01$+""
  
  ; Standard procedre
  ; Define *catch_buffer=Data_InCode_Decoder_BF(Str_01$) 
  ; Define image_ID=CatchImage(#PB_Any, *catch_buffer)
  ; FreeMemory(*catch_buffer)
  
  ; Simplificated
  Define image_ID=ChatchImageInCode_BF(Str_01$)
  
  Debug Str(CompressionPercent_ICD_BF())+" % compression" ; This function show the actual compression as percent
  
  Define window_ID=OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If IsImage(image_ID)
    Define image_gadget_ID=ImageGadget(#PB_Any,
                                       WindowWidth(window_ID)/2-ImageWidth(image_ID)/2,
                                       WindowHeight(window_ID)/2-ImageHeight(image_ID)/2,
                                       0,
                                       0,
                                       ImageID(image_ID))
  Else
    CloseWindow(window_ID)
    MessageRequester("Hint", "Embedded image not usable")
    End
  EndIf
  
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
  
CompilerEndIf

Encoder with AES encryption, crypt randomized IV and LZMA compression

Code: Select all

DeclareModule Data_Tool_InCode_Encoder_AES_BF
  EnableExplicit
  ; Sakis data InCode Generator © - Encoder - Using StringFields_BF - Incredible fast - With compression
  ; Variant with integrated AES encryption and automatically crypt randomized IV
  ; This code is free for use, changing and enhancing
  ; character_offset - Base character offset - As sample 30, 160, 230 - Try what ever you want
  ; Seed - set as a integer - This works as password for the integrated data crypter
  Declare Data_InCode_Encoder_AES_BF(path$="", data_name$="Str_01$+", length_data_line=100, character_offset=230, password$="")
  Declare CompressionPercent_ICEA_BF() ; This function show the actual compression as percent
EndDeclareModule

Module Data_Tool_InCode_Encoder_AES_BF
  UseMD5Fingerprint()
  ; StringField_Tool_BF - By Saki - Unicode - This code is free for using and enhancing
  Global NewList index(), NewList indexes.s(), empty_fields.q, start_index.q=1, end_index.q=-1, compression_percent.d
  AddElement(index()) : AddElement(indexes())
  Procedure StringFields_BF(string$, separator$, mode=1, ignore_empty_fields=1)
    Select mode
      Case 0 : Protected add_first=1 : Case 2 : Protected add_last=1 : Case 3 : add_first=1 : add_last=1
    EndSelect
    Protected skip_first : If start_index<1 : start_index=1 : EndIf
    Protected i, ii, iii, iiii, pos_1, pos_2, length_result, comp, count_index, amount_indexes
    Protected len_separator=StringByteLength(separator$), skip_last, *string=@string$
    Protected *separator=@separator$, *pointer.word, byte_pos_last, result$ 
    If end_index=-1 : end_index=1e18 : EndIf : end_index-1
    If start_index>end_index : start_index=end_index+1 : EndIf : If Not PeekW(*string) : ProcedureReturn 0 : EndIf
    ClearList(index()) : AddElement(index()) : ClearList(indexes()) : AddElement(indexes())
    If comp=CompareMemory(*string, *separator, len_separator) : end_index+1 : count_index+1 : skip_first=1 : EndIf
    If add_first
      If skip_first And start_index : start_index-2 : end_index-1 : EndIf
    Else
      If skip_first And start_index : start_index-1 : EndIf 
    EndIf
    i=-2
    Repeat
      i+2 : comp=CompareMemory(*string+i, *separator, len_separator)
      If comp
        iii=i : count_index+1 : ii+1 : i+len_separator-2 : amount_indexes+1 : AddElement(index()) : index()=i+2
      EndIf 
      *pointer=*string+i+len_separator
    Until count_index>end_index Or Not *pointer\w
    iiii=i : byte_pos_last=iii+len_separator
    If comp=CompareMemory(*string+i-len_separator-1, *separator, len_separator) : skip_last=1 : EndIf
    If end_index>count_index : end_index=count_index : EndIf : amount_indexes=ii : i=skip_first
    If start_index>amount_indexes : start_index=amount_indexes-1 : EndIf : i+start_index+skip_first+skip_last
    If skip_first : i-1 : EndIf : If skip_last : i-1 : EndIf
    If amount_indexes
      Repeat 
        If ListSize(index())>i : SelectElement(index(), i) : pos_1=index() : EndIf
        If ListSize(index())>i+1 : SelectElement(index(), i+1) : pos_2=index() : EndIf
        length_result=(pos_2-pos_1-len_separator)>>1
        If pos_2-pos_1>0
          If length_result>0
            result$=PeekS(*string+pos_1, length_result) : AddElement(indexes()) : indexes()=result$ 
          Else 
            empty_fields+1
            If ignore_empty_fields : result$=#Null$ : Else : result$="" : AddElement(indexes()) : EndIf         
          EndIf
        EndIf
        i+1
      Until i>end_index Or i=amount_indexes
      If add_last And skip_last
        result$=PeekS(*string+byte_pos_last, (iiii-byte_pos_last+len_separator)>>1)
        AddElement(indexes()) : indexes()=result$ 
      EndIf
    EndIf
    ProcedureReturn 1
  EndProcedure
  
  Procedure.s GetStringFields_BF(field)
    If field <1 : ProcedureReturn "" : EndIf
    SelectElement(indexes(), field) : If field<ListSize(indexes()) : ProcedureReturn indexes() : EndIf
  EndProcedure
  
  Procedure FreeAllStringFields_BF()
    ClearList(index()) : AddElement(index()) : ClearList(indexes()) : AddElement(indexes())
    empty_fields.q=0 : start_index.q=1 : end_index.q=-1 : ProcedureReturn 1
  EndProcedure
  
  Procedure Data_InCode_Encoder_AES_BF(path$="", data_name$="Str_01$+", length_data_line=100, character_offset=230, password$="")
    ; Sakis data Generator - This code put the generated data in your Clipboard - You see nothing
    Protected i, ii, iii, i1, depth, breaks, *buffer, len_buffer, file, lof_file, len_source.q, padding=3
    Protected string$, new_string$, temp_string$, last$, length$, iv$
    Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_MD5) : Dim register.q(3)
    Repeat ; 16 Bytes
      PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
    Until ii=StringByteLength(fixed$) : i=0 : ii=0
    If OpenCryptRandom() : CryptRandomData(@register(2), 16) : Else : RandomData(@register(2), 16) : EndIf
    register(0)!register(2) : register(1)!register(3) 
    Macro Create_length_string
      length$=LSet(Chr(character_offset<<8), 6, Chr(character_offset<<8))
      ii=0 : For i=0 To 5 : PokeA(@length$+ii, PeekA(@len_source+i)) : ii+2 : Next
    EndMacro
    Macro Create_string
      iv$=LSet(Chr(character_offset<<8), 16, Chr(character_offset<<8))
      ii=0 : For i=0 To 15 : PokeA(@iv$+ii, PeekA(@register(2)+i)) : ii+2 : Next
    EndMacro
    If path$="" : path$=OpenFileRequester("Select source", "", "", 0) : If path$="" : ProcedureReturn 0 : EndIf : EndIf
    If FileSize(path$)<1 : ProcedureReturn 0 : EndIf
    file=ReadFile(#PB_Any, path$) : If Not file : ProcedureReturn 0 : EndIf
    lof_file=Lof(file) : *buffer=AllocateMemory(lof_file)
    If Not ReadData(file, *buffer, lof_file) : CloseFile(file) : ProcedureReturn 0 : EndIf : CloseFile(file)
    
    UseLZMAPacker()
    Protected length_uncompressed$, length_uncompressed.q=lof_file
    Protected *buffer_compressed=AllocateMemory(lof_file*1.5)
    Protected size_compressed=CompressMemory(*buffer, lof_file, *buffer_compressed,
                                             MemorySize(*buffer_compressed), #PB_PackerPlugin_Lzma)
    If size_compressed<lof_file
      compression_percent=100-100/length_uncompressed*size_compressed
      CopyMemory(*buffer_compressed, *buffer, size_compressed)
      len_buffer=size_compressed+padding
    Else
      compression_percent=-1
      len_buffer=lof_file+padding
    EndIf
    FreeMemory(*buffer_compressed)
    len_source.q=len_buffer-3
    Macro Create_length_uncompressed
      length_uncompressed$=LSet(Chr(character_offset<<8), 6, Chr(character_offset<<8))
      ii=0 : For i=0 To 5 : PokeA(@length_uncompressed$+ii, PeekA(@length_uncompressed+i)) : ii+2 : Next i
    EndMacro
    
    breaks=len_buffer/length_data_line : breaks+Bool(len_buffer%length_data_line) : string$=Space(len_buffer+breaks-1)
    For i=0 To len_buffer-1 : ii+1 : iii+1
      Repeat
        If Not i1 : AESEncoder(@register(0), @register(0), 16, @register(0), 128, 0, #PB_Cipher_ECB) : EndIf
        depth=PeekA(@register(0)+i1) : i1+1 : If i1>15 : i1=0 : EndIf
      Until depth
      PokeA(@string$+i+ii, character_offset) : PokeA(@string$+i+ii-1, PeekA(*buffer+i)+depth)
      If Not iii%length_data_line : ii+1 : PokeW(@string$+i+ii, 10) : ii+1 : EndIf
    Next 
    If breaks<2
      Create_length_string : Create_string : Create_length_uncompressed
      SetClipboardText(data_name$+Chr(34)+string$+Chr(34)+"+"+Chr(34)+iv$+length_uncompressed$+length$+Chr(34))
      ProcedureReturn 1
    EndIf
    iii=(length_data_line+Len(data_name$)+3) : new_string$=Space(breaks*iii) : ii=0 : iii*2
    StringFields_BF(string$, #LF$, 3)
    For i=1 To breaks
      temp_string$=data_name$+Chr(34)+GetStringFields_BF(i)+Chr(34)+#LF$
      CopyMemory(@temp_string$, @new_string$+ii, iii) : ii+iii 
    Next
    new_string$+data_name$+Chr(34) : Create_length_string : Create_string : Create_length_uncompressed
    new_string$+iv$+length_uncompressed$+length$+Chr(34)
    SetClipboardText(new_string$) : FreeAllStringFields_BF()
    ProcedureReturn 1
  EndProcedure
  
  Procedure CompressionPercent_ICEA_BF() : ProcedureReturn compression_percent : EndProcedure
  
EndModule
UseModule Data_Tool_InCode_Encoder_AES_BF

; #################### Encode - You found the data in your Clipboard ########################
CompilerIf #PB_Compiler_IsMainFile
  ; Data_InCode_Encoder_AES_BF() ; Preset
  Define password$=""
  Define len_line=100
  Define character_offset=230
  Data_InCode_Encoder_AES_BF("", "Str_01$+", len_line, character_offset, password$)
  MessageRequester("Encoder", "Encode finished"+#LF$+#LF$+ "The data are now in your Clipboard"+#LF$+#LF$+
                              Str(CompressionPercent_ICEA_BF())+" % compression")
CompilerEndIf

Decoder with AES encryption, crypt randomized IV and LZMA compression

Code: Select all

DeclareModule Data_Tool_InCode_Decoder_AES_BF
  EnableExplicit
  ; Sakis data InCode Generator © - Decoder - This code is free for use, changing and enhancing
  ; Variant with integrated AES encryption, automatically crypt randomized IV and compression
  ; character_offset - Base character offset - As sample 30, 160, 230 - Try what ever you want
  ; Seed - set as a integer - This works as password for the integrated data crypter
  Declare Data_InCode_Decoder_AES_BF(Data_BF$, password$="")
  Declare ChatchImageInCode_BF(Data_01$, password$="") ; Catch a image
  Declare CompressionPercent_ICDA_BF()                 ; This function show the actual compression as percent
EndDeclareModule

Module Data_Tool_InCode_Decoder_AES_BF
  ; You can remove what you not want
  UsePNGImageDecoder() : UseJPEGImageDecoder() : UseGIFImageDecoder() : UseTIFFImageDecoder() : UseMD5Fingerprint()
  Global compression_percent.d
  
  Procedure Data_InCode_Decoder_AES_BF(Data_BF$, password$="")
    ; Sakis data Generator - This function give back a pointer to a allocated memory with your data
    Protected *data_start_pointer=@Data_BF$, len_line=Len(Data_BF$), len_data.q, data_size=len_line*2-2
    Protected i, ii, i1, depth, offset, *buffer, *catch_buffer, len_buffer=data_size/2, *catch_buffer_new
    Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_MD5) : Dim register.q(3)
    Repeat ; 16 Bytes
      PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
    Until ii=StringByteLength(fixed$) : i=0 : ii=0 : *buffer=AllocateMemory(data_size)
    CopyMemory(*data_start_pointer, *buffer, data_size) : *catch_buffer=AllocateMemory(data_size/2-offset)
    ii=56 : For i=0 To 15 : PokeA(@register(2)+i, PeekA(*data_start_pointer+StringByteLength(Data_BF$)-ii)) : ii-2 : Next
    ii=0 : register(0)!register(2) : register(1)!register(3) 
    For i=0 To len_buffer-offset-1 : If Not i%len_line And i : ii+2 : EndIf
      Repeat
        If Not i1 : AESEncoder(@register(0), @register(0), 16, @register(0), 128, 0, #PB_Cipher_ECB) : EndIf
        depth=PeekA(@register(0)+i1) : i1+1 : If i1>15 : i1=0 : EndIf
      Until depth
      PokeA(*catch_buffer+i, PeekA(*buffer+ii)-depth) : ii+2
    Next
    
    Protected len_uncompressed
    ii=12 : For i=0 To 5 : PokeA(@len_data+i, PeekA(*data_start_pointer+StringByteLength(Data_BF$)-ii)) : ii-2 : Next
    ii=24 : For i=0 To 5 : PokeA(@len_uncompressed+i, PeekA(*data_start_pointer+StringByteLength(Data_BF$)-ii)) : ii-2 : Next
    *catch_buffer_new=AllocateMemory(len_data)
    CopyMemory(*catch_buffer, *catch_buffer_new, MemorySize(*catch_buffer_new))
    FreeMemory(*catch_buffer) : FreeMemory(*buffer)
    
    UseLZMAPacker()
    Protected *buffer_uncompressed=AllocateMemory(len_uncompressed)
    Protected size_uncompressed=UncompressMemory(*catch_buffer_new, MemorySize(*catch_buffer_new),
                                                 *buffer_uncompressed, MemorySize(*buffer_uncompressed),
                                                 #PB_PackerPlugin_Lzma)
    
    If size_uncompressed<>-1
      Swap *buffer_uncompressed, *catch_buffer_new
      compression_percent=100-100/size_uncompressed*len_data
    Else
      compression_percent=-1
    EndIf
    FreeMemory(*buffer_uncompressed)
    
    ProcedureReturn *catch_buffer_new
  EndProcedure
  Procedure ChatchImageInCode_BF(Str_01$, password$="")
    If Str_01$="" : ProcedureReturn 0 : EndIf
    Protected *catch_buffer=Data_InCode_Decoder_AES_BF(Str_01$, password$)
    If *catch_buffer : Protected image_ID=CatchImage(#PB_Any, *catch_buffer) : FreeMemory(*catch_buffer)
    ProcedureReturn image_ID : EndIf
  EndProcedure
  
  Procedure CompressionPercent_ICDA_BF() : ProcedureReturn compression_percent : EndProcedure
  
EndModule
UseModule Data_Tool_InCode_Decoder_AES_BF

; #################### Decoder Demo part ########################
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
  
  Define Str_01$
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
  
  ; Standard procedre
  ; Define *catch_buffer=Data_InCode_Decoder_AES_BF(Str_01$) 
  ; Define image_ID=CatchImage(#PB_Any, *catch_buffer)
  ; FreeMemory(*catch_buffer)
  
  ; Simplificated
  
  Define password$=""
  Define image_ID=ChatchImageInCode_BF(Str_01$, password$)
  
  Debug Str(CompressionPercent_ICDA_BF())+" % compression" ; This function show the actual compression as percent
  
  Define window_ID=OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If IsImage(image_ID)
    Define image_gadget_ID=ImageGadget(#PB_Any,
                                       WindowWidth(window_ID)/2-ImageWidth(image_ID)/2,
                                       WindowHeight(window_ID)/2-ImageHeight(image_ID)/2,
                                       0,
                                       0,
                                       ImageID(image_ID))
  Else
    CloseWindow(window_ID)
    MessageRequester("Hint", "Embedded image not usable")
    End
  EndIf
  
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
  
CompilerEndIf
Last edited by Saki on Sat Jun 26, 2021 9:11 am, edited 55 times in total.
地球上の平和
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Data_Tool_InCode_BF - Data outside Data Section - For Images and other

Post by Saki »

Codes updated and enhanced

The calculation of the last required byte and an automatic padding has been revised.

The latest version of the StringField_BF inserted.
地球上の平和
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Data_Tool_InCode_BF - Data outside Data Section - For Images and other

Post by blueb »

Thanks Saki,

I tried both your original version and the updated version to encrypt a simple JPG file using your "Encoder", placed the data on the clipboard, then using your "Decoder" sample, I removed your sample data provided and replaced it with my JPG's clipboard data from the "Encoder"

In both versions, I get an error on line 34... 'Invalid memory access'

Here's the JPG data from the encoder:

Code: Select all

Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
Data_01$+""
I tried changing the Window size thinking it was a sizing issue, but that made no difference. :(

PS - I notice that the code above shows "small squares" for characters, where PureBasic's IDE displays "small squares with a question mark inside".
I don't know if that will make a difference in your test.
Using 5.73 LTS (x64)
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Data_Tool_InCode_BF - Data outside Data Section - For Images and other

Post by Saki »

Hi blueb,
Many thanks for testing.
It is only a padding issue.
Add simple temporary a point at the end of the data and it works.
I have changed the code now.
Test it please with the new encoder again.

"FUN FACT:" (Content from your image) :wink:

Best regards Saki
地球上の平和
User avatar
blueb
Addict
Addict
Posts: 1041
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Data_Tool_InCode_BF - Data outside Data Section - For Images and other

Post by blueb »

Saki wrote: Sat May 22, 2021 1:39 pm Hi blueb,

"FUN FACT:" (Content from your image) :wink:
Works now Saki.
Fun Fact... Funny but true!
- It was too lonely at the top.

System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Data_Tool_InCode_BF - Data outside Data Section - For Images and other

Post by Saki »

Hi blueb.
Yes, that's absolutely sure.
But they always dig only on one side, never on the other, LOL.

Your picture still has me a little busy today.
It is broken, Windows can display it, PB but not, so it banged, the last byte is missing.
Took a while until I realized what's going on.

I have adjust the padding now again something.
地球上の平和
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Data_Tool_InCode_BF - Data outside Data Section - For Images and other + AES - Modules

Post by Saki »

New codes added.

A variant with supporting AES and a fully automatic randomized IV has been added.
This means that all encrypted data is encrypted completely differently for each encryption process.
The behavior of the crypter is similar to the CBC mode with randomized IV.
地球上の平和
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Data_Tool_InCode_BF - Data outside Data Section + AES + Compression - Modules

Post by Saki »

Variants with integrated fully automatically LZMA compression added.

This is the second approach to add compression.
Structuring and embedding is now easier and clearer.
The whole tool is quite complicated to code and debug.
Misbehavior can occur sporadically, which makes this even more difficult.
However, everything seems to be OK with compression now.
Please give it a try.
I might add compression to the other codes as well.
The effect is really enormous.

All codes here in this thread have been optimized.

These few data lines contain this text. :wink:
Image

Needed code for this output

Code: Select all

  
  Define Str_01$
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
Str_01$+""
  Debug PeekS(Data_InCode_Decoder_BF(Str_01$), -1, #PB_UTF8)
  
地球上の平和
Post Reply