OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [crypto/] [deflate.c] - Diff between revs 1275 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1275 Rev 1765
/*
/*
 * Cryptographic API.
 * Cryptographic API.
 *
 *
 * Deflate algorithm (RFC 1951), implemented here primarily for use
 * Deflate algorithm (RFC 1951), implemented here primarily for use
 * by IPCOMP (RFC 3173 & RFC 2394).
 * by IPCOMP (RFC 3173 & RFC 2394).
 *
 *
 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
 *
 *
 * This program is free software; you can redistribute it and/or modify it
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * any later version.
 *
 *
 * FIXME: deflate transforms will require up to a total of about 436k of kernel
 * FIXME: deflate transforms will require up to a total of about 436k of kernel
 * memory on i386 (390k for compression, the rest for decompression), as the
 * memory on i386 (390k for compression, the rest for decompression), as the
 * current zlib kernel code uses a worst case pre-allocation system by default.
 * current zlib kernel code uses a worst case pre-allocation system by default.
 * This needs to be fixed so that the amount of memory required is properly
 * This needs to be fixed so that the amount of memory required is properly
 * related to the  winbits and memlevel parameters.
 * related to the  winbits and memlevel parameters.
 *
 *
 * The default winbits of 11 should suit most packets, and it may be something
 * The default winbits of 11 should suit most packets, and it may be something
 * to configure on a per-tfm basis in the future.
 * to configure on a per-tfm basis in the future.
 *
 *
 * Currently, compression history is not maintained between tfm calls, as
 * Currently, compression history is not maintained between tfm calls, as
 * it is not needed for IPCOMP and keeps the code simpler.  It can be
 * it is not needed for IPCOMP and keeps the code simpler.  It can be
 * implemented if someone wants it.
 * implemented if someone wants it.
 */
 */
#include <linux/init.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/crypto.h>
#include <linux/zlib.h>
#include <linux/zlib.h>
#include <linux/vmalloc.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/mm.h>
#include <linux/net.h>
#include <linux/net.h>
#include <linux/slab.h>
#include <linux/slab.h>
 
 
#define DEFLATE_DEF_LEVEL               Z_DEFAULT_COMPRESSION
#define DEFLATE_DEF_LEVEL               Z_DEFAULT_COMPRESSION
#define DEFLATE_DEF_WINBITS             11
#define DEFLATE_DEF_WINBITS             11
#define DEFLATE_DEF_MEMLEVEL            MAX_MEM_LEVEL
#define DEFLATE_DEF_MEMLEVEL            MAX_MEM_LEVEL
 
 
struct deflate_ctx {
struct deflate_ctx {
        int comp_initialized;
        int comp_initialized;
        int decomp_initialized;
        int decomp_initialized;
        struct z_stream_s comp_stream;
        struct z_stream_s comp_stream;
        struct z_stream_s decomp_stream;
        struct z_stream_s decomp_stream;
};
};
 
 
static inline int deflate_gfp(void)
static inline int deflate_gfp(void)
{
{
        return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
        return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
}
}
 
 
static int deflate_init(void *ctx)
static int deflate_init(void *ctx)
{
{
        return 0;
        return 0;
}
}
 
 
static void deflate_exit(void *ctx)
static void deflate_exit(void *ctx)
{
{
        struct deflate_ctx *dctx = ctx;
        struct deflate_ctx *dctx = ctx;
 
 
        if (dctx->comp_initialized)
        if (dctx->comp_initialized)
                vfree(dctx->comp_stream.workspace);
                vfree(dctx->comp_stream.workspace);
        if (dctx->decomp_initialized)
        if (dctx->decomp_initialized)
                kfree(dctx->decomp_stream.workspace);
                kfree(dctx->decomp_stream.workspace);
}
}
 
 
/*
/*
 * Lazy initialization to make interface simple without allocating
 * Lazy initialization to make interface simple without allocating
 * un-needed workspaces.  Thus can be called in softirq context.
 * un-needed workspaces.  Thus can be called in softirq context.
 */
 */
