kusano 7d535a
/* 
kusano 7d535a
 * usage : lzocompress.exe <size></size>
kusano 7d535a
 * 
kusano 7d535a
 * lzocompress reads <size> bytes from stdin, compress them and write the result to stdout</size>
kusano 7d535a
 * errors are logged to stderr
kusano 7d535a
 * return == 0 iff no errors
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
#include <fcntl.h></fcntl.h>
kusano 7d535a
kusano 7d535a
kusano 7d535a
#include <stdio.h></stdio.h>
kusano 7d535a
kusano 7d535a
int main(int argc, char *argv[])
kusano 7d535a
{
kusano 7d535a
  lzo_uint src_len, dst_len, max_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 != 2) 
kusano 7d535a
  {
kusano 7d535a
    fprintf(stderr, "Usage : %s <size>\n    <size> is the input buffer size in bytes\n", argv[0]);</size></size>
kusano 7d535a
	return -1; 
kusano 7d535a
  }
kusano 7d535a
  src_len = atoi(argv[1]); 
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
  /* 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 to store the compressed data */
kusano 7d535a
  max_dst_len = src_len + src_len/64 + 16 + 3;  
kusano 7d535a
  dst = (lzo_bytep) lzo_malloc(max_dst_len);
kusano 7d535a
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
  /* compress data */  
kusano 7d535a
  dst_len = 0;
kusano 7d535a
  r = lzo1x_1_compress(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 memory */  
kusano 7d535a
  lzo_free(wrkmem);
kusano 7d535a
  lzo_free(dst);
kusano 7d535a
  lzo_free(src);
kusano 7d535a
  
kusano 7d535a
  /* return final result */
kusano 7d535a
  if (r == LZO_E_OK)
kusano 7d535a
    return 0;
kusano 7d535a
  else 
kusano 7d535a
    return -3;
kusano 7d535a
}
kusano 7d535a
    
kusano 7d535a