Mail Archives: djgpp/2004/12/25/02:15:51
Here's a little something I hope triggers some discussion...
DJGPP has *MANY* features simply not found in most compilers,
Borland, visualc, etc...
but even DJGPP still requires a bit of thoughtful ingenuity to
get access to some values in a file....
Case in point is reading of a "Raw Binary Value" directly out of
a file.
By "Raw Binary Value" we mean *NOT* being switched around
by Intel's storage format (big vs little endian).
1. Lets create a binary file, containing an unsigned long:
outfile = fopen ("bf.bin", "wb");
fprintf(outfile,"\x12\x34\x56\x78");
fclose(outfile);
//4-bytes expected and NOTHING else.
2. Object here is to now obtain in an unsigned long,
with as little effort as possible the ORIGINAL
ALIGNMNET of BYTES (defeating intel format);
what I am calling "raw binary value"
3. Attemp #1:
struct lbf {
unsigned long bf:32;
} abf;
void main(void) {
FILE *infile;
FILE *outfile;
outfile = fopen ("bf.bin", "wb");
fprintf(outfile,"\x12\x34\x56\x78");
fclose(outfile);
infile = fopen ("bf.bin", "rb");
fread(&abf, sizeof(abf), 1, infile);
fclose(infile);
printf("bf = %lu = %X\n", abf.bf, abf.bf );
/* Guess what! Its *NOT* a raw bitfield!!
It's actually in Intels format! */
};
4. Attempt #2:
unsigned long abf;
unsigned char Buf[4];
void main(void) {
FILE *infile;
FILE *outfile;
outfile = fopen ("bf.bin", "wb");
fprintf(outfile,"\x12\x34\x56\x78");
fclose(outfile);
infile = fopen ("bf.bin", "rb");
fread(Buf, sizeof(Buf), 1, infile);
fclose(infile);
abf = (unsigned long)((Buf[0] << 24) | (Buf[1] << 16) | (Buf[2] <<
8) | Buf[3]);
printf("bf = %lu = %X\n", abf.bf, abf.bf );
/* Works at the expense of Shifts & Ors, Casting required. */
} //main
WHICH ENDS up being resolved into:
5. Attempt #3:
unsigned long abf;
unsigned char Buf[4];
void main(void) {
FILE *infile;
FILE *outfile;
outfile = fopen ("bf.bin", "wb");
fprintf(outfile,"\x12\x34\x56\x78");
fclose(outfile);
infile = fopen ("bf.bin", "rb");
fread(Buf[3], sizeof(char), 1, infile);
fread(Buf[2], sizeof(char), 1, infile);
fread(Buf[1], sizeof(char), 1, infile);
fread(Buf[0], sizeof(char), 1, infile);
fclose(infile);
abf = cvul( Buf ); //Expect Unsigned Char Address Space to be UInt
printf("bf = %lu = %X\n", abf.bf, abf.bf );
/* Eureka! Success !!
At the expense of using a String-Array,
... *4* separate fread calls!
We still have to resolve the value as an unsigned long, which
is accomplished thru trivial pointer/casting of the contents of
the unsigned char buffer space...
*/
} //main
6. Attempt 6,7,8,9,etc.
Are just too freaky to bother posting!
7. Conclusion:
Except for being "clever" at using *4* fread's
or by re-arranging a unsigned char, byte-by-byte
using shifts and ors,
I just see NO WAY to directly read in Integer
Values without being "corrupted" by Intel's expectations!
what other method(s) are available to achieves this that
I may of missed?
ANY COMMENTS WELCOMED. Thanks.
- Raw text -