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