1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
* Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2021 Vladimir Sadovnikov <sadko4u@gmail.com>
*
* This file is part of lsp-plugin-fw
* Created on: 7 дек. 2021 г.
*
* lsp-plugin-fw is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* lsp-plugin-fw is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with lsp-plugin-fw. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LSP_PLUG_IN_PLUG_FW_WRAP_VST2_CHUNK_H_
#define LSP_PLUG_IN_PLUG_FW_WRAP_VST2_CHUNK_H_
#include <lsp-plug.in/plug-fw/version.h>
#include <lsp-plug.in/common/endian.h>
#include <lsp-plug.in/common/types.h>
#include <lsp-plug.in/common/status.h>
#include <lsp-plug.in/stdlib/stdlib.h>
#include <lsp-plug.in/stdlib/string.h>
namespace lsp
{
namespace vst2
{
/**
* VST plugin state chunk generator
*/
typedef struct chunk_t
{
uint8_t *data;
size_t offset;
size_t capacity;
status_t res;
explicit chunk_t()
{
data = NULL;
offset = 0;
capacity = 0;
res = STATUS_OK;
}
~chunk_t()
{
if (data != NULL)
{
::free(data);
data = NULL;
}
offset = 0;
capacity = 0;
}
void clear()
{
offset = 0;
res = STATUS_OK;
}
bool ensure_capacity(size_t count)
{
size_t cap = offset + count;
if (cap <= capacity)
return true;
cap += (cap >> 1);
uint8_t *ptr = reinterpret_cast<uint8_t *>(::realloc(data, cap));
if (ptr == NULL)
{
res = STATUS_NO_MEM;
return false;
}
data = ptr;
capacity = cap;
return true;
}
size_t write(const void *bytes, size_t count)
{
if (res != STATUS_OK)
return 0;
if (!ensure_capacity(count))
return 0;
size_t off = offset;
::memcpy(&data[offset], bytes, count);
offset += count;
return off;
}
template <class T>
size_t write(T value)
{
if (res != STATUS_OK)
return 0;
if (!ensure_capacity(sizeof(value)))
return 0;
size_t off = offset;
IF_UNALIGNED_MEMORY_SAFE(
*reinterpret_cast<T *>(&data[off]) = CPU_TO_BE(value);
)
IF_UNALIGNED_MEMORY_UNSAFE(
value = CPU_TO_BE(value);
::memcpy(&data[off], &value, sizeof(value));
)
offset += sizeof(value);
return off;
}
template <class T>
bool write_at(size_t position, T value)
{
if (res != STATUS_OK)
return false;
if ((offset - position) < sizeof(T))
{
res = STATUS_OVERFLOW;
return false;
}
IF_UNALIGNED_MEMORY_SAFE(
*reinterpret_cast<T *>(&data[position]) = CPU_TO_BE(value);
)
IF_UNALIGNED_MEMORY_UNSAFE(
value = CPU_TO_BE(value);
::memcpy(&data[position], &value, sizeof(value));
)
return true;
}
inline size_t write_string(const char *str)
{
if (res != STATUS_OK)
return 0;
size_t slen = ::strlen(str) + 1;
if (!ensure_capacity(slen))
return 0;
size_t off = offset;
::memcpy(&data[offset], str, slen);
offset += slen;
return off;
}
inline size_t write_byte(int b)
{
if (res != STATUS_OK)
return 0;
if (!ensure_capacity(sizeof(uint8_t)))
return 0;
size_t off = offset;
data[offset++] = uint8_t(b);
return off;
}
template <class T>
inline T *fetch(size_t offset)
{
return reinterpret_cast<T *>(&data[offset]);
}
} chunk_t;
} /* namespace vst2 */
} /* namespace lsp */
#endif /* LSP_PLUG_IN_PLUG_FW_WRAP_VST2_CHUNK_H_ */
|