/*
 * Copyright (c) 2008, Swedish Institute of Computer Science
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @(#)$Id: example.c,v 1.3 2008/07/14 21:44:57 nvt Exp $
 */

/**
 * \file
 *         SBZIP usage example.
 * \author
 *         Nicolas Tsiftes <nvt@sics.se>
 *
 */

#include <stdio.h>
#include <string.h>
#include "sbz.h"

int
main(int argc, char *argv[])
{
  unsigned char *inbuf, *outbuf;
  int len, clen, index;

  if (argc < 2) {
    fprintf(stderr, "Usage: %s <string>\n", argv[0]);
     return EXIT_FAILURE;
  }

  len = strlen(argv[1]);

  inbuf = malloc(len + 1);
  outbuf = malloc(len + 2);
  if (inbuf == NULL || outbuf == NULL) {
    fprintf(stderr, "failed to allocate output buffer\n");
    return EXIT_FAILURE;
  }

  memcpy(inbuf, argv[1], len);

  index = 0;
  clen = sbz_compress(inbuf, outbuf, len, &index);
  if(clen < 0) {
    fputs("compression failed\n", stderr);
     return EXIT_FAILURE;
  }
  printf("Compressed buffer with length %d to length %d\n", len, clen);

  memset(inbuf, 0, len);
  len = sbz_decompress(outbuf, inbuf, index);
  if(len < 0) {
    fputs("decompression failed\n", stderr);
    return EXIT_FAILURE;
  }

  inbuf[len] = '\0';
  printf("Original data: %s (length = %d)\n", inbuf, len);

  return 0;
}
