/* The file contains a header, a fixed-size table of named tensors, and tensor * data at 64-byte boundaries. */ #include "model_blob.h" #include #include #include #include #include #include #define MAGIC "SDC1" #define VERSION 1u #define NAME_LEN 24 #define MAX_TENSORS 1024 typedef struct { char magic[4]; uint32_t version; uint32_t count; uint32_t total; } blob_header; typedef struct { char name[NAME_LEN]; uint32_t offset; uint32_t nbytes; uint32_t digest; } blob_entry; static uint32_t fnv1a(const unsigned char *p, size_t n) { uint32_t h = 0x811C9DC5u; for (size_t i = 0; i < n; ++i) h = (h ^ p[i]) * 0x01000193u; return h; } int framesed_model_blob_parse(const void *data, size_t len, int verify, framesed_model_blob *out) { if (!data || len < sizeof(blob_header)) return -1; const blob_header *h = (const blob_header *)data; if (memcmp(h->magic, MAGIC, 4) != 0 || h->version != VERSION) return -1; if (h->count == 0 || h->count > MAX_TENSORS) return -1; const size_t table_bytes = (size_t)h->count * sizeof(blob_entry); if (len < sizeof(blob_header) + table_bytes || len < h->total) return -1; out->base = (const unsigned char *)data; out->len = len; out->table = (const unsigned char *)data + sizeof *h; out->count = h->count; out->verify = verify; out->failed = 0; return 0; } const float *framesed_model_blob_take(framesed_model_blob *blob, const char *name, size_t floats) { if (blob->failed) return NULL; if (floats > SIZE_MAX / sizeof(float)) { blob->failed = 1; return NULL; } const blob_entry *table = (const blob_entry *)blob->table; for (unsigned i = 0; i < blob->count; ++i) { const blob_entry *e = &table[i]; if (strncmp(e->name, name, NAME_LEN) != 0) continue; if (e->nbytes != floats * sizeof(float) || e->offset % 64 != 0 || e->offset > blob->len || e->nbytes > blob->len - e->offset) break; const unsigned char *payload = blob->base + e->offset; if (blob->verify && fnv1a(payload, e->nbytes) != e->digest) break; return (const float *)(const void *)payload; } blob->failed = 1; return NULL; } int framesed_model_blob_map( const char *path, int verify, framesed_model_blob *out, void **map, size_t *map_len) { const int fd = open(path, O_RDONLY); if (fd < 0) return -1; struct stat st; if (fstat(fd, &st) != 0 || st.st_size <= 0) { close(fd); return -1; } const size_t len = (size_t)st.st_size; void *addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); if (addr == MAP_FAILED) return -1; if (framesed_model_blob_parse(addr, len, verify, out) != 0) { munmap(addr, len); return -1; } *map = addr; *map_len = len; return 0; } void framesed_model_blob_unmap(void *map, size_t len) { if (map) munmap(map, len); }