Binary data visualization with OpenGL

Source-code only - please, don't post questions here.
IchMagBier
Posts: 25
Joined: Jan 13, 2018 8:47

Binary data visualization with OpenGL

Postby IchMagBier » Apr 30, 2018 14:06

Hello
After finding this, I decided to write my own "renderer" for binary data. Its just about 70 lines of FB/GL-Code:

Code: Select all

#include once "GL/gl.bi"
#include once "GL/glu.bi"

var ff=freefile

open "SOME FILE" for binary as #ff      <- Change this to a valid filepath
   
   dim shared as ubyte bytes(255,255,255)
   dim as ubyte byte1,byte2, layer
   dim as integer mx,my,mb,oldmx,oldmy
   dim as byte r,g,b

   dim as integer layermax=lof(ff)/255,layertimer
   get #ff,,byte2
   do
      byte1=byte2
      get #ff,,byte2
      layertimer+=1
      if layertimer>=layermax then
         layer+=1
         layertimer=0
      end if
      bytes(byte1,byte2,layer)+=4
   loop until eof(ff)
close #ff

screenres 1024,768,24,,2

glViewport 0, 0, 1024, 768                   
glMatrixMode GL_PROJECTION                   
glLoadIdentity                               
gluPerspective 45.0, 1024/768.0, 0.1, 100.0   
glMatrixMode GL_MODELVIEW                     
glLoadIdentity                                 
glClearColor 0.1, 0.0, 0.1, 0.0               
glClearDepth 1.0                             
glEnable GL_DEPTH_TEST                       
glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT 
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

glTranslatef 0, 0, -20
glRotatef 150,1,0,0
glRotatef 45,0,0,1
glPointSize(3)

do until inkey=chr(27)

   sleep 1,1
   oldmx=mx
   oldmy=my
   getmouse mx,my,,mb   
   
   glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
   glBegin gl_Points
   r=-1:g=127:b=127
   for z as integer=0 to 255 step 2
      r+=1:b-=1
      for y as integer=0 to 255
         for x as integer=0 to 255
            if bytes(x,y,z)>0 then
               glColor4b r,g,b,bytes(x,y,z)
               glVertex3f x/32-256/64, y/32-256/64, -z/32+256/64
            end if
         next
      next
   next
   glEnd
   
   if mb=1 then
      glRotatef (mx-oldmx), 0, 1, 0
      glRotatef (my-oldmy), 1, 0, 0
   end if
   
   flip
   
loop

Hold your left mouse button and then move the mouse to rotate the rendered file.

I took some images from the renders:
Image
The 64bit gcc-compiler. Notice the bright "cubes" in the center of the image. Those are the areas where the strings are stored in the file.
Image
Random bitmap-image.
Image
A 32bit library with a lot of images saved inside.
UEZ
Posts: 177
Joined: May 05, 2017 19:59
Location: Germany

Re: Binary data visualization with OpenGL

Postby UEZ » Apr 30, 2018 20:01

I like the idea - well done. :-)

Prost.
cbruce
Posts: 104
Joined: Sep 12, 2007 19:13
Location: Dallas, Texas

Re: Binary data visualization with OpenGL

Postby cbruce » Apr 30, 2018 20:13

.
Interesting... I guess you inspected the binary files to figure out where the location of the data was that was being rendered, so that you could tell "what is what" in the image.

Might be even more interesting to have the program inspect the data while it was reading it and create a key that displayed which datatype belonged to which color... so you would know what you were looking at without having to bring up a hex viewer on some large program. Might have to parse the file first to identify strings, etc. - and then assign colors based on that. Visual inspection of files might create some insights that we normally don't have.

I'll have to think if I can make more use of it than just pretty pictures.

Over all - very nice!
.
IchMagBier
Posts: 25
Joined: Jan 13, 2018 8:47

Re: Binary data visualization with OpenGL

Postby IchMagBier » May 01, 2018 4:43

cbruce wrote:Might be even more interesting to have the program inspect the data while it was reading it and create a key that displayed which datatype belonged to which color... so you would know what you were looking at without having to bring up a hex viewer on some large program. Might have to parse the file first to identify strings, etc. - and then assign colors based on that.

