Page 1 of 1

File2Data

Posted: Tue Aug 11, 2009 1:09 am
by Peyman
hi, How can i convert any file to datasection

for examp convert file.bin to

Code: Select all

DataSection  
  ByteCode:  
  Data.b $01,$02,$74,$65,$73,$74,$00,$03,$48,$65,$6C,$6C  
  Data.b $6F,$21,$00,$03,$54,$68,$69,$73,$20,$69,$73,$20  
  Data.b $61,$20,$73,$6D,$61,$6C,$6C,$20,$63,$6F,$6E,$73  
  Data.b $6F,$6C,$65,$20,$74,$65,$73,$74,$2E,$00,$03,$00  
  Data.b $03,$70,$72,$65,$73,$73,$20,$3C,$52,$45,$54,$55  
  Data.b $52,$4E,$3E,$20,$74,$6F,$20,$63,$6F,$6E,$74,$69  
  Data.b $6E,$75,$65,$2E,$2E,$2E,$00,$04,$05,$FF  
EndDataSection
i know this way is easier to use

Code: Select all

DataSection
  File:
  IncludeBinary "C:\file.bin"
EndDataSection
but i want know how can i convert my files to these bytes ?
anybody have a code or program ?
thanks

Posted: Tue Aug 11, 2009 1:19 am
by netmaestro
There is code for PicPak on the forums, it's the same thing made specially for images. A search should find it without difficulty. I don't know if there's something else posted or not.

Posted: Tue Aug 11, 2009 12:56 pm
by Peyman
thanks netmaestro for reply i see PicPak but i want a code to convert all files to data.
i write these codes but this is low working on big files and output is very large somebody can optimize it:

Code: Select all

open.s = OpenFileRequester("Open", "", "All files (*.*)|*.*", 0)

If open
  
  save.s = SaveFileRequester("Save in", "Data Section.pbi", "PureBasic (*.pb;*.pbi)|*.pb;*.pbi|All files (*.*)|*.*", 0)
  
  If save
    OpenFile(0, open)
    mem = AllocateMemory(Lof(0))
    ReadData(0, mem, Lof(0))
    
    CreateFile(1, save)
    WriteStringN(1, "DataSection")
    WriteStringN(1, " Start_" + Mid(GetFilePart(open), 1, FindString(GetFilePart(open), ".", 1)-1) + ":")
    
    WriteString(1, "  Data.b " + "$" + RSet(Right(Hex(PeekB(mem)), 2), 2, "0"))
    
    For pos=1 To Lof(0)-1
      
     WriteString(1, "," + "$" + RSet(Right(Hex(PeekB(mem+pos)), 2), 2, "0"))
      
      If pos%20 = 0
        WriteStringN(1, "")
        WriteString(1, "  Data.b " + "$" + RSet(Right(Hex(PeekB(mem+(pos+1))), 2), 2, "0"))
        pos+1
      EndIf
    
    Next
    
    WriteStringN(1, "")
    WriteStringN(1, " End_" + Mid(GetFilePart(open), 1, FindString(GetFilePart(open), ".", 1)-1) + ":")
    WriteStringN(1, "EndDataSection")
    
    
    CloseFile(0)
    CloseFile(1)
    FreeMemory(mem)
  
  EndIf
EndIf


Posted: Tue Aug 11, 2009 2:39 pm
by Vera
Hello Peyman,

just the other day I came across a small tool that generates Assembler-sourcecode out of any file. It's by a german programer but the GUI is in english and easy to understand, though I don't know if the data is unique enough to be used with any language.
HP: http://frabbing.bplaced.net/_asmdg.html (everything is free to use)
DL: http://frabbing.bplaced.net/AsmDatagenerator.zip


good success ~ Vera

Posted: Tue Aug 11, 2009 9:46 pm
by utopiomania
Use includeBinary.. If you insist on converting a file to a datasection this way you cannot do something
about the size problem, so why bother?

