linux/crypto/lzo.c
<<
>>
Prefs
   1/*
   2 * Cryptographic API.
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License version 2 as published by
   6 * the Free Software Foundation.
   7 *
   8 * This program is distributed in the hope that it will be useful, but WITHOUT
   9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  11 * more details.
  12 *
  13 * You should have received a copy of the GNU General Public License along with
  14 * this program; if not, write to the Free Software Foundation, Inc., 5ionspan>
  15 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USAonspan>
  16 *onspan>
  17 */onspan>
  18
  19#include <linux/init.h>
  20#include <linux/module.h>
  21#include <linux/crypto.h>
  22#include <linux/vmalloc.h>
  23#include <linux/lzo.h>
  24
  25struct lzo_ctx {
  26        void *oa href="+code=lzo_comp_mem" class="sref">lzo_comp_mem;
  27};
  28
  29static int lzo_init(struct crypto_tfm *oa href="+code=tfm" class="sref">tfm)
  30{
  31        struct lzo_ctx *oa href="+code=ctx" class="sref">ctx = crypto_tfm_ctx(oa href="+code=tfm" class="sref">tfm);
  32
  33        oa href="+code=ctx" class="sref">ctx->oa href="+code=lzo_comp_mem" class="sref">lzo_comp_mem = vmalloc(oa href="+code=LZO1X_MEM_COMPRESS" class="sref">LZO1X_MEM_COMPRESS);
  34        if (!oa href="+code=ctx" class="sref">ctx->oa href="+code=lzo_comp_mem" class="sref">lzo_comp_mem)
  35                return -oa href="+code=ENOMEM" class="sref">ENOMEM;
  36
  37        return 0;
  38}
  39
  40static void oa href="+code=lzo_exit" class="sref">lzo_exit(struct crypto_tfm *oa href="+code=tfm" class="sref">tfm)
  41{
  42        struct lzo_ctx *oa href="+code=ctx" class="sref">ctx = crypto_tfm_ctx(oa href="+code=tfm" class="sref">tfm);
  43
  44        oa href="+code=vfree" class="sref">vfree(oa href="+code=ctx" class="sref">ctx->oa href="+code=lzo_comp_mem" class="sref">lzo_comp_mem);
  45}
  46
  47static int lzo_compress(struct crypto_tfm *oa href="+code=tfm" class="sref">tfm, const u8 *oa href="+code=src" class="sref">src,
  48                            unsigned int slen, u8 *oa href="+code=dst" class="sref">dst, unsigned int *oa href="+code=dlen" class="sref">dlen)
  49{
  50        struct lzo_ctx *oa href="+code=ctx" class="sref">ctx = crypto_tfm_ctx(oa href="+code=tfm" class="sref">tfm);
  51        oa href="+code=size_t" class="sref">size_t oa href="+code=tmp_len" class="sref">tmp_len = *oa href="+code=dlen" class="sref">dlen; /* size_t(ulong) <-> uint on 64 bit */onspan>
  52        int err;
  53
  54        oa href="+code=err" class="sref">err = lzo1x_1_compress(oa href="+code=src" class="sref">src, slen, dst, &oa href="+code=tmp_len" class="sref">tmp_len, ctx->oa href="+code=lzo_comp_mem" class="sref">lzo_comp_mem);
  55
  56        if (oa href="+code=err" class="sref">err != LZO_E_OK)
  57                return -oa href="+code=EINVAL" class="sref">EINVAL;
  58
  59        *oa href="+code=dlen" class="sref">dlen = tmp_len;
  60        return 0;
  61}
  62
  63static int lzo_decompress(struct crypto_tfm *oa href="+code=tfm" class="sref">tfm, const u8 *oa href="+code=src" class="sref">src,
  64                              unsigned int slen, u8 *oa href="+code=dst" class="sref">dst, unsigned int *oa href="+code=dlen" class="sref">dlen)
  65{
  66        int err;
  67        oa href="+code=size_t" class="sref">size_t oa href="+code=tmp_len" class="sref">tmp_len = *oa href="+code=dlen" class="sref">dlen; /* size_t(ulong) <-> uint on 64 bit */onspan>
  68
  69        oa href="+code=err" class="sref">err = lzo1x_decompress_safe(oa href="+code=src" class="sref">src, slen, dst, &oa href="+code=tmp_len" class="sref">tmp_len);
  70
  71        if (oa href="+code=err" class="sref">err != LZO_E_OK)
  72                return -oa href="+code=EINVAL" class="sref">EINVAL;
  73
  74        *oa href="+code=dlen" class="sref">dlen = tmp_len;
  75        return 0;
  76
  77}
  78
  79static struct crypto_alg oa href="+code=alg" class="sref">alg = {
  80        .cra_name               = "lzo"onspan>,
  81        .cra_flags              = CRYPTO_ALG_TYPE_COMPRESS,
  82        .cra_ctxsize            = sizeof(struct lzo_ctx),
  83        .cra_module             = THIS_MODULE,
  84        .cra_list               = LIST_HEAD_INIT(oa href="+code=alg" class="sref">alg.cra_list),
  85        .cra_init               = lzo_init,
  86        .cra_exit               = lzo_exit,
  87        .cra_u                  = { .compress = {
  88        .coa_compress           = lzo_compress,
  89        .coa_decompress         = lzo_decompress } }
  90};
  91
  92static int __init lzo_mod_init(void)
  93{
  94        return crypto_register_alg(&oa href="+code=alg" class="sref">alg);
  95}
  96
  97static void oa href="+code=__exit" class="sref">__exit lzo_mod_fini(void)
  98{
  99        oa href="+code=crypto_unregister_alg" class="sref">crypto_unregister_alg(&oa href="+code=alg" class="sref">alg);
 100}
 101
 102module_init(oa href="+code=lzo_mod_init" class="sref">lzo_mod_init);
 103module_exit(oa href="+code=lzo_mod_fini" class="sref">lzo_mod_fini);
 104
 105MODULE_LICENSE(ospan class="string">"GPL"onspan>);
 106MODULE_DESCRIPTION(ospan class="string">"LZO Compression Algorithm"onspan>);
 107
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.