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.

57 lines
1.5 KiB

  1. #include "npcommon.h"
  2. // strncpyf(dest, src, cb)
  3. //
  4. // Always stores cb bytes to dest. If total characters copied
  5. // ends up less than cb bytes, zero-fills dest.
  6. // If strlen(src) >= cb, dest is NOT null-terminated.
  7. // Returns dest.
  8. LPSTR WINAPI strncpyf(LPSTR lpDest, LPCSTR lpSrc, UINT cbCopy)
  9. {
  10. LPCSTR lpChr = lpSrc;
  11. UINT cbToCopy = 0;
  12. // find ptr past last char to copy
  13. while (*lpChr) {
  14. if (cbToCopy + (IS_LEAD_BYTE(*lpChr) ? 2 : 1) > cbCopy)
  15. break; // copying this char would run over the limit
  16. cbToCopy += IS_LEAD_BYTE(*lpChr) ? 2 : 1;
  17. ADVANCE(lpChr);
  18. }
  19. // copy that many bytes
  20. memcpyf(lpDest, lpSrc, cbToCopy);
  21. memsetf(lpDest + cbToCopy, '\0', cbCopy - cbToCopy);
  22. return lpDest;
  23. }
  24. // strncatf(dest, src, cb)
  25. //
  26. // Concatenates at most cb bytes of src onto the end of dest.
  27. // Unlike strncpyf, does not pad with extra nulls, but does
  28. // guarantee a null-terminated destination.
  29. // Returns dest.
  30. LPSTR WINAPI strncatf(LPSTR lpDest, LPCSTR lpSrc, UINT cbCopy)
  31. {
  32. LPCSTR lpChr = lpSrc;
  33. UINT cbToCopy = 0;
  34. // find ptr past last char to copy
  35. while (*lpChr) {
  36. if (cbToCopy + (IS_LEAD_BYTE(*lpChr) ? 2 : 1) > cbCopy)
  37. break; // copying this char would run over the limit
  38. cbToCopy += IS_LEAD_BYTE(*lpChr) ? 2 : 1;
  39. ADVANCE(lpChr);
  40. }
  41. // copy that many bytes
  42. memcpyf(lpDest, lpSrc, cbToCopy);
  43. lpDest[cbToCopy] = '\0';
  44. return lpDest;
  45. }