static int deflate_comp_init(struct deflate_ctx *ctx)
static int deflate_comp_init(struct deflate_ctx *ctx)
{
{
        int ret = 0;
        int ret = 0;
        struct z_stream_s *stream = &ctx->comp_stream;
        struct z_stream_s *stream = &ctx->comp_stream;
 
 
        stream->workspace = __vmalloc(zlib_deflate_workspacesize(),
        stream->workspace = __vmalloc(zlib_deflate_workspacesize(),
                                      deflate_gfp()|__GFP_HIGHMEM,
                                      deflate_gfp()|__GFP_HIGHMEM,
                                      PAGE_KERNEL);
                                      PAGE_KERNEL);
        if (!stream->workspace ) {
        if (!stream->workspace ) {
                ret = -ENOMEM;
                ret = -ENOMEM;
                goto out;
                goto out;
        }
        }
        memset(stream->workspace, 0, zlib_deflate_workspacesize());
        memset(stream->workspace, 0, zlib_deflate_workspacesize());
        ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
        ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
                                -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
                                -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
                                Z_DEFAULT_STRATEGY);
                                Z_DEFAULT_STRATEGY);
        if (ret != Z_OK) {
        if (ret != Z_OK) {
                ret = -EINVAL;
                ret = -EINVAL;
                goto out_free;
                goto out_free;
        }
        }
        ctx->comp_initialized = 1;
        ctx->comp_initialized = 1;
out:
out:
        return ret;
        return ret;
out_free:
out_free:
        vfree(stream->workspace);
        vfree(stream->workspace);
        goto out;
        goto out;
}
}
 
 
static int deflate_decomp_init(struct deflate_ctx *ctx)
static int deflate_decomp_init(struct deflate_ctx *ctx)
{
{
        int ret = 0;
        int ret = 0;
        struct z_stream_s *stream = &ctx->decomp_stream;
        struct z_stream_s *stream = &ctx->decomp_stream;
 
 
        stream->workspace = kmalloc(zlib_inflate_workspacesize(),
        stream->workspace = kmalloc(zlib_inflate_workspacesize(),
                                    deflate_gfp());
                                    deflate_gfp());
        if (!stream->workspace ) {
        if (!stream->workspace ) {
                ret = -ENOMEM;
                ret = -ENOMEM;
                goto out;
                goto out;
        }
        }
        memset(stream->workspace, 0, zlib_inflate_workspacesize());
        memset(stream->workspace, 0, zlib_inflate_workspacesize());
        ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
        ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
        if (ret != Z_OK) {
        if (ret != Z_OK) {
                ret = -EINVAL;
                ret = -EINVAL;
                goto out_free;
                goto out_free;
        }
        }
        ctx->decomp_initialized = 1;
        ctx->decomp_initialized = 1;
out:
out:
        return ret;
        return ret;
out_free:
out_free:
        kfree(stream->workspace);
        kfree(stream->workspace);
        goto out;
        goto out;
}
}
 
 
static int deflate_compress(void *ctx, const u8 *src, unsigned int slen,
static int deflate_compress(void *ctx, const u8 *src, unsigned int slen,
                            u8 *dst, unsigned int *dlen)
                            u8 *dst, unsigned int *dlen)
{
{
        int ret = 0;
        int ret = 0;
        struct deflate_ctx *dctx = ctx;
        struct deflate_ctx *dctx = ctx;
        struct z_stream_s *stream = &dctx->comp_stream;
        struct z_stream_s *stream = &dctx->comp_stream;
 
 
        if (!dctx->comp_initialized) {
        if (!dctx->comp_initialized) {
                ret = deflate_comp_init(dctx);
                ret = deflate_comp_init(dctx);
                if (ret)
                if (ret)
                        goto out;
                        goto out;
        }
        }
 
 
        ret = zlib_deflateReset(stream);
        ret = zlib_deflateReset(stream);
        if (ret != Z_OK) {
        if (ret != Z_OK) {
                ret = -EINVAL;
                ret = -EINVAL;
                goto out;
                goto out;
        }
        }
 
 
        stream->next_in = (u8 *)src;
        stream->next_in = (u8 *)src;
        stream->avail_in = slen;
        stream->avail_in = slen;
        stream->next_out = (u8 *)dst;
        stream->next_out = (u8 *)dst;
        stream->avail_out = *dlen;
        stream->avail_out = *dlen;
 
 
        ret = zlib_deflate(stream, Z_FINISH);
        ret = zlib_deflate(stream, Z_FINISH);
        if (ret != Z_STREAM_END) {
        if (ret != Z_STREAM_END) {
                ret = -EINVAL;
                ret = -EINVAL;
                goto out;
                goto out;
        }
        }
        ret = 0;
        ret = 0;
        *dlen = stream->total_out;
        *dlen = stream->total_out;
out:
out:
        return ret;
        return ret;
}
}
 
 
static int deflate_decompress(void *ctx, const u8 *src, unsigned int slen,
static int deflate_decompress(void *ctx, const u8 *src, unsigned int slen,
                              u8 *dst, unsigned int *dlen)
                              u8 *dst, unsigned int *dlen)
{
{
 
 
        int ret = 0;
        int ret = 0;
        struct deflate_ctx *dctx = ctx;
        struct deflate_ctx *dctx = ctx;
        struct z_stream_s *stream = &dctx->decomp_stream;
        struct z_stream_s *stream = &dctx->decomp_stream;
 
 
        if (!dctx->decomp_initialized) {
        if (!dctx->decomp_initialized) {
                ret = deflate_decomp_init(dctx);
                ret = deflate_decomp_init(dctx);
                if (ret)
                if (ret)
                        goto out;
                        goto out;
        }
        }
 
 
        ret = zlib_inflateReset(stream);
        ret = zlib_inflateReset(stream);
        if (ret != Z_OK) {
        if (ret != Z_OK) {
                ret = -EINVAL;
                ret = -EINVAL;
                goto out;
                goto out;
        }
        }
 
 
        stream->next_in = (u8 *)src;
        stream->next_in = (u8 *)src;
        stream->avail_in = slen;
        stream->avail_in = slen;
        stream->next_out = (u8 *)dst;
        stream->next_out = (u8 *)dst;
        stream->avail_out = *dlen;
        stream->avail_out = *dlen;
 
 
        ret = zlib_inflate(stream, Z_SYNC_FLUSH);
        ret = zlib_inflate(stream, Z_SYNC_FLUSH);
        /*
        /*
         * Work around a bug in zlib, which sometimes wants to taste an extra
         * Work around a bug in zlib, which sometimes wants to taste an extra
         * byte when being used in the (undocumented) raw deflate mode.
         * byte when being used in the (undocumented) raw deflate mode.
         * (From USAGI).
         * (From USAGI).
         */
         */
        if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
        if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
                u8 zerostuff = 0;
                u8 zerostuff = 0;
                stream->next_in = &zerostuff;
                stream->next_in = &zerostuff;
                stream->avail_in = 1;
                stream->avail_in = 1;
                ret = zlib_inflate(stream, Z_FINISH);
                ret = zlib_inflate(stream, Z_FINISH);
        }
        }
        if (ret != Z_STREAM_END) {
        if (ret != Z_STREAM_END) {
                ret = -EINVAL;
                ret = -EINVAL;
                goto out;
                goto out;
        }
        }
        ret = 0;
        ret = 0;
        *dlen = stream->total_out;
        *dlen = stream->total_out;
