nbd: Permit simple error to NBD_CMD_BLOCK_STATUS
[qemu/ericb.git] / slirp / src / stream.c
blob9c1764c0b7e0d6f0b8331c4f48ceff56530b4476
1 /* SPDX-License-Identifier: MIT */
2 /*
3 * libslirp io streams
5 * Copyright (c) 2018 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 #include "stream.h"
26 #include <glib.h>
28 bool slirp_istream_read(SlirpIStream *f, void *buf, size_t size)
30 return f->read_cb(buf, size, f->opaque) == size;
33 bool slirp_ostream_write(SlirpOStream *f, const void *buf, size_t size)
35 return f->write_cb(buf, size, f->opaque) == size;
38 uint8_t slirp_istream_read_u8(SlirpIStream *f)
40 uint8_t b;
42 if (slirp_istream_read(f, &b, sizeof(b))) {
43 return b;
46 return 0;
49 bool slirp_ostream_write_u8(SlirpOStream *f, uint8_t b)
51 return slirp_ostream_write(f, &b, sizeof(b));
54 uint16_t slirp_istream_read_u16(SlirpIStream *f)
56 uint16_t b;
58 if (slirp_istream_read(f, &b, sizeof(b))) {
59 return GUINT16_FROM_BE(b);
62 return 0;
65 bool slirp_ostream_write_u16(SlirpOStream *f, uint16_t b)
67 b = GUINT16_TO_BE(b);
68 return slirp_ostream_write(f, &b, sizeof(b));
71 uint32_t slirp_istream_read_u32(SlirpIStream *f)
73 uint32_t b;
75 if (slirp_istream_read(f, &b, sizeof(b))) {
76 return GUINT32_FROM_BE(b);
79 return 0;
82 bool slirp_ostream_write_u32(SlirpOStream *f, uint32_t b)
84 b = GUINT32_TO_BE(b);
85 return slirp_ostream_write(f, &b, sizeof(b));
88 int16_t slirp_istream_read_i16(SlirpIStream *f)
90 int16_t b;
92 if (slirp_istream_read(f, &b, sizeof(b))) {
93 return GINT16_FROM_BE(b);
96 return 0;
99 bool slirp_ostream_write_i16(SlirpOStream *f, int16_t b)
101 b = GINT16_TO_BE(b);
102 return slirp_ostream_write(f, &b, sizeof(b));
105 int32_t slirp_istream_read_i32(SlirpIStream *f)
107 int32_t b;
109 if (slirp_istream_read(f, &b, sizeof(b))) {
110 return GINT32_FROM_BE(b);
113 return 0;
116 bool slirp_ostream_write_i32(SlirpOStream *f, int32_t b)
118 b = GINT32_TO_BE(b);
119 return slirp_ostream_write(f, &b, sizeof(b));