| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: Big endian machines |
Hi Bo.
19-Jan-04 14:33:08, Bo Simonsen wrote to Pascal Schmidt
PS>> Hi Bo! :-)
BS> Hi Pascal,
BS>> I'm looking into this issue because it looks like
BS>> that SquishMail has
BS>> problems my reading a packed (__attribute__(packed)) structure from
BS>> diskfile written on a little endian machine.
PS>> No guesses please, show us the structure definition. :)
BS> It would be no-sence to show you the whole structure, so here's just a
BS> bit
BS> of it:
BS> struct _pkthdr
BS> {
BS> sword orig_node; /* originating node */
BS> sword dest_node; /* destination node */
BS> } __attribute__((packed, aligned(2)));
OK, a fidonet .PKT file, there are little endian (opposite of network byte
order)
the easiest fix it to use do byte-swap operation on the words after/before
reading/writing them.
&define fido_swab(x) (x=x>8 & 0xff )
BS> read(fp, &pkthdr, sizeof(struct _pkthdr));
fido_swab(pkthdr.orig_node)
fido_swab(pkthdr.dest_node)
and before you write them do fido_swab on them again....
The trick for portable code is to get fido_swab to do nothing when htons
does something and vice versa. on little-endian hardware fido_swab should
be defined blank.
#define fido_swab
probably it's a good idea to test it at runtime, or as part of the make
process.
{
short int x=0
((char*)x)[0]=1;
fido_swab[x]
if(x != 1) { fpriintf(stderr "fido_swab mis-defined in source";
exit(1);
}
}
This isn't a truly general solution as it won;'t work on macines that don't
use two's complement numbers but those are practiacally unheard of these
days,
the other fix is to re-write it to use all bytes.
BS> struct _pkthdr
BS> {
BS> uchar orig_node_minor,orig_node_major; /* originating node */
BS> uchar dest_node_minor,dest_node_major; /* destination node */
BS> } __attribute__((packed, aligned(1)));
byt then you may need to do more edits to your code so that it can get the
node number
eg:
unsigned short orig_node = (pkt.orig_node_major << 8 & 0xff00)
+(pkt.orig_node_minor & 0x00FF)
BS> Should I read every single variable in the structure to get it working?
no, you either need to address each byte separately or convert the
multi-byte values from file-format to host-format, and back to file format
before writing
-=> Bye <=-
---
* Origin: Entropy isn't what it used to be. (3:640/1042)SEEN-BY: 633/267 270 @PATH: 640/1042 531 954 774/605 123/500 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.