~ubuntu-branches/ubuntu/utopic/open-vm-tools/utopic

« back to all changes in this revision

Viewing changes to lib/string/str.c

  • Committer: Package Import Robot
  • Author(s): Bernd Zeimetz, 31c30832
  • Date: 2014-01-08 20:28:33 UTC
  • mfrom: (1.4.14) (2.4.42 sid)
  • Revision ID: package-import@ubuntu.com-20140108202833-l0ycdpzq5ayadedn
Tags: 2:9.4.0-1280544-5
[31c30832] Revert "Enable building of vmci again."
This reverts commit 0d55577cd3c262dbbc2bf79593d6f500f84c4170.
Too fast upload, sorry. vmhgfs is indeed (still) broken with
vmci.

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
extern int vasprintf(char **ptr, const char *f, va_list arg);
58
58
/*
59
59
 * Declare vswprintf on platforms where it's not known to exist.  We know
60
 
 * it's available on glibc >= 2.2, FreeBSD >= 5.0, and all versions of
 
60
 * it's available on glibc >= 2.2, FreeBSD >= 5.0, and all versions of 
61
61
 * Solaris.
62
62
 * (Re: Solaris, vswprintf has been present since Solaris 8, and we only
63
63
 * support Solaris 9 and above, since that was the first release available
80
80
 *
81
81
 * Str_Vsnprintf --
82
82
 *
83
 
 *      Compatibility wrapper b/w different libc versions
 
83
 *      Compatibility wrapper b/w different libc versions
84
84
 *
85
85
 * Results:
86
86
 *
87
 
 *      int - number of bytes stored in 'str' (not including NUL
88
 
 *      terminate character), -1 on overflow (insufficient space for
89
 
 *      NUL terminate is considered overflow)
 
87
 *      int - number of bytes stored in 'str' (not including NUL
 
88
 *      terminate character), -1 on overflow (insufficient space for
 
89
 *      NUL terminate is considered overflow)
90
90
 *
91
 
 *      NB: on overflow the buffer WILL be NUL terminated at the last
92
 
 *      UTF-8 code point boundary within the buffer's bounds.
 
91
 *      NB: on overflow the buffer WILL be NUL terminated at the last
 
92
 *      UTF-8 code point boundary within the buffer's bounds.
93
93
 *
94
94
 * WARNING: See warning at the top of this file.
95
95
 *
96
96
 * Side effects:
97
 
 *      None
 
97
 *      None
98
98
 *
99
99
 *----------------------------------------------------------------------
100
100
 */
110
110
   ASSERT(str != NULL);
111
111
   ASSERT(format != NULL);
112
112
 
113
 
#if defined HAS_BSD_PRINTF && !defined __ANDROID__
 
113
#ifdef HAS_BSD_PRINTF
114
114
   retval = bsd_vsnprintf(&str, size, format, ap);
115
115
#else
116
116
   retval = vsnprintf(str, size, format, ap);
165
165
                     const char *fmt,  // IN:
166
166
                     ...)              // IN:
167
167
{
 
168
   uint32 *stack = (uint32*) &buf;
168
169
   va_list args;
169
170
   int retval;
170
171
 
171
172
   ASSERT(buf);
172
173
   ASSERT(fmt);
173
 
 
 
174
   
174
175
   va_start(args, fmt);
175
176
   retval = bsd_vsnprintf_c_locale(&buf, maxSize, fmt, args);
176
177
   va_end(args);
185
186
   }
186
187
 
187
188
   if (retval >= maxSize) {
188
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
 
189
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
189
190
   }
190
191
 
191
192
   return retval;
215
216
            const char *fmt, // IN
216
217
            ...)             // IN
217
218
{
 
219
   uint32 *stack = (uint32*) &buf;
218
220
   va_list args;
219
221
   int i;
220
 
 
 
222
   
221
223
   va_start(args, fmt);
222
224
   i = Str_Vsnprintf(buf, maxSize, fmt, args);
223
225
   va_end(args);
224
226
   if (i < 0) {
225
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
 
227
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
226
228
   }
227
229
   return i;
228
230
}
233
235
 *
234
236
 * Str_Snprintf --
235
237
 *
236
 
 *      Compatibility wrapper b/w different libc versions
 
238
 *      Compatibility wrapper b/w different libc versions
237
239
 *
238
240
 * Results:
239
241
 *
