tests: test-announce-self: fix memory leak
[qemu/ar7.git] / slirp / src / stream.c
blobd114dde3343e139c3ebba6e17b711d4b5c1ac70e
1 /*
2 * libslirp io streams
4 * Copyright (c) 2018 Red Hat, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "stream.h"
25 #include <glib.h>
27 bool slirp_istream_read(SlirpIStream *f, void *buf, size_t size)
29 return f->read_cb(buf, size, f->opaque) == size;
32 bool slirp_ostream_write(SlirpOStream *f, const void *buf, size_t size)
34 return f->write_cb(buf, size, f->opaque) == size;
37 uint8_t slirp_istream_read_u8(SlirpIStream *f)
39 uint8_t b;
41 if (slirp_istream_read(f, &b, sizeof(b))) {
42 return b;
45 return 0;
48 bool slirp_ostream_write_u8(SlirpOStream *f, uint8_t b)
50 return slirp_ostream_write(f, &b, sizeof(b));
53 uint16_t slirp_istream_read_u16(SlirpIStream *f)
55 uint16_t b;
57 if (slirp_istream_read(f, &b, sizeof(b))) {
58 return GUINT16_FROM_BE(b);
61 return 0;
64 bool slirp_ostream_write_u16(SlirpOStream *f, uint16_t b)
66 b = GUINT16_TO_BE(b);
67 return slirp_ostream_write(f, &b, sizeof(b));
70 uint32_t slirp_istream_read_u32(SlirpIStream *f)
72 uint32_t b;
74 if (slirp_istream_read(f, &b, sizeof(b))) {
75 return GUINT32_FROM_BE(b);
78 return 0;
81 bool slirp_ostream_write_u32(SlirpOStream *f, uint32_t b)
83 b = GUINT32_TO_BE(b);
84 return slirp_ostream_write(f, &b, sizeof(b));
87 int16_t slirp_istream_read_i16(SlirpIStream *f)
89 int16_t b;
91 if (slirp_istream_read(f, &b, sizeof(b))) {
92 return GINT16_FROM_BE(b);
95 return 0;
98 bool slirp_ostream_write_i16(SlirpOStream *f, int16_t b)
100 b = GINT16_TO_BE(b);
101 return slirp_ostream_write(f, &b, sizeof(b));
104 int32_t slirp_istream_read_i32(SlirpIStream *f)
106 int32_t b;
108 if (slirp_istream_read(f, &b, sizeof(b))) {
109 return GINT32_FROM_BE(b);
112 return 0;
115 bool slirp_ostream_write_i32(SlirpOStream *f, int32_t b)
117 b = GINT32_TO_BE(b);
118 return slirp_ostream_write(f, &b, sizeof(b));