summaryrefslogtreecommitdiffstats
path: root/src/test/test1.c
blob: d6d18d1c771de85a63313a4d5ffc5442279ff33b (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Author Jerry Lundström <jerry@dns-oarc.net>
 * Copyright (c) 2020, OARC, Inc.
 * All rights reserved.
 *
 * This file is part of the tinyframe library.
 *
 * tinyframe library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tinyframe library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with tinyframe library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <tinyframe/tinyframe.h>

#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

static void
print_string(const void* data, size_t len)
{
    uint8_t* str = (uint8_t*)data;
    putc('"', stdout);
    while (len-- != 0) {
        unsigned c = *(str++);
        if (isprint(c)) {
            if (c == '"')
                puts("\\\"");
            else
                putc(c, stdout);
        } else {
            printf("\\x%02x", c);
        }
    }
    putc('"', stdout);
}

int main(int argc, const char* argv[])
{
    if (argc < 3) {
        return 1;
    }

    FILE* fp = fopen(argv[1], "r");
    if (!fp) {
        return 2;
    }

    int rbuf_len = atoi(argv[2]);

    struct tinyframe_reader h = TINYFRAME_READER_INITIALIZER;

    size_t  s = 0, r;
    uint8_t buf[4096], rbuf[rbuf_len];
    while ((r = fread(rbuf, 1, sizeof(rbuf), fp)) > 0) {
        printf("read %zu\n", r);

        if (s + r > sizeof(buf)) {
            printf("overflow\n");
            break;
        }
        memcpy(&buf[s], rbuf, r);
        s += r;

        int r = 1;
        while (r) {
            switch (tinyframe_read(&h, buf, s)) {
            case tinyframe_have_control:
                printf("control type %" PRIu32 " len %" PRIu32 "\n", h.control.type, h.control.length);
                break;
            case tinyframe_have_control_field:
                printf("control_field type %" PRIu32 " len %" PRIu32 " data: ", h.control_field.type, h.control_field.length);
                print_string(h.control_field.data, h.control_field.length);
                printf("\n");
                break;
            case tinyframe_have_frame:
                printf("frame len %" PRIu32 " data: ", h.frame.length);
                print_string(h.frame.data, h.frame.length);
                printf("\n");
                break;
            case tinyframe_need_more:
                printf("need more\n");
                r = 0;
                break;
            case tinyframe_error:
                printf("error\n");
                fclose(fp);
                return 2;
            case tinyframe_stopped:
                printf("stopped\n");
                fclose(fp);
                return 0;
            case tinyframe_finished:
                printf("finished\n");
                fclose(fp);
                return 0;
            default:
                printf("unexpected return code\n");
                fclose(fp);
                return 3;
            }

            if (r && h.bytes_read && h.bytes_read <= s) {
                s -= h.bytes_read;
                if (s) {
                    memmove(buf, &buf[h.bytes_read], s);
                }
            }
        }
    }

    fclose(fp);
    return 0;
}