LZW15.C
上传用户:bjghjy
上传日期:2007-01-07
资源大小:379k
文件大小:7k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "lzw.h"
  6. #define         BITS            15
  7. #define         MAX_CODE        ((1L<<BITS) -1 )        //32767
  8. #define         TABLE_SIZE      35023                   //137*256=35072
  9. #define         TABLE_BANKS     ((TABLE_SIZE >> 8) +1)  //137
  10. #define         END_OF_STREAM   256
  11. #define         BUMP_CODE       257
  12. #define         FLUSH_CODE      258
  13. #define         FIRST_CODE      259
  14. #define         UNUSED          -1
  15. typedef struct tag_dictionary
  16. {
  17. short code_value;
  18. short parent_code;
  19. char character;
  20. }dictionary;
  21. int find_child_node(short parent_code,short child_character,dictionary *dict);
  22. //unsigned short decode_string(unsigned short offset,unsigned short code);
  23. void Writelog(char *msg);
  24. //char *CompressionName ="lzw 15 bit Encode";
  25. //char *Usage         ="in-file out-filenn";
  26. //this package inlude file:bitio.c/lzw15.c/lzw.h
  27. #define DICT(i) dict[i >> 8][i&0xff]
  28. //char *decode_stack=NULL;
  29. BOOL CompressFile(char * infile,char *outfile)
  30. {
  31. short character;
  32. short string_code;
  33. unsigned short index;
  34. HANDLE hd;
  35. BIT_FILE * output;
  36. FILE * input;
  37. int ret;
  38. unsigned short next_code;
  39. short current_code_bits;
  40. unsigned short next_bump_code;
  41. unsigned short i;
  42. dictionary *dict;
  43. input = fopen(infile,"rb");
  44. if(input==NULL)
  45. {
  46. fatal_error("Can not open input file");
  47. return FALSE;
  48. }
  49. output =OpenOutputBitFile(outfile);
  50. if(output ==NULL)
  51. {
  52. fatal_error("Can not open output file");
  53. fclose(input);
  54. return FALSE;
  55.  }
  56. hd=HeapCreate((DWORD)0,TABLE_BANKS*256*sizeof(dictionary),(DWORD)0);
  57. if(hd==NULL)
  58. {
  59. CloseOutputBitFile(output);
  60. fclose(input);
  61. return FALSE;
  62. }
  63. //if(!InitializeStorage(hd))
  64. //{
  65. // CloseOutputBitFile(output);
  66. // fclose(input);
  67. // HeapDestroy(hd);
  68. // return FALSE;
  69. //}
  70. dict=HeapAlloc(hd,HEAP_ZERO_MEMORY,TABLE_BANKS*256*sizeof(dictionary));
  71. if(dict== NULL)
  72. {
  73. CloseOutputBitFile(output);
  74. fclose(input);
  75. HeapDestroy(hd);
  76. return FALSE;
  77. }
  78. if(HeapSize(hd,0,dict)<TABLE_BANKS*256*sizeof(dictionary))
  79. {
  80. CloseOutputBitFile(output);
  81. fclose(input);
  82. HeapDestroy(hd);
  83. return FALSE;
  84. }
  85. //InitializeDictionary();
  86. for(i=0;i<TABLE_SIZE;i++)
  87. dict[i].code_value = UNUSED;
  88. next_code =FIRST_CODE;
  89. current_code_bits =9;
  90. next_bump_code =511;
  91. if((string_code = getc(input)) == EOF)
  92. string_code = END_OF_STREAM;
  93. while((character = getc(input))!=EOF)
  94. {
  95. ret =find_child_node(string_code,character,dict);
  96. if(ret<0)
  97. {
  98. CloseOutputBitFile(output);
  99. fclose(input);
  100. HeapDestroy(hd);
  101. return FALSE;
  102. }
  103. index =(unsigned short)ret;
  104. if(dict[index].code_value !=-1)
  105. string_code =dict[index].code_value;
  106. else
  107. {
  108. dict[index].code_value =next_code++;
  109. dict[index].parent_code=string_code;
  110. dict[index].character=(char)character;
  111. OutputBits(output,(unsigned int)string_code,current_code_bits);
  112. string_code =character;
  113. if(next_code>MAX_CODE)
  114. {
  115. OutputBits(output,(unsigned int)FLUSH_CODE,current_code_bits);
  116. //InitializeDictionary();
  117. for(i=0;i<TABLE_SIZE;i++)
  118. dict[i].code_value = UNUSED;
  119. next_code =FIRST_CODE;
  120. current_code_bits =9;
  121. next_bump_code =511;
  122. }
  123. else if(next_code>next_bump_code)
  124. {
  125. OutputBits(output,(unsigned int)BUMP_CODE,current_code_bits);
  126. current_code_bits++;
  127. next_bump_code<<=1;
  128. next_bump_code|=1;
  129. }
  130. }
  131. }
  132. OutputBits(output,(unsigned short)string_code,current_code_bits);
  133. OutputBits(output,(unsigned short)END_OF_STREAM,current_code_bits);
  134. CloseOutputBitFile(output);
  135. fclose(input);
  136. //ExitLzw(hd);
  137. HeapDestroy(hd);
  138. return TRUE;
  139. }
  140. int find_child_node(short parent_code ,short child_character,dictionary *dict)
  141. {
  142. unsigned short index;
  143. unsigned short offset;
  144. index =(child_character <<(BITS-8)) ^parent_code;
  145. if(index ==0)
  146. offset =1;
  147. else
  148. offset =TABLE_SIZE -index;
  149. for(;;)
  150. {
  151. if(IsBadStringPtr((char *)&dict[index],(UINT)sizeof(short)))
  152. return -1;
  153. if(dict[index].code_value ==UNUSED)
  154. return(int)index;
  155. if(dict[index].parent_code ==parent_code &&
  156. dict[index].character ==(char)child_character)
  157. return(int)index;
  158. if(index >=offset)
  159. index =index -offset;
  160. else
  161. index=index +TABLE_SIZE-offset;
  162. }
  163. }
  164. //unsigned short decode_string(unsigned short count,unsigned short code)
  165. //{
  166. // while(code>255)
  167. // {
  168. // decode_stack[count++] =dict[code].character;
  169. // code =dict[code].parent_code;
  170. // }
  171. // decode_stack[count++] =(char)code;
  172. // return(count);
  173. //}
  174. void Writelog(char *msg)
  175. {
  176. OFSTRUCT os;
  177. HFILE hf;
  178.     
  179.     hf=OpenFile("\sv_com32.log",&os,OF_WRITE);
  180. if(hf ==HFILE_ERROR)
  181. hf =OpenFile("\sv_com32.log",&os,OF_WRITE|OF_CREATE);
  182. else
  183. _llseek(hf,0L,SEEK_END);
  184. _lwrite(hf,msg,strlen(msg));
  185. _lclose(hf);
  186. return;
  187. }
  188. short  getmemc(char *buff,int *ylen,int len)
  189. {
  190. unsigned char ret;
  191. if(*ylen<len)
  192. {
  193. ret =buff[*ylen];
  194. *ylen =*ylen +1;
  195. }
  196. else
  197. return EOF;
  198. return ret;
  199. }
  200. BOOL CompressMemFile(char *buff,int len,char *outfile)
  201. {
  202. short character;
  203. short string_code;
  204. unsigned short index;
  205. HANDLE hd;
  206. BIT_FILE * output;
  207. int ylen =0;
  208. char * input;
  209. int ret;
  210. unsigned short next_code;
  211. short current_code_bits;
  212. unsigned short next_bump_code;
  213. unsigned short i;
  214. dictionary *dict;
  215. input = buff;
  216. if(input==NULL||len<0)
  217. {
  218. fatal_error("Can not open mem data");
  219. return FALSE;
  220. }
  221. output =OpenOutputBitFile(outfile);
  222. if(output ==NULL)
  223. {
  224. fatal_error("Can not open output file");
  225. return FALSE;
  226. }
  227. hd=HeapCreate((DWORD)0,TABLE_BANKS*256*sizeof(dictionary),(DWORD)0);
  228. if(hd==NULL)
  229. {
  230. CloseOutputBitFile(output);
  231. return FALSE;
  232. }
  233. //if(!InitializeStorage(hd))
  234. //{
  235. // CloseOutputBitFile(output);
  236. // HeapDestroy(hd);
  237. // return FALSE;
  238. //}
  239. dict=HeapAlloc(hd,HEAP_ZERO_MEMORY,TABLE_BANKS*256*sizeof(dictionary));
  240. if(dict== NULL)
  241. {
  242. CloseOutputBitFile(output);
  243. HeapDestroy(hd);
  244. return FALSE;
  245. }
  246. if(HeapSize(hd,0,dict)<TABLE_BANKS*256*sizeof(dictionary))
  247. {
  248. CloseOutputBitFile(output);
  249. HeapDestroy(hd);
  250. return FALSE;
  251. }
  252. //InitializeDictionary();
  253. for(i=0;i<TABLE_SIZE;i++)
  254. dict[i].code_value = UNUSED;
  255. next_code =FIRST_CODE;
  256. current_code_bits =9;
  257. next_bump_code =511;
  258. if((string_code = getmemc(input,&ylen,len)) == EOF)
  259. string_code = END_OF_STREAM;
  260. while((character = getmemc(input,&ylen,len))!=EOF)
  261. {
  262. ret =find_child_node(string_code,character,dict);
  263. if(ret<0)
  264. {
  265. CloseOutputBitFile(output);
  266. HeapDestroy(hd);
  267. return FALSE;
  268. }
  269. index =(unsigned short)ret;
  270. if(dict[index].code_value !=-1)
  271. string_code =dict[index].code_value;
  272. else
  273. {
  274. dict[index].code_value =next_code++;
  275. dict[index].parent_code=string_code;
  276. dict[index].character=(char)character;
  277. OutputBits(output,(unsigned int)string_code,current_code_bits);
  278. string_code =character;
  279. if(next_code>MAX_CODE)
  280. {
  281. OutputBits(output,(unsigned int)FLUSH_CODE,current_code_bits);
  282. //InitializeDictionary();
  283. for(i=0;i<TABLE_SIZE;i++)
  284. dict[i].code_value = UNUSED;
  285. next_code =FIRST_CODE;
  286. current_code_bits =9;
  287. next_bump_code =511;
  288. }
  289. else if(next_code>next_bump_code)
  290. {
  291. OutputBits(output,(unsigned int)BUMP_CODE,current_code_bits);
  292. current_code_bits++;
  293. next_bump_code<<=1;
  294. next_bump_code|=1;
  295. }
  296. }
  297. }
  298. OutputBits(output,(unsigned short)string_code,current_code_bits);
  299. OutputBits(output,(unsigned short)END_OF_STREAM,current_code_bits);
  300. CloseOutputBitFile(output);
  301. HeapDestroy(hd);
  302. return TRUE;
  303. }