Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.3 KiB

  1. /***
  2. *mbsnset.c - Sets first n charcaters of string to given character (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Sets first n charcaters of string to given character (MBCS)
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 08-20-93 CFW Change short params to int for 32-bit tree.
  12. * 10-05-93 GJF Replaced _CRTAPI1 with __cdecl.
  13. * 04-15-93 CFW Add _MB_CP_LOCK.
  14. * 05-09-94 CFW Optimize for SBCS.
  15. * 05-19-94 CFW Enable non-Win32.
  16. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  17. * 04-17-98 GJF Revised multithread support based on threadmbcinfo
  18. * structs
  19. *
  20. *******************************************************************************/
  21. #ifdef _MBCS
  22. #include <mtdll.h>
  23. #include <cruntime.h>
  24. #include <string.h>
  25. #include <mbdata.h>
  26. #include <mbctype.h>
  27. #include <mbstring.h>
  28. /***
  29. * _mbsnset - Sets first n charcaters of string to given character (MBCS)
  30. *
  31. *Purpose:
  32. * Sets the first n characters of string to the supplied
  33. * character value. If the length of string is less than n,
  34. * the length of string is used in place of n. Handles
  35. * MBCS chars correctly.
  36. *
  37. * There are several factors that make this routine complicated:
  38. * (1) The fill value may be 1 or 2 bytes long.
  39. * (2) The fill operation may end by hitting the count value
  40. * or by hitting the end of the string.
  41. * (3) A null terminating char is NOT placed at the end of
  42. * the string.
  43. *
  44. * Cases to be careful of (both of these can occur at once):
  45. * (1) Leaving an "orphaned" trail byte in the string (e.g.,
  46. * overwriting a lead byte but not the corresponding trail byte).
  47. * (2) Writing only the 1st byte of a 2-byte fill value because the
  48. * end of string was encountered.
  49. *
  50. *Entry:
  51. * unsigned char *string = string to modify
  52. * unsigned int val = value to fill string with
  53. * size_t count = count of characters to fill
  54. *
  55. *
  56. *Exit:
  57. * Returns string = now filled with char val
  58. *
  59. *Uses:
  60. *
  61. *Exceptions:
  62. *
  63. *******************************************************************************/
  64. unsigned char * __cdecl _mbsnset(
  65. unsigned char *string,
  66. unsigned int val,
  67. size_t count
  68. )
  69. {
  70. unsigned char *start = string;
  71. unsigned int leadbyte = 0;
  72. unsigned char highval, lowval;
  73. #ifdef _MT
  74. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  75. if ( ptmbci != __ptmbcinfo )
  76. ptmbci = __updatetmbcinfo();
  77. #endif
  78. /*
  79. * leadbyte flag indicates if the last byte we overwrote was
  80. * a lead byte or not.
  81. */
  82. #ifdef _MT
  83. if ( _ISNOTMBCP_MT(ptmbci) )
  84. #else
  85. if ( _ISNOTMBCP )
  86. #endif
  87. return _strnset(string, val, count);
  88. if (highval = (unsigned char)(val>>8)) {
  89. /* double byte value */
  90. lowval = (unsigned char)(val & 0x00ff);
  91. while (count-- && *string) {
  92. #ifdef _MT
  93. leadbyte = __ismbbtruelead_mt(ptmbci, leadbyte, *string);
  94. #else
  95. leadbyte = _ismbbtruelead(leadbyte, *string);
  96. #endif
  97. *string++ = highval;
  98. if (*string) {
  99. #ifdef _MT
  100. leadbyte = __ismbbtruelead_mt(ptmbci, leadbyte, *string);
  101. #else
  102. leadbyte = _ismbbtruelead(leadbyte, *string);
  103. #endif
  104. *string++ = lowval;
  105. }
  106. else
  107. /* overwrite orphaned highval byte */
  108. *(string-1) = ' ';
  109. }
  110. }
  111. else {
  112. /* single byte value */
  113. while (count-- && *string) {
  114. #ifdef _MT
  115. leadbyte = __ismbbtruelead_mt(ptmbci, leadbyte, *string);
  116. #else
  117. leadbyte = _ismbbtruelead(leadbyte, *string);
  118. #endif
  119. *string++ = (unsigned char)val;
  120. }
  121. }
  122. /* overwrite orphaned trailing byte, if necessary */
  123. if(leadbyte && *string)
  124. *string = ' ';
  125. return( start );
  126. }
  127. #endif /* _MBCS */