Annotation of embedaddon/strongswan/src/libstrongswan/plugins/bliss/bliss_huffman_coder.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2014 Andreas Steffen
        !             3:  * HSR Hochschule fuer Technik Rapperswil
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY;https://www.hsr.ch/HSR-intern-Anmeldung.4409.0.html?&no_cache=1 without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: #include "bliss_huffman_coder.h"
        !            17: 
        !            18: typedef struct private_bliss_huffman_coder_t private_bliss_huffman_coder_t;
        !            19: 
        !            20: /**
        !            21:  * Private data structure for bliss_huffman_coder_t object
        !            22:  */
        !            23: struct private_bliss_huffman_coder_t {
        !            24:        /**
        !            25:         * Public interface.
        !            26:         */
        !            27:        bliss_huffman_coder_t public;
        !            28: 
        !            29:        /**
        !            30:         * Bitpacker to write to or read from
        !            31:         */
        !            32:        bliss_bitpacker_t *packer;
        !            33: 
        !            34:        /**
        !            35:         * Huffman code table to be used
        !            36:         */
        !            37:        bliss_huffman_code_t *code;
        !            38: 
        !            39:        /**
        !            40:      * Maximum index into tuples table
        !            41:         */
        !            42:        int index_max;
        !            43: 
        !            44:        /**
        !            45:      * Number of encoded or decoded bits
        !            46:         */
        !            47:        size_t bits;
        !            48: 
        !            49: };
        !            50: 
        !            51: METHOD(bliss_huffman_coder_t, get_bits, size_t,
        !            52:        private_bliss_huffman_coder_t *this)
        !            53: {
        !            54:        return this->bits;
        !            55: }
        !            56: 
        !            57: METHOD(bliss_huffman_coder_t, encode, bool,
        !            58:        private_bliss_huffman_coder_t *this, int32_t z1, int16_t z2)
        !            59: {
        !            60:        uint32_t code;
        !            61:        uint16_t bits;
        !            62:        int index;
        !            63: 
        !            64:        index = z1 * (2*this->code->n_z2 - 1) + z2 + this->code->n_z2 - 1;
        !            65:        if (index >= this->index_max)
        !            66:        {
        !            67:                DBG1(DBG_LIB, "index exceeded in Huffman encoding table");
        !            68:                return FALSE;
        !            69:        }
        !            70:        code = this->code->tuples[index].code;
        !            71:        bits = this->code->tuples[index].bits;
        !            72:        if (!this->packer->write_bits(this->packer, code, bits))
        !            73:        {
        !            74:                DBG1(DBG_LIB, "bitpacker exceeded its buffer");
        !            75:                return FALSE;
        !            76:        }
        !            77:        this->bits += bits;
        !            78: 
        !            79:        return TRUE;
        !            80: }
        !            81: 
        !            82: METHOD(bliss_huffman_coder_t, decode, bool,
        !            83:        private_bliss_huffman_coder_t *this, int32_t *z1, int16_t *z2)
        !            84: {
        !            85:        bliss_huffman_code_node_t *node;
        !            86:        uint32_t bit;
        !            87: 
        !            88:        node = this->code->nodes;
        !            89:        while (node->tuple == BLISS_HUFFMAN_CODE_NO_TUPLE)
        !            90:        {
        !            91:                if (node->node_0 == BLISS_HUFFMAN_CODE_NO_NODE ||
        !            92:                        node->node_1 == BLISS_HUFFMAN_CODE_NO_NODE)
        !            93:                {
        !            94:                        DBG1(DBG_LIB, "error in Huffman decoding table");
        !            95:                        return FALSE;
        !            96:                }
        !            97:                if (!this->packer->read_bits(this->packer, &bit, 1))
        !            98:                {
        !            99:                        DBG1(DBG_LIB, "bitpacker depleted its buffer");
        !           100:                        return FALSE;
        !           101:                }
        !           102:                node = &this->code->nodes[bit ? node->node_1 : node->node_0];
        !           103:                this->bits++;
        !           104:        }
        !           105:        *z1 = node->tuple / (2*this->code->n_z2 - 1);
        !           106:        *z2 = node->tuple - (2*this->code->n_z2 - 1) * (*z1) - this->code->n_z2 + 1;
        !           107: 
        !           108:        return TRUE;
        !           109: }
        !           110: 
        !           111: METHOD(bliss_huffman_coder_t, destroy, void,
        !           112:        private_bliss_huffman_coder_t *this)
        !           113: {
        !           114:        free(this);
        !           115: }
        !           116: 
        !           117: /**
        !           118:  * See header.
        !           119:  */
        !           120: bliss_huffman_coder_t *bliss_huffman_coder_create(bliss_huffman_code_t *code,
        !           121:                                                                                                  bliss_bitpacker_t *packer)
        !           122: {
        !           123:        private_bliss_huffman_coder_t *this;
        !           124: 
        !           125:        INIT(this,
        !           126:                .public = {
        !           127:                        .get_bits = _get_bits,
        !           128:                        .encode = _encode,
        !           129:                        .decode = _decode,
        !           130:                        .destroy = _destroy,
        !           131:                },
        !           132:                .packer = packer,
        !           133:                .code = code,
        !           134:                .index_max = (2*code->n_z2 - 1) * code->n_z1,
        !           135:        );
        !           136: 
        !           137:        return &this->public;
        !           138: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>