libssh  0.7.3
buffer.h
1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2009 by Aris Adamantiadis
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef BUFFER_H_
22 #define BUFFER_H_
23 
24 #include <stdarg.h>
25 
26 #include "libssh/libssh.h"
27 /*
28  * Describes a buffer state
29  * [XXXXXXXXXXXXDATA PAYLOAD XXXXXXXXXXXXXXXXXXXXXXXX]
30  * ^ ^ ^ ^]
31  * \_data points\_pos points here \_used points here | /
32  * here Allocated
33  */
34 struct ssh_buffer_struct {
35  char *data;
36  uint32_t used;
37  uint32_t allocated;
38  uint32_t pos;
39  int secure;
40 };
41 
42 #define SSH_BUFFER_PACK_END ((uint32_t) 0x4f65feb3)
43 
44 LIBSSH_API void ssh_buffer_free(ssh_buffer buffer);
45 LIBSSH_API void *ssh_buffer_get_begin(ssh_buffer buffer);
46 LIBSSH_API uint32_t ssh_buffer_get_len(ssh_buffer buffer);
47 LIBSSH_API ssh_buffer ssh_buffer_new(void);
48 void ssh_buffer_set_secure(ssh_buffer buffer);
49 int buffer_add_ssh_string(ssh_buffer buffer, ssh_string string);
50 int buffer_add_u8(ssh_buffer buffer, uint8_t data);
51 int buffer_add_u16(ssh_buffer buffer, uint16_t data);
52 int buffer_add_u32(ssh_buffer buffer, uint32_t data);
53 int buffer_add_u64(ssh_buffer buffer, uint64_t data);
54 int ssh_buffer_add_data(ssh_buffer buffer, const void *data, uint32_t len);
55 
56 int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
57  const char *format,
58  int argc,
59  va_list ap);
60 int _ssh_buffer_pack(struct ssh_buffer_struct *buffer,
61  const char *format,
62  int argc,
63  ...);
64 #define ssh_buffer_pack(buffer, format, ...) \
65  _ssh_buffer_pack((buffer), (format), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SSH_BUFFER_PACK_END)
66 
67 int ssh_buffer_unpack_va(struct ssh_buffer_struct *buffer,
68  const char *format, int argc,
69  va_list ap);
70 int _ssh_buffer_unpack(struct ssh_buffer_struct *buffer,
71  const char *format,
72  int argc,
73  ...);
74 #define ssh_buffer_unpack(buffer, format, ...) \
75  _ssh_buffer_unpack((buffer), (format), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SSH_BUFFER_PACK_END)
76 
77 int buffer_prepend_data(ssh_buffer buffer, const void *data, uint32_t len);
78 int buffer_add_buffer(ssh_buffer buffer, ssh_buffer source);
79 int ssh_buffer_reinit(ssh_buffer buffer);
80 
81 /* buffer_get_rest returns a pointer to the current position into the buffer */
82 void *buffer_get_rest(ssh_buffer buffer);
83 /* buffer_get_rest_len returns the number of bytes which can be read */
84 uint32_t buffer_get_rest_len(ssh_buffer buffer);
85 
86 /* buffer_read_*() returns the number of bytes read, except for ssh strings */
87 int buffer_get_u8(ssh_buffer buffer, uint8_t *data);
88 int buffer_get_u32(ssh_buffer buffer, uint32_t *data);
89 int buffer_get_u64(ssh_buffer buffer, uint64_t *data);
90 
91 uint32_t buffer_get_data(ssh_buffer buffer, void *data, uint32_t requestedlen);
92 /* buffer_get_ssh_string() is an exception. if the String read is too large or invalid, it will answer NULL. */
93 ssh_string buffer_get_ssh_string(ssh_buffer buffer);
94 /* gets a string out of a SSH-1 mpint */
95 ssh_string buffer_get_mpint(ssh_buffer buffer);
96 /* buffer_pass_bytes acts as if len bytes have been read (used for padding) */
97 uint32_t buffer_pass_bytes_end(ssh_buffer buffer, uint32_t len);
98 uint32_t buffer_pass_bytes(ssh_buffer buffer, uint32_t len);
99 
100 #endif /* BUFFER_H_ */
void ssh_buffer_free(struct ssh_buffer_struct *buffer)
Deallocate a SSH buffer.
Definition: buffer.c:102
void * ssh_buffer_get_begin(struct ssh_buffer_struct *buffer)
Get a pointer on the head of a buffer.
Definition: buffer.c:450
uint32_t ssh_buffer_get_len(struct ssh_buffer_struct *buffer)
Get the length of the buffer, not counting position.
Definition: buffer.c:479
LIBSSH_API ssh_buffer ssh_buffer_new(void)
Create a new SSH buffer.
Definition: buffer.c:86
void ssh_buffer_set_secure(ssh_buffer buffer)
Sets the buffer as secure.
Definition: buffer.c:125