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