mxml-string.c 11.1 KB
Newer Older
1
/*
Michael Sweet's avatar
Michael Sweet committed
2
 * String functions for Mini-XML, a small XML file parsing library.
3
 *
4
 * Copyright 2003-2018 by Michael R Sweet.
5
 *
6 7 8 9 10
 * These coded instructions, statements, and computer programs are the
 * property of Michael R Sweet and are protected by Federal copyright
 * law.  Distribution and use rights are outlined in the file "COPYING"
 * which should have been included with this file.  If this file is
 * missing or damaged, see the license at:
11
 *
Michael Sweet's avatar
Michael Sweet committed
12
 *     https://michaelrsweet.github.io/mxml
13 14 15 16 17 18 19 20 21
 */

/*
 * Include necessary headers...
 */

#include "config.h"


22 23 24 25 26 27 28 29 30
/*
 * The va_copy macro is part of C99, but many compilers don't implement it.
 * Provide a "direct assignment" implmentation when va_copy isn't defined...
 */

#ifndef va_copy
#  ifdef __va_copy
#    define va_copy(dst,src) __va_copy(dst,src)
#  else
31
#    define va_copy(dst,src) memcpy(&dst, &src, sizeof(va_list))
32 33 34 35
#  endif /* __va_copy */
#endif /* va_copy */


Michael R Sweet's avatar
Michael R Sweet committed
36
#ifndef HAVE_SNPRINTF
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * '_mxml_snprintf()' - Format a string.
 */

int					/* O - Number of bytes formatted */
_mxml_snprintf(char       *buffer,	/* I - Output buffer */
               size_t     bufsize,	/* I - Size of output buffer */
	       const char *format,	/* I - Printf-style format string */
	       ...)			/* I - Additional arguments as needed */
{
  va_list	ap;			/* Argument list */
  int		bytes;			/* Number of bytes formatted */


  va_start(ap, format);
  bytes = vsnprintf(buffer, bufsize, format, ap);
  va_end(ap);

  return (bytes);
}
Michael R Sweet's avatar
Michael R Sweet committed
57
#endif /* !HAVE_SNPRINTF */
58 59


60
/*
Michael R Sweet's avatar
Michael R Sweet committed
61
 * '_mxml_strdup()' - Duplicate a string.
62 63 64
 */

#ifndef HAVE_STRDUP
65
char *					/* O - New string pointer */
Michael R Sweet's avatar
Michael R Sweet committed
66
_mxml_strdup(const char *s)		/* I - String to duplicate */
67
{
68
  char	*t;				/* New string pointer */
69 70 71 72 73 74 75 76 77 78 79 80 81 82


  if (s == NULL)
    return (NULL);

  if ((t = malloc(strlen(s) + 1)) == NULL)
    return (NULL);

  return (strcpy(t, s));
}
#endif /* !HAVE_STRDUP */


/*
Michael R Sweet's avatar
Michael R Sweet committed
83
 * '_mxml_strdupf()' - Format and duplicate a string.
84 85 86
 */

char *					/* O - New string pointer */
Michael R Sweet's avatar
Michael R Sweet committed
87 88
_mxml_strdupf(const char *format,	/* I - Printf-style format string */
              ...)			/* I - Additional arguments as needed */
89 90 91 92 93 94 95 96 97 98 99
{
  va_list	ap;			/* Pointer to additional arguments */
  char		*s;			/* Pointer to formatted string */


 /*
  * Get a pointer to the additional arguments, format the string,
  * and return it...
  */

  va_start(ap, format);
100
#ifdef HAVE_VASPRINTF
101 102
  if (vasprintf(&s, format, ap) < 0)
    s = NULL;
103
#else
Michael R Sweet's avatar
Michael R Sweet committed
104
  s = _mxml_vstrdupf(format, ap);
105
#endif /* HAVE_VASPRINTF */
106 107 108 109 110 111
  va_end(ap);

  return (s);
}


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
#ifndef HAVE_STRLCAT
/*
 * '_mxml_strlcat()' - Safely concatenate a string.
 */

