1 /* 2 * Copyright (C) 2000 Roy Keene 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * email: rkeene@rkeene.org 19 */ 20 21 22 #include <stdio.h> 23 #include <string.h> 24 #include <stdlib.h> 25 #include "conf.h" 26 #include "buffer.h" 27 28 uint32_t bit_buffer_data=0; 29 int bit_buffer_location=0; 30 31 char *byte_buffer_data=NULL; 32 int byte_buffer_location=-1; 33 34 35 /* 36 Func: bit_buffer_read 37 Args: 38 bits - (int) Number of bits to read from the buffer. 39 Rets: Int, value from buffer. 40 Meth: Bread high BITS bits from BUFFER_DATA and shift to the left. 41 Stat: Complete 42 43 */ 44 int bit_buffer_read(int bits) { 45 int retval; 46 if (bits>bit_buffer_location) { 47 fprintf(stderr, __FILE__ ": bit_buffer_read: Request for %i bits, buffer contains %i bits\n",bits,bit_buffer_location); 48 bits=bit_buffer_location; 49 } 50 51 retval=(bit_buffer_data>>((sizeof(bit_buffer_data)*8)-bits)); 52 bit_buffer_data=(bit_buffer_data<<bits); 53 bit_buffer_location-=bits; 54 #ifdef BUFFER_VERBOSE 55 fprintf(stderr, __FILE__ ": bit_buffer_read(%i)=%i\n",bits,retval); 56 #endif 57 return(retval); 58 } 59 60 /* 61 Func: bit_buffer_write 62 Args: 63 val - (uint) Value to write 64 bits - (uint) Number of bits to allocate 65 Rets: Nothing 66 Meth: Add to right of current BUFFER_DATA[BUFFER_LOCATION], incremint 67 BUFFER_LOCATION 68 Stat: Complete 69 */ 70 void bit_buffer_write(unsigned int val, unsigned int bits) { 71 while ((val>>bits)!=0) { 72 fprintf(stderr, __FILE__ ": bit_buffer_write: Value (%i) is bigger than %i bits.\n",val,bits); 73 bits++; 74 } 75 76 if ((bits+bit_buffer_location)>(sizeof(bit_buffer_data)*8)) { 77 fprintf(stderr, __FILE__ ": bit_buffer_write: Buffer overflow\n"); 78 return; 79 } 80 bit_buffer_location+=bits; 81 bit_buffer_data+=(val<<((sizeof(bit_buffer_data)*8)-bit_buffer_location)); 82 #ifdef BUFFER_VERBOSE 83 fprintf(stderr, __FILE__ ": bit_buffer_write(%i, %i)\n",val,bits); 84 #endif 85 return; 86 } 87 88 89 /* 90 Func: bit_buffer_size 91 Args: None 92 Rets: Int, number of bits in buffer 93 Meth: buffer_location 94 Stat: Complete 95 */ 96 int bit_buffer_size(void) { 97 #ifdef BUFFER_VERBOSE 98 fprintf(stderr, __FILE__ ": bit_buffer_size()=%i\n",bit_buffer_location); 99 #endif 100 return(bit_buffer_location); 101 } 102 103 104 /* 105 Func: bit_buffer_purge 106 Args: None 107 Rets: Nothing 108 Meth: Clear BUFFER_DATA and set BUFFER_LOCATION to 0 109 */ 110 void bit_buffer_purge(void) { 111 bit_buffer_location=0; 112 bit_buffer_data=0; 113 #ifdef BUFFER_VERBOSE 114 fprintf(stderr, __FILE__ ": bit_buffer_purge()\n"); 115 #endif 116 return; 117 } 118 119 120 char *byte_buffer_read(int bytes) { 121 char *retval; 122 if (byte_buffer_location==-1) return(NULL); 123 if (bytes>byte_buffer_location) { 124 fprintf(stderr, __FILE__ ": byte_buffer_read: Request for %i bytes, buffer contains %i bytes.\n",bytes,byte_buffer_location); 125 bytes=byte_buffer_location; 126 } 127 retval=malloc(bytes); 128 memcpy(retval,byte_buffer_data,bytes); 129 memmove(byte_buffer_data,byte_buffer_data+bytes,(byte_buffer_location-=bytes)); 130 return(retval); 131 } 132 133 void byte_buffer_write(char *val, unsigned int size) { 134 if (byte_buffer_location==-1) { 135 byte_buffer_purge(); 136 } 137 if ((byte_buffer_location+size)>(BYTE_BUFF_SIZE)) { 138 fprintf(stderr, __FILE__ ": byte_buffer_write: Buffer overflow.\n"); 139 return; 140 } 141 memcpy(byte_buffer_data+byte_buffer_location,val,size); 142 byte_buffer_location+=size; 143 } 144 145 char byte_buffer_read_1(void) { 146 char *buffread; 147 char retval; 148 buffread=byte_buffer_read(1); 149 retval=buffread[0]; 150 free(buffread); 151 return(retval); 152 } 153 154 int byte_buffer_size(void) { 155 return(byte_buffer_location); 156 } 157 158 void byte_buffer_purge(void) { 159 int i; 160 161 #if 0 162 if (byte_buffer_data!=NULL) free(byte_buffer_data); 163 byte_buffer_data=calloc(BYTE_BUFF_SIZE, 1); 164 #endif 165 if (byte_buffer_data==NULL) byte_buffer_data=malloc(BYTE_BUFF_SIZE); 166 if (byte_buffer_location==-1) { 167 for (i=0;i<(BYTE_BUFF_SIZE);i++) byte_buffer_data[i]=0; 168 } else { 169 for (i=0;i<(byte_buffer_location+1);i++) byte_buffer_data[i]=0; 170 } 171 byte_buffer_location=0; 172 } |