linux/fs/logfs/compr.c
<<
>>
Prefs
   1/*
   2 * fs/logfs/compr.c     - compression routines
   3 *
   4 * As should be obvious for Linux kernel code, license is GPLv2
   5 *
   6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
   7 */
   8#include "logfs.h"
   9#include <linux/vmalloc.h>
  10#include <linux/zlib.h>
  11
  12#define COMPR_LEVEL 3
  13
  14static DEFINE_MUTEX(compr_mutex);
  15static struct z_stream_s stream;
  16
  17int logfs_compress(void *in, void *out, size_t inlen, size_t outlen)
  18{
  19        int err, ret;
  20
  21        ret = -EIO;
  22        mutex_lock(&compr_mutex);
  23        err = zlib_deflateInit(&stream, COMPR_LEVEL);
  24        if (err != Z_OK)
  25                goto error;
  26
  27        stream.next_in = in;
  28        stream.avail_in = inlen;
  29        stream.total_in = 0;
  30        stream.next_out = out;
  31        stream.avail_out = outlen;
  32        stream.total_out = 0;
  33
  34        err = zlib_deflate(&stream, Z_FINISH);
  35        if (err != Z_STREAM_END)
  36                goto error;
  37
  38        err = zlib_deflateEnd(&stream);
  39        if (err != Z_OK)
  40                goto error;
  41
  42        if (stream.total_out >= stream.total_in)
  43                goto error;
  44
  45        ret = stream.total_out;
  46error:
  47        mutex_unlock(&compr_mutex);
  48        return ret;
  49}
  50
  51int logfs_uncompress(void *in, void *out, size_t inlen, size_t outlen)
  52{
  53        int err, ret;
  54
  55        ret = -EIO;
  56        mutex_lock(&compr_mutex);
  57        err = zlib_inflateInit(&stream);
  58        if (err != Z_OK)
  59                goto error;
  60
  61        stream.next_in = in;
  62        stream.avail_in = inlen;
  63        stream.total_in = 0;
  64        stream.next_out = out;
  65        stream.avail_out = outlen;
  66        stream.total_out = 0;
  67
  68        err = zlib_inflate(&stream, Z_FINISH);
  69        if (err != Z_STREAM_END)
  70                goto error;
  71
  72        err = zlib_inflateEnd(&stream);
  73        if (err != Z_OK)
  74                goto error;
  75
  76        ret = 0;
  77error:
  78        mutex_unlock(&compr_mutex);
  79        return ret;
  80}
  81
  82int __init logfs_compr_init(void)
  83{
  84        size_t size = max(zlib_deflate_workspacesize((  61      ax"gfs/compr.c#L42" id="L42mteEnd(&stream);
      ax"gfs/compr.c#L42" id="L42mteE#L42" id="L42mt83">  83{
(&(&);)L80" class="line" name="L80"> 8/a>8ne" name="L67">  67
 8   );
  67
size_ame="L9">>   9#include <  78    9ine" nam9="L82">  82int __initlogfs_compr_init(void9
  84      exne href="+code=size_t" class="sref">size9t size = );
  78    9s/compr49ef="fs/logfs/compr.c#L79" id=95de=__init" class="sref">__init9sref">/pre55104c9b

line" name="L+code=efooter">
The original LXR software by th="L14" clashttp://sourceforge.net/project="sxr">LXR ef="u 
8xr.defin.no kindly hosted"by L14" clashttp://www.redpill-defpro.no  Redpillr.c#pro A"line" provider ofr.c#L6"c