size_t					/* O - Number of bytes copied */
_mxml_strlcat(char       *dst,		/* I - Destination buffer */
              const char *src,		/* I - Source string */
              size_t     dstsize)	/* I - Size of destinatipon buffer */
{
  size_t	srclen;			/* Length of source string */
  size_t	dstlen;			/* Length of destination string */


 /*
  * Figure out how much room is left...
  */

  dstlen = strlen(dst);

  if (dstsize <= (dstlen + 1))
    return (dstlen);		        /* No room, return immediately... */

  dstsize -= dstlen + 1;

 /*
  * Figure out how much room is needed...
  */

  srclen = strlen(src);

 /*
  * Copy the appropriate amount...
  */

  if (srclen > dstsize)
    srclen = dstsize;

  memmove(dst + dstlen, src, srclen);
  dst[dstlen + srclen] = '\0';

  return (dstlen + srclen);
}
#endif /* !HAVE_STRLCAT */


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
#ifndef HAVE_STRLCPY
/*
 * '_mxml_strlcpy()' - Safely copy a string.
 */

size_t					/* O - Number of bytes copied */
_mxml_strlcpy(char       *dst,		/* I - Destination buffer */
              const char *src,		/* I - Source string */
              size_t     dstsize)	/* I - Size of destinatipon buffer */
{
  size_t        srclen;                 /* Length of source string */


 /*
  * Figure out how much room is needed...
  */

  dstsize --;

  srclen = strlen(src);

 /*
  * Copy the appropriate amount...
  */

  if (srclen > dstsize)
    srclen = dstsize;

  memmove(dst, src, srclen);
  dst[srclen] = '\0';

  return (srclen);
}
#endif /* !HAVE_STRLCPY */


194 195
#ifndef HAVE_VSNPRINTF
/*
Michael R Sweet's avatar
Michael R Sweet committed
196
 * '_mxml_vsnprintf()' - Format a string into a fixed size buffer.
197 198 199
 */

int					/* O - Number of bytes formatted */
Michael R Sweet's avatar
Michael R Sweet committed
200 201 202 203
_mxml_vsnprintf(char       *buffer,	/* O - Output buffer */
                size_t     bufsize,	/* O - Size of output buffer */
		const char *format,	/* I - Printf-style format string */
 		va_list    ap)		/* I - Pointer to additional arguments */