Like this?
Image
Yellow bytes are ASCII-letters, blue bytes are opcode-prefixes. The blue line is common for 64bit executables, but you won't find it in 32bit binaries. It's quite easy to achieve this. Just change the "bytes"-array to 3 seperate arrays containing the rgb-values and then check, if the bytes you were reading are between 65 and 122 and then assign the colors.
cbruce wrote:Visual inspection of files might create some insights that we normally don't have.

It can come in handy when you want to inspect an unknown file. It is pretty easy to distinquish images, sounds and strings in a tar-ball or a library for example. "Trigram"-visualization is also interesting. It uses 3 byte-patterns instead of 2.

This is the same program with a trigram and ASCII-highlighting:

Code: Select all

#include once "GL/gl.bi"
#include once "GL/glu.bi"

var ff=freefile

open "/usr/bin/gcc" for binary as #ff     
   dim as integer mx,my,mb,oldmx,oldmy

   dim shared as ubyte bytesr(255,255,255),bytesg(255,255,255),bytesb(255,255,255)
   dim as ubyte byte1,byte2,byte3
   get #ff,,byte2
   get #ff,,byte3
   do
      byte1=byte2
      byte2=byte3
      get #ff,,byte3
          
      If byte2>64 And byte2<123 And byte3>64 And byte3<123 Then
         If bytesr(byte1,byte2,byte3)<127 Then bytesr(byte1,byte2,byte3)+=8
         If bytesg(byte1,byte2,byte3)<127 Then bytesg(byte1,byte2,byte3)+=8
      Else
         If bytesr(byte1,byte2,byte3)<127 Then bytesr(byte1,byte2,byte3)+=8
         If bytesg(byte1,byte2,byte3)<127 Then bytesg(byte1,byte2,byte3)+=8
         If bytesb(byte1,byte2,byte3)<127 Then bytesb(byte1,byte2,byte3)+=8
      End If
   loop until eof(ff)
close #ff

screenres 1024,768,24,,2

glViewport 0, 0, 1024, 768                   
glMatrixMode GL_PROJECTION                   
glLoadIdentity                               
gluPerspective 45.0, 1024/768.0, 0.1, 100.0   
glMatrixMode GL_MODELVIEW                     
glLoadIdentity                                 
glClearColor 0.1, 0.0, 0.1, 0.0               
glClearDepth 1.0                             
glEnable GL_DEPTH_TEST                       
glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

glTranslatef 0, 0, -20
glRotatef 150,1,0,0
glRotatef 45,0,0,1
glPointSize(3)

do until inkey=chr(27)

   sleep 1,1
   oldmx=mx
   oldmy=my
   getmouse mx,my,,mb   
   
   glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
   glBegin gl_Points
   for z as integer=0 to 255 step 2
      for y as integer=0 to 255
         for x as integer=0 to 255
            if bytesg(x,y,z)>0 then
               glColor4b bytesr(x,y,z),bytesg(x,y,z),bytesb(x,y,z),bytesg(x,y,z)
               glVertex3f x/32-256/64, y/32-256/64, -z/32+256/64
            end if
         next
      next
   next
   glEnd
   
   if mb=1 then
      glRotatef (mx-oldmx), 0, 1, 0
      glRotatef (my-oldmy), 1, 0, 0
   end if
   
   flip
   
loop


UEZ wrote:Prost.

Prost!
cbruce
Posts: 104
Joined: Sep 12, 2007 19:13
Location: Dallas, Texas

Re: Binary data visualization with OpenGL

Postby cbruce » May 01, 2018 14:02

Nice... thanks! I just put that one in my toolbox.
tinram
Posts: 88
Joined: Nov 30, 2006 13:35
Location: UK

Re: Binary data visualization with OpenGL

Postby tinram » May 30, 2018 23:01

Great original concept and nifty OpenGL implementation!

I added the following after:

Code: Select all

#include once "GL/glu.bi"

to load different files more conveniently.

Code: Select all

DIM AS STRING filename
DIM AS INTEGER ff = FreeFile()
SCREENRES 300, 100, 32,, &h04

PRINT
INPUT " filename? ", filename

IF OPEN(filename FOR BINARY ACCESS READ AS #ff) <> 0 THEN
   PRINT " File not found / file read error!"
   SLEEP
   END
END IF

Return to “Tips and Tricks”

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests