How to use LZMA SDK in C++? -


i have difficulties in using lzma sdk in application.

i create kind of single file compression tool. dont need directory support, need lzma2 stream. have no idea on how lzma sdk used this.

please can give me little example on how lzma sdk can used under c++?

i think it's little example use lzma sdk.

/* lzmautil.c -- test application lzma compression   2008-08-05   igor pavlov   public domain */     #define _crt_secure_no_warnings     #include <stdio.h>    #include <stdlib.h>    #include <string.h>     #include "../lzmadec.h"    #include "../lzmaenc.h"    #include "../alloc.h"     const char *kcantreadmessage = "can not read input file";    const char *kcantwritemessage = "can not write output file";    const char *kcantallocatemessage = "can not allocate memory";    const char *kdataerrormessage = "data error";     static void *szalloc(void *p, size_t size) { p = p; return myalloc(size); }    static void szfree(void *p, void *address) {  p = p; myfree(address); }    static iszalloc g_alloc = { szalloc, szfree };     #define kinbuffersize (1 << 15)    #define koutbuffersize (1 << 15)     unsigned char g_inbuffer[kinbuffersize];    unsigned char g_outbuffer[koutbuffersize];     size_t myreadfile(file *file, void *data, size_t size)      { return fread(data, 1, size, file); }     int myreadfileandcheck(file *file, void *data, size_t size)      { return (myreadfile(file, data, size) == size); }     size_t mywritefile(file *file, const void *data, size_t size)    {      if (size == 0)        return 0;      return fwrite(data, 1, size, file);    }     int mywritefileandcheck(file *file, const void *data, size_t size)      { return (mywritefile(file, data, size) == size); }     long mygetfilelength(file *file)    {      long length;      fseek(file, 0, seek_end);      length = ftell(file);      fseek(file, 0, seek_set);      return length;    }     void printhelp(char *buffer)    {      strcat(buffer, "\nlzma utility 4.58 copyright (c) 1999-2008 igor pavlov  2008-04-11\n"          "\nusage:  lzma <e|d> inputfile outputfile\n"                 "  e: encode file\n"                 "  d: decode file\n");    }     int printerror(char *buffer, const char *message)    {      strcat(buffer, "\nerror: ");      strcat(buffer, message);      strcat(buffer, "\n");      return 1;    }     int printerrornumber(char *buffer, sres val)    {      sprintf(buffer + strlen(buffer), "\nerror code: %x\n", (unsigned)val);      return 1;    }     int printusererror(char *buffer)    {      return printerror(buffer, "incorrect command");    }     #define in_buf_size (1 << 16)    #define out_buf_size (1 << 16)     static int decode(file *infile, file *outfile, char *rs)    {      uint64 unpacksize;      int thereissize; /* = 1, if there uncompressed size in headers */      int i;      int res = 0;       clzmadec state;       /* header: 5 bytes of lzma properties , 8 bytes of uncompressed size */      unsigned char header[lzma_props_size + 8];       /* read , parse header */       if (!myreadfileandcheck(infile, header, sizeof(header)))        return printerror(rs, kcantreadmessage);       unpacksize = 0;      thereissize = 0;      (i = 0; < 8; i++)      {        unsigned char b = header[lzma_props_size + i];        if (b != 0xff)          thereissize = 1;        unpacksize += (uint64)b << (i * 8);      }       lzmadec_construct(&state);      res = lzmadec_allocate(&state, header, lzma_props_size, &g_alloc);      if (res != sz_ok)        return res;      {        byte inbuf[in_buf_size];        byte outbuf[out_buf_size];        size_t inpos = 0, insize = 0, outpos = 0;        lzmadec_init(&state);        (;;)        {          if (inpos == insize)          {            insize = myreadfile(infile, inbuf, in_buf_size);            inpos = 0;          }          {            sizet inprocessed = insize - inpos;            sizet outprocessed = out_buf_size - outpos;            elzmafinishmode finishmode = lzma_finish_any;            elzmastatus status;            if (thereissize && outprocessed > unpacksize)            {              outprocessed = (sizet)unpacksize;              finishmode = lzma_finish_end;            }             res = lzmadec_decodetobuf(&state, outbuf + outpos, &outprocessed,                inbuf + inpos, &inprocessed, finishmode, &status);            inpos += (uint32)inprocessed;            outpos += outprocessed;            unpacksize -= outprocessed;             if (outfile != 0)              mywritefile(outfile, outbuf, outpos);            outpos = 0;             if (res != sz_ok || thereissize && unpacksize == 0)              break;             if (inprocessed == 0 && outprocessed == 0)            {              if (thereissize || status != lzma_status_finished_with_mark)                res = sz_error_data;              break;            }          }        }      }       lzmadec_free(&state, &g_alloc);      return res;    }     typedef struct _cfileseqinstream    {      iseqinstream functable;      file *file;    } cfileseqinstream;     static sres myread(void *p, void *buf, size_t *size)    {      if (*size == 0)        return sz_ok;      *size = myreadfile(((cfileseqinstream*)p)->file, buf, *size);      /*     if (*size == 0)       return sze_fail;     */      return sz_ok;    }     typedef struct _cfileseqoutstream    {      iseqoutstream functable;      file *file;    } cfileseqoutstream;     static size_t mywrite(void *pp, const void *buf, size_t size)    {      return mywritefile(((cfileseqoutstream *)pp)->file, buf, size);    }     static sres encode(file *infile, file *outfile, char *rs)    {      clzmaenchandle enc;      sres res;      cfileseqinstream instream;      cfileseqoutstream outstream;      clzmaencprops props;       enc = lzmaenc_create(&g_alloc);      if (enc == 0)        return sz_error_mem;       instream.functable.read = myread;      instream.file = infile;      outstream.functable.write = mywrite;      outstream.file = outfile;       lzmaencprops_init(&props);      res = lzmaenc_setprops(enc, &props);       if (res == sz_ok)      {        byte header[lzma_props_size + 8];        size_t headersize = lzma_props_size;        uint64 filesize;        int i;         res = lzmaenc_writeproperties(enc, header, &headersize);        filesize = mygetfilelength(infile);        (i = 0; < 8; i++)          header[headersize++] = (byte)(filesize >> (8 * i));        if (!mywritefileandcheck(outfile, header, headersize))          return printerror(rs, "writing error");         if (res == sz_ok)          res = lzmaenc_encode(enc, &outstream.functable, &instream.functable,            null, &g_alloc, &g_alloc);      }      lzmaenc_destroy(enc, &g_alloc, &g_alloc);      return res;    }     int main2(int numargs, const char *args[], char *rs)    {      file *infile = 0;      file *outfile = 0;      char c;      int res;      int encodemode;       if (numargs == 1)      {        printhelp(rs);        return 0;      }       if (numargs < 3 || numargs > 4 || strlen(args[1]) != 1)        return printusererror(rs);       c = args[1][0];      encodemode = (c == 'e' || c == 'e');      if (!encodemode && c != 'd' && c != 'd')        return printusererror(rs);       {        size_t t4 = sizeof(uint32);        size_t t8 = sizeof(uint64);        if (t4 != 4 || t8 != 8)          return printerror(rs, "lzma util needs correct uint32 , uint64");      }       infile = fopen(args[2], "rb");      if (infile == 0)        return printerror(rs, "can not open input file");       if (numargs > 3)      {        outfile = fopen(args[3], "wb+");        if (outfile == 0)          return printerror(rs, "can not open output file");      }      else if (encodemode)        printusererror(rs);       if (encodemode)      {        res = encode(infile, outfile, rs);      }      else      {        res = decode(infile, outfile, rs);      }       if (outfile != 0)        fclose(outfile);      fclose(infile);       if (res != sz_ok)      {        if (res == sz_error_mem)          return printerror(rs, kcantallocatemessage);        else if (res == sz_error_data)          return printerror(rs, kdataerrormessage);        else          return printerrornumber(rs, res);      }      return 0;    }     int my_cdecl main(int numargs, const char *args[])    {      char rs[800] = { 0 };      int res = main2(numargs, args, rs);      printf(rs);      return res;    } 

also can see at:

http://read.pudn.com/downloads151/sourcecode/zip/656407/7z460/c/lzmautil/lzmautil.c__.htm

http://read.pudn.com/downloads157/sourcecode/zip/698262/lzma/lzmautil.c__.htm


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -