00001
00022
00023
#include"AVRGSM_zip.h"
00024
00026 const unsigned char __flash
hex_lookup[] =
hex_nmbr;
00027 const unsigned char __flash
mask[7] = {1,3,7,15,31,63,127};
00028 const unsigned char __flash
power[7] = {128,64,32,16,8,4,2};
00029
00030
00040 int ZIP_compress(
unsigned char *in,
unsigned char *discarded,
unsigned char ret[] )
00041 {
00042
00044
int i,ii,iii;
00045
unsigned char encode_c, now_c, next_c;
00046
00047
00048 *discarded = 0;
00049
00050
for( i = ii = iii = 0; ( in[i] !=
'\0' ) && ( i <
MESSAGE_LENGTH ); )
00051 {
00052 now_c = in[ i++ ];
00053 next_c = in[ i ];
00054
00055
00056
if( next_c ==
'\0' )
00057 {
00058 next_c = 0;
00059 }
00060
00061 encode_c = ( now_c >> ii )+ (
mask[ ii ] & next_c )*
power[ ii ];
00062 ret[ iii++ ] =
hex_lookup[ ( encode_c >> 4 ) & 0x0F ];
00063 ret[ iii++ ] =
hex_lookup[ ( encode_c & 0x0F ) ];
00064
00065
if( ii == 6 )
00066 {
00067 ii = 0;
00068 i++;
00069 (*discarded)++;
00070 }
00071
00072
else
00073 {
00074 ii++;
00075 }
00076 }
00077
00078 ret[iii] =
'\0';
00079
00080
return i;
00081 }
00082
00083
00091 int ZIP_decompress(
unsigned char *compressed,
unsigned char *decompressed )
00092 {
00093
00095
int i,ii,iii;
00096
unsigned char rest_c, ans_c ,dec_c , this_c, next_c;
00097
00098
for( i = ii = iii = rest_c = 0; (this_c = compressed[i++]) !=
'\0'; )
00099 {
00100
00101 next_c = compressed[i++];
00102
00103
00104 dec_c = 16 *
ZIP_htoi( this_c ) +
ZIP_htoi( next_c );
00105 ans_c = dec_c &
mask[6 - ii];
00106 ans_c <<= ii;
00107 decompressed[iii++] = ans_c + rest_c;
00108
00109 rest_c = (dec_c & ~
mask[6 - ii]) >> ( 7 - ii );
00110
00111
if( ii == 6)
00112 {
00113 ii = 0;
00114 decompressed[ iii++ ] = rest_c;
00115 rest_c = 0;
00116 }
00117
00118
else
00119 {
00120 ii++;
00121 }
00122
00123 }
00124
00125 decompressed[ iii ] =
'\0';
00126
return iii;
00127 }
00128
00129
00140 int ZIP_atoi(
unsigned char *a )
00141 {
00142
00143
int i, n;
00144
00145 n = 0;
00146
00147
for( i=0; ( a[ i ] >=
'0' ) && ( a[ i ] <=
'9' ); ++i )
00148 {
00149 n = 10*n + ( a[ i ] -
'0' );
00150 }
00151
00152
return n;
00153 }
00154
00155
00164 int ZIP_htoi(
unsigned char hex )
00165 {
00166
00167
if( ( hex >=
'A' ) && ( hex <=
'F' ) )
00168 {
00169
return hex -
'A' + 10;
00170 }
00171
else
00172 {
00173
return hex -
'0';
00174 }
00175 }
00176
00177
00187 void ZIP_itoh(
int n,
unsigned char *ret )
00188 {
00189
00190 ret[ 0 ] =
hex_lookup[ ( n >> 4 ) & 0x0F ];
00191 ret[ 1 ] =
hex_lookup[ ( n & 0x0F ) ];
00192 ret[ 2 ] =
'\0';
00193 }