Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1bfe1d41ce92daea089292c7d8210e01c596bd8c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
 * Copyright (c) 2007, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * Abstract byte stream. Bytes in the stream can be divided into groups - messages.
 */

#include <tcf/config.h>
#include <stddef.h>
#include <string.h>
#include <tcf/framework/myalloc.h>
#include <tcf/framework/streams.h>

int (read_stream)(InputStream * inp) {
    return (inp->cur < inp->end) ? *inp->cur++ : inp->read(inp);
}

int (peek_stream)(InputStream * inp) {
    return (inp->cur < inp->end) ? *inp->cur : inp->peek(inp);
}

void (write_stream)(OutputStream * out, int b) {
    if (b > ESC && out->cur < out->end) *out->cur++ = (unsigned char)b;
    else out->write(out, b);
}

void (write_block_stream)(OutputStream * out, const char * bytes, size_t size) {
    out->write_block(out, bytes, size);
}

ssize_t (splice_block_stream)(OutputStream * out, int fd, size_t size, int64_t * offset) {
    return out->splice_block(out, fd, size, offset);
}

void write_string(OutputStream * out, const char * str) {
    while (*str) write_stream(out, (*str++) & 0xff);
}

void write_stringz(OutputStream * out, const char * str) {
    while (*str) write_stream(out, (*str++) & 0xff);
    write_stream(out, 0);
}

static void write_byte_array_output_stream(OutputStream * out, int byte) {
    ByteArrayOutputStream * buf = (ByteArrayOutputStream *)((char *)out - offsetof(ByteArrayOutputStream, out));
    if (buf->pos < sizeof(buf->buf)) {
        buf->buf[buf->pos++] = (char)byte;
    }
    else {
        if (buf->mem == NULL) {
            buf->mem = (char *)loc_alloc(buf->max = buf->pos * 2);
            memcpy(buf->mem, buf->buf, buf->pos);
        }
        else if (buf->pos >= buf->max) {
            buf->mem = (char *)loc_realloc(buf->mem, buf->max *= 2);
        }
        buf->mem[buf->pos++] = (char)byte;
    }
}

static void write_block_byte_array_output_stream(OutputStream * out, const char * bytes, size_t size) {
    size_t pos = 0;
    while (pos < size) write_byte_array_output_stream(out, ((const uint8_t *)bytes)[pos++]);
}

OutputStream * create_byte_array_output_stream(ByteArrayOutputStream * buf) {
    memset(buf, 0, sizeof(ByteArrayOutputStream));
    buf->out.write_block = write_block_byte_array_output_stream;
    buf->out.write = write_byte_array_output_stream;
    return &buf->out;
}

void get_byte_array_output_stream_data(ByteArrayOutputStream * buf, char ** data, size_t * size) {
    if (buf->mem == NULL) {
        buf->max = buf->pos;
        buf->mem = (char *)loc_alloc(buf->max);
        memcpy(buf->mem, buf->buf, buf->pos);
    }
    if (data != NULL) *data = buf->mem;
    if (size != NULL) *size = buf->pos;
    buf->mem = NULL;
    buf->max = 0;
    buf->pos = 0;
}

static int read_byte_array_input_stream(InputStream * inp) {
    ByteArrayInputStream * buf = (ByteArrayInputStream *)((char *)inp - offsetof(ByteArrayInputStream, inp));
    if (buf->pos >= buf->max) return MARKER_EOS;
    return ((unsigned char *)buf->buf)[buf->pos++];
}

static int peek_byte_array_input_stream(InputStream * inp) {
    ByteArrayInputStream * buf = (ByteArrayInputStream *)((char *)inp - offsetof(ByteArrayInputStream, inp));
    if (buf->pos >= buf->max) return MARKER_EOS;
    return ((unsigned char *)buf->buf)[buf->pos];
}

InputStream * create_byte_array_input_stream(ByteArrayInputStream * buf, char * data, size_t size) {
    memset(buf, 0, sizeof(ByteArrayInputStream));
    buf->inp.read = read_byte_array_input_stream;
    buf->inp.peek = peek_byte_array_input_stream;
    buf->buf = data;
    buf->max = size;
    return &buf->inp;
}

static int read_forwarding_input_stream(InputStream * inp) {
    ForwardingInputStream * buf = (ForwardingInputStream *)((char *)inp - offsetof(ForwardingInputStream, fwd));
    int ch = read_stream(buf->inp);
    if (ch != MARKER_EOS) write_stream(buf->out, ch);
    return ch;
}

static int peek_forwarding_input_stream(InputStream * inp) {
    ForwardingInputStream * buf = (ForwardingInputStream *)((char *)inp - offsetof(ForwardingInputStream, fwd));
    return peek_stream(buf->inp);
}

InputStream * create_forwarding_input_stream(ForwardingInputStream * buf, InputStream * inp, OutputStream * out) {
    memset(buf, 0, sizeof(ForwardingInputStream));
    buf->fwd.read = read_forwarding_input_stream;
    buf->fwd.peek = peek_forwarding_input_stream;
    buf->inp = inp;
    buf->out = out;
    return &buf->fwd;
}

Back to the top