2
* string.c - ssh string functions
4
* This file is part of the SSH Library
6
* Copyright (c) 2003-2008 by Aris Adamantiadis
8
* The SSH Library is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser General Public License as published by
10
* the Free Software Foundation; either version 2.1 of the License, or (at your
11
* option) any later version.
13
* The SSH Library is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16
* License for more details.
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with the SSH Library; see the file COPYING. If not, write to
20
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29
#include <arpa/inet.h>
32
#include "libssh/priv.h"
33
#include "libssh/string.h"
36
* @defgroup libssh_string The SSH string functions
39
* @brief String manipulations used in libssh.
45
* @brief Create a new SSH String object.
47
* @param[in] size The size of the string.
49
* @return The newly allocated string, NULL on error.
51
struct ssh_string_struct *ssh_string_new(size_t size) {
52
struct ssh_string_struct *str = NULL;
54
str = malloc(size + 4);
59
str->size = htonl(size);
64
* @brief Fill a string with given data. The string should be big enough.
66
* @param s An allocated string to fill with data.
68
* @param data The data to fill the string with.
70
* @param len Size of data.
72
* @return 0 on success, < 0 on error.
74
int ssh_string_fill(struct ssh_string_struct *s, const void *data, size_t len) {
75
if ((s == NULL) || (data == NULL) ||
76
(len == 0) || (len > s->size)) {
80
memcpy(s->string, data, len);
85
* @brief Create a ssh string using a C string
87
* @param[in] what The source 0-terminated C string.
89
* @return The newly allocated string, NULL on error with errno
92
* @note The nul byte is not copied nor counted in the ouput string.
94
struct ssh_string_struct *ssh_string_from_char(const char *what) {
95
struct ssh_string_struct *ptr;
105
ptr = malloc(4 + len);
109
ptr->size = htonl(len);
110
memcpy(ptr->string, what, len);
116
* @brief Return the size of a SSH string.
118
* @param[in] s The the input SSH string.
120
* @return The size of the content of the string, 0 on error.
122
size_t ssh_string_len(struct ssh_string_struct *s) {
127
return ntohl(s->size);
131
* @brief Convert a SSH string to a C nul-terminated string.
133
* @param[in] s The SSH input string.
135
* @return An allocated string pointer, NULL on error with errno
138
* @note If the input SSH string contains zeroes, some parts of the output
139
* string may not be readable with regular libc functions.
141
char *ssh_string_to_char(struct ssh_string_struct *s) {
144
if(s==NULL || s->string == NULL)
146
len = ntohl(s->size) + 1;
152
memcpy(new, s->string, len - 1);
158
* @brief Deallocate a char string object.
160
* @param[in] s The string to delete.
162
void ssh_string_free_char(char *s) {
167
* @brief Copy a string, return a newly allocated string. The caller has to
170
* @param[in] s String to copy.
172
* @return Newly allocated copy of the string, NULL on error.
174
struct ssh_string_struct *ssh_string_copy(struct ssh_string_struct *s) {
175
struct ssh_string_struct *new;
177
if(s == NULL || s->string == NULL) {
180
new = malloc(ntohl(s->size) + 4);
186
memcpy(new->string, s->string, ntohl(s->size));
192
* @brief Destroy the data in a string so it couldn't appear in a core dump.
194
* @param[in] s The string to burn.
196
void ssh_string_burn(struct ssh_string_struct *s) {
200
memset(s->string, 'X', ssh_string_len(s));
204
* @brief Get the payload of the string.
206
* @param s The string to get the data from.
208
* @return Return the data of the string or NULL on error.
210
void *ssh_string_data(struct ssh_string_struct *s) {
219
* @brief Deallocate a SSH string object.
221
* \param[in] s The SSH string to delete.
223
void ssh_string_free(struct ssh_string_struct *s) {
229
/* vim: set ts=4 sw=4 et cindent: */