libssh  0.7.2
include/libssh/buffer.h
00001 /*
00002  * This file is part of the SSH Library
00003  *
00004  * Copyright (c) 2009 by Aris Adamantiadis
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #ifndef BUFFER_H_
00022 #define BUFFER_H_
00023 
00024 #include <stdarg.h>
00025 
00026 #include "libssh/libssh.h"
00027 /*
00028  * Describes a buffer state
00029  * [XXXXXXXXXXXXDATA PAYLOAD       XXXXXXXXXXXXXXXXXXXXXXXX]
00030  * ^            ^                  ^                       ^]
00031  * \_data points\_pos points here  \_used points here |    /
00032  *   here                                          Allocated
00033  */
00034 struct ssh_buffer_struct {
00035     char *data;
00036     uint32_t used;
00037     uint32_t allocated;
00038     uint32_t pos;
00039     int secure;
00040 };
00041 
00042 #define SSH_BUFFER_PACK_END ((uint32_t) 0x4f65feb3)
00043 
00044 LIBSSH_API void ssh_buffer_free(ssh_buffer buffer);
00045 LIBSSH_API void *ssh_buffer_get_begin(ssh_buffer buffer);
00046 LIBSSH_API uint32_t ssh_buffer_get_len(ssh_buffer buffer);
00047 LIBSSH_API ssh_buffer ssh_buffer_new(void);
00048 void ssh_buffer_set_secure(ssh_buffer buffer);
00049 int buffer_add_ssh_string(ssh_buffer buffer, ssh_string string);
00050 int buffer_add_u8(ssh_buffer buffer, uint8_t data);
00051 int buffer_add_u16(ssh_buffer buffer, uint16_t data);
00052 int buffer_add_u32(ssh_buffer buffer, uint32_t data);
00053 int buffer_add_u64(ssh_buffer buffer, uint64_t data);
00054 int ssh_buffer_add_data(ssh_buffer buffer, const void *data, uint32_t len);
00055 
00056 int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
00057                        const char *format,
00058                        int argc,
00059                        va_list ap);
00060 int _ssh_buffer_pack(struct ssh_buffer_struct *buffer,
00061                      const char *format,
00062                      int argc,
00063                      ...);
00064 #define ssh_buffer_pack(buffer, format, ...) \
00065     _ssh_buffer_pack((buffer), (format), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SSH_BUFFER_PACK_END)
00066 
00067 int ssh_buffer_unpack_va(struct ssh_buffer_struct *buffer,
00068                          const char *format, int argc,
00069                          va_list ap);
00070 int _ssh_buffer_unpack(struct ssh_buffer_struct *buffer,
00071                        const char *format,
00072                        int argc,
00073                        ...);
00074 #define ssh_buffer_unpack(buffer, format, ...) \
00075     _ssh_buffer_unpack((buffer), (format), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SSH_BUFFER_PACK_END)
00076 
00077 int buffer_prepend_data(ssh_buffer buffer, const void *data, uint32_t len);
00078 int buffer_add_buffer(ssh_buffer buffer, ssh_buffer source);
00079 int ssh_buffer_reinit(ssh_buffer buffer);
00080 
00081 /* buffer_get_rest returns a pointer to the current position into the buffer */
00082 void *buffer_get_rest(ssh_buffer buffer);
00083 /* buffer_get_rest_len returns the number of bytes which can be read */
00084 uint32_t buffer_get_rest_len(ssh_buffer buffer);
00085 
00086 /* buffer_read_*() returns the number of bytes read, except for ssh strings */
00087 int buffer_get_u8(ssh_buffer buffer, uint8_t *data);
00088 int buffer_get_u32(ssh_buffer buffer, uint32_t *data);
00089 int buffer_get_u64(ssh_buffer buffer, uint64_t *data);
00090 
00091 uint32_t buffer_get_data(ssh_buffer buffer, void *data, uint32_t requestedlen);
00092 /* buffer_get_ssh_string() is an exception. if the String read is too large or invalid, it will answer NULL. */
00093 ssh_string buffer_get_ssh_string(ssh_buffer buffer);
00094 /* gets a string out of a SSH-1 mpint */
00095 ssh_string buffer_get_mpint(ssh_buffer buffer);
00096 /* buffer_pass_bytes acts as if len bytes have been read (used for padding) */
00097 uint32_t buffer_pass_bytes_end(ssh_buffer buffer, uint32_t len);
00098 uint32_t buffer_pass_bytes(ssh_buffer buffer, uint32_t len);
00099 
00100 #endif /* BUFFER_H_ */