Page 1 of 1

getting ALL the data from a .exe file? Please Help Me

Posted: Wed Sep 12, 2007 7:26 pm
by Evan
I need help?
I want to make a program that encrypts any file. so I tried to make a program that would grab the data from a .exe file but ran into some problems

first I ran out of memory so i figurd that i could take the header from the .exe file and put in a new file and then erase the header from. But i do not know how to erase the header from the .exe file.

Code: Select all

dim header as string * 60000
dim body as string * 60000
dim footer as string * 6000
open "c:\test.exe" for binary as #1
get #1, ,header
get #1, body
get #1, footer
close

Thanks

No idea what you are talking about

Posted: Wed Sep 12, 2007 8:21 pm
by Mac
DIM $$$
There is no such thing.

Who told you that EXE files consisted of three sections of 60000 each?

Simply read the file as binary. There are no sections to remove.

Mac

Posted: Wed Sep 12, 2007 8:43 pm
by Evan
As for the $$$ that is on error on the web page. it dose not like the word h-e-a-d-e-r but without the '-'
ok!
lets try this again
:oops:

Code: Select all

dim header as string * 460 'I know that the h-e-a-d-e-r is around 460 
characterslong


dim body as string * 6000 'I dont know how many characters the middle part of the .exe file but it is more then 60000  

dim footer as string * 6000 I dont know how many character it is
open "c:\test.exe" for binary as #1
get #1, ,header
get #1, body
get #1, footer
close 

Here is my problem:
when i grab 60000 characters from the beginning of the file i don't have the space to get any more, well its less then 60000, about 40000so how do i grab the rest of the file.
the input statement dose not work because it comes to a part of the file and thinks that it is the end of the file when it not.

thanks

Posted: Thu Sep 13, 2007 1:23 am
by Anonymous
QBasic has its limits when it comes to how much memory you can access. You need to only load parts of the file into the memory, process it and then replace it with the next bit of data from the file. After all what would you do if you had 100mb file, you wouldn't just load it all into the memory.

Also you may want to figure out the size of the file you are loading so you load the correct amount of data (don't use an integer to store that or you will have problems, try a double instead)

Posted: Thu Sep 13, 2007 12:45 pm
by Evan
Right but what I want to know is after getting data form the begenning of the file to the middle of the file then gett the data from the middle of the file to the end of the file in binary.

thanks

Posted: Fri Sep 14, 2007 5:22 am
by Mac
Evan wrote:Right but what I want to know is after getting data form the begenning of the file to the middle of the file then gett the data from the middle of the file to the end of the file in binary.
You probably didn't understand. You don't get blocks of data. You get one character at a time. Here is a simple program to encrypt an EXE by adding 1 to each character.

Code: Select all

OPEN "z.exe" FOR BINARY AS #1
OPEN "z.xxx" FOR BINARY AS #2
DIM i AS LONG, x AS STRING * 1
DIM a AS INTEGER
FOR i = 1 TO LOF(1)
  GET #1, i, x
  a = ASC(x) + 1: IF a > 255 THEN a = 0
  LSET x = CHR$(a)
  PUT #2, i, x
NEXT i
CLOSE
SYSTEM
Mac

Posted: Fri Sep 14, 2007 6:26 pm
by Anonymous
Actual you do, the string size you use to get the data determines how much data you get from a file at one time, it is actually quicker to get a bigger block than smaller ones because the HDD has to go get the data each time time you call a get.

so this code

Code: Select all

Dim SomeData as string * 15 'will get 15 bytes
Test$ = "    " 'Four spaces will get 4 bytes
open "c:\test.exe" for binary as #1
get #1, SomeData
get #1, Test$
close #1
http://www.petesqbsite.com/sections/tut ... _bmps.html This shows how to load a bmp header with 1 get. Useful way to use types.

Also you can run into problems when you try to get integers in FB as they are a different size to the ones in QB, so it gets more data then you want give of the wrong data.

Posted: Fri Sep 14, 2007 8:19 pm
by Evan
Thank you MAC!!!!
:P

Posted: Sat Sep 15, 2007 9:43 am
by Mac
Nixon wrote:Actually you do [read/write blocks]
True. But a blocksize of 1 is best for this kind of operation. Disk i/o is the same as QBasic keeps a buffer anyway. It would be difficult to detect any difference in run time, but you may want to write some test programs to show me otherwise.

Why is 1 the best? Simplicity of code. Compare my original with this one that uses a blocksize other than 1.

Mac

Code: Select all

OPEN "z.bas" FOR BINARY AS #1
OPEN "z.xxx" FOR OUTPUT AS #2: CLOSE #2: 'reset size to zero
OPEN "z.xxx" FOR BINARY AS #2
CONST blocksize = 50
DIM i AS LONG, x AS STRING * 1, xx AS STRING * blocksize
DIM a AS INTEGER, j AS INTEGER
DIM LOF1 AS LONG: LOF1 = LOF(1)
FOR i = 1 TO LOF1 - blocksize STEP blocksize
  GET #1, i, xx
  FOR j = 1 TO blocksize
    a = ASC(MID$(xx, j, 1)) + 1: IF a > 255 THEN a = 0
    MID$(xx, j, 1) = CHR$(a)
  NEXT j
  PUT #2, i, xx
NEXT i
i = LOF(2)
DO WHILE i < LOF1 '
  i = i + 1
  GET #1, i, x
  a = ASC(x) + 1: IF a > 255 THEN a = 0
  LSET x = CHR$(a)
  PUT #2, i, x
LOOP
CLOSE
SYSTEM

Posted: Sat Sep 15, 2007 9:49 am
by Mac
Evan wrote:Thank you
You're welcome, but I have a bug in the code I gave you.

Insert
OPEN "z.xxx" FOR OUTPUT AS #2: CLOSE #2: 'reset size to zero
before
OPEN "z.xxx" FOR BINARY AS #2

(assuming you use two files. If you are encrypting in place, you will have only one file and no need for the FOR OUTPUT).

The reason: When creating a binary file, if there is one already there, it's size cannot be reduced.

So if I encrypt a 500-character file into a 700-character file, I will get a 700-character file, the last 200 characters being crap left over from before. To avoid that, erase or make the output file zero in length. The OUTPUT command does that.

Sorry,

Mac