Source code of Windows XP (NT5)
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.

245 lines
7.7 KiB

  1. page ,132
  2. title strncpy - copy at most n characters of string
  3. ;***
  4. ;strncpy.asm - copy at most n characters of string
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines strncpy() - copy at most n characters of string
  10. ;
  11. ;Revision History:
  12. ; 10-25-83 RN initial version
  13. ; 05-18-88 SJM Add model-independent (large model) ifdef
  14. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  15. ; 08-23-88 JCR 386 cleanup
  16. ; 10-26-88 JCR General cleanup for 386-only code
  17. ; 10-26-88 JCR Re-arrange regs to avoid push/pop ebx
  18. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  19. ; 05-10-91 GJF Back to _cdecl, sigh...
  20. ; 12-20-96 GJF Faster version from Intel.
  21. ;
  22. ;*******************************************************************************
  23. .xlist
  24. include cruntime.inc
  25. .list
  26. page
  27. ;***
  28. ;char *strncpy(dest, source, count) - copy at most n characters
  29. ;
  30. ;Purpose:
  31. ; Copies count characters from the source string to the
  32. ; destination. If count is less than the length of source,
  33. ; NO NULL CHARACTER is put onto the end of the copied string.
  34. ; If count is greater than the length of sources, dest is padded
  35. ; with null characters to length count.
  36. ;
  37. ; Algorithm:
  38. ; char *
  39. ; strncpy (dest, source, count)
  40. ; char *dest, *source;
  41. ; unsigned count;
  42. ; {
  43. ; char *start = dest;
  44. ;
  45. ; while (count && (*dest++ = *source++))
  46. ; count--;
  47. ; if (count)
  48. ; while (--count)
  49. ; *dest++ = '\0';
  50. ; return(start);
  51. ; }
  52. ;
  53. ;Entry:
  54. ; char *dest - pointer to spot to copy source, enough space
  55. ; is assumed.
  56. ; char *source - source string for copy
  57. ; unsigned count - characters to copy
  58. ;
  59. ;Exit:
  60. ; returns dest, with the character copied there.
  61. ;
  62. ;Uses:
  63. ;
  64. ;Exceptions:
  65. ;
  66. ;*******************************************************************************
  67. CODESEG
  68. public strncpy
  69. strncpy proc
  70. .FPO ( 0, 3, 0, 0, 0, 0 )
  71. mov ecx,[esp + 0ch] ; ecx = count
  72. push edi ; preserve edi
  73. test ecx,ecx
  74. jz short finish ; leave if count is zero
  75. push esi ; preserve edi
  76. push ebx ; preserve ebx
  77. mov ebx,ecx ; store count for tail loop
  78. mov esi,[esp + 14h] ; esi -> source string
  79. test esi,3 ; test if source string is aligned on 32 bits
  80. mov edi,[esp + 10h] ; edi -> dest string
  81. jnz short src_misaligned ; (almost always source is aligned)
  82. shr ecx,2 ; convert ecx to dword count
  83. jnz short main_loop_entrance
  84. jmp short copy_tail_loop ; 0 < count < 4
  85. ; simple byte loop until string is aligned
  86. src_misaligned:
  87. mov al,byte ptr [esi] ; copy a byte from source to dest
  88. inc esi
  89. mov [edi],al
  90. inc edi
  91. dec ecx
  92. jz fill_tail_end1 ; if count == 0, leave
  93. test al,al ; was last copied byte zero?
  94. jz short align_dest ; if so, go align dest and pad it out
  95. ; with zeros
  96. test esi,3 ; esi already aligned ?
  97. jne short src_misaligned
  98. mov ebx,ecx ; store count for tail loop
  99. shr ecx,2
  100. jnz short main_loop_entrance
  101. tail_loop_start:
  102. and ebx,3 ; ebx = count_before_main_loop%4
  103. jz short fill_tail_end1 ; if ebx == 0 then leave without
  104. ; appending a null byte
  105. ; while ( EOS (end-of-string) not found and count > 0 ) copy bytes
  106. copy_tail_loop:
  107. mov al,byte ptr [esi] ; load byte from source
  108. inc esi
  109. mov [edi],al ; store byte to dest
  110. inc edi
  111. test al,al ; EOS found?
  112. je short fill_tail_zero_bytes ; '\0' was already copied
  113. dec ebx
  114. jnz copy_tail_loop
  115. fill_tail_end1:
  116. mov eax,[esp + 10h] ; prepare return value
  117. pop ebx
  118. pop esi
  119. pop edi
  120. ret
  121. ; EOS found. Pad with null characters to length count
  122. align_dest:
  123. test edi,3 ; dest string aligned?
  124. jz dest_align_loop_end
  125. dest_align_loop:
  126. mov [edi],al
  127. inc edi
  128. dec ecx ; count == 0?
  129. jz fill_tail_end ; if so, finished
  130. test edi,3 ; is edi aligned ?
  131. jnz dest_align_loop
  132. dest_align_loop_end:
  133. mov ebx,ecx ; ebx > 0
  134. shr ecx,2 ; convert ecx to count of dwords
  135. jnz fill_dwords_with_EOS
  136. ; pad tail bytes
  137. finish_loop: ; 0 < ebx < 4
  138. mov [edi],al
  139. inc edi
  140. fill_tail_zero_bytes:
  141. dec ebx
  142. jnz finish_loop
  143. pop ebx
  144. pop esi
  145. finish:
  146. mov eax,[esp + 8] ; return in eax pointer to dest string
  147. pop edi
  148. ret
  149. ; copy (source) string to (dest). Also look for end of (source) string
  150. main_loop: ; edx contains first dword of source string
  151. mov [edi],edx ; store one more dword
  152. add edi,4 ; kick dest pointer
  153. dec ecx
  154. jz tail_loop_start
  155. main_loop_entrance:
  156. mov edx,7efefeffh
  157. mov eax,dword ptr [esi] ; read 4 bytes (dword)
  158. add edx,eax
  159. xor eax,-1
  160. xor eax,edx
  161. mov edx,[esi] ; it's in cache now
  162. add esi,4 ; kick dest pointer
  163. test eax,81010100h
  164. je short main_loop
  165. ; may have found zero byte in the dword
  166. test dl,dl ; is it byte 0
  167. je short byte_0
  168. test dh,dh ; is it byte 1
  169. je short byte_1
  170. test edx,00ff0000h ; is it byte 2
  171. je short byte_2
  172. test edx,0ff000000h ; is it byte 3
  173. jne short main_loop ; taken if bits 24-30 are clear and bit
  174. ; 31 is set
  175. ; a null character was found, so dest needs to be padded out with null chars
  176. ; to count length.
  177. mov [edi],edx
  178. jmp short fill_with_EOS_dwords
  179. byte_2:
  180. and edx,0ffffh ; fill high 2 bytes with 0
  181. mov [edi],edx
  182. jmp short fill_with_EOS_dwords
  183. byte_1:
  184. and edx,0ffh ; fill high 3 bytes with 0
  185. mov [edi],edx
  186. jmp short fill_with_EOS_dwords
  187. byte_0:
  188. xor edx,edx ; fill whole dword with 0
  189. mov [edi],edx
  190. ; End of string was found. Pad out dest string with dwords of 0
  191. fill_with_EOS_dwords: ; ecx > 0 (ecx is dword counter)
  192. add edi,4
  193. xor eax,eax ; it is instead of ???????????????????
  194. dec ecx
  195. jz fill_tail ; we filled all dwords
  196. fill_dwords_with_EOS:
  197. xor eax,eax
  198. fill_with_EOS_loop:
  199. mov [edi],eax
  200. add edi,4
  201. dec ecx
  202. jnz short fill_with_EOS_loop
  203. fill_tail: ; let's pad tail bytes with zero
  204. and ebx,3 ; ebx = ebx % 4
  205. jnz short finish_loop ; taken, when there are some tail bytes
  206. fill_tail_end:
  207. mov eax,[esp + 10h]
  208. pop ebx
  209. pop esi
  210. pop edi
  211. ret
  212. strncpy endp
  213. end