1 /* $OpenBSD: buffer.c,v 1.36 2014/04/30 05:29:56 djm Exp $ */
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 */
23 #include <sys/types.h>
30 buffer_append(Buffer
*buffer
, const void *data
, u_int len
)
34 if ((ret
= sshbuf_put(buffer
, data
, len
)) != 0)
35 fatal("%s: %s", __func__
, ssh_err(ret
));
39 buffer_append_space(Buffer
*buffer
, u_int len
)
44 if ((ret
= sshbuf_reserve(buffer
, len
, &p
)) != 0)
45 fatal("%s: %s", __func__
, ssh_err(ret
));
50 buffer_check_alloc(Buffer
*buffer
, u_int len
)
52 int ret
= sshbuf_check_reserve(buffer
, len
);
56 if (ret
== SSH_ERR_NO_BUFFER_SPACE
)
58 fatal("%s: %s", __func__
, ssh_err(ret
));
62 buffer_get_ret(Buffer
*buffer
, void *buf
, u_int len
)
66 if ((ret
= sshbuf_get(buffer
, buf
, len
)) != 0) {
67 error("%s: %s", __func__
, ssh_err(ret
));
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__
);
81 buffer_consume_ret(Buffer
*buffer
, u_int bytes
)
83 int ret
= sshbuf_consume(buffer
, bytes
);
87 if (ret
== SSH_ERR_MESSAGE_INCOMPLETE
)
89 fatal("%s: %s", __func__
, ssh_err(ret
));
93 buffer_consume(Buffer
*buffer
, u_int bytes
)
95 if (buffer_consume_ret(buffer
, bytes
) == -1)
96 fatal("%s: buffer error", __func__
);
100 buffer_consume_end_ret(Buffer
*buffer
, u_int bytes
)
102 int ret
= sshbuf_consume_end(buffer
, bytes
);
106 if (ret
== SSH_ERR_MESSAGE_INCOMPLETE
)
108 fatal("%s: %s", __func__
, ssh_err(ret
));
112 buffer_consume_end(Buffer
*buffer
, u_int bytes
)
114 if (buffer_consume_end_ret(buffer
, bytes
) == -1)
115 fatal("%s: buffer error", __func__
);