Subject: Re: Alignment issue in msdosfs - bpb.h
To: John Hayward <John.C.Hayward@wheaton.edu>
From: Todd Whitesel <toddpw@best.com>
List: current-users
Date: 03/21/2001 04:38:15
>    I am running on a NetBSD-1.5 i386 port - the question below may
> apply to other ports.

It is even more relevant for other ports; the i386 hardware accepts unaligned
memory operations, but most RISC boxes do not.

> Since this info is read from the disk I cannot change how things are
> actually laid out (why did MS mix single byte items with 2 byte items
> where the layout would require that the 2 byte item start at an odd
> location?)
> 
> Is there a compiler flag which will allow me to use the first structure
> and retrieve the values correctly?

There are compiler keywords and pragmas, not options. Generally this sort
of thing needs to be set on a single file or a few structs.

Just write your code to be portable and move on to better things:


/* Routine to read 8/16/32 bit little endian numbers from memory. */
/* 'lsb' is a pointer to the lowest address of the number. */
/* 'bytes' is the number of bytes, and must be 1, 2, or 4. */

u_int32_t ReadLittleEndianNumber (u_int8_t *lsb, int bytes)
{
	u_int32_t val = *lsb++;

	bytes -= 2;
	if (bytes >= 0) {
		val |= (*lsb++ << 8);
		if (bytes) {
			val |= (*lsb++ << 16);
			val |= (*lsb++ << 24);
		}
	}
	return val;
}

u_int16_t Fetch_bpbResSectors (struct byte_bpb710 *pbpb)
{
	return ReadLittleEndianNumber (&pbpb->bpbResSectors,
					sizeof(pbpb->bpbResSectors));
}

... oops, shouldn't this sort of thing be in a library somewhere?

Todd Whitesel
toddpw @ best.com