1 |
|
|
/* $OpenBSD: buffer.c,v 1.36 2014/04/30 05:29:56 djm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2012 Damien Miller <djm@mindrot.org> |
5 |
|
|
* |
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. |
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. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */ |
20 |
|
|
|
21 |
|
|
#include <sys/types.h> |
22 |
|
|
|
23 |
|
|
#include "buffer.h" |
24 |
|
|
#include "log.h" |
25 |
|
|
#include "ssherr.h" |
26 |
|
|
|
27 |
|
|
void |
28 |
|
|
buffer_append(Buffer *buffer, const void *data, u_int len) |
29 |
|
|
{ |
30 |
|
|
int ret; |
31 |
|
|
|
32 |
|
|
if ((ret = sshbuf_put(buffer, data, len)) != 0) |
33 |
|
|
fatal("%s: %s", __func__, ssh_err(ret)); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
void * |
37 |
|
|
buffer_append_space(Buffer *buffer, u_int len) |
38 |
|
|
{ |
39 |
|
|
int ret; |
40 |
|
|
u_char *p; |
41 |
|
|
|
42 |
|
|
if ((ret = sshbuf_reserve(buffer, len, &p)) != 0) |
43 |
|
|
fatal("%s: %s", __func__, ssh_err(ret)); |
44 |
|
|
return p; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
int |
48 |
|
|
buffer_check_alloc(Buffer *buffer, u_int len) |
49 |
|
|
{ |
50 |
|
|
int ret = sshbuf_check_reserve(buffer, len); |
51 |
|
|
|
52 |
|
|
if (ret == 0) |
53 |
|
|
return 1; |
54 |
|
|
if (ret == SSH_ERR_NO_BUFFER_SPACE) |
55 |
|
|
return 0; |
56 |
|
|
fatal("%s: %s", __func__, ssh_err(ret)); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
int |
60 |
|
|
buffer_get_ret(Buffer *buffer, void *buf, u_int len) |
61 |
|
|
{ |
62 |
|
|
int ret; |
63 |
|
|
|
64 |
|
|
if ((ret = sshbuf_get(buffer, buf, len)) != 0) { |
65 |
|
|
error("%s: %s", __func__, ssh_err(ret)); |
66 |
|
|
return -1; |
67 |
|
|
} |
68 |
|
|
return 0; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
void |
72 |
|
|
buffer_get(Buffer *buffer, void *buf, u_int len) |
73 |
|
|
{ |
74 |
|
|
if (buffer_get_ret(buffer, buf, len) == -1) |
75 |
|
|
fatal("%s: buffer error", __func__); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
int |
79 |
|
|
buffer_consume_ret(Buffer *buffer, u_int bytes) |
80 |
|
|
{ |
81 |
|
|
int ret = sshbuf_consume(buffer, bytes); |
82 |
|
|
|
83 |
|
|
if (ret == 0) |
84 |
|
|
return 0; |
85 |
|
|
if (ret == SSH_ERR_MESSAGE_INCOMPLETE) |
86 |
|
|
return -1; |
87 |
|
|
fatal("%s: %s", __func__, ssh_err(ret)); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
void |
91 |
|
|
buffer_consume(Buffer *buffer, u_int bytes) |
92 |
|
|
{ |
93 |
|
|
if (buffer_consume_ret(buffer, bytes) == -1) |
94 |
|
|
fatal("%s: buffer error", __func__); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
int |
98 |
|
|
buffer_consume_end_ret(Buffer *buffer, u_int bytes) |
99 |
|
|
{ |
100 |
|
|
int ret = sshbuf_consume_end(buffer, bytes); |
101 |
|
|
|
102 |
|
|
if (ret == 0) |
103 |
|
|
return 0; |
104 |
|
|
if (ret == SSH_ERR_MESSAGE_INCOMPLETE) |
105 |
|
|
return -1; |
106 |
|
|
fatal("%s: %s", __func__, ssh_err(ret)); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
void |
110 |
|
|
buffer_consume_end(Buffer *buffer, u_int bytes) |
111 |
|
|
{ |
112 |
|
|
if (buffer_consume_end_ret(buffer, bytes) == -1) |
113 |
|
|
fatal("%s: buffer error", __func__); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
|