[3745] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * bitio_m_mem.h -- Macros for bitio to memory
|
---|
| 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 an array
|
---|
| 23 | * of chars. These routines are faster than the ones in "mems" files. But
|
---|
| 24 | * with these routines you cannot mix reads and writes on the array of chars,
|
---|
| 25 | * or multiple write, at the same time and guarantee them to work, also you
|
---|
| 26 | * cannot seek to a point and do a write. The decode routine can detect when
|
---|
| 27 | * you run off the end of the array and will produce an approate error
|
---|
| 28 | * message, and the encode routine will stop when it gets to the end of the
|
---|
| 29 | * character array.
|
---|
| 30 | *
|
---|
| 31 | **************************************************************************/
|
---|
| 32 |
|
---|
| 33 | #ifndef H_BITIO_M_MEM
|
---|
| 34 | #define H_BITIO_M_MEM
|
---|
| 35 |
|
---|
| 36 | typedef struct mem_bitio_state
|
---|
| 37 | {
|
---|
| 38 | unsigned char *Base;
|
---|
| 39 | unsigned char *Pos;
|
---|
| 40 | int Remaining;
|
---|
| 41 | unsigned char Buff;
|
---|
| 42 | unsigned char Btg;
|
---|
| 43 | }
|
---|
| 44 | mem_bitio_state;
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | #ifndef DECODE_ERROR
|
---|
| 50 | #define DECODE_ERROR (fprintf(stderr,"Unexpected EOF in \"%s\" on line %d\n",\
|
---|
| 51 | __FILE__, __LINE__), exit(1))
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | #define ENCODE_START(p,r) \
|
---|
| 57 | { \
|
---|
| 58 | register unsigned char *__pos = p; \
|
---|
| 59 | register unsigned char *__base = p; \
|
---|
| 60 | register int __remaining = r; \
|
---|
| 61 | register unsigned char __buff = 0; \
|
---|
| 62 | register unsigned char __btg = sizeof(__buff)*8;
|
---|
| 63 |
|
---|
| 64 | #define ENCODE_CONTINUE(b) \
|
---|
| 65 | { \
|
---|
| 66 | register unsigned char *__pos = (b).Pos; \
|
---|
| 67 | register unsigned char *__base = (b).Base; \
|
---|
| 68 | register int __remaining = (b).Remaining; \
|
---|
| 69 | register unsigned char __buff = (b).Buff; \
|
---|
| 70 | register unsigned char __btg = (b).Btg;
|
---|
| 71 |
|
---|
| 72 | #define ENCODE_BIT(b) \
|
---|
| 73 | do { \
|
---|
| 74 | __btg--; \
|
---|
| 75 | if (b) __buff |= (1 << __btg); \
|
---|
| 76 | if (!__btg) \
|
---|
| 77 | { \
|
---|
| 78 | if (__remaining) \
|
---|
| 79 | { \
|
---|
| 80 | *__pos++ = __buff; \
|
---|
| 81 | __remaining--; \
|
---|
| 82 | } \
|
---|
| 83 | __buff = 0; \
|
---|
| 84 | __btg = sizeof(__buff)*8; \
|
---|
| 85 | } \
|
---|
| 86 | } while(0)
|
---|
| 87 |
|
---|
| 88 | #define ENCODE_PAUSE(b) \
|
---|
| 89 | (b).Pos = __pos; \
|
---|
| 90 | (b).Base = __base; \
|
---|
| 91 | (b).Remaining = __remaining; \
|
---|
| 92 | (b).Buff = __buff; \
|
---|
| 93 | (b).Btg = __btg; \
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | #define ENCODE_FLUSH \
|
---|
| 97 | do { \
|
---|
| 98 | if (__btg != sizeof(__buff)*8) \
|
---|
| 99 | if (__remaining) \
|
---|
| 100 | { \
|
---|
| 101 | *__pos++ = __buff; \
|
---|
| 102 | __remaining--; \
|
---|
| 103 | } \
|
---|
| 104 | __btg = sizeof(__buff)*8; \
|
---|
| 105 | } while (0)
|
---|
| 106 |
|
---|
| 107 | #define ENCODE_DONE \
|
---|
| 108 | ENCODE_FLUSH; \
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | #define DECODE_START(p,r) \
|
---|
| 113 | { \
|
---|
| 114 | register unsigned char *__pos = p; \
|
---|
| 115 | register unsigned char *__base = p; \
|
---|
| 116 | register int __remaining = r; \
|
---|
| 117 | register unsigned char __buff = 0; \
|
---|
| 118 | register unsigned char __btg = 0;
|
---|
| 119 |
|
---|
| 120 | #define DECODE_CONTINUE(b) \
|
---|
| 121 | { \
|
---|
| 122 | register unsigned char *__pos = (b).Pos; \
|
---|
| 123 | register unsigned char *__base = (b).Base; \
|
---|
| 124 | register int __remaining = (b).Remaining; \
|
---|
| 125 | register unsigned char __buff = (b).Buff; \
|
---|
| 126 | register unsigned char __btg = (b).Btg;
|
---|
| 127 |
|
---|
| 128 | #define DECODE_ADD_FF(b) \
|
---|
| 129 | do { \
|
---|
| 130 | if (!__btg) \
|
---|
| 131 | { \
|
---|
| 132 | if (__remaining) \
|
---|
| 133 | { \
|
---|
| 134 | __buff = *__pos++; \
|
---|
| 135 | __remaining--; \
|
---|
| 136 | } \
|
---|
| 137 | else \
|
---|
| 138 | __buff = 0xff; \
|
---|
| 139 | __btg = sizeof(__buff)*8; \
|
---|
| 140 | } \
|
---|
| 141 | (b) += (b) + ((__buff >> --__btg) & 1); \
|
---|
| 142 | } while(0)
|
---|
| 143 |
|
---|
| 144 | #define DECODE_ADD_00(b) \
|
---|
| 145 | do { \
|
---|
| 146 | if (!__btg) \
|
---|
| 147 | { \
|
---|
| 148 | if (__remaining) \
|
---|
| 149 | { \
|
---|
| 150 | __buff = *__pos++; \
|
---|
| 151 | __remaining--; \
|
---|
| 152 | } \
|
---|
| 153 | else \
|
---|
| 154 | __buff = 0x00; \
|
---|
| 155 | __btg = sizeof(__buff)*8; \
|
---|
| 156 | } \
|
---|
| 157 | (b) += (b) + ((__buff >> --__btg) & 1); \
|
---|
| 158 | } while(0)
|
---|
| 159 |
|
---|
| 160 | #define DECODE_BIT (__btg ? (__buff >> --__btg) & 1 : \
|
---|
| 161 | (!__remaining ? \
|
---|
| 162 | (DECODE_ERROR, 0) : \
|
---|
| 163 | (__buff = *__pos++, __remaining--, __btg = sizeof(__buff)*8, \
|
---|
| 164 | (__buff >> --__btg) & 1)))
|
---|
| 165 |
|
---|
| 166 | #define DECODE_DONE ; \
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | #define DECODE_PAUSE(b) \
|
---|
| 170 | (b).Pos = __pos; \
|
---|
| 171 | (b).Base = __base; \
|
---|
| 172 | (b).Remaining = __remaining; \
|
---|
| 173 | (b).Buff = __buff; \
|
---|
| 174 | (b).Btg = __btg; \
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | #define DECODE_SEEK(pos) \
|
---|
| 178 | do { \
|
---|
| 179 | register long __new_pos = (pos); \
|
---|
| 180 | __pos = __base + (__new_pos >> 3); \
|
---|
| 181 | __buff = *__pos++; \
|
---|
| 182 | __btg = 8 - (__new_pos&7); \
|
---|
| 183 | } \
|
---|
| 184 | while(0)
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | #endif
|
---|