PureBasic Constant Sniffer

Share your advanced PureBasic knowledge/code with the community.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

You guys rock with this tool. This would be usefull as a link in that sticky tool thread.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Deeem2031 wrote:Why you dont read the values for the constants, too?

Code: Select all

; PB Resident Sniffer for "PB_" constants 

open.s=OpenFileRequester("Res Sniffer","C:\Purebasic\Residents","*.res",1) 

If ReadFile(0,open) 
  size.l=Lof(0) 
  open = GetFilePart(open) 
  
  If CreateFile(1,open +".txt") 

    For a=0 To size ;Step 1 
      Number.b = ReadByte(0) 

      If Number = 80 ;Hex = 50 
        Number.b = ReadByte(0) 

        If Number = 66 ;Hex = 42 
          Number.b = ReadByte(0) 

          If Number = 95 ;Hex = 5F 
            StartPoint.l = Loc(0) - 3 
              
            Repeat 
              Number.b = ReadByte(0) 

              If Number = 0 
                EndPoint.l = Loc(0) 
                Break 
              EndIf 
            ForEver 
          
            BufferSize.l = EndPoint - StartPoint - 1 
            buffer=AllocateMemory(BufferSize) 
            FileSeek(0, StartPoint) 
            ReadData(0,buffer,BufferSize) 
            line.s=PeekS(buffer,BufferSize) 
            FreeMemory(buffer) 
    
            If line = "PB_Explorer_Column_Accessed" ;do nothing 
            ElseIf line = "PB_Explorer_Column_Attributes" ;do nothing 
            ElseIf line = "PB_Explorer_Column_Created" ;do nothing 
            ElseIf line = "PB_Explorer_Column_Modified" ;do nothing 
            ElseIf line = "PB_Explorer_Column_Name" ;do nothing 
            ElseIf line = "PB_Explorer_Column_Size" ;do nothing 
            ElseIf line = "PB_Explorer_Column_Type" ;do nothing 
            Else 
              ReadWord(0)
              WriteStringN(1,"#"+line+" = "+Str(ReadLong(0))) 
              ;Debug "#"+line+" = "+Str(ReadLong(0))
            EndIf 
            
          EndIf 
        EndIf 
      EndIf 
    Next 
    
    CloseFile(1) 
  EndIf 

  CloseFile(0) 
  
EndIf 

MessageRequester("HI", "ALL DONE") 
End
Values are wrong... at least here using V4.
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Post by Deeem2031 »

Yea i know, i tested it with pb3.94. I could give you a new code which can read all constants, structures, macros and prototypes from pb3.94 and pb4.0 residents, but Fred said i shouldnt do that. So find out for yourself ;)
irc://irc.freenode.org/#purebasic
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

here is a workaround that should work fine for v4+ ;)
Btw, nice work Num3 and Deeem2031 ;-)
The final file will saved to C:\ - just change it ^^

Code: Select all

; PB Resident Sniffer for "PB_" constants

