aboutsummaryrefslogtreecommitdiff
path: root/model_blob.c
blob: a13d1132c223054743f5abb8110ceb6f325b9a09 (plain) (blame)
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
/* The file contains a header, a fixed-size table of named tensors, and tensor
 * data at 64-byte boundaries. */

#include "model_blob.h"

#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#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);
}