Mercurial > hg > th-libs
diff th_endian.c @ 0:bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
here, because it's cluttered and commit messages are mostly crap.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 26 Mar 2008 04:41:58 +0200 |
parents | |
children | 8552edc844a7 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/th_endian.c Wed Mar 26 04:41:58 2008 +0200 @@ -0,0 +1,96 @@ +/* + * Endianess handling + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2002-2007 Tecnic Software productions (TNSP) + * + * Please read file 'COPYING' for information on license and distribution. + */ +#include "th_endian.h" +#include <stdio.h> + + +/* + * Endianess conversion routines + */ +#ifdef TH_BIGENDIAN +/* Convert to from Small Endian + */ +void TH_NE16(uint16_t * iVal) +{ + uint16_t r = *iVal; + *iVal = ((r & 0xff) << 8) | ((r >> 8) & 0xff); +} + + +void TH_NE32(uint32_t * iVal) +{ + uint32_t r = *iVal; + *iVal = ((r & 0xff) << 8) | ((r >> 8) & 0xff) | + ((r & 0x00ff0000L) << 8) | ((r >> 8) & 0x00ff0000L); +} + + +/* Convert to Small/Little Endian + */ +uint16_t TH_LE16(uint16_t iVal) +{ + return ((iVal & 0xff) << 8) | ((iVal >> 8) & 0xff); +} + + +uint32_t TH_LE32(uint32_t iVal) +{ + return ((iVal & 0xff) << 8) | ((iVal >> 8) & 0xff) | + ((iVal & 0x00ff0000L) << 8) | ((iVal >> 8) & 0x00ff0000L); +} +#endif /* TH_BIGENDIAN */ + + +/* + * File routines for endian-dependant data + */ +#define TH_FILE FILE +#define TH_FREAD fread +#define TH_FGETC fgetc +#define TH_RNAME(X) TH_READ_##X +#define TH_CODE_ENDIAN2 +#include "th_endian2.h" +#undef TH_CODE_ENDIAN2 + + +BOOL TH_WRITE_STR(FILE * f, uint8_t * s, size_t l) +{ + return (fwrite(s, sizeof(uint8_t), l, f) == l); +} + + +void TH_WRITE_BE16(FILE * f, uint16_t v) +{ + fputc((v >> 8) & 0xFF, f); + fputc(v & 0xFF, f); +} + + +void TH_WRITE_BE32(FILE * f, uint32_t v) +{ + fputc((v >> 24) & 0xFF, f); + fputc((v >> 16) & 0xFF, f); + fputc((v >> 8) & 0xFF, f); + fputc(v & 0xFF, f); +} + + +void TH_WRITE_LE16(FILE * f, uint16_t v) +{ + fputc(v & 0xFF, f); + fputc((v >> 8) & 0xFF, f); +} + + +void TH_WRITE_LE32(FILE * f, uint32_t v) +{ + fputc(v & 0xFF, f); + fputc((v >> 8) & 0xFF, f); + fputc((v >> 16) & 0xFF, f); + fputc((v >> 24) & 0xFF, f); +}