"Fossies" - the Fresh Open Source Software Archive

Member "sfk-1.9.6/sfkpackio.cpp" (22 Feb 2020, 16531 Bytes) of package /linux/misc/sfk-1.9.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sfkpackio.cpp" see the Fossies "Dox" file reference documentation.

    1 
    2 #include "sfkpackio.hpp"
    3 
    4 uint sfkPackSum(uchar *buf, uint len, uint crc)
    5 {
    6    return crc32_z(crc, (const uchar *)buf, len);
    7 }
    8 
    9 // IN: bPack
   10 int sfkPackStart(SFKPackStream *p)
   11 {
   12    z_stream *pstrm = new z_stream;
   13 
   14    memset(pstrm, 0, sizeof(z_stream));
   15 
   16    int ret = 0;
   17    p->szerr[0] = '\0';
   18 
   19    if (p->bPack)
   20    {
   21       int windowBits = 15;
   22       int GZIP_ENCODING = 16;
   23 
   24       ret = deflateInit2(pstrm,
   25                p->bFast ? Z_BEST_SPEED : Z_DEFAULT_COMPRESSION,
   26                Z_DEFLATED,
   27                windowBits | GZIP_ENCODING,
   28                8,
   29                Z_DEFAULT_STRATEGY);
   30    }
   31    else
   32    {
   33       ret = inflateInit2(pstrm, 16+MAX_WBITS);
   34    }
   35 
   36    p->pstream = pstrm;
   37 
   38    return ret;
   39 }
   40 
   41 // IN: pin, nin, pout, nout
   42 // CONV: if nin returned is >0 call again
   43 int sfkPackProc(SFKPackStream *p, int bLastBlock, int *bReloop)
   44 {
   45    z_stream *pstrm = (z_stream *)p->pstream;
   46  
   47    int ret = 0;
   48    p->szerr[0] = '\0';
   49 
   50    if (p->bPack)
   51    {
   52       pstrm->next_in   = p->pin;
   53       pstrm->avail_in  = p->nin;
   54       pstrm->next_out  = p->pout;
   55       pstrm->avail_out = p->nout;
   56 
   57       ret = deflate(pstrm, bLastBlock ? Z_FINISH : Z_PARTIAL_FLUSH);
   58    }
   59    else
   60    {
   61       if (*bReloop == 0)
   62       {
   63          pstrm->next_in   = p->pin;
   64          pstrm->avail_in  = p->nin;
   65       }
   66 
   67       pstrm->next_out  = p->pout;
   68       pstrm->avail_out = p->nout;
   69 
   70       ret = inflate(pstrm, Z_NO_FLUSH);
   71 
   72       // printf("inf nout=%d availout=%d availin=%d\n",
   73       //   p->nout, pstrm->avail_out, pstrm->avail_in);
   74    }
   75 
   76    if (ret!=0 && pstrm->msg!=0)
   77    {
   78       int imaxlen = (int)sizeof(p->szerr)-10;
   79       strncpy(p->szerr, pstrm->msg, imaxlen);
   80       p->szerr[imaxlen] = '\0';
   81    }
   82 
   83    p->nout = p->nout - pstrm->avail_out;
   84    p->nin  = pstrm->avail_in;
   85 
   86    // reference does this. sometimes it causes reloops 
   87    // with empty input, followed by Z_BUF_ERROR,
   88    // but the Z_BUF_ERROR is IGNORED, and it works.
   89    *bReloop = (pstrm->avail_out == 0) ? 1 : 0;
   90 
   91    return ret;
   92 }
   93 
   94 int sfkPackEnd(SFKPackStream *p)
   95 {
   96    z_stream *pstrm = (z_stream *)p->pstream;
   97 
   98    int ret = 0;
   99    p->szerr[0] = '\0';
  100 
  101    if (p->bPack)
  102       ret = deflateEnd(pstrm);
  103    else
  104       ret = inflateEnd(pstrm);
  105 
  106    p->pstream = 0;
  107 
  108    return ret;
  109 }
  110 
  111 void zipGetStatus(void *file, sfkuint64 *pTotalOut)
  112 {
  113    zip64_internal* zi = (zip64_internal *)file;
  114    *pTotalOut = zi->ci.totalCompressedData;
  115 }
  116 
  117 int sfkOpenNewFileInZip(void *file,
  118       const char* filename, const zip_fileinfo* zipfi,
  119       const void* extrafield_local, uInt size_extrafield_local,
  120       const void* extrafield_global, uInt size_extrafield_global,
  121       const char* comment, int method, int level, int raw,
  122       int windowBits,int memLevel, int strategy,
  123       const char* password, uLong crcForCrypting,
  124       uLong versionMadeBy, uLong flagBase, int zip64
  125  )
  126 {
  127     zip64_internal* zi;
  128     uInt size_filename;
  129     uInt size_comment;
  130     uInt i;
  131     int err = ZIP_OK;
  132 #    ifdef NOCRYPT
  133     (crcForCrypting);
  134     if (password != NULL)
  135         return ZIP_PARAMERROR;
  136 #    endif
  137     if (file == NULL)
  138         return ZIP_PARAMERROR;
  139 #ifdef HAVE_BZIP2
  140     if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED))
  141       return ZIP_PARAMERROR;
  142 #else
  143     if ((method!=0) && (method!=Z_DEFLATED))
  144       return ZIP_PARAMERROR;
  145 #endif
  146     zi = (zip64_internal*)file;
  147     if (zi->in_opened_file_inzip == 1)
  148     {
  149         err = zipCloseFileInZip (file);
  150         if (err != ZIP_OK)
  151             return err;
  152     }
  153     if (filename==NULL)
  154         filename="-";
  155     if (comment==NULL)
  156         size_comment = 0;
  157     else
  158         size_comment = (uInt)strlen(comment);
  159     size_filename = (uInt)strlen(filename);
  160     if (zipfi == NULL)
  161         zi->ci.dosDate = 0;
  162     else
  163     {
  164         if (zipfi->dosDate != 0)
  165             zi->ci.dosDate = zipfi->dosDate;
  166         else
  167           zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date);
  168     }
  169 
  170     zi->ci.zip64 = zip64; // fix: stw
  171 
  172     zi->ci.flag = flagBase;
  173     if ((level==8) || (level==9))
  174       zi->ci.flag |= 2;
  175     if (level==2)
  176       zi->ci.flag |= 4;
  177     if (level==1)
  178       zi->ci.flag |= 6;
  179     if (password != NULL)
  180       zi->ci.flag |= 1;
  181     zi->ci.crc32 = 0;
  182     zi->ci.method = method;
  183     zi->ci.encrypt = 0;
  184     zi->ci.stream_initialised = 0;
  185     zi->ci.pos_in_buffered_data = 0;
  186     zi->ci.raw = raw;
  187     zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream);
  188     zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment;
  189     zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data
  190     zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree);
  191     zi->ci.size_centralExtra = size_extrafield_global;
  192     zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
  193 
  194     zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2);
  195 
  196     if(zi->ci.zip64) {
  197       // printf("sfkOpenNew: write 45 1 %d\n",zip64);
  198       zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2); // fix: stw
  199     } else {
  200       // printf("sfkOpenNew: write 20 0 %d\n",zip64);
  201       zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
  202     }
  203 
  204     zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
  205     zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
  206     zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
  207     zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4);
  208     zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4);
  209     zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4);
  210     zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
  211     zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
  212     zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
  213     zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2);
  214     if (zipfi==NULL)
  215         zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
  216     else
  217         zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
  218     if (zipfi==NULL)
  219         zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
  220     else
  221         zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
  222     if(zi->ci.pos_local_header >= 0xffffffff)
  223       zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4);
  224     else
  225       zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writing_offset,4);
  226     for (i=0;i<size_filename;i++)
  227         *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
  228     for (i=0;i<size_extrafield_global;i++)
  229         *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
  230               *(((const char*)extrafield_global)+i);
  231     for (i=0;i<size_comment;i++)
  232         *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
  233               size_extrafield_global+i) = *(comment+i);
  234     if (zi->ci.central_header == NULL)
  235         return ZIP_INTERNALERROR;
  236     zi->ci.zip64 = zip64;
  237     zi->ci.totalCompressedData = 0;
  238     zi->ci.totalUncompressedData = 0;
  239     zi->ci.pos_zip64extrainfo = 0;
  240     err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local);
  241 #ifdef HAVE_BZIP2
  242     zi->ci.bstream.avail_in = (uInt)0;
  243     zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
  244     zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
  245     zi->ci.bstream.total_in_hi32 = 0;
  246     zi->ci.bstream.total_in_lo32 = 0;
  247     zi->ci.bstream.total_out_hi32 = 0;
  248     zi->ci.bstream.total_out_lo32 = 0;
  249 #endif
  250     zi->ci.stream.avail_in = (uInt)0;
  251     zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
  252     zi->ci.stream.next_out = zi->ci.buffered_data;
  253     zi->ci.stream.total_in = 0;
  254     zi->ci.stream.total_out = 0;
  255     zi->ci.stream.data_type = Z_BINARY;
  256 #ifdef HAVE_BZIP2
  257     if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
  258 #else
  259     if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  260 #endif
  261     {
  262         if(zi->ci.method == Z_DEFLATED)
  263         {
  264           zi->ci.stream.zalloc = (alloc_func)0;
  265           zi->ci.stream.zfree = (free_func)0;
  266           zi->ci.stream.opaque = (voidpf)0;
  267           if (windowBits>0)
  268               windowBits = -windowBits;
  269           err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy);
  270           if (err==Z_OK)
  271               zi->ci.stream_initialised = Z_DEFLATED;
  272         }
  273         else if(zi->ci.method == Z_BZIP2ED)
  274         {
  275 #ifdef HAVE_BZIP2
  276             // Init BZip stuff here
  277           zi->ci.bstream.bzalloc = 0;
  278           zi->ci.bstream.bzfree = 0;
  279           zi->ci.bstream.opaque = (voidpf)0;
  280           err = sp_BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35);
  281           if(err == BZ_OK)
  282             zi->ci.stream_initialised = Z_BZIP2ED;
  283 #endif
  284         }
  285     }
  286 #    ifndef NOCRYPT
  287     zi->ci.crypt_header_size = 0;
  288     if ((err==Z_OK) && (password != NULL))
  289     {
  290         unsigned char bufHead[RAND_HEAD_LEN];
  291         unsigned int sizeHead;
  292         zi->ci.encrypt = 1;
  293         zi->ci.pcrc_32_tab = get_crc_table();
  294 
  295         sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
  296         zi->ci.crypt_header_size = sizeHead;
  297         if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
  298                 err = ZIP_ERRNO;
  299     }
  300 #    endif
  301     if (err==Z_OK)
  302         zi->in_opened_file_inzip = 1;
  303     return err;
  304 }
  305 
  306 #ifdef TESTCOMP
  307 
  308 #include <stdarg.h>
  309 
  310 #ifdef _WIN32
  311  typedef __int64 num;
  312 #else
  313  typedef long long num;
  314 #endif
  315 
  316 static int perr(const char *pszFormat, ...)
  317 {
  318    va_list argList;
  319    va_start(argList, pszFormat);
  320    char szBuf[1024];
  321    ::vsprintf(szBuf, pszFormat, argList);
  322    fprintf(stderr, "error: %s", szBuf);
  323    return 0;
  324 }
  325 
  326 #define CHUNK 16384
  327 #define USE_GZIP
  328 
  329 num nDoneIn=0,nDoneOut=0;
  330 
  331 int def(FILE *source, FILE *dest, int level)
  332 {
  333     int ret, flush;
  334     unsigned have;
  335     z_stream strm;
  336     unsigned char in[CHUNK];
  337     unsigned char out[CHUNK];
  338 
  339     strm.zalloc = Z_NULL;
  340     strm.zfree = Z_NULL;
  341     strm.opaque = Z_NULL;
  342 
  343     #ifdef USE_GZIP
  344     int windowBits = 15;
  345     int GZIP_ENCODING = 16;
  346     ret = deflateInit2 (&strm,
  347             Z_DEFAULT_COMPRESSION,
  348             Z_DEFLATED,
  349             windowBits | GZIP_ENCODING,
  350             8,
  351             Z_DEFAULT_STRATEGY);
  352     #else
  353     ret = deflateInit(&strm, level);
  354     #endif
  355 
  356     if (ret != Z_OK)
  357         return ret;
  358 
  359     do
  360     {
  361         strm.avail_in = fread(in, 1, CHUNK, source);
  362         if (ferror(source)) {
  363             (void)deflateEnd(&strm);
  364             return Z_ERRNO;
  365         }
  366         flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
  367         strm.next_in = in;
  368 
  369         // printf("Read %d eof=%d\n", strm.avail_in, flush);
  370         nDoneIn += strm.avail_in;
  371 
  372         do
  373         {
  374             strm.avail_out = CHUNK;
  375             strm.next_out = out;
  376             ret = deflate(&strm, flush);    /* no bad return value */
  377             have = CHUNK - strm.avail_out;
  378 
  379             // printf("Write %d\n", have);
  380             if (fwrite(out, 1, have, dest) != have || ferror(dest))
  381             {
  382                 (void)deflateEnd(&strm);
  383                 return Z_ERRNO;
  384             }
  385             nDoneOut += have;
  386 
  387         } while (strm.avail_out == 0);
  388 
  389     } while (flush != Z_FINISH);
  390 
  391     (void)deflateEnd(&strm);
  392     return Z_OK;
  393 }
  394 
  395 int inf(FILE *source, FILE *dest)
  396 {
  397     int ret;
  398     unsigned have;
  399     z_stream strm;
  400     unsigned char in[CHUNK];
  401     unsigned char out[CHUNK];
  402 
  403     strm.zalloc = Z_NULL;
  404     strm.zfree = Z_NULL;
  405     strm.opaque = Z_NULL;
  406     strm.avail_in = 0;
  407     strm.next_in = Z_NULL;
  408 
  409     #ifdef USE_GZIP
  410     ret = inflateInit2(&strm, 16+MAX_WBITS);
  411     #else
  412     ret = inflateInit(&strm);
  413     #endif
  414 
  415     if (ret != Z_OK)
  416         return ret;
  417 
  418     do
  419     {
  420         strm.avail_in = fread(in, 1, CHUNK, source);
  421 
  422         if (ferror(source)) {
  423             (void)inflateEnd(&strm);
  424             return Z_ERRNO;
  425         }
  426 
  427         if (strm.avail_in == 0)
  428             break;
  429 
  430         strm.next_in = in;
  431 
  432         nDoneIn += strm.avail_in;
  433 
  434         do
  435         {
  436             strm.avail_out = CHUNK;
  437             strm.next_out = out;
  438             ret = inflate(&strm, Z_NO_FLUSH);
  439             switch (ret) {
  440             case Z_NEED_DICT:
  441                 ret = Z_DATA_ERROR;     /* and fall through */
  442             case Z_DATA_ERROR:
  443             case Z_MEM_ERROR:
  444                 (void)inflateEnd(&strm);
  445                 return ret;
  446             }
  447             have = CHUNK - strm.avail_out;
  448 
  449             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
  450                 (void)inflateEnd(&strm);
  451                 return Z_ERRNO;
  452             }
  453             nDoneOut += have;
  454 
  455         } while (strm.avail_out == 0);
  456 
  457     } while (ret != Z_STREAM_END);
  458 
  459     (void)inflateEnd(&strm);
  460 
  461     return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  462 }
  463 
  464 int execPackFile(FILE *pin, FILE *pout, bool bPack, char *pszFilename)
  465 {
  466    int iInBufSize  = 100000;
  467    int iOutBufSize = 100000;
  468 
  469    char *pInBuf  = new char[iInBufSize+100];
  470    char *pOutBuf = new char[iOutBufSize+100];
  471 
  472    if (!pInBuf || !pOutBuf)
  473       return 9+perr("outofmem\n");
  474 
  475    int nLastDoneMB = 0;
  476 
  477    SFKPackStream ostrm;
  478    memset(&ostrm, 0, sizeof(ostrm));
  479 
  480    ostrm.bPack = bPack;
  481 
  482    sfkPackStart(&ostrm);
  483 
  484    bool bLastBlock = 0;
  485    int  bReloop = 0, nRead = 0, isubrc = 0;
  486 
  487    int irc = 0;
  488 
  489    while (1)
  490    {
  491       if (bReloop == 0)
  492       {
  493          int iMaxRead = iInBufSize;
  494 
  495          nRead  = fread(pInBuf, 1, iMaxRead, pin);
  496 
  497          if (nRead < 1)
  498             break;
  499          if (nRead < iMaxRead)
  500             bLastBlock = 1;
  501  
  502          nDoneIn += nRead;
  503  
  504          ostrm.pin  = (uchar*)pInBuf;
  505          ostrm.nin  = nRead;
  506       }
  507 
  508       ostrm.pout = (uchar*)pOutBuf;
  509       ostrm.nout = iOutBufSize;
  510 
  511       isubrc = sfkPackProc(&ostrm, bLastBlock, &bReloop);
  512 
  513       if (isubrc > 1)
  514       {
  515          perr("extract error %d\n",isubrc);
  516          irc = isubrc;
  517          break;
  518       }
  519 
  520       if (ostrm.nout > 0) {
  521          if (fwrite(pOutBuf, 1, ostrm.nout, pout) < ostrm.nout) {
  522             perr("cannot fully write, disk full\n");
  523             irc = 9;
  524             break;
  525          }
  526       }
  527 
  528       nDoneOut += ostrm.nout;
  529 
  530       int iDoneMB = nDoneIn/1000000;
  531       int iOutMB  = nDoneOut/1000000;
  532       if (iDoneMB != nLastDoneMB)
  533       {
  534          nLastDoneMB = iDoneMB;
  535          printf("%05d/%05d mb %s %s\r",
  536             iDoneMB, iOutMB, bPack ? "pack":"unpk", pszFilename);
  537       }
  538    }
  539 
  540    sfkPackEnd(&ostrm);
  541 
  542    delete [] pInBuf;
  543    delete [] pOutBuf;
  544 
  545    return irc;
  546 }
  547 
  548 /*
  549 cl -DTESTCOMP -omycomp.exe sfkpack.cpp
  550 */
  551 
  552 int main(int argc, char *argv[])
  553 {
  554    if (argc<4)
  555    {
  556       printf("usage: mycomp c in.dat out.gz\n"
  557              "       mycomp d in.gz  out.dat\n");
  558       return 0;
  559    }
  560 
  561    char *pszMode    = argv[1];
  562    char *pszInFile  = argv[2];
  563    char *pszOutFile = argv[3];
  564 
  565    int err = 0;
  566    int bcomp = 0;
  567 
  568    if (!strcmp(pszMode, "c"))
  569       bcomp = 1;
  570    else
  571    if (!strcmp(pszMode, "d"))
  572       bcomp = 0;
  573    else
  574       return 9+perr("wrong mode\n");
  575 
  576    FILE *fin = fopen(pszInFile,"rb");
  577    if (!fin)
  578       return 9+perr("cannot read\n");
  579 
  580    FILE *fout=fopen(pszOutFile,"wb");
  581    if (!fout)
  582       return 9+perr("cannot write\n");
  583 
  584    #if 1
  585 
  586    err = execPackFile(fin, fout, bcomp, pszInFile);
  587 
  588    #else
  589 
  590    if (bcomp)
  591       err = def(fin, fout, Z_DEFAULT_COMPRESSION);
  592    else
  593       err = inf(fin, fout);
  594 
  595    #endif
  596 
  597    fclose(fout);
  598    fclose(fin);
  599 
  600    if (err > 0)
  601       printf("rc %d\n",err);
  602 
  603    #ifdef _WIN32
  604    printf("Done from %I64d to %I64d bytes.\n", nDoneIn, nDoneOut);
  605    #else
  606    printf("Done from %lld to %lld bytes.\n", nDoneIn, nDoneOut);
  607    #endif
  608 
  609    return 0;
  610 }
  611 
  612 #endif
  613