240
 
 *      int - number of bytes stored in 'str' (not including NUL
241
 
 *      terminate character), -1 on overflow (insufficient space for
242
 
 *      NUL terminate is considered overflow)
 
242
 *      int - number of bytes stored in 'str' (not including NUL
 
243
 *      terminate character), -1 on overflow (insufficient space for
 
244
 *      NUL terminate is considered overflow)
243
245
 *
244
 
 *      NB: on overflow the buffer WILL be NUL terminated
 
246
 *      NB: on overflow the buffer WILL be NUL terminated
245
247
 *
246
248
 * Side effects:
247
 
 *      None
 
249
 *      None
248
250
 *
249
251
 *----------------------------------------------------------------------
250
252
 */
251
253
 
252
 
int
 
254
int      
253
255
Str_Snprintf(char *str,          // OUT
254
256
             size_t size,        // IN
255
257
             const char *format, // IN
290
292
           const char *src, // IN
291
293
           size_t maxSize)  // IN
292
294
{
 
295
   uint32 *stack = (uint32 *)&buf;
293
296
   size_t len;
294
297
 
295
298
   ASSERT(buf != NULL);
297
300
 
298
301
   len = strlen(src);
299
302
   if (len >= maxSize) {
300
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
 
303
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
301
304
      ASSERT_BUG(5686, FALSE);
302
305
   }
303
306
   return memcpy(buf, src, len + 1);
312
315
 *      Calculate length of the string.
313
316
 *
314
317
 * Results:
315
 
 *      Length of s not including the terminating '\0' character.
 
318
 *      Length of s not including the terminating '\0' character. 
316
319
 *      If there is no '\0' for first maxLen bytes, then it
317
320
 *      returns maxLen.
318
321
 *
323
326
 */
324
327
 
325
328
size_t
326
 
Str_Strlen(const char *s,  // IN:
327
 
           size_t maxLen)  // IN:
 
329
Str_Strlen(const char *s,       // IN 
 
330
           size_t maxLen)       // IN
328
331
 
329
332
{
330
333
   const char *end;
333
336
 
334
337
   if ((end = memchr(s, '\0', maxLen)) == NULL) {
335
338
      return maxLen;
336
 
   }
 
339
   } 
337
340
   return end - s;
338
341
}
339
342
 
343
346
 *
344
347
 * Str_Strnstr --
345
348
 *
346
 
 *      Find a substring within a string of length at most n. 'sub' must be
 
349
 *      Find a substring within a string of length at most n. 'sub' must be
347
350
 *      NUL-terminated. 'n' is interpreted as an unsigned int.
348
351
 *
349
352
 * Results:
350
 
 *      A pointer to the beginning of the substring, or NULL if not found.
 
353
 *      A pointer to the beginning of the substring, or NULL if not found.
351
354
 *
352
355
 * Side Effects:
353
356
 *      None
356
359
 */
357
360
 
358
361
char *
359
 
Str_Strnstr(const char *src,  // IN:
360
 
            const char *sub,  // IN:
361
 
            size_t n)         // IN:
 
362
Str_Strnstr(const char *src,    // IN 
 
363
            const char *sub,    // IN
 
364
            size_t n)           // IN
 
365
 
362
366
{
363
367
   size_t subLen;
364
368
   const char *end;
377
381
      return NULL;
378
382
   }
379
383
   for (;
380
 
       (src = memchr(src, sub[0], end - src)) != NULL &&
381
 
        memcmp(src, sub, subLen) != 0;
382
 
        src++) {
 
384
        (src = memchr(src, sub[0], end - src)) != NULL &&
 
385
        memcmp(src, sub, subLen) != 0;
 
386
        src++) {
383
387
   }
384
388
   return (char *) src;
385
389
}
406
410
           const char *src, // IN
407
411
           size_t maxSize)  // IN
408
412
{
 
413
   uint32 *stack = (uint32 *)&buf;
409
414
   size_t bufLen;
410
415
   size_t srcLen;
411
416
 
417
422
 
418
423
   /* The first comparison checks for numeric overflow */
419
424
   if (bufLen + srcLen < srcLen || bufLen + srcLen >= maxSize) {
420
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
 
425
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
421
426
   }
422
427
 
423
428
   memcpy(buf + bufLen, src, srcLen + 1);
451
456
            const char *src, // IN: String to append
452
457
            size_t n)        // IN: Max chars of src to append
