string.c   string.c 
skipping to change at line 25 skipping to change at line 25
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details. * License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with the SSH Library; see the file COPYING. If not, write to * along with the SSH Library; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA. * MA 02111-1307, USA.
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#ifndef _WIN32 #ifndef _WIN32
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#include "libssh/priv.h" #include "libssh/priv.h"
#include "libssh/string.h"
/** \defgroup ssh_string SSH Strings /** \defgroup ssh_string SSH Strings
* \brief string manipulations * \brief string manipulations
*/ */
/** \addtogroup ssh_string /** \addtogroup ssh_string
* @{ */ * @{ */
/** /**
* \brief Creates a new SSH String object * \brief Creates a new SSH String object
* \param size size of the string * \param size size of the string
* \return the newly allocated string * \return the newly allocated string
*/ */
struct string_struct *string_new(size_t size) { struct ssh_string_struct *string_new(size_t size) {
struct string_struct *str = NULL; struct ssh_string_struct *str = NULL;
str = malloc(size + 4); str = malloc(size + 4);
if (str == NULL) { if (str == NULL) {
return NULL; return NULL;
} }
str->size = htonl(size); str->size = htonl(size);
return str; return str;
} }
skipping to change at line 68 skipping to change at line 67
* @brief Fill a string with given data. The string should be big enough. * @brief Fill a string with given data. The string should be big enough.
* *
* @param s An allocated string to fill with data. * @param s An allocated string to fill with data.
* *
* @param data The data to fill the string with. * @param data The data to fill the string with.
* *
* @param len Size of data. * @param len Size of data.
* *
* @return 0 on success, < 0 on error. * @return 0 on success, < 0 on error.
*/ */
int string_fill(struct string_struct *s, const void *data, size_t len) { int string_fill(struct ssh_string_struct *s, const void *data, size_t len) {
if ((s == NULL) || (data == NULL) || if ((s == NULL) || (data == NULL) ||
(len == 0) || (len > s->size)) { (len == 0) || (len > s->size)) {
return -1; return -1;
} }
memcpy(s->string, data, len); memcpy(s->string, data, len);
return 0; return 0;
} }
/** /**
* \brief Creates a ssh stream using a C string * \brief Creates a ssh stream using a C string
* \param what source 0-terminated C string * \param what source 0-terminated C string
* \return the newly allocated string. * \return the newly allocated string.
* \warning The nul byte is not copied nor counted in the ouput string. * \warning The nul byte is not copied nor counted in the ouput string.
*/ */
struct string_struct *string_from_char(const char *what) { struct ssh_string_struct *string_from_char(const char *what) {
struct string_struct *ptr = NULL; struct ssh_string_struct *ptr = NULL;
size_t len = strlen(what); size_t len = strlen(what);
ptr = malloc(4 + len); ptr = malloc(4 + len);
if (ptr == NULL) { if (ptr == NULL) {
return NULL; return NULL;
} }
ptr->size = htonl(len); ptr->size = htonl(len);
memcpy(ptr->string, what, len); memcpy(ptr->string, what, len);
return ptr; return ptr;
} }
/** /**
* \brief returns the size of a SSH string * \brief returns the size of a SSH string
* \param str the input SSH string * \param s the input SSH string
* \return size of the content of str, 0 on error * \return size of the content of str, 0 on error
*/ */
size_t string_len(struct string_struct *s) { size_t string_len(struct ssh_string_struct *s) {
if (s == NULL) { if (s == NULL) {
return ntohl(0); return ntohl(0);
} }
return ntohl(s->size); return ntohl(s->size);
} }
/** /**
* \brief convert a SSH string to a C nul-terminated string * \brief convert a SSH string to a C nul-terminated string
* \param str the input SSH string * \param s the input SSH string
* \return a malloc'ed string pointer. * \return a malloc'ed string pointer.
* \warning If the input SSH string contains zeroes, some parts of * \warning If the input SSH string contains zeroes, some parts of
* the output string may not be readable with regular libc functions. * the output string may not be readable with regular libc functions.
*/ */
char *string_to_char(struct string_struct *s) { char *string_to_char(struct ssh_string_struct *s) {
size_t len = ntohl(s->size) + 1; size_t len = ntohl(s->size) + 1;
char *new = malloc(len); char *new = malloc(len);
if (new == NULL) { if (new == NULL) {
return NULL; return NULL;
} }
memcpy(new, s->string, len - 1); memcpy(new, s->string, len - 1);
new[len - 1] = '\0'; new[len - 1] = '\0';
return new; return new;
} }
/** /**
* @brief Copy a string, return a newly allocated string. The caller has to * @brief Copy a string, return a newly allocated string. The caller has to
* free the string. * free the string.
* *
* @param s String to copy. * @param s String to copy.
* *
* @return Newly allocated copy of the string, NULL on error. * @return Newly allocated copy of the string, NULL on error.
*/ */
struct string_struct *string_copy(struct string_struct *s) { struct ssh_string_struct *string_copy(struct ssh_string_struct *s) {
struct string_struct *new = malloc(ntohl(s->size) + 4); struct ssh_string_struct *new = malloc(ntohl(s->size) + 4);
if (new == NULL) { if (new == NULL) {
return NULL; return NULL;
} }
new->size = s->size; new->size = s->size;
memcpy(new->string, s->string, ntohl(s->size)); memcpy(new->string, s->string, ntohl(s->size));
return new; return new;
} }
/** \brief destroy data in a string so it couldn't appear in a core dump /** \brief destroy data in a string so it couldn't appear in a core dump
* \param s string to burn * \param s string to burn
*/ */
void string_burn(struct string_struct *s) { void string_burn(struct ssh_string_struct *s) {
if (s == NULL) { if (s == NULL) {
return; return;
} }
memset(s->string, 'X', string_len(s)); memset(s->string, 'X', string_len(s));
} }
/** /**
* @brief Get the payload of the string. * @brief Get the payload of the string.
* *
* @param s The string to get the data from. * @param s The string to get the data from.
* *
* @return Return the data of the string or NULL on error. * @return Return the data of the string or NULL on error.
*/ */
void *string_data(struct string_struct *s) { void *string_data(struct ssh_string_struct *s) {
if (s == NULL) { if (s == NULL) {
return NULL; return NULL;
} }
return s->string; return s->string;
} }
/** /**
* \brief deallocate a STRING object * \brief deallocate a STRING object
* \param s String to delete * \param s String to delete
*/ */
void string_free(struct string_struct *s) { void string_free(struct ssh_string_struct *s) {
SAFE_FREE(s); SAFE_FREE(s);
} }
/** @} */ /** @} */
/* vim: set ts=2 sw=2 et cindent: */ /* vim: set ts=2 sw=2 et cindent: */
 End of changes. 13 change blocks. 
16 lines changed or deleted 15 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/