Subversion Repositories Kolibri OS

Rev

Rev 6781 | Rev 6820 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;;
  4. ;;  Distributed under terms of the GNU General Public License.  ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 6798 $
  9.  
  10. ; NTFS external functions
  11. ;   in:
  12. ; ebx -> parameter structure of sysfunc 70
  13. ; ebp -> NTFS structure
  14. ; esi -> path string in UTF-8
  15. ;   out:
  16. ; eax, ebx = return values for sysfunc 70
  17. iglobal
  18. align 4
  19. ntfs_user_functions:
  20.         dd      ntfs_free
  21.         dd      (ntfs_user_functions_end - ntfs_user_functions - 4) / 4
  22.         dd      ntfs_ReadFile
  23.         dd      ntfs_ReadFolder
  24.         dd      ntfs_CreateFile
  25.         dd      ntfs_WriteFile
  26.         dd      ntfs_SetFileEnd
  27.         dd      ntfs_GetFileInfo
  28.         dd      ntfs_SetFileInfo
  29.         dd      0
  30.         dd      ntfs_Delete
  31.         dd      ntfs_CreateFolder
  32. ntfs_user_functions_end:
  33. endg
  34.  
  35. ; Basic concepts:
  36. ; File is a FileRecord in the $MFT.
  37. ; $MFT is a file, that consists of FileRecords and starts with FileRecord of itself.
  38. ; FileRecord (FILE) consists of a header and attributes.
  39. ; Attribute consists of a header and a body.
  40. ; Attribute's body can be inside (resident) or outside of FileRecord.
  41. ; File's data is a body of $Data (80h) attribute.
  42. ; FileRecords is a data of the $MFT file.
  43. ; Directory is a file, that consists of index nodes.
  44. ; Resident index node is always located in a body of $IndexRoot (90h) attribute.
  45. ; Body of $IndexAllocation (A0h) attribute is always non resident
  46. ;  and consists of IndexRecords.
  47. ; IndexRecord (INDX) consists of a header and an index node.
  48. ; Index node consists of a header and indexes.
  49. ; Index consists of a header and a copy of indexed attribute's body.
  50. ; Directories index $Filename (30h) attribute of all existing files.
  51. ; $IndexRoot and $IndexAllocation attributes of a directory has a name — $I30.
  52.  
  53. ; Offsets:
  54.     ; record header
  55. magic = 0
  56. updateSequenceOffset = 4
  57. updateSequenceSize = 6
  58.     ; FileRecord header
  59. reuseCounter = 16
  60. hardLinkCounter = 12h
  61. attributeOffset = 14h
  62. recordFlags = 16h
  63. recordRealSize = 18h
  64. recordAllocatedSize = 1ch
  65. baseRecordReference = 20h       ; for auxiliary records
  66. baseRecordReuse = 26h
  67. newAttributeID = 28h
  68.     ; attribute header
  69. attributeType = 0
  70. sizeWithHeader = 4
  71. nonResidentFlag = 8
  72. nameLength = 9
  73. nameOffset = 10
  74. attributeFlags = 12
  75. attributeID = 14
  76.     ; resident attribute header
  77. sizeWithoutHeader = 10h
  78. attributeOffset = 14h
  79. indexedFlag = 16h
  80.     ; non resident attribute header
  81. firstVCN = 10h
  82. lastVCN = 18h
  83. dataRunsOffset = 20h
  84. attributeAllocatedSize = 28h
  85. attributeRealSize = 30h
  86. initialDataSize = 38h
  87.     ; $IndexRoot
  88. indexedAttributesType = 0
  89. collationRule = 4
  90. indexRecordSize = 8
  91. indexRecordSizeClus = 12        ; in sectors if less than one cluster
  92. rootNode = 16
  93.     ; IndexRecord header
  94. recordVCN = 16
  95. recordNode = 18h
  96.     ; node header
  97. indexOffset = 0
  98. nodeRealSize = 4
  99. nodeAllocatedSize = 8
  100. nonLeafFlag = 12
  101.     ; $Filename index
  102. fileRecordReference = 0
  103. fileReferenceReuse = 6
  104. indexAllocatedSize = 8
  105. indexRawSize = 10
  106. indexFlags = 12
  107. directoryRecordReference = 16
  108. directoryReferenceReuse = 16h
  109. fileCreated = 18h
  110. fileModified = 20h
  111. recordModified = 28h
  112. fileAccessed = 30h
  113. fileAllocatedSize = 38h
  114. fileRealSize = 40h
  115. fileFlags = 48h
  116. fileNameLength = 50h
  117. namespace = 51h
  118. fileName = 52h
  119.  
  120. struct NTFS PARTITION
  121. Lock                MUTEX   ; Currently operations with one partition
  122. ; can not be executed in parallel since the legacy code is not ready.
  123. sectors_per_cluster dd  ?
  124. mft_cluster         dd  ?   ; location
  125. mftmirr_cluster     dd  ?   ; location
  126. frs_size            dd  ?   ; in bytes
  127. frs_buffer          dd  ?   ; MFT fileRecord buffer
  128. mft_retrieval_end   dd  ?
  129. mftSize             dd  ?   ; in sectors
  130. cur_index_size      dd  ?   ; in sectors
  131. cur_index_buf       dd  ?   ; index node buffer
  132. secondIndexBuffer   dd  ?
  133. BitmapBuffer        dd  ?
  134. BitmapTotalSize     dd  ?   ; bytes reserved
  135. BitmapSize          dd  ?   ; bytes readen
  136. BitmapLocation      dd  ?   ; starting sector
  137. BitmapStart         dd  ?   ; first byte after area, reserved for MFT
  138. mftBitmapBuffer     dd  ?   ; one cluster
  139. mftBitmapSize       dd  ?   ; bytes readen
  140. mftBitmapLocation   dd  ?   ; starting sector
  141.  
  142. attr_size           dq  ?
  143. attr_offs           dd  ?
  144. attr_list           dd  ?
  145. attr_iBaseRecord    dd  ?
  146. cur_attr            dd  ?   ; attribute type
  147. cur_iRecord         dd  ?   ; number of fileRecord in MFT
  148. cur_offs            dd  ?   ; attribute VCN in sectors
  149. cur_size            dd  ?   ; max sectors to read
  150. cur_buf             dd  ?
  151. cur_read            dd  ?   ; bytes readen
  152. cur_tail            dd  ?
  153. cur_subnode_size    dd  ?
  154. LastRead            dd  ?   ; last readen block of sectors
  155. mftLastRead         dd  ?
  156. rootLastRead        dd  ?
  157. nodeLastRead        dd  ?
  158. indexRoot           dd  ?
  159. indexPointer        dd  ?
  160. newRecord           dd  ?
  161. fileDataStart       dd  ?   ; starting cluster
  162. fileDataSize        dd  ?   ; in clusters
  163. fileDataBuffer      dd  ?
  164. fileRealSize        dd  ?   ; in bytes
  165. fragmentCount       db  ?
  166. bCanContinue        db  ?
  167. bFolder             db  ?
  168. bWriteAttr          db  ?   ; Warning: Don't forget to turn off!!!
  169.  
  170. mft_retrieval       rb  512
  171. align0  rb  1024-NTFS.align0
  172. attrlist_buf        rb  1024
  173. attrlist_mft_buf    rb  1024
  174. bitmap_buf          rb  1024
  175. ends
  176.  
  177. ntfs_test_bootsec:
  178. ; in: ebx -> buffer, edx = size of partition
  179. ; out: CF=1 -> invalid
  180. ; 1. Name=='NTFS    '
  181.         cmp     dword [ebx+3], 'NTFS'
  182.         jnz     .no
  183.         cmp     dword [ebx+7], '    '
  184.         jnz     .no
  185. ; 2. Number of bytes per sector is the same as for physical device
  186. ; (that is, 0x200 for hard disk)
  187.         cmp     word [ebx+11], 0x200
  188.         jnz     .no
  189. ; 3. Number of sectors per cluster must be power of 2
  190.         movzx   eax, byte [ebx+13]
  191.         dec     eax
  192.         js      .no
  193.         test    al, [ebx+13]
  194.         jnz     .no
  195. ; 4. FAT parameters must be zero
  196.         cmp     word [ebx+14], 0
  197.         jnz     .no
  198.         cmp     dword [ebx+16], 0
  199.         jnz     .no
  200.         cmp     byte [ebx+20], 0
  201.         jnz     .no
  202.         cmp     word [ebx+22], 0
  203.         jnz     .no
  204.         cmp     dword [ebx+32], 0
  205.         jnz     .no
  206. ; 5. Number of sectors <= partition size
  207.         cmp     dword [ebx+0x2C], 0
  208.         ja      .no
  209.         cmp     [ebx+0x28], edx
  210.         ja      .no
  211. ; 6. $MFT and $MFTMirr clusters must be within partition
  212.         cmp     dword [ebx+0x34], 0
  213.         ja      .no
  214.         push    edx
  215.         movzx   eax, byte [ebx+13]
  216.         mul     dword [ebx+0x30]
  217.         test    edx, edx
  218.         pop     edx
  219.         jnz     .no
  220.         cmp     eax, edx
  221.         ja      .no
  222.         cmp     dword [ebx+0x3C], 0
  223.         ja      .no
  224.         push    edx
  225.         movzx   eax, byte [ebx+13]
  226.         mul     dword [ebx+0x38]
  227.         test    edx, edx
  228.         pop     edx
  229.         jnz     .no
  230.         cmp     eax, edx
  231.         ja      .no
  232. ; 7. Clusters per FRS must be either power of 2 or between -31 and -9
  233.         movsx   eax, byte [ebx+0x40]
  234.         cmp     al, -31
  235.         jl      .no
  236.         cmp     al, -9
  237.         jle     @f
  238.         dec     eax
  239.         js      .no
  240.         test    [ebx+0x40], al
  241.         jnz     .no
  242. @@:         ; 8. Same for clusters per IndexAllocationBuffer
  243.         movsx   eax, byte [ebx+0x44]
  244.         cmp     al, -31
  245.         jl      .no
  246.         cmp     al, -9
  247.         jle     @f
  248.         dec     eax
  249.         js      .no
  250.         test    [ebx+0x44], al
  251.         jnz     .no
  252. @@:         ; OK, this is correct NTFS bootsector
  253.         clc
  254.         ret
  255. .no:        ; No, this bootsector isn't NTFS
  256.         stc
  257.         ret
  258.  
  259. ; Mount if it's a valid NTFS partition.
  260. ntfs_create_partition:
  261. ;   in:
  262. ; ebp -> PARTITION structure
  263. ; ebx -> boot sector
  264. ; ebx+512 -> buffer
  265. ;   out:
  266. ; eax -> NTFS structure, 0 = not NTFS
  267.         cmp     dword [esi+DISK.MediaInfo.SectorSize], 512
  268.         jnz     .nope
  269.         mov     edx, dword [ebp+PARTITION.Length]
  270.         cmp     dword [esp+4], 0
  271.         jz      .boot_read_ok
  272.         add     ebx, 512
  273.         lea     eax, [edx-1]
  274.         call    fs_read32_sys
  275.         test    eax, eax
  276.         jnz     @f
  277.         call    ntfs_test_bootsec
  278.         jnc     .ntfs_setup
  279. @@:
  280.         mov     eax, edx
  281.         shr     eax, 1
  282.         call    fs_read32_sys
  283.         test    eax, eax
  284.         jnz     .nope
  285. .boot_read_ok:
  286.         call    ntfs_test_bootsec
  287.         jnc     .ntfs_setup
  288. .nope:
  289.         xor     eax, eax
  290.         jmp     .exit
  291.  
  292. .ntfs_setup:    ; By given bootsector, initialize some NTFS variables
  293.         stdcall kernel_alloc, 1000h
  294.         test    eax, eax
  295.         jz      .exit
  296.         mov     ecx, dword [ebp+PARTITION.FirstSector]
  297.         mov     dword [eax+NTFS.FirstSector], ecx
  298.         mov     ecx, dword [ebp+PARTITION.FirstSector+4]
  299.         mov     dword [eax+NTFS.FirstSector+4], ecx
  300.         mov     ecx, [ebp+PARTITION.Disk]
  301.         mov     [eax+NTFS.Disk], ecx
  302.         mov     [eax+NTFS.FSUserFunctions], ntfs_user_functions
  303.         mov     [eax+NTFS.bWriteAttr], 0
  304.  
  305.         push    ebx ebp esi
  306.         mov     ebp, eax
  307.         lea     ecx, [ebp+NTFS.Lock]
  308.         call    mutex_init
  309.         movzx   eax, byte [ebx+13]
  310.         mov     [ebp+NTFS.sectors_per_cluster], eax
  311.         mov     eax, [ebx+0x28]
  312.         mov     dword [ebp+NTFS.Length], eax
  313.         and     dword [ebp+NTFS.Length+4], 0
  314.         mov     eax, [ebx+0x30]
  315.         mov     [ebp+NTFS.mft_cluster], eax
  316.         mov     eax, [ebx+0x38]
  317.         mov     [ebp+NTFS.mftmirr_cluster], eax
  318.         movsx   eax, byte [ebx+0x40]
  319.         test    eax, eax
  320.         js      @f
  321.         mul     [ebp+NTFS.sectors_per_cluster]
  322.         shl     eax, 9
  323.         jmp     .1
  324.  
  325. @@:
  326.         neg     eax
  327.         mov     ecx, eax
  328.         mov     eax, 1
  329.         shl     eax, cl
  330. .1:
  331.         mov     [ebp+NTFS.frs_size], eax
  332.         stdcall kernel_alloc, eax
  333.         test    eax, eax
  334.         jz      .fail_free
  335.         mov     [ebp+NTFS.frs_buffer], eax
  336. ; read $MFT disposition
  337.         mov     eax, [ebp+NTFS.mft_cluster]
  338.         mul     [ebp+NTFS.sectors_per_cluster]
  339.         mov     ecx, [ebp+NTFS.frs_size]
  340.         shr     ecx, 9
  341.         mov     ebx, [ebp+NTFS.frs_buffer]
  342.         call    fs_read64_sys
  343.         test    eax, eax
  344.         jnz     .usemirr
  345.         cmp     dword [ebx], 'FILE'
  346.         jnz     .usemirr
  347.         call    ntfs_restore_usa_frs
  348.         jnc     .mftok
  349. .usemirr:
  350.         mov     eax, [ebp+NTFS.mftmirr_cluster]
  351.         mul     [ebp+NTFS.sectors_per_cluster]
  352.         mov     ecx, [ebp+NTFS.frs_size]
  353.         shr     ecx, 9
  354.         mov     ebx, [ebp+NTFS.frs_buffer]
  355.         call    fs_read64_sys
  356.         test    eax, eax
  357.         jnz     .fail_free_frs
  358.         cmp     dword [ebx], 'FILE'
  359.         jnz     .fail_free_frs
  360.         call    ntfs_restore_usa_frs
  361.         jc      .fail_free_frs
  362. .mftok:     ; prepare $MFT retrieval information
  363. ; search for unnamed non-resident $DATA attribute
  364.         movzx   eax, word [ebx+attributeOffset]
  365.         add     eax, ebx
  366. .scandata:
  367.         cmp     dword [eax], -1
  368.         jz      .fail_free_frs
  369.         cmp     dword [eax], 0x80
  370.         jnz     @f
  371.         cmp     byte [eax+nameLength], 0
  372.         jz      .founddata
  373. @@:
  374.         add     eax, [eax+sizeWithHeader]
  375.         jmp     .scandata
  376.  
  377. .founddata:
  378.         cmp     byte [eax+nonResidentFlag], 0
  379.         jz      .fail_free_frs
  380.         movzx   esi, word [eax+dataRunsOffset]
  381.         add     esi, eax
  382.         mov     edx, [eax+attributeAllocatedSize+4]
  383.         mov     eax, [eax+attributeAllocatedSize]
  384.         shrd    eax, edx, 9
  385.         mov     [ebp+NTFS.mftSize], eax
  386.         sub     esp, 10h
  387.         lea     ecx, [ebp+NTFS.mft_retrieval]
  388.         xor     edx, edx
  389. .scanmcb:   ; load descriptions of fragments
  390.         call    ntfs_decode_mcb_entry
  391.         jnc     .scanmcbend
  392.         mov     eax, [esp]      ; block length
  393.         mov     [ecx], eax
  394.         add     edx, [esp+8]    ; block addr
  395.         mov     [ecx+4], edx
  396.         add     ecx, 8
  397.         jmp     .scanmcb
  398.  
  399. .scanmcbend:
  400.         add     esp, 10h
  401.         lea     eax, [ebp+NTFS.attrlist_buf]
  402.         cmp     eax, ecx
  403.         jc      @f
  404.         mov     eax, ecx
  405. @@:
  406.         mov     [ebp+NTFS.mft_retrieval_end], eax
  407. ; allocate index buffers
  408.         stdcall kernel_alloc, 2000h
  409.         test    eax, eax
  410.         jz      .fail_free_frs
  411.         mov     [ebp+NTFS.cur_index_buf], eax
  412.         add     eax, 1000h
  413.         mov     [ebp+NTFS.secondIndexBuffer], eax
  414.         mov     [ebp+NTFS.cur_index_size], 8
  415. ; reserve adress space for bitmap buffer and load some part of bitmap
  416.         mov     eax, dword [ebp+NTFS.Length]
  417.         xor     edx, edx
  418.         div     [ebp+NTFS.sectors_per_cluster]
  419.         shr     eax, 3
  420.         mov     [ebp+NTFS.BitmapTotalSize], eax
  421.         add     eax, 7FFFh
  422.         and     eax, not 7FFFh
  423.         push    eax
  424.         call    alloc_kernel_space
  425.         test    eax, eax
  426.         jz      .failFreeIndex
  427.         mov     [ebp+NTFS.BitmapBuffer], eax
  428.         mov     [ebp+NTFS.cur_buf], eax
  429.         mov     eax, [ebp+NTFS.BitmapTotalSize]
  430.         add     eax, [ebp+NTFS.mft_cluster]
  431.         shr     eax, 3+2        ; reserve 1/8 of partition for $MFT
  432.         shl     eax, 2
  433.         mov     [ebp+NTFS.BitmapStart], eax
  434.         shr     eax, 15
  435.         inc     eax
  436.         shl     eax, 3
  437.         push    eax
  438.         push    eax
  439.         shl     eax, 3
  440.         mov     [ebp+NTFS.cur_size], eax
  441.         call    alloc_pages
  442.         test    eax, eax
  443.         pop     ecx
  444.         jz      .failFreeBitmap
  445.         add     eax, 3
  446.         mov     ebx, [ebp+NTFS.BitmapBuffer]
  447.         call    commit_pages
  448.         mov     [ebp+NTFS.cur_iRecord], 6
  449.         mov     [ebp+NTFS.cur_attr], 0x80
  450.         mov     [ebp+NTFS.cur_offs], 0
  451.         call    ntfs_read_attr
  452.         jc      .failFreeBitmap
  453.         mov     eax, [ebp+NTFS.cur_read]
  454.         mov     [ebp+NTFS.BitmapSize], eax
  455.         mov     eax, [ebp+NTFS.LastRead]
  456.         mov     [ebp+NTFS.BitmapLocation], eax
  457. ; read MFT $BITMAP attribute
  458.         mov     eax, [ebp+NTFS.sectors_per_cluster]
  459.         mov     [ebp+NTFS.cur_size], eax
  460.         shl     eax, 9
  461.         stdcall kernel_alloc, eax
  462.         test    eax, eax
  463.         jz      .failFreeBitmap
  464.         mov     [ebp+NTFS.mftBitmapBuffer], eax
  465.         mov     [ebp+NTFS.cur_buf], eax
  466.         mov     [ebp+NTFS.cur_iRecord], 0
  467.         mov     [ebp+NTFS.cur_attr], 0xB0
  468.         mov     [ebp+NTFS.cur_offs], 0
  469.         call    ntfs_read_attr
  470.         mov     eax, [ebp+NTFS.cur_read]
  471.         cmp     eax, 4
  472.         jc      .failFreeBitmapMFT
  473.         mov     ecx, [ebp+NTFS.attr_offs]
  474.         cmp     byte [ecx+nonResidentFlag], 1
  475.         jnz     .failFreeBitmapMFT
  476.         mov     [ebp+NTFS.mftBitmapSize], eax
  477.         mov     eax, [ebp+NTFS.LastRead]
  478.         mov     [ebp+NTFS.mftBitmapLocation], eax
  479.  
  480.         mov     eax, ebp
  481. .pop_exit:
  482.         pop     esi ebp ebx
  483. .exit:
  484.         cmp     dword [esp+4], 0
  485.         jz      @f
  486.         sub     ebx, 512
  487. @@:
  488.         ret
  489.  
  490. .failFreeBitmapMFT:
  491.         stdcall kernel_free, [ebp+NTFS.mftBitmapBuffer]
  492. .failFreeBitmap:
  493.         stdcall kernel_free, [ebp+NTFS.BitmapBuffer]
  494. .failFreeIndex:
  495.         mov     eax, [ebp+NTFS.cur_index_buf]
  496.         cmp     eax, [ebp+NTFS.secondIndexBuffer]
  497.         jc      @f
  498.         mov     eax, [ebp+NTFS.secondIndexBuffer]
  499. @@:
  500.         stdcall kernel_free, eax
  501. .fail_free_frs:
  502.         stdcall kernel_free, [ebp+NTFS.frs_buffer]
  503. .fail_free:
  504.         stdcall kernel_free, ebp
  505.         xor     eax, eax
  506.         jmp     .pop_exit
  507.  
  508. ntfs_free:
  509.         push    ebx
  510.         mov     ebx, eax
  511.         stdcall kernel_free, [ebx+NTFS.frs_buffer]
  512.         stdcall kernel_free, [ebx+NTFS.mftBitmapBuffer]
  513.         stdcall kernel_free, [ebx+NTFS.BitmapBuffer]
  514.         mov     eax, [ebx+NTFS.cur_index_buf]
  515.         cmp     eax, [ebx+NTFS.secondIndexBuffer]
  516.         jc      @f
  517.         mov     eax, [ebx+NTFS.secondIndexBuffer]
  518. @@:
  519.         stdcall kernel_free, eax
  520.         stdcall kernel_free, ebx
  521.         pop     ebx
  522.         ret
  523.  
  524. ntfs_lock:
  525.         lea     ecx, [ebp+NTFS.Lock]
  526.         jmp     mutex_lock
  527.  
  528. ntfs_unlock:
  529.         lea     ecx, [ebp+NTFS.Lock]
  530.         jmp     mutex_unlock
  531.  
  532. ntfs_read_attr:
  533. ; [ebp+NTFS.bWriteAttr]=1 -> write attribute
  534. ;   in:
  535. ; [ebp+NTFS.cur_iRecord] = number of fileRecord
  536. ; [ebp+NTFS.cur_attr] = attribute type
  537. ; [ebp+NTFS.cur_offs] = attribute VCN in sectors
  538. ; [ebp+NTFS.cur_buf] -> buffer for data
  539. ; [ebp+NTFS.cur_size] = max sectors to read
  540. ;   out:
  541. ; [ebp+NTFS.cur_read] = bytes readen
  542. ; CF=1 -> failed, eax = disk error code, eax=0 -> something with FS
  543.         xor     eax, eax
  544.         pushad
  545.         and     [ebp+NTFS.cur_read], 0
  546.         cmp     [ebp+NTFS.cur_iRecord], 0
  547.         jnz     .nomft
  548.         cmp     [ebp+NTFS.cur_attr], 0x80
  549.         jnz     .nomft
  550. ; precalculated part of $Mft $DATA
  551.         mov     eax, [ebp+NTFS.cur_offs]
  552.         xor     edx, edx
  553.         div     [ebp+NTFS.sectors_per_cluster]
  554.         mov     ebx, edx
  555.         mov     [ebp+NTFS.fragmentCount], 0
  556. ; eax = VCN, ebx = offset in sectors from beginning of cluster
  557.         lea     esi, [ebp+NTFS.mft_retrieval]
  558.         sub     esi, 8
  559. .mftscan:
  560.         add     esi, 8
  561.         cmp     esi, [ebp+NTFS.mft_retrieval_end]
  562.         jz      .nomft
  563.         mov     ecx, [esi+4]
  564.         sub     eax, [esi]
  565.         jnc     .mftscan
  566.         add     ecx, eax
  567.         add     ecx, [esi]
  568.         neg     eax
  569.         mul     [ebp+NTFS.sectors_per_cluster]
  570.         xchg    eax, ecx
  571.         mul     [ebp+NTFS.sectors_per_cluster]
  572.         sub     ecx, ebx
  573.         add     eax, ebx
  574.         mov     ebx, [ebp+NTFS.cur_buf]
  575.         cmp     ecx, [ebp+NTFS.cur_size]
  576.         jb      @f
  577.         mov     ecx, [ebp+NTFS.cur_size]
  578. @@:
  579.         mov     [ebp+NTFS.LastRead], eax
  580.         mov     edi, ecx
  581.         call    fs_read64_sys
  582.         test    eax, eax
  583.         jnz     .errret
  584.         sub     [ebp+NTFS.cur_size], edi
  585.         add     [ebp+NTFS.cur_offs], edi
  586.         shl     edi, 9
  587.         add     [ebp+NTFS.cur_read], edi
  588.         add     [ebp+NTFS.cur_buf], edi
  589.         inc     [ebp+NTFS.fragmentCount]
  590.         xor     eax, eax
  591.         xor     ebx, ebx
  592.         cmp     [ebp+NTFS.cur_size], eax
  593.         jz      @f
  594.         jmp     .mftscan
  595.  
  596. .errret2_pop:
  597.         xor     eax, eax
  598. .errret_pop:
  599.         pop     ecx
  600.         pop     ecx
  601. .errret:
  602.         mov     [esp+28], eax
  603.         stc
  604. @@:
  605.         popad
  606.         ret
  607.  
  608. .nomft:
  609. ; 1. Read file record.
  610. ; N.B. This will do recursive call of read_attr for $MFT::$Data.
  611.         mov     eax, [ebp+NTFS.cur_iRecord]
  612.         and     [ebp+NTFS.attr_list], 0
  613.         or      dword [ebp+NTFS.attr_size+4], -1
  614.         or      [ebp+NTFS.attr_iBaseRecord], -1
  615.         call    ntfs_read_file_record
  616.         jc      .errret
  617. ; 2. Find required attribute.
  618.         mov     eax, [ebp+NTFS.frs_buffer]
  619. ; a) For auxiliary records, read base record.
  620. ; If base record is present, base iRecord may be 0 (for $Mft),
  621. ; but SequenceNumber is nonzero.
  622.         cmp     word [eax+baseRecordReuse], 0
  623.         jz      @f
  624.         mov     eax, [eax+baseRecordReference]
  625. .beginfindattr:
  626.         call    ntfs_read_file_record
  627.         jc      .errret
  628.         jmp     @f
  629.  
  630. .newAttribute:
  631.         pushad
  632.         and     [ebp+NTFS.cur_read], 0
  633. @@:
  634. ; b) Scan for required attribute and for $ATTR_LIST
  635.         mov     eax, [ebp+NTFS.frs_buffer]
  636.         movzx   ecx, word [eax+attributeOffset]
  637.         add     eax, ecx
  638.         mov     ecx, [ebp+NTFS.cur_attr]
  639.         and     [ebp+NTFS.attr_offs], 0
  640. .scanattr:
  641.         cmp     dword [eax], -1
  642.         jz      .scandone
  643.         cmp     dword [eax], ecx
  644.         jz      .okattr
  645.         cmp     [ebp+NTFS.attr_iBaseRecord], -1
  646.         jnz     .scancont
  647.         cmp     dword [eax], 0x20       ; $ATTR_LIST
  648.         jnz     .scancont
  649.         mov     [ebp+NTFS.attr_list], eax
  650.         jmp     .scancont
  651.  
  652. .okattr:
  653. ; ignore named $DATA attributes (aka NTFS streams)
  654.         cmp     ecx, 0x80
  655.         jnz     @f
  656.         cmp     byte [eax+nameLength], 0
  657.         jnz     .scancont
  658. @@:
  659.         mov     [ebp+NTFS.attr_offs], eax
  660. .scancont:
  661.         add     eax, [eax+sizeWithHeader]
  662.         jmp     .scanattr
  663.  
  664. .continue:
  665.         pushad
  666.         and     [ebp+NTFS.cur_read], 0
  667. .scandone:
  668. ; c) Check for required offset and length
  669.         mov     ecx, [ebp+NTFS.attr_offs]
  670.         jecxz   .noattr
  671.         push    [ebp+NTFS.cur_size]
  672.         push    [ebp+NTFS.cur_read]
  673.         call    .doreadattr
  674.         pop     edx
  675.         pop     ecx
  676.         jc      .ret
  677.         cmp     [ebp+NTFS.bCanContinue], 0
  678.         jz      .ret
  679.         sub     edx, [ebp+NTFS.cur_read]
  680.         neg     edx
  681.         shr     edx, 9
  682.         sub     ecx, edx
  683.         mov     [ebp+NTFS.cur_size], ecx
  684.         jz      .ret
  685. .noattr:
  686.         cmp     [ebp+NTFS.cur_attr], 0x20
  687.         jz      @f
  688.         mov     ecx, [ebp+NTFS.attr_list]
  689.         test    ecx, ecx
  690.         jnz     .lookattr
  691.         and     dword [esp+28], 0
  692.         cmp     [ebp+NTFS.attr_offs], 1     ; define CF
  693. .ret:
  694.         popad
  695.         ret
  696.  
  697. .lookattr:
  698. ; required attribute or required offset was not found in base record;
  699. ; it may be present in auxiliary records;
  700. ; scan $ATTR_LIST
  701.         mov     eax, [ebp+NTFS.attr_iBaseRecord]
  702.         cmp     eax, -1
  703.         jz      @f
  704.         call    ntfs_read_file_record
  705.         jc      .errret
  706.         or      [ebp+NTFS.attr_iBaseRecord], -1
  707. @@:
  708.         push    [ebp+NTFS.cur_offs]
  709.         push    [ebp+NTFS.cur_size]
  710.         push    [ebp+NTFS.cur_read]
  711.         push    [ebp+NTFS.cur_buf]
  712.         push    dword [ebp+NTFS.attr_size]
  713.         push    dword [ebp+NTFS.attr_size+4]
  714.         or      dword [ebp+NTFS.attr_size+4], -1
  715.         and     [ebp+NTFS.cur_offs], 0
  716.         mov     [ebp+NTFS.cur_size], 2
  717.         and     [ebp+NTFS.cur_read], 0
  718.         lea     eax, [ebp+NTFS.attrlist_buf]
  719.         cmp     [ebp+NTFS.cur_iRecord], 0
  720.         jnz     @f
  721.         lea     eax, [ebp+NTFS.attrlist_mft_buf]
  722. @@:
  723.         mov     [ebp+NTFS.cur_buf], eax
  724.         push    eax
  725.         call    .doreadattr
  726.         pop     esi
  727.         mov     edx, 1
  728.         pop     dword [ebp+NTFS.attr_size+4]
  729.         pop     dword [ebp+NTFS.attr_size]
  730.         mov     ecx, [ebp+NTFS.cur_read]
  731.         pop     [ebp+NTFS.cur_buf]
  732.         pop     [ebp+NTFS.cur_read]
  733.         pop     [ebp+NTFS.cur_size]
  734.         pop     [ebp+NTFS.cur_offs]
  735.         jc      .errret
  736.         or      edi, -1
  737.         lea     ecx, [ecx+esi-1Ah]
  738. .scanliststart:
  739.         push    ecx
  740.         mov     eax, [ebp+NTFS.cur_attr]
  741. .scanlist:
  742.         cmp     esi, [esp]
  743.         jae     .scanlistdone
  744.         cmp     eax, [esi]
  745.         jz      @f
  746. .scanlistcont:
  747.         movzx   ecx, word [esi+4]
  748.         add     esi, ecx
  749.         jmp     .scanlist
  750.  
  751. @@:
  752. ; ignore named $DATA attributes (aka NTFS streams)
  753.         cmp     eax, 0x80
  754.         jnz     @f
  755.         cmp     byte [esi+6], 0
  756.         jnz     .scanlistcont
  757. @@:
  758.         push    eax
  759.         mov     eax, [esi+8]
  760.         test    eax, eax
  761.         jnz     .testf
  762.         cmp     dword [ebp+NTFS.attr_size+4], -1
  763.         jnz     .testfz
  764. ; if attribute is in auxiliary records, its size is defined only in first
  765.         mov     eax, [esi+10h]
  766.         call    ntfs_read_file_record
  767.         jc      .errret_pop
  768.         mov     eax, [ebp+NTFS.frs_buffer]
  769.         movzx   ecx, word [eax+14h]
  770.         add     eax, ecx
  771.         mov     ecx, [ebp+NTFS.cur_attr]
  772. @@:
  773.         cmp     dword [eax], -1
  774.         jz      .errret2_pop
  775.         cmp     dword [eax], ecx
  776.         jz      @f
  777. .l1:
  778.         add     eax, [eax+4]
  779.         jmp     @b
  780.  
  781. @@:
  782.         cmp     eax, 0x80
  783.         jnz     @f
  784.         cmp     byte [eax+9], 0
  785.         jnz     .l1
  786. @@:
  787.         cmp     byte [eax+8], 0
  788.         jnz     .sdnores
  789.         mov     eax, [eax+10h]
  790.         mov     dword [ebp+NTFS.attr_size], eax
  791.         and     dword [ebp+NTFS.attr_size+4], 0
  792.         jmp     .testfz
  793.  
  794. .sdnores:
  795.         mov     ecx, [eax+30h]
  796.         mov     dword [ebp+NTFS.attr_size], ecx
  797.         mov     ecx, [eax+34h]
  798.         mov     dword [ebp+NTFS.attr_size+4], ecx
  799. .testfz:
  800.         xor     eax, eax
  801. .testf:
  802.         imul    eax, [ebp+NTFS.sectors_per_cluster]
  803.         cmp     eax, [ebp+NTFS.cur_offs]
  804.         pop     eax
  805.         ja      @f
  806.         mov     edi, [esi+10h]  ; keep previous iRecord
  807.         jmp     .scanlistcont
  808.  
  809. @@:
  810.         pop     ecx
  811. .scanlistfound:
  812.         cmp     edi, -1
  813.         jz      .ret
  814.         mov     eax, [ebp+NTFS.cur_iRecord]
  815.         mov     [ebp+NTFS.attr_iBaseRecord], eax
  816.         mov     eax, edi
  817.         jmp     .beginfindattr
  818.  
  819. .scanlistdone:
  820.         pop     ecx
  821.         sub     ecx, ebp
  822.         sub     ecx, NTFS.attrlist_buf-1Ah
  823.         cmp     [ebp+NTFS.cur_iRecord], 0
  824.         jnz     @f
  825.         sub     ecx, NTFS.attrlist_mft_buf-NTFS.attrlist_buf
  826. @@:
  827.         cmp     ecx, 0x400
  828.         jnz     .scanlistfound
  829.         inc     edx
  830.         push    esi edi
  831.         lea     esi, [ebp+NTFS.attrlist_buf+0x200]
  832.         lea     edi, [ebp+NTFS.attrlist_buf]
  833.         cmp     [ebp+NTFS.cur_iRecord], 0
  834.         jnz     @f
  835.         lea     esi, [ebp+NTFS.attrlist_mft_buf+0x200]
  836.         lea     edi, [ebp+NTFS.attrlist_mft_buf]
  837. @@:
  838.         mov     ecx, 0x200/4
  839.         rep movsd
  840.         mov     eax, edi
  841.         pop     edi esi
  842.         sub     esi, 0x200
  843.         push    [ebp+NTFS.cur_offs]
  844.         push    [ebp+NTFS.cur_size]
  845.         push    [ebp+NTFS.cur_read]
  846.         push    [ebp+NTFS.cur_buf]
  847.         push    dword [ebp+NTFS.attr_size]
  848.         push    dword [ebp+NTFS.attr_size+4]
  849.         or      dword [ebp+NTFS.attr_size+4], -1
  850.         mov     [ebp+NTFS.cur_offs], edx
  851.         mov     [ebp+NTFS.cur_size], 1
  852.         and     [ebp+NTFS.cur_read], 0
  853.         mov     [ebp+NTFS.cur_buf], eax
  854.         mov     ecx, [ebp+NTFS.attr_list]
  855.         push    esi edx edi
  856.         call    .doreadattr
  857.         pop     edi edx esi
  858.         mov     ecx, [ebp+NTFS.cur_read]
  859.         pop     dword [ebp+NTFS.attr_size+4]
  860.         pop     dword [ebp+NTFS.attr_size]
  861.         pop     [ebp+NTFS.cur_buf]
  862.         pop     [ebp+NTFS.cur_read]
  863.         pop     [ebp+NTFS.cur_size]
  864.         pop     [ebp+NTFS.cur_offs]
  865.         jc      .errret
  866.         lea     ecx, [ecx+ebp+NTFS.attrlist_buf+0x200-0x1A]
  867.         cmp     [ebp+NTFS.cur_iRecord], 0
  868.         jnz     .scanliststart
  869.         add     ecx, NTFS.attrlist_mft_buf-NTFS.attrlist_buf
  870.         jmp     .scanliststart
  871.  
  872. .doreadattr:
  873.         mov     [ebp+NTFS.bCanContinue], 0
  874.         cmp     byte [ecx+nonResidentFlag], 0
  875.         jnz     .nonresident
  876.         mov     eax, [ecx+sizeWithoutHeader]
  877.         mov     esi, eax
  878.         mov     edx, [ebp+NTFS.cur_offs]
  879.         shr     eax, 9
  880.         cmp     eax, edx
  881.         jb      .okret
  882.         shl     edx, 9
  883.         sub     esi, edx
  884.         movzx   eax, word [ecx+attributeOffset]
  885.         add     edx, eax
  886.         add     edx, ecx        ; edx -> data
  887.         mov     eax, [ebp+NTFS.cur_size]
  888.         cmp     eax, (0xFFFFFFFF shr 9)+1
  889.         jbe     @f
  890.         mov     eax, (0xFFFFFFFF shr 9)+1
  891. @@:
  892.         shl     eax, 9
  893.         cmp     eax, esi
  894.         jbe     @f
  895.         mov     eax, esi
  896. @@:
  897. ; eax = length, edx -> data
  898.         mov     [ebp+NTFS.cur_read], eax
  899.         mov     ecx, eax
  900.         mov     eax, edx
  901.         mov     ebx, [ebp+NTFS.cur_buf]
  902.         call    memmove
  903.         and     [ebp+NTFS.cur_size], 0      ; CF=0
  904.         ret
  905.  
  906. .nonresident:
  907. ; Not all auxiliary records contain correct FileSize info
  908.         mov     eax, dword [ebp+NTFS.attr_size]
  909.         mov     edx, dword [ebp+NTFS.attr_size+4]
  910.         cmp     edx, -1
  911.         jnz     @f
  912.         mov     eax, [ecx+attributeRealSize]
  913.         mov     edx, [ecx+attributeRealSize+4]
  914.         mov     dword [ebp+NTFS.attr_size], eax
  915.         mov     dword [ebp+NTFS.attr_size+4], edx
  916. @@:
  917.         add     eax, 0x1FF
  918.         adc     edx, 0
  919.         shrd    eax, edx, 9
  920.         sub     eax, [ebp+NTFS.cur_offs]
  921.         ja      @f
  922. ; return with nothing read
  923.         and     [ebp+NTFS.cur_size], 0
  924. .okret:
  925.         clc
  926.         ret
  927.  
  928. @@:
  929. ; reduce read length
  930.         and     [ebp+NTFS.cur_tail], 0
  931.         cmp     [ebp+NTFS.cur_size], eax
  932.         jb      @f
  933.         mov     [ebp+NTFS.cur_size], eax
  934.         mov     eax, dword [ebp+NTFS.attr_size]
  935.         and     eax, 0x1FF
  936.         mov     [ebp+NTFS.cur_tail], eax
  937. @@:
  938.         mov     eax, [ebp+NTFS.cur_offs]
  939.         xor     edx, edx
  940.         div     [ebp+NTFS.sectors_per_cluster]
  941.         sub     eax, [ecx+firstVCN]
  942.         jb      .okret
  943.         mov     ebx, edx
  944. ; eax = starting cluster, ebx = sector in the cluster
  945.         cmp     [ebp+NTFS.cur_attr], 0x80
  946.         jnz     .sys
  947.         cmp     [ebp+NTFS.cur_iRecord], 0
  948.         jz      .sys
  949.         push    fs_read64_app
  950.         cmp     [ebp+NTFS.bWriteAttr], 1
  951.         jnz     @f
  952.         mov     dword[esp], fs_write64_app
  953.         jmp     @f
  954.  
  955. .sys:
  956.         push    fs_read64_sys
  957. @@:
  958.         sub     esp, 10h
  959.         movzx   esi, word [ecx+dataRunsOffset]
  960.         add     esi, ecx
  961.         xor     edi, edi
  962.         mov     [ebp+NTFS.fragmentCount], 0
  963. .readloop:
  964.         call    ntfs_decode_mcb_entry
  965.         jnc     .break
  966.         add     edi, [esp+8]
  967.         sub     eax, [esp]
  968.         jae     .readloop
  969.         mov     ecx, edi
  970.         add     ecx, eax
  971.         add     ecx, [esp]
  972.         neg     eax
  973.         mul     [ebp+NTFS.sectors_per_cluster]
  974.         xchg    eax, ecx
  975.         mul     [ebp+NTFS.sectors_per_cluster]
  976.         sub     ecx, ebx
  977.         add     eax, ebx
  978.         mov     ebx, [ebp+NTFS.cur_buf]
  979.         cmp     ecx, [ebp+NTFS.cur_size]
  980.         jb      @f
  981.         mov     ecx, [ebp+NTFS.cur_size]
  982. @@:
  983.         mov     [ebp+NTFS.LastRead], eax
  984.         push    ecx
  985.         call    dword[esp+14h]
  986.         pop     ecx
  987.         test    eax, eax
  988.         jnz     .errread2
  989.         sub     [ebp+NTFS.cur_size], ecx
  990.         add     [ebp+NTFS.cur_offs], ecx
  991.         shl     ecx, 9
  992.         add     [ebp+NTFS.cur_read], ecx
  993.         add     [ebp+NTFS.cur_buf], ecx
  994.         inc     [ebp+NTFS.fragmentCount]
  995.         xor     eax, eax
  996.         xor     ebx, ebx
  997.         cmp     [ebp+NTFS.cur_size], 0
  998.         jnz     .readloop
  999.         add     esp, 14h
  1000.         mov     eax, [ebp+NTFS.cur_tail]
  1001.         test    eax, eax
  1002.         jz      @f
  1003.         sub     eax, 0x200
  1004.         add     [ebp+NTFS.cur_read], eax
  1005. @@:
  1006.         clc
  1007.         ret
  1008.  
  1009. .errread2:
  1010.         add     esp, 14h
  1011.         stc
  1012.         ret
  1013.  
  1014. .break:
  1015.         add     esp, 14h        ; CF=0
  1016.         mov     [ebp+NTFS.bCanContinue], 1
  1017.         ret
  1018.  
  1019. ntfs_read_file_record:
  1020. ; in: eax = iRecord
  1021. ; out: [ebp+NTFS.frs_buffer] -> file record
  1022. ; CF=1 -> failed, eax = disk error code, eax=0 -> something with FS
  1023.     ; Read attr $DATA of $Mft, starting from eax*[ebp+NTFS.frs_size]
  1024.         push    ecx edx
  1025.         mov     ecx, [ebp+NTFS.frs_size]
  1026.         mul     ecx
  1027.         shrd    eax, edx, 9
  1028.         shr     edx, 9
  1029.         jnz     .errret
  1030.         push    [ebp+NTFS.attr_iBaseRecord]
  1031.         push    [ebp+NTFS.attr_offs]
  1032.         push    [ebp+NTFS.attr_list]
  1033.         push    dword [ebp+NTFS.attr_size+4]
  1034.         push    dword [ebp+NTFS.attr_size]
  1035.         push    [ebp+NTFS.cur_iRecord]
  1036.         push    [ebp+NTFS.cur_attr]
  1037.         push    [ebp+NTFS.cur_offs]
  1038.         push    [ebp+NTFS.cur_size]
  1039.         push    [ebp+NTFS.cur_buf]
  1040.         push    [ebp+NTFS.cur_read]
  1041.         mov     [ebp+NTFS.cur_attr], 0x80   ; $DATA
  1042.         and     [ebp+NTFS.cur_iRecord], 0   ; $Mft
  1043.         mov     [ebp+NTFS.cur_offs], eax
  1044.         shr     ecx, 9
  1045.         mov     [ebp+NTFS.cur_size], ecx
  1046.         mov     eax, [ebp+NTFS.frs_buffer]
  1047.         mov     [ebp+NTFS.cur_buf], eax
  1048.         call    ntfs_read_attr
  1049.         mov     edx, [ebp+NTFS.cur_read]
  1050.         pop     [ebp+NTFS.cur_read]
  1051.         pop     [ebp+NTFS.cur_buf]
  1052.         pop     [ebp+NTFS.cur_size]
  1053.         pop     [ebp+NTFS.cur_offs]
  1054.         pop     [ebp+NTFS.cur_attr]
  1055.         pop     [ebp+NTFS.cur_iRecord]
  1056.         pop     dword [ebp+NTFS.attr_size]
  1057.         pop     dword [ebp+NTFS.attr_size+4]
  1058.         pop     [ebp+NTFS.attr_list]
  1059.         pop     [ebp+NTFS.attr_offs]
  1060.         pop     [ebp+NTFS.attr_iBaseRecord]
  1061.         jc      .ret
  1062.         cmp     edx, [ebp+NTFS.frs_size]
  1063.         jnz     .errret
  1064.         mov     eax, [ebp+NTFS.LastRead]
  1065.         mov     [ebp+NTFS.mftLastRead], eax
  1066.         mov     eax, [ebp+NTFS.frs_buffer]
  1067.         cmp     dword [eax], 'FILE'
  1068.         jnz     .errret
  1069.         push    ebx
  1070.         mov     ebx, eax
  1071.         call    ntfs_restore_usa_frs
  1072.         pop     ebx
  1073.         jc      .errret
  1074. .ret:
  1075.         pop     edx ecx
  1076.         ret
  1077.  
  1078. .errret:
  1079.         pop     edx ecx
  1080.         xor     eax, eax
  1081.         stc
  1082.         ret
  1083.  
  1084. ntfs_restore_usa_frs:
  1085.         mov     eax, [ebp+NTFS.frs_size]
  1086. ntfs_restore_usa:
  1087. ;   in:
  1088. ; ebx -> record
  1089. ; eax = size in bytes
  1090.         pushad
  1091.         shr     eax, 9
  1092.         mov     ecx, eax
  1093.         inc     eax
  1094.         cmp     [ebx+updateSequenceSize], ax
  1095.         jnz     .err
  1096.         movzx   eax, word [ebx+updateSequenceOffset]
  1097.         lea     esi, [eax+ebx]
  1098.         lodsw
  1099.         mov     edx, eax
  1100.         lea     edi, [ebx+0x1FE]
  1101. @@:
  1102.         cmp     [edi], dx
  1103.         jnz     .err
  1104.         lodsw
  1105.         stosw
  1106.         add     edi, 0x1FE
  1107.         loop    @b
  1108.         popad
  1109.         clc
  1110.         ret
  1111.  
  1112. .err:
  1113.         popad
  1114.         stc
  1115.         ret
  1116.  
  1117. ntfs_decode_mcb_entry:
  1118. ;   in:
  1119. ; esi -> MCB entry
  1120. ; esp -> buffer (16 bytes)
  1121. ;   out:
  1122. ; esi -> next MCB entry
  1123. ; esp -> data run size
  1124. ; esp+8 -> cluster (delta)
  1125. ; CF=0 -> MCB end
  1126.         push    eax ecx edi
  1127.         lea     edi, [esp+16]
  1128.         xor     eax, eax
  1129.         lodsb
  1130.         test    al, al
  1131.         jz      .end
  1132.         mov     ecx, eax
  1133.         and     ecx, 0xF
  1134.         cmp     ecx, 8
  1135.         ja      .end
  1136.         push    ecx
  1137.         rep movsb
  1138.         pop     ecx
  1139.         sub     ecx, 8
  1140.         neg     ecx
  1141.         cmp     byte [esi-1], 80h
  1142.         jae     .end
  1143.         push    eax
  1144.         xor     eax, eax
  1145.         rep stosb
  1146.         pop     ecx
  1147.         shr     ecx, 4
  1148.         cmp     ecx, 8
  1149.         ja      .end
  1150.         push    ecx
  1151.         rep movsb
  1152.         pop     ecx
  1153.         sub     ecx, 8
  1154.         neg     ecx
  1155.         cmp     byte [esi-1], 80h
  1156.         cmc
  1157.         sbb     eax, eax
  1158.         rep stosb
  1159.         stc
  1160. .end:
  1161.         pop     edi ecx eax
  1162.         ret
  1163.  
  1164. ntfs_find_lfn:
  1165. ; in: esi -> path string in UTF-8
  1166. ;   out:
  1167. ; [ebp+NTFS.cur_iRecord] = target fileRecord
  1168. ; eax -> target index in the node
  1169. ; [ebp+NTFS.LastRead] = target node location
  1170. ; [ebp+NTFS.indexPointer] -> index, that points the target subnode
  1171. ; [ebp+NTFS.nodeLastRead] = branch node location
  1172. ; [ebp+NTFS.indexRoot] -> attribute
  1173. ; [ebp+NTFS.rootLastRead] = directory fileRecord location
  1174. ; [ebp+NTFS.cur_size] = index record size in sectors
  1175. ; [ebp+NTFS.cur_subnode_size] = index record size in clusters or sectors
  1176. ; CF=1 -> file not found, eax=0 -> error
  1177.         mov     [ebp+NTFS.cur_iRecord], 5   ; start from root directory
  1178. .doit2:
  1179.         mov     [ebp+NTFS.cur_attr], 0x90   ; $INDEX_ROOT
  1180.         and     [ebp+NTFS.cur_offs], 0
  1181.         mov     eax, [ebp+NTFS.cur_index_size]
  1182.         mov     [ebp+NTFS.cur_size], eax
  1183.         mov     eax, [ebp+NTFS.cur_index_buf]
  1184.         mov     [ebp+NTFS.cur_buf], eax
  1185.         call    ntfs_read_attr
  1186.         mov     eax, 0
  1187.         jc      .ret
  1188.         cmp     [ebp+NTFS.cur_read], 0x20
  1189.         jc      .ret
  1190.         push    esi
  1191.         pushad
  1192.         mov     esi, [ebp+NTFS.cur_index_buf]
  1193.         mov     eax, [esi+indexRecordSize]
  1194.         shr     eax, 9
  1195.         cmp     [ebp+NTFS.cur_index_size], eax
  1196.         jc      .realloc
  1197.         mov     [ebp+NTFS.cur_size], eax
  1198.         mov     al, [esi+indexRecordSizeClus]
  1199.         mov     [ebp+NTFS.cur_subnode_size], eax
  1200.         add     esi, rootNode
  1201.         mov     eax, [esi+nodeRealSize]
  1202.         add     eax, rootNode
  1203.         cmp     [ebp+NTFS.cur_read], eax
  1204.         jc      .err
  1205.         mov     eax, [ebp+NTFS.mftLastRead]
  1206.         mov     [ebp+NTFS.rootLastRead], eax
  1207.         mov     eax, [ebp+NTFS.attr_offs]
  1208.         mov     [ebp+NTFS.indexRoot], eax
  1209. .scanloop:  ; esi -> current index node
  1210.         add     esi, [esi+indexOffset]
  1211. .scanloopint:
  1212.         push    esi
  1213.         test    byte [esi+indexFlags], 2
  1214.         jnz     .subnode
  1215.         movzx   ecx, byte [esi+fileNameLength]
  1216.         lea     edi, [esi+fileName]
  1217.         mov     esi, [esp+8]
  1218. @@:
  1219.         call    utf8to16
  1220.         cmp     ax, '/'
  1221.         jz      .subnode
  1222.         call    utf16toUpper
  1223.         push    eax
  1224.         mov     ax, [edi]
  1225.         call    utf16toUpper
  1226.         cmp     [esp], ax
  1227.         pop     eax
  1228.         jc      .subnode
  1229.         jnz     .scanloopcont
  1230.         add     edi, 2
  1231.         loop    @b
  1232.         call    utf8to16
  1233.         cmp     ax, '/'
  1234.         jz      .found
  1235.         test    ax, ax
  1236.         jz      .found
  1237. .scanloopcont:
  1238.         pop     esi
  1239.         movzx   eax, word [esi+indexAllocatedSize]
  1240.         add     esi, eax
  1241.         jmp     .scanloopint
  1242.  
  1243. .realloc:
  1244.         mov     edi, eax
  1245.         mov     eax, [esi+indexRecordSize]
  1246.         shl     eax, 1
  1247.         stdcall kernel_alloc, eax
  1248.         test    eax, eax
  1249.         jz      .err
  1250.         mov     edx, [ebp+NTFS.cur_index_buf]
  1251.         cmp     edx, [ebp+NTFS.secondIndexBuffer]
  1252.         jc      @f
  1253.         mov     edx, [ebp+NTFS.secondIndexBuffer]
  1254. @@:
  1255.         mov     [ebp+NTFS.cur_index_buf], eax
  1256.         add     eax, [esi+indexRecordSize]
  1257.         mov     [ebp+NTFS.secondIndexBuffer], eax
  1258.         mov     [ebp+NTFS.cur_index_size], edi
  1259.         stdcall kernel_free, edx
  1260.         popad
  1261.         pop     eax
  1262.         jmp     .doit2
  1263.  
  1264. .notfound:
  1265.         mov     [esp+28], esi
  1266. .err:
  1267.         popad
  1268.         stc
  1269. .ret2:
  1270.         pop     esi
  1271. .ret:
  1272.         ret
  1273.  
  1274. .subnode:
  1275.         pop     esi
  1276.         test    byte [esi+indexFlags], 1
  1277.         jz      .notfound
  1278.         mov     eax, [ebp+NTFS.LastRead]
  1279.         mov     [ebp+NTFS.nodeLastRead], eax
  1280.         mov     [ebp+NTFS.indexPointer], esi
  1281.         movzx   eax, word [esi+indexAllocatedSize]
  1282.         mov     eax, [esi+eax-8]
  1283.         mov     edx, [ebp+NTFS.cur_size]
  1284.         push    edx
  1285.         cmp     edx, [ebp+NTFS.cur_subnode_size]
  1286.         jz      @f
  1287.         mul     [ebp+NTFS.sectors_per_cluster]
  1288. @@:
  1289.         mov     esi, [ebp+NTFS.cur_index_buf]
  1290.         xchg    [ebp+NTFS.secondIndexBuffer], esi
  1291.         mov     [ebp+NTFS.cur_index_buf], esi
  1292.         mov     [ebp+NTFS.cur_buf], esi
  1293.         mov     [ebp+NTFS.cur_attr], 0xA0   ; $INDEX_ALLOCATION
  1294.         mov     [ebp+NTFS.cur_offs], eax
  1295.         call    ntfs_read_attr.newAttribute
  1296.         pop     eax
  1297.         mov     [ebp+NTFS.cur_size], eax
  1298.         shl     eax, 9
  1299.         cmp     [ebp+NTFS.cur_read], eax
  1300.         jnz     .err
  1301.         cmp     dword [esi], 'INDX'
  1302.         jnz     .err
  1303.         mov     ebx, esi
  1304.         call    ntfs_restore_usa
  1305.         jc      .err
  1306.         add     esi, recordNode
  1307.         jmp     .scanloop
  1308.  
  1309. .found:
  1310.         mov     [esp+8], esi
  1311.         pop     eax
  1312.         mov     [esp+28], eax
  1313.         mov     eax, [eax+fileRecordReference]
  1314.         mov     [ebp+NTFS.cur_iRecord], eax
  1315.         popad
  1316.         cmp     byte [esi-1], 0
  1317.         jz      .ret2
  1318.         pop     eax
  1319.         jmp     .doit2
  1320.  
  1321. ;----------------------------------------------------------------
  1322. ntfs_ReadFile:
  1323.         cmp     byte [esi], 0
  1324.         jnz     @f
  1325.         or      ebx, -1
  1326.         movi    eax, ERROR_ACCESS_DENIED
  1327.         ret
  1328.  
  1329. @@:
  1330.         call    ntfs_lock
  1331.         call    ntfs_find_lfn
  1332.         jnc     .found
  1333.         call    ntfs_unlock
  1334.         or      ebx, -1
  1335.         movi    eax, ERROR_FILE_NOT_FOUND
  1336.         ret
  1337.  
  1338. .found:
  1339.         mov     [ebp+NTFS.cur_attr], 0x80   ; $DATA
  1340.         and     [ebp+NTFS.cur_offs], 0
  1341.         and     [ebp+NTFS.cur_size], 0
  1342.         call    ntfs_read_attr
  1343.         jnc     @f
  1344.         call    ntfs_unlock
  1345.         or      ebx, -1
  1346.         movi    eax, ERROR_ACCESS_DENIED
  1347.         ret
  1348.  
  1349. @@:
  1350.         pushad
  1351.         and     dword [esp+10h], 0
  1352.         xor     eax, eax
  1353.         cmp     dword [ebx+8], 0x200
  1354.         jb      @f
  1355. .eof0:
  1356.         popad
  1357.         xor     ebx, ebx
  1358. .eof:
  1359.         call    ntfs_unlock
  1360.         movi    eax, ERROR_END_OF_FILE
  1361.         ret
  1362.  
  1363. @@:
  1364.         mov     ecx, [ebx+12]
  1365.         mov     edx, [ebx+16]
  1366.         mov     eax, [ebx+4]
  1367.         test    eax, 0x1FF
  1368.         jz      .alignedstart
  1369.         push    edx
  1370.         mov     edx, [ebx+8]
  1371.         shrd    eax, edx, 9
  1372.         pop     edx
  1373.         mov     [ebp+NTFS.cur_offs], eax
  1374.         mov     [ebp+NTFS.cur_size], 1
  1375.         lea     eax, [ebp+NTFS.bitmap_buf]
  1376.         mov     [ebp+NTFS.cur_buf], eax
  1377.         call    ntfs_read_attr.continue
  1378.         mov     eax, [ebx+4]
  1379.         and     eax, 0x1FF
  1380.         lea     esi, [ebp+NTFS.bitmap_buf+eax]
  1381.         sub     eax, [ebp+NTFS.cur_read]
  1382.         jae     .eof0
  1383.         neg     eax
  1384.         push    ecx
  1385.         cmp     ecx, eax
  1386.         jb      @f
  1387.         mov     ecx, eax
  1388. @@:
  1389.         mov     [esp+10h+4], ecx
  1390.         mov     edi, edx
  1391.         rep movsb
  1392.         mov     edx, edi
  1393.         pop     ecx
  1394.         sub     ecx, [esp+10h]
  1395.         jnz     @f
  1396. .retok:
  1397.         popad
  1398.         call    ntfs_unlock
  1399.         xor     eax, eax
  1400.         ret
  1401.  
  1402. @@:
  1403.         cmp     [ebp+NTFS.cur_read], 0x200
  1404.         jz      .alignedstart
  1405. .eof_ebx:
  1406.         popad
  1407.         jmp     .eof
  1408.  
  1409. .alignedstart:
  1410.         mov     eax, [ebx+4]
  1411.         push    edx
  1412.         mov     edx, [ebx+8]
  1413.         add     eax, 511
  1414.         adc     edx, 0
  1415.         shrd    eax, edx, 9
  1416.         pop     edx
  1417.         mov     [ebp+NTFS.cur_offs], eax
  1418.         mov     [ebp+NTFS.cur_buf], edx
  1419.         mov     eax, ecx
  1420.         shr     eax, 9
  1421.         mov     [ebp+NTFS.cur_size], eax
  1422.         add     eax, [ebp+NTFS.cur_offs]
  1423.         push    eax
  1424.         call    ntfs_read_attr.continue
  1425.         pop     [ebp+NTFS.cur_offs]
  1426.         mov     eax, [ebp+NTFS.cur_read]
  1427.         add     [esp+10h], eax
  1428.         mov     eax, ecx
  1429.         and     eax, not 0x1FF
  1430.         cmp     [ebp+NTFS.cur_read], eax
  1431.         jnz     .eof_ebx
  1432.         and     ecx, 0x1FF
  1433.         jz      .retok
  1434.         add     edx, [ebp+NTFS.cur_read]
  1435.         mov     [ebp+NTFS.cur_size], 1
  1436.         lea     eax, [ebp+NTFS.bitmap_buf]
  1437.         mov     [ebp+NTFS.cur_buf], eax
  1438.         call    ntfs_read_attr.continue
  1439.         cmp     [ebp+NTFS.cur_read], ecx
  1440.         jb      @f
  1441.         mov     [ebp+NTFS.cur_read], ecx
  1442. @@:
  1443.         xchg    ecx, [ebp+NTFS.cur_read]
  1444.         push    ecx
  1445.         mov     edi, edx
  1446.         lea     esi, [ebp+NTFS.bitmap_buf]
  1447.         add     [esp+10h+4], ecx
  1448.         rep movsb
  1449.         pop     ecx
  1450.         xor     eax, eax
  1451.         cmp     ecx, [ebp+NTFS.cur_read]
  1452.         jz      @f
  1453.         mov     al, ERROR_END_OF_FILE
  1454. @@:
  1455.         mov     [esp+1Ch], eax
  1456.         call    ntfs_unlock
  1457.         popad
  1458.         ret
  1459.  
  1460. ;----------------------------------------------------------------
  1461. ntfs_ReadFolder:
  1462.         call    ntfs_lock
  1463.         mov     [ebp+NTFS.cur_iRecord], 5   ; root directory
  1464.         cmp     byte [esi], 0
  1465.         jz      @f
  1466.         call    ntfs_find_lfn
  1467.         jc      ntfsNotFound
  1468. @@:
  1469.         mov     [ebp+NTFS.cur_attr], 0x10   ; $STANDARD_INFORMATION
  1470.         and     [ebp+NTFS.cur_offs], 0
  1471.         mov     [ebp+NTFS.cur_size], 1
  1472.         lea     eax, [ebp+NTFS.bitmap_buf]
  1473.         mov     [ebp+NTFS.cur_buf], eax
  1474.         call    ntfs_read_attr
  1475.         jc      ntfsFail
  1476.         mov     [ebp+NTFS.cur_attr], 0x90   ; $INDEX_ROOT
  1477. .doit:
  1478.         mov     eax, [ebp+NTFS.cur_index_size]
  1479.         mov     [ebp+NTFS.cur_size], eax
  1480.         mov     eax, [ebp+NTFS.cur_index_buf]
  1481.         mov     [ebp+NTFS.cur_buf], eax
  1482.         call    ntfs_read_attr.newAttribute
  1483.         jc      ntfsFail
  1484.         cmp     [ebp+NTFS.cur_read], 0x20
  1485.         jc      ntfsFail
  1486.         mov     esi, [ebp+NTFS.cur_index_buf]
  1487.         mov     eax, [esi+indexRecordSize]
  1488.         shr     eax, 9
  1489.         cmp     [ebp+NTFS.cur_index_size], eax
  1490.         jc      .realloc
  1491.         mov     [ebp+NTFS.cur_subnode_size], eax
  1492.         add     esi, rootNode
  1493.         mov     eax, [esi+nodeRealSize]
  1494.         add     eax, rootNode
  1495.         cmp     [ebp+NTFS.cur_read], eax
  1496.         jc      ntfsFail
  1497.         mov     edi, [ebx+16]
  1498.         mov     ecx, [ebx+12]
  1499.         pushd   [ebx]
  1500.         pushd   [ebx+8]     ; read ANSI/UNICODE name
  1501.         push    edi
  1502.         mov     edx, esp
  1503.         mov     ebx, [ebx+4]
  1504. ; init header
  1505.         xor     eax, eax
  1506.         mov     [edi+8], eax
  1507.         mov     [edi+4], eax
  1508.         inc     eax
  1509.         mov     [edi], eax      ; version
  1510.         add     edi, 32
  1511. ; edi -> BDFE, esi -> current index data, ebx = first wanted block,
  1512. ; ecx = number of blocks to read
  1513. ; edx -> parameters block: dd <output>, dd <flags>
  1514.         cmp     [ebp+NTFS.cur_iRecord], 5
  1515.         jz      .skip_specials
  1516. ; dot and dotdot entries
  1517.         push    esi
  1518.         xor     esi, esi
  1519.         call    .add_special_entry
  1520.         inc     esi
  1521.         call    .add_special_entry
  1522.         pop     esi
  1523. .skip_specials:
  1524. ; at first, dump index root
  1525.         add     esi, [esi+indexOffset]
  1526. .dump_root:
  1527.         test    byte [esi+indexFlags], 2
  1528.         jnz     .dump_root_done
  1529.         call    .add_entry
  1530.         movzx   eax, word [esi+indexAllocatedSize]
  1531.         add     esi, eax
  1532.         jmp     .dump_root
  1533.  
  1534. .realloc:
  1535.         mov     edi, eax
  1536.         mov     eax, [esi+indexRecordSize]
  1537.         shl     eax, 1
  1538.         stdcall kernel_alloc, eax
  1539.         test    eax, eax
  1540.         jz      ntfsFail
  1541.         mov     edx, [ebp+NTFS.cur_index_buf]
  1542.         cmp     edx, [ebp+NTFS.secondIndexBuffer]
  1543.         jc      @f
  1544.         mov     edx, [ebp+NTFS.secondIndexBuffer]
  1545. @@:
  1546.         mov     [ebp+NTFS.cur_index_buf], eax
  1547.         add     eax, [esi+indexRecordSize]
  1548.         mov     [ebp+NTFS.secondIndexBuffer], eax
  1549.         mov     [ebp+NTFS.cur_index_size], edi
  1550.         stdcall kernel_free, edx
  1551.         jmp     .doit
  1552.  
  1553. .dump_root_done:
  1554. ; now dump all subnodes
  1555.         push    ecx edi
  1556.         lea     edi, [ebp+NTFS.bitmap_buf]
  1557.         mov     [ebp+NTFS.cur_buf], edi
  1558.         mov     ecx, 0x400/4
  1559.         xor     eax, eax
  1560.         rep stosd
  1561.         mov     [ebp+NTFS.cur_attr], 0xB0   ; $BITMAP
  1562.         and     [ebp+NTFS.cur_offs], 0
  1563.         mov     [ebp+NTFS.cur_size], 2
  1564.         call    ntfs_read_attr.newAttribute
  1565.         pop     edi ecx
  1566.         push    0       ; save offset in $BITMAP attribute
  1567.         and     [ebp+NTFS.cur_offs], 0
  1568. .dumploop:
  1569.         mov     [ebp+NTFS.cur_attr], 0xA0
  1570.         mov     eax, [ebp+NTFS.cur_subnode_size]
  1571.         mov     [ebp+NTFS.cur_size], eax
  1572.         mov     esi, [ebp+NTFS.cur_index_buf]
  1573.         mov     [ebp+NTFS.cur_buf], esi
  1574.         mov     eax, [ebp+NTFS.cur_offs]
  1575.         push    eax
  1576.         imul    eax, [ebp+NTFS.cur_subnode_size]
  1577.         mov     [ebp+NTFS.cur_offs], eax
  1578.         call    ntfs_read_attr.newAttribute
  1579.         pop     [ebp+NTFS.cur_offs]
  1580.         mov     eax, [ebp+NTFS.cur_subnode_size]
  1581.         shl     eax, 9
  1582.         cmp     [ebp+NTFS.cur_read], eax
  1583.         jnz     .done
  1584.         push    eax
  1585.         mov     eax, [ebp+NTFS.cur_offs]
  1586.         and     eax, 0x400*8-1
  1587.         bt      dword [ebp+NTFS.bitmap_buf], eax
  1588.         pop     eax
  1589.         jnc     .dump_subnode_done
  1590.         cmp     dword [esi], 'INDX'
  1591.         jnz     .dump_subnode_done
  1592.         push    ebx
  1593.         mov     ebx, esi
  1594.         call    ntfs_restore_usa
  1595.         pop     ebx
  1596.         jc      .dump_subnode_done
  1597.         add     esi, recordNode
  1598.         add     esi, [esi+indexOffset]
  1599. .dump_subnode:
  1600.         test    byte [esi+indexFlags], 2
  1601.         jnz     .dump_subnode_done
  1602.         call    .add_entry
  1603.         movzx   eax, word [esi+indexAllocatedSize]
  1604.         add     esi, eax
  1605.         jmp     .dump_subnode
  1606.  
  1607. .dump_subnode_done:
  1608.         inc     [ebp+NTFS.cur_offs]
  1609.         test    [ebp+NTFS.cur_offs], 0x400*8-1
  1610.         jnz     .dumploop
  1611.         mov     [ebp+NTFS.cur_attr], 0xB0
  1612.         push    ecx edi
  1613.         lea     edi, [ebp+NTFS.bitmap_buf]
  1614.         mov     [ebp+NTFS.cur_buf], edi
  1615.         mov     ecx, 0x400/4
  1616.         xor     eax, eax
  1617.         rep stosd
  1618.         pop     edi ecx
  1619.         pop     eax
  1620.         push    [ebp+NTFS.cur_offs]
  1621.         inc     eax
  1622.         mov     [ebp+NTFS.cur_offs], eax
  1623.         mov     [ebp+NTFS.cur_size], 2
  1624.         push    eax
  1625.         call    ntfs_read_attr.newAttribute
  1626.         pop     eax
  1627.         pop     [ebp+NTFS.cur_offs]
  1628.         push    eax
  1629.         jmp     .dumploop
  1630.  
  1631. .done:
  1632.         pop     eax
  1633.         pop     eax
  1634.         mov     ebx, [eax+4]
  1635.         pop     eax
  1636.         pop     eax
  1637.         test    eax, eax
  1638.         jz      .ret
  1639.         xor     eax, eax
  1640.         dec     ecx
  1641.         js      @f
  1642.         mov     al, ERROR_END_OF_FILE
  1643. @@:
  1644.         push    eax
  1645.         call    ntfs_unlock
  1646.         pop     eax
  1647.         ret
  1648.  
  1649. .add_special_entry:
  1650.         mov     eax, [edx]
  1651.         inc     dword [eax+8]   ; new file found
  1652.         dec     ebx
  1653.         jns     .ret
  1654.         dec     ecx
  1655.         js      .ret
  1656.         inc     dword [eax+4]   ; new file block copied
  1657.         mov     eax, [edx+4]
  1658.         mov     [edi+4], eax
  1659.         mov     eax, 0x10
  1660.         stosd
  1661.         scasd
  1662.         push    ebx ecx edx
  1663.         mov     eax, dword [ebp+NTFS.bitmap_buf]
  1664.         mov     edx, dword [ebp+NTFS.bitmap_buf+4]
  1665.         call    ntfs_datetime_to_bdfe
  1666.         mov     eax, dword [ebp+NTFS.bitmap_buf+0x18]
  1667.         mov     edx, dword [ebp+NTFS.bitmap_buf+0x1C]
  1668.         call    ntfs_datetime_to_bdfe
  1669.         mov     eax, dword [ebp+NTFS.bitmap_buf+8]
  1670.         mov     edx, dword [ebp+NTFS.bitmap_buf+0xC]
  1671.         call    ntfs_datetime_to_bdfe
  1672.         pop     edx ecx ebx
  1673.         xor     eax, eax
  1674.         stosd
  1675.         stosd
  1676.         mov     al, '.'
  1677.         push    edi ecx
  1678.         lea     ecx, [esi+1]
  1679.         cmp     dword[edi-36], 2
  1680.         jz      .utf16sp
  1681.         rep stosb
  1682.         mov     byte [edi], 0
  1683.         pop     ecx edi
  1684.         cmp     dword[edi-36], 3
  1685.         jz      @f
  1686.         add     edi, 264
  1687.         ret
  1688.  
  1689. .utf16sp:
  1690.         rep stosw
  1691.         mov     word [edi], 0
  1692.         pop     ecx edi
  1693. @@:
  1694.         add     edi, 520
  1695. .ret:
  1696.         ret
  1697.  
  1698. .add_entry:
  1699. ; do not return DOS 8.3 names
  1700.         cmp     byte [esi+namespace], 2
  1701.         jz      .ret
  1702. ; do not return system files
  1703.         cmp     dword[esi+fileRecordReference], 16
  1704.         jb      .ret
  1705.         cmp     byte [esi+fileNameLength], 0
  1706.         jz      .ret
  1707.         mov     eax, [edx]
  1708.         inc     dword [eax+8]   ; new file found
  1709.         dec     ebx
  1710.         jns     .ret
  1711.         dec     ecx
  1712.         js      .ret
  1713.         inc     dword [eax+4]   ; new file block copied
  1714.         mov     eax, [edx+4]    ; flags
  1715.         call    ntfs_direntry_to_bdfe
  1716.         push    ecx esi edi
  1717.         movzx   ecx, byte [esi+fileNameLength]
  1718.         add     esi, fileName
  1719.         cmp     dword[edi-36], 2
  1720.         jz      .utf16
  1721.         cmp     dword[edi-36], 3
  1722.         jz      .utf8
  1723. @@:
  1724.         lodsw
  1725.         call    uni2ansi_char
  1726.         stosb
  1727.         loop    @b
  1728.         mov     byte [edi], 0
  1729.         pop     edi esi ecx
  1730.         add     edi, 264
  1731.         ret
  1732.  
  1733. .utf8:
  1734.         push    ecx
  1735.         mov     cx, 519
  1736. @@:
  1737.         lodsw
  1738.         call    UTF16to8
  1739.         js      @f
  1740.         dec     dword[esp]
  1741.         jnz     @b
  1742. @@:
  1743.         mov     byte [edi], 0
  1744.         pop     edi
  1745. @@:
  1746.         pop     edi esi ecx
  1747.         add     edi, 520
  1748.         ret
  1749.  
  1750. .utf16:
  1751.         rep movsw
  1752.         mov     word [edi], 0
  1753.         jmp     @b
  1754.  
  1755. ntfs_direntry_to_bdfe:
  1756.         mov     [edi+4], eax    ; ANSI/UNICODE name
  1757.         mov     eax, [esi+fileFlags]
  1758.         test    eax, 0x10000000
  1759.         jz      @f
  1760.         and     eax, not 0x10000000
  1761.         or      al, 0x10
  1762. @@:
  1763.         stosd
  1764.         scasd
  1765.         push    ebx ecx edx
  1766.         mov     eax, [esi+fileCreated]
  1767.         mov     edx, [esi+fileCreated+4]
  1768.         call    ntfs_datetime_to_bdfe
  1769.         mov     eax, [esi+fileAccessed]
  1770.         mov     edx, [esi+fileAccessed+4]
  1771.         call    ntfs_datetime_to_bdfe
  1772.         mov     eax, [esi+fileModified]
  1773.         mov     edx, [esi+fileModified+4]
  1774.         call    ntfs_datetime_to_bdfe
  1775.         pop     edx ecx ebx
  1776.         mov     eax, [esi+fileRealSize]
  1777.         stosd
  1778.         mov     eax, [esi+fileRealSize+4]
  1779.         stosd
  1780.         ret
  1781.  
  1782. ntfs_datetime_to_bdfe:
  1783. ; in: edx:eax = seconds since 01.01.1601 x10000000
  1784. ; edi -> data block
  1785. ; out: edi = edi+8
  1786.         sub     eax, 3365781504
  1787.         sbb     edx, 29389701
  1788.         mov     ecx, 10000000
  1789.         cmp     edx, ecx
  1790.         jc      @f
  1791.         xor     edx, edx
  1792. @@:
  1793.         div     ecx
  1794.         jmp     fsTime2bdfe
  1795.  
  1796. ;----------------------------------------------------------------
  1797. ntfs_GetFileInfo:
  1798.         call    ntfs_lock
  1799.         mov     edi, [ebx+16]
  1800.         cmp     byte [esi], 0
  1801.         jz      .volume
  1802.         call    ntfs_find_lfn
  1803.         jnc     .found
  1804.         test    eax, eax
  1805.         jz      ntfsFail
  1806.         jmp     ntfsNotFound
  1807.  
  1808. .found:
  1809.         mov     esi, eax
  1810.         xor     eax, eax
  1811.         call    ntfs_direntry_to_bdfe
  1812. .end:
  1813.         call    ntfs_unlock
  1814.         xor     eax, eax
  1815.         ret
  1816.  
  1817. .volume:
  1818.         mov     byte [edi], 8
  1819.         mov     eax, [ebx+8]
  1820.         mov     [edi+4], eax
  1821.         mov     eax, dword [ebp+NTFS.Length]
  1822.         mov     edx, dword [ebp+NTFS.Length+4]
  1823.         shld    edx, eax, 9
  1824.         shl     eax, 9
  1825.         mov     [edi+36], edx
  1826.         mov     [edi+32], eax
  1827.         add     edi, 40
  1828.         mov     [ebp+NTFS.cur_buf], edi
  1829.         mov     [ebp+NTFS.cur_iRecord], 3
  1830.         mov     [ebp+NTFS.cur_attr], 0x60
  1831.         mov     [ebp+NTFS.cur_offs], 0
  1832.         mov     [ebp+NTFS.cur_size], 1
  1833.         call    ntfs_read_attr
  1834.         jc      ntfsFail
  1835.         mov     ecx, [ebp+NTFS.cur_read]
  1836.         mov     [edi+ecx], ax
  1837.         cmp     [ebx+8], eax
  1838.         jnz     .end
  1839.         mov     esi, edi
  1840.         shr     ecx, 1
  1841.         jz      .end
  1842. @@:
  1843.         lodsw
  1844.         call    uni2ansi_char
  1845.         stosb
  1846.         loop    @b
  1847.         mov     byte [edi], 0
  1848.         jmp     .end
  1849.  
  1850. ;----------------------------------------------------------------
  1851. ntfs_CreateFolder:
  1852.         mov     [ebp+NTFS.bFolder], 1
  1853.         jmp     @f
  1854.  
  1855. ntfs_CreateFile:
  1856.         mov     [ebp+NTFS.bFolder], 0
  1857. @@:
  1858.         cmp     byte [esi], 0
  1859.         jnz     @f
  1860.         xor     ebx, ebx
  1861.         movi    eax, ERROR_ACCESS_DENIED
  1862.         ret
  1863.  
  1864. @@: ; 1. Search file
  1865.         call    ntfs_lock
  1866.         call    ntfs_find_lfn
  1867.         jc      .notFound
  1868. ; found, rewrite
  1869.         cmp     [ebp+NTFS.cur_iRecord], 16
  1870.         jc      ntfsDenied
  1871.         cmp     [ebp+NTFS.bFolder], 1
  1872.         jz      .folder
  1873.         test    byte [eax+fileFlags], 1
  1874.         jnz     ntfsDenied
  1875.         cmp     [ebp+NTFS.fragmentCount], 1
  1876.         jnz     ntfsUnsupported     ; record fragmented
  1877. ; edit directory node
  1878.         mov     edi, [ebp+NTFS.cur_index_buf]
  1879.         cmp     dword [edi], 'INDX'
  1880.         jz      @f
  1881.         mov     esi, [ebp+NTFS.frs_buffer]
  1882.         mov     ecx, [esi+recordRealSize]
  1883.         shr     ecx, 2
  1884.         rep movsd
  1885.         mov     esi, [ebp+NTFS.attr_offs]
  1886.         mov     cl, [esi+attributeOffset]
  1887.         sub     esi, [ebp+NTFS.frs_buffer]
  1888.         add     eax, ecx
  1889.         add     eax, esi
  1890. @@:
  1891.         mov     edi, eax
  1892.         mov     eax, [ebx+12]
  1893.         mov     [edi+fileRealSize], eax
  1894.         mov     dword [edi+fileRealSize+4], 0
  1895.         push    ebx eax
  1896.         call    ntfsGetTime
  1897.         mov     [edi+fileModified], eax
  1898.         mov     [edi+fileModified+4], edx
  1899.         mov     [edi+recordModified], eax
  1900.         mov     [edi+recordModified+4], edx
  1901.         mov     [edi+fileAccessed], eax
  1902.         mov     [edi+fileAccessed+4], edx
  1903.         pop     edx ebx
  1904.         mov     eax, [ebp+NTFS.LastRead]
  1905.         mov     [ebp+NTFS.nodeLastRead], eax
  1906.         mov     [ebp+NTFS.cur_attr], 0x80
  1907.         mov     [ebp+NTFS.cur_offs], 0
  1908.         mov     [ebp+NTFS.cur_size], 0
  1909.         call    ntfs_read_attr
  1910.         jc      ntfsFail
  1911.         mov     esi, edi
  1912.         mov     edi, [ebp+NTFS.frs_buffer]
  1913.         cmp     word [edi+baseRecordReuse], 0
  1914.         jnz     ntfsUnsupported     ; auxiliary record
  1915.         mov     al, [edi+attributeOffset]
  1916.         add     edi, eax
  1917.         mov     al, [edi+attributeOffset]
  1918.         add     edi, eax
  1919.         mov     ecx, 6
  1920.         add     esi, fileModified
  1921.         add     edi, 8
  1922.         rep movsd
  1923.         mov     eax, edx
  1924.         xor     edx, edx
  1925.         mov     ecx, [ebp+NTFS.attr_offs]
  1926.         cmp     word [ecx+attributeFlags], 0
  1927.         jnz     ntfsUnsupported
  1928.         push    ebx
  1929.         cmp     byte [ecx+nonResidentFlag], 0
  1930.         jz      @f
  1931.         cmp     [ecx+attributeRealSize+4], edx
  1932.         jnz     @f
  1933.         cmp     [ecx+attributeRealSize], eax
  1934.         jz      ntfs_WriteFile.writeNode
  1935. @@:
  1936.         jmp     ntfs_WriteFile.resizeAttribute
  1937.  
  1938. .folder:
  1939.         bt      dword [eax+fileFlags], 28
  1940.         jnc     ntfsDenied
  1941.         push    0
  1942.         jmp     ntfsOut
  1943.  
  1944. .notFound:  ; create
  1945.         test    eax, eax
  1946.         jz      ntfsFail
  1947.         cmp     [ebp+NTFS.fragmentCount], 1
  1948.         jnz     ntfsUnsupported     ; record fragmented
  1949. ; 2. Prepare directory record
  1950.         mov     edi, esi
  1951.         mov     edx, eax
  1952.         xor     ecx, ecx
  1953. @@:         ; count characters
  1954.         call    utf8to16
  1955.         cmp     ax, '/'
  1956.         jz      ntfsNotFound    ; path folder not found
  1957.         inc     ecx
  1958.         test    ax, ax
  1959.         jnz     @b
  1960.         dec     ecx
  1961.         push    ecx     ; name length in chars
  1962.         push    edi
  1963.         shl     ecx, 1
  1964.         add     ecx, fileName+7
  1965.         and     ecx, not 7
  1966.         mov     edi, [ebp+NTFS.cur_index_buf]
  1967.         mov     eax, [ebx+12]
  1968.         mov     [ebp+NTFS.fileRealSize], eax
  1969.         mov     eax, [ebx+16]
  1970.         mov     [ebp+NTFS.fileDataBuffer], eax
  1971.         push    ecx     ; index length
  1972.         mov     eax, edx
  1973.         mov     edx, ecx
  1974.         cmp     dword [edi], 'INDX'
  1975.         jz      .indexRecord
  1976.         mov     esi, [ebp+NTFS.frs_buffer]  ; indexRoot
  1977.         mov     ecx, [esi+recordRealSize]
  1978.         add     edx, ecx
  1979.         cmp     [esi+recordAllocatedSize], edx
  1980.         jc      .growTree
  1981.         mov     [esi+recordRealSize], edx
  1982.         shr     ecx, 2
  1983.         rep movsd
  1984.         mov     edi, [ebp+NTFS.indexRoot]
  1985.         sub     edi, [ebp+NTFS.frs_buffer]
  1986.         add     edi, [ebp+NTFS.cur_index_buf]
  1987.         mov     esi, [esp]
  1988.         add     [edi+sizeWithHeader], esi
  1989.         add     [edi+sizeWithoutHeader], esi
  1990.         mov     cl, [edi+attributeOffset]
  1991.         add     edi, ecx
  1992.         add     [edi+rootNode+nodeRealSize], esi
  1993.         add     [edi+rootNode+nodeAllocatedSize], esi
  1994.         sub     eax, [ebp+NTFS.cur_index_buf]
  1995.         add     eax, edi
  1996.         mov     edi, [ebp+NTFS.cur_index_buf]
  1997.         jmp     .common
  1998.  
  1999. .growTree:  ; create indexRecord
  2000.         mov     edi, [ebp+NTFS.cur_index_buf]
  2001.         mov     ecx, 10
  2002.         xor     eax, eax
  2003.         rep stosd
  2004.         mov     esi, [ebp+NTFS.indexRoot]
  2005.         mov     al, [esi+attributeOffset]
  2006.         add     esi, eax
  2007.         rdtsc
  2008.         stosw
  2009.         mov     eax, [esi+indexRecordSize]
  2010.         cmp     eax, [ebp+NTFS.frs_size]
  2011.         jc      .errorPop3
  2012.         shr     eax, 9
  2013.         inc     eax
  2014.         mov     edi, [ebp+NTFS.cur_index_buf]
  2015.         mov     dword[edi], 'INDX'
  2016.         mov     byte [edi+updateSequenceOffset], 28h
  2017.         mov     [edi+updateSequenceSize], al
  2018.         add     edi, recordNode
  2019.         shl     eax, 1
  2020.         add     eax, 28h-recordNode+7
  2021.         and     eax, not 7
  2022.         mov     [edi+indexOffset], eax
  2023.         mov     ecx, [esi+indexRecordSize]
  2024.         sub     ecx, recordNode
  2025.         mov     [edi+nodeAllocatedSize], ecx
  2026.         add     esi, rootNode
  2027.         push    esi
  2028.         mov     ecx, [esi+nodeRealSize]
  2029.         sub     ecx, [esi+indexOffset]
  2030.         add     eax, ecx
  2031.         mov     [edi+nodeRealSize], eax
  2032.         mov     eax, [esi+nonLeafFlag]
  2033.         mov     [edi+nonLeafFlag], eax
  2034.         shr     ecx, 2
  2035.         add     esi, [esi+indexOffset]
  2036.         add     edi, [edi+indexOffset]
  2037.         rep movsd       ; copy root indexes
  2038. ; clear root node
  2039.         mov     cl, 10
  2040.         mov     edi, [esp]
  2041.         xor     eax, eax
  2042.         rep stosd
  2043.         pop     edi
  2044.         mov     byte [edi+indexOffset], 16
  2045.         mov     byte [edi+nodeRealSize], 28h
  2046.         mov     byte [edi+nodeAllocatedSize], 28h
  2047.         mov     byte [edi+nonLeafFlag], 1
  2048.         mov     byte [edi+16+indexAllocatedSize], 18h
  2049.         mov     byte [edi+16+indexFlags], 3
  2050.         mov     esi, [ebp+NTFS.indexRoot]
  2051.         add     edi, 28h
  2052.         mov     eax, edi
  2053.         sub     eax, esi
  2054.         mov     word [esi+sizeWithoutHeader], 38h
  2055.         xchg    [esi+sizeWithHeader], eax
  2056.         add     esi, eax
  2057.         mov     [ebp+NTFS.attr_offs], edi
  2058.         cmp     byte [esi], 0xA0
  2059.         jnz     @f
  2060.         cmp     dword [esi+attributeAllocatedSize], 0
  2061.         jz      @f
  2062.         mov     eax, [ebp+NTFS.frs_buffer]
  2063.         mov     ecx, eax
  2064.         add     ecx, [eax+recordRealSize]
  2065.         sub     ecx, esi
  2066.         shr     ecx, 2
  2067.         rep movsd
  2068.         sub     edi, eax
  2069.         mov     [eax+recordRealSize], edi
  2070.         call    .ntfsNodeAlloc
  2071.         jc      ntfsErrorPop3
  2072.         mov     eax, [ebp+NTFS.newRecord]
  2073.         mov     edi, [ebp+NTFS.cur_index_buf]
  2074.         mov     [edi+recordVCN], eax
  2075.         mov     edi, [ebp+NTFS.attr_offs]
  2076.         mov     [edi-8], eax
  2077.         jmp     .refresh
  2078.  
  2079. @@:
  2080.         mov     cl, 32
  2081.         xor     eax, eax
  2082.         rep stosd
  2083.         mov     eax, [ebp+NTFS.cur_subnode_size]
  2084.         cmp     eax, [ebp+NTFS.cur_size]
  2085.         jnz     @f
  2086.         mov     al, 1
  2087. @@:
  2088.         mov     [ebp+NTFS.fileDataSize], eax
  2089.         mov     edi, [ebp+NTFS.BitmapStart]
  2090.         call    ntfsSpaceAlloc
  2091.         movi    eax, ERROR_DISK_FULL
  2092.         jc      ntfsErrorPop3
  2093. ; create $IndexAllocation
  2094.         mov     edi, [ebp+NTFS.attr_offs]
  2095.         mov     byte [edi+attributeType], 0xA0
  2096.         mov     byte [edi+nonResidentFlag], 1
  2097.         mov     byte [edi+nameLength], 4
  2098.         mov     byte [edi+nameOffset], 40h
  2099.         mov     byte [edi+dataRunsOffset], 48h
  2100.         mov     byte [edi+sizeWithHeader], 50h
  2101.         mov     eax, [ebp+NTFS.fileDataSize]
  2102.         dec     eax
  2103.         mov     [edi+lastVCN], eax
  2104.         inc     eax
  2105.         mul     [ebp+NTFS.sectors_per_cluster]
  2106.         shl     eax, 9
  2107.         mov     [edi+attributeAllocatedSize], eax
  2108.         mov     [edi+attributeRealSize], eax
  2109.         mov     [edi+initialDataSize], eax
  2110.         mov     dword[edi+40h], 490024h     ; unicode $I30
  2111.         mov     dword[edi+40h+4], 300033h
  2112.         push    edi
  2113.         mov     esi, edi
  2114.         add     edi, 48h
  2115.         call    createMcbEntry
  2116.         mov     esi, [ebp+NTFS.frs_buffer]
  2117.         pop     edi
  2118.         mov     al, [esi+newAttributeID]
  2119.         mov     [edi+attributeID], al
  2120.         add     edi, 50h
  2121.         inc     eax
  2122. ; create $Bitmap
  2123.         mov     [edi+attributeID], al
  2124.         inc     eax
  2125.         mov     [esi+newAttributeID], al
  2126.         mov     byte [edi+attributeType], 0xB0
  2127.         mov     byte [edi+nameLength], 4
  2128.         mov     byte [edi+nameOffset], 18h
  2129.         mov     byte [edi+attributeOffset], 20h
  2130.         mov     byte [edi+sizeWithoutHeader], 8
  2131.         mov     byte [edi+sizeWithHeader], 28h
  2132.         mov     dword[edi+18h], 490024h     ; unicode $I30
  2133.         mov     dword[edi+18h+4], 300033h
  2134.         mov     byte [edi+20h], 1
  2135.         mov     dword[edi+28h], -1
  2136.         add     edi, 30h
  2137.         sub     edi, esi
  2138.         mov     [esi+recordRealSize], edi
  2139.         mov     eax, [ebp+NTFS.fileDataStart]
  2140.         mul     [ebp+NTFS.sectors_per_cluster]
  2141.         mov     edx, eax
  2142.         jmp     @f
  2143.  
  2144. .refresh:
  2145.         mov     [ebp+NTFS.cur_size], 0
  2146.         call    ntfs_read_attr.continue
  2147.         movi    eax, ERROR_FS_FAIL
  2148.         jc      ntfsErrorPop3
  2149.         mov     edx, [ebp+NTFS.LastRead]
  2150. @@:
  2151.         mov     ebx, [ebp+NTFS.cur_index_buf]
  2152.         call    writeRecord
  2153.         mov     ebx, [ebp+NTFS.frs_buffer]
  2154.         mov     edx, [ebp+NTFS.rootLastRead]
  2155.         call    writeRecord
  2156.         mov     esi, [esp+4]
  2157.         call    ntfs_find_lfn.doit2
  2158.         test    eax, eax
  2159.         jz      .errorPop3
  2160.         mov     edi, [ebp+NTFS.cur_index_buf]
  2161.         mov     edx, [esp]
  2162. .indexRecord:
  2163.         add     edi, recordNode
  2164.         add     edx, [edi+nodeRealSize]
  2165.         cmp     [edi+nodeAllocatedSize], edx
  2166.         jc      .arborizeTree
  2167.         mov     [edi+nodeRealSize], edx
  2168.         jmp     .common
  2169.  
  2170. .errorPop3:
  2171.         add     esp, 12
  2172.         jmp     ntfsUnsupported
  2173.  
  2174. .ntfsNodeAlloc:
  2175. ; in: [ebp+NTFS.attr_offs] -> $IndexAllocation
  2176. ;   out:
  2177. ; [ebp+NTFS.newRecord] = node VCN
  2178. ; [ebp+NTFS.cur_offs]
  2179. ; CF=1 -> eax = error code
  2180.         mov     esi, [ebp+NTFS.attr_offs]
  2181.         add     esi, [esi+sizeWithHeader]
  2182.         cmp     byte [esi], 0xB0
  2183.         jnz     .ret
  2184.         movzx   ecx, word [esi+sizeWithoutHeader]
  2185.         shr     ecx, 2
  2186.         movzx   edi, byte [esi+attributeOffset]
  2187.         add     edi, esi
  2188.         mov     edx, edi
  2189.         or      eax, -1
  2190.         repz scasd
  2191.         jnz     @f
  2192.         cmp     [edi], eax
  2193.         jnz     .ret
  2194. ; extend folder $Bitmap
  2195.         add     word [esi+sizeWithHeader], 8
  2196.         add     word [esi+sizeWithoutHeader], 8
  2197.         mov     esi, [ebp+NTFS.frs_buffer]
  2198.         mov     eax, [esi+recordRealSize]
  2199.         add     eax, 8
  2200.         cmp     [esi+recordAllocatedSize], eax
  2201.         jc      .ret
  2202.         mov     [esi+recordRealSize], eax
  2203.         xor     eax, eax
  2204.         stosd
  2205.         mov     [edi], eax
  2206.         mov     [edi+8], eax
  2207.         dec     eax
  2208.         mov     [edi+4], eax
  2209. @@:
  2210.         sub     edi, 4
  2211.         mov     eax, [edi]
  2212.         not     eax
  2213.         bsf     eax, eax
  2214.         bts     [edi], eax
  2215.         sub     edi, edx
  2216.         shl     edi, 3
  2217.         add     eax, edi
  2218.         mul     [ebp+NTFS.cur_subnode_size]
  2219.         mov     [ebp+NTFS.newRecord], eax
  2220.         mov     ecx, [ebp+NTFS.cur_size]
  2221.         cmp     ecx, [ebp+NTFS.cur_subnode_size]
  2222.         jz      @f
  2223.         mul     [ebp+NTFS.sectors_per_cluster]
  2224. @@:
  2225.         mov     [ebp+NTFS.cur_offs], eax
  2226.         add     eax, ecx
  2227.         shl     eax, 9
  2228.         mov     esi, [ebp+NTFS.attr_offs]
  2229.         cmp     [esi+attributeAllocatedSize], eax
  2230.         jnc     @f
  2231.         xor     edx, edx
  2232.         jmp     resizeAttribute
  2233.  
  2234. .ret:
  2235.         movi    eax, ERROR_UNSUPPORTED_FS
  2236.         stc
  2237. @@:
  2238.         ret
  2239.  
  2240. .arborizeTree:      ; find median index
  2241.         mov     ecx, [edi+nodeRealSize]
  2242.         sub     ecx, [edi+indexOffset]
  2243.         shr     ecx, 1
  2244.         add     edi, [edi+indexOffset]
  2245.         xor     eax, eax
  2246. @@:
  2247.         add     edi, eax
  2248.         mov     ax, [edi+indexAllocatedSize]
  2249.         sub     ecx, eax
  2250.         jnc     @b
  2251.         add     eax, 8
  2252.         mov     esi, [ebp+NTFS.secondIndexBuffer]
  2253.         cmp     dword [esi], 'INDX'
  2254.         jz      @f
  2255. ; move index to the root node
  2256.         mov     esi, [ebp+NTFS.frs_buffer]
  2257.         mov     ecx, eax
  2258.         add     ecx, 8
  2259.         add     ecx, [esi+recordRealSize]
  2260.         cmp     [esi+recordAllocatedSize], ecx
  2261.         jc      .growTree
  2262.         push    edi eax
  2263.         call    .ntfsNodeAlloc
  2264.         jc      ntfsErrorPop5
  2265.         pop     eax
  2266.         mov     edi, [ebp+NTFS.indexRoot]
  2267.         add     [ebp+NTFS.attr_offs], eax
  2268.         add     [edi+sizeWithHeader], eax
  2269.         add     [edi+sizeWithoutHeader], eax
  2270.         movzx   ecx, byte [edi+attributeOffset]
  2271.         add     ecx, edi
  2272.         add     [ecx+rootNode+nodeRealSize], eax
  2273.         add     [ecx+rootNode+nodeAllocatedSize], eax
  2274.         add     ecx, [ebp+NTFS.indexPointer]
  2275.         sub     ecx, [ebp+NTFS.secondIndexBuffer]
  2276.         mov     esi, [ebp+NTFS.frs_buffer]
  2277.         add     [esi+recordRealSize], eax
  2278.         add     esi, [esi+recordRealSize]
  2279.         mov     edi, esi
  2280.         sub     esi, eax
  2281.         neg     ecx
  2282.         add     ecx, esi
  2283.         shr     ecx, 2
  2284.         sub     esi, 4
  2285.         sub     edi, 4
  2286.         std
  2287.         rep movsd   ; make space
  2288.         mov     [edi], ecx
  2289.         mov     edi, esi
  2290.         add     edi, 4
  2291.         mov     esi, [esp]
  2292.         add     word [esi+indexAllocatedSize], 8
  2293.         mov     byte [esi+indexFlags], 1
  2294.         mov     ecx, eax
  2295.         sub     ecx, 8
  2296.         shr     ecx, 2
  2297.         cld
  2298.         rep movsd   ; insert index
  2299.         mov     eax, [ebp+NTFS.newRecord]
  2300.         stosd
  2301.         jmp     .splitNode
  2302.  
  2303. .growBranch:    ; move node and replace it with empty one
  2304.         mov     esi, [ebp+NTFS.cur_index_buf]
  2305.         mov     edi, [ebp+NTFS.secondIndexBuffer]
  2306.         mov     eax, [esi+recordVCN]
  2307.         mov     [edi+recordVCN], eax
  2308.         add     edi, recordNode
  2309.         mov     eax, [edi+indexOffset]
  2310.         add     eax, 18h
  2311.         mov     [edi+nodeRealSize], eax
  2312.         add     edi, [edi+indexOffset]
  2313.         mov     ecx, 6
  2314.         xor     eax, eax
  2315.         mov     [ebp+NTFS.indexPointer], edi
  2316.         push    edi
  2317.         rep stosd
  2318.         pop     edi
  2319.         mov     eax, [ebp+NTFS.newRecord]
  2320.         mov     byte [edi+indexAllocatedSize], 18h
  2321.         mov     byte [edi+indexFlags], 3
  2322.         mov     [edi+16], eax
  2323.         mov     [esi+recordVCN], eax
  2324.         mov     eax, [ebp+NTFS.LastRead]
  2325.         mov     [ebp+NTFS.nodeLastRead], eax
  2326.         push    [ebp+NTFS.cur_size]
  2327.         mov     [ebp+NTFS.cur_size], 0
  2328.         call    ntfs_read_attr.continue
  2329.         pop     [ebp+NTFS.cur_size]
  2330.         movi    eax, ERROR_FS_FAIL
  2331.         jc      ntfsErrorPop5
  2332.         pop     eax edi
  2333. @@:         ; move index to the branch node
  2334.         push    edi eax
  2335.         call    .ntfsNodeAlloc
  2336.         jc      ntfsErrorPop5
  2337.         mov     eax, [esp]
  2338.         mov     esi, [ebp+NTFS.secondIndexBuffer]
  2339.         add     esi, recordNode
  2340.         mov     ecx, [esi+nodeRealSize]
  2341.         add     eax, ecx
  2342.         cmp     [esi+nodeAllocatedSize], eax
  2343.         jc      .growBranch
  2344.         mov     [esi+nodeRealSize], eax
  2345.         lea     edi, [esi+eax-4]
  2346.         add     esi, ecx
  2347.         mov     ecx, esi
  2348.         sub     ecx, [ebp+NTFS.indexPointer]
  2349.         shr     ecx, 2
  2350.         sub     esi, 4
  2351.         std
  2352.         rep movsd   ; make space
  2353.         mov     [edi], ecx
  2354.         pop     ecx
  2355.         sub     ecx, 8
  2356.         shr     ecx, 2
  2357.         mov     edi, esi
  2358.         add     edi, 4
  2359.         mov     esi, [esp]
  2360.         add     word [esi+indexAllocatedSize], 8
  2361.         mov     byte [esi+indexFlags], 1
  2362.         cld
  2363.         rep movsd   ; insert index
  2364.         mov     eax, [ebp+NTFS.newRecord]
  2365.         stosd
  2366.         mov     ebx, [ebp+NTFS.secondIndexBuffer]
  2367.         mov     edx, [ebp+NTFS.nodeLastRead]
  2368.         push    esi
  2369.         call    writeRecord
  2370.         pop     esi
  2371. .splitNode:
  2372.         mov     edi, [ebp+NTFS.cur_index_buf]
  2373.         mov     eax, edi
  2374.         add     eax, recordNode
  2375.         add     eax, [edi+recordNode+nodeRealSize]
  2376.         sub     eax, esi
  2377.         push    eax
  2378.         mov     ecx, [edi+recordNode+indexOffset]
  2379.         add     eax, ecx
  2380.         add     ecx, recordNode
  2381.         shr     ecx, 2
  2382.         push    esi
  2383.         mov     esi, edi
  2384.         mov     edi, [ebp+NTFS.secondIndexBuffer]
  2385.         rep movsd
  2386.         pop     esi
  2387.         pop     ecx
  2388.         shr     ecx, 2
  2389.         rep movsd
  2390.         mov     edi, [ebp+NTFS.secondIndexBuffer]
  2391.         mov     [edi+recordNode+nodeRealSize], eax
  2392.         pop     edi
  2393.         mov     cl, 4
  2394.         xor     eax, eax
  2395.         mov     esi, edi
  2396.         rep stosd
  2397.         mov     byte [esi+indexAllocatedSize], 16
  2398.         mov     byte [esi+indexFlags], 2
  2399.         mov     esi, [ebp+NTFS.cur_index_buf]
  2400.         mov     eax, [ebp+NTFS.newRecord]
  2401.         mov     [esi+recordVCN], eax
  2402.         add     esi, recordNode
  2403.         sub     edi, esi
  2404.         mov     [esi+nodeRealSize], edi
  2405.         mov     ebx, [ebp+NTFS.secondIndexBuffer]
  2406.         mov     edx, [ebp+NTFS.LastRead]
  2407.         call    writeRecord
  2408.         jmp     .refresh
  2409.  
  2410. .common:
  2411.         add     edi, edx
  2412.         sub     edi, 4
  2413.         mov     esi, edi
  2414.         sub     esi, [esp]
  2415.         mov     ecx, esi
  2416.         sub     ecx, eax    ; eax = pointer in the record
  2417.         shr     ecx, 2
  2418.         inc     ecx
  2419.         std
  2420.         rep movsd           ; move forward, make space
  2421.         mov     ecx, [esp]
  2422.         shr     ecx, 2
  2423.         xor     eax, eax
  2424.         rep stosd
  2425.         cld
  2426.         add     edi, 4
  2427.         call    ntfsGetTime
  2428.         mov     [edi+fileCreated], eax
  2429.         mov     [edi+fileCreated+4], edx
  2430.         mov     [edi+fileModified], eax
  2431.         mov     [edi+fileModified+4], edx
  2432.         mov     [edi+recordModified], eax
  2433.         mov     [edi+recordModified+4], edx
  2434.         mov     [edi+fileAccessed], eax
  2435.         mov     [edi+fileAccessed+4], edx
  2436.         pop     ecx
  2437.         pop     esi
  2438.         mov     [edi+indexAllocatedSize], cx    ; fill index with data
  2439.         mov     eax, [esp]
  2440.         shl     eax, 1
  2441.         add     eax, 42h
  2442.         mov     [edi+indexRawSize], ax
  2443.         mov     eax, [ebp+NTFS.cur_iRecord]
  2444.         mov     [edi+directoryRecordReference], eax
  2445.         mov     eax, [ebp+NTFS.frs_buffer]
  2446.         mov     eax, [eax+reuseCounter]
  2447.         mov     [edi+directoryReferenceReuse], ax
  2448.         mov     eax, [ebp+NTFS.frs_size]
  2449.         shr     eax, 8
  2450.         add     ecx, 30h+48h+8+18h+8
  2451.         add     ecx, eax
  2452.         mov     eax, [ebp+NTFS.fileRealSize]
  2453.         add     ecx, eax
  2454.         mov     [edi+fileRealSize], eax
  2455.         cmp     [ebp+NTFS.frs_size], ecx
  2456.         jc      @f
  2457.         xor     eax, eax
  2458. @@:
  2459.         mov     ecx, [ebp+NTFS.sectors_per_cluster]
  2460.         shl     ecx, 9
  2461.         add     eax, ecx
  2462.         dec     eax
  2463.         xor     edx, edx
  2464.         div     ecx
  2465.         mov     [ebp+NTFS.fileDataSize], eax
  2466.         mul     ecx
  2467.         mov     [edi+fileAllocatedSize], eax
  2468.         pop     ecx
  2469.         mov     [ebp+NTFS.indexPointer], edi
  2470.         mov     [edi+fileNameLength], cl
  2471.         add     edi, fileName
  2472. @@:         ; record filename
  2473.         call    utf8to16
  2474.         stosw
  2475.         loop    @b
  2476.         mov     eax, [ebp+NTFS.LastRead]
  2477.         mov     [ebp+NTFS.nodeLastRead], eax
  2478.         cmp     [ebp+NTFS.bFolder], 0
  2479.         jz      @f
  2480.         mov     edi, [ebp+NTFS.indexPointer]
  2481.         bts     dword [edi+fileFlags], 28
  2482.         jmp     .mftBitmap
  2483.  
  2484. @@: ; 3. File data
  2485.         cmp     [ebp+NTFS.fileDataSize], 0
  2486.         jz      .mftBitmap
  2487.         mov     edi, [ebp+NTFS.BitmapStart]
  2488.         call    ntfsSpaceAlloc
  2489.         jc      ntfsDiskFull
  2490.         mov     eax, [ebp+NTFS.fileDataStart]
  2491.         mul     [ebp+NTFS.sectors_per_cluster]
  2492.         mov     ecx, [ebp+NTFS.fileRealSize]
  2493.         add     ecx, 511
  2494.         shr     ecx, 9
  2495.         mov     ebx, [ebp+NTFS.fileDataBuffer]
  2496.         call    fs_write64_app
  2497.         test    eax, eax
  2498.         jnz     ntfsDevice
  2499.     ; 4. MFT record
  2500. .mftBitmap: ; search for free record
  2501.         mov     edi, [ebp+NTFS.mftBitmapBuffer]
  2502.         mov     ecx, [ebp+NTFS.mftBitmapSize]
  2503.         mov     al, -1
  2504.         add     edi, 3
  2505.         sub     ecx, 3
  2506.         repz scasb
  2507.         dec     edi
  2508.         movzx   eax, byte [edi]
  2509.         not     al
  2510.         bsf     ecx, eax
  2511.         jz      .extendBitmapMFT    ; no free records
  2512.         bts     [edi], ecx
  2513. ; get record location
  2514.         sub     edi, [ebp+NTFS.mftBitmapBuffer]
  2515.         shl     edi, 3
  2516.         add     edi, ecx
  2517.         mov     [ebp+NTFS.newRecord], edi
  2518.         mov     eax, [ebp+NTFS.frs_size]
  2519.         shr     eax, 9
  2520.         mul     edi
  2521.         mov     [ebp+NTFS.cur_iRecord], 0
  2522.         mov     [ebp+NTFS.cur_attr], 0x80
  2523.         mov     [ebp+NTFS.cur_offs], eax
  2524.         push    eax
  2525.         mov     [ebp+NTFS.cur_size], 0
  2526.         mov     eax, [ebp+NTFS.frs_buffer]
  2527.         mov     [ebp+NTFS.cur_buf], eax
  2528.         call    ntfs_read_attr
  2529.         pop     eax
  2530.         jc      ntfsFail
  2531.         cmp     eax, [ebp+NTFS.mftSize]
  2532.         jnc     .extendMFT
  2533.         jmp     .mftRecord
  2534.  
  2535. .extendBitmapMFT:
  2536.         mov     eax, [ebp+NTFS.sectors_per_cluster]
  2537.         mov     [ebp+NTFS.cur_offs], eax
  2538.         shl     eax, 9
  2539.         cmp     [ebp+NTFS.mftBitmapSize], eax
  2540.         jnc     ntfsUnsupported
  2541.         mov     [ebp+NTFS.cur_iRecord], 0
  2542.         mov     [ebp+NTFS.cur_attr], 0xB0
  2543.         mov     [ebp+NTFS.cur_size], 0
  2544.         call    ntfs_read_attr
  2545.         jc      ntfsFail
  2546.         mov     eax, [ebp+NTFS.mft_cluster]
  2547.         mul     [ebp+NTFS.sectors_per_cluster]
  2548.         cmp     eax, [ebp+NTFS.LastRead]
  2549.         jnz     ntfsUnsupported     ; auxiliary record
  2550.         mov     edi, [ebp+NTFS.mftBitmapBuffer]
  2551.         mov     ecx, [ebp+NTFS.mftBitmapSize]
  2552.         add     edi, ecx
  2553.         mov     eax, ecx
  2554.         mov     edx, [ebp+NTFS.attr_offs]
  2555.         add     ecx, 8
  2556.         mov     [edx+attributeRealSize], ecx
  2557.         mov     [edx+initialDataSize], ecx
  2558.         shl     eax, 3
  2559.         mov     [ebp+NTFS.newRecord], eax
  2560.         mov     dword [edi], 1
  2561.         mov     dword [edi+4], 0
  2562.         mov     [ebp+NTFS.cur_attr], 0x80
  2563.         mov     [ebp+NTFS.cur_offs], 0
  2564.         call    ntfs_read_attr.newAttribute
  2565.         jc      ntfsFail
  2566.         mov     [ebp+NTFS.mftBitmapSize], ecx
  2567. .extendMFT:
  2568.         mov     eax, [ebp+NTFS.mft_cluster]
  2569.         mul     [ebp+NTFS.sectors_per_cluster]
  2570.         cmp     eax, [ebp+NTFS.LastRead]
  2571.         jnz     ntfsUnsupported     ; auxiliary record
  2572.         mov     ecx, [ebp+NTFS.attr_offs]
  2573.         mov     eax, [ecx+attributeRealSize]
  2574.         mov     edx, [ecx+attributeRealSize+4]
  2575.         xor     ax, ax
  2576.         add     eax, 10000h
  2577.         adc     edx, 0
  2578.         push    [ebp+NTFS.fileDataStart]
  2579.         push    [ebp+NTFS.fileDataSize]
  2580.         call    resizeAttribute
  2581.         jc      ntfsErrorPop2
  2582.         mov     ebx, [ebp+NTFS.frs_buffer]
  2583.         mov     edx, [ebp+NTFS.LastRead]
  2584.         call    writeRecord     ; $MFT
  2585.         mov     eax, [ebp+NTFS.mftmirr_cluster]
  2586.         mul     [ebp+NTFS.sectors_per_cluster]
  2587.         mov     ecx, [ebp+NTFS.frs_size]
  2588.         shr     ecx, 9
  2589.         call    fs_write64_sys  ; $MFTMirr
  2590. ; update $MFT retrieval information
  2591.         mov     edi, [ebp+NTFS.mft_retrieval_end]
  2592.         mov     eax, [edi-4]
  2593.         add     eax, [edi-8]
  2594.         mov     edx, [ebp+NTFS.fileDataSize]
  2595.         cmp     eax, [ebp+NTFS.fileDataStart]
  2596.         jnz     .newFragment
  2597.         add     [edi-8], edx
  2598.         jmp     @f
  2599. .newFragment:
  2600.         lea     eax, [ebp+NTFS.attrlist_buf]
  2601.         cmp     eax, edi
  2602.         jz      @f
  2603.         mov     [edi], edx
  2604.         mov     eax, [ebp+NTFS.fileDataStart]
  2605.         mov     [edi+4], eax
  2606.         add     [ebp+NTFS.mft_retrieval_end], 8
  2607. @@:
  2608.         mov     eax, [ebp+NTFS.fileDataSize]
  2609.         mul     [ebp+NTFS.sectors_per_cluster]
  2610.         add     [ebp+NTFS.mftSize], eax
  2611.         call    ntfsSpaceClean
  2612.         pop     [ebp+NTFS.fileDataSize]
  2613.         pop     [ebp+NTFS.fileDataStart]
  2614. .mftRecord:
  2615.         mov     ecx, [ebp+NTFS.frs_size]
  2616.         shr     ecx, 2
  2617.         mov     edi, [ebp+NTFS.frs_buffer]
  2618.         xor     eax, eax
  2619.         rep stosd
  2620.         mov     esi, [ebp+NTFS.indexPointer]
  2621.         mov     eax, [ebp+NTFS.newRecord]
  2622.         mov     [esi+fileRecordReference], eax
  2623.         rdtsc
  2624.         mov     [esi+fileReferenceReuse], ax
  2625.         mov     edi, [ebp+NTFS.frs_buffer]
  2626. ; record header
  2627.         mov     [edi+reuseCounter], ax
  2628.         mov     [edi+2ah], ax
  2629.         mov     eax, [ebp+NTFS.frs_size]
  2630.         mov     [edi+recordAllocatedSize], eax
  2631.         shr     eax, 9
  2632.         inc     eax
  2633.         mov     [edi+updateSequenceSize], al
  2634.         shl     eax, 1
  2635.         add     eax, 2ah+7
  2636.         and     eax, not 7
  2637.         mov     dword[edi], 'FILE'
  2638.         mov     byte [edi+updateSequenceOffset], 2ah
  2639.         mov     byte [edi+hardLinkCounter], 1
  2640.         mov     byte [edi+newAttributeID], 3
  2641.         mov     [edi+attributeOffset], al
  2642.         add     edi, eax
  2643. ; $StandardInformation
  2644.         mov     byte [edi+attributeType], 10h
  2645.         mov     byte [edi+sizeWithHeader], 48h
  2646.         mov     byte [edi+sizeWithoutHeader], 30h
  2647.         mov     byte [edi+attributeOffset], 18h
  2648.         mov     cl, 8
  2649.         add     esi, fileCreated
  2650.         add     edi, 18h
  2651.         rep movsd
  2652.         add     edi, 16
  2653.         mov     esi, [ebp+NTFS.indexPointer]
  2654. ; $FileName
  2655.         mov     byte [edi+attributeType], 30h
  2656.         mov     byte [edi+attributeID], 1
  2657.         mov     byte [edi+attributeOffset], 18h
  2658.         mov     byte [edi+indexedFlag], 1
  2659.         mov     cx, [esi+indexRawSize]
  2660.         mov     [edi+sizeWithoutHeader], ecx
  2661.         mov     cx, [esi+indexAllocatedSize]
  2662.         add     ecx, 8
  2663.         mov     [edi+sizeWithHeader], ecx
  2664.         add     edi, 18h
  2665.         add     esi, 16
  2666.         sub     ecx, 18h
  2667.         shr     ecx, 2
  2668.         rep movsd
  2669.         mov     byte [edi+sizeWithHeader], 50h
  2670.         mov     byte [edi+attributeID], 2
  2671.         cmp     [ebp+NTFS.bFolder], 1
  2672.         jz      .indexRoot
  2673. ; $Data
  2674.         mov     byte [edi+attributeType], 80h
  2675.         mov     eax, [ebp+NTFS.fileDataSize]
  2676.         test    eax, eax
  2677.         jz      .resident
  2678.         mov     esi, [ebp+NTFS.indexPointer]
  2679.         dec     eax
  2680.         mov     [edi+lastVCN], eax
  2681.         mov     byte [edi+nonResidentFlag], 1
  2682.         mov     byte [edi+dataRunsOffset], 40h
  2683.         mov     eax, [esi+fileAllocatedSize]
  2684.         mov     [edi+attributeAllocatedSize], eax
  2685.         mov     eax, [esi+fileRealSize]
  2686.         mov     [edi+attributeRealSize], eax
  2687.         mov     [edi+initialDataSize], eax
  2688.         push    edi
  2689.         mov     esi, edi
  2690.         add     edi, 40h
  2691.         call    createMcbEntry
  2692.         inc     edi
  2693.         jmp     @f
  2694.  
  2695. .resident:
  2696.         mov     ecx, [ebp+NTFS.fileRealSize]
  2697.         mov     [edi+sizeWithoutHeader], ecx
  2698.         mov     byte [edi+attributeOffset], 18h
  2699.         push    edi
  2700.         mov     esi, [ebp+NTFS.fileDataBuffer]
  2701.         add     edi, 18h
  2702.         rep movsb
  2703. @@:
  2704.         mov     eax, edi
  2705.         pop     edi
  2706.         sub     eax, edi
  2707.         add     eax, 7
  2708.         and     eax, not 7
  2709.         mov     [edi+sizeWithHeader], eax
  2710.         add     edi, eax
  2711.         mov     al, 1
  2712.         jmp     .end
  2713.  
  2714. .indexRoot:
  2715.         mov     byte [edi+attributeType], 90h
  2716.         mov     byte [edi+nameLength], 4
  2717.         mov     byte [edi+nameOffset], 18h
  2718.         mov     byte [edi+sizeWithoutHeader], 30h
  2719.         mov     byte [edi+attributeOffset], 20h
  2720.         mov     dword[edi+18h], 490024h     ; unicode $I30
  2721.         mov     dword[edi+18h+4], 300033h
  2722.         mov     byte [edi+20h+indexedAttributesType], 30h
  2723.         mov     byte [edi+20h+collationRule], 1
  2724.         mov     eax, [ebp+NTFS.sectors_per_cluster]
  2725.         mov     dl, 1
  2726.         shl     eax, 8
  2727. @@:
  2728.         shl     eax, 1
  2729.         shl     edx, 1
  2730.         cmp     eax, [ebp+NTFS.frs_size]
  2731.         jc      @b
  2732.         shr     edx, 1
  2733.         mov     [edi+20h+indexRecordSize], eax
  2734.         mov     [edi+20h+indexRecordSizeClus], dl
  2735.         mov     byte [edi+30h+indexOffset], 16
  2736.         mov     byte [edi+30h+nodeRealSize], 32
  2737.         mov     byte [edi+30h+nodeAllocatedSize], 32
  2738.         mov     byte [edi+40h+indexAllocatedSize], 16
  2739.         mov     byte [edi+40h+indexFlags], 2
  2740.         add     edi, 50h
  2741.         mov     al, 3
  2742. .end:
  2743.         mov     ebx, [ebp+NTFS.frs_buffer]
  2744.         mov     dword [edi], -1
  2745.         mov     dword [edi+4], 0
  2746.         add     edi, 8
  2747.         sub     edi, ebx
  2748.         mov     [ebx+recordFlags], al
  2749.         mov     [ebx+recordRealSize], edi
  2750.         mov     edx, [ebp+NTFS.LastRead]
  2751.         call    writeRecord
  2752. ; write MFT bitmap
  2753.         mov     eax, [ebp+NTFS.newRecord]
  2754.         shr     eax, 3+9
  2755.         mov     ebx, eax
  2756.         shl     ebx, 9
  2757.         add     eax, [ebp+NTFS.mftBitmapLocation]
  2758.         add     ebx, [ebp+NTFS.mftBitmapBuffer]
  2759.         mov     ecx, 1
  2760.         xor     edx, edx
  2761.         call    fs_write64_sys
  2762. ; 5. Write directory node
  2763.         mov     ebx, [ebp+NTFS.cur_index_buf]
  2764.         mov     edx, [ebp+NTFS.nodeLastRead]
  2765.         call    writeRecord
  2766.         mov     ebx, [ebp+NTFS.fileRealSize]
  2767. ntfsDone:
  2768.         mov     esi, [ebp+PARTITION.Disk]
  2769.         call    disk_sync
  2770.         call    ntfs_unlock
  2771.         xor     eax, eax
  2772.         ret
  2773.  
  2774. writeRecord:
  2775. ; make updateSequence and write to disk
  2776. ;   in:
  2777. ; ebx -> record
  2778. ; edx = partition sector
  2779.         mov     esi, ebx
  2780.         mov     edi, ebx
  2781.         movzx   ecx, word [esi+updateSequenceOffset]
  2782.         add     edi, ecx
  2783.         mov     ax, [edi]
  2784.         inc     ax
  2785.         stosw
  2786.         mov     cx, [esi+updateSequenceSize]
  2787.         dec     ecx
  2788.         push    ecx
  2789. @@:
  2790.         add     esi, 510
  2791.         movsw
  2792.         mov     [esi-2], ax
  2793.         loop    @b
  2794.         mov     eax, edx
  2795.         xor     edx, edx
  2796.         pop     ecx
  2797.         jmp     fs_write64_sys
  2798.  
  2799. createMcbEntry:
  2800. ;   in:
  2801. ; [ebp+NTFS.fileDataStart] = position value
  2802. ; [ebp+NTFS.fileDataSize] = size value
  2803. ; edi -> destination
  2804. ; esi -> attribute header
  2805.         mov     eax, [ebp+NTFS.fileDataStart]
  2806.         xor     edx, edx
  2807.         shl     eax, 1
  2808.         jnc     @f
  2809.         not     eax
  2810. @@:
  2811.         inc     edx
  2812.         shr     eax, 8
  2813.         jnz     @b
  2814.         mov     eax, [ebp+NTFS.fileDataSize]
  2815.         shl     eax, 1
  2816.         xor     ecx, ecx
  2817. @@:
  2818.         inc     ecx
  2819.         shr     eax, 8
  2820.         jnz     @b
  2821.         lea     eax, [edi+edx+1]
  2822.         add     eax, ecx
  2823.         sub     eax, esi
  2824.         sub     eax, [esi+sizeWithHeader]
  2825.         jc      @f
  2826.         add     word [esi+sizeWithHeader], 8    ; extend attribute
  2827.         mov     esi, [ebp+NTFS.frs_buffer]
  2828.         mov     eax, [esi+recordRealSize]
  2829.         add     eax, 8
  2830.         cmp     [esi+recordAllocatedSize], eax
  2831.         jc      .end    ; no space in the record
  2832.         mov     [esi+recordRealSize], eax
  2833.         push    ecx edi
  2834.         add     esi, eax
  2835.         mov     ecx, esi
  2836.         sub     ecx, edi
  2837.         sub     ecx, 8
  2838.         shr     ecx, 2
  2839.         mov     edi, esi
  2840.         sub     edi, 4
  2841.         sub     esi, 12
  2842.         std
  2843.         rep movsd
  2844.         cld
  2845.         pop     edi ecx
  2846. @@:
  2847.         mov     eax, edx
  2848.         shl     eax, 4
  2849.         add     eax, ecx
  2850.         stosb
  2851.         lea     esi, [ebp+NTFS.fileDataSize]
  2852.         rep movsb
  2853.         lea     esi, [ebp+NTFS.fileDataStart]
  2854.         mov     ecx, edx
  2855.         rep movsb
  2856.         mov     [edi], cl
  2857. .end:
  2858.         ret
  2859.  
  2860. resizeAttribute:
  2861. ;   in:
  2862. ; [ebp+NTFS.frs_buffer] -> file record
  2863. ; [ebp+NTFS.attr_offs] -> attribute
  2864. ; edx:eax = new size
  2865. ;   out:
  2866. ; [ebp+NTFS.fileDataSize] = clusters added (positive)
  2867. ; [ebp+NTFS.fileDataStart] = added block
  2868. ; CF=1 -> eax = error code
  2869.         mov     esi, [ebp+NTFS.attr_offs]
  2870.         mov     dword [ebp+NTFS.attr_size], eax
  2871.         mov     dword [ebp+NTFS.attr_size+4], edx
  2872.         cmp     byte [esi+nonResidentFlag], 0
  2873.         jz      .resident
  2874.         mov     ecx, [ebp+NTFS.sectors_per_cluster]
  2875.         shl     ecx, 9
  2876.         mov     [esi+attributeRealSize], eax
  2877.         mov     [esi+attributeRealSize+4], edx
  2878.         mov     [esi+initialDataSize], eax
  2879.         mov     [esi+initialDataSize+4], edx
  2880.         sub     eax, 1
  2881.         sbb     edx, 0
  2882.         jc      .makeResident
  2883.         div     ecx
  2884.         mov     edi, eax
  2885.         inc     eax
  2886.         mul     ecx
  2887.         mov     [esi+attributeAllocatedSize], eax
  2888.         mov     [esi+attributeAllocatedSize+4], edx
  2889.         mov     ecx, [esi+lastVCN]
  2890.         mov     [esi+lastVCN], edi
  2891.         movzx   eax, byte [esi+dataRunsOffset]
  2892.         sub     edi, ecx
  2893.         mov     [ebp+NTFS.fileDataSize], edi
  2894.         jz      .done
  2895.         jc      .shrinkAttribute
  2896. ; extend attribute
  2897.         xor     edi, edi
  2898.         add     esi, eax
  2899.         push    edi edi edi edi
  2900. @@:
  2901.         mov     edx, eax
  2902.         mov     eax, esi
  2903.         add     edi, [esp+8]
  2904.         call    ntfs_decode_mcb_entry
  2905.         jc      @b
  2906.         mov     [esp+4], edx
  2907.         mov     [esp+12], edi
  2908.         add     edi, [esp]
  2909.         push    edi
  2910.         shr     edi, 5
  2911.         shl     edi, 2
  2912.         push    eax
  2913.         cmp     [ebp+NTFS.cur_iRecord], 0
  2914.         jz      @f
  2915.         cmp     edi, [ebp+NTFS.BitmapStart]
  2916.         jc      .err1
  2917. @@:
  2918.         call    ntfsSpaceAlloc
  2919.         jc      .err1
  2920.         mov     eax, [ebp+NTFS.fileDataStart]
  2921.         pop     edi
  2922.         pop     edx
  2923.         cmp     edx, eax
  2924.         jnz     .newEntry
  2925.         pop     edx
  2926.         pop     edi
  2927.         pop     [ebp+NTFS.fileDataStart]
  2928.         mov     [esp], eax
  2929.         push    [ebp+NTFS.fileDataSize]
  2930.         add     [ebp+NTFS.fileDataSize], edx
  2931.         jmp     @f
  2932.  
  2933. .newEntry:
  2934.         add     esp, 12
  2935.         pop     edx
  2936.         push    eax
  2937.         push    [ebp+NTFS.fileDataSize]
  2938.         sub     eax, edx
  2939.         mov     [ebp+NTFS.fileDataStart], eax
  2940. @@:
  2941.         mov     esi, [ebp+NTFS.attr_offs]
  2942.         call    createMcbEntry
  2943.         pop     [ebp+NTFS.fileDataSize]
  2944.         pop     [ebp+NTFS.fileDataStart]
  2945.         movi    eax, ERROR_UNSUPPORTED_FS
  2946. .done:
  2947.         ret
  2948.  
  2949. .err1:
  2950.         add     esp, 24
  2951.         stc
  2952. .err2:
  2953.         movi    eax, ERROR_DISK_FULL
  2954.         ret
  2955.  
  2956. .err3:
  2957.         movi    eax, ERROR_FS_FAIL
  2958.         add     esp, 20
  2959.         stc
  2960.         ret
  2961.  
  2962. .shrinkAttribute:
  2963.         add     ecx, edi
  2964.         inc     ecx
  2965.         add     esi, eax
  2966.         xor     edi, edi
  2967.         sub     esp, 20
  2968. @@:
  2969.         mov     [esp+16], esi
  2970.         call    ntfs_decode_mcb_entry
  2971.         jnc     .err3
  2972.         add     edi, [esp+8]
  2973.         sub     ecx, [esp]
  2974.         jnc     @b
  2975.         mov     ebx, ecx
  2976.         add     ecx, [esp]
  2977.         mov     eax, [esp+8]
  2978.         mov     [ebp+NTFS.fileDataSize], ecx
  2979.         mov     [ebp+NTFS.fileDataStart], eax
  2980.         push    edi
  2981.         add     edi, ecx
  2982.         neg     ebx
  2983.         call    ntfsSpaceFree
  2984.         pop     edi
  2985.         jc      .end
  2986. @@:
  2987.         call    ntfs_decode_mcb_entry
  2988.         jnc     .end
  2989.         cmp     dword[esp+8], 0
  2990.         jz      @b
  2991.         add     edi, [esp+8]
  2992.         mov     ebx, [esp]
  2993.         call    ntfsSpaceFree
  2994.         jnc     @b
  2995. .end:
  2996.         add     esp, 16
  2997.         pop     edi
  2998.         cmp     [ebp+NTFS.fileDataSize], 0
  2999.         jz      @f
  3000.         mov     esi, [ebp+NTFS.attr_offs]
  3001.         call    createMcbEntry
  3002.         mov     [ebp+NTFS.fileDataSize], 0
  3003. @@:
  3004.         ret
  3005.  
  3006. .resident:
  3007.         test    edx, edx
  3008.         jnz     .nonResident
  3009.         cmp     eax, 8000h
  3010.         jnc     .nonResident
  3011.         add     ax, [esi+attributeOffset]
  3012.         sub     eax, [esi+sizeWithHeader]
  3013.         jc      @f
  3014.         mov     edi, [ebp+NTFS.frs_buffer]
  3015.         mov     ecx, eax
  3016.         add     ecx, [edi+recordRealSize]
  3017.         cmp     [edi+recordAllocatedSize], ecx
  3018.         jc      .nonResident
  3019.         add     eax, 7
  3020.         and     eax, not 7
  3021.         add     [edi+recordRealSize], eax
  3022.         add     edi, [edi+recordRealSize]
  3023.         add     [esi+sizeWithHeader], eax
  3024.         add     esi, [esi+sizeWithHeader]
  3025.         mov     ecx, edi
  3026.         sub     ecx, esi
  3027.         shr     ecx, 2
  3028.         sub     edi, 4
  3029.         mov     esi, edi
  3030.         sub     esi, eax
  3031.         std
  3032.         rep movsd
  3033.         mov     ecx, eax
  3034.         shr     ecx, 2
  3035.         xor     eax, eax
  3036.         rep stosd
  3037.         cld
  3038.         mov     esi, [ebp+NTFS.attr_offs]
  3039. @@:
  3040.         mov     eax, dword [ebp+NTFS.attr_size]
  3041.         mov     [esi+sizeWithoutHeader], eax
  3042.         mov     [ebp+NTFS.fileDataSize], 0
  3043.         clc
  3044.         ret
  3045.  
  3046. .nonResident:   ; convert resident to non-resident
  3047.         mov     eax, dword [ebp+NTFS.attr_size]
  3048.         sub     eax, 1
  3049.         sbb     edx, 0
  3050.         mov     ecx, [ebp+NTFS.sectors_per_cluster]
  3051.         shl     ecx, 9
  3052.         div     ecx
  3053.         inc     eax
  3054.         mov     [ebp+NTFS.fileDataSize], eax
  3055.         mov     edi, [ebp+NTFS.BitmapStart]
  3056.         push    ecx
  3057.         call    ntfsSpaceAlloc
  3058.         pop     ecx
  3059.         jc      .err2
  3060.         mov     esi, [ebp+NTFS.attr_offs]
  3061.         xor     eax, eax
  3062.         xor     edx, edx
  3063. @@:
  3064.         add     eax, ecx
  3065.         inc     edx
  3066.         cmp     eax, [esi+sizeWithoutHeader]
  3067.         jc      @b
  3068.         push    edx
  3069.         push    eax
  3070.         stdcall kernel_alloc, eax
  3071.         mov     ecx, [esp]
  3072.         shr     ecx, 2
  3073.         mov     edi, eax
  3074.         mov     ebx, eax
  3075.         xor     eax, eax
  3076.         rep stosd
  3077.         mov     al, [esi+attributeOffset]
  3078.         mov     ecx, [esi+sizeWithoutHeader]
  3079.         add     esi, eax
  3080.         mov     edi, ebx
  3081.         rep movsb
  3082.         mov     eax, [ebp+NTFS.fileDataStart]
  3083.         mul     [ebp+NTFS.sectors_per_cluster]
  3084.         pop     ecx
  3085.         shr     ecx, 9
  3086.         call    fs_write64_app
  3087.         stdcall kernel_free, ebx
  3088.         mov     esi, [ebp+NTFS.attr_offs]
  3089.         add     esi, [esi+sizeWithHeader]
  3090.         mov     ecx, [ebp+NTFS.frs_buffer]
  3091.         add     ecx, [ecx+recordRealSize]
  3092.         sub     ecx, esi
  3093.         shr     ecx, 2
  3094.         lea     edi, [ebp+NTFS.bitmap_buf]
  3095.         push    ecx
  3096.         rep movsd
  3097.         mov     edi, [ebp+NTFS.attr_offs]
  3098.         add     edi, 16
  3099.         mov     cl, 6
  3100.         xor     eax, eax
  3101.         rep stosd
  3102.         mov     edi, [ebp+NTFS.attr_offs]
  3103.         mov     eax, [ebp+NTFS.fileDataSize]
  3104.         dec     eax
  3105.         mov     [edi+lastVCN], eax
  3106.         inc     eax
  3107.         mov     ecx, [ebp+NTFS.sectors_per_cluster]
  3108.         shl     ecx, 9
  3109.         mul     ecx
  3110.         mov     byte [edi+sizeWithHeader], 50h
  3111.         mov     byte [edi+nonResidentFlag], 1
  3112.         mov     byte [edi+dataRunsOffset], 40h
  3113.         mov     [edi+attributeAllocatedSize], eax
  3114.         mov     [edi+attributeAllocatedSize+4], edx
  3115.         mov     eax, dword [ebp+NTFS.attr_size]
  3116.         mov     edx, dword [ebp+NTFS.attr_size+4]
  3117.         mov     [edi+attributeRealSize], eax
  3118.         mov     [edi+attributeRealSize+4], edx
  3119.         mov     [edi+initialDataSize], eax
  3120.         mov     [edi+initialDataSize+4], edx
  3121.         mov     esi, edi
  3122.         add     edi, 40h
  3123.         call    createMcbEntry
  3124.         mov     eax, edi
  3125.         mov     edi, [ebp+NTFS.attr_offs]
  3126.         sub     eax, edi
  3127.         add     eax, 8
  3128.         and     eax, not 7
  3129.         mov     [edi+sizeWithHeader], eax
  3130.         pop     ecx
  3131.         lea     esi, [ebp+NTFS.bitmap_buf]
  3132.         add     edi, eax
  3133.         rep movsd
  3134.         mov     esi, [ebp+NTFS.frs_buffer]
  3135.         sub     edi, esi
  3136.         mov     [esi+recordRealSize], edi
  3137.         pop     edx
  3138.         sub     [ebp+NTFS.fileDataSize], edx
  3139.         add     [ebp+NTFS.fileDataStart], edx
  3140.         ret
  3141.  
  3142. .makeResident:  ; convert non-resident to empty resident
  3143.         movzx   eax, byte [esi+dataRunsOffset]
  3144.         mov     byte [esi+nonResidentFlag], 0
  3145.         mov     dword [esi+sizeWithoutHeader], 0
  3146.         mov     dword [esi+attributeOffset], 18h
  3147.         add     esi, eax
  3148.         xor     edi, edi
  3149.         sub     esp, 16
  3150. @@:
  3151.         call    ntfs_decode_mcb_entry
  3152.         jnc     @f
  3153.         cmp     dword[esp+8], 0
  3154.         jz      @b
  3155.         add     edi, [esp+8]
  3156.         mov     ebx, [esp]
  3157.         call    ntfsSpaceFree
  3158.         jnc     @b
  3159. @@:
  3160.         add     esp, 16
  3161.         mov     [ebp+NTFS.fileDataSize], 0
  3162.         ret
  3163.  
  3164. ntfsSpaceClean:
  3165. ; clean up to 16 Mb of disk space
  3166. ;   in:
  3167. ; [ebp+NTFS.fileDataStart] = block to clean
  3168. ; [ebp+NTFS.fileDataSize] = block size
  3169.         mov     eax, [ebp+NTFS.fileDataSize]
  3170.         test    eax, eax
  3171.         jz      @f
  3172.         mul     [ebp+NTFS.sectors_per_cluster]
  3173.         cmp     eax, 8001h
  3174.         jnc     @f
  3175.         push    eax
  3176.         shl     eax, 9
  3177.         stdcall kernel_alloc, eax
  3178.         pop     ecx
  3179.         test    eax, eax
  3180.         jz      @f
  3181.         push    ecx
  3182.         shl     ecx, 7
  3183.         mov     edi, eax
  3184.         mov     ebx, eax
  3185.         xor     eax, eax
  3186.         rep stosd
  3187.         mov     eax, [ebp+NTFS.fileDataStart]
  3188.         mul     [ebp+NTFS.sectors_per_cluster]
  3189.         mov     [ebp+NTFS.LastRead], eax
  3190.         pop     ecx
  3191.         call    fs_write64_app
  3192.         stdcall kernel_free, ebx
  3193. @@:
  3194.         ret
  3195.  
  3196. ntfsSpaceAlloc:
  3197. ; allocate disk space
  3198. ;   in:
  3199. ; edi = offset in bitmap to start search from
  3200. ; [ebp+NTFS.fileDataSize] = block size in clusters
  3201. ;   out:
  3202. ; [ebp+NTFS.fileDataStart] = allocated block starting cluster
  3203. ; CF=1 -> disk full
  3204.         mov     ecx, [ebp+NTFS.BitmapBuffer]
  3205.         add     edi, ecx
  3206.         add     ecx, [ebp+NTFS.BitmapSize]
  3207.         sub     ecx, edi
  3208.         ja      @f
  3209.         push    eax
  3210.         call    bitmapBuffering
  3211.         pop     eax
  3212.         shl     ecx, 2
  3213. @@:
  3214.         shr     ecx, 2
  3215.         push    ecx
  3216.         mov     eax, [ebp+NTFS.fileDataSize]
  3217.         shr     eax, 5
  3218.         jz      .small
  3219.         mov     ebx, eax    ; bitmap dwords
  3220. .start:
  3221.         mov     ecx, [ebp+NTFS.BitmapBuffer]
  3222.         add     ecx, [ebp+NTFS.BitmapSize]
  3223.         sub     ecx, edi
  3224.         shr     ecx, 2
  3225. @@:
  3226.         xor     eax, eax
  3227.         repnz scasd         ; search for empty dword
  3228.         jz      @f
  3229.         call    bitmapBuffering
  3230.         jmp     @b
  3231. @@:
  3232.         cmp     ecx, ebx
  3233.         jnc     @f
  3234.         call    bitmapBuffering
  3235.         jmp     @b
  3236. @@:
  3237.         sub     edi, 4
  3238.         mov     ecx, ebx
  3239.         mov     esi, edi
  3240.         xor     eax, eax
  3241.         repz scasd          ; check following dwords
  3242.         jnz     .start
  3243.         sub     esi, 4
  3244.         mov     eax, [esi]
  3245.         xor     edx, edx
  3246.         bsr     edx, eax
  3247.         inc     edx
  3248.         push    edx         ; starting bit
  3249.         push    esi         ; starting dword
  3250.         add     esi, 4
  3251.         neg     edx
  3252.         add     edx, 32
  3253.         mov     eax, [ebp+NTFS.fileDataSize]
  3254.         sub     eax, edx
  3255.         mov     edx, eax
  3256.         shr     eax, 5
  3257.         shl     eax, 2
  3258.         add     esi, eax
  3259.         mov     eax, [esi]
  3260.         bsf     ecx, eax    ; check last dword
  3261.         jz      .done
  3262.         and     edx, 31
  3263.         cmp     ecx, edx
  3264.         jnc     .done
  3265.         add     esp, 8
  3266.         jmp     .start
  3267.  
  3268. @@:
  3269.         sub     edi, 4
  3270.         call    bitmapBuffering
  3271.         push    ecx
  3272. .small:     ; less than 32 clusters
  3273.         pop     ecx
  3274.         or      eax, -1
  3275.         repz scasd
  3276.         jecxz   @b
  3277.         push    ecx
  3278.         mov     eax, [edi-4]
  3279.         not     eax
  3280. @@:
  3281.         bsf     ecx, eax    ; first 0
  3282.         jz      .small
  3283.         not     eax
  3284.         shr     eax, cl
  3285.         shl     eax, cl
  3286.         bsf     edx, eax    ; next 1
  3287.         jz      @f
  3288.         sub     edx, ecx
  3289.         cmp     edx, [ebp+NTFS.fileDataSize]
  3290.         jnc     .got        ; fits inside
  3291.         bsf     ecx, eax
  3292.         not     eax
  3293.         shr     eax, cl
  3294.         shl     eax, cl
  3295.         jmp     @b
  3296.  
  3297. @@:         ; next dword
  3298.         mov     eax, [edi]
  3299.         bsf     edx, eax
  3300.         jz      .got        ; empty
  3301.         add     edx, 32
  3302.         sub     edx, ecx
  3303.         cmp     edx, [ebp+NTFS.fileDataSize]
  3304.         jc      .small
  3305. .got:
  3306.         sub     edi, 4
  3307.         push    ecx         ; starting bit
  3308.         push    edi         ; starting dword
  3309. .done:      ; mark space
  3310.         pop     edi ecx
  3311.         cmp     ecx, 32
  3312.         jc      @f
  3313.         xor     ecx, ecx
  3314.         add     edi, 4
  3315. @@:
  3316.         push    ecx edi
  3317.         or      eax, -1
  3318.         shr     eax, cl
  3319.         shl     eax, cl
  3320.         neg     ecx
  3321.         add     ecx, 32
  3322.         sub     ecx, [ebp+NTFS.fileDataSize]
  3323.         jc      @f
  3324.         shl     eax, cl     ; fits inside dword
  3325.         shr     eax, cl
  3326.         or      [edi], eax
  3327.         jmp     .end
  3328.  
  3329. @@:
  3330.         or      [edi], eax
  3331.         neg     ecx
  3332.         push    ecx
  3333.         shr     ecx, 5
  3334.         add     edi, 4
  3335.         or      eax, -1
  3336.         rep stosd
  3337.         pop     ecx
  3338.         and     ecx, 31
  3339.         shr     eax, cl
  3340.         shl     eax, cl
  3341.         not     eax
  3342.         or      [edi], eax
  3343. .end:
  3344.         pop     eax
  3345.         pop     ecx
  3346.         sub     eax, [ebp+NTFS.BitmapBuffer]
  3347.         shl     eax, 3
  3348.         add     eax, ecx
  3349.         pop     ecx
  3350.         mov     ecx, [ebp+NTFS.fileDataSize]
  3351.         mov     [ebp+NTFS.fileDataStart], eax
  3352.         add     ecx, eax
  3353.         add     ecx, 4095
  3354.         shr     ecx, 3+9
  3355.         shr     eax, 3+9
  3356.         sub     ecx, eax
  3357.         mov     ebx, eax
  3358.         shl     ebx, 9
  3359.         add     eax, [ebp+NTFS.BitmapLocation]
  3360.         add     ebx, [ebp+NTFS.BitmapBuffer]
  3361.         xor     edx, edx
  3362.         jmp     fs_write64_app
  3363.  
  3364. ntfsSpaceFree:
  3365. ; free disk space
  3366. ;   in:
  3367. ; edi = starting cluster
  3368. ; ebx = size in clusters
  3369.         mov     eax, edi
  3370.         add     eax, ebx
  3371.         shr     eax, 3
  3372.         cmp     eax, [ebp+NTFS.BitmapSize]
  3373.         jc      @f
  3374.         add     eax, [ebp+NTFS.BitmapBuffer]
  3375.         push    edi
  3376.         mov     edi, eax
  3377.         call    bitmapBuffering
  3378.         pop     edi
  3379. @@:
  3380.         push    edi
  3381.         mov     ecx, edi
  3382.         shr     edi, 5
  3383.         shl     edi, 2
  3384.         add     edi, [ebp+NTFS.BitmapBuffer]
  3385.         and     ecx, 31
  3386.         xor     eax, eax
  3387.         dec     eax
  3388.         shr     eax, cl
  3389.         shl     eax, cl
  3390.         neg     ecx
  3391.         add     ecx, 32
  3392.         sub     ecx, ebx
  3393.         jc      @f
  3394.         shl     eax, cl     ; fits inside dword
  3395.         shr     eax, cl
  3396.         not     eax
  3397.         and     [edi], eax
  3398.         jmp     .writeBitmap
  3399.  
  3400. @@:
  3401.         not     eax
  3402.         and     [edi], eax
  3403.         neg     ecx
  3404.         push    ecx
  3405.         shr     ecx, 5
  3406.         add     edi, 4
  3407.         xor     eax, eax
  3408.         rep stosd
  3409.         pop     ecx
  3410.         and     ecx, 31
  3411.         dec     eax
  3412.         shr     eax, cl
  3413.         shl     eax, cl
  3414.         and     [edi], eax
  3415. .writeBitmap:
  3416.         pop     eax
  3417.         mov     edi, eax
  3418.         lea     ecx, [eax+ebx+4095]
  3419.         shr     eax, 3+9
  3420.         shr     ecx, 3+9
  3421.         sub     ecx, eax
  3422.         mov     ebx, eax
  3423.         shl     ebx, 9
  3424.         add     eax, [ebp+NTFS.BitmapLocation]
  3425.         add     ebx, [ebp+NTFS.BitmapBuffer]
  3426.         xor     edx, edx
  3427.         jmp     fs_write64_app
  3428.  
  3429. bitmapBuffering:
  3430. ; Extend BitmapBuffer and read next 32kb of bitmap
  3431. ; Warning: $Bitmap fragmentation is not foreseen
  3432. ; in: edi -> position in bitmap buffer
  3433. ; out: ecx = number of buffered dwords left
  3434.         push    ebx
  3435.         mov     eax, [ebp+NTFS.BitmapTotalSize]
  3436.         cmp     eax, [ebp+NTFS.BitmapSize]
  3437.         jz      .end
  3438.         stdcall alloc_pages, 8
  3439.         test    eax, eax
  3440.         jz      .end
  3441.         add     eax, 3
  3442.         mov     ebx, [ebp+NTFS.BitmapBuffer]
  3443.         add     ebx, [ebp+NTFS.BitmapSize]
  3444.         push    ebx
  3445.         mov     ecx, 8
  3446.         call    commit_pages
  3447.         mov     eax, [ebp+NTFS.BitmapSize]
  3448.         shr     eax, 9
  3449.         add     eax, [ebp+NTFS.BitmapLocation]
  3450.         pop     ebx
  3451.         mov     ecx, 64
  3452.         xor     edx, edx
  3453.         call    fs_read64_app
  3454.         test    eax, eax
  3455.         jnz     .err
  3456.         mov     eax, [ebp+NTFS.BitmapSize]
  3457.         add     eax, 8000h
  3458.         cmp     [ebp+NTFS.BitmapTotalSize], eax
  3459.         jnc     @f
  3460.         mov     eax, [ebp+NTFS.BitmapTotalSize]
  3461. @@:
  3462.         mov     [ebp+NTFS.BitmapSize], eax
  3463.         pop     ebx
  3464.         mov     ecx, [ebp+NTFS.BitmapBuffer]
  3465.         add     ecx, eax
  3466.         sub     ecx, edi
  3467.         jbe     bitmapBuffering
  3468.         shr     ecx, 2
  3469.         ret
  3470.  
  3471. .err:
  3472.         mov     eax, [ebp+NTFS.BitmapBuffer]
  3473.         add     eax, [ebp+NTFS.BitmapSize]
  3474.         mov     ecx, 8
  3475.         call    release_pages
  3476. .end:
  3477.         add     esp, 12     ; ret
  3478.         stc
  3479.         ret
  3480.  
  3481. ;----------------------------------------------------------------
  3482. ntfs_WriteFile:
  3483.         cmp     byte [esi], 0
  3484.         jnz     @f
  3485.         xor     ebx, ebx
  3486.         movi    eax, ERROR_ACCESS_DENIED
  3487.         ret
  3488. @@:
  3489.         call    ntfs_lock
  3490.         call    ntfs_find_lfn
  3491.         jc      ntfsNotFound
  3492.         cmp     [ebp+NTFS.cur_iRecord], 16
  3493.         jc      ntfsDenied
  3494.         test    dword [eax+fileFlags], 10000001h
  3495.         jnz     ntfsDenied
  3496.         cmp     [ebp+NTFS.fragmentCount], 1
  3497.         jnz     ntfsUnsupported     ; record fragmented
  3498. ; edit directory node
  3499.         mov     edi, [ebp+NTFS.cur_index_buf]
  3500.         cmp     dword [edi], 'INDX'
  3501.         jz      @f
  3502.         mov     esi, [ebp+NTFS.frs_buffer]
  3503.         mov     ecx, [esi+recordRealSize]
  3504.         shr     ecx, 2
  3505.         rep movsd
  3506.         mov     esi, [ebp+NTFS.attr_offs]
  3507.         mov     cl, [esi+attributeOffset]
  3508.         sub     esi, [ebp+NTFS.frs_buffer]
  3509.         add     eax, ecx
  3510.         add     eax, esi
  3511. @@:
  3512.         mov     edi, eax
  3513.         mov     eax, [ebx+4]
  3514.         mov     edx, [ebx+8]
  3515.         add     eax, [ebx+12]
  3516.         adc     edx, 0
  3517.         mov     [edi+fileRealSize], eax
  3518.         mov     [edi+fileRealSize+4], edx
  3519.         push    edx eax ebx
  3520.         call    ntfsGetTime
  3521.         mov     [edi+fileModified], eax
  3522.         mov     [edi+fileModified+4], edx
  3523.         mov     [edi+recordModified], eax
  3524.         mov     [edi+recordModified+4], edx
  3525.         mov     [edi+fileAccessed], eax
  3526.         mov     [edi+fileAccessed+4], edx
  3527.         pop     ebx ecx edx
  3528.         mov     eax, [ebp+NTFS.LastRead]
  3529.         mov     [ebp+NTFS.nodeLastRead], eax
  3530.         mov     [ebp+NTFS.cur_attr], 0x80
  3531.         mov     [ebp+NTFS.cur_offs], 0
  3532.         mov     [ebp+NTFS.cur_size], 0
  3533.         call    ntfs_read_attr
  3534.         jc      ntfsFail
  3535.         mov     esi, edi
  3536.         mov     edi, [ebp+NTFS.frs_buffer]
  3537.         cmp     word [edi+baseRecordReuse], 0
  3538.         jnz     ntfsUnsupported     ; auxiliary record
  3539.         mov     al, [edi+attributeOffset]
  3540.         add     edi, eax
  3541.         mov     al, [edi+attributeOffset]
  3542.         add     edi, eax
  3543.         mov     eax, ecx
  3544.         mov     ecx, 6
  3545.         add     esi, fileModified
  3546.         add     edi, 8
  3547.         rep movsd
  3548.         mov     ecx, [ebp+NTFS.attr_offs]
  3549.         cmp     word [ecx+attributeFlags], 0
  3550.         jnz     ntfsUnsupported
  3551.         push    ebx
  3552.         cmp     byte [ecx+nonResidentFlag], 0
  3553.         jz      .resizeAttribute
  3554.         cmp     edx, [ecx+attributeRealSize+4]
  3555.         jc      .writeNode
  3556.         jnz     .resizeAttribute
  3557.         cmp     [ecx+attributeRealSize], eax
  3558.         jnc     .writeNode
  3559. .resizeAttribute:
  3560.         call    resizeAttribute
  3561.         jc      ntfsErrorPop
  3562.         mov     ecx, [ebp+NTFS.attr_offs]
  3563.         cmp     byte [ecx+nonResidentFlag], 1
  3564.         jz      @f
  3565.         mov     ebx, [esp]
  3566.         movzx   edi, byte [ecx+attributeOffset]
  3567.         add     edi, ecx
  3568.         add     edi, [ebx+4]
  3569.         mov     ecx, [ebx+12]
  3570.         mov     esi, [ebx+16]
  3571.         rep movsb
  3572. @@:
  3573.         mov     ebx, [ebp+NTFS.frs_buffer]
  3574.         mov     edx, [ebp+NTFS.mftLastRead]
  3575.         call    writeRecord     ; file
  3576.         call    ntfs_restore_usa_frs
  3577. .writeNode:
  3578.         mov     ebx, [ebp+NTFS.cur_index_buf]
  3579.         mov     edx, [ebp+NTFS.nodeLastRead]
  3580.         call    writeRecord     ; directory
  3581.         pop     ebx
  3582.         mov     ecx, [ebp+NTFS.attr_offs]
  3583.         cmp     byte [ecx+nonResidentFlag], 0
  3584.         jz      .done
  3585.         mov     ecx, [ebx+12]
  3586.         test    ecx, ecx
  3587.         jz      .done
  3588.         mov     eax, [ebx+4]
  3589.         mov     edx, [ebx+8]
  3590.         mov     esi, [ebx+16]
  3591.         shrd    eax, edx, 9
  3592.         test    dword[ebx+4], 1FFh
  3593.         jz      .aligned
  3594.         mov     [ebp+NTFS.cur_offs], eax
  3595.         mov     [ebp+NTFS.cur_size], 1
  3596.         lea     edi, [ebp+NTFS.bitmap_buf]
  3597.         mov     [ebp+NTFS.cur_buf], edi
  3598.         call    ntfs_read_attr.continue
  3599.         jc      ntfsDevice
  3600.         mov     eax, [ebx+4]
  3601.         and     eax, 1FFh
  3602.         add     edi, eax
  3603.         sub     eax, [ebp+NTFS.cur_read]
  3604.         neg     eax
  3605.         push    ecx
  3606.         cmp     ecx, eax
  3607.         jb      @f
  3608.         mov     ecx, eax
  3609. @@:
  3610.         sub     [esp], ecx
  3611.         rep movsb
  3612.         push    ebx
  3613.         mov     eax, [ebp+NTFS.LastRead]
  3614.         lea     ebx, [ebp+NTFS.bitmap_buf]
  3615.         mov     ecx, 1
  3616.         xor     edx, edx
  3617.         call    fs_write64_app
  3618.         pop     ebx
  3619.         pop     ecx
  3620.         test    ecx, ecx
  3621.         jz      .done
  3622.         mov     eax, [ebx+4]
  3623.         mov     edx, [ebx+8]
  3624.         shrd    eax, edx, 9
  3625.         inc     eax
  3626. .aligned:
  3627.         push    ecx
  3628.         shr     ecx, 9
  3629.         mov     [ebp+NTFS.cur_offs], eax
  3630.         mov     [ebp+NTFS.cur_size], ecx
  3631.         mov     [ebp+NTFS.cur_buf], esi
  3632.         add     eax, ecx
  3633.         push    eax
  3634.         mov     [ebp+NTFS.bWriteAttr], 1
  3635.         call    ntfs_read_attr.continue
  3636.         mov     [ebp+NTFS.bWriteAttr], 0
  3637.         pop     [ebp+NTFS.cur_offs]
  3638.         pop     ecx
  3639.         jc      ntfsDevice
  3640.         and     ecx, 1FFh
  3641.         jz      .done
  3642.         add     esi, [ebp+NTFS.cur_read]
  3643.         mov     [ebp+NTFS.cur_size], 1
  3644.         lea     edi, [ebp+NTFS.bitmap_buf]
  3645.         mov     [ebp+NTFS.cur_buf], edi
  3646.         call    ntfs_read_attr.continue
  3647.         jc      ntfsDevice
  3648.         rep movsb
  3649.         push    ebx
  3650.         mov     eax, [ebp+NTFS.LastRead]
  3651.         lea     ebx, [ebp+NTFS.bitmap_buf]
  3652.         mov     ecx, 1
  3653.         xor     edx, edx
  3654.         call    fs_write64_app
  3655.         pop     ebx
  3656. .done:
  3657.         mov     ebx, [ebx+12]
  3658.         jmp     ntfsDone
  3659.  
  3660. ;----------------------------------------------------------------
  3661. ntfs_Delete:
  3662.         cmp     byte [esi], 0
  3663.         jnz     @f
  3664.         xor     ebx, ebx
  3665.         movi    eax, ERROR_ACCESS_DENIED
  3666.         ret
  3667.  
  3668. @@:
  3669.         call    ntfs_lock
  3670.         call    ntfs_find_lfn
  3671.         jc      ntfsNotFound
  3672.         cmp     [ebp+NTFS.cur_iRecord], 16
  3673.         jc      ntfsDenied
  3674.         test    byte [eax+fileFlags], 1
  3675.         jnz     ntfsDenied
  3676.         cmp     [ebp+NTFS.fragmentCount], 1
  3677.         jnz     ntfsUnsupported     ; record fragmented
  3678.         mov     ebx, [eax+directoryRecordReference]
  3679.         mov     [ebp+NTFS.newRecord], ebx
  3680.         mov     bx, [eax+fileReferenceReuse]
  3681.         mov     [ebp+NTFS.indexPointer], esi
  3682.         mov     eax, [ebp+NTFS.cur_iRecord]
  3683.         shr     eax, 3
  3684.         cmp     eax, [ebp+NTFS.mftBitmapSize]
  3685.         jnc     ntfsUnsupported
  3686. ; examine file record
  3687.         mov     [ebp+NTFS.cur_attr], 0x80   ; file?
  3688.         mov     [ebp+NTFS.cur_offs], 0
  3689.         mov     [ebp+NTFS.cur_size], 0
  3690.         call    ntfs_read_attr
  3691.         jnc     @f
  3692.         xor     eax, eax
  3693.         push    ebx eax eax eax eax
  3694.         mov     [esp+12], esp
  3695.         push    eax
  3696.         mov     ebx, esp
  3697.         mov     [ebp+NTFS.cur_attr], 0x90   ; folder?
  3698.         call    ntfs_ReadFolder.doit
  3699.         mov     edx, [esp+12]
  3700.         add     esp, 20
  3701.         pop     ebx
  3702.         test    eax, eax
  3703.         jnz     .ret
  3704.         cmp     edx, 2
  3705.         jnz     ntfsDenied      ; folder is not empty
  3706.         mov     [ebp+NTFS.cur_attr], 0xA0
  3707.         mov     [ebp+NTFS.cur_offs], 0
  3708.         mov     [ebp+NTFS.cur_size], 0
  3709.         call    ntfs_read_attr.newAttribute
  3710.         jc      .deleteFileRecord
  3711. @@:
  3712.         mov     esi, [ebp+NTFS.frs_buffer]
  3713.         cmp     word [esi+baseRecordReuse], 0
  3714.         jnz     ntfsUnsupported     ; auxiliary record
  3715.         cmp     word [esi+reuseCounter], bx
  3716.         jnz     .backToIndex        ; broken index
  3717.         test    byte [esi+recordFlags], 1
  3718.         jz      .writeBitmapMFT     ; record deleted
  3719.         cmp     byte [esi+hardLinkCounter], 3
  3720.         jnc     ntfsUnsupported
  3721.         mov     esi, [ebp+NTFS.attr_offs]
  3722.         cmp     byte [esi+nonResidentFlag], 0
  3723.         jz      .deleteFileRecord
  3724.         movzx   eax, byte [esi+dataRunsOffset]
  3725.         add     esi, eax
  3726.         xor     edi, edi
  3727.         sub     esp, 16
  3728. @@:
  3729.         call    ntfs_decode_mcb_entry
  3730.         jnc     @f
  3731.         cmp     dword[esp+8], 0
  3732.         jz      @b
  3733.         add     edi, [esp+8]
  3734.         mov     ebx, [esp]
  3735.         call    ntfsSpaceFree
  3736.         jnc     @b
  3737. @@:
  3738.         add     esp, 16
  3739. .deleteFileRecord:
  3740.         mov     ebx, [ebp+NTFS.frs_buffer]
  3741.         mov     byte [ebx+recordFlags], 0
  3742.         mov     edx, [ebp+NTFS.mftLastRead]
  3743.         call    writeRecord
  3744. .writeBitmapMFT:
  3745.         mov     eax, [ebp+NTFS.cur_iRecord]
  3746.         mov     ecx, eax
  3747.         shr     eax, 3
  3748.         and     ecx, 7
  3749.         mov     edi, [ebp+NTFS.mftBitmapBuffer]
  3750.         btr     [edi+eax], ecx
  3751.         shr     eax, 9
  3752.         mov     ebx, eax
  3753.         shl     ebx, 9
  3754.         add     eax, [ebp+NTFS.mftBitmapLocation]
  3755.         add     ebx, edi
  3756.         mov     ecx, 1
  3757.         xor     edx, edx
  3758.         call    fs_write64_sys
  3759. .backToIndex:
  3760.         mov     eax, [ebp+NTFS.newRecord]
  3761.         mov     [ebp+NTFS.cur_iRecord], eax
  3762.         mov     esi, [ebp+NTFS.indexPointer]
  3763.         call    ntfs_find_lfn.doit2
  3764.         jc      ntfsFail
  3765.         mov     ebx, [ebp+NTFS.secondIndexBuffer]
  3766.         mov     byte [ebx], 0
  3767.         mov     ebx, [ebp+NTFS.LastRead]
  3768.         mov     [ebp+NTFS.nodeLastRead], ebx
  3769.         xor     ebx, ebx
  3770.         test    byte [eax+indexFlags], 1
  3771.         jz      .deleteIndex    ; no subnode
  3772.         mov     edi, eax
  3773.         call    .findSubindex
  3774.         jc      ntfsFail
  3775.         movzx   edx, word [edi+indexAllocatedSize]
  3776.         test    esi, esi
  3777.         jz      @f
  3778.         sub     edx, eax
  3779.         sub     edx, 8
  3780. @@:
  3781.         mov     eax, edi
  3782.         mov     ebx, esi
  3783.         jmp     @f
  3784.  
  3785. .deleteIndex:
  3786.         movzx   edx, word [eax+indexAllocatedSize]
  3787.         mov     ecx, [eax+fileRecordReference]
  3788.         cmp     [eax+edx+fileRecordReference], ecx
  3789.         jnz     @f
  3790.         add     dx, [eax+edx+indexAllocatedSize]
  3791. @@:
  3792.         mov     edi, [ebp+NTFS.cur_index_buf]
  3793.         cmp     dword [edi], 'INDX'
  3794.         jz      .indexRecord
  3795.         sub     eax, edi
  3796.         mov     edi, [ebp+NTFS.indexRoot]
  3797.         sub     [edi+sizeWithHeader], edx
  3798.         sub     [edi+sizeWithoutHeader], edx
  3799.         movzx   ecx, byte [edi+attributeOffset]
  3800.         add     edi, ecx
  3801.         add     eax, edi
  3802.         sub     [edi+rootNode+nodeRealSize], edx
  3803.         sub     [edi+rootNode+nodeAllocatedSize], edx
  3804.         mov     edi, [ebp+NTFS.frs_buffer]
  3805.         sub     [edi+recordRealSize], edx
  3806.         mov     ecx, [edi+recordRealSize]
  3807.         cmp     [edi+recordAllocatedSize], ecx
  3808.         jmp     @f
  3809.  
  3810. .indexRecord:
  3811.         add     edi, recordNode
  3812.         sub     [edi+nodeRealSize], edx
  3813.         mov     ecx, [edi+nodeRealSize]
  3814.         cmp     [edi+nodeAllocatedSize], ecx
  3815. @@:
  3816.         jc      ntfsUnsupported
  3817.         add     ecx, edi
  3818.         sub     ecx, eax
  3819.         mov     esi, eax
  3820.         add     esi, edx
  3821.         mov     edi, eax
  3822.         test    edx, edx
  3823.         jns     @f
  3824.         neg     edx
  3825.         add     edx, ecx
  3826.         sub     edx, 4
  3827.         add     esi, edx
  3828.         add     edi, edx
  3829.         std
  3830. @@:
  3831.         jz      @f
  3832.         shr     ecx, 2
  3833.         rep movsd
  3834.         cld
  3835. @@:
  3836.         test    ebx, ebx
  3837.         jz      .done
  3838. ; copy index from the subnode to replace deleted pointing index
  3839.         movzx   ecx, word [ebx+indexAllocatedSize]
  3840.         mov     edx, ecx
  3841.         test    byte [ebx+indexFlags], 1
  3842.         jz      @f
  3843.         sub     ecx, 8
  3844.         movzx   edi, word [ebx+edx+indexAllocatedSize]
  3845.         add     edi, edx
  3846.         mov     esi, [ebx+ecx]
  3847.         mov     [ebx+edi-8], esi
  3848.         mov     [ebx+indexAllocatedSize], cx
  3849. @@:
  3850.         shr     ecx, 2
  3851.         mov     esi, ebx
  3852.         mov     edi, eax
  3853.         rep movsd
  3854.         add     word [eax+indexAllocatedSize], 8
  3855.         mov     byte [eax+indexFlags], 1
  3856.         mov     edi, [ebp+NTFS.secondIndexBuffer]
  3857.         mov     eax, ebx
  3858.         xor     ebx, ebx
  3859.         jmp     .indexRecord
  3860.  
  3861. .done:
  3862.         mov     ebx, [ebp+NTFS.frs_buffer]
  3863.         mov     edx, [ebp+NTFS.rootLastRead]
  3864.         call    writeRecord
  3865.         mov     ebx, [ebp+NTFS.cur_index_buf]
  3866.         cmp     dword [ebx], 'INDX'
  3867.         jnz     @f
  3868.         mov     edx, [ebp+NTFS.nodeLastRead]
  3869.         call    writeRecord
  3870. @@:
  3871.         mov     ebx, [ebp+NTFS.secondIndexBuffer]
  3872.         cmp     byte [ebx], 0
  3873.         jz      ntfsDone
  3874.         mov     edx, [ebp+NTFS.LastRead]
  3875.         call    writeRecord
  3876.         jmp     ntfsDone
  3877.  
  3878. .findSubindex:
  3879. ; in: eax -> index
  3880. ;   out:
  3881. ; CF=1 -> error
  3882. ; esi=0 -> subnode deleted
  3883. ; esi -> replacement index
  3884. ; eax = index effective size
  3885.         movzx   edx, word [eax+indexAllocatedSize]
  3886.         mov     eax, [eax+edx-8]
  3887.         mov     edx, [ebp+NTFS.cur_size]
  3888.         push    edx
  3889.         cmp     edx, [ebp+NTFS.cur_subnode_size]
  3890.         jz      @f
  3891.         mul     [ebp+NTFS.sectors_per_cluster]
  3892. @@:
  3893.         mov     [ebp+NTFS.cur_attr], 0xA0
  3894.         mov     [ebp+NTFS.cur_offs], eax
  3895.         push    eax
  3896.         mov     ebx, [ebp+NTFS.secondIndexBuffer]
  3897.         mov     esi, ebx
  3898.         mov     [ebp+NTFS.cur_buf], ebx
  3899.         call    ntfs_read_attr.newAttribute
  3900.         pop     [ebp+NTFS.cur_offs]
  3901.         pop     eax
  3902.         jc      .ret
  3903.         cmp     dword [esi], 'INDX'
  3904.         stc
  3905.         jnz     .ret
  3906.         mov     [ebp+NTFS.cur_size], eax
  3907.         shl     eax, 9
  3908.         call    ntfs_restore_usa
  3909.         jc      .ret
  3910.         add     esi, recordNode
  3911.         add     esi, [esi+indexOffset]
  3912.         test    byte [esi+indexFlags], 2
  3913.         jnz     .emptyNode
  3914.         cmp     [ebp+NTFS.fragmentCount], 1
  3915.         stc
  3916.         jnz     .ret    ; record fragmented
  3917.         xor     eax, eax
  3918. @@:
  3919.         add     esi, eax
  3920.         mov     ax, [esi+indexAllocatedSize]
  3921.         test    byte [esi+eax+indexFlags], 2
  3922.         jz      @b
  3923.         test    byte [esi+indexFlags], 1
  3924.         jz      .ret
  3925.         add     eax, esi
  3926.         push    esi
  3927.         push    [ebp+NTFS.cur_offs]
  3928.         call    .findSubindex
  3929.         pop     [ebp+NTFS.cur_offs]
  3930.         pop     edx
  3931.         jc      .ret
  3932.         test    esi, esi
  3933.         jnz     .ret
  3934.         mov     esi, edx
  3935.         mov     ebx, [ebp+NTFS.secondIndexBuffer]
  3936.         mov     [ebp+NTFS.cur_buf], ebx
  3937.         push    [ebp+NTFS.cur_size]
  3938.         call    ntfs_read_attr.continue
  3939.         pop     eax
  3940.         jc      .ret
  3941.         shl     eax, 9
  3942.         call    ntfs_restore_usa
  3943.         jc      .ret
  3944.         movzx   eax, word [esi+indexAllocatedSize]
  3945.         sub     eax, 8
  3946. .ret:
  3947.         ret
  3948.  
  3949. .emptyNode:
  3950.         test    byte [esi+indexFlags], 1
  3951.         jz      @f
  3952.         mov     eax, esi
  3953.         push    [ebp+NTFS.cur_offs]
  3954.         call    .findSubindex
  3955.         pop     [ebp+NTFS.cur_offs]
  3956.         jc      .ret
  3957.         test    esi, esi
  3958.         jnz     .ret
  3959. @@:         ; delete node
  3960.         mov     esi, [ebp+NTFS.attr_offs]
  3961.         add     esi, [esi+sizeWithHeader]
  3962.         cmp     byte [esi], 0xB0
  3963.         stc
  3964.         jnz     .ret
  3965.         movzx   eax, byte [esi+attributeOffset]
  3966.         add     esi, eax
  3967.         mov     eax, [ebp+NTFS.cur_offs]
  3968.         xor     edx, edx
  3969.         div     [ebp+NTFS.cur_size]
  3970.         mov     edx, eax
  3971.         shr     eax, 3
  3972.         and     edx, 7
  3973.         btr     [esi+eax], edx
  3974.         mov     esi, [ebp+NTFS.secondIndexBuffer]
  3975.         mov     byte [esi], 0
  3976.         xor     esi, esi
  3977.         ret
  3978.  
  3979. ;----------------------------------------------------------------
  3980. ntfs_SetFileEnd:
  3981.         cmp     byte [esi], 0
  3982.         jnz     @f
  3983.         xor     ebx, ebx
  3984.         movi    eax, ERROR_ACCESS_DENIED
  3985.         ret
  3986. @@:
  3987.         call    ntfs_lock
  3988.         call    ntfs_find_lfn
  3989.         jc      ntfsNotFound
  3990.         cmp     [ebp+NTFS.cur_iRecord], 16
  3991.         jc      ntfsDenied
  3992.         test    dword [eax+fileFlags], 10000001h
  3993.         jnz     ntfsDenied
  3994.         cmp     [ebp+NTFS.fragmentCount], 1
  3995.         jnz     ntfsUnsupported     ; record fragmented
  3996. ; edit directory node
  3997.         mov     edi, [ebp+NTFS.cur_index_buf]
  3998.         cmp     dword [edi], 'INDX'
  3999.         jz      @f
  4000.         mov     esi, [ebp+NTFS.frs_buffer]
  4001.         mov     ecx, [esi+recordRealSize]
  4002.         shr     ecx, 2
  4003.         rep movsd
  4004.         mov     esi, [ebp+NTFS.attr_offs]
  4005.         mov     cl, [esi+attributeOffset]
  4006.         sub     esi, [ebp+NTFS.frs_buffer]
  4007.         add     eax, ecx
  4008.         add     eax, esi
  4009. @@:
  4010.         mov     edi, eax
  4011.         mov     eax, [ebx+4]
  4012.         mov     edx, [ebx+8]
  4013.         mov     [edi+fileRealSize], eax
  4014.         mov     [edi+fileRealSize+4], edx
  4015.         push    edx eax ebx
  4016.         call    ntfsGetTime
  4017.         mov     [edi+fileModified], eax
  4018.         mov     [edi+fileModified+4], edx
  4019.         mov     [edi+recordModified], eax
  4020.         mov     [edi+recordModified+4], edx
  4021.         mov     [edi+fileAccessed], eax
  4022.         mov     [edi+fileAccessed+4], edx
  4023.         pop     ebx ecx edx
  4024.         mov     eax, [ebp+NTFS.LastRead]
  4025.         mov     [ebp+NTFS.nodeLastRead], eax
  4026.         mov     [ebp+NTFS.cur_attr], 0x80
  4027.         mov     [ebp+NTFS.cur_offs], 0
  4028.         mov     [ebp+NTFS.cur_size], 0
  4029.         call    ntfs_read_attr
  4030.         jc      ntfsFail
  4031.         mov     esi, edi
  4032.         mov     edi, [ebp+NTFS.frs_buffer]
  4033.         cmp     word [edi+baseRecordReuse], 0
  4034.         jnz     ntfsUnsupported     ; auxiliary record
  4035.         mov     al, [edi+attributeOffset]
  4036.         add     edi, eax
  4037.         mov     al, [edi+attributeOffset]
  4038.         add     edi, eax
  4039.         mov     eax, ecx
  4040.         mov     ecx, 6
  4041.         add     esi, fileModified
  4042.         add     edi, 8
  4043.         rep movsd
  4044.         mov     ecx, [ebp+NTFS.attr_offs]
  4045.         cmp     word [ecx+attributeFlags], 0
  4046.         jnz     ntfsUnsupported
  4047.         cmp     byte [ecx+nonResidentFlag], 0
  4048.         jz      .resizeAttribute
  4049.         cmp     [ecx+attributeRealSize+4], edx
  4050.         jnz     .resizeAttribute
  4051.         cmp     [ecx+attributeRealSize], eax
  4052.         jnc     .resizeAttribute
  4053.         mov     eax, [ecx+attributeRealSize]
  4054.         mov     ecx, [ebp+NTFS.sectors_per_cluster]
  4055.         mov     [ebp+NTFS.cur_size], ecx
  4056.         shl     ecx, 9
  4057.         div     ecx
  4058.         test    edx, edx
  4059.         jz      .aligned
  4060.         push    edx
  4061.         push    ecx
  4062.         mul     [ebp+NTFS.sectors_per_cluster]
  4063.         mov     [ebp+NTFS.cur_offs], eax
  4064.         stdcall kernel_alloc, ecx
  4065.         pop     ecx
  4066.         pop     edi
  4067.         mov     esi, eax
  4068.         sub     ecx, edi
  4069.         add     edi, eax
  4070.         mov     [ebp+NTFS.cur_buf], eax
  4071.         call    ntfs_read_attr.continue
  4072.         jc      @f
  4073.         xor     eax, eax
  4074.         rep stosb
  4075.         push    ebx
  4076.         mov     eax, [ebp+NTFS.LastRead]
  4077.         mov     ebx, esi
  4078.         mov     ecx, [ebp+NTFS.sectors_per_cluster]
  4079.         xor     edx, edx
  4080.         call    fs_write64_app
  4081.         pop     ebx
  4082. @@:
  4083.         stdcall kernel_free, esi
  4084. .aligned:
  4085.         mov     eax, [ebx+4]
  4086.         mov     edx, [ebx+8]
  4087. .resizeAttribute:
  4088.         call    resizeAttribute
  4089.         jc      ntfsError
  4090.         mov     ebx, [ebp+NTFS.frs_buffer]
  4091.         mov     edx, [ebp+NTFS.mftLastRead]
  4092.         call    writeRecord     ; file
  4093.         mov     ebx, [ebp+NTFS.cur_index_buf]
  4094.         mov     edx, [ebp+NTFS.nodeLastRead]
  4095.         call    writeRecord     ; directory
  4096.         call    ntfsSpaceClean
  4097.         jmp     ntfsDone
  4098.  
  4099. ntfsGetTime:
  4100.         call    fsGetTime
  4101.         jmp     @f
  4102.  
  4103. ntfsCalculateTime:
  4104. ; in: esi -> data block
  4105. ; out: edx:eax = seconds since 01.01.1601 x10000000
  4106.         call    fsCalculateTime
  4107. @@:
  4108.         mov     edx, 10000000
  4109.         mul     edx
  4110.         add     eax, 3365781504
  4111.         adc     edx, 29389701
  4112.         ret
  4113.  
  4114. ;----------------------------------------------------------------
  4115. ntfs_SetFileInfo:
  4116.         cmp     byte [esi], 0
  4117.         jnz     @f
  4118.         movi    eax, ERROR_UNSUPPORTED_FS
  4119.         ret
  4120. @@:
  4121.         call    ntfs_lock
  4122.         call    ntfs_find_lfn
  4123.         jnc     @f
  4124.         test    eax, eax
  4125.         jz      ntfsFail
  4126.         jmp     ntfsNotFound
  4127.  
  4128. @@:
  4129.         cmp     [ebp+NTFS.fragmentCount], 1
  4130.         jnz     ntfsUnsupported     ; record fragmented
  4131.         mov     esi, [ebp+NTFS.cur_index_buf]
  4132.         cmp     dword [esi], 'INDX'
  4133.         jz      @f
  4134.         sub     eax, esi
  4135.         mov     esi, [ebp+NTFS.indexRoot]
  4136.         movzx   edx, byte [esi+attributeOffset]
  4137.         add     eax, esi
  4138.         add     eax, edx
  4139. @@:
  4140.         mov     esi, [ebx+16]
  4141.         mov     edi, eax
  4142.         mov     eax, [esi]
  4143.         and     eax, 27h
  4144.         and     byte [edi+fileFlags], -28h
  4145.         or      [edi+fileFlags], al
  4146.         add     esi, 8
  4147.         call    ntfsCalculateTime
  4148.         mov     [edi+fileCreated], eax
  4149.         mov     [edi+fileCreated+4], edx
  4150.         add     esi, 8
  4151.         call    ntfsCalculateTime
  4152.         mov     [edi+fileAccessed], eax
  4153.         mov     [edi+fileAccessed+4], edx
  4154.         add     esi, 8
  4155.         call    ntfsCalculateTime
  4156.         mov     [edi+fileModified], eax
  4157.         mov     [edi+fileModified+4], edx
  4158.         mov     ebx, [ebp+NTFS.cur_index_buf]
  4159.         cmp     dword [ebx], 'INDX'
  4160.         jz      @f
  4161.         mov     ebx, [ebp+NTFS.frs_buffer]
  4162. @@:
  4163.         mov     edx, [ebp+NTFS.LastRead]
  4164.         call    writeRecord
  4165.         jmp     ntfsDone
  4166.  
  4167. ntfsUnsupported:
  4168.         push    ERROR_UNSUPPORTED_FS
  4169.         jmp     ntfsOut
  4170. ntfsDevice:
  4171.         push    ERROR_DEVICE
  4172.         jmp     ntfsOut
  4173. ntfsNotFound:
  4174.         push    ERROR_FILE_NOT_FOUND
  4175.         jmp     ntfsOut
  4176. ntfsDenied:
  4177.         push    ERROR_ACCESS_DENIED
  4178.         jmp     ntfsOut
  4179. ntfsFail:
  4180.         push    ERROR_FS_FAIL
  4181.         jmp     ntfsOut
  4182. ntfsDiskFull:
  4183.         push    ERROR_DISK_FULL
  4184.         jmp     ntfsOut
  4185. ntfsErrorPop5:
  4186.         pop     ebx
  4187.         pop     ebx
  4188. ntfsErrorPop3:
  4189.         pop     ebx
  4190. ntfsErrorPop2:
  4191.         pop     ebx
  4192. ntfsErrorPop:
  4193.         pop     ebx
  4194. ntfsError:
  4195.         push    eax
  4196. ntfsOut:
  4197.         call    ntfs_unlock
  4198.         xor     ebx, ebx
  4199.         pop     eax
  4200.         ret
  4201.