[3745] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * bitio_m_stdio.h -- Macros for bitio to a file
|
---|
| 4 | * Copyright (C) 1994 Neil Sharman
|
---|
| 5 | *
|
---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
---|
| 7 | * it under the terms of the GNU General Public License as published by
|
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | * (at your option) any later version.
|
---|
| 10 | *
|
---|
| 11 | * This program is distributed in the hope that it will be useful,
|
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | * GNU General Public License for more details.
|
---|
| 15 | *
|
---|
| 16 | * You should have received a copy of the GNU General Public License
|
---|
| 17 | * along with this program; if not, write to the Free Software
|
---|
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 19 | *
|
---|
| 20 | **************************************************************************
|
---|
| 21 | *
|
---|
| 22 | * This file contains macros for doing bitwise input and output on a FILE*.
|
---|
| 23 | * With these routines you cannot mix reads and writes on the FILE,
|
---|
| 24 | * or multiple writes, at the same time and guarantee them to work, also you
|
---|
| 25 | * cannot seek to a point and do a write. The decode routine can detect when
|
---|
| 26 | * you run off the end of the file and will produce an approate error message.
|
---|
| 27 | *
|
---|
| 28 | *
|
---|
| 29 | **************************************************************************/
|
---|
| 30 |
|
---|
| 31 | #ifndef H_BITIO_M_STDIO
|
---|
| 32 | #define H_BITIO_M_STDIO
|
---|
| 33 |
|
---|
| 34 | typedef struct stdio_bitio_state
|
---|
| 35 | {
|
---|
| 36 | FILE *File;
|
---|
| 37 | unsigned char Buff;
|
---|
| 38 | unsigned char Btg;
|
---|
| 39 | }
|
---|
| 40 | stdio_bitio_state;
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | #ifndef DECODE_ERROR
|
---|
| 45 | #define DECODE_ERROR (fprintf(stderr,"Unexpected EOF in \"%s\" on line %d\n",\
|
---|
| 46 | __FILE__, __LINE__), exit(1))
|
---|
| 47 | #endif
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | #define ENCODE_START(f) \
|
---|
| 52 | { \
|
---|
| 53 | FILE *__outfile = f; \
|
---|
| 54 | register unsigned char __buff = 0; \
|
---|
| 55 | register unsigned char __btg = sizeof(__buff)*8;
|
---|
| 56 |
|
---|
| 57 | #define ENCODE_CONTINUE(b) \
|
---|
| 58 | { \
|
---|
| 59 | FILE *__outfile = (b).File; \
|
---|
| 60 | register unsigned char __buff = (b).Buff; \
|
---|
| 61 | register unsigned char __btg = (b).Btg;
|
---|
| 62 |
|
---|
| 63 | #define ENCODE_BIT(b) \
|
---|
| 64 | do { \
|
---|
[13658] | 65 | --__btg; \
|
---|
[3745] | 66 | if (b) __buff |= (1 << __btg); \
|
---|
| 67 | if (!__btg) \
|
---|
| 68 | { \
|
---|
| 69 | putc(__buff, __outfile); \
|
---|
| 70 | __buff = 0; \
|
---|
| 71 | __btg = sizeof(__buff)*8; \
|
---|
| 72 | } \
|
---|
| 73 | } while(0)
|
---|
| 74 |
|
---|
| 75 | #define ENCODE_PAUSE(b) \
|
---|
| 76 | (b).File = __outfile; \
|
---|
| 77 | (b).Buff = __buff; \
|
---|
| 78 | (b).Btg = __btg; \
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | #define ENCODE_DONE \
|
---|
| 82 | if (__btg != sizeof(__buff)*8) \
|
---|
| 83 | putc(__buff,__outfile); \
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | #define DECODE_START(f) \
|
---|
| 88 | { \
|
---|
| 89 | FILE *__infile = f; \
|
---|
| 90 | register unsigned char __buff = 0; \
|
---|
| 91 | register unsigned char __btg = 0;
|
---|
| 92 |
|
---|
| 93 | #define DECODE_CONTINUE(b) \
|
---|
| 94 | { \
|
---|
| 95 | FILE *__infile = (b).File; \
|
---|
| 96 | register unsigned char __buff = (b).Buff; \
|
---|
| 97 | register unsigned char __btg = (b).Btg;
|
---|
| 98 |
|
---|
| 99 | #define DECODE_ADD_FF(b) \
|
---|
| 100 | do { \
|
---|
| 101 | if (!__btg) \
|
---|
| 102 | { \
|
---|
| 103 | register int val = getc(__infile); \
|
---|
| 104 | __buff = val == EOF ? 0xff : val; \
|
---|
| 105 | __btg = sizeof(__buff)*8; \
|
---|
| 106 | } \
|
---|
| 107 | (b) += (b) + ((__buff >> --__btg) & 1); \
|
---|
| 108 | } while(0)
|
---|
| 109 |
|
---|
| 110 | #define DECODE_ADD_00(b) \
|
---|
| 111 | do { \
|
---|
| 112 | if (!__btg) \
|
---|
| 113 | { \
|
---|
| 114 | register int val = getc(__infile); \
|
---|
| 115 | __buff = val == EOF ? 0xff : val; \
|
---|
| 116 | __btg = sizeof(__buff)*8; \
|
---|
| 117 | } \
|
---|
| 118 | (b) += (b) + ((__buff >> --__btg) & 1); \
|
---|
| 119 | } while(0)
|
---|
| 120 |
|
---|
| 121 | #define DECODE_BIT (__btg ? (__buff >> --__btg) & 1 : \
|
---|
| 122 | (__buff = getc(__infile), (feof(__infile) ? \
|
---|
| 123 | (DECODE_ERROR, 0) : \
|
---|
| 124 | (__btg = sizeof(__buff)*8, (__buff >> --__btg) & 1))))
|
---|
| 125 |
|
---|
| 126 | #define DECODE_DONE ; \
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | #define DECODE_PAUSE(b) \
|
---|
| 130 | (b).File = __infile; \
|
---|
| 131 | (b).Buff = __buff; \
|
---|
| 132 | (b).Btg = __btg; \
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | #define DECODE_SEEK(pos) \
|
---|
| 137 | do { \
|
---|
| 138 | register long __new_pos = (pos); \
|
---|
| 139 | fseek(__infile, __new_pos >> 3, 0); \
|
---|
| 140 | __buff = getc(__infile); \
|
---|
| 141 | __btg = 8 - (__new_pos&7); \
|
---|
| 142 | } \
|
---|
| 143 | while(0)
|
---|
| 144 |
|
---|
| 145 | #endif
|
---|