aboutsummaryrefslogtreecommitdiff
path: root/src/section/common.c
blob: 84ed191c3b5d0026e6ae208afa9678992284796a (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
/*
 * nanowasm, a tiny WebAssembly/Wasm interpreter
 * Copyright (C) 2023-2024  Xavier Del Campo Romero
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include <nw/log.h>
#include <nw/sections.h>
#include <nw/types.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

int check_resizable_limits(FILE *const f, struct resizable_limits *const r)
{
    varuint1 flags;
    varuint32 initial;

    *r = (const struct resizable_limits){0};

    if (varuint1_read(f, &flags))
    {
        LOG("%s: varuint1_read failed\n", __func__);
        return -1;
    }
    else if (varuint32_read(f, &initial))
    {
        LOG("%s: varuint32_read failed\n", __func__);
        return -1;
    }

    if (flags)
    {
        varuint32 maximum;

        if (varuint32_read(f, &maximum))
        {
            LOG("%s: varuint32_read failed\n", __func__);
            return -1;
        }

        r->max = maximum;
        r->max_available = true;
    }

    static const unsigned long page_size = 64lu * 1024lu;

    if (initial > UINT32_MAX / page_size)
    {
        LOG("%s: initial size (%lu) overflow \n", __func__,
            (unsigned long)initial);
        return 1;
    }

    r->sz = initial * page_size;
    return 0;
}