open.s=OpenFileRequester("Res Sniffer","C:\Purebasic\Residents\","*.res",1)

If ReadFile(0,open)
  size.l=Lof(0)
  open = GetFilePart(open)
 
  If CreateFile(1,"c:\"+open +".txt")

    For a=0 To size   ;Step 1                       ; FIX
      Number.b = ReadByte(0)

      If Number = 80 ;Hex = 50        ; P
        Number.b = ReadByte(0)

        If Number = 66 ;Hex = 42      ; B
          Number.b = ReadByte(0)

          If Number = 95 ;Hex = 5F    ; _
            StartPoint.l = Loc(0) - 3
             
            Repeat
              Number.b = ReadByte(0)

              If Number = 0
                EndPoint.l = Loc(0)
                Break
              EndIf
            ForEver
         
            BufferSize.l = EndPoint - StartPoint - 1
            buffer=AllocateMemory(BufferSize)
            FileSeek(0, StartPoint)
            ReadData(0,buffer,BufferSize)
            line.s=PeekS(buffer,BufferSize)
            FreeMemory(buffer)
   
            If line = "PB_Explorer_Column_Accessed" ;do nothing
            ElseIf line = "PB_Explorer_Column_Attributes" ;do nothing
            ElseIf line = "PB_Explorer_Column_Created" ;do nothing
            ElseIf line = "PB_Explorer_Column_Modified" ;do nothing
            ElseIf line = "PB_Explorer_Column_Name" ;do nothing
            ElseIf line = "PB_Explorer_Column_Size" ;do nothing
            ElseIf line = "PB_Explorer_Column_Type" ;do nothing
            Else

            ; -------- works for v4 ---- fixed va!n --------
               
            ValueType1 = ReadByte(0)
            ValueType2 = ReadByte(0)            

            Select ValueType2
              Case $0
                 value = ReadByte(0)
              Case $1
                 value = ReadWord(0)
              Case $2
                 value = ReadLong(0) 
            EndSelect

            WriteStringN(1,"#"+line+" = "+Str(value))
        ;   Debug "#"+line+" = "+Str(value)
            EndIf
           
          EndIf
        EndIf
      EndIf
    Next
   
    CloseFile(1)
  EndIf

  CloseFile(0)
 
EndIf

MessageRequester("HI", "ALL DONE")
End
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

Tested with PB4 beta9 and beta10 - seems to works fine ^^
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Deeem2031 wrote:... but Fred said i shouldnt do that.
Got that, thanks.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

I have modified the code above.
This (PB3.94) code seems to work with any (tested) resident :

Code: Select all

;
; PB Resident Constant Extractor
;

#RESFILE = 0

Procedure ExtractConstants(ResFile.s)
  Protected Size.l, *Buffer, *ReadLong.LONG, *ReadByte.BYTE, Value.l, ValueOk.l, StartPoint.l, EndPoint.l
  Protected Number.l, Number1.l, Number2.l, Number3.l, Number4.l, ReadEnd.l, Line.s, ValueType1.l, ValueType2.l
  Static IsInitialized.l
  ;
  If IsInitialized = #False
    IsInitialized = #True
    Dim IsValidCNST(255)
    IsValidCNST('_') = #True
    For i = '0' To '9'
      IsValidCNST(i) = #True
    Next
    For i = 'A' To 'Z'
      IsValidCNST(i) = #True
    Next
    For i = 'a' To 'z'
      IsValidCNST(i) = #True
    Next
  EndIf
  ;
  If ReadFile(#RESFILE, ResFile) 
    Size = Lof()
    *Buffer = AllocateMemory(Size)
    If *Buffer
      If ReadData(*Buffer, Size)
        *ReadLong = *Buffer
        If *ReadLong\l = 'PURE'
          ReadEnd = *Buffer + Size
          While *ReadLong < (ReadEnd - 3)
            If *ReadLong\l = 'CNST'
              *ReadByte = *ReadLong + SizeOf(LONG)
              While *ReadByte < (ReadEnd - 3)
                Number1 = *ReadByte\b & $FF
                *ReadByte + SizeOf(BYTE)
                Number2 = *ReadByte\b & $FF
                *ReadByte + SizeOf(BYTE)
                Number3 = *ReadByte\b & $FF
                *ReadByte + SizeOf(BYTE)
                Number4 = *ReadByte\b & $FF
                *ReadByte + SizeOf(BYTE)
                If IsValidCNST(Number1) And IsValidCNST(Number2) And IsValidCNST(Number3) And IsValidCNST(Number4)
                  StartPoint = *ReadByte - 4
                  While *ReadByte < ReadEnd
                    Number = *ReadByte\b & $FF
                    *ReadByte + SizeOf(BYTE)
                    If IsValidCNST(Number) = #False
                      EndPoint.l = *ReadByte - SizeOf(BYTE)
                      *ReadByte - SizeOf(BYTE)
                      Break 
                    EndIf
                    If *ReadByte = ReadEnd
                      Break 2
                    EndIf
                  Wend
                  ReadSize.l = EndPoint - StartPoint
                  Line.s = PeekS(StartPoint, ReadSize) 
                  If Line = "PB_Explorer_Column_Accessed"
                    ; is prop, not constant
                  ElseIf Line = "PB_Explorer_Column_Attributes"
                    ; is prop, not constant
                  ElseIf Line = "PB_Explorer_Column_Created"
                    ; is prop, not constant
                  ElseIf Line = "PB_Explorer_Column_Modified"
                    ; is prop, not constant
                  ElseIf Line = "PB_Explorer_Column_Name"
                    ; is prop, not constant
                  ElseIf Line = "PB_Explorer_Column_Size"
                    ; is prop, not constant
                  ElseIf Line = "PB_Explorer_Column_Type"
                    ; is prop, not constant
                  ElseIf Line = "MACR"
                    ; MACRO
                    Break
                  ElseIf Line = "PROT"
                    ; PROTOTYPE
                    Break
                  Else 
                    ValueType1 = *ReadByte\b
                    *ReadByte + SizeOf(BYTE)
                    ValueType2 = *ReadByte\b
                    *ReadByte + SizeOf(BYTE)
                    ;
                    Select ValueType2 
                      Case $0 
                        Value = PeekB(*ReadByte)
                        *ReadByte + SizeOf(BYTE)
                        ValueOk = #True
                      Case $1 
                        Value = PeekW(*ReadByte)
                        *ReadByte + SizeOf(WORD)
                        ValueOk = #True
                      Case $2 
                        Value = PeekL(*ReadByte)
                        *ReadByte + SizeOf(LONG)
                        ValueOk = #True
                      Default 
                        ValueOk = #False
                    EndSelect 
                    ;
                    If ValueOk
                      ;
                      Debug "#"+Line+" = "+Str(Value)
                      ;
                    EndIf
                    ;
                  EndIf  
                Else
                  *ReadByte -3
                EndIf 
              Wend
              Break
            Else
              *ReadLong + 1
            EndIf 
          Wend
        EndIf
      EndIf
      FreeMemory(*Buffer)
    EndIf
    CloseFile(0) 
  EndIf 
EndProcedure

file.s="C:\Purebasic400\Residents\Purebasic.res"
;file.s="C:\Purebasic400\Residents\PureLVSORT.res"
;file.s="C:\Purebasic400\Residents\PureCOLOR.res"
;file.s="C:\Purebasic400\Residents\InterfaceDX.res"
;file.s="C:\Purebasic400\Residents\Windows.res"

ExtractConstants(file)
So maybe I will add a constant tab to the jaPBe structure viewer.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Post Reply