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.

263 lines
5.5 KiB

  1. #include "wzmanip.h"
  2. #include <assert.h>
  3. //////////////////////////////////////////////////////////////////////////
  4. // Wzncpy
  5. //
  6. // Copies the lesser of len(wzFrom) or n characters (n includes the
  7. // null terminator) from wzFrom to wzTo.
  8. WCHAR * Wzncpy(WCHAR* wzTo, const WCHAR* wzFrom, unsigned int cchTo)
  9. {
  10. if (cchTo <= 0 || !wzTo || !wzFrom)
  11. return wzTo;
  12. unsigned int cchFrom = (unsigned int) (wcslen(wzFrom) + 1);
  13. if (cchTo >= cchFrom)
  14. {
  15. memmove(wzTo, wzFrom, cchFrom*2);
  16. return wzTo + cchFrom - 1;
  17. }
  18. else
  19. {
  20. cchFrom = cchTo - 1;
  21. memmove(wzTo, wzFrom, cchFrom*2);
  22. wzTo[cchFrom] = 0;
  23. return wzTo+cchFrom;
  24. }
  25. }
  26. //////////////////////////////////////////////////////////////////////////
  27. // Szncpy
  28. //
  29. // Copies the lesser of len(szFrom) or n characters (n includes the
  30. // null terminator) from wzFrom to szTo.
  31. char * Szncpy(char* szTo, const char* szFrom, unsigned int cchTo)
  32. {
  33. if (cchTo <= 0 || !szTo || !szFrom)
  34. return szTo;
  35. unsigned int cchFrom = (unsigned int)(strlen(szFrom) + 1);
  36. if (cchTo >= cchFrom)
  37. {
  38. memmove(szTo, szFrom, cchFrom);
  39. return szTo + cchFrom - 1;
  40. }
  41. else
  42. {
  43. cchFrom = cchTo - 1;
  44. memmove(szTo, szFrom, cchFrom);
  45. szTo[cchFrom] = 0;
  46. return szTo+cchFrom;
  47. }
  48. }
  49. //////////////////////////////////////////////////////////////////////////
  50. // WznCat
  51. //
  52. // Append wzFrom onto wzTo.
  53. WCHAR* WznCat(WCHAR* wzTo, const WCHAR* wzFrom, unsigned int cchTo)
  54. {
  55. while (*wzTo != L'\0')
  56. {
  57. wzTo++;
  58. cchTo--;
  59. }
  60. if (cchTo <= 0)
  61. {
  62. return wzTo;
  63. }
  64. unsigned int cchFrom = (unsigned int)(wcslen(wzFrom) + 1);
  65. if (cchTo >= cchFrom)
  66. {
  67. memmove(wzTo, wzFrom, cchFrom*sizeof(WCHAR));
  68. return wzTo + cchFrom - 1;
  69. }
  70. else
  71. {
  72. cchFrom = cchTo - 1;
  73. memmove(wzTo, wzFrom, cchFrom*sizeof(WCHAR));
  74. wzTo[cchFrom] = 0;
  75. return wzTo+cchFrom;
  76. }
  77. }
  78. //////////////////////////////////////////////////////////////////////////
  79. // SznCat
  80. //
  81. // Append szFrom onto szTo.
  82. char* SznCat(char* szTo, const char* szFrom, unsigned int cchTo)
  83. {
  84. while (*szTo != L'\0')
  85. {
  86. szTo++;
  87. cchTo--;
  88. }
  89. if (cchTo <= 0)
  90. {
  91. return szTo;
  92. }
  93. unsigned int cchFrom = (unsigned int)(strlen(szFrom) + 1);
  94. if (cchTo >= cchFrom)
  95. {
  96. memmove(szTo, szFrom, cchFrom);
  97. return szTo + cchFrom - 1;
  98. }
  99. else
  100. {
  101. cchFrom = cchTo - 1;
  102. memmove(szTo, szFrom, cchFrom);
  103. szTo[cchFrom] = 0;
  104. return szTo+cchFrom;
  105. }
  106. }
  107. //
  108. /*------------------------------------------------------------------------
  109. MsoWzDecodeUInt
  110. Decodes the integer w into Unicode text in base wBase.
  111. Returns the length of the text decoded.
  112. ---------------------------------------------------------------- RICKP -*/
  113. const char rgchHex[] = "0123456789ABCDEF";
  114. int MsoWzDecodeUint(WCHAR* rgwch, int cch, unsigned u, int wBase)
  115. {
  116. assert(wBase >= 2 && wBase <= 16);
  117. if (cch == 1)
  118. *rgwch = 0;
  119. if (cch <= 1)
  120. return 0;
  121. if (u == 0)
  122. {
  123. rgwch[0] = L'0';
  124. rgwch[1] = 0;
  125. return 1;
  126. }
  127. int cDigits = 0;
  128. unsigned uT = u;
  129. while(uT)
  130. {
  131. cDigits++;
  132. uT /= wBase;
  133. };
  134. if (cDigits >= cch)
  135. return 0;
  136. rgwch += cDigits;
  137. *rgwch-- = 0;
  138. uT = u;
  139. while(uT)
  140. {
  141. *rgwch-- = rgchHex[uT % wBase];
  142. uT /= wBase;
  143. };
  144. return cDigits;
  145. }
  146. /*------------------------------------------------------------------------
  147. MsoWzDecodeInt
  148. Decodes the signed integer w into ASCII text in base wBase. The string
  149. is stored in the rgch buffer, which is assumed to be large enough to hold
  150. the number's text and a null terminator. Returns the length of the text
  151. decoded.
  152. ---------------------------------------------------------------- RICKP -*/
  153. int MsoWzDecodeInt(WCHAR* rgwch, int cch, int w, int wBase)
  154. {
  155. if (cch <= 0)
  156. {
  157. assert(cch == 0);
  158. return 0;
  159. };
  160. if (w < 0)
  161. {
  162. *rgwch = '-';
  163. return MsoWzDecodeUint(rgwch+1, cch-1, -w, wBase) + 1;
  164. }
  165. return MsoWzDecodeUint(rgwch, cch, w, wBase);
  166. }
  167. /*------------------------------------------------------------------------
  168. MsoSzDecodeUint
  169. Decodes the unsigned integer u into ASCII text in base wBase. The string
  170. is stored in the rgch buffer, which is assumed to be large enough to hold
  171. the number's text and a null terminator. Returns the length of the text
  172. decoded.
  173. ---------------------------------------------------------------- RICKP -*/
  174. int MsoSzDecodeUint(char* rgch, int cch, unsigned u, int wBase)
  175. {
  176. assert(wBase >= 2 && wBase <= 16);
  177. if (cch == 1)
  178. *rgch = 0;
  179. if (cch <= 1)
  180. return 0;
  181. if (u == 0)
  182. {
  183. rgch[0] = '0';
  184. rgch[1] = 0;
  185. return 1;
  186. }
  187. int cDigits = 0;
  188. unsigned uT = u;
  189. while(uT)
  190. {
  191. cDigits++;
  192. uT /= wBase;
  193. };
  194. if (cDigits >= cch)
  195. return 0;
  196. rgch += cDigits;
  197. *rgch-- = 0;
  198. uT = u;
  199. while(uT)
  200. {
  201. *rgch-- = rgchHex[uT % wBase];
  202. uT /= wBase;
  203. };
  204. return cDigits;
  205. }
  206. /*------------------------------------------------------------------------
  207. MsoSzDecodeInt
  208. Decodes the signed integer w into ASCII text in base wBase. The string
  209. is stored in the rgch buffer, which is assumed to be large enough to hold
  210. the number's text and a null terminator. Returns the length of the text
  211. decoded.
  212. ---------------------------------------------------------------- RICKP -*/
  213. int MsoSzDecodeInt(char* rgch, int cch, int w, int wBase)
  214. {
  215. if (cch <= 0)
  216. {
  217. assert(cch == 0);
  218. return 0;
  219. };
  220. if (w < 0)
  221. {
  222. *rgch = '-';
  223. return MsoSzDecodeUint(rgch+1, cch-1, -w, wBase) + 1;
  224. }
  225. return MsoSzDecodeUint(rgch, cch, w, wBase);
  226. }