Esempio n. 1
0
/* The flag d is the direction (compress, decompress). Non zero is
 * decompress.
 */
int _gnutls_comp_init(comp_hd_st * handle,
		      gnutls_compression_method_t method, int d)
{
	handle->algo = method;
	handle->handle = NULL;

	switch (method) {
	case GNUTLS_COMP_DEFLATE:
#ifdef HAVE_LIBZ
		{
			int window_bits, mem_level;
			int comp_level;
			z_stream *zhandle;
			int err;

			window_bits = get_wbits(method);
			mem_level = get_mem_level(method);
			comp_level = get_comp_level(method);

			handle->handle = gnutls_malloc(sizeof(z_stream));
			if (handle->handle == NULL)
				return
				    gnutls_assert_val
				    (GNUTLS_E_MEMORY_ERROR);

			zhandle = handle->handle;

			zhandle->zalloc = (alloc_func) 0;
			zhandle->zfree = (free_func) 0;
			zhandle->opaque = (voidpf) 0;

			if (d)
				err = inflateInit2(zhandle, window_bits);
			else {
				err = deflateInit2(zhandle,
						   comp_level, Z_DEFLATED,
						   window_bits, mem_level,
						   Z_DEFAULT_STRATEGY);
			}
			if (err != Z_OK) {
				gnutls_assert();
				gnutls_free(handle->handle);
				return GNUTLS_E_COMPRESSION_FAILED;
			}
		}
		break;
#endif
	case GNUTLS_COMP_NULL:
	case GNUTLS_COMP_UNKNOWN:
		break;
	default:
		return GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM;
	}

	return 0;
}
Esempio n. 2
0
/* The flag d is the direction (compress, decompress). Non zero is
 * decompress.
 */
comp_hd_t
_gnutls_comp_init (gnutls_compression_method_t method, int d)
{
  comp_hd_t ret;

  ret = gnutls_malloc (sizeof (struct comp_hd_t_STRUCT));
  if (ret == NULL)
    {
      gnutls_assert ();
      return NULL;
    }

  ret->algo = method;
  ret->handle = NULL;

  switch (method)
    {
    case GNUTLS_COMP_DEFLATE:
#ifdef HAVE_LIBZ
      {
        int window_bits, mem_level;
        int comp_level;
        z_stream *zhandle;
        int err;

        window_bits = get_wbits (method);
        mem_level = get_mem_level (method);
        comp_level = get_comp_level (method);

        ret->handle = gnutls_malloc (sizeof (z_stream));
        if (ret->handle == NULL)
          {
            gnutls_assert ();
            goto cleanup_ret;
          }

        zhandle = ret->handle;

        zhandle->zalloc = (alloc_func) 0;
        zhandle->zfree = (free_func) 0;
        zhandle->opaque = (voidpf) 0;

        if (d)
          err = inflateInit2 (zhandle, window_bits);
        else
          {
            err = deflateInit2 (zhandle,
                                comp_level, Z_DEFLATED,
                                window_bits, mem_level, Z_DEFAULT_STRATEGY);
          }
        if (err != Z_OK)
          {
            gnutls_assert ();
            gnutls_free (ret->handle);
            goto cleanup_ret;
          }
      }
      break;
#endif
    case GNUTLS_COMP_LZO:
#ifdef USE_LZO
      /* LZO does not use memory on decompressor */
      if (!d)
        {
          ret->handle = gnutls_malloc (LZO1X_1_MEM_COMPRESS);

          if (ret->handle == NULL)
            {
              gnutls_assert ();
              goto cleanup_ret;
            }
        }
      break;
#endif
    case GNUTLS_COMP_NULL:
    case GNUTLS_COMP_UNKNOWN:
      break;
    }

  return ret;

cleanup_ret:
  gnutls_free (ret);
  return NULL;
}