Hex editing with C++ - c++

Let's assume I want to change something in the 000F5344 address of an executable. How do I go about it?

#Pablo Santa Cruz provides an excellent way, but in C. If you prefer to go pure C++, here's how:
Open file: fstream::open (remember to use the binary flag)
Set the put pointer position: fstream::seekp
Write data at the put pointer position: fstream::put
Close file: fstream::close
This is by no means better than the C version though.

Four steps solution follows:
Open the file with binary flag (use fopen).
Move to the address (offset) (use fseek).
Write your data (use fwrite).
Close the file (use fclose).
Use google to get help on the four mentioned functions calls. Will also work in C.

Related

How do you get a directory listing in Red/System?

I've tried to include Kaj de Vos's excellent C library binding available in ANSI.reds, but still cannot find a way to read a directory listing.
I thought that maybe if I could read the . file in binary, I could parse it, but I just get back a NULL pointer:
sizedir: 0
dir: read-file-binary "my-dir/." :sizedir
print-line dir
If I try reading it as a text file, I get a:
*** Runtime Error 1: access violation
*** at: 0040152Eh
Any ideas?
A piece of additional code and definitions needs to be written to read the directory structure and interpret it. It's tricky, because it's partly system dependent, so it goes beyond the ANSI part of my C library binding. Also, to achieve a higher abstraction level on the way to the directory functions in REBOL, you'd have to think about extra data structures that are not native to Red/System.

saving file position and returning in fortran 90

I am editing a fortran 90 to read a file. A particular file happens to be 'contaminated' with some extra information, so I wanted to attempt a read and then rewind; reread if in error:
open(filenum,file=filename,form="unformatted",iostat=ierr) //'direct' access
...lots of stuff...
here = ftell(filenum)
read(filenum,iostat=ierr) var1, var2 //try reading as var1, var2
if(iswrong(var1, var2)) then //check if correct
call fseek(filenum,here-ftell(filenum),1) //rewind
read(filenum,iostat=ierr) vara, varb, varc //read as different type
endif
However, when I compile this program, I get
Undefined symbols:
"_fseek_", referenced from:
___myreader__subroutine_name in myreader.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
(I am trying to compile on gfortran (i686-apple-darwin8-gfortran-4.2)). I understand that fseek is not a standard fortran routine.
I wonder if there is an alternative. I understand I can do something like read(filenum,rec=somevalue) but how can I use this in a similar fashion? I also thought to attempt reading with read(,advance='no') for testing and then reading again with advance='yes' if it is in the correct format, but this requires a specific format expression, which is not specified. Thank you.
You have to use backspace to go back to the previous record. Traditional Fortran IO is record based, non stream based (like in C)
On Linux and Windows, it is okay in general to compute seek offsets.
However, on record-oriented file systems (OpenVMS, OS/370, NOS, etc.), the value which comes from ftell() is a magic cookie and cannot be inspected or processed using trivial arithmetic to compute a new file location. Instead, use the rewind statement to go back to the beginning of a file (if it is indeed seekable).
There is another technique for processing input if you are uncertain about the file contents -- in this case sometimes the files contents are incorrect or different -- which is to read into a string, then examine the contents of the string. If the contents are valid you can use a read statement to read from the string. Or if the contents are "different", use a different read statement, or take appropriate action. No rewind or backspace is necessary. You do have to guess the longest possible line length to declare the length of the string.

Read from a file when a new line 's been written to it by another process

What is the fastest method in C++, to read a new line from a file which is written by another process. Or how my program can be notified that there is a new line in file so read it? (in linux)
The fastest method is to use pipes or events (for Windows apps).
If you still want use files, first of all that you really need, making sure, that a file has been really modified (use seek and compare it with prew value). Than go to the 'last val of seek' and read it.
And it will be better use mutex (if you read data from file).
Assuming the OS supports concurrent file access, all you should need to do is seek to EOF, wait for the stat to change then try to read from the file. You might want to add in a sleep to slow down the loop.
The 'tail' command on POISX (with the -f option) implements this - source code is available.
From the top of my head, did u tried something like this:
Count the lines in a file, store it.
Get the size of the file (google it, i dont want to ruin the fun :D ).
Then try to read from the last line u stored when size of the file changes... and again and again.
Have fun :)
Use inotify to get notification about file changes and then reread from your last pos if the file is now larger then before.