204 205 206 207 208 209 210 211 212
{
  char		*bufptr,		/* Pointer to position in buffer */
		*bufend,		/* Pointer to end of buffer */
		sign,			/* Sign of format width */
		size,			/* Size character (h, l, L) */
		type;			/* Format type character */
  int		width,			/* Width of field */
		prec;			/* Number of characters of precision */
  char		tformat[100],		/* Temporary format string for sprintf() */
213
		*tptr,			/* Pointer into temporary format */
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		temp[1024];		/* Buffer for formatted numbers */
  char		*s;			/* Pointer to string */
  int		slen;			/* Length of string */
  int		bytes;			/* Total number of bytes needed */


 /*
  * Loop through the format string, formatting as needed...
  */

  bufptr = buffer;
  bufend = buffer + bufsize - 1;
  bytes  = 0;

  while (*format)
  {
    if (*format == '%')
    {
232 233
      tptr = tformat;
      *tptr++ = *format++;
234 235 236

      if (*format == '%')
      {
Michael R Sweet's avatar
Michael R Sweet committed
237 238
        if (bufptr && bufptr < bufend)
          *bufptr++ = *format;
239 240
        bytes ++;
        format ++;
241 242 243
	continue;
      }
      else if (strchr(" -+#\'", *format))
244 245
      {
        *tptr++ = *format;
246
        sign = *format++;
247
      }
248 249 250
      else
        sign = 0;

251 252
      if (*format == '*')
      {
Michael R Sweet's avatar
Michael R Sweet committed
253 254 255 256
       /*
        * Get width from argument...
	*/

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	format ++;
	width = va_arg(ap, int);

	snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", width);
	tptr += strlen(tptr);
      }
      else
      {
	width = 0;

	while (isdigit(*format & 255))
	{
	  if (tptr < (tformat + sizeof(tformat) - 1))
	    *tptr++ = *format;

	  width = width * 10 + *format++ - '0';
	}
      }
275 276 277

      if (*format == '.')
      {
278 279 280
	if (tptr < (tformat + sizeof(tformat) - 1))
	  *tptr++ = *format;

281 282
        format ++;

283 284
        if (*format == '*')
	{
Michael R Sweet's avatar
Michael R Sweet committed
285 286 287 288
         /*
	  * Get precision from argument...
	  */

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	  format ++;
	  prec = va_arg(ap, int);

	  snprintf(tptr, sizeof(tformat) - (tptr - tformat), "%d", prec);
	  tptr += strlen(tptr);
	}
	else
	{
	  prec = 0;

	  while (isdigit(*format & 255))
	  {
	    if (tptr < (tformat + sizeof(tformat) - 1))
	      *tptr++ = *format;

	    prec = prec * 10 + *format++ - '0';
	  }
	}
307 308 309 310 311 312 313
      }
      else
        prec = -1;

      if (*format == 'l' && format[1] == 'l')
      {
        size = 'L';
314 315 316 317 318 319 320

	if (tptr < (tformat + sizeof(tformat) - 2))
	{
	  *tptr++ = 'l';
	  *tptr++ = 'l';
	}

321 322 323
	format += 2;
      }
      else if (*format == 'h' || *format == 'l' || *format == 'L')
324 325 326 327
      {
	if (tptr < (tformat + sizeof(tformat) - 1))
	  *tptr++ = *format;

328
        size = *format++;
329
      }
330 331 332 333

      if (!*format)
        break;

334 335 336 337 338
      if (tptr < (tformat + sizeof(tformat) - 1))
        *tptr++ = *format;

      type  = *format++;
      *tptr = '\0';
339 340 341 342 343 344 345 346

      switch (type)
      {
	case 'E' : /* Floating point formats */
	case 'G' :
	case 'e' :
	case 'f' :
	case 'g' :
347
	    if ((width + 2) > sizeof(temp))
348 349 350 351 352 353 354 355 356 357
	      break;

	    sprintf(temp, tformat, va_arg(ap, double));

            bytes += strlen(temp);

            if (bufptr)
	    {
	      if ((bufptr + strlen(temp)) > bufend)
	      {
358
		strncpy(bufptr, temp, (size_t)(bufend - bufptr));
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
		bufptr = bufend;
	      }
	      else
	      {
		strcpy(bufptr, temp);
		bufptr += strlen(temp);
	      }
	    }
	    break;

        case 'B' : /* Integer formats */
	case 'X' :
	case 'b' :
        case 'd' :
	case 'i' :
	case 'o' :
	case 'u' :
	case 'x' :
377
	    if ((width + 2) > sizeof(temp))
378 379
	      break;

380 381 382 383 384
#ifdef HAVE_LONG_LONG
	    if (size == 'L')
	      sprintf(temp, tformat, va_arg(ap, long long));
	    else
#endif /* HAVE_LONG_LONG */
385 386 387 388 389 390 391 392
	    sprintf(temp, tformat, va_arg(ap, int));

            bytes += strlen(temp);

	    if (bufptr)
	    {
	      if ((bufptr + strlen(temp)) > bufend)
	      {
393
		strncpy(bufptr, temp, (size_t)(bufend - bufptr));
394 395 396 397 398 399 400 401 402
		bufptr = bufend;
	      }
	      else
	      {
		strcpy(bufptr, temp);
		bufptr += strlen(temp);
	      }
	    }
	    break;
403

404
	case 'p' : /* Pointer value */
405
	    if ((width + 2) > sizeof(temp))
406 407 408 409 410 411 412 413 414 415
	      break;

	    sprintf(temp, tformat, va_arg(ap, void *));

            bytes += strlen(temp);

	    if (bufptr)
	    {
	      if ((bufptr + strlen(temp)) > bufend)
	      {
416
		strncpy(bufptr, temp, (size_t)(bufend - bufptr));
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
		bufptr = bufend;
	      }
	      else
	      {
		strcpy(bufptr, temp);
		bufptr += strlen(temp);
	      }
	    }
	    break;

        case 'c' : /* Character or character array */
	    bytes += width;

	    if (bufptr)
	    {
	      if (width <= 1)
433
	        *bufptr++ = va_arg(ap, int);
434 435 436
	      else
	      {
		if ((bufptr + width) > bufend)
437
		  width = bufend - bufptr;
438

439
		memcpy(bufptr, va_arg(ap, char *), (size_t)width);
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
		bufptr += width;
	      }
	    }
	    break;

	case 's' : /* String */
	    if ((s = va_arg(ap, char *)) == NULL)
	      s = "(null)";

	    slen = strlen(s);
	    if (slen > width && prec != width)
	      width = slen;

            bytes += width;

	    if (bufptr)
	    {
	      if ((bufptr + width) > bufend)
458
	        width = bufend - bufptr;
459 460

              if (slen > width)
461
	        slen = width;
462 463 464

	      if (sign == '-')
	      {
465 466
		strncpy(bufptr, s, (size_t)slen);
		memset(bufptr + slen, ' ', (size_t)(width - slen));
467 468 469
	      }
	      else
	      {
470 471
		memset(bufptr, ' ', (size_t)(width - slen));
		strncpy(bufptr + width - slen, s, (size_t)slen);
472 473 474 475 476 477 478
	      }

	      bufptr += width;
	    }
	    break;

	case 'n' : /* Output number of chars so far */
479
	    *(va_arg(ap, int *)) = bytes;
480 481 482 483 484 485 486 487
	    break;
      }
    }
    else
    {
      bytes ++;

      if (bufptr && bufptr < bufend)
488
        *bufptr++ = *format;
489 490

      format ++;
491 492 493 494 495 496 497 498 499 500 501 502 503 504
    }
  }

 /*
  * Nul-terminate the string and return the number of characters needed.
  */

  *bufptr = '\0';

  return (bytes);
}
#endif /* !HAVE_VSNPRINTF */


