1 | /* SPDX-License-Identifier: MIT */
|
---|
2 | /*
|
---|
3 | * libslirp io streams
|
---|
4 | *
|
---|
5 | * Copyright (c) 2018 Red Hat, Inc.
|
---|
6 | *
|
---|
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:
|
---|
13 | *
|
---|
14 | * The above copyright notice and this permission notice shall be included in
|
---|
15 | * all copies or substantial portions of the Software.
|
---|
16 | *
|
---|
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.
|
---|
24 | */
|
---|
25 | #include "stream.h"
|
---|
26 | #include <glib.h>
|
---|
27 |
|
---|
28 | bool slirp_istream_read(SlirpIStream *f, void *buf, size_t size)
|
---|
29 | {
|
---|
30 | return f->read_cb(buf, size, f->opaque) == size;
|
---|
31 | }
|
---|
32 |
|
---|
33 | bool slirp_ostream_write(SlirpOStream *f, const void *buf, size_t size)
|
---|
34 | {
|
---|
35 | return f->write_cb(buf, size, f->opaque) == size;
|
---|
36 | }
|
---|
37 |
|
---|
38 | uint8_t slirp_istream_read_u8(SlirpIStream *f)
|
---|
39 | {
|
---|
40 | uint8_t b;
|
---|
41 |
|
---|
42 | if (slirp_istream_read(f, &b, sizeof(b))) {
|
---|
43 | return b;
|
---|
44 | }
|
---|
45 |
|
---|
46 | return 0;
|
---|
47 | }
|
---|
48 |
|
---|
49 | bool slirp_ostream_write_u8(SlirpOStream *f, uint8_t b)
|
---|
50 | {
|
---|
51 | return slirp_ostream_write(f, &b, sizeof(b));
|
---|
52 | }
|
---|
53 |
|
---|
54 | uint16_t slirp_istream_read_u16(SlirpIStream *f)
|
---|
55 | {
|
---|
56 | uint16_t b;
|
---|
57 |
|
---|
58 | if (slirp_istream_read(f, &b, sizeof(b))) {
|
---|
59 | return GUINT16_FROM_BE(b);
|
---|
60 | }
|
---|
61 |
|
---|
62 | return 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | bool slirp_ostream_write_u16(SlirpOStream *f, uint16_t b)
|
---|
66 | {
|
---|
67 | b = GUINT16_TO_BE(b);
|
---|
68 | return slirp_ostream_write(f, &b, sizeof(b));
|
---|
69 | }
|
---|
70 |
|
---|
71 | uint32_t slirp_istream_read_u32(SlirpIStream *f)
|
---|
72 | {
|
---|
73 | uint32_t b;
|
---|
74 |
|
---|
75 | if (slirp_istream_read(f, &b, sizeof(b))) {
|
---|
76 | return GUINT32_FROM_BE(b);
|
---|
77 | }
|
---|
78 |
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | bool slirp_ostream_write_u32(SlirpOStream *f, uint32_t b)
|
---|
83 | {
|
---|
84 | b = GUINT32_TO_BE(b);
|
---|
85 | return slirp_ostream_write(f, &b, sizeof(b));
|
---|
86 | }
|
---|
87 |
|
---|
88 | int16_t slirp_istream_read_i16(SlirpIStream *f)
|
---|
89 | {
|
---|
90 | int16_t b;
|
---|
91 |
|
---|
92 | if (slirp_istream_read(f, &b, sizeof(b))) {
|
---|
93 | return GINT16_FROM_BE(b);
|
---|
94 | }
|
---|
95 |
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | bool slirp_ostream_write_i16(SlirpOStream *f, int16_t b)
|
---|
100 | {
|
---|
101 | b = GINT16_TO_BE(b);
|
---|
102 | return slirp_ostream_write(f, &b, sizeof(b));
|
---|
103 | }
|
---|
104 |
|
---|
105 | int32_t slirp_istream_read_i32(SlirpIStream *f)
|
---|
106 | {
|
---|
107 | int32_t b;
|
---|
108 |
|
---|
109 | if (slirp_istream_read(f, &b, sizeof(b))) {
|
---|
110 | return GINT32_FROM_BE(b);
|
---|
111 | }
|
---|
112 |
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool slirp_ostream_write_i32(SlirpOStream *f, int32_t b)
|
---|
117 | {
|
---|
118 | b = GINT32_TO_BE(b);
|
---|
119 | return slirp_ostream_write(f, &b, sizeof(b));
|
---|
120 | }
|
---|