| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 13 мар. 2021 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_RESOURCE_BUFFER_H_ |
| 23 |
#define LSP_PLUG_IN_RESOURCE_BUFFER_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
#include <lsp-plug.in/common/status.h> |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
namespace resource |
| 32 |
{ |
| 33 |
/** |
| 34 |
* The maximum number of characters appended to the replay buffer |
| 35 |
* if the character repeat at the tail is used. |
| 36 |
*/ |
| 37 |
static const size_t REPEAT_BUF_MAX = 4; |
| 38 |
|
| 39 |
/** |
| 40 |
* Location in the buffer |
| 41 |
*/ |
| 42 |
typedef struct location_t |
| 43 |
{ |
| 44 |
ssize_t offset; // Offset relative to the buffer |
| 45 |
size_t len; // Length of the block |
| 46 |
size_t repeat; // Number of repeats |
| 47 |
} location_t; |
| 48 |
|
| 49 |
/** |
| 50 |
* Compression buffer |
| 51 |
*/ |
| 52 |
typedef struct cbuffer_t |
| 53 |
{ |
| 54 |
public: |
| 55 |
uint8_t *data; // Buffer data (2 x capacity) |
| 56 |
uint32_t *index; // Index |
| 57 |
uint32_t *root; // Root index |
| 58 |
uint32_t head; // Head of the buffer |
| 59 |
uint32_t length; // Buffer length |
| 60 |
uint32_t cap; // Buffer capacity |
| 61 |
|
| 62 |
public: |
| 63 |
explicit cbuffer_t(); |
| 64 |
~cbuffer_t(); |
| 65 |
|
| 66 |
status_t init(size_t log_capacity); |
| 67 |
void destroy(); |
| 68 |
|
| 69 |
public: |
| 70 |
/** |
| 71 |
* Append buffer to compression buffer |
| 72 |
* @param src buffer to append |
| 73 |
* @param count the length of the buffer to append |
| 74 |
*/ |
| 75 |
void append(const void *src, size_t count); |
| 76 |
|
| 77 |
/** |
| 78 |
* Append single byte to compression buffer |
| 79 |
* @param v byte to append |
| 80 |
*/ |
| 81 |
void append(uint8_t v); |
| 82 |
|
| 83 |
/** |
| 84 |
* Lookup for byte sequence inside of the buffer |
| 85 |
* @param out relative offset of the sub-sequence in the buffer to the last byte stored in the buffer |
| 86 |
* @param src byte sequence to search inside of the buffer |
| 87 |
* @param avail number of bytes available in the sequence |
| 88 |
* @return the length of sub-sequence found in the buffer |
| 89 |
*/ |
| 90 |
size_t lookup(size_t *out, const void *src, size_t avail); |
| 91 |
|
| 92 |
/** |
| 93 |
* Get byte relative to the last written byte |
| 94 |
* @param offset offset relative to the last written byte |
| 95 |
* @return byte |
| 96 |
*/ |
| 97 |
uint8_t byte_at(size_t offset); |
| 98 |
|
| 99 |
/** |
| 100 |
* Cleanup state of the buffer |
| 101 |
*/ |
| 102 |
void clear(); |
| 103 |
|
| 104 |
/** |
| 105 |
* Get size of data currently stored in the buffer |
| 106 |
* @return size of data currently stored in the buffer |
| 107 |
*/ |
| 108 |
inline size_t size() const { return lsp_min(length, cap); } |
| 109 |
|
| 110 |
} cbuffer_t; |
| 111 |
|
| 112 |
/** |
| 113 |
* Decompression buffer |
| 114 |
*/ |
| 115 |
typedef struct dbuffer_t |
| 116 |
{ |
| 117 |
public: |
| 118 |
uint8_t *data; // Buffer data (2 x capacity) |
| 119 |
uint32_t length; // Actual size of buffer |
| 120 |
uint32_t head; // Head of the buffer |
| 121 |
uint32_t cap; // Buffer capacity, power of two |
| 122 |
|
| 123 |
public: |
| 124 |
explicit dbuffer_t(); |
| 125 |
~dbuffer_t(); |
| 126 |
|
| 127 |
status_t init(size_t log_capacity); |
| 128 |
void destroy(); |
| 129 |
|
| 130 |
public: |
| 131 |
/** |
| 132 |
* Extract data from buffer |
| 133 |
* @param dst destination pointer to store result |
| 134 |
* @param offset relative offset of the subsequence in the buffer to the last byte store in the buffer |
| 135 |
* @param count number of bytes to extract |
| 136 |
* @return status of operation (error on buffer underflow) |
| 137 |
*/ |
| 138 |
status_t extract(void *dst, size_t offset, size_t count); |
| 139 |
|
| 140 |
/** |
| 141 |
* Append multiple bytes to the buffer |
| 142 |
* @param src data to append to the buffer |
| 143 |
* @param count number of bytes to append |
| 144 |
*/ |
| 145 |
void append(const void *src, size_t count); |
| 146 |
|
| 147 |
/** |
| 148 |
* Append single byte to the buffer |
| 149 |
* @param v byt to append |
| 150 |
*/ |
| 151 |
void append(uint8_t v); |
| 152 |
|
| 153 |
/** |
| 154 |
* Clear buffer state |
| 155 |
*/ |
| 156 |
void clear(); |
| 157 |
|
| 158 |
/** |
| 159 |
* Get size of data currently stored in the buffer |
| 160 |
* @return size of data currently stored in the buffer |
| 161 |
*/ |
| 162 |
inline size_t size() const { return length; } |
| 163 |
|
| 164 |
} duffer_t; |
| 165 |
|
| 166 |
} /* namespace resource */ |
| 167 |
} /* namespace lsp */ |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
#endif /* LSP_PLUG_IN_RESOURCE_BUFFER_H_ */ |