libssh  0.6.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 "libssh/libssh.h"
25 /*
26  * Describes a buffer state
27  * [XXXXXXXXXXXXDATA PAYLOAD XXXXXXXXXXXXXXXXXXXXXXXX]
28  * ^ ^ ^ ^]
29  * \_data points\_pos points here \_used points here | /
30  * here Allocated
31  */
32 struct ssh_buffer_struct {
33  char *data;
34  uint32_t used;
35  uint32_t allocated;
36  uint32_t pos;
37 };
38 
39 LIBSSH_API void ssh_buffer_free(ssh_buffer buffer);
40 LIBSSH_API void *ssh_buffer_get_begin(ssh_buffer buffer);
41 LIBSSH_API uint32_t ssh_buffer_get_len(ssh_buffer buffer);
42 LIBSSH_API ssh_buffer ssh_buffer_new(void);
43 int buffer_add_ssh_string(ssh_buffer buffer, ssh_string string);
44 int buffer_add_u8(ssh_buffer buffer, uint8_t data);
45 int buffer_add_u16(ssh_buffer buffer, uint16_t data);
46 int buffer_add_u32(ssh_buffer buffer, uint32_t data);
47 int buffer_add_u64(ssh_buffer buffer, uint64_t data);
48 int buffer_add_data(ssh_buffer buffer, const void *data, uint32_t len);
49 int buffer_prepend_data(ssh_buffer buffer, const void *data, uint32_t len);
50 int buffer_add_buffer(ssh_buffer buffer, ssh_buffer source);
51 int buffer_reinit(ssh_buffer buffer);
52 
53 /* buffer_get_rest returns a pointer to the current position into the buffer */
54 void *buffer_get_rest(ssh_buffer buffer);
55 /* buffer_get_rest_len returns the number of bytes which can be read */
56 uint32_t buffer_get_rest_len(ssh_buffer buffer);
57 
58 /* buffer_read_*() returns the number of bytes read, except for ssh strings */
59 int buffer_get_u8(ssh_buffer buffer, uint8_t *data);
60 int buffer_get_u32(ssh_buffer buffer, uint32_t *data);
61 int buffer_get_u64(ssh_buffer buffer, uint64_t *data);
62 
63 uint32_t buffer_get_data(ssh_buffer buffer, void *data, uint32_t requestedlen);
64 /* buffer_get_ssh_string() is an exception. if the String read is too large or invalid, it will answer NULL. */
65 ssh_string buffer_get_ssh_string(ssh_buffer buffer);
66 /* gets a string out of a SSH-1 mpint */
67 ssh_string buffer_get_mpint(ssh_buffer buffer);
68 /* buffer_pass_bytes acts as if len bytes have been read (used for padding) */
69 uint32_t buffer_pass_bytes_end(ssh_buffer buffer, uint32_t len);
70 uint32_t buffer_pass_bytes(ssh_buffer buffer, uint32_t len);
71 
72 #endif /* BUFFER_H_ */