blob: c5f708ab2e107544e28bdc73ca11d21f64c4d884 [file] [log] [blame]
Damien Miller05e82c32014-05-15 14:33:43 +10001/* $OpenBSD: buffer.c,v 1.36 2014/04/30 05:29:56 djm Exp $ */
2
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller05e82c32014-05-15 14:33:43 +10004 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Miller05e82c32014-05-15 14:33:43 +10006 * 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.
9 *
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.
Damien Miller95def091999-11-25 00:26:21 +110017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
Damien Miller05e82c32014-05-15 14:33:43 +100019/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
Darren Tucker58538d72014-06-11 13:39:24 +100021#include "includes.h"
22
Damien Miller05e82c32014-05-15 14:33:43 +100023#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100024
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "log.h"
Damien Miller05e82c32014-05-15 14:33:43 +100027#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
Damien Miller4af51302000-04-16 11:18:38 +100029void
Damien Miller5a6b4fe2001-12-21 14:56:54 +110030buffer_append(Buffer *buffer, const void *data, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031{
Damien Miller05e82c32014-05-15 14:33:43 +100032 int ret;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
Damien Miller05e82c32014-05-15 14:33:43 +100034 if ((ret = sshbuf_put(buffer, data, len)) != 0)
35 fatal("%s: %s", __func__, ssh_err(ret));
Damien Miller499a0d52006-04-23 12:06:03 +100036}
37
Damien Miller5a6b4fe2001-12-21 14:56:54 +110038void *
39buffer_append_space(Buffer *buffer, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040{
Damien Miller05e82c32014-05-15 14:33:43 +100041 int ret;
42 u_char *p;
Damien Miller5a6b4fe2001-12-21 14:56:54 +110043
Damien Miller05e82c32014-05-15 14:33:43 +100044 if ((ret = sshbuf_reserve(buffer, len, &p)) != 0)
45 fatal("%s: %s", __func__, ssh_err(ret));
46 return p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047}
48
Damien Miller499a0d52006-04-23 12:06:03 +100049int
50buffer_check_alloc(Buffer *buffer, u_int len)
51{
Damien Miller05e82c32014-05-15 14:33:43 +100052 int ret = sshbuf_check_reserve(buffer, len);
53
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));
Damien Miller499a0d52006-04-23 12:06:03 +100059}
60
Darren Tucker50dbe832004-11-05 20:41:24 +110061int
62buffer_get_ret(Buffer *buffer, void *buf, u_int len)
63{
Damien Miller05e82c32014-05-15 14:33:43 +100064 int ret;
65
66 if ((ret = sshbuf_get(buffer, buf, len)) != 0) {
67 error("%s: %s", __func__, ssh_err(ret));
68 return -1;
Darren Tucker50dbe832004-11-05 20:41:24 +110069 }
Damien Miller05e82c32014-05-15 14:33:43 +100070 return 0;
Darren Tucker50dbe832004-11-05 20:41:24 +110071}
72
Damien Miller4af51302000-04-16 11:18:38 +100073void
Damien Miller5a6b4fe2001-12-21 14:56:54 +110074buffer_get(Buffer *buffer, void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075{
Darren Tucker50dbe832004-11-05 20:41:24 +110076 if (buffer_get_ret(buffer, buf, len) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100077 fatal("%s: buffer error", __func__);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078}
79
Darren Tucker50dbe832004-11-05 20:41:24 +110080int
81buffer_consume_ret(Buffer *buffer, u_int bytes)
82{
Damien Miller05e82c32014-05-15 14:33:43 +100083 int ret = sshbuf_consume(buffer, bytes);
84
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));
Darren Tucker50dbe832004-11-05 20:41:24 +110090}
91
Damien Miller4af51302000-04-16 11:18:38 +100092void
Ben Lindstrom46c16222000-12-22 01:43:59 +000093buffer_consume(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094{
Darren Tucker50dbe832004-11-05 20:41:24 +110095 if (buffer_consume_ret(buffer, bytes) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100096 fatal("%s: buffer error", __func__);
Damien Miller95def091999-11-25 00:26:21 +110097}
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Darren Tucker50dbe832004-11-05 20:41:24 +110099int
100buffer_consume_end_ret(Buffer *buffer, u_int bytes)
101{
Damien Miller05e82c32014-05-15 14:33:43 +1000102 int ret = sshbuf_consume_end(buffer, bytes);
103
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));
Darren Tucker50dbe832004-11-05 20:41:24 +1100109}
110
Damien Miller4af51302000-04-16 11:18:38 +1000111void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000112buffer_consume_end(Buffer *buffer, u_int bytes)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113{
Darren Tucker50dbe832004-11-05 20:41:24 +1100114 if (buffer_consume_end_ret(buffer, bytes) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +1000115 fatal("%s: buffer error", __func__);
Damien Miller95def091999-11-25 00:26:21 +1100116}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118