| 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: 24 окт. 2022 г. |
| 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 |
#include <lsp-plug.in/common/alloc.h> |
| 23 |
#include <lsp-plug.in/common/endian.h> |
| 24 |
#include <lsp-plug.in/fmt/lspc/ChunkReader.h> |
| 25 |
#include <lsp-plug.in/fmt/lspc/ChunkWriter.h> |
| 26 |
#include <lsp-plug.in/fmt/lspc/util/path.h> |
| 27 |
#include <lsp-plug.in/stdlib/stdlib.h> |
| 28 |
#include <lsp-plug.in/stdlib/string.h> |
| 29 |
|
| 30 |
namespace lsp |
| 31 |
{ |
| 32 |
namespace lspc |
| 33 |
{ |
| 34 |
//--------------------------------------------------------------------- |
| 35 |
// Allocation/deallocation |
| 36 |
LSP_RUNTIME_LIB_PUBLIC |
| 37 |
path_entry_t *alloc_path(size_t path_len) |
| 38 |
{ |
| 39 |
// Allocate entry |
| 40 |
size_t hdr_size = align_size(sizeof(path_entry_t), sizeof(umword_t)); |
| 41 |
size_t path_size = align_size(path_len + 1, sizeof(umword_t)); |
| 42 |
|
| 43 |
uint8_t *bytes = static_cast<uint8_t *>(malloc(hdr_size + path_size)); |
| 44 |
if (bytes == NULL) |
| 45 |
return NULL; |
| 46 |
|
| 47 |
path_entry_t *res = reinterpret_cast<path_entry_t *>(bytes); |
| 48 |
char *str = reinterpret_cast<char *>(&bytes[hdr_size]); |
| 49 |
|
| 50 |
// Fill entry with data |
| 51 |
res->path = str; |
| 52 |
res->flags = 0; |
| 53 |
res->chunk_id = 0; |
| 54 |
|
| 55 |
return res; |
| 56 |
} |
| 57 |
|
| 58 |
LSP_RUNTIME_LIB_PUBLIC |
| 59 |
path_entry_t *alloc_path(const char *path, size_t flags, chunk_id_t reference_id) |
| 60 |
{ |
| 61 |
if (path == NULL) |
| 62 |
return NULL; |
| 63 |
|
| 64 |
// Allocate entry |
| 65 |
size_t path_len = strlen(path) + 1; |
| 66 |
path_entry_t *res = alloc_path(path_len); |
| 67 |
if (res == NULL) |
| 68 |
return NULL; |
| 69 |
|
| 70 |
// Fill entry with data |
| 71 |
res->flags = uint32_t(flags); |
| 72 |
res->chunk_id = reference_id; |
| 73 |
memcpy(res->path, path, path_len); |
| 74 |
|
| 75 |
return res; |
| 76 |
} |
| 77 |
|
| 78 |
LSP_RUNTIME_LIB_PUBLIC |
| 79 |
path_entry_t *alloc_path(const io::Path *path, size_t flags, chunk_id_t reference_id) |
| 80 |
{ |
| 81 |
if (path == NULL) |
| 82 |
return NULL; |
| 83 |
return alloc_path(path->as_utf8(), flags, reference_id); |
| 84 |
} |
| 85 |
|
| 86 |
LSP_RUNTIME_LIB_PUBLIC |
| 87 |
path_entry_t *alloc_path(const LSPString *path, size_t flags, chunk_id_t reference_id) |
| 88 |
{ |
| 89 |
if (path == NULL) |
| 90 |
return NULL; |
| 91 |
return alloc_path(path->get_utf8(), flags, reference_id); |
| 92 |
} |
| 93 |
|
| 94 |
LSP_RUNTIME_LIB_PUBLIC |
| 95 |
void free_path(path_entry_t * path) |
| 96 |
{ |
| 97 |
if (path != NULL) |
| 98 |
free(path); |
| 99 |
} |
| 100 |
|
| 101 |
//--------------------------------------------------------------------- |
| 102 |
// Write operations |
| 103 |
LSP_RUNTIME_LIB_PUBLIC |
| 104 |
status_t write_path(chunk_id_t *chunk_id, File *file, const path_entry_t *path) |
| 105 |
{ |
| 106 |
if (path == NULL) |
| 107 |
return STATUS_BAD_ARGUMENTS; |
| 108 |
|
| 109 |
// Create chunk and remember it's size |
| 110 |
status_t res; |
| 111 |
lspc::ChunkWriter *cw = file->write_chunk(LSPC_CHUNK_PATH); |
| 112 |
if (cw == NULL) |
| 113 |
return STATUS_BAD_STATE; |
| 114 |
lsp_finally { delete cw; }; |
| 115 |
chunk_id_t res_chunk_id = cw->unique_id(); |
| 116 |
|
| 117 |
// Create header |
| 118 |
size_t path_size = strlen(path->path); |
| 119 |
if (path_size > UINT16_MAX) |
| 120 |
return STATUS_OVERFLOW; |
| 121 |
|
| 122 |
chunk_path_t cp; |
| 123 |
bzero(&cp, sizeof(cp)); |
| 124 |
|
| 125 |
cp.common.size = sizeof(cp); |
| 126 |
cp.common.version = 0; |
| 127 |
cp.path_size = path_size; |
| 128 |
cp.flags = path->flags; |
| 129 |
cp.chunk_id = path->chunk_id; |
| 130 |
|
| 131 |
// Convert endianess |
| 132 |
cp.path_size = CPU_TO_BE(cp.path_size); |
| 133 |
cp.flags = CPU_TO_BE(cp.flags); |
| 134 |
cp.chunk_id = CPU_TO_BE(cp.chunk_id); |
| 135 |
|
| 136 |
if ((res = cw->write_header(&cp)) != STATUS_OK) |
| 137 |
return res; |
| 138 |
if ((res = cw->write(path->path, path_size)) != STATUS_OK) |
| 139 |
return res; |
| 140 |
if ((res = cw->close()) != STATUS_OK) |
| 141 |
return res; |
| 142 |
|
| 143 |
// Return result |
| 144 |
if (chunk_id != NULL) |
| 145 |
*chunk_id = res_chunk_id; |
| 146 |
|
| 147 |
return STATUS_OK; |
| 148 |
} |
| 149 |
|
| 150 |
LSP_RUNTIME_LIB_PUBLIC |
| 151 |
status_t write_path(chunk_id_t *chunk_id, File *file, const char *path, size_t flags, chunk_id_t reference_id) |
| 152 |
{ |
| 153 |
if (path == NULL) |
| 154 |
return STATUS_BAD_ARGUMENTS; |
| 155 |
|
| 156 |
// Convert path to unix-like path string |
| 157 |
LSPString tmp; |
| 158 |
if (!tmp.set_utf8(path)) |
| 159 |
return STATUS_NO_MEM; |
| 160 |
tmp.replace_all('\\', '/'); |
| 161 |
|
| 162 |
// Write the entry |
| 163 |
path_entry_t pe; |
| 164 |
pe.path = const_cast<char *>(tmp.get_utf8()); |
| 165 |
pe.flags = uint32_t(flags); |
| 166 |
pe.chunk_id = reference_id; |
| 167 |
|
| 168 |
return write_path(chunk_id, file, &pe); |
| 169 |
} |
| 170 |
|
| 171 |
LSP_RUNTIME_LIB_PUBLIC |
| 172 |
status_t write_path(chunk_id_t *chunk_id, File *file, const LSPString *path, size_t flags, chunk_id_t reference_id) |
| 173 |
{ |
| 174 |
if (path == NULL) |
| 175 |
return STATUS_BAD_ARGUMENTS; |
| 176 |
|
| 177 |
// Convert path to unix-like path string |
| 178 |
LSPString tmp; |
| 179 |
if (!tmp.set(path)) |
| 180 |
return STATUS_NO_MEM; |
| 181 |
tmp.replace_all('\\', '/'); |
| 182 |
|
| 183 |
// Write the entry |
| 184 |
path_entry_t pe; |
| 185 |
pe.path = const_cast<char *>(tmp.get_utf8()); |
| 186 |
pe.flags = uint32_t(flags); |
| 187 |
pe.chunk_id = reference_id; |
| 188 |
|
| 189 |
return write_path(chunk_id, file, &pe); |
| 190 |
} |
| 191 |
|
| 192 |
LSP_RUNTIME_LIB_PUBLIC |
| 193 |
status_t write_path(chunk_id_t *chunk_id, File *file, const io::Path *path, size_t flags, chunk_id_t reference_id) |
| 194 |
{ |
| 195 |
if (path == NULL) |
| 196 |
return STATUS_BAD_ARGUMENTS; |
| 197 |
|
| 198 |
// Convert path to unix-like path string |
| 199 |
LSPString tmp; |
| 200 |
status_t res = path->get(&tmp); |
| 201 |
if (res != STATUS_OK) |
| 202 |
return res; |
| 203 |
tmp.replace_all('\\', '/'); |
| 204 |
|
| 205 |
// Write the entry |
| 206 |
path_entry_t pe; |
| 207 |
pe.path = const_cast<char *>(tmp.get_utf8()); |
| 208 |
pe.flags = uint32_t(flags); |
| 209 |
pe.chunk_id = reference_id; |
| 210 |
|
| 211 |
return write_path(chunk_id, file, &pe); |
| 212 |
} |
| 213 |
|
| 214 |
//--------------------------------------------------------------------- |
| 215 |
// Read operations |
| 216 |
LSP_RUNTIME_LIB_PUBLIC |
| 217 |
status_t read_path(chunk_id_t chunk_id, File *file, path_entry_t **path) |
| 218 |
{ |
| 219 |
// Get the chunk |
| 220 |
if (file == NULL) |
| 221 |
return STATUS_BAD_ARGUMENTS; |
| 222 |
ChunkReader *rd = file->read_chunk(chunk_id, LSPC_CHUNK_PATH); |
| 223 |
if (rd == NULL) |
| 224 |
return STATUS_NOT_FOUND; |
| 225 |
lsp_finally { delete rd; }; |
| 226 |
|
| 227 |
// Read the header |
| 228 |
chunk_path_t hdr; |
| 229 |
ssize_t read = rd->read_header(&hdr, sizeof(hdr)); |
| 230 |
if (read < 0) |
| 231 |
return status_t(-read); |
| 232 |
else if (read != sizeof(hdr)) |
| 233 |
return STATUS_CORRUPTED; |
| 234 |
else if (hdr.common.version != 0) |
| 235 |
return STATUS_NOT_SUPPORTED; |
| 236 |
|
| 237 |
// Nothing to return? |
| 238 |
if (path == NULL) |
| 239 |
return STATUS_OK; |
| 240 |
|
| 241 |
// Convert the header |
| 242 |
hdr.path_size = BE_TO_CPU(hdr.path_size); |
| 243 |
hdr.flags = BE_TO_CPU(hdr.flags); |
| 244 |
hdr.chunk_id = BE_TO_CPU(hdr.chunk_id); |
| 245 |
|
| 246 |
// Allocate path entry |
| 247 |
path_entry_t *pe= alloc_path(hdr.path_size); |
| 248 |
if (pe == NULL) |
| 249 |
return STATUS_NO_MEM; |
| 250 |
lsp_finally { |
| 251 |
if (pe != NULL) |
| 252 |
free_path(pe); |
| 253 |
}; |
| 254 |
|
| 255 |
pe->flags = hdr.flags; |
| 256 |
pe->chunk_id = hdr.chunk_id; |
| 257 |
|
| 258 |
// Read the path string |
| 259 |
read = rd->read(pe->path, hdr.path_size); |
| 260 |
if (read < 0) |
| 261 |
return status_t(-read); |
| 262 |
else if (read != hdr.path_size) |
| 263 |
return STATUS_CORRUPTED; |
| 264 |
pe->path[hdr.path_size] = '\0'; // Do not forget the trailing zero |
| 265 |
|
| 266 |
// Close the reader |
| 267 |
status_t res = rd->close(); |
| 268 |
if (res != STATUS_OK) |
| 269 |
return res; |
| 270 |
|
| 271 |
// Return the result |
| 272 |
*path = pe; |
| 273 |
pe = NULL; |
| 274 |
|
| 275 |
return STATUS_OK; |
| 276 |
} |
| 277 |
|
| 278 |
LSP_RUNTIME_LIB_PUBLIC |
| 279 |
status_t read_path(chunk_id_t chunk_id, File *file, char **path, size_t *flags, chunk_id_t *reference_id) |
| 280 |
{ |
| 281 |
// Read path entry |
| 282 |
path_entry_t *entry = NULL; |
| 283 |
status_t res = read_path(chunk_id, file, &entry); |
| 284 |
if (res != STATUS_OK) |
| 285 |
return res; |
| 286 |
else if (entry == NULL) |
| 287 |
return STATUS_NO_MEM; |
| 288 |
lsp_finally { free_path(entry); }; |
| 289 |
|
| 290 |
// Pass the arguments to the caller |
| 291 |
if (path != NULL) |
| 292 |
{ |
| 293 |
char *p = strdup(entry->path); |
| 294 |
if (p == NULL) |
| 295 |
return STATUS_NO_MEM; |
| 296 |
*path = p; |
| 297 |
} |
| 298 |
if (flags != NULL) |
| 299 |
*flags = entry->flags; |
| 300 |
if (reference_id != NULL) |
| 301 |
*reference_id = entry->chunk_id; |
| 302 |
|
| 303 |
return STATUS_OK; |
| 304 |
} |
| 305 |
|
| 306 |
LSP_RUNTIME_LIB_PUBLIC |
| 307 |
status_t read_path(chunk_id_t chunk_id, File *file, io::Path *path, size_t *flags, chunk_id_t *reference_id) |
| 308 |
{ |
| 309 |
// Read path entry |
| 310 |
path_entry_t *entry = NULL; |
| 311 |
status_t res = read_path(chunk_id, file, &entry); |
| 312 |
if (res != STATUS_OK) |
| 313 |
return res; |
| 314 |
else if (entry == NULL) |
| 315 |
return STATUS_NO_MEM; |
| 316 |
lsp_finally { free_path(entry); }; |
| 317 |
|
| 318 |
// Pass the arguments to the caller |
| 319 |
if (path != NULL) |
| 320 |
{ |
| 321 |
if ((res = path->set(entry->path)) != STATUS_OK) |
| 322 |
return res; |
| 323 |
} |
| 324 |
if (flags != NULL) |
| 325 |
*flags = entry->flags; |
| 326 |
if (reference_id != NULL) |
| 327 |
*reference_id = entry->chunk_id; |
| 328 |
|
| 329 |
return STATUS_OK; |
| 330 |
} |
| 331 |
|
| 332 |
status_t read_path(chunk_id_t chunk_id, File *file, LSPString *path, size_t *flags, chunk_id_t *reference_id) |
| 333 |
{ |
| 334 |
// Read path entry |
| 335 |
path_entry_t *entry = NULL; |
| 336 |
status_t res = read_path(chunk_id, file, &entry); |
| 337 |
if (res != STATUS_OK) |
| 338 |
return res; |
| 339 |
else if (entry == NULL) |
| 340 |
return STATUS_NO_MEM; |
| 341 |
lsp_finally { free_path(entry); }; |
| 342 |
|
| 343 |
// Pass the arguments to the caller |
| 344 |
if (path != NULL) |
| 345 |
{ |
| 346 |
if (!path->set_utf8(entry->path)) |
| 347 |
return STATUS_NO_MEM; |
| 348 |
} |
| 349 |
if (flags != NULL) |
| 350 |
*flags = entry->flags; |
| 351 |
if (reference_id != NULL) |
| 352 |
*reference_id = entry->chunk_id; |
| 353 |
|
| 354 |
return STATUS_OK; |
| 355 |
} |
| 356 |
} /* namespace lspc */ |
| 357 |
} /* namespace lsp */ |
| 358 |
|