~ubuntu-branches/ubuntu/hardy/exim4/hardy-proposed

« back to all changes in this revision

Viewing changes to src/string.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Haber
  • Date: 2005-07-02 06:08:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050702060834-qk17pd52kb9nt3bj
Tags: 4.52-1
* new upstream version 4.51. (mh)
  * adapt 70_remove_exim-users_references
  * remove 37_gnutlsparams
  * adapt 36_pcre
  * adapt 31_eximmanpage
* fix package priorities to have them in sync with override again. (mh)
* Fix error in nb (Norwegian) translation.
  Thanks to Helge Hafting. (mh). Closes: #315775
* Standards-Version: 3.6.2, no changes needed. (mh)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Cambridge: exim/exim-src/src/string.c,v 1.7 2005/06/20 10:04:55 ph10 Exp $ */
 
2
 
1
3
/*************************************************
2
4
*     Exim - an Internet mail transport agent    *
3
5
*************************************************/
4
6
 
5
 
/* Copyright (c) University of Cambridge 1995 - 2004 */
 
7
/* Copyright (c) University of Cambridge 1995 - 2005 */
6
8
/* See the file NOTICE for conditions of use and distribution. */
7
9
 
8
10
/* Miscellaneous string-handling functions. Some are not required for
334
336
*            Copy and save string                *
335
337
*************************************************/
336
338
 
337
 
/*
 
339
/* This function assumes that memcpy() is faster than strcpy().
 
340
 
338
341
Argument: string to copy
339
342
Returns:  copy of string in new store
340
343
*/
342
345
uschar *
343
346
string_copy(uschar *s)
344
347
{
345
 
uschar *ss = store_get(Ustrlen(s) + 1);
346
 
Ustrcpy(ss, s);
 
348
int len = Ustrlen(s) + 1;
 
349
uschar *ss = store_get(len);
 
350
memcpy(ss, s, len);
347
351
return ss;
348
352
}
349
353
 
353
357
*     Copy and save string in malloc'd store     *
354
358
*************************************************/
355
359
 
356
 
/*
 
360
/* This function assumes that memcpy() is faster than strcpy().
 
361
 
357
362
Argument: string to copy
358
363
Returns:  copy of string in new store
359
364
*/
361
366
uschar *
362
367
string_copy_malloc(uschar *s)
363
368
{
364
 
uschar *ss = store_malloc(Ustrlen(s) + 1);
365
 
Ustrcpy(ss, s);
 
369
int len = Ustrlen(s) + 1;
 
370
uschar *ss = store_malloc(len);
 
371
memcpy(ss, s, len);
366
372
return ss;
367
373
}
368
374
 
938
944
as a va_list item.
939
945
 
940
946
The formats are the usual printf() ones, with some omissions (never used) and
941
 
two additions for strings: %S forces lower case, %#s or %#S prints nothing for
942
 
a NULL string. Without the # "NULL" is printed (useful in debugging). There is
943
 
also the addition of %D, which inserts the date in the form used for
 
947
two additions for strings: %S forces lower case, and %#s or %#S prints nothing
 
948
for a NULL string. Without the # "NULL" is printed (useful in debugging). There
 
949
is also the addition of %D, which inserts the date in the form used for
944
950
datestamped log files.
945
951
 
946
952
Arguments:
967
973
BOOL
968
974
string_vformat(uschar *buffer, int buflen, char *format, va_list ap)
969
975
{
 
976
enum { L_NORMAL, L_SHORT, L_LONG, L_LONGLONG, L_LONGDOUBLE };
 
977
 
970
978
BOOL yield = TRUE;
971
979
int width, precision;
972
980
char *fp = format;             /* Deliberately not unsigned */
979
987
 
980
988
while (*fp != 0)
981
989
  {
 
990
  int length = L_NORMAL;
982
991
  int *nptr;
983
992
  int slen;
984
993
  char *null = "NULL";         /* ) These variables */
1032
1041
      }
1033
1042
    }
1034
1043
 
1035
 
  if (strchr("hlL", *fp) != NULL) fp++;
 
1044
  /* Skip over 'h', 'L', 'l', and 'll', remembering the item length */
 
1045
 
 
1046
  if (*fp == 'h')
 
1047
    { fp++; length = L_SHORT; }
 
1048
  else if (*fp == 'L')
 
1049
    { fp++; length = L_LONGDOUBLE; }
 
1050
  else if (*fp == 'l')
 
1051
    {
 
1052
    if (fp[1] == 'l')
 
1053
      {
 
1054
      fp += 2;
 
1055
      length = L_LONGLONG;
 
1056
      }
 
1057
    else
 
1058
      {
 
1059
      fp++;
 
1060
      length = L_LONG;
 
1061
      }
 
1062
    }
1036
1063
 
1037
1064
  /* Handle each specific format type. */
1038
1065
 
1048
1075
    case 'u':
1049
1076
    case 'x':
1050
1077
    case 'X':
1051
 
    if (p >= last - 12) { yield = FALSE; goto END_FORMAT; }
 
1078
    if (p >= last - ((length > L_LONG)? 24 : 12))
 
1079
      { yield = FALSE; goto END_FORMAT; }
1052
1080
    strncpy(newformat, item_start, fp - item_start);
1053
1081
    newformat[fp - item_start] = 0;
1054
 
    sprintf(CS p, newformat, va_arg(ap, int));
 
1082
 
 
1083
    /* Short int is promoted to int when passing through ..., so we must use
 
1084
    int for va_arg(). */
 
1085
 
 
1086
    switch(length)
 
1087
      {
 
1088
      case L_SHORT:
 
1089
      case L_NORMAL:   sprintf(CS p, newformat, va_arg(ap, int)); break;
 
1090
      case L_LONG:     sprintf(CS p, newformat, va_arg(ap, long int)); break;
 
1091
      case L_LONGLONG: sprintf(CS p, newformat, va_arg(ap, LONGLONG_T)); break;
 
1092
      }
1055
1093
    while (*p) p++;
1056
1094
    break;
1057
1095
 
1064
1102
    break;
1065
1103
 
1066
1104
    /* %f format is inherently insecure if the numbers that it may be
1067
 
    handed are unknown (e.g. 1e300). However, in Exim, the only use of %f
1068
 
    is for printing load averages, and these are actually stored as integers
1069
 
    (load average * 1000) so the size of the numbers is constrained. */
 
1105
    handed are unknown (e.g. 1e300). However, in Exim, %f is used for
 
1106
    printing load averages, and these are actually stored as integers
 
1107
    (load average * 1000) so the size of the numbers is constrained.
 
1108
    It is also used for formatting sending rates, where the simplicity
 
1109
    of the format prevents overflow. */
1070
1110
 
1071
1111
    case 'f':
1072
1112
    case 'e':
1077
1117
    if (p >= last - precision - 8) { yield = FALSE; goto END_FORMAT; }
1078
1118
    strncpy(newformat, item_start, fp - item_start);
1079
1119
    newformat[fp-item_start] = 0;
1080
 
    sprintf(CS p, newformat, va_arg(ap, double));
 
1120
    if (length == L_LONGDOUBLE)
 
1121
      sprintf(CS p, newformat, va_arg(ap, long double));
 
1122
    else
 
1123
      sprintf(CS p, newformat, va_arg(ap, double));
1081
1124
    while (*p) p++;
1082
1125
    break;
1083
1126