drm/i915: Update to Linux 4.2
[dragonfly.git] / crypto / openssh / buffer.c
blobc5f708ab2e107544e28bdc73ca11d21f64c4d884
1 /* $OpenBSD: buffer.c,v 1.36 2014/04/30 05:29:56 djm Exp $ */
3 /*
4 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
21 #include "includes.h"
23 #include <sys/types.h>
25 #include "buffer.h"
26 #include "log.h"
27 #include "ssherr.h"
29 void
30 buffer_append(Buffer *buffer, const void *data, u_int len)
32 int ret;
34 if ((ret = sshbuf_put(buffer, data, len)) != 0)
35 fatal("%s: %s", __func__, ssh_err(ret));
38 void *
39 buffer_append_space(Buffer *buffer, u_int len)
41 int ret;
42 u_char *p;
44 if ((ret = sshbuf_reserve(buffer, len, &p)) != 0)
45 fatal("%s: %s", __func__, ssh_err(ret));
46 return p;
49 int
50 buffer_check_alloc(Buffer *buffer, u_int len)
52 int ret = sshbuf_check_reserve(buffer, len);
54 if (ret == 0)
55 return 1;
56 if (ret == SSH_ERR_NO_BUFFER_SPACE)
57 return 0;
58 fatal("%s: %s", __func__, ssh_err(ret));
61 int
62 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
64 int ret;
66 if ((ret = sshbuf_get(buffer, buf, len)) != 0) {
67 error("%s: %s", __func__, ssh_err(ret));
68 return -1;
70 return 0;
73 void
74 buffer_get(Buffer *buffer, void *buf, u_int len)
76 if (buffer_get_ret(buffer, buf, len) == -1)
77 fatal("%s: buffer error", __func__);
80 int
81 buffer_consume_ret(Buffer *buffer, u_int bytes)
83 int ret = sshbuf_consume(buffer, bytes);
85 if (ret == 0)
86 return 0;
87 if (ret == SSH_ERR_MESSAGE_INCOMPLETE)
88 return -1;
89 fatal("%s: %s", __func__, ssh_err(ret));
92 void
93 buffer_consume(Buffer *buffer, u_int bytes)
95 if (buffer_consume_ret(buffer, bytes) == -1)
96 fatal("%s: buffer error", __func__);
99 int
100 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
102 int ret = sshbuf_consume_end(buffer, bytes);
104 if (ret == 0)
105 return 0;
106 if (ret == SSH_ERR_MESSAGE_INCOMPLETE)
107 return -1;
108 fatal("%s: %s", __func__, ssh_err(ret));
111 void
112 buffer_consume_end(Buffer *buffer, u_int bytes)
114 if (buffer_consume_end_ret(buffer, bytes) == -1)
115 fatal("%s: buffer error", __func__);