1 /* 2 * Copyright (C) 2001, 2002, and 2003 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: dact@rkeene.org 19 */ 20 21 22 #include "dact.h" 23 #include "comp_delta.h" 24 #include "buffer.h" 25 #include <stdio.h> 26 #ifdef HAVE_UNISTD_H 27 #include <unistd.h> 28 #endif 29 #ifdef HAVE_STDLIB_H 30 #include <stdlib.h> 31 #endif 32 33 34 35 /* 36 mode - DACT_MODE_COMPR or DACT_MODE_DECMP 37 Determine whether to compress or decompress. 38 prev_block - Previous (uncompressed) block. 39 curr_block - The data to be compressed. 40 out_block - Where to put data after compression. 41 blk_size - Size of prev_block and curr_block. 42 */ 43 44 #if defined(USE_MODULES) && defined(AS_MODULE) 45 #include "module.h" 46 uint32_t DC_NUM=2; 47 uint32_t DC_TYPE=DACT_MOD_TYPE_COMP; 48 void *DC_ALGO=comp_delta_algo; 49 char *DC_NAME="Delta Compression (MOD)"; 50 #endif 51 52 53 int comp_delta_algo(int mode, unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) { 54 switch(mode) { 55 case DACT_MODE_COMPR: 56 return(comp_delta_compress(prev_block, curr_block, out_block, blk_size, bufsize)); 57 break; /* Heh */ 58 case DACT_MODE_DECMP: 59 return(comp_delta_decompress(prev_block, curr_block, out_block, blk_size, bufsize)); 60 break; 61 default: 62 printf("Unsupported mode: %i\n", mode); 63 return(-1); 64 } 65 } 66 67 int comp_delta_compress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) { 68 int i,x=0,y=0; 69 char Val; 70 unsigned char CurrByte, PrevByte; 71 signed char DeltaByte; 72 73 bit_buffer_purge(); 74 75 CurrByte=curr_block[0]; 76 out_block[x]=CurrByte; 77 for (i=1;i<blk_size;i++) { 78 PrevByte=CurrByte; 79 CurrByte=curr_block[i]; 80 DeltaByte=(CurrByte-PrevByte); 81 if (abs(DeltaByte)<32) { 82 Val=(((1<<6) | (( ( DeltaByte<0 ))<<5) | (abs(DeltaByte)&33))); 83 bit_buffer_write(Val,7); 84 } else { 85 bit_buffer_write(CurrByte,9); 86 } 87 y=bit_buffer_size(); 88 while (y>=8 && y!=16) { 89 out_block[++x]=bit_buffer_read(8); 90 if (x>=blk_size*2) return(-1); 91 y=bit_buffer_size(); 92 } 93 94 } 95 y=bit_buffer_size(); 96 if (y!=0) { 97 out_block[++x]=(bit_buffer_read(y)<<(8-y)); 98 } 99 return(x+1); 100 } 101 102 int comp_delta_decompress(unsigned char *prev_block, unsigned char *curr_block, char *out_block, int blk_size, int bufsize) { 103 int i=0,x=0; 104 unsigned char CurrByte; 105 unsigned char DeltaByte; 106 char CompBit; 107 char Neg; 108 char Val; 109 110 CurrByte=curr_block[0]; 111 bit_buffer_purge(); 112 while (1) { 113 if ((bit_buffer_size()<=8) && i==blk_size) break; 114 if ((bit_buffer_size()<=8) && i!=blk_size) 115 bit_buffer_write((unsigned char) curr_block[++i],8); 116 CompBit=bit_buffer_read(1); 117 if ((bit_buffer_size()<=8) && i!=blk_size) 118 bit_buffer_write((unsigned char) curr_block[++i],8); 119 120 if (CompBit==1) { 121 Neg=bit_buffer_read(1); 122 DeltaByte=bit_buffer_read(5); 123 Val=CurrByte+(((-2*Neg)+1)*DeltaByte); 124 } else { 125 Val=bit_buffer_read(8); 126 } 127 out_block[x++]=CurrByte; 128 CurrByte=Val; 129 } 130 return(x); 131 } |