out:
out:
        return ret;
        return ret;
}
}
 
 
static struct crypto_alg alg = {
static struct crypto_alg alg = {
        .cra_name               = "deflate",
        .cra_name               = "deflate",
        .cra_flags              = CRYPTO_ALG_TYPE_COMPRESS,
        .cra_flags              = CRYPTO_ALG_TYPE_COMPRESS,
        .cra_ctxsize            = sizeof(struct deflate_ctx),
        .cra_ctxsize            = sizeof(struct deflate_ctx),
        .cra_module             = THIS_MODULE,
        .cra_module             = THIS_MODULE,
        .cra_list               = LIST_HEAD_INIT(alg.cra_list),
        .cra_list               = LIST_HEAD_INIT(alg.cra_list),
        .cra_u                  = { .compress = {
        .cra_u                  = { .compress = {
        .coa_init               = deflate_init,
        .coa_init               = deflate_init,
        .coa_exit               = deflate_exit,
        .coa_exit               = deflate_exit,
        .coa_compress           = deflate_compress,
        .coa_compress           = deflate_compress,
        .coa_decompress         = deflate_decompress } }
        .coa_decompress         = deflate_decompress } }
};
};
 
 
static int __init init(void)
static int __init init(void)
{
{
        return crypto_register_alg(&alg);
        return crypto_register_alg(&alg);
}
}
 
 
static void __exit fini(void)
static void __exit fini(void)
{
{
        crypto_unregister_alg(&alg);
        crypto_unregister_alg(&alg);
}
}
 
 
module_init(init);
module_init(init);
module_exit(fini);
module_exit(fini);
 
 
MODULE_LICENSE("GPL");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.