kusano 7d535a
/* 
kusano 7d535a
 * usage : lzodecompress.exe <output-size> <input-size></input-size></output-size>
kusano 7d535a
 * 
kusano 7d535a
 * lzodecompress reads <input-size> bytes from stdin, decompress them and write the result to stdout</input-size>
kusano 7d535a
 * the decompressed buffer must be <output-size> bytes </output-size>
kusano 7d535a
 *
kusano 7d535a
 * errors are logged to stderr
kusano 7d535a
 * return == 0 iff no errors
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "lzo/lzoconf.h"
kusano 7d535a
#include "lzo/lzo1x.h"
kusano 7d535a
kusano 7d535a
/* portability layer */
kusano 7d535a
#define WANT_LZO_MALLOC 1
roentgen 3047bb
#include "lzo/lzoutil.h"
kusano 7d535a
kusano 7d535a
#include <stdio.h></stdio.h>
kusano 7d535a
#include <fcntl.h></fcntl.h>
kusano d191aa
#include <stdlib.h></stdlib.h>
kusano d191aa
#if defined(_WIN32)
kusano d191aa
#include <io.h></io.h>
kusano d191aa
#endif
kusano 7d535a
kusano 7d535a
int main(int argc, char *argv[])
kusano 7d535a
{
kusano 7d535a
  lzo_uint src_len, dst_len;
kusano 7d535a
  lzo_bytep src;
kusano 7d535a
  lzo_bytep dst;
kusano 7d535a
  lzo_bytep wrkmem;
kusano 7d535a
  int r;
kusano 7d535a
kusano 7d535a
#if defined(_WIN32)
kusano 7d535a
  /* stdin/stdout must be binary streams */
kusano 7d535a
  r = _setmode(_fileno(stdout), _O_BINARY);
kusano 7d535a
  r = _setmode(_fileno(stdin), _O_BINARY);
kusano 7d535a
#endif
kusano 7d535a
  
kusano 7d535a
  if(argc != 3) 
kusano 7d535a
  {
kusano 7d535a
    fprintf(stderr, "Usage : %s <output-size> <input-size>\n"</input-size></output-size>
kusano 7d535a
	                "    <output-size> is the decompressed buffer size in bytes\n"</output-size>
kusano 7d535a
                    "    <input-size> is the compressed buffer size in bytes\n", argv[0]);</input-size>
kusano 7d535a
    return -1; 
kusano 7d535a
  }
kusano 7d535a
  dst_len = atoi(argv[1]); 
kusano 7d535a
  src_len = atoi(argv[2]); 
kusano 7d535a
  if (lzo_init() != LZO_E_OK)
kusano 7d535a
  {
kusano 7d535a
    fprintf(stderr, "Couldn't initialize lzo\n");
kusano 7d535a
	return -2;
kusano 7d535a
  }
kusano 7d535a
    
kusano 7d535a
  /* allocate and read input buffer */
kusano 7d535a
  src = (lzo_bytep) lzo_malloc(src_len);  
kusano 7d535a
  fread(src, 1, src_len, stdin);
kusano 7d535a
kusano 7d535a
  /* allocate the buffer for the decompressed data */  
kusano 7d535a
  dst = (lzo_bytep) lzo_malloc(dst_len);
kusano 7d535a
  
kusano 7d535a
  /* allocate the working memory for lzo */  
kusano 7d535a
  wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS);
kusano 7d535a
  
kusano 7d535a
  /* decompress data (note: lzo1x_decompress_safe does not throw exception. It instead can return error codes) */
kusano 7d535a
  r = lzo1x_decompress_safe(src, src_len, dst, &dst_len, wrkmem);
kusano 7d535a
  
kusano 7d535a
  if (r == LZO_E_OK)
kusano 7d535a
  {
kusano 7d535a
    /* write compressed data */
kusano 7d535a
	fwrite(dst, 1, dst_len, stdout);
kusano 7d535a
  }
kusano 7d535a
  else
kusano 7d535a
  {
kusano 7d535a
    fprintf(stderr, "Compression failed : code = %d\n", r);
kusano 7d535a
  }  
kusano 7d535a
  
kusano 7d535a
  /* free the memory */  
kusano 7d535a
  lzo_free(wrkmem);
kusano 7d535a
  lzo_free(dst);
kusano 7d535a
  lzo_free(src);
kusano 7d535a
  
kusano 7d535a
  /* return code */
kusano 7d535a
  if (r == LZO_E_OK)
kusano 7d535a
    return 0;
kusano 7d535a
  else 
kusano 7d535a
    return -3;
kusano 7d535a
}