Aw zip

From ActiveWiki
Jump to navigation Jump to search


Minimum requirements
Added in version 4.2
SDKbuild 70


int aw_zip (unsigned char *data_out, unsigned int *len_out,
            unsigned char *data_in, unsigned int len_in);

Description

Compresses the input data into the output buffer.

Callback

None (returns immediately)

Notes

This is a wrapper for compress2 which is part of the zlib compression library. The level parameter of the function call is set to 9 by the SDK.

Arguments

data_out
Output buffer. Needs to be at least 0.1% larger than the input data plus an additional 12 bytes to guarantee success (i.e. no RC_Z_BUF_ERROR).
len_out
On entry, it must point to the total size of the output buffer, which must be large enough to hold the entire compressed data. On return, it points to the actual size of the compressed data, provided that the call was successful.
data_in
Input buffer.
len_in
Length of the input data.

Argument attributes

None

Return values

RC_SUCCESS
RC_INVALID_ARGUMENT
A pointer was NULL, or a length was 0.
RC_Z_MEM_ERROR
Memory could not be allocated for processing.
RC_Z_BUF_ERROR
Not enough room in the output buffer.

Returned attributes

None

Usage

void test_zip (void)
{
  unsigned char in[1024];
  unsigned int len_in; /* size of the data to be compressed */

  unsigned char out[1047]; /* sizeof (in) * 1.01 + 13 = 1047, adding 1 to round up instead of truncating */
  unsigned int len_out; /* size of the output buffer, will contain size of compressed data if aw_zip is successful */

  unsigned int i;
  int rc;

  strcpy ((char*)in, "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer mi. Proin in eros. Curabitur a "
                     "purus id mauris lacinia tincidunt. Fusce sed libero. Praesent cursus blandit leo. Quisque eleifen"
                     "d. Fusce interdum mi in quam. Nulla at metus. Cras facilisis dapibus dolor. Class aptent taciti s"
                     "ociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vivamus varius sagittis me"
                     "tus. Aliquam erat volutpat. Sed quis eros et ante egestas auctor. Aliquam ultrices posuere mi.");

  len_in = strlen ((char*)in) + 1;
  len_out = sizeof (out);

  rc = aw_zip (out, &len_out, in, len_in);
  if (rc != RC_SUCCESS)
  {
    printf ("Unable to compress data (reason %d)\n", rc);
    return;
  }

  printf ("Uncompressed data: ");
  for (i = 0; i < len_in; i++)
    printf ("%2.2x", in[i]);
  printf ("\n");

  printf ("Compressed data: ");
  for (i = 0; i < len_out; i++)
    printf ("%2.2x", out[i]);
  printf ("\n");

  printf ("Data was compressed from %d to %d bytes\n", len_in, len_out);
}

See also