home *** CD-ROM | disk | FTP | other *** search
- /*
- * July 5, 1991
- * Copyright 1991 Lance Norskog And Sundry Contributors
- * This source code is freely redistributable and may be used for
- * any purpose. This copyright notice must be maintained.
- * Lance Norskog And Sundry Contributors are not responsible for
- * the consequences of using this software.
- */
-
- /*
- * AUX miscellaneous stuff.
- */
-
- char *sizes[] = {
- "NONSENSE!",
- "bytes",
- "shorts",
- "longs",
- "32-bit floats",
- "64-bit floats",
- "IEEE floats"
- };
-
- char *styles[] = {
- "NONSENSE!",
- "unsigned",
- "signed (2's complement)",
- "u-law",
- "a-law"
- };
-
- #include "aux.h"
-
- /* Utilities */
-
- /* Read short, little-endian: little end first. VAX/386 style. */
- unsigned short
- rlshort(ft)
- ft_t ft;
- {
- unsigned char uc, uc2;
- uc = getc(ft->fp);
- uc2 = getc(ft->fp);
- return (uc2 << 8) | uc;
- }
-
- /* Read short, bigendian: big first. 68000/SPARC style. */
- unsigned short
- rbshort(ft)
- ft_t ft;
- {
- unsigned char uc, uc2;
- uc2 = getc(ft->fp);
- uc = getc(ft->fp);
- return (uc2 << 8) | uc;
- }
-
- /* Write short, little-endian: little end first. VAX/386 style. */
- unsigned short
- wlshort(ft, us)
- ft_t ft;
- unsigned short us;
- {
- putc(us, ft->fp);
- putc(us >> 8, ft->fp);
- }
-
- /* Write short, big-endian: big end first. 68000/SPARC style. */
- unsigned short
- wbshort(ft, us)
- ft_t ft;
- unsigned short us;
- {
- putc(us >> 8, ft->fp);
- putc(us, ft->fp);
- }
-
- /* Read long, little-endian: little end first. VAX/386 style. */
- unsigned long
- rllong(ft)
- ft_t ft;
- {
- unsigned char uc, uc2, uc3, uc4;
- uc = getc(ft->fp);
- uc2 = getc(ft->fp);
- uc3 = getc(ft->fp);
- uc4 = getc(ft->fp);
- return (uc4 << 24) | (uc3 << 16) | (uc2 << 8) | uc;
- }
-
- /* Read long, bigendian: big first. 68000/SPARC style. */
- unsigned long
- rblong(ft)
- ft_t ft;
- {
- unsigned char uc, uc2, uc3, uc4;
- uc4 = getc(ft->fp);
- uc3 = getc(ft->fp);
- uc2 = getc(ft->fp);
- uc = getc(ft->fp);
- return (uc << 24) | (uc2 << 16) | (uc3 << 8) | uc4;
- }
-
- /* Write long, little-endian: little end first. VAX/386 style. */
- unsigned long
- wllong(ft, ul)
- ft_t ft;
- unsigned long ul;
- {
- putc(ul, ft->fp);
- putc(ul >> 8, ft->fp);
- putc(ul >> 16, ft->fp);
- putc(ul >> 24, ft->fp);
- }
-
- /* Write long, big-endian: big end first. 68000/SPARC style. */
- unsigned long
- wblong(ft, ul)
- ft_t ft;
- unsigned long ul;
- {
- putc(ul >> 24, ft->fp);
- putc(ul >> 16, ft->fp);
- putc(ul >> 8, ft->fp);
- putc(ul, ft->fp);
- }
-
- /* Read and write words and longs in "machine format". Swap if indicated. */
-
- /* Read short. */
- unsigned short
- rshort(ft)
- ft_t ft;
- {
- unsigned short us;
-
- fread(&us, 2, 1, ft->fp);
- if (ft->swap)
- us = swapw(us);
- return us;
- }
-
- /* Write short. */
- unsigned short
- wshort(ft, us)
- ft_t ft;
- unsigned short us;
- {
- if (ft->swap)
- us = swapw(us);
- fwrite(&us, 2, 1, ft->fp);
- }
-
- /* Read long. */
- unsigned long
- rlong(ft)
- ft_t ft;
- {
- unsigned long ul;
-
- fread(&ul, 4, 1, ft->fp);
- if (ft->swap)
- ul = swapl(ul);
- return ul;
- }
-
- /* Write long. */
- unsigned long
- wlong(ft, ul)
- ft_t ft;
- unsigned long ul;
- {
- if (ft->swap)
- ul = swapl(ul);
- fwrite(&ul, 4, 1, ft->fp);
- }
-
- /* Byte swappers */
-
- unsigned short
- swapw(us)
- unsigned short us;
- {
- return ((us >> 8) | (us << 8)) & 0xffff;
- }
-
- unsigned long
- swapl(ul)
- unsigned long ul;
- {
- return (ul >> 24) | ((ul >> 16) & 0xff00) | ((ul << 16) & 0xff0000) | (ul << 24);
- }
-