1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <stdarg.h>
#include "mozilla/Assertions.h"
#include "cpr_types.h"
#include "cpr_string.h"
#include "cpr_strings.h"
/* From cpr_stdlib.h */
#ifdef CPR_STRING_USE_FALLIBLE_MALLOC
#define cpr_malloc(a) malloc(a)
#define cpr_calloc(a, b) calloc(a, b)
#define cpr_realloc(a, b) realloc(a, b)
#define cpr_free(a) free(a)
#else
#include "mozilla/mozalloc.h"
#define cpr_malloc(a) moz_xmalloc(a)
#define cpr_calloc(a, b) moz_xcalloc(a, b)
#define cpr_realloc(a, b) moz_xrealloc(a, b)
#define cpr_free(a) free(a)
#endif
/**
* sstrncpy
*
* This is Cisco's *safe* version of strncpy. The string will always
* be NUL terminated (which is not ANSI compliant).
*
* Parameters: s1 - first string
* s2 - second string
* max - maximum length in octets to concat.
*
* Return: Pointer to the *end* of the string
*
* Remarks: Modified to be explicitly safe for all inputs.
* Also return the number of characters copied excluding the
* NUL terminator vs. the original string s1. This simplifies
* code where sstrncat functions follow.
*/
unsigned long
sstrncpy (char *dst, const char *src, unsigned long max)
{
unsigned long cnt = 0;
if (dst == NULL) {
return 0;
}
if (src) {
while ((max-- > 1) && (*src)) {
*dst = *src;
dst++;
src++;
cnt++;
}
}
#if defined(CPR_SSTRNCPY_PAD)
/*
* To be equivalent to the TI compiler version
* v2.01, SSTRNCPY_PAD needs to be defined
*/
while (max-- > 1) {
*dst = '\0';
dst++;
}
#endif
*dst = '\0';
return cnt;
}
/**
* sstrncat
*
* This is Cisco's *safe* version of strncat. The string will always
* be NUL terminated (which is not ANSI compliant).
*
* Parameters: s1 - first string
* s2 - second string
* max - maximum length in octets to concatenate
*
* Return: Pointer to the *end* of the string
*
* Remarks: Modified to be explicitly safe for all inputs.
* Also return the end vs. the beginning of the string s1
* which is useful for multiple sstrncat calls.
*/
char *
sstrncat (char *s1, const char *s2, unsigned long max)
{
if (s1 == NULL)
return (char *) NULL;
while (*s1)
s1++;
if (s2) {
while ((max-- > 1) && (*s2)) {
*s1 = *s2;
s1++;
s2++;
}
}
*s1 = '\0';
return s1;
}
/*
* flex_string
*/
/*
* flex_string_init
*
* Not thread-safe
*/
void flex_string_init(flex_string *fs) {
fs->buffer_length = FLEX_STRING_CHUNK_SIZE;
fs->string_length = 0;
fs->buffer = cpr_malloc(fs->buffer_length);
fs->buffer[0] = '\0';
}
/*
* flex_string_free
*
* Not thread-safe
*/
void flex_string_free(flex_string *fs) {
fs->buffer_length = 0;
fs->string_length = 0;
cpr_free(fs->buffer);
fs->buffer = NULL;
}
/* For sanity check before alloc */
#define FLEX_STRING_MAX_SIZE (10 * 1024 * 1024) /* 10MB */
/*
* flex_string_check_alloc
*
* Allocate enough chunks to hold the new minimum size.
*
* Not thread-safe
*/
void flex_string_check_alloc(flex_string *fs, size_t new_min_length) {
if (new_min_length > fs->buffer_length) {
/* Oversize, allocate more */
/* Sanity check on allocation size */
if (new_min_length > FLEX_STRING_MAX_SIZE) {
MOZ_CRASH();
}
/* Alloc to nearest chunk */
fs->buffer_length = (((new_min_length - 1) / FLEX_STRING_CHUNK_SIZE) + 1) * FLEX_STRING_CHUNK_SIZE;
fs->buffer = cpr_realloc(fs->buffer, fs->buffer_length);
}
}
/*
* flex_string_append
*
* Not thread-safe
*/
void flex_string_append(flex_string *fs, const char *more) {
fs->string_length += strlen(more);
flex_string_check_alloc(fs, fs->string_length + 1);
sstrncat(fs->buffer, more, fs->buffer_length - strlen(fs->buffer));
}
/*
* va_copy is part of the C99 spec but MSVC doesn't have it.
*/
#ifndef va_copy
#define va_copy(d,s) ((d) = (s))
#endif
/*
* flex_string_vsprintf
*
* Not thread-safe
*/
void flex_string_vsprintf(flex_string *fs, const char *format, va_list original_ap) {
va_list ap;
int vsnprintf_result;
va_copy(ap, original_ap);
vsnprintf_result = vsnprintf(fs->buffer + fs->string_length, fs->buffer_length - fs->string_length, format, ap);
va_end(ap);
/* Special case just for Windows where vsnprintf is broken
and returns -1 if buffer too large unless you size it 0. */
if (vsnprintf_result < 0) {
va_copy(ap, original_ap);
vsnprintf_result = vsnprintf(NULL, 0, format, ap);
va_end(ap);
}
if (fs->string_length + vsnprintf_result >= fs->buffer_length) {
/* Buffer overflow, resize */
flex_string_check_alloc(fs, fs->string_length + vsnprintf_result + 1);
/* Try again with new buffer */
va_copy(ap, original_ap);
vsnprintf_result = vsnprintf(fs->buffer + fs->string_length, fs->buffer_length - fs->string_length, format, ap);
va_end(ap);
MOZ_ASSERT(vsnprintf_result > 0 &&
(size_t)vsnprintf_result < (fs->buffer_length - fs->string_length));
}
if (vsnprintf_result > 0) {
fs->string_length += vsnprintf_result;
}
}
/*
* flex_string_sprintf
*
* Not thread-safe
*/
void flex_string_sprintf(flex_string *fs, const char *format, ...) {
va_list ap;
va_start(ap, format);
flex_string_vsprintf(fs, format, ap);
va_end(ap);
}
/* From cpr_linux_string.c */
/**
* cpr_strdup
*
* @brief The CPR wrapper for strdup
* The cpr_strdup shall return a pointer to a new string, which is a duplicate
* of the string pointed to by "str" argument. A null pointer is returned if the
* new string cannot be created.
*
* @param[in] str - The string that needs to be duplicated
*
* @return The duplicated string or NULL in case of no memory
*
*/
char *
cpr_strdup (const char *str)
{
char *dup;
size_t len;
if (!str) {
return (char *) NULL;
}
len = strlen(str);
if (len == 0) {
return (char *) NULL;
}
len++;
dup = cpr_malloc(len * sizeof(char));
if (!dup) {
return (char *) NULL;
}
(void) memcpy(dup, str, len);
return dup;
}
|