Re: File2Data

Posted: Tue Aug 11, 2009 10:20 pm
by Psychophanta
Peyman wrote:hi, How can i convert any file to datasection

for examp convert file.bin to

Code: Select all

DataSection  
  ByteCode:  
  Data.b $01,$02,$74,$65,$73,$74,$00,$03,$48,$65,$6C,$6C  
  Data.b $6F,$21,$00,$03,$54,$68,$69,$73,$20,$69,$73,$20  
  Data.b $61,$20,$73,$6D,$61,$6C,$6C,$20,$63,$6F,$6E,$73  
  Data.b $6F,$6C,$65,$20,$74,$65,$73,$74,$2E,$00,$03,$00  
  Data.b $03,$70,$72,$65,$73,$73,$20,$3C,$52,$45,$54,$55  
  Data.b $52,$4E,$3E,$20,$74,$6F,$20,$63,$6F,$6E,$74,$69  
  Data.b $6E,$75,$65,$2E,$2E,$2E,$00,$04,$05,$FF  
EndDataSection
i know this way is easier to use

Code: Select all

DataSection
  File:
  IncludeBinary "C:\file.bin"
EndDataSection
but i want know how can i convert my files to these bytes ?
anybody have a code or program ?
thanks
Yes,
ts-soft made it in the past:
http://www.purebasic.fr/english/viewtopic.php?t=18203
8)

Posted: Wed Aug 12, 2009 1:56 pm
by TeddyLM
Something like this?

Code: Select all