505 506 507 508 509 510 511 512
/*
 * '_mxml_vstrdupf()' - Format and duplicate a string.
 */

char *					/* O - New string pointer */
_mxml_vstrdupf(const char *format,	/* I - Printf-style format string */
               va_list    ap)		/* I - Pointer to additional arguments */
{
513 514 515
#ifdef HAVE_VASPRINTF
  char		*s;			/* String */

516 517
  if (vasprintf(&s, format, ap) < 0)
    s = NULL;
518 519 520 521

  return (s);

#else
522 523 524
  int		bytes;			/* Number of bytes required */
  char		*buffer,		/* String buffer */
		temp[256];		/* Small buffer for first vsnprintf */
525 526 527 528 529 530 531


 /*
  * First format with a tiny buffer; this will tell us how many bytes are
  * needed...
  */

532 533 534 535 536 537
#  ifdef WIN32
  bytes = _vscprintf(format, ap);

#  else
  va_list	apcopy;			/* Copy of argument list */

538 539
  va_copy(apcopy, ap);
  bytes = vsnprintf(temp, sizeof(temp), format, apcopy);
540
#  endif /* WIN32 */
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

  if (bytes < sizeof(temp))
  {
   /*
    * Hey, the formatted string fits in the tiny buffer, so just dup that...
    */

    return (strdup(temp));
  }

 /*
  * Allocate memory for the whole thing and reformat to the new, larger
  * buffer...
  */

  if ((buffer = calloc(1, bytes + 1)) != NULL)
    vsnprintf(buffer, bytes + 1, format, ap);

 /*
  * Return the new string...
  */

  return (buffer);
564
#endif /* HAVE_VASPRINTF */
565
}