blob: 656db4a04c6f94749351c156b081028528f15172 [file] [log] [blame]
Eduardo Habkost9a4ac512014-10-01 14:47:33 -03001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
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 */
Peter Maydell1393a482016-01-26 18:16:54 +000024#include "qemu/osdep.h"
Liang Li44f0ead2015-03-23 16:32:19 +080025#include <zlib.h>
Eduardo Habkost093c4552013-11-28 12:01:16 -020026#include "qemu-common.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010027#include "qemu/error-report.h"
Eduardo Habkost093c4552013-11-28 12:01:16 -020028#include "qemu/iov.h"
29#include "qemu/sockets.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010030#include "qemu/coroutine.h"
Eduardo Habkost093c4552013-11-28 12:01:16 -020031#include "migration/migration.h"
32#include "migration/qemu-file.h"
Dr. David Alan Gilbert4f9d0902014-12-12 11:13:40 +000033#include "migration/qemu-file-internal.h"
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +110034#include "trace.h"
Eduardo Habkost093c4552013-11-28 12:01:16 -020035
Dr. David Alan Gilberte1a8c9b2015-01-08 11:11:30 +000036/*
37 * Stop a file from being read/written - not all backing files can do this
38 * typically only sockets can.
39 */
40int qemu_file_shutdown(QEMUFile *f)
41{
42 if (!f->ops->shut_down) {
43 return -ENOSYS;
44 }
45 return f->ops->shut_down(f->opaque, true, true);
46}
47
Dr. David Alan Gilbertadc468e2015-11-05 18:10:43 +000048/*
49 * Result: QEMUFile* for a 'return path' for comms in the opposite direction
50 * NULL if not available
51 */
52QEMUFile *qemu_file_get_return_path(QEMUFile *f)
53{
54 if (!f->ops->get_return_path) {
55 return NULL;
56 }
57 return f->ops->get_return_path(f->opaque);
58}
59
Eduardo Habkost093c4552013-11-28 12:01:16 -020060bool qemu_file_mode_is_not_valid(const char *mode)
61{
62 if (mode == NULL ||
63 (mode[0] != 'r' && mode[0] != 'w') ||
64 mode[1] != 'b' || mode[2] != 0) {
65 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
66 return true;
67 }
68
69 return false;
70}
71
Eduardo Habkost093c4552013-11-28 12:01:16 -020072QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
73{
74 QEMUFile *f;
75
Markus Armbruster97f3ad32015-09-14 13:51:31 +020076 f = g_new0(QEMUFile, 1);
Eduardo Habkost093c4552013-11-28 12:01:16 -020077
78 f->opaque = opaque;
79 f->ops = ops;
80 return f;
81}
82
83/*
84 * Get last error for stream f
85 *
86 * Return negative error value if there has been an error on previous
87 * operations, return 0 if no error happened.
88 *
89 */
90int qemu_file_get_error(QEMUFile *f)
91{
92 return f->last_error;
93}
94
95void qemu_file_set_error(QEMUFile *f, int ret)
96{
97 if (f->last_error == 0) {
98 f->last_error = ret;
99 }
100}
101
Eduardo Habkoste68dd362014-10-01 17:34:34 -0300102bool qemu_file_is_writable(QEMUFile *f)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200103{
104 return f->ops->writev_buffer || f->ops->put_buffer;
105}
106
107/**
108 * Flushes QEMUFile buffer
109 *
110 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
Daniel P. Berrangebaf51e72016-04-27 11:04:54 +0100111 * put_buffer ops. This will flush all pending data. If data was
112 * only partially flushed, it will set an error state.
Eduardo Habkost093c4552013-11-28 12:01:16 -0200113 */
114void qemu_fflush(QEMUFile *f)
115{
116 ssize_t ret = 0;
Daniel P. Berrangebaf51e72016-04-27 11:04:54 +0100117 ssize_t expect = 0;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200118
119 if (!qemu_file_is_writable(f)) {
120 return;
121 }
122
123 if (f->ops->writev_buffer) {
124 if (f->iovcnt > 0) {
Daniel P. Berrangebaf51e72016-04-27 11:04:54 +0100125 expect = iov_size(f->iov, f->iovcnt);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200126 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
127 }
128 } else {
129 if (f->buf_index > 0) {
Daniel P. Berrangebaf51e72016-04-27 11:04:54 +0100130 expect = f->buf_index;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200131 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
132 }
133 }
Daniel P. Berrangebaf51e72016-04-27 11:04:54 +0100134
Eduardo Habkost093c4552013-11-28 12:01:16 -0200135 if (ret >= 0) {
136 f->pos += ret;
137 }
Daniel P. Berrangebaf51e72016-04-27 11:04:54 +0100138 /* We expect the QEMUFile write impl to send the full
139 * data set we requested, so sanity check that.
140 */
141 if (ret != expect) {
142 qemu_file_set_error(f, ret < 0 ? ret : -EIO);
143 }
Eduardo Habkost093c4552013-11-28 12:01:16 -0200144 f->buf_index = 0;
145 f->iovcnt = 0;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200146}
147
148void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
149{
150 int ret = 0;
151
152 if (f->ops->before_ram_iterate) {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100153 ret = f->ops->before_ram_iterate(f, f->opaque, flags, NULL);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200154 if (ret < 0) {
155 qemu_file_set_error(f, ret);
156 }
157 }
158}
159
160void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
161{
162 int ret = 0;
163
164 if (f->ops->after_ram_iterate) {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100165 ret = f->ops->after_ram_iterate(f, f->opaque, flags, NULL);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200166 if (ret < 0) {
167 qemu_file_set_error(f, ret);
168 }
169 }
170}
171
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100172void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200173{
174 int ret = -EINVAL;
175
176 if (f->ops->hook_ram_load) {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100177 ret = f->ops->hook_ram_load(f, f->opaque, flags, data);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200178 if (ret < 0) {
179 qemu_file_set_error(f, ret);
180 }
181 } else {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100182 /*
183 * Hook is a hook specifically requested by the source sending a flag
184 * that expects there to be a hook on the destination.
185 */
186 if (flags == RAM_CONTROL_HOOK) {
187 qemu_file_set_error(f, ret);
188 }
Eduardo Habkost093c4552013-11-28 12:01:16 -0200189 }
190}
191
192size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
Juan Quintela6e1dea42015-02-12 19:02:42 +0100193 ram_addr_t offset, size_t size,
194 uint64_t *bytes_sent)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200195{
196 if (f->ops->save_page) {
197 int ret = f->ops->save_page(f, f->opaque, block_offset,
198 offset, size, bytes_sent);
199
200 if (ret != RAM_SAVE_CONTROL_DELAYED) {
201 if (bytes_sent && *bytes_sent > 0) {
202 qemu_update_position(f, *bytes_sent);
203 } else if (ret < 0) {
204 qemu_file_set_error(f, ret);
205 }
206 }
207
208 return ret;
209 }
210
211 return RAM_SAVE_CONTROL_NOT_SUPP;
212}
213
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100214/*
215 * Attempt to fill the buffer from the underlying file
216 * Returns the number of bytes read, or negative value for an error.
217 *
218 * Note that it can return a partially full buffer even in a not error/not EOF
219 * case if the underlying file descriptor gives a short read, and that can
220 * happen even on a blocking fd.
221 */
222static ssize_t qemu_fill_buffer(QEMUFile *f)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200223{
224 int len;
225 int pending;
226
227 assert(!qemu_file_is_writable(f));
228
229 pending = f->buf_size - f->buf_index;
230 if (pending > 0) {
231 memmove(f->buf, f->buf + f->buf_index, pending);
232 }
233 f->buf_index = 0;
234 f->buf_size = pending;
235
236 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
237 IO_BUF_SIZE - pending);
238 if (len > 0) {
239 f->buf_size += len;
240 f->pos += len;
241 } else if (len == 0) {
242 qemu_file_set_error(f, -EIO);
243 } else if (len != -EAGAIN) {
244 qemu_file_set_error(f, len);
245 }
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100246
247 return len;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200248}
249
250int qemu_get_fd(QEMUFile *f)
251{
252 if (f->ops->get_fd) {
253 return f->ops->get_fd(f->opaque);
254 }
255 return -1;
256}
257
258void qemu_update_position(QEMUFile *f, size_t size)
259{
260 f->pos += size;
261}
262
263/** Closes the file
264 *
265 * Returns negative error value if any error happened on previous operations or
266 * while closing the file. Returns 0 or positive number on success.
267 *
268 * The meaning of return value on success depends on the specific backend
269 * being used.
270 */
271int qemu_fclose(QEMUFile *f)
272{
273 int ret;
274 qemu_fflush(f);
275 ret = qemu_file_get_error(f);
276
277 if (f->ops->close) {
278 int ret2 = f->ops->close(f->opaque);
279 if (ret >= 0) {
280 ret = ret2;
281 }
282 }
283 /* If any error was spotted before closing, we should report it
284 * instead of the close() return value.
285 */
286 if (f->last_error) {
287 ret = f->last_error;
288 }
289 g_free(f);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100290 trace_qemu_file_fclose();
Eduardo Habkost093c4552013-11-28 12:01:16 -0200291 return ret;
292}
293
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100294static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200295{
296 /* check for adjacent buffer and coalesce them */
297 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
298 f->iov[f->iovcnt - 1].iov_len) {
299 f->iov[f->iovcnt - 1].iov_len += size;
300 } else {
301 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
302 f->iov[f->iovcnt++].iov_len = size;
303 }
304
305 if (f->iovcnt >= MAX_IOV_SIZE) {
306 qemu_fflush(f);
307 }
308}
309
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100310void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200311{
312 if (!f->ops->writev_buffer) {
313 qemu_put_buffer(f, buf, size);
314 return;
315 }
316
317 if (f->last_error) {
318 return;
319 }
320
321 f->bytes_xfer += size;
322 add_to_iovec(f, buf, size);
323}
324
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100325void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200326{
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100327 size_t l;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200328
329 if (f->last_error) {
330 return;
331 }
332
333 while (size > 0) {
334 l = IO_BUF_SIZE - f->buf_index;
335 if (l > size) {
336 l = size;
337 }
338 memcpy(f->buf + f->buf_index, buf, l);
339 f->bytes_xfer += l;
340 if (f->ops->writev_buffer) {
341 add_to_iovec(f, f->buf + f->buf_index, l);
342 }
343 f->buf_index += l;
344 if (f->buf_index == IO_BUF_SIZE) {
345 qemu_fflush(f);
346 }
347 if (qemu_file_get_error(f)) {
348 break;
349 }
350 buf += l;
351 size -= l;
352 }
353}
354
355void qemu_put_byte(QEMUFile *f, int v)
356{
357 if (f->last_error) {
358 return;
359 }
360
361 f->buf[f->buf_index] = v;
362 f->bytes_xfer++;
363 if (f->ops->writev_buffer) {
364 add_to_iovec(f, f->buf + f->buf_index, 1);
365 }
366 f->buf_index++;
367 if (f->buf_index == IO_BUF_SIZE) {
368 qemu_fflush(f);
369 }
370}
371
372void qemu_file_skip(QEMUFile *f, int size)
373{
374 if (f->buf_index + size <= f->buf_size) {
375 f->buf_index += size;
376 }
377}
378
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100379/*
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100380 * Read 'size' bytes from file (at 'offset') without moving the
381 * pointer and set 'buf' to point to that data.
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100382 *
383 * It will return size bytes unless there was an error, in which case it will
384 * return as many as it managed to read (assuming blocking fd's which
385 * all current QEMUFile are)
386 */
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100387size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200388{
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100389 ssize_t pending;
390 size_t index;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200391
392 assert(!qemu_file_is_writable(f));
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100393 assert(offset < IO_BUF_SIZE);
394 assert(size <= IO_BUF_SIZE - offset);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200395
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100396 /* The 1st byte to read from */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200397 index = f->buf_index + offset;
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100398 /* The number of available bytes starting at index */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200399 pending = f->buf_size - index;
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100400
401 /*
402 * qemu_fill_buffer might return just a few bytes, even when there isn't
403 * an error, so loop collecting them until we get enough.
404 */
405 while (pending < size) {
406 int received = qemu_fill_buffer(f);
407
408 if (received <= 0) {
409 break;
410 }
411
Eduardo Habkost093c4552013-11-28 12:01:16 -0200412 index = f->buf_index + offset;
413 pending = f->buf_size - index;
414 }
415
416 if (pending <= 0) {
417 return 0;
418 }
419 if (size > pending) {
420 size = pending;
421 }
422
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100423 *buf = f->buf + index;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200424 return size;
425}
426
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100427/*
428 * Read 'size' bytes of data from the file into buf.
429 * 'size' can be larger than the internal buffer.
430 *
431 * It will return size bytes unless there was an error, in which case it will
432 * return as many as it managed to read (assuming blocking fd's which
433 * all current QEMUFile are)
434 */
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100435size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200436{
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100437 size_t pending = size;
438 size_t done = 0;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200439
440 while (pending > 0) {
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100441 size_t res;
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100442 uint8_t *src;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200443
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100444 res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200445 if (res == 0) {
446 return done;
447 }
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100448 memcpy(buf, src, res);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200449 qemu_file_skip(f, res);
450 buf += res;
451 pending -= res;
452 done += res;
453 }
454 return done;
455}
456
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100457/*
Dr. David Alan Gilbert9504fb52015-11-05 18:10:35 +0000458 * Read 'size' bytes of data from the file.
459 * 'size' can be larger than the internal buffer.
460 *
461 * The data:
462 * may be held on an internal buffer (in which case *buf is updated
463 * to point to it) that is valid until the next qemu_file operation.
464 * OR
465 * will be copied to the *buf that was passed in.
466 *
467 * The code tries to avoid the copy if possible.
468 *
469 * It will return size bytes unless there was an error, in which case it will
470 * return as many as it managed to read (assuming blocking fd's which
471 * all current QEMUFile are)
472 *
473 * Note: Since **buf may get changed, the caller should take care to
474 * keep a pointer to the original buffer if it needs to deallocate it.
475 */
476size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
477{
478 if (size < IO_BUF_SIZE) {
479 size_t res;
480 uint8_t *src;
481
482 res = qemu_peek_buffer(f, &src, size, 0);
483
484 if (res == size) {
485 qemu_file_skip(f, res);
486 *buf = src;
487 return res;
488 }
489 }
490
491 return qemu_get_buffer(f, *buf, size);
492}
493
494/*
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100495 * Peeks a single byte from the buffer; this isn't guaranteed to work if
496 * offset leaves a gap after the previous read/peeked data.
497 */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200498int qemu_peek_byte(QEMUFile *f, int offset)
499{
500 int index = f->buf_index + offset;
501
502 assert(!qemu_file_is_writable(f));
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100503 assert(offset < IO_BUF_SIZE);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200504
505 if (index >= f->buf_size) {
506 qemu_fill_buffer(f);
507 index = f->buf_index + offset;
508 if (index >= f->buf_size) {
509 return 0;
510 }
511 }
512 return f->buf[index];
513}
514
515int qemu_get_byte(QEMUFile *f)
516{
517 int result;
518
519 result = qemu_peek_byte(f, 0);
520 qemu_file_skip(f, 1);
521 return result;
522}
523
Alexander Graf97221402015-01-22 15:01:38 +0100524int64_t qemu_ftell_fast(QEMUFile *f)
525{
526 int64_t ret = f->pos;
527 int i;
528
529 if (f->ops->writev_buffer) {
530 for (i = 0; i < f->iovcnt; i++) {
531 ret += f->iov[i].iov_len;
532 }
533 } else {
534 ret += f->buf_index;
535 }
536
537 return ret;
538}
539
Eduardo Habkost093c4552013-11-28 12:01:16 -0200540int64_t qemu_ftell(QEMUFile *f)
541{
542 qemu_fflush(f);
543 return f->pos;
544}
545
546int qemu_file_rate_limit(QEMUFile *f)
547{
548 if (qemu_file_get_error(f)) {
549 return 1;
550 }
551 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
552 return 1;
553 }
554 return 0;
555}
556
557int64_t qemu_file_get_rate_limit(QEMUFile *f)
558{
559 return f->xfer_limit;
560}
561
562void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
563{
564 f->xfer_limit = limit;
565}
566
567void qemu_file_reset_rate_limit(QEMUFile *f)
568{
569 f->bytes_xfer = 0;
570}
571
572void qemu_put_be16(QEMUFile *f, unsigned int v)
573{
574 qemu_put_byte(f, v >> 8);
575 qemu_put_byte(f, v);
576}
577
578void qemu_put_be32(QEMUFile *f, unsigned int v)
579{
580 qemu_put_byte(f, v >> 24);
581 qemu_put_byte(f, v >> 16);
582 qemu_put_byte(f, v >> 8);
583 qemu_put_byte(f, v);
584}
585
586void qemu_put_be64(QEMUFile *f, uint64_t v)
587{
588 qemu_put_be32(f, v >> 32);
589 qemu_put_be32(f, v);
590}
591
592unsigned int qemu_get_be16(QEMUFile *f)
593{
594 unsigned int v;
595 v = qemu_get_byte(f) << 8;
596 v |= qemu_get_byte(f);
597 return v;
598}
599
600unsigned int qemu_get_be32(QEMUFile *f)
601{
602 unsigned int v;
Peter Maydell90d6a672014-12-23 22:26:55 +0000603 v = (unsigned int)qemu_get_byte(f) << 24;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200604 v |= qemu_get_byte(f) << 16;
605 v |= qemu_get_byte(f) << 8;
606 v |= qemu_get_byte(f);
607 return v;
608}
609
610uint64_t qemu_get_be64(QEMUFile *f)
611{
612 uint64_t v;
613 v = (uint64_t)qemu_get_be32(f) << 32;
614 v |= qemu_get_be32(f);
615 return v;
616}
Liang Li44f0ead2015-03-23 16:32:19 +0800617
618/* compress size bytes of data start at p with specific compression
619 * level and store the compressed data to the buffer of f.
620 */
621
622ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
623 int level)
624{
625 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
626
627 if (blen < compressBound(size)) {
628 return 0;
629 }
630 if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen,
631 (Bytef *)p, size, level) != Z_OK) {
632 error_report("Compress Failed!");
633 return 0;
634 }
635 qemu_put_be32(f, blen);
636 f->buf_index += blen;
637 return blen + sizeof(int32_t);
638}
639
640/* Put the data in the buffer of f_src to the buffer of f_des, and
641 * then reset the buf_index of f_src to 0.
642 */
643
644int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
645{
646 int len = 0;
647
648 if (f_src->buf_index > 0) {
649 len = f_src->buf_index;
650 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
651 f_src->buf_index = 0;
652 }
653 return len;
654}
Dr. David Alan Gilbertb3af1bc2015-05-21 13:24:11 +0100655
656/*
657 * Get a string whose length is determined by a single preceding byte
658 * A preallocated 256 byte buffer must be passed in.
659 * Returns: len on success and a 0 terminated string in the buffer
660 * else 0
661 * (Note a 0 length string will return 0 either way)
662 */
663size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
664{
665 size_t len = qemu_get_byte(f);
666 size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
667
668 buf[res] = 0;
669
670 return res == len ? res : 0;
671}
Dr. David Alan Gilberta800cd52015-11-05 18:10:36 +0000672
673/*
674 * Set the blocking state of the QEMUFile.
675 * Note: On some transports the OS only keeps a single blocking state for
676 * both directions, and thus changing the blocking on the main
677 * QEMUFile can also affect the return path.
678 */
679void qemu_file_set_blocking(QEMUFile *f, bool block)
680{
681 if (block) {
682 qemu_set_block(qemu_get_fd(f));
683 } else {
684 qemu_set_nonblock(qemu_get_fd(f));
685 }
686}