;****************************************
Procedure CreateDataFile(OldFilename$, NewFilename$, NbrBytes.l, Format.l)
    NewFileID = CreateFile(#PB_Any, NewFilename$)
    If NewFileID
        WriteStringN(NewFileID, "DataSection")
        OldFileID = ReadFile(#PB_Any, OldFilename$)
        If OldFileID
            CharPos = 0 
            Offset = 0
            ASCII_Dump$ = ""
            VALUE_Dump$ = ""
            While Eof(OldFileID) = 0
                ThisByte.b = ReadByte(OldFileID)
                ;Get Value
                Select Format
                    Case 0
                        ThisValue$ = "$"+RSet(Hex(ThisByte.b & $ff),2,"0")
                    Default
                        ThisValue$ = RSet(Str(ThisByte.b & $ff),3,"0")
                EndSelect
                ;Get Char
                If ThisByte.b < 32
                    ASCII_Dump$ = ASCII_Dump$ + "."
                Else
                    ASCII_Dump$ = ASCII_Dump$ + Chr(ThisByte.b)
                EndIf
                ;End of Line ?
                CharPos = CharPos + 1
                If CharPos < NbrBytes
                    VALUE_Dump$ = VALUE_Dump$ + ThisValue$ + ", "  
                Else
                    VALUE_Dump$ = VALUE_Dump$ + ThisValue$
                    WriteStringN(NewFileID, "    Data.b " + VALUE_Dump$ + "  ;" + RSet(Str(Offset),8,"0") + "(" + RSet(Hex(Offset),8,"0") + ")  " + ASCII_Dump$)
                    ASCII_Dump$ = ""
                    VALUE_Dump$ = ""
                    Offset = Offset + NbrBytes
                    CharPos = 0
                EndIf    
            Wend
            If CharPos > 0
                If Right(VALUE_Dump$, 2) = ", " : VALUE_Dump$ = Left(VALUE_Dump$, Len(VALUE_Dump$)-2) : EndIf
                WriteStringN(NewFileID, "    Data.b " + VALUE_Dump$ + "  ;" + RSet(Str(Offset),8,"0") + "(" + RSet(Hex(Offset),8,"0") + ")  " + ASCII_Dump$)
            EndIf
            CloseFile(OldFileID) 
        Else
            MessageRequester("","Unable to read the old file",16)
        EndIf
        WriteStringN(NewFileID, "EndDataSection")
        CloseFile(NewFileID)
    Else
        MessageRequester("","Unable to create the new file",16)
    EndIf
EndProcedure
;****************************************
SourceFile$ = "C:\windows\system32\calc.exe" 
TargetFile$ = SourceFile$ + ".pbi"
NbrBytes.l = 16       ;Nbr bytes per line to display
Format.l = 1          ;0=Hexadecimal  1=Decimal
CreateDataFile(SourceFile$, TargetFile$, NbrBytes, Format)
End

Posted: Wed Aug 12, 2009 2:16 pm
by myutopia
@TeddyLM
Wow... Neat! :D

Posted: Fri Aug 14, 2009 7:51 am
by pablov
try this

Code: Select all

EnableASM

Global ResT.s=Space(4) 
Procedure.s Hex2Str(HB) 
   MOV eax, ResT 
   MOV edx, HB 
   MOV byte [eax], "$" 
   INC eax 
   MOV dh,dl                
   MOV cl,4 
   SHR dl,cl 
   CMP dl,0Ah        
   JAE HEX_LETTER 
   ADD dl,30h 
   JMP WRITE_DIGIT 
! HEX_LETTER: 
   ADD  dl,37h 
! WRITE_DIGIT: 
   MOV [eax], dl 
   INC eax 
   MOV dl,dh 
   And dl,0Fh 
   CMP dl,0Ah        
   JAE HEX_LETTER1 
   ADD dl,30h 
   JMP WRITE_DIGIT1 
! HEX_LETTER1: 
   ADD dl,37h 
! WRITE_DIGIT1: 
   MOV [eax], dl 
   INC eax 
   MOV byte[eax], 0 
 ProcedureReturn ResT 
EndProcedure 

;LOCALS 
 byte.b 
 string.s 
 n.b 
  
 file$=OpenFileRequester("Choose a file", "", "*.*", 0) 
If file$ 
 hFileCr.l=CreateFile_(file$,#GENERIC_READ,#FILE_SHARE_READ|#FILE_SHARE_WRITE,NULL,#OPEN_EXISTING,GetFileAttributes_(file$),NULL)      
   If hFileCr = -1 Or hFileCr = 0 
             MessageRequester("Attention", "Can Not open file" + Chr(10) + "Possible file is occupied other program.", #MB_OK|#MB_ICONWARNING) 
      Else 
           CreateFile(0, GetPathPart(file$) + "DataSec.txt") 
           WriteStringN(0, ";      " + file$) 
           length.l = GetFileSize_(hFileCr, pFileSizeHigh.l) 
           hFileMap.l = CreateFileMapping_(hFileCr, 0, 8, 0, 0, 0)      
           MapVOF.l = MapViewOfFile_(hFileMap, 1, 0, 0, length)  
           PUSH eax 
           CloseHandle_(hFileCr)    
           MOV n, 16                                      
     ! lop: 
           XOR ebx, ebx 
           MOV eax, MapVOF 
           MOV bl, [eax] 
           INC eax 
           MOV MapVOF, eax 
           MOV byte, bl 
           DEC length                                  
           JZ endfile 
           DEC n 
           JNZ NoW 
           string=string + Hex2Str(byte) 
           WriteStringN(0, "Data.b  "+ string) 
           MOV eax, string 
          !MOV byte[eax], 0                   ;         string="" 
           MOV n, 16 
           JMP lop 
     ! NoW: 
           string=string+Hex2Str(byte) + ", "                  
           JMP lop 
     ! endfile: 
           string=string+Hex2Str(byte) 
           WriteStringN(0, "Data.b  "+ string) 
           POP eax 
           MOV MapVOF, eax 
          
     UnmapViewOfFile_(MapVOF) 
     MessageRequester("Saved", "Data are successfully saved in file 'DataSec.txt'", #MB_OK|#MB_ICONINFORMATION) 
   EndIf 
EndIf 
  End

DisableASM