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.

113 lines
3.4 KiB

  1. /***
  2. *mbsnbset.c - Sets first n bytes of string to given character (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Sets first n bytes of string to given character (MBCS)
  8. *
  9. *Revision History:
  10. * 08-03-93 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 Fix history.
  14. * 05-09-94 CFW Optimize for SBCS.
  15. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  16. *
  17. *******************************************************************************/
  18. #ifdef _MBCS
  19. #include <cruntime.h>
  20. #include <string.h>
  21. #include <mbdata.h>
  22. #include <mbctype.h>
  23. #include <mbstring.h>
  24. /***
  25. * _mbsnbset - Sets first n bytes of string to given character (MBCS)
  26. *
  27. *Purpose:
  28. * Sets the first n bytes of string to the supplied
  29. * character value. If the length of string is less than n,
  30. * the length of string is used in place of n. Handles
  31. * MBCS chars correctly.
  32. *
  33. * There are several factors that make this routine complicated:
  34. * (1) The fill value may be 1 or 2 bytes long.
  35. * (2) The fill operation may end by hitting the count value
  36. * or by hitting the end of the string.
  37. * (3) A null terminating char is NOT placed at the end of
  38. * the string.
  39. *
  40. * Cases to be careful of (both of these can occur at once):
  41. * (1) Leaving an "orphaned" trail byte in the string (e.g.,
  42. * overwriting a lead byte but not the corresponding trail byte).
  43. * (2) Writing only the 1st byte of a 2-byte fill value because the
  44. * end of string was encountered.
  45. *
  46. *Entry:
  47. * unsigned char *string = string to modify
  48. * unsigned int val = value to fill string with
  49. * size_t count = count of characters to fill
  50. *
  51. *
  52. *Exit:
  53. * Returns string = now filled with char val
  54. *
  55. *Uses:
  56. *
  57. *Exceptions:
  58. *
  59. *******************************************************************************/
  60. unsigned char * __cdecl _mbsnbset(
  61. unsigned char *string,
  62. unsigned int val,
  63. size_t count
  64. )
  65. {
  66. unsigned char *start = string;
  67. unsigned char highval, lowval;
  68. if ( _ISNOTMBCP )
  69. return _strnset(string, val, count);
  70. /*
  71. * leadbyte flag indicates if the last byte we overwrote was
  72. * a lead byte or not.
  73. */
  74. if (highval = (unsigned char)(val>>8)) {
  75. /* double byte value */
  76. lowval = (unsigned char)(val & 0x00ff);
  77. while ((count--) && *string) {
  78. /* pad with ' ' if no room for both bytes -- odd len */
  79. if ((!count--) || (!*(string+1))) {
  80. *string = ' ';
  81. break;
  82. }
  83. *string++ = highval;
  84. *string++ = lowval;
  85. }
  86. }
  87. else {
  88. /* single byte value */
  89. while (count-- && *string) {
  90. *string++ = (unsigned char)val;
  91. }
  92. }
  93. return( start );
  94. }
  95. #endif /* _MBCS */