If I read from a file a %0.2X number, and I bring in a large number of these, how would I examine each byte with if?
If I read from a file a %0.2X number, and I bring in a large number of these, how would I examine each byte with if?
That depends what kind of datatype you (need to) use. Do you want to break these up into ints or something? You could just put the whole file into one big char buffer and parse it with read(), which has no problem with zero bytes -- although you could do this when you read the file in:
just a sketchCode:int fd=open("file", O_RDONLY), i++; char byte, buffer[4096]; while ((read(fd,&byte,1)) { /* true until EOF */ [filter or look for bytes?] buffer[i]=byte; i++; }
Last edited by MK27; 02-28-2009 at 11:11 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
For example, my program reads F4 CD 21. I examine these
Also, how would I make it recognize that CD 21 goes together as INT 21?Code:switch(opcode) { case F4: puts("F4 HLT"); break; case CD: puts("CD INT"); break; default: printf("%0.2X ???"); }
And I would use stat() to get the number of bytes and then decrement it to make s0ure I got the whole file.
You can use that switch with (byte) in the above read() loop as part of a "parsing pipeline":
Code:int fd=open("file", O_RDONLY), i++; char byte, buffer[4096], tmp[256]; while ((read(fd,&byte,1)) { /* true until EOF */ switch (byte) { case (0xcd): read(fd,&byte,1); sprintf(tmp,"INT %d",(int)byte); strcat(buffer,tmp); break; case (0xf4) : etc.... }
Last edited by MK27; 02-28-2009 at 11:16 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
How would you do that using fread()
Yes, fread() is more portable than read, sorry.
You do it the same way but you use a FILE* stream -- fopen() -- instead of a file descriptor from open(). The fread() would look like this:
I just noticed I have some unclosed parantheses in my previous work here, but anyway...Code:while ((fread(&byte,1,1,fstr))) {
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
My only problem is, how would I get it to recognize certian bytes as prefixes rather than ???. And how would I grab only a byte at a time.
Let's look at this more closely!
I just noticed I get a gcc warning unless you use an unsigned char here, because 0xf4 would be an "illegal" value for a char.Code:#include <stdio.h> int main() { int halt=0; FILE *file=fopen("file", "r"); unsigned char byte; while ((fread(&byte,1,1,file))) { /* true until EOF */ switch (byte) { case (0xcd): /* notice: 1,1 means one byte at a time */ fread(&byte,1,1,file); printf("INT %d\n",(int)byte); break; case (0xf4): halt=1; break; default: break; /* could use this for something too */ } if (halt==1) break; /* still more opportunities to work with "byte" */ } return 0; }
Anyway, you get it to recognize a certain byte as a prefix using the switch statement -- but if you are worried that there will be a 0xcd that is not really a 0xcd, well...
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
But if the EOF comes up as an opcode...
Where would I put the x--? Also, I don't really understand how to do this with all possible hex opcodes.Code:#include <stdio.h> int main() { int x; struct stat fbuf; int halt=0; FILE *file=fopen("file", "r"); unsigned char byte; x = stat(argc[1],&fbuf); x = fbuf.st_size; while ((fread(&byte,1,1,file))) { /* true until EOF */ switch (byte) { case (0xcd): /* notice: 1,1 means one byte at a time */ fread(&byte,1,1,file); printf("INT %d\n",(int)byte); break; case (0xf4): halt=1; break; default: break; /* could use this for something too */ } if (halt==1) break; /* still more opportunities to work with "byte" */ } return 0; }
why to use fread for reading 1 byte?
what is the problem with fgetc?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I don't just want to read one byte, I want to grab all of them and examine them as one byte chunks. Also, I need sometimes to examine them as more than one byte.
I would say it doesn't matter much, but then fgetc returns an int.
Also, as the OP indicates, sh/e may in fact need to read more than one byte.
If you do that, you can use a small buffer and iterate thru each byte in a loop:
Just keep in mind that "buffer" will not be null terminated.Code:char buffer[256]; fread(buffer,1,256,file); for (i=0; i<256; i++) { switch (buffer[i]) { ...etc.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
OK. Thanks. But where would I put the x--?
Last edited by Sly; 02-28-2009 at 12:11 PM.
stat() is just as portable (or unportable) as open()/read()/etc. If you want to figure out the size of a file, open it, fseek() to the end, ftell() to get the position, and fseek()/rewind() back to the beginning of the file.Code:x = stat(argc[1],&fbuf); x = fbuf.st_size;
I don't know what you're asking with the "x--" question. If stat() returns the number of bytes in the file, loop in the range 0 to x-1.
But I don't see why you need to know the size of the file, anyway. MK27's code handles files of any size. Perhaps you need the count for something else, but you haven't said so. In fact, you haven't said what you're doing at all (though I'd guess some sort of disassembler). What are you doing?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I have a disassembler for x86, it's about 36KB in size.
It is table-driven, and although it say "disasm.cpp", it's not really C++ code.
It doesn't do the MMX/3DNow!/SSE instructions, or any other new instructions introduced in recent processors (branch prediction prefix for example).
But you are welcome to use it as a template, or simply play around with it.
Note that it is NOT an attempt on "most pretty code" - it does use a few different dirty tricks that may not be portable, it has at least one goto (that I spotted when doing a fast scan through the code), and I'm sure there are other things that aren't great either.
Due to the extension rules, the file is a renamed zip-file.
Edit: Perhaps I should have stated that I'm happy for anyone to use the source code, but you must mention that I wrote the original code - I think that's fair.
--
Mats
Last edited by matsp; 02-28-2009 at 07:44 PM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.