home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 January / VPR0101A.BIN / OLS / BZ2L003 / bz2l003.lzh / BZ2LIB / BZ2LIB.C < prev    next >
C/C++ Source or Header  |  1998-06-02  |  4KB  |  198 lines

  1. #include "bz2lib.h"
  2. #include "stringq.h"
  3. #include "bzip2.h"
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #ifdef _WIN32
  7. #include <io.h>
  8. #endif
  9.  
  10. static volatile int flag_running=0;
  11.  
  12. /*    Get Version Number (x 100) of bz2lib */
  13. int EXPORTAPI BZ2GetVersion(void)
  14. {
  15.     return BZ2LIB_VERSION;
  16. }
  17. /*    return 1 if bz2lib is already running (check for multithreading) */
  18. int EXPORTAPI BZ2GetRunning(void)
  19. {
  20.     return flag_running;
  21. }
  22. /*  get bz2 file's level. if not bzip2 file,return -1 */
  23. int EXPORTAPI BZ2GetLevelStream(FILE *zStream)
  24. {
  25.     char magic[16];
  26.     int level;
  27.     int len;
  28.  
  29.     len=fread(magic,1,16,zStream);
  30.     fseek(zStream,-len,SEEK_CUR);
  31.  
  32.     if(strncmp("BZh",magic,3)!=0){return -1;}
  33.     /* magic is 3.14159265359 (= Pi) */
  34.     if(strncmp(magic+4,"\x31\x41\x59\x26\x53\x59",6)!=0){return -1;}
  35.     if('0'<=magic[3] && magic[3]<='9'){level = magic[3];}else{return -1;}
  36.     return level;
  37. }
  38. int EXPORTAPI BZ2GetLevel(char *file)
  39. {
  40.     FILE *fp;
  41.     int level;
  42.  
  43.     fp=fopen(file,"rb");
  44.     if(fp==NULL){return -1;}
  45.     level=BZ2GetLevelStream(fp);
  46.     fclose(fp);
  47.     return level;
  48. }
  49.  
  50. BZ2FILE * EXPORTAPI BZ2OpenStream(FILE *zStream,char *mode)
  51. {
  52.     BZ2FILE *BZ2fp;
  53.  
  54.     flag_running++;
  55.  
  56.     BZ2fp = malloc(sizeof(BZ2FILE));
  57.     if(BZ2fp==NULL){goto endlabel;}
  58.     memset(BZ2fp,0,sizeof(BZ2FILE));
  59.     BZ2fp->sq = STRINGQ_open(STRING_QUEUE_BUFFER_SIZE);
  60.     if(BZ2fp->sq==NULL){
  61.         free(BZ2fp);BZ2fp=NULL;
  62.         goto endlabel;
  63.     }
  64.     BZ2fp->fp = zStream;
  65.     BZ2fp->mode ='r';
  66.     BZ2fp->level = 9;
  67.     while(*mode){
  68.         if(*mode == 'r'){
  69.             BZ2fp->mode ='r';
  70.         }else if(*mode =='w'){
  71.             BZ2fp->mode ='w';
  72.         }else if('0' <= *mode && *mode <='9'){
  73.             BZ2fp->level = *mode - '0';
  74.         }
  75.         mode++;
  76.     }
  77.     /*{
  78.         int fd2;
  79.  
  80.         fd2 = dup(zStream);
  81.         if(BZ2fp->mode == 'w'){
  82.             BZ2fp->fp = fdopen(fd2,"wb");
  83.         }else{
  84.             BZ2fp->fp = fdopen(fd2,"rb");
  85.         }
  86.     }*/
  87. endlabel:
  88.     flag_running--;
  89.  
  90.     /*fputc ( 'A',BZ2fp->fp);*/
  91.  
  92.     
  93.     return BZ2fp;
  94. }
  95. /*static FILE * orig_fp;*/
  96.  
  97. BZ2FILE * EXPORTAPI BZ2Open(char *file,char *mode)
  98. {
  99.     FILE *fp;
  100.     BZ2FILE *BZ2fp;
  101.     
  102.     if(strchr(mode,'w')){
  103.         if(file){
  104.             fp=fopen(file,"wb");
  105.         }else{
  106.             fp=stdout;
  107.         }
  108.     }else{
  109.         if(file){
  110.             fp=fopen(file,"rb");
  111.         }else{
  112.             fp=stdin;
  113.         }
  114.     }
  115.     if(fp==NULL){return NULL;}
  116.     BZ2fp=BZ2OpenStream(fp,mode);
  117.     if(BZ2fp==NULL){
  118.         if(!(fp==stdin || fp==stdout)){
  119.             fclose(fp);
  120.         }
  121.         return NULL;
  122.     }
  123.     /*orig_fp = fp;*/
  124.     return BZ2fp;
  125. }
  126. void EXPORTAPI BZ2CloseStream(BZ2FILE *BZ2fp)
  127. {
  128.     flag_running++;
  129.     STRINGQ_puteof(BZ2fp->sq);
  130.  
  131.     BZ2fp->blocking = 0;
  132.     BZ2fp->error = 0;
  133.     if(BZ2fp->mode =='w'){
  134.         compressStream2(BZ2fp->sq,BZ2fp->fp,BZ2fp);
  135.     }else{
  136.         ;/*uncompressStream2(BZ2fp->fp,BZ2fp->sq,BZ2fp);*/
  137.     }
  138.     if(BZ2fp->error == 1){goto endlabel /*return -1*/;}
  139.     fclose(BZ2fp->fp);
  140.     free(BZ2fp);
  141. endlabel:
  142.     flag_running--;
  143. }
  144. void EXPORTAPI BZ2Close(BZ2FILE *BZ2fp)
  145. {
  146.     FILE *fp = BZ2fp->fp;
  147.  
  148.     BZ2CloseStream(BZ2fp);
  149.     if(!(fp==stdin || fp==stdout)){
  150.         fclose(fp);
  151.     }
  152. }
  153. int EXPORTAPI BZ2Write(BZ2FILE *BZ2fp,char *buff,int size)
  154. {
  155.     int len;
  156.     int left_size = size;
  157.     int retval;
  158.  
  159.     flag_running++;
  160.     while(left_size>0){
  161.         len = MIN(left_size,BZ2fp->sq->bufsize);
  162.         STRINGQ_write(BZ2fp->sq,buff,len);
  163.  
  164.         BZ2fp->blocking = 0;
  165.         BZ2fp->error = 0;
  166.         compressStream2(BZ2fp->sq,BZ2fp->fp,BZ2fp);
  167.         if(BZ2fp->error == 1){retval=-1;goto endlabel;}
  168.         left_size -=len;
  169.         buff +=len;
  170.     }
  171.     retval = size;
  172. endlabel:
  173.     flag_running--;
  174.     return retval;
  175. }
  176. int EXPORTAPI BZ2Read(BZ2FILE *BZ2fp,char *buff,int size)
  177. {
  178.     int len;
  179.     int left_size = size;
  180.  
  181.     flag_running++;
  182.     while(left_size>0){
  183.         len = MIN(left_size,BZ2fp->sq->len);
  184.         STRINGQ_read(BZ2fp->sq,buff,len);
  185.         left_size -= len;
  186.         buff += len;
  187.         if(len == left_size){break;}
  188.         if(BZ2fp->sq->exist_eof){break;}
  189.  
  190.         BZ2fp->blocking = 0;
  191.         BZ2fp->error = 0;
  192.         uncompressStream2(BZ2fp->fp,BZ2fp->sq,BZ2fp);
  193.     }
  194.     flag_running--;
  195.     return size - left_size;
  196. }
  197.  
  198.