Phase 15: host trusted LADSPA processors with persisted graph state

This commit is contained in:
Johannes Findeisen 2026-10-27 04:00:27 +01:00
commit f72314b5a3
24 changed files with 1046 additions and 70 deletions

View file

@ -125,6 +125,19 @@ static int decode_name(const char *text, char *name)
return xtc_project_valid_name(name);
}
/* Binary-safe nonempty hexadecimal strings, excluding embedded NUL. */
static int decode_bytes(const char *text, char *out, size_t capacity)
{
size_t n = strlen(text), i;
if (!n || n % 2 || n / 2 >= capacity) return 0;
for (i = 0; i < n/2; ++i) {
int hi = hex_digit(text[2*i]), lo = hex_digit(text[2*i+1]);
if (hi < 0 || lo < 0 || (!hi && !lo)) return 0;
out[i] = (char)(hi*16+lo);
}
out[n/2] = 0; return 1;
}
/**
* @brief Decodes eight hexadecimal IEEE-754 bits into a finite float.
*
@ -178,7 +191,7 @@ static xtc_result read_project(reader *input, xtc_project *project)
xtc_graph *graph = project->graph;
READ("XTC_PROJECT", 2U);
NUM(1U, version);
if (version < 1U || version > 4U)
if (version < 1U || version > 5U)
return XTC_ERR_UNSUPPORTED;
format_version = version;
READ("APP", 4U);
@ -212,9 +225,11 @@ static xtc_result read_project(reader *input, xtc_project *project)
if (value >= next_id || value >= SIZE_MAX || version < XTC_NODE_GAIN ||
version > (format_version == 1U ? XTC_NODE_OUTPUT
: format_version == 2U ? XTC_NODE_AUDIO
: XTC_NODE_INSTRUMENT) ||
: format_version < 5U ? XTC_NODE_INSTRUMENT
: XTC_NODE_PLUGIN) ||
!decode_float(input->tokens[3], &node.value))
return XTC_ERR_FORMAT;
if (version == XTC_NODE_NATIVE) return XTC_ERR_UNSUPPORTED;
node.id = (size_t)value;
node.type = (xtc_node_type)version;
if (node.type == XTC_NODE_OSCILLATOR && node.value < 0.0f)
@ -223,6 +238,25 @@ static xtc_result read_project(reader *input, xtc_project *project)
node.type == XTC_NODE_INSTRUMENT) &&
node.value != 0.0f)
return XTC_ERR_FORMAT;
if (node.type == XTC_NODE_PLUGIN) {
xtc_plugin_spec *p = &node.plugin;
size_t k;
if (node.value != 0) return XTC_ERR_FORMAT;
READ("PLUGIN",6U);
NUM(1U,value); if (value > UINT32_MAX) return XTC_ERR_FORMAT; p->unique_id = (unsigned int)value;
NUM(2U,value); if (value >= 1024) return XTC_ERR_FORMAT; p->index = (unsigned int)value;
NUM(3U,value); if (value > 1) return XTC_ERR_FORMAT; p->inputs = (unsigned int)value;
NUM(4U,value); if (value > XTC_PLUGIN_CONTROLS) return XTC_ERR_FORMAT; p->control_count = (unsigned int)value;
if (!decode_bytes(input->tokens[5],p->label,sizeof(p->label))) return XTC_ERR_FORMAT;
READ("MODULE",2U);
if (!decode_bytes(input->tokens[1],p->path,sizeof(p->path))) return XTC_ERR_FORMAT;
for (k = 0; k < p->control_count; ++k) {
READ("CONTROL",3U); NUM(1U,value);
if (value >= 64 || !decode_float(input->tokens[2],p->values+k)) return XTC_ERR_FORMAT;
p->ports[k] = (unsigned int)value;
}
if (xtc_plugin_spec_validate(p) != XTC_OK) return XTC_ERR_FORMAT;
}
for (j = 0U; j < i; ++j)
if (graph->nodes[j].id == node.id)
return XTC_ERR_FORMAT;
@ -389,7 +423,7 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
return XTC_ERR_UNSUPPORTED;
if (sizeof(float) != sizeof(uint32_t) || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128)
return XTC_ERR_UNSUPPORTED;
if (fprintf(file, "XTC_PROJECT 4\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
if (fprintf(file, "XTC_PROJECT 5\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
XTC_VERSION_MAJOR, XTC_VERSION_MINOR, XTC_VERSION_PATCH, project->tempo, project->next_track_id,
project->next_clip_id, graph->require_output_nodes, graph->next_id, graph->node_count,
graph->edge_count) < 0)
@ -400,6 +434,17 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
if (fprintf(file, "NODE %zu %u %08" PRIx32 "\n", graph->nodes[i].id, (unsigned int)graph->nodes[i].type, bits) <
0)
return XTC_ERR_IO;
if (graph->nodes[i].type == XTC_NODE_PLUGIN) {
const xtc_plugin_spec *p = &graph->nodes[i].plugin;
size_t k;
if (xtc_plugin_spec_validate(p) != XTC_OK) return XTC_ERR_FORMAT;
if (fprintf(file,"PLUGIN %u %u %u %u ",p->unique_id,p->index,p->inputs,p->control_count) < 0 ||
!write_name(file,p->label) || fputs("MODULE ",file) == EOF || !write_name(file,p->path)) return XTC_ERR_IO;
for (k = 0; k < p->control_count; ++k) {
uint32_t value; memcpy(&value,p->values+k,4);
if (fprintf(file,"CONTROL %u %08" PRIx32 "\n",p->ports[k],value) < 0) return XTC_ERR_IO;
}
}
}
for (i = 0U; i < graph->edge_count; ++i)
if (fprintf(file, "EDGE %zu %zu\n", graph->nodes[graph->edges[i].source].id,