~ubuntu-branches/ubuntu/wily/trafficserver/wily

« back to all changes in this revision

Viewing changes to lib/ts/ink_string.cc

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2012-12-17 22:28:16 UTC
  • mfrom: (5.1.8 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20121217222816-7xwjsx5k76zkb63d
Tags: 3.2.0-1ubuntu1
* Revert FreeBSD strerror_r() fixes that give errors with glibc 2.16.
* Apply patch from Konstantinos Margaritis to define barriers on ARM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/** @file
2
2
 
3
 
  A brief file description
 
3
  String and text processing routines for libts
4
4
 
5
5
  @section license License
6
6
 
21
21
  limitations under the License.
22
22
 */
23
23
 
24
 
/****************************************************************************
25
 
 
26
 
  ink_string.c
27
 
 
28
 
  String and text processing routines for libts
29
 
 
30
 
 ****************************************************************************/
31
 
 
32
24
#include "libts.h"   /* MAGIC_EDITING_TAG */
33
25
 
34
26
#include <assert.h>
36
28
#include <stdlib.h>
37
29
#include <string.h>
38
30
 
39
 
 
40
31
#define INK_MAX_STRING_ARRAY_SIZE 128
41
32
 
 
33
/*-------------------------------------------------------------------------
 
34
  -------------------------------------------------------------------------*/
 
35
char *
 
36
ink_memcpy_until_char(char *dst, char *src, unsigned int n, unsigned char c)
 
37
{
 
38
  unsigned int i = 0;
 
39
  for (; ((i < n) && (((unsigned char) src[i]) != c)); i++)
 
40
    dst[i] = src[i];
 
41
  return &src[i];
 
42
}
 
43
 
42
44
/*---------------------------------------------------------------------------*
43
45
 
44
46
  char *ink_strncpy(char *dest, char *src, int n)
63
65
  return (dest);
64
66
}                               /* End ink_strncpy */
65
67
 
 
68
/*---------------------------------------------------------------------------*
 
69
 
 
70
  char *ink_strncat(char *dest, char *src, int n)
 
71
 
 
72
  This routine is a safer version of strncat which always NUL terminates
 
73
  the destination string.  Note that this routine has the SAME semantics
 
74
  as strncat, such as concatinating exactly n bytes, padding dest with NULs
 
75
  is necessary.  Use ink_string_copy for a non-padding version.
 
76
 
 
77
 *---------------------------------------------------------------------------*/
 
78
 
 
79
char *
 
80
ink_strncat(char *dest, const char *src, int n)
 
81
{
 
82
  if (likely(src && dest)) {
 
83
    if (n > 1)
 
84
      strncat(dest, src, (n - 1));
 
85
    if (n > 0)
 
86
      dest[n - 1] = '\0';
 
87
  }
 
88
 
 
89
  return (dest);
 
90
}                               /* End ink_strncat */
66
91
 
67
92
/*---------------------------------------------------------------------------*
68
93
 
189
214
 
190
215
/*---------------------------------------------------------------------------*
191
216
 
192
 
  char *ink_string_duplicate(char *ptr)
193
 
 
194
 
  This routine allocates memory for the string <ptr>, and copies the string
195
 
  into the new buffer.  The pointer to the new buffer is returned.
196
 
 
197
 
 *---------------------------------------------------------------------------*/
198
 
 
199
 
char *
200
 
ink_string_duplicate(char *ptr)
201
 
{
202
 
  char *n = NULL;
203
 
 
204
 
  if (likely(ptr)) {
205
 
    const size_t nSize = strlen(ptr) + 1;
206
 
    n = (char *) ink_malloc(nSize);
207
 
    ink_strncpy(n, ptr, nSize);
208
 
  }
209
 
  return (n);
210
 
}                               /* End ink_string_duplicate */
211
 
 
212
 
 
213
 
/*---------------------------------------------------------------------------*
214
 
 
215
217
  char *ink_string_find_dotted_extension(char *str, char *ext, int max_ext_len)
216
218
 
217
219
  This routine takes a string <str>, copies the period-separated extension to
242
244
  return (p);
243
245
}                               /* End ink_string_find_dotted_extension */
244
246
 
245
 
/*---------------------------------------------------------------------------*
246
 
 
247
 
  char *ink_string_mpath(int nstrings, char *str1, bool free1,
248
 
    char *str2, bool free2, ...);
249
 
 
250
 
  This routine joins multiple path components together to make
251
 
  a new path.  Each component can optionally start with a / in which
252
 
  case all the preceeding components are ignored.
253
 
 
254
 
  Each component can optionally be free()d.
255
 
 
256
 
  Space is malloc()d to hold the resulting path.
257
 
 
258
 
 *---------------------------------------------------------------------------*/
259
 
 
260
 
char *
261
 
ink_string_mpath(int nstrings, ...)
262
 
{
263
 
  va_list ap;
264
 
 
265
 
  char *e[INK_MAX_STRING_ARRAY_SIZE];
266
 
  bool f[INK_MAX_STRING_ARRAY_SIZE];
267
 
  size_t s[INK_MAX_STRING_ARRAY_SIZE];
268
 
  int slash = 0;
269
 
  size_t ts = 0;
270
 
  char *ns = NULL;
271
 
  char *p;
272
 
  int i;
273
 
 
274
 
  if (likely(nstrings < INK_MAX_STRING_ARRAY_SIZE)) {
275
 
    va_start(ap, nstrings);
276
 
 
277
 
    for (i = 0; i < nstrings; i++) {
278
 
      e[i] = va_arg(ap, char *);
279
 
      f[i] = va_arg(ap, int);
280
 
    }
281
 
 
282
 
    for (i = nstrings - 1; i >= 0; i--) {
283
 
      if (!e[i])
284
 
        continue;
285
 
      s[i] = strlen(e[i]);
286
 
      ts += s[i] + 1;
287
 
      if (e[i][0] == '/') {
288
 
        slash = i;
289
 
        break;
290
 
      }
291
 
    }
292
 
    if ((slash == nstrings - 1) && f[slash]) {
293
 
      for (i = 0; i < nstrings - 1; i++) {
294
 
        if (f[i])
295
 
          xfree(e[i]);
296
 
      }
297
 
      va_end(ap);
298
 
      return e[slash];
299
 
    } else {
300
 
      const size_t nsSize = ts + 1;
301
 
      p = (ns = (char *) xmalloc(nsSize));
302
 
      ink_assert(ns);
303
 
      for (i = slash; i < nstrings - 1; i++) {
304
 
        ink_strncpy(p, e[i], (nsSize - (p - ns)));
305
 
        p += s[i];
306
 
        *p++ = '/';
307
 
      }
308
 
      ink_strncpy(p, e[nstrings - 1], (nsSize - (p - ns)));
309
 
    }
310
 
    for (i = 0; i < nstrings; i++) {
311
 
      if (f[i])
312
 
        xfree(e[i]);
313
 
    }
314
 
    va_end(ap);
315
 
  }
316
 
  return ns;
317
 
}
318
 
 
319
 
/*---------------------------------------------------------------------------*
320
 
 
321
 
  char *ink_string_mcopy(char *source);
322
 
 
323
 
  This simply makes a copy of a string into freshly malloc()ed space.
324
 
 
325
 
 *---------------------------------------------------------------------------*/
326
 
 
327
 
char *
328
 
ink_string_mcopy(char *source)
329
 
{
330
 
  char *n = NULL;
331
 
 
332
 
  if (likely(source)) {
333
 
    const size_t nSize = strlen(source) + 1;
334
 
    n = (char *) xmalloc(nSize);
335
 
    ink_strncpy(n, source, nSize);
336
 
  }
337
 
  return n;
338
 
}
339
 
 
340
 
/*---------------------------------------------------------------------------*
341
 
 
342
 
  char *ink_string_mjoin(int nstrings, char *str1, bool free1,
343
 
    char *str2, bool free2, ...);
344
 
 
345
 
  This routine joins multiple strings components together to make
346
 
  a new string.  Each component can optionally be free()d.
347
 
 
348
 
  Space is malloc()d to hold the resulting path.
349
 
 
350
 
 *---------------------------------------------------------------------------*/
351
 
 
352
 
char *
353
 
ink_string_mjoin(int nstrings, ...)
354
 
{
355
 
  va_list ap;
356
 
 
357
 
  char *e[INK_MAX_STRING_ARRAY_SIZE];
358
 
  bool f[INK_MAX_STRING_ARRAY_SIZE];
359
 
 
360
 
  size_t s[INK_MAX_STRING_ARRAY_SIZE];
361
 
  int slash = 0;
362
 
  size_t ts = 0;
363
 
  char *ns = NULL;
364
 
  char *p;
365
 
  int i;
366
 
 
367
 
  if (likely(nstrings < INK_MAX_STRING_ARRAY_SIZE)) {
368
 
    va_start(ap, nstrings);
369
 
 
370
 
    for (i = 0; i < nstrings; i++) {
371
 
      e[i] = va_arg(ap, char *);
372
 
      f[i] = va_arg(ap, int);
373
 
      if (e[i]) {
374
 
        s[i] = strlen(e[i]);
375
 
        ts += s[i];
376
 
      }
377
 
    }
378
 
    const size_t nsSize = ts + 1;
379
 
    p = (ns = (char *) xmalloc(nsSize));
380
 
    for (i = slash; i < nstrings - 1; i++) {
381
 
      ink_strncpy(p, e[i], (nsSize - (p - ns)));
382
 
      p += s[i];
383
 
    }
384
 
    ink_strncpy(p, e[nstrings - 1], (nsSize - (p - ns)));
385
 
    for (i = 0; i < nstrings; i++) {
386
 
      if (f[i])
387
 
        xfree(e[i]);
388
 
    }
389
 
    va_end(ap);
390
 
  }
391
 
  return ns;
392
 
}
393
 
 
394
 
#if !TS_HAS_STRNDUP
395
 
char *
396
 
ink_strndup(const char *str, size_t n)
397
 
{
398
 
  char *cstr = NULL;
399
 
 
400
 
  if (likely(str)) {
401
 
    size_t len = strlen(str);
402
 
    cstr = (char *)xmalloc(len + 1);
403
 
    if (cstr == NULL)
404
 
      return (NULL);
405
 
    memcpy(cstr, str, len);
406
 
    cstr[len] = '\0';
407
 
  }
408
 
  return (cstr);
409
 
}
410
 
#endif
411
247
 
412
248
#if !TS_HAS_STRLCPY
413
249
size_t
500
336
 
501
337
  inbytesleft = inlen;
502
338
  outbytesleft = *outlen;
503
 
#if !defined(kfreebsd) && (defined(freebsd) || defined(solaris))
 
339
#if !defined(kfreebsd) && (defined(freebsd) || (!defined(__GNUC__) && defined(solaris)))
504
340
  if (iconv(ic, &in, &inbytesleft, &out, &outbytesleft) == (size_t) - 1)
505
341
#else
506
342
  if (iconv(ic, (char **) &in, &inbytesleft, &out, &outbytesleft) == (size_t) - 1)