Editing an /etc/fstab entry in C++

I'm trying to edit the /etc/fstab file on a CentOS installation using C++. The idea being that based on another config file I will add entries that don't exist in the fstab, or edit entries in the fstab file where the mount point is the same. This lets us set the system up properly on initial bootup.
I've found setmntent() and getmntent() for iterating over the exiting entries so I can easily check whether an entry in fstab also exists in my config file. And I can then use addmntent() to add any entry that doesn't already exist - the documentation says nothing about this being able to edit an entry, only add a new entry to the end of the file. There seems to be no way to edit an existing entry or delete an entry. It seems odd that this feature doesn't exist, only the CR and not the UD of CRUD.
I'd rather not have to write my own parser if I can at all help it.
My other alternative is to:
open the file using setmntent()
read the whole of fstab into memory using getmentent() and perform any additions and/or edits
close the file using endmntent()
open /etc/fstab for writing
close /etc/fstab (thus emptying the file)
open the fstab using setmntent()
loop through the entries I read in previously and write them out using addmntent()
Which although probably fine, just seems a bit messy.
When modifying system configuration files such as /etc/fstab keep in mind that these are critical state and, should your "edit" be interrupted by a power loss, might result in a failure to reboot.
The way to deal with this is:
create an empty output:
FILE* out = setmntent("/etc/fstab.new", "rw");
open the original for input:
FILE* in = setmntent("/etc/fstab", "r");
copy the contents:
while (m = getmntent(in)) { addmntent(out, m); }
make sure the output has it all:
fflush(out); endmntent(out); endmntent(in);
atomically replace /etc/fstab:
rename("/etc/fstab.new", "/etc/fstab");
It's left as an exercise to the reader to change the body of the while loop to make a modification to an existing element, to substitute a specifically crafted mntent or whatever. If you have specific questions on that please ask.
UN*X semantics for rename() guarantee that even in the case of power loss, you'll have either the original version or your new updated one.
There's a reason why there is no modifymntent() - because that would encourage bad programming / bad ways of changing system critical files. You say at the end of your post "... probably fine ..." - not. The only safe way to change a system configuration file is to write a complete modified copy, sync that to safe storage, and then use rename to replace the old one.

Implementing "app.exe -instruction file" notation in C++

I have a project for my Data Structures class, which is a file compressor that works using Binary Trees and other stuff. We are required to "zip" and "unzip" any given file by using the following instructions in the command line:
For compressing: compressor.exe -zip file.whatever
For uncompressing: compressor.exe -unzip file.zip
We are programming in C++. I use the IDE Code::Blocks and compile using GCC in Windows.
My question is: How do you even implement that??!! How can you make your .exe receive those parameters in command line, and then execute them the way you want?
Also, anything special to have in mind if I want that implementation to compile in Linux?
Thanks for your help
You may want to look in your programming text for the signature of the main function, your program's entry point. That's where you'll be able to pull in those command line parameters.
I don't want to be more detailed than that because this is apparently a key point of the assignment, and if I ever find myself working with you, I'll expect you to be able to figure this sort of stuff out on your own once you've received an appropriate nudge. :)
Good luck!
As I recall, the Single UNIX Specification / POSIX defines getopt in unistd.h to handle the parsing of arguments for you. While this is a C function, it should also work in C++.
GNU GLIBC has this in addition to getopt_long (in getopt.h) to support GNU's extended --style .
Lo logré, I gotz it!!
I now have a basic understanding on how to use the argc and argv[ ] parameters on the main() function (I always wondered what they were good for...). For example, if I put in the command line:
compressor.exe -unzip file.zip
Then:
argc initializes in '3' (number of arguments in line)
argv[0] == "compressor.exe" (name of app.)
argv[1] == "-unzip"
argv[2] == "file.zip"
Greg (not 'Creg', sorry =P) and Bemrose, thank you guys for your help!! ^^