libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

h2_huffman_codec.h (4728B)


      1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
      2 /*
      3   This file is part of GNU libmicrohttpd.
      4   Copyright (C) 2025 Evgeny Grin (Karlson2k)
      5 
      6   GNU libmicrohttpd is free software; you can redistribute it and/or
      7   modify it under the terms of the GNU Lesser General Public
      8   License as published by the Free Software Foundation; either
      9   version 2.1 of the License, or (at your option) any later version.
     10 
     11   GNU libmicrohttpd is distributed in the hope that it will be useful,
     12   but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14   Lesser General Public License for more details.
     15 
     16   Alternatively, you can redistribute GNU libmicrohttpd and/or
     17   modify it under the terms of the GNU General Public License as
     18   published by the Free Software Foundation; either version 2 of
     19   the License, or (at your option) any later version, together
     20   with the eCos exception, as follows:
     21 
     22     As a special exception, if other files instantiate templates or
     23     use macros or inline functions from this file, or you compile this
     24     file and link it with other works to produce a work based on this
     25     file, this file does not by itself cause the resulting work to be
     26     covered by the GNU General Public License. However the source code
     27     for this file must still be made available in accordance with
     28     section (3) of the GNU General Public License v2.
     29 
     30     This exception does not invalidate any other reasons why a work
     31     based on this file might be covered by the GNU General Public
     32     License.
     33 
     34   You should have received copies of the GNU Lesser General Public
     35   License and the GNU General Public License along with this library;
     36   if not, see <https://www.gnu.org/licenses/>.
     37 */
     38 
     39 /**
     40  * @file src/mhd2/h2/hpack/h2_huffman_codec.h
     41  * @brief  The declaration for HTTP/2 Huffman encoding and decoding function
     42  * @author Karlson2k (Evgeny Grin)
     43  */
     44 
     45 #ifndef MHD_H2_HUFFMAN_CODEC_H
     46 #define MHD_H2_HUFFMAN_CODEC_H 1
     47 
     48 #include "mhd_sys_options.h"
     49 
     50 #include "sys_sizet_type.h"
     51 
     52 /**
     53  * Perform one-time initialisation of H2 static Huffman decoding tables
     54  *
     55  * Must be always be called once before calling #mhd_h2_huffman_decode().
     56  */
     57 MHD_INTERNAL void
     58 mhd_h2_huffman_init (void);
     59 
     60 
     61 /**
     62  * Perform H2 static Huffman encoding
     63  * @param str_len the size of the @a str, must not be zero
     64  * @param str the pointer to the data to encoded,
     65  *            does not need to be zero-terminated
     66  * @param out_buf_size the size of the @a out_buf; when used inside MHD
     67  *                     must not be larger than @a str_len
     68  * @param out_buf the output buffer to put the encoded data. May be altered
     69  *            even if encoding  failed.
     70  * @return non-zero size of the encoded data placed to the @a out_buf on
     71  *         success,
     72  *         zero if output buffer is not large enough,
     73  */
     74 MHD_INTERNAL size_t
     75 mhd_h2_huffman_encode (size_t str_len,
     76                        const char *restrict str,
     77                        size_t out_buf_size,
     78                        void *restrict out_buf)
     79 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2,1) MHD_FN_PAR_OUT_SIZE_ (4,3);
     80 
     81 
     82 /**
     83  * Result of H2 static Huffman decoding
     84  */
     85 enum MHD_FIXED_ENUM_ mhd_H2HuffDecodeRes
     86 {
     87   /**
     88    * Decoded successfully
     89    */
     90   MHD_H2_HUFF_DEC_RES_OK = 0
     91   ,
     92   /**
     93    * The output buffer is too small
     94    */
     95   MHD_H2_HUFF_DEC_RES_NO_SPACE
     96   ,
     97   /**
     98    * Encoded data is invalid (malformed or wrong padding)
     99    */
    100   MHD_H2_HUFF_DEC_RES_BROKEN_DATA
    101 };
    102 
    103 /**
    104  * Perform H2 static Huffman decoding
    105  * @param encoded_size the size of the data pointed by the @a encoded pointer,
    106  *                     must not be zero
    107  * @param encoded the pointer to the data to decode
    108  * @param out_buf_size the size of the @a out_buf
    109  * @param out_buf the output buffer to put the decoded data. The decoded
    110  *                data is NOT zero-terminated. May be altered even if decoding
    111  *                failed.
    112  * @param decode_result the pointer to variable to set to the decoding status,
    113  *                      always set to #MHD_H2_HUFF_DEC_RES_OK if returned
    114  *                      value is not zero.
    115  * @return non-zero size of the decoded data placed to the @a out_buf
    116  *         on success,
    117  *         zero if decoding is failed (see @a decode_result).
    118  */
    119 MHD_INTERNAL size_t
    120 mhd_h2_huffman_decode (size_t encoded_size,
    121                        const void *restrict encoded,
    122                        size_t out_buf_size,
    123                        char *restrict out_buf,
    124                        enum mhd_H2HuffDecodeRes *restrict decode_result)
    125 MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2,1) MHD_FN_PAR_OUT_SIZE_ (4,3);
    126 
    127 
    128 #endif /* ! MHD_H2_HUFFMAN_CODEC_H */