453
458
{
454
 
   size_t bufLen;
 
459
   uint32 *stack; 
 
460
   size_t bufLen; 
455
461
 
456
462
   ASSERT(buf != NULL);
457
463
   ASSERT(src != NULL);
458
464
 
 
465
   stack = (uint32 *)&buf;
 
466
   bufLen = strlen(buf);
 
467
 
459
468
   /*
460
469
    * Check bufLen + n first so we can avoid the second call to strlen
461
470
    * if possible.
468
477
    * happen by adding the == case to the Panic test.
469
478
    */
470
479
 
471
 
   bufLen = strlen(buf);
472
 
 
473
480
   if (bufLen + n >= bufSize &&
474
481
       bufLen + strlen(src) >= bufSize) {
475
 
      Panic("%s:%d Buffer too small\n", __FILE__,__LINE__);
 
482
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__,__LINE__, stack[-1]);
476
483
   }
477
484
 
478
485
   /*
539
546
{
540
547
   va_list arguments;
541
548
   char *result;
542
 
 
 
549
   
543
550
   va_start(arguments, format);
544
551
   result = Str_SafeVasprintf(length, format, arguments);
545
552
   va_end(arguments);
581
588
   char *buf = NULL;
582
589
   int ret;
583
590
 
584
 
#if defined HAS_BSD_PRINTF && !defined __ANDROID__
 
591
#ifdef HAS_BSD_PRINTF
585
592
   ret = bsd_vsnprintf(&buf, 0, format, arguments);
586
593
 
587
594
#elif !defined sun && !defined STR_NO_WIN32_LIBS
713
720
             const wchar_t *fmt, // IN
714
721
             ...)                // IN
715
722
{
 
723
   uint32 *stack = (uint32*) &buf;
716
724
   va_list args;
717
725
   int i;
718
 
 
 
726
   
719
727
   va_start(args,fmt);
720
728
   i = Str_Vsnwprintf(buf, maxSize, fmt, args);
721
729
   va_end(args);
722
730
   if (i < 0) {
723
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
 
731
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
724
732
   }
725
733
   return i;
726
734
}
731
739
 *
732
740
 * Str_Vsnwprintf --
733
741
 *
734
 
 *      Compatibility wrapper b/w different libc versions
 
742
 *      Compatibility wrapper b/w different libc versions
735
743
 *
736
744
 * Results:
737
745
 *
738
 
 *      int - number of wchar_ts stored in 'str' (not including NUL
739
 
 *      terminate character), -1 on overflow (insufficient space for
740
 
 *      NUL terminate is considered overflow)
 
746
 *      int - number of wchar_ts stored in 'str' (not including NUL
 
747
 *      terminate character), -1 on overflow (insufficient space for
 
748
 *      NUL terminate is considered overflow)
741
749
 *
742
 
 *      NB: on overflow the buffer WILL be NUL terminated
 
750
 *      NB: on overflow the buffer WILL be NUL terminated
743
751
 *
744
752
 * WARNING: See warning at the top of this file.
745
753
 *
746
754
 * Side effects:
747
 
 *      None
 
755
 *      None
748
756
 *
749
757
 *----------------------------------------------------------------------
750
758
 */
794
802
 *
795
803
 * Str_Snwprintf --
796
804
 *
797
 
 *      Compatibility wrapper b/w different libc versions
 
805
 *      Compatibility wrapper b/w different libc versions
798
806
 *
799
807
 * Results:
800
808
 *
801
 
 *      int - number of wchar_ts stored in 'str' (not including NUL
802
 
 *      terminate character), -1 on overflow (insufficient space for
803
 
 *      NUL terminate is considered overflow)
 
809
 *      int - number of wchar_ts stored in 'str' (not including NUL
 
810
 *      terminate character), -1 on overflow (insufficient space for
 
811
 *      NUL terminate is considered overflow)
804
812
 *
805
 
 *      NB: on overflow the buffer WILL be NUL terminated
 
813
 *      NB: on overflow the buffer WILL be NUL terminated
806
814
 *
807
815
 * Side effects:
808
 
 *      None
 
816
 *      None
809
817
 *
810
818
 *----------------------------------------------------------------------
811
819
 */
812
820
 
813
 
int
 
821
int      
814
822
Str_Snwprintf(wchar_t *str,          // OUT
815
823
              size_t size,           // IN: Size of str, in wide-characters.
816
824
              const wchar_t *format, // IN
847
855
           const wchar_t *src, // IN
848
856
           size_t maxSize)     // IN: Size of buf, in wide-characters.
849
857
{
 
858
   uint32 *stack = (uint32 *)&buf;
850
859
   size_t len;
851
860
 
852
861
   len = wcslen(src);
853
 
   if (len >= maxSize) {
854
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
855
 
      ASSERT_BUG(5686, FALSE);
 
862
   if (len >= maxSize) { 
 
863
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
 
864
      ASSERT_BUG(5686, FALSE); 
856
865
   }
857
866
   return memcpy(buf, src, (len + 1)*sizeof(wchar_t));
858
867
}
879
888
           const wchar_t *src, // IN
880
889
           size_t maxSize)     // IN: Size of buf, in wide-characters.
881
890
{
 
891
   uint32 *stack = (uint32 *)&buf;
882
892
   size_t bufLen;
883
893
   size_t srcLen;
884
894
 
887
897
 
888
898
   /* The first comparison checks for numeric overflow */
889
899
   if (bufLen + srcLen < srcLen || bufLen + srcLen >= maxSize) {
890
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
 
900
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
891
901
   }
892
902
 
893
903
   memcpy(buf + bufLen, src, (srcLen + 1)*sizeof(wchar_t));
921
931
            const wchar_t *src, // IN: String to append
922
932
            size_t n)           // IN: Max chars of src to append
923
933
{
 
934
   uint32 *stack = (uint32 *)&buf;
924
935
   size_t bufLen = wcslen(buf);
925
936
 
926
937
   /*
937
948
 
938
949
   if (bufLen + n >= bufSize &&
939
950
       bufLen + wcslen(src) >= bufSize) {
940
 
      Panic("%s:%d Buffer too small\n", __FILE__,__LINE__);
 
951
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__,__LINE__, stack[-1]);
941
952
   }
942
953
 
943
954
   /*
970
981
           const char *src,          // IN
971
982
           size_t maxSize)           // IN
972
983
{
 
984
   uint32 *stack = (uint32 *)&buf;
973
985
   size_t len;
974
986
 
975
987
   len = strlen((const char *) src);
976
988
   if (len >= maxSize) {
977
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
 
989
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
978
990
   }
979
991
   return memcpy(buf, src, len + 1);
980
992
}
1004
1016
           const char *src,          // IN
1005
1017
           size_t maxSize)           // IN
1006
1018
{
 
1019
   uint32 *stack = (uint32 *)&buf;
1007
1020
   size_t bufLen;
1008
1021
   size_t srcLen;
1009
1022
 
1012
1025
 
1013
1026
   /* The first comparison checks for numeric overflow */
1014
1027
   if (bufLen + srcLen < srcLen || bufLen + srcLen >= maxSize) {
1015
 
      Panic("%s:%d Buffer too small\n", __FILE__, __LINE__);
 
1028
      Panic("%s:%d Buffer too small 0x%x\n", __FILE__, __LINE__, stack[-1]);
1016
1029
   }
1017
1030
 
1018
1031
   memcpy(buf + bufLen, src, srcLen + 1);
1119
1132
{
1120
1133
   va_list arguments;
1121
1134
   wchar_t *result;
1122
 
 
 
1135
   
1123
1136
   va_start(arguments, format);
1124
1137
   result = Str_Vaswprintf(length, format, arguments);
1125
1138
   va_end(arguments);
1178
1191
{
1179
1192
   va_list arguments;
1180
1193
   wchar_t *result;
1181
 
 
 
1194
   
1182
1195
   va_start(arguments, format);
1183
1196
   result = Str_SafeVaswprintf(length, format, arguments);
1184
1197
   va_end(arguments);
1304
1317
   char buf1[1024], buf2[1024];
1305
1318
   int count;
1306
1319
   va_list args;
1307
 
 
 
1320
   
1308
1321
   va_start(args, fmt);
1309
1322
   count = Str_Vsnprintf(buf1, sizeof buf1, fmt, args);
1310
1323
   va_end(args);
1344
1357
   wchar_t buf1[1024], buf2[1024];
1345
1358
   int count;
1346
1359
   va_list args;
1347
 
 
 
1360
   
1348
1361
   va_start(args, fmt);
1349
1362
   count = Str_Vsnwprintf(buf1, sizeof buf1, fmt, args);
1350
1363
   va_end(args);