Subversion Repositories Kolibri OS

Rev

Rev 6798 | Rev 6824 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
6080 pathoswith 3
;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;;
6462 pathoswith 4
;;  Distributed under terms of the GNU General Public License.  ;;
2288 clevermous 5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
$Revision: 6820 $
9
 
6462 pathoswith 10
; NTFS external functions
11
;   in:
12
; ebx -> parameter structure of sysfunc 70
13
; ebp -> NTFS structure
6471 pathoswith 14
; esi -> path string in UTF-8
6462 pathoswith 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
5954 pathoswith 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
6273 pathoswith 55
magic = 0
5954 pathoswith 56
updateSequenceOffset = 4
57
updateSequenceSize = 6
6273 pathoswith 58
    ; FileRecord header
5954 pathoswith 59
reuseCounter = 16
60
hardLinkCounter = 12h
61
attributeOffset = 14h
62
recordFlags = 16h
63
recordRealSize = 18h
64
recordAllocatedSize = 1ch
5994 pathoswith 65
baseRecordReference = 20h       ; for auxiliary records
66
baseRecordReuse = 26h
5954 pathoswith 67
newAttributeID = 28h
68
    ; attribute header
69
attributeType = 0
70
sizeWithHeader = 4
71
nonResidentFlag = 8
72
nameLength = 9
73
nameOffset = 10
6019 pathoswith 74
attributeFlags = 12
5954 pathoswith 75
attributeID = 14
6080 pathoswith 76
    ; resident attribute header
77
sizeWithoutHeader = 10h
6273 pathoswith 78
attributeOffset = 14h
6019 pathoswith 79
indexedFlag = 16h
5954 pathoswith 80
    ; non resident attribute header
6080 pathoswith 81
firstVCN = 10h
5954 pathoswith 82
lastVCN = 18h
83
dataRunsOffset = 20h
84
attributeAllocatedSize = 28h
85
attributeRealSize = 30h
86
initialDataSize = 38h
87
    ; $IndexRoot
6273 pathoswith 88
indexedAttributesType = 0
5954 pathoswith 89
collationRule = 4
90
indexRecordSize = 8
6340 pathoswith 91
indexRecordSizeClus = 12        ; in sectors if less than one cluster
6273 pathoswith 92
rootNode = 16
93
    ; IndexRecord header
94
recordVCN = 16
95
recordNode = 18h
5954 pathoswith 96
    ; node header
97
indexOffset = 0
98
nodeRealSize = 4
99
nodeAllocatedSize = 8
6273 pathoswith 100
nonLeafFlag = 12
5954 pathoswith 101
    ; $Filename index
102
fileRecordReference = 0
103
fileReferenceReuse = 6
104
indexAllocatedSize = 8
105
indexRawSize = 10
106
indexFlags = 12
107
directoryRecordReference = 16
108
directoryReferenceReuse = 16h
6297 pathoswith 109
fileCreated = 18h
110
fileModified = 20h
111
recordModified = 28h
112
fileAccessed = 30h
5954 pathoswith 113
fileAllocatedSize = 38h
114
fileRealSize = 40h
115
fileFlags = 48h
116
fileNameLength = 50h
6297 pathoswith 117
namespace = 51h
118
fileName = 52h
5954 pathoswith 119
 
3742 clevermous 120
struct NTFS PARTITION
6462 pathoswith 121
Lock                MUTEX   ; Currently operations with one partition
5954 pathoswith 122
; can not be executed in parallel since the legacy code is not ready.
6273 pathoswith 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  ?
6405 pathoswith 129
mftSize             dd  ?   ; in sectors
6273 pathoswith 130
cur_index_size      dd  ?   ; in sectors
131
cur_index_buf       dd  ?   ; index node buffer
6340 pathoswith 132
secondIndexBuffer   dd  ?
6273 pathoswith 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
2288 clevermous 141
 
6405 pathoswith 142
attr_size           dq  ?
143
attr_offs           dd  ?
144
attr_list           dd  ?
145
attr_iBaseRecord    dd  ?
6273 pathoswith 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
6405 pathoswith 152
cur_tail            dd  ?
153
cur_subnode_size    dd  ?
6273 pathoswith 154
LastRead            dd  ?   ; last readen block of sectors
6408 pathoswith 155
mftLastRead         dd  ?
6340 pathoswith 156
rootLastRead        dd  ?
157
nodeLastRead        dd  ?
158
indexRoot           dd  ?
6405 pathoswith 159
indexPointer        dd  ?
6340 pathoswith 160
newRecord           dd  ?
6273 pathoswith 161
fileDataStart       dd  ?   ; starting cluster
162
fileDataSize        dd  ?   ; in clusters
6292 pathoswith 163
fileDataBuffer      dd  ?
6273 pathoswith 164
fileRealSize        dd  ?   ; in bytes
165
fragmentCount       db  ?
166
bCanContinue        db  ?
167
bFolder             db  ?
168
bWriteAttr          db  ?   ; Warning: Don't forget to turn off!!!
3742 clevermous 169
 
6558 pathoswith 170
mft_retrieval       rb  512
171
align0  rb  1024-NTFS.align0
6405 pathoswith 172
attrlist_buf        rb  1024
173
attrlist_mft_buf    rb  1024
174
bitmap_buf          rb  1024
3742 clevermous 175
ends
176
 
2288 clevermous 177
ntfs_test_bootsec:
5954 pathoswith 178
; in: ebx -> buffer, edx = size of partition
179
; out: CF=1 -> invalid
2288 clevermous 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
5954 pathoswith 232
; 7. Clusters per FRS must be either power of 2 or between -31 and -9
2288 clevermous 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
5954 pathoswith 242
@@:         ; 8. Same for clusters per IndexAllocationBuffer
2288 clevermous 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
5954 pathoswith 252
@@:         ; OK, this is correct NTFS bootsector
2288 clevermous 253
        clc
254
        ret
5954 pathoswith 255
.no:        ; No, this bootsector isn't NTFS
2288 clevermous 256
        stc
257
        ret
258
 
6462 pathoswith 259
; Mount if it's a valid NTFS partition.
5954 pathoswith 260
ntfs_create_partition:
6462 pathoswith 261
;   in:
262
; ebp -> PARTITION structure
263
; ebx -> boot sector
264
; ebx+512 -> buffer
265
;   out:
266
; eax -> NTFS structure, 0 = not NTFS
5089 clevermous 267
        cmp     dword [esi+DISK.MediaInfo.SectorSize], 512
268
        jnz     .nope
3742 clevermous 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
5954 pathoswith 284
        jnz     .nope
3742 clevermous 285
.boot_read_ok:
286
        call    ntfs_test_bootsec
287
        jnc     .ntfs_setup
288
.nope:
289
        xor     eax, eax
290
        jmp     .exit
6297 pathoswith 291
 
6405 pathoswith 292
.ntfs_setup:    ; By given bootsector, initialize some NTFS variables
293
        stdcall kernel_alloc, 1000h
3742 clevermous 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
6273 pathoswith 303
        mov     [eax+NTFS.bWriteAttr], 0
5954 pathoswith 304
 
3742 clevermous 305
        push    ebx ebp esi
306
        mov     ebp, eax
307
        lea     ecx, [ebp+NTFS.Lock]
308
        call    mutex_init
2288 clevermous 309
        movzx   eax, byte [ebx+13]
3742 clevermous 310
        mov     [ebp+NTFS.sectors_per_cluster], eax
2288 clevermous 311
        mov     eax, [ebx+0x28]
3742 clevermous 312
        mov     dword [ebp+NTFS.Length], eax
313
        and     dword [ebp+NTFS.Length+4], 0
2288 clevermous 314
        mov     eax, [ebx+0x30]
3742 clevermous 315
        mov     [ebp+NTFS.mft_cluster], eax
2288 clevermous 316
        mov     eax, [ebx+0x38]
3742 clevermous 317
        mov     [ebp+NTFS.mftmirr_cluster], eax
2288 clevermous 318
        movsx   eax, byte [ebx+0x40]
319
        test    eax, eax
5954 pathoswith 320
        js      @f
3742 clevermous 321
        mul     [ebp+NTFS.sectors_per_cluster]
2288 clevermous 322
        shl     eax, 9
5954 pathoswith 323
        jmp     .1
6297 pathoswith 324
 
5954 pathoswith 325
@@:
2288 clevermous 326
        neg     eax
327
        mov     ecx, eax
328
        mov     eax, 1
329
        shl     eax, cl
5954 pathoswith 330
.1:
3742 clevermous 331
        mov     [ebp+NTFS.frs_size], eax
5954 pathoswith 332
        stdcall kernel_alloc, eax
2288 clevermous 333
        test    eax, eax
3742 clevermous 334
        jz      .fail_free
335
        mov     [ebp+NTFS.frs_buffer], eax
2288 clevermous 336
; read $MFT disposition
3742 clevermous 337
        mov     eax, [ebp+NTFS.mft_cluster]
338
        mul     [ebp+NTFS.sectors_per_cluster]
6405 pathoswith 339
        mov     ecx, [ebp+NTFS.frs_size]
340
        shr     ecx, 9
341
        mov     ebx, [ebp+NTFS.frs_buffer]
342
        call    fs_read64_sys
3742 clevermous 343
        test    eax, eax
2288 clevermous 344
        jnz     .usemirr
345
        cmp     dword [ebx], 'FILE'
346
        jnz     .usemirr
347
        call    ntfs_restore_usa_frs
348
        jnc     .mftok
349
.usemirr:
3742 clevermous 350
        mov     eax, [ebp+NTFS.mftmirr_cluster]
351
        mul     [ebp+NTFS.sectors_per_cluster]
6405 pathoswith 352
        mov     ecx, [ebp+NTFS.frs_size]
353
        shr     ecx, 9
354
        mov     ebx, [ebp+NTFS.frs_buffer]
355
        call    fs_read64_sys
3742 clevermous 356
        test    eax, eax
5954 pathoswith 357
        jnz     .fail_free_frs
2288 clevermous 358
        cmp     dword [ebx], 'FILE'
5954 pathoswith 359
        jnz     .fail_free_frs
2288 clevermous 360
        call    ntfs_restore_usa_frs
5954 pathoswith 361
        jc      .fail_free_frs
6405 pathoswith 362
.mftok:     ; prepare $MFT retrieval information
363
; search for unnamed non-resident $DATA attribute
364
        movzx   eax, word [ebx+attributeOffset]
2288 clevermous 365
        add     eax, ebx
366
.scandata:
367
        cmp     dword [eax], -1
6405 pathoswith 368
        jz      .fail_free_frs
2288 clevermous 369
        cmp     dword [eax], 0x80
370
        jnz     @f
6405 pathoswith 371
        cmp     byte [eax+nameLength], 0
2288 clevermous 372
        jz      .founddata
373
@@:
6405 pathoswith 374
        add     eax, [eax+sizeWithHeader]
2288 clevermous 375
        jmp     .scandata
6297 pathoswith 376
 
2288 clevermous 377
.founddata:
6405 pathoswith 378
        cmp     byte [eax+nonResidentFlag], 0
379
        jz      .fail_free_frs
380
        movzx   esi, word [eax+dataRunsOffset]
2288 clevermous 381
        add     esi, eax
6405 pathoswith 382
        mov     edx, [eax+attributeAllocatedSize+4]
383
        mov     eax, [eax+attributeAllocatedSize]
384
        shrd    eax, edx, 9
385
        mov     [ebp+NTFS.mftSize], eax
2288 clevermous 386
        sub     esp, 10h
6405 pathoswith 387
        lea     ecx, [ebp+NTFS.mft_retrieval]
388
        xor     edx, edx
389
.scanmcb:   ; load descriptions of fragments
2288 clevermous 390
        call    ntfs_decode_mcb_entry
391
        jnc     .scanmcbend
6405 pathoswith 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
2288 clevermous 397
        jmp     .scanmcb
6297 pathoswith 398
 
2288 clevermous 399
.scanmcbend:
400
        add     esp, 10h
6405 pathoswith 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
6340 pathoswith 408
        stdcall kernel_alloc, 2000h
2288 clevermous 409
        test    eax, eax
6405 pathoswith 410
        jz      .fail_free_frs
3742 clevermous 411
        mov     [ebp+NTFS.cur_index_buf], eax
6340 pathoswith 412
        add     eax, 1000h
413
        mov     [ebp+NTFS.secondIndexBuffer], eax
414
        mov     [ebp+NTFS.cur_index_size], 8
5954 pathoswith 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
6273 pathoswith 428
        mov     [ebp+NTFS.cur_buf], eax
5954 pathoswith 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
6273 pathoswith 440
        mov     [ebp+NTFS.cur_size], eax
5954 pathoswith 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
6273 pathoswith 448
        mov     [ebp+NTFS.cur_iRecord], 6
449
        mov     [ebp+NTFS.cur_attr], 0x80
450
        mov     [ebp+NTFS.cur_offs], 0
5954 pathoswith 451
        call    ntfs_read_attr
452
        jc      .failFreeBitmap
6273 pathoswith 453
        mov     eax, [ebp+NTFS.cur_read]
5954 pathoswith 454
        mov     [ebp+NTFS.BitmapSize], eax
6273 pathoswith 455
        mov     eax, [ebp+NTFS.LastRead]
5954 pathoswith 456
        mov     [ebp+NTFS.BitmapLocation], eax
457
; read MFT $BITMAP attribute
458
        mov     eax, [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 459
        mov     [ebp+NTFS.cur_size], eax
5954 pathoswith 460
        shl     eax, 9
461
        stdcall kernel_alloc, eax
462
        test    eax, eax
463
        jz      .failFreeBitmap
464
        mov     [ebp+NTFS.mftBitmapBuffer], eax
6273 pathoswith 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
5954 pathoswith 469
        call    ntfs_read_attr
6273 pathoswith 470
        mov     eax, [ebp+NTFS.cur_read]
5954 pathoswith 471
        cmp     eax, 4
472
        jc      .failFreeBitmapMFT
6273 pathoswith 473
        mov     ecx, [ebp+NTFS.attr_offs]
6107 pathoswith 474
        cmp     byte [ecx+nonResidentFlag], 1
475
        jnz     .failFreeBitmapMFT
5954 pathoswith 476
        mov     [ebp+NTFS.mftBitmapSize], eax
6273 pathoswith 477
        mov     eax, [ebp+NTFS.LastRead]
5954 pathoswith 478
        mov     [ebp+NTFS.mftBitmapLocation], eax
2288 clevermous 479
 
3742 clevermous 480
        mov     eax, ebp
5954 pathoswith 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:
6405 pathoswith 491
        stdcall kernel_free, [ebp+NTFS.mftBitmapBuffer]
5954 pathoswith 492
.failFreeBitmap:
6405 pathoswith 493
        stdcall kernel_free, [ebp+NTFS.BitmapBuffer]
5954 pathoswith 494
.failFreeIndex:
6405 pathoswith 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
5954 pathoswith 501
.fail_free_frs:
502
        stdcall kernel_free, [ebp+NTFS.frs_buffer]
503
.fail_free:
6405 pathoswith 504
        stdcall kernel_free, ebp
5954 pathoswith 505
        xor     eax, eax
3742 clevermous 506
        jmp     .pop_exit
2288 clevermous 507
 
5954 pathoswith 508
ntfs_free:
3742 clevermous 509
        push    ebx
5954 pathoswith 510
        mov     ebx, eax
3742 clevermous 511
        stdcall kernel_free, [ebx+NTFS.frs_buffer]
5954 pathoswith 512
        stdcall kernel_free, [ebx+NTFS.mftBitmapBuffer]
513
        stdcall kernel_free, [ebx+NTFS.BitmapBuffer]
6405 pathoswith 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
3742 clevermous 521
        pop     ebx
6405 pathoswith 522
        ret
3742 clevermous 523
 
5954 pathoswith 524
ntfs_lock:
3742 clevermous 525
        lea     ecx, [ebp+NTFS.Lock]
526
        jmp     mutex_lock
527
 
5954 pathoswith 528
ntfs_unlock:
3742 clevermous 529
        lea     ecx, [ebp+NTFS.Lock]
530
        jmp     mutex_unlock
531
 
2288 clevermous 532
ntfs_read_attr:
6273 pathoswith 533
; [ebp+NTFS.bWriteAttr]=1 -> write attribute
5954 pathoswith 534
;   in:
6273 pathoswith 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
5954 pathoswith 540
;   out:
6273 pathoswith 541
; [ebp+NTFS.cur_read] = bytes readen
5954 pathoswith 542
; CF=1 -> failed, eax = disk error code, eax=0 -> something with FS
3742 clevermous 543
        xor     eax, eax
2288 clevermous 544
        pushad
6273 pathoswith 545
        and     [ebp+NTFS.cur_read], 0
546
        cmp     [ebp+NTFS.cur_iRecord], 0
2288 clevermous 547
        jnz     .nomft
6273 pathoswith 548
        cmp     [ebp+NTFS.cur_attr], 0x80
2288 clevermous 549
        jnz     .nomft
550
; precalculated part of $Mft $DATA
6273 pathoswith 551
        mov     eax, [ebp+NTFS.cur_offs]
2288 clevermous 552
        xor     edx, edx
3742 clevermous 553
        div     [ebp+NTFS.sectors_per_cluster]
6405 pathoswith 554
        mov     ebx, edx
6418 pathoswith 555
        mov     [ebp+NTFS.fragmentCount], 0
6405 pathoswith 556
; eax = VCN, ebx = offset in sectors from beginning of cluster
557
        lea     esi, [ebp+NTFS.mft_retrieval]
558
        sub     esi, 8
2288 clevermous 559
.mftscan:
6405 pathoswith 560
        add     esi, 8
561
        cmp     esi, [ebp+NTFS.mft_retrieval_end]
562
        jz      .nomft
563
        mov     ecx, [esi+4]
2288 clevermous 564
        sub     eax, [esi]
6405 pathoswith 565
        jnc     .mftscan
2288 clevermous 566
        add     ecx, eax
567
        add     ecx, [esi]
6405 pathoswith 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
6273 pathoswith 574
        mov     ebx, [ebp+NTFS.cur_buf]
575
        cmp     ecx, [ebp+NTFS.cur_size]
2288 clevermous 576
        jb      @f
6273 pathoswith 577
        mov     ecx, [ebp+NTFS.cur_size]
2288 clevermous 578
@@:
6405 pathoswith 579
        mov     [ebp+NTFS.LastRead], eax
580
        mov     edi, ecx
581
        call    fs_read64_sys
3742 clevermous 582
        test    eax, eax
6405 pathoswith 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
6418 pathoswith 589
        inc     [ebp+NTFS.fragmentCount]
2288 clevermous 590
        xor     eax, eax
6405 pathoswith 591
        xor     ebx, ebx
6273 pathoswith 592
        cmp     [ebp+NTFS.cur_size], eax
2288 clevermous 593
        jz      @f
594
        jmp     .mftscan
6297 pathoswith 595
 
596
.errret2_pop:
597
        xor     eax, eax
598
.errret_pop:
599
        pop     ecx
2288 clevermous 600
        pop     ecx
601
.errret:
3742 clevermous 602
        mov     [esp+28], eax
2288 clevermous 603
        stc
6297 pathoswith 604
@@:
2288 clevermous 605
        popad
606
        ret
6297 pathoswith 607
 
2288 clevermous 608
.nomft:
609
; 1. Read file record.
610
; N.B. This will do recursive call of read_attr for $MFT::$Data.
6273 pathoswith 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
2288 clevermous 615
        call    ntfs_read_file_record
3742 clevermous 616
        jc      .errret
2288 clevermous 617
; 2. Find required attribute.
3742 clevermous 618
        mov     eax, [ebp+NTFS.frs_buffer]
6107 pathoswith 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.
6297 pathoswith 622
        cmp     word [eax+baseRecordReuse], 0
2288 clevermous 623
        jz      @f
6297 pathoswith 624
        mov     eax, [eax+baseRecordReference]
2288 clevermous 625
.beginfindattr:
626
        call    ntfs_read_file_record
3742 clevermous 627
        jc      .errret
6107 pathoswith 628
        jmp     @f
6297 pathoswith 629
 
6107 pathoswith 630
.newAttribute:
631
        pushad
6297 pathoswith 632
        and     [ebp+NTFS.cur_read], 0
2288 clevermous 633
@@:
634
; b) Scan for required attribute and for $ATTR_LIST
3742 clevermous 635
        mov     eax, [ebp+NTFS.frs_buffer]
6297 pathoswith 636
        movzx   ecx, word [eax+attributeOffset]
2288 clevermous 637
        add     eax, ecx
6273 pathoswith 638
        mov     ecx, [ebp+NTFS.cur_attr]
639
        and     [ebp+NTFS.attr_offs], 0
2288 clevermous 640
.scanattr:
641
        cmp     dword [eax], -1
642
        jz      .scandone
643
        cmp     dword [eax], ecx
644
        jz      .okattr
6273 pathoswith 645
        cmp     [ebp+NTFS.attr_iBaseRecord], -1
2288 clevermous 646
        jnz     .scancont
647
        cmp     dword [eax], 0x20       ; $ATTR_LIST
648
        jnz     .scancont
6273 pathoswith 649
        mov     [ebp+NTFS.attr_list], eax
2288 clevermous 650
        jmp     .scancont
6297 pathoswith 651
 
2288 clevermous 652
.okattr:
653
; ignore named $DATA attributes (aka NTFS streams)
654
        cmp     ecx, 0x80
655
        jnz     @f
6297 pathoswith 656
        cmp     byte [eax+nameLength], 0
2288 clevermous 657
        jnz     .scancont
658
@@:
6273 pathoswith 659
        mov     [ebp+NTFS.attr_offs], eax
2288 clevermous 660
.scancont:
6297 pathoswith 661
        add     eax, [eax+sizeWithHeader]
2288 clevermous 662
        jmp     .scanattr
6297 pathoswith 663
 
2288 clevermous 664
.continue:
665
        pushad
6273 pathoswith 666
        and     [ebp+NTFS.cur_read], 0
2288 clevermous 667
.scandone:
668
; c) Check for required offset and length
6273 pathoswith 669
        mov     ecx, [ebp+NTFS.attr_offs]
2288 clevermous 670
        jecxz   .noattr
6273 pathoswith 671
        push    [ebp+NTFS.cur_size]
672
        push    [ebp+NTFS.cur_read]
2288 clevermous 673
        call    .doreadattr
674
        pop     edx
3742 clevermous 675
        pop     ecx
6297 pathoswith 676
        jc      .ret
6273 pathoswith 677
        cmp     [ebp+NTFS.bCanContinue], 0
6297 pathoswith 678
        jz      .ret
6273 pathoswith 679
        sub     edx, [ebp+NTFS.cur_read]
2288 clevermous 680
        neg     edx
681
        shr     edx, 9
3742 clevermous 682
        sub     ecx, edx
6273 pathoswith 683
        mov     [ebp+NTFS.cur_size], ecx
6297 pathoswith 684
        jz      .ret
2288 clevermous 685
.noattr:
6273 pathoswith 686
        cmp     [ebp+NTFS.cur_attr], 0x20
2288 clevermous 687
        jz      @f
6273 pathoswith 688
        mov     ecx, [ebp+NTFS.attr_list]
2288 clevermous 689
        test    ecx, ecx
690
        jnz     .lookattr
3742 clevermous 691
        and     dword [esp+28], 0
6297 pathoswith 692
        cmp     [ebp+NTFS.attr_offs], 1     ; define CF
693
.ret:
2288 clevermous 694
        popad
695
        ret
6297 pathoswith 696
 
2288 clevermous 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
6273 pathoswith 701
        mov     eax, [ebp+NTFS.attr_iBaseRecord]
2288 clevermous 702
        cmp     eax, -1
703
        jz      @f
704
        call    ntfs_read_file_record
3742 clevermous 705
        jc      .errret
6273 pathoswith 706
        or      [ebp+NTFS.attr_iBaseRecord], -1
2288 clevermous 707
@@:
6273 pathoswith 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
2288 clevermous 720
        jnz     @f
6273 pathoswith 721
        lea     eax, [ebp+NTFS.attrlist_mft_buf]
2288 clevermous 722
@@:
6273 pathoswith 723
        mov     [ebp+NTFS.cur_buf], eax
2288 clevermous 724
        push    eax
725
        call    .doreadattr
726
        pop     esi
727
        mov     edx, 1
6273 pathoswith 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]
2288 clevermous 735
        jc      .errret
736
        or      edi, -1
3742 clevermous 737
        lea     ecx, [ecx+esi-1Ah]
2288 clevermous 738
.scanliststart:
3742 clevermous 739
        push    ecx
6273 pathoswith 740
        mov     eax, [ebp+NTFS.cur_attr]
2288 clevermous 741
.scanlist:
3742 clevermous 742
        cmp     esi, [esp]
2288 clevermous 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
6297 pathoswith 750
 
2288 clevermous 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
6407 pathoswith 762
        cmp     dword [ebp+NTFS.attr_size+4], -1
2288 clevermous 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
6297 pathoswith 767
        jc      .errret_pop
3742 clevermous 768
        mov     eax, [ebp+NTFS.frs_buffer]
2288 clevermous 769
        movzx   ecx, word [eax+14h]
770
        add     eax, ecx
6273 pathoswith 771
        mov     ecx, [ebp+NTFS.cur_attr]
2288 clevermous 772
@@:
773
        cmp     dword [eax], -1
3742 clevermous 774
        jz      .errret2_pop
2288 clevermous 775
        cmp     dword [eax], ecx
776
        jz      @f
777
.l1:
778
        add     eax, [eax+4]
779
        jmp     @b
6297 pathoswith 780
 
2288 clevermous 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]
6273 pathoswith 790
        mov     dword [ebp+NTFS.attr_size], eax
791
        and     dword [ebp+NTFS.attr_size+4], 0
2288 clevermous 792
        jmp     .testfz
6297 pathoswith 793
 
2288 clevermous 794
.sdnores:
795
        mov     ecx, [eax+30h]
6273 pathoswith 796
        mov     dword [ebp+NTFS.attr_size], ecx
2288 clevermous 797
        mov     ecx, [eax+34h]
6273 pathoswith 798
        mov     dword [ebp+NTFS.attr_size+4], ecx
2288 clevermous 799
.testfz:
800
        xor     eax, eax
801
.testf:
3742 clevermous 802
        imul    eax, [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 803
        cmp     eax, [ebp+NTFS.cur_offs]
2288 clevermous 804
        pop     eax
805
        ja      @f
806
        mov     edi, [esi+10h]  ; keep previous iRecord
807
        jmp     .scanlistcont
6297 pathoswith 808
 
2288 clevermous 809
@@:
3742 clevermous 810
        pop     ecx
2288 clevermous 811
.scanlistfound:
812
        cmp     edi, -1
6297 pathoswith 813
        jz      .ret
6273 pathoswith 814
        mov     eax, [ebp+NTFS.cur_iRecord]
815
        mov     [ebp+NTFS.attr_iBaseRecord], eax
2288 clevermous 816
        mov     eax, edi
817
        jmp     .beginfindattr
6297 pathoswith 818
 
2288 clevermous 819
.scanlistdone:
3742 clevermous 820
        pop     ecx
821
        sub     ecx, ebp
6273 pathoswith 822
        sub     ecx, NTFS.attrlist_buf-1Ah
823
        cmp     [ebp+NTFS.cur_iRecord], 0
2288 clevermous 824
        jnz     @f
6273 pathoswith 825
        sub     ecx, NTFS.attrlist_mft_buf-NTFS.attrlist_buf
2288 clevermous 826
@@:
3742 clevermous 827
        cmp     ecx, 0x400
2288 clevermous 828
        jnz     .scanlistfound
829
        inc     edx
830
        push    esi edi
6273 pathoswith 831
        lea     esi, [ebp+NTFS.attrlist_buf+0x200]
832
        lea     edi, [ebp+NTFS.attrlist_buf]
833
        cmp     [ebp+NTFS.cur_iRecord], 0
2288 clevermous 834
        jnz     @f
6273 pathoswith 835
        lea     esi, [ebp+NTFS.attrlist_mft_buf+0x200]
836
        lea     edi, [ebp+NTFS.attrlist_mft_buf]
2288 clevermous 837
@@:
838
        mov     ecx, 0x200/4
839
        rep movsd
840
        mov     eax, edi
841
        pop     edi esi
842
        sub     esi, 0x200
6273 pathoswith 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]
3742 clevermous 855
        push    esi edx edi
2288 clevermous 856
        call    .doreadattr
3742 clevermous 857
        pop     edi edx esi
6273 pathoswith 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]
2288 clevermous 865
        jc      .errret
6273 pathoswith 866
        lea     ecx, [ecx+ebp+NTFS.attrlist_buf+0x200-0x1A]
867
        cmp     [ebp+NTFS.cur_iRecord], 0
2288 clevermous 868
        jnz     .scanliststart
6273 pathoswith 869
        add     ecx, NTFS.attrlist_mft_buf-NTFS.attrlist_buf
2288 clevermous 870
        jmp     .scanliststart
871
 
872
.doreadattr:
6273 pathoswith 873
        mov     [ebp+NTFS.bCanContinue], 0
6297 pathoswith 874
        cmp     byte [ecx+nonResidentFlag], 0
2288 clevermous 875
        jnz     .nonresident
6297 pathoswith 876
        mov     eax, [ecx+sizeWithoutHeader]
2288 clevermous 877
        mov     esi, eax
6273 pathoswith 878
        mov     edx, [ebp+NTFS.cur_offs]
2288 clevermous 879
        shr     eax, 9
880
        cmp     eax, edx
881
        jb      .okret
882
        shl     edx, 9
883
        sub     esi, edx
6297 pathoswith 884
        movzx   eax, word [ecx+attributeOffset]
2288 clevermous 885
        add     edx, eax
886
        add     edx, ecx        ; edx -> data
6273 pathoswith 887
        mov     eax, [ebp+NTFS.cur_size]
2288 clevermous 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
6273 pathoswith 898
        mov     [ebp+NTFS.cur_read], eax
2288 clevermous 899
        mov     ecx, eax
900
        mov     eax, edx
6273 pathoswith 901
        mov     ebx, [ebp+NTFS.cur_buf]
2288 clevermous 902
        call    memmove
6273 pathoswith 903
        and     [ebp+NTFS.cur_size], 0      ; CF=0
2288 clevermous 904
        ret
6297 pathoswith 905
 
2288 clevermous 906
.nonresident:
907
; Not all auxiliary records contain correct FileSize info
6273 pathoswith 908
        mov     eax, dword [ebp+NTFS.attr_size]
909
        mov     edx, dword [ebp+NTFS.attr_size+4]
6407 pathoswith 910
        cmp     edx, -1
2288 clevermous 911
        jnz     @f
6297 pathoswith 912
        mov     eax, [ecx+attributeRealSize]
913
        mov     edx, [ecx+attributeRealSize+4]
6273 pathoswith 914
        mov     dword [ebp+NTFS.attr_size], eax
915
        mov     dword [ebp+NTFS.attr_size+4], edx
2288 clevermous 916
@@:
917
        add     eax, 0x1FF
918
        adc     edx, 0
919
        shrd    eax, edx, 9
6273 pathoswith 920
        sub     eax, [ebp+NTFS.cur_offs]
2288 clevermous 921
        ja      @f
922
; return with nothing read
6273 pathoswith 923
        and     [ebp+NTFS.cur_size], 0
2288 clevermous 924
.okret:
925
        clc
926
        ret
6297 pathoswith 927
 
2288 clevermous 928
@@:
929
; reduce read length
6273 pathoswith 930
        and     [ebp+NTFS.cur_tail], 0
931
        cmp     [ebp+NTFS.cur_size], eax
2288 clevermous 932
        jb      @f
6273 pathoswith 933
        mov     [ebp+NTFS.cur_size], eax
934
        mov     eax, dword [ebp+NTFS.attr_size]
2288 clevermous 935
        and     eax, 0x1FF
6273 pathoswith 936
        mov     [ebp+NTFS.cur_tail], eax
2288 clevermous 937
@@:
6273 pathoswith 938
        mov     eax, [ebp+NTFS.cur_offs]
2288 clevermous 939
        xor     edx, edx
3742 clevermous 940
        div     [ebp+NTFS.sectors_per_cluster]
6297 pathoswith 941
        sub     eax, [ecx+firstVCN]
2288 clevermous 942
        jb      .okret
6405 pathoswith 943
        mov     ebx, edx
944
; eax = starting cluster, ebx = sector in the cluster
6273 pathoswith 945
        cmp     [ebp+NTFS.cur_attr], 0x80
5994 pathoswith 946
        jnz     .sys
6273 pathoswith 947
        cmp     [ebp+NTFS.cur_iRecord], 0
5994 pathoswith 948
        jz      .sys
949
        push    fs_read64_app
6273 pathoswith 950
        cmp     [ebp+NTFS.bWriteAttr], 1
5994 pathoswith 951
        jnz     @f
952
        mov     dword[esp], fs_write64_app
953
        jmp     @f
6297 pathoswith 954
 
5994 pathoswith 955
.sys:
956
        push    fs_read64_sys
957
@@:
2288 clevermous 958
        sub     esp, 10h
6297 pathoswith 959
        movzx   esi, word [ecx+dataRunsOffset]
2288 clevermous 960
        add     esi, ecx
3742 clevermous 961
        xor     edi, edi
6273 pathoswith 962
        mov     [ebp+NTFS.fragmentCount], 0
2288 clevermous 963
.readloop:
964
        call    ntfs_decode_mcb_entry
965
        jnc     .break
3742 clevermous 966
        add     edi, [esp+8]
2288 clevermous 967
        sub     eax, [esp]
968
        jae     .readloop
6405 pathoswith 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]
6273 pathoswith 979
        cmp     ecx, [ebp+NTFS.cur_size]
2288 clevermous 980
        jb      @f
6273 pathoswith 981
        mov     ecx, [ebp+NTFS.cur_size]
2288 clevermous 982
@@:
6273 pathoswith 983
        mov     [ebp+NTFS.LastRead], eax
5954 pathoswith 984
        push    ecx
6405 pathoswith 985
        call    dword[esp+14h]
5954 pathoswith 986
        pop     ecx
3742 clevermous 987
        test    eax, eax
2288 clevermous 988
        jnz     .errread2
6273 pathoswith 989
        sub     [ebp+NTFS.cur_size], ecx
990
        add     [ebp+NTFS.cur_offs], ecx
5954 pathoswith 991
        shl     ecx, 9
6273 pathoswith 992
        add     [ebp+NTFS.cur_read], ecx
993
        add     [ebp+NTFS.cur_buf], ecx
994
        inc     [ebp+NTFS.fragmentCount]
2288 clevermous 995
        xor     eax, eax
6405 pathoswith 996
        xor     ebx, ebx
6273 pathoswith 997
        cmp     [ebp+NTFS.cur_size], 0
2288 clevermous 998
        jnz     .readloop
5994 pathoswith 999
        add     esp, 14h
6273 pathoswith 1000
        mov     eax, [ebp+NTFS.cur_tail]
2288 clevermous 1001
        test    eax, eax
1002
        jz      @f
1003
        sub     eax, 0x200
6273 pathoswith 1004
        add     [ebp+NTFS.cur_read], eax
2288 clevermous 1005
@@:
1006
        clc
1007
        ret
6297 pathoswith 1008
 
2288 clevermous 1009
.errread2:
5994 pathoswith 1010
        add     esp, 14h
2288 clevermous 1011
        stc
1012
        ret
6297 pathoswith 1013
 
2288 clevermous 1014
.break:
5994 pathoswith 1015
        add     esp, 14h        ; CF=0
6273 pathoswith 1016
        mov     [ebp+NTFS.bCanContinue], 1
2288 clevermous 1017
        ret
1018
 
1019
ntfs_read_file_record:
5954 pathoswith 1020
; in: eax = iRecord
6080 pathoswith 1021
; out: [ebp+NTFS.frs_buffer] -> file record
5954 pathoswith 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]
2288 clevermous 1024
        push    ecx edx
3742 clevermous 1025
        mov     ecx, [ebp+NTFS.frs_size]
2288 clevermous 1026
        mul     ecx
1027
        shrd    eax, edx, 9
1028
        shr     edx, 9
3742 clevermous 1029
        jnz     .errret
6273 pathoswith 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
2288 clevermous 1044
        shr     ecx, 9
6273 pathoswith 1045
        mov     [ebp+NTFS.cur_size], ecx
3742 clevermous 1046
        mov     eax, [ebp+NTFS.frs_buffer]
6273 pathoswith 1047
        mov     [ebp+NTFS.cur_buf], eax
2288 clevermous 1048
        call    ntfs_read_attr
6273 pathoswith 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]
3742 clevermous 1061
        jc      .ret
1062
        cmp     edx, [ebp+NTFS.frs_size]
2288 clevermous 1063
        jnz     .errret
6408 pathoswith 1064
        mov     eax, [ebp+NTFS.LastRead]
1065
        mov     [ebp+NTFS.mftLastRead], eax
3742 clevermous 1066
        mov     eax, [ebp+NTFS.frs_buffer]
2288 clevermous 1067
        cmp     dword [eax], 'FILE'
1068
        jnz     .errret
1069
        push    ebx
1070
        mov     ebx, eax
1071
        call    ntfs_restore_usa_frs
1072
        pop     ebx
3742 clevermous 1073
        jc      .errret
2288 clevermous 1074
.ret:
3742 clevermous 1075
        pop     edx ecx
2288 clevermous 1076
        ret
6297 pathoswith 1077
 
3742 clevermous 1078
.errret:
2288 clevermous 1079
        pop     edx ecx
1080
        xor     eax, eax
3742 clevermous 1081
        stc
2288 clevermous 1082
        ret
1083
 
1084
ntfs_restore_usa_frs:
3742 clevermous 1085
        mov     eax, [ebp+NTFS.frs_size]
2288 clevermous 1086
ntfs_restore_usa:
6080 pathoswith 1087
;   in:
1088
; ebx -> record
1089
; eax = size in bytes
2288 clevermous 1090
        pushad
1091
        shr     eax, 9
1092
        mov     ecx, eax
1093
        inc     eax
6297 pathoswith 1094
        cmp     [ebx+updateSequenceSize], ax
2288 clevermous 1095
        jnz     .err
6297 pathoswith 1096
        movzx   eax, word [ebx+updateSequenceOffset]
2288 clevermous 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
6297 pathoswith 1111
 
2288 clevermous 1112
.err:
1113
        popad
1114
        stc
1115
        ret
1116
 
1117
ntfs_decode_mcb_entry:
6019 pathoswith 1118
;   in:
6080 pathoswith 1119
; esi -> MCB entry
6019 pathoswith 1120
; esp -> buffer (16 bytes)
1121
;   out:
6080 pathoswith 1122
; esi -> next MCB entry
6019 pathoswith 1123
; esp -> data run size
1124
; esp+8 -> cluster (delta)
6080 pathoswith 1125
; CF=0 -> MCB end
2288 clevermous 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:
6471 pathoswith 1165
; in: esi -> path string in UTF-8
5954 pathoswith 1166
;   out:
6405 pathoswith 1167
; [ebp+NTFS.cur_iRecord] = target fileRecord
6411 pathoswith 1168
; eax -> target index in the node
1169
; [ebp+NTFS.LastRead] = target node location
6405 pathoswith 1170
; [ebp+NTFS.indexPointer] -> index, that points the target subnode
6411 pathoswith 1171
; [ebp+NTFS.nodeLastRead] = branch node location
6405 pathoswith 1172
; [ebp+NTFS.indexRoot] -> attribute
6411 pathoswith 1173
; [ebp+NTFS.rootLastRead] = directory fileRecord location
6405 pathoswith 1174
; [ebp+NTFS.cur_size] = index record size in sectors
1175
; [ebp+NTFS.cur_subnode_size] = index record size in clusters or sectors
5994 pathoswith 1176
; CF=1 -> file not found, eax=0 -> error
6297 pathoswith 1177
        mov     [ebp+NTFS.cur_iRecord], 5   ; start from root directory
2288 clevermous 1178
.doit2:
6273 pathoswith 1179
        mov     [ebp+NTFS.cur_attr], 0x90   ; $INDEX_ROOT
1180
        and     [ebp+NTFS.cur_offs], 0
3742 clevermous 1181
        mov     eax, [ebp+NTFS.cur_index_size]
6273 pathoswith 1182
        mov     [ebp+NTFS.cur_size], eax
3742 clevermous 1183
        mov     eax, [ebp+NTFS.cur_index_buf]
6273 pathoswith 1184
        mov     [ebp+NTFS.cur_buf], eax
2288 clevermous 1185
        call    ntfs_read_attr
5994 pathoswith 1186
        mov     eax, 0
6297 pathoswith 1187
        jc      .ret
6273 pathoswith 1188
        cmp     [ebp+NTFS.cur_read], 0x20
2288 clevermous 1189
        jc      .ret
6418 pathoswith 1190
        push    esi
2288 clevermous 1191
        pushad
3742 clevermous 1192
        mov     esi, [ebp+NTFS.cur_index_buf]
6340 pathoswith 1193
        mov     eax, [esi+indexRecordSize]
1194
        shr     eax, 9
1195
        cmp     [ebp+NTFS.cur_index_size], eax
6297 pathoswith 1196
        jc      .realloc
6340 pathoswith 1197
        mov     [ebp+NTFS.cur_size], eax
1198
        mov     al, [esi+indexRecordSizeClus]
1199
        mov     [ebp+NTFS.cur_subnode_size], eax
6297 pathoswith 1200
        add     esi, rootNode
1201
        mov     eax, [esi+nodeRealSize]
1202
        add     eax, rootNode
6273 pathoswith 1203
        cmp     [ebp+NTFS.cur_read], eax
6297 pathoswith 1204
        jc      .err
6408 pathoswith 1205
        mov     eax, [ebp+NTFS.mftLastRead]
6340 pathoswith 1206
        mov     [ebp+NTFS.rootLastRead], eax
1207
        mov     eax, [ebp+NTFS.attr_offs]
1208
        mov     [ebp+NTFS.indexRoot], eax
6471 pathoswith 1209
.scanloop:  ; esi -> current index node
6297 pathoswith 1210
        add     esi, [esi+indexOffset]
2288 clevermous 1211
.scanloopint:
6471 pathoswith 1212
        push    esi
6297 pathoswith 1213
        test    byte [esi+indexFlags], 2
2288 clevermous 1214
        jnz     .subnode
6297 pathoswith 1215
        movzx   ecx, byte [esi+fileNameLength]
6471 pathoswith 1216
        lea     edi, [esi+fileName]
1217
        mov     esi, [esp+8]
2288 clevermous 1218
@@:
6471 pathoswith 1219
        call    utf8to16
1220
        cmp     ax, '/'
1221
        jz      .subnode
1222
        call    utf16toUpper
2288 clevermous 1223
        push    eax
6471 pathoswith 1224
        mov     ax, [edi]
1225
        call    utf16toUpper
1226
        cmp     [esp], ax
2288 clevermous 1227
        pop     eax
6471 pathoswith 1228
        jc      .subnode
1229
        jnz     .scanloopcont
1230
        add     edi, 2
1231
        loop    @b
1232
        call    utf8to16
1233
        cmp     ax, '/'
2288 clevermous 1234
        jz      .found
6471 pathoswith 1235
        test    ax, ax
1236
        jz      .found
1237
.scanloopcont:
2288 clevermous 1238
        pop     esi
6297 pathoswith 1239
        movzx   eax, word [esi+indexAllocatedSize]
2288 clevermous 1240
        add     esi, eax
1241
        jmp     .scanloopint
6297 pathoswith 1242
 
1243
.realloc:
6340 pathoswith 1244
        mov     edi, eax
1245
        mov     eax, [esi+indexRecordSize]
1246
        shl     eax, 1
1247
        stdcall kernel_alloc, eax
6297 pathoswith 1248
        test    eax, eax
1249
        jz      .err
6405 pathoswith 1250
        mov     edx, [ebp+NTFS.cur_index_buf]
1251
        cmp     edx, [ebp+NTFS.secondIndexBuffer]
1252
        jc      @f
1253
        mov     edx, [ebp+NTFS.secondIndexBuffer]
1254
@@:
6297 pathoswith 1255
        mov     [ebp+NTFS.cur_index_buf], eax
6340 pathoswith 1256
        add     eax, [esi+indexRecordSize]
1257
        mov     [ebp+NTFS.secondIndexBuffer], eax
1258
        mov     [ebp+NTFS.cur_index_size], edi
6405 pathoswith 1259
        stdcall kernel_free, edx
6297 pathoswith 1260
        popad
6418 pathoswith 1261
        pop     eax
6297 pathoswith 1262
        jmp     .doit2
1263
 
1264
.notfound:
6471 pathoswith 1265
        mov     [esp+28], esi
6297 pathoswith 1266
.err:
1267
        popad
1268
        stc
6418 pathoswith 1269
.ret2:
1270
        pop     esi
6297 pathoswith 1271
.ret:
6468 pathoswith 1272
        ret
6297 pathoswith 1273
 
6471 pathoswith 1274
.subnode:
2288 clevermous 1275
        pop     esi
6297 pathoswith 1276
        test    byte [esi+indexFlags], 1
2288 clevermous 1277
        jz      .notfound
6411 pathoswith 1278
        mov     eax, [ebp+NTFS.LastRead]
1279
        mov     [ebp+NTFS.nodeLastRead], eax
1280
        mov     [ebp+NTFS.indexPointer], esi
6297 pathoswith 1281
        movzx   eax, word [esi+indexAllocatedSize]
2288 clevermous 1282
        mov     eax, [esi+eax-8]
6340 pathoswith 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
6273 pathoswith 1294
        mov     [ebp+NTFS.cur_offs], eax
6297 pathoswith 1295
        call    ntfs_read_attr.newAttribute
6340 pathoswith 1296
        pop     eax
1297
        mov     [ebp+NTFS.cur_size], eax
2288 clevermous 1298
        shl     eax, 9
6273 pathoswith 1299
        cmp     [ebp+NTFS.cur_read], eax
5954 pathoswith 1300
        jnz     .err
2288 clevermous 1301
        cmp     dword [esi], 'INDX'
5954 pathoswith 1302
        jnz     .err
2288 clevermous 1303
        mov     ebx, esi
1304
        call    ntfs_restore_usa
5954 pathoswith 1305
        jc      .err
6297 pathoswith 1306
        add     esi, recordNode
2288 clevermous 1307
        jmp     .scanloop
6297 pathoswith 1308
 
2288 clevermous 1309
.found:
6471 pathoswith 1310
        mov     [esp+8], esi
1311
        pop     eax
1312
        mov     [esp+28], eax
1313
        mov     eax, [eax+fileRecordReference]
6273 pathoswith 1314
        mov     [ebp+NTFS.cur_iRecord], eax
2288 clevermous 1315
        popad
6471 pathoswith 1316
        cmp     byte [esi-1], 0
6468 pathoswith 1317
        jz      .ret2
6418 pathoswith 1318
        pop     eax
2288 clevermous 1319
        jmp     .doit2
1320
 
1321
;----------------------------------------------------------------
5954 pathoswith 1322
ntfs_ReadFile:
2288 clevermous 1323
        cmp     byte [esi], 0
1324
        jnz     @f
1325
        or      ebx, -1
3598 clevermous 1326
        movi    eax, ERROR_ACCESS_DENIED
2288 clevermous 1327
        ret
6297 pathoswith 1328
 
2288 clevermous 1329
@@:
3742 clevermous 1330
        call    ntfs_lock
6468 pathoswith 1331
        call    ntfs_find_lfn
2288 clevermous 1332
        jnc     .found
3742 clevermous 1333
        call    ntfs_unlock
2288 clevermous 1334
        or      ebx, -1
3598 clevermous 1335
        movi    eax, ERROR_FILE_NOT_FOUND
2288 clevermous 1336
        ret
6297 pathoswith 1337
 
2288 clevermous 1338
.found:
6273 pathoswith 1339
        mov     [ebp+NTFS.cur_attr], 0x80   ; $DATA
1340
        and     [ebp+NTFS.cur_offs], 0
1341
        and     [ebp+NTFS.cur_size], 0
2288 clevermous 1342
        call    ntfs_read_attr
1343
        jnc     @f
3742 clevermous 1344
        call    ntfs_unlock
2288 clevermous 1345
        or      ebx, -1
3598 clevermous 1346
        movi    eax, ERROR_ACCESS_DENIED
2288 clevermous 1347
        ret
6297 pathoswith 1348
 
2288 clevermous 1349
@@:
1350
        pushad
1351
        and     dword [esp+10h], 0
1352
        xor     eax, eax
3742 clevermous 1353
        cmp     dword [ebx+8], 0x200
2288 clevermous 1354
        jb      @f
1355
.eof0:
1356
        popad
1357
        xor     ebx, ebx
1358
.eof:
3742 clevermous 1359
        call    ntfs_unlock
6297 pathoswith 1360
        movi    eax, ERROR_END_OF_FILE
2288 clevermous 1361
        ret
6297 pathoswith 1362
 
2288 clevermous 1363
@@:
3742 clevermous 1364
        mov     ecx, [ebx+12]
1365
        mov     edx, [ebx+16]
1366
        mov     eax, [ebx+4]
2288 clevermous 1367
        test    eax, 0x1FF
1368
        jz      .alignedstart
1369
        push    edx
3742 clevermous 1370
        mov     edx, [ebx+8]
2288 clevermous 1371
        shrd    eax, edx, 9
1400 turbanoff 1372
        pop     edx
6273 pathoswith 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
2288 clevermous 1377
        call    ntfs_read_attr.continue
3742 clevermous 1378
        mov     eax, [ebx+4]
2288 clevermous 1379
        and     eax, 0x1FF
6273 pathoswith 1380
        lea     esi, [ebp+NTFS.bitmap_buf+eax]
1381
        sub     eax, [ebp+NTFS.cur_read]
2288 clevermous 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
3742 clevermous 1398
        call    ntfs_unlock
2288 clevermous 1399
        xor     eax, eax
1400 turbanoff 1400
        ret
6297 pathoswith 1401
 
2288 clevermous 1402
@@:
6273 pathoswith 1403
        cmp     [ebp+NTFS.cur_read], 0x200
2288 clevermous 1404
        jz      .alignedstart
1405
.eof_ebx:
1406
        popad
1407
        jmp     .eof
6297 pathoswith 1408
 
2288 clevermous 1409
.alignedstart:
3742 clevermous 1410
        mov     eax, [ebx+4]
2288 clevermous 1411
        push    edx
3742 clevermous 1412
        mov     edx, [ebx+8]
2288 clevermous 1413
        add     eax, 511
1414
        adc     edx, 0
1415
        shrd    eax, edx, 9
1416
        pop     edx
6273 pathoswith 1417
        mov     [ebp+NTFS.cur_offs], eax
1418
        mov     [ebp+NTFS.cur_buf], edx
2288 clevermous 1419
        mov     eax, ecx
1420
        shr     eax, 9
6273 pathoswith 1421
        mov     [ebp+NTFS.cur_size], eax
1422
        add     eax, [ebp+NTFS.cur_offs]
2288 clevermous 1423
        push    eax
1424
        call    ntfs_read_attr.continue
6273 pathoswith 1425
        pop     [ebp+NTFS.cur_offs]
1426
        mov     eax, [ebp+NTFS.cur_read]
2288 clevermous 1427
        add     [esp+10h], eax
1428
        mov     eax, ecx
1429
        and     eax, not 0x1FF
6273 pathoswith 1430
        cmp     [ebp+NTFS.cur_read], eax
2288 clevermous 1431
        jnz     .eof_ebx
1432
        and     ecx, 0x1FF
1433
        jz      .retok
6273 pathoswith 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
2288 clevermous 1438
        call    ntfs_read_attr.continue
6273 pathoswith 1439
        cmp     [ebp+NTFS.cur_read], ecx
2288 clevermous 1440
        jb      @f
6273 pathoswith 1441
        mov     [ebp+NTFS.cur_read], ecx
2288 clevermous 1442
@@:
6273 pathoswith 1443
        xchg    ecx, [ebp+NTFS.cur_read]
2288 clevermous 1444
        push    ecx
1445
        mov     edi, edx
6273 pathoswith 1446
        lea     esi, [ebp+NTFS.bitmap_buf]
2288 clevermous 1447
        add     [esp+10h+4], ecx
1448
        rep movsb
1449
        pop     ecx
1450
        xor     eax, eax
6273 pathoswith 1451
        cmp     ecx, [ebp+NTFS.cur_read]
2288 clevermous 1452
        jz      @f
1453
        mov     al, ERROR_END_OF_FILE
1454
@@:
1455
        mov     [esp+1Ch], eax
3742 clevermous 1456
        call    ntfs_unlock
2288 clevermous 1457
        popad
1458
        ret
1400 turbanoff 1459
 
2288 clevermous 1460
;----------------------------------------------------------------
3742 clevermous 1461
ntfs_ReadFolder:
1462
        call    ntfs_lock
6297 pathoswith 1463
        mov     [ebp+NTFS.cur_iRecord], 5   ; root directory
2288 clevermous 1464
        cmp     byte [esi], 0
6297 pathoswith 1465
        jz      @f
6468 pathoswith 1466
        call    ntfs_find_lfn
6297 pathoswith 1467
        jc      ntfsNotFound
1468
@@:
6273 pathoswith 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
2288 clevermous 1474
        call    ntfs_read_attr
6297 pathoswith 1475
        jc      ntfsFail
6273 pathoswith 1476
        mov     [ebp+NTFS.cur_attr], 0x90   ; $INDEX_ROOT
6297 pathoswith 1477
.doit:
3742 clevermous 1478
        mov     eax, [ebp+NTFS.cur_index_size]
6273 pathoswith 1479
        mov     [ebp+NTFS.cur_size], eax
3742 clevermous 1480
        mov     eax, [ebp+NTFS.cur_index_buf]
6273 pathoswith 1481
        mov     [ebp+NTFS.cur_buf], eax
6297 pathoswith 1482
        call    ntfs_read_attr.newAttribute
1483
        jc      ntfsFail
6273 pathoswith 1484
        cmp     [ebp+NTFS.cur_read], 0x20
6297 pathoswith 1485
        jc      ntfsFail
3742 clevermous 1486
        mov     esi, [ebp+NTFS.cur_index_buf]
6340 pathoswith 1487
        mov     eax, [esi+indexRecordSize]
1488
        shr     eax, 9
1489
        cmp     [ebp+NTFS.cur_index_size], eax
6297 pathoswith 1490
        jc      .realloc
6340 pathoswith 1491
        mov     [ebp+NTFS.cur_subnode_size], eax
6297 pathoswith 1492
        add     esi, rootNode
1493
        mov     eax, [esi+nodeRealSize]
1494
        add     eax, rootNode
1495
        cmp     [ebp+NTFS.cur_read], eax
6408 pathoswith 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]
2288 clevermous 1504
; init header
1505
        xor     eax, eax
6408 pathoswith 1506
        mov     [edi+8], eax
1507
        mov     [edi+4], eax
1508
        inc     eax
1509
        mov     [edi], eax      ; version
1510
        add     edi, 32
3742 clevermous 1511
; edi -> BDFE, esi -> current index data, ebx = first wanted block,
2288 clevermous 1512
; ecx = number of blocks to read
1513
; edx -> parameters block: dd , dd 
6273 pathoswith 1514
        cmp     [ebp+NTFS.cur_iRecord], 5
2288 clevermous 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
6297 pathoswith 1525
        add     esi, [esi+indexOffset]
2288 clevermous 1526
.dump_root:
6297 pathoswith 1527
        test    byte [esi+indexFlags], 2
2288 clevermous 1528
        jnz     .dump_root_done
1529
        call    .add_entry
6297 pathoswith 1530
        movzx   eax, word [esi+indexAllocatedSize]
2288 clevermous 1531
        add     esi, eax
1532
        jmp     .dump_root
6297 pathoswith 1533
 
1534
.realloc:
6340 pathoswith 1535
        mov     edi, eax
1536
        mov     eax, [esi+indexRecordSize]
1537
        shl     eax, 1
1538
        stdcall kernel_alloc, eax
6297 pathoswith 1539
        test    eax, eax
6408 pathoswith 1540
        jz      ntfsFail
6405 pathoswith 1541
        mov     edx, [ebp+NTFS.cur_index_buf]
1542
        cmp     edx, [ebp+NTFS.secondIndexBuffer]
1543
        jc      @f
1544
        mov     edx, [ebp+NTFS.secondIndexBuffer]
1545
@@:
6297 pathoswith 1546
        mov     [ebp+NTFS.cur_index_buf], eax
6340 pathoswith 1547
        add     eax, [esi+indexRecordSize]
1548
        mov     [ebp+NTFS.secondIndexBuffer], eax
1549
        mov     [ebp+NTFS.cur_index_size], edi
6405 pathoswith 1550
        stdcall kernel_free, edx
6297 pathoswith 1551
        jmp     .doit
1552
 
2288 clevermous 1553
.dump_root_done:
1554
; now dump all subnodes
1555
        push    ecx edi
6273 pathoswith 1556
        lea     edi, [ebp+NTFS.bitmap_buf]
1557
        mov     [ebp+NTFS.cur_buf], edi
2288 clevermous 1558
        mov     ecx, 0x400/4
1559
        xor     eax, eax
1560
        rep stosd
6273 pathoswith 1561
        mov     [ebp+NTFS.cur_attr], 0xB0   ; $BITMAP
1562
        and     [ebp+NTFS.cur_offs], 0
1563
        mov     [ebp+NTFS.cur_size], 2
6297 pathoswith 1564
        call    ntfs_read_attr.newAttribute
2288 clevermous 1565
        pop     edi ecx
1566
        push    0       ; save offset in $BITMAP attribute
6273 pathoswith 1567
        and     [ebp+NTFS.cur_offs], 0
2288 clevermous 1568
.dumploop:
6273 pathoswith 1569
        mov     [ebp+NTFS.cur_attr], 0xA0
3742 clevermous 1570
        mov     eax, [ebp+NTFS.cur_subnode_size]
6273 pathoswith 1571
        mov     [ebp+NTFS.cur_size], eax
6297 pathoswith 1572
        mov     esi, [ebp+NTFS.cur_index_buf]
1573
        mov     [ebp+NTFS.cur_buf], esi
6273 pathoswith 1574
        mov     eax, [ebp+NTFS.cur_offs]
6297 pathoswith 1575
        push    eax
3742 clevermous 1576
        imul    eax, [ebp+NTFS.cur_subnode_size]
6273 pathoswith 1577
        mov     [ebp+NTFS.cur_offs], eax
6297 pathoswith 1578
        call    ntfs_read_attr.newAttribute
6273 pathoswith 1579
        pop     [ebp+NTFS.cur_offs]
3742 clevermous 1580
        mov     eax, [ebp+NTFS.cur_subnode_size]
2288 clevermous 1581
        shl     eax, 9
6273 pathoswith 1582
        cmp     [ebp+NTFS.cur_read], eax
2288 clevermous 1583
        jnz     .done
1584
        push    eax
6273 pathoswith 1585
        mov     eax, [ebp+NTFS.cur_offs]
2288 clevermous 1586
        and     eax, 0x400*8-1
6273 pathoswith 1587
        bt      dword [ebp+NTFS.bitmap_buf], eax
2288 clevermous 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
6297 pathoswith 1597
        add     esi, recordNode
1598
        add     esi, [esi+indexOffset]
2288 clevermous 1599
.dump_subnode:
6297 pathoswith 1600
        test    byte [esi+indexFlags], 2
2288 clevermous 1601
        jnz     .dump_subnode_done
1602
        call    .add_entry
6297 pathoswith 1603
        movzx   eax, word [esi+indexAllocatedSize]
2288 clevermous 1604
        add     esi, eax
1605
        jmp     .dump_subnode
6297 pathoswith 1606
 
2288 clevermous 1607
.dump_subnode_done:
6273 pathoswith 1608
        inc     [ebp+NTFS.cur_offs]
1609
        test    [ebp+NTFS.cur_offs], 0x400*8-1
2288 clevermous 1610
        jnz     .dumploop
6273 pathoswith 1611
        mov     [ebp+NTFS.cur_attr], 0xB0
2288 clevermous 1612
        push    ecx edi
6273 pathoswith 1613
        lea     edi, [ebp+NTFS.bitmap_buf]
1614
        mov     [ebp+NTFS.cur_buf], edi
2288 clevermous 1615
        mov     ecx, 0x400/4
1616
        xor     eax, eax
1617
        rep stosd
1618
        pop     edi ecx
1619
        pop     eax
6273 pathoswith 1620
        push    [ebp+NTFS.cur_offs]
2288 clevermous 1621
        inc     eax
6273 pathoswith 1622
        mov     [ebp+NTFS.cur_offs], eax
1623
        mov     [ebp+NTFS.cur_size], 2
2288 clevermous 1624
        push    eax
6297 pathoswith 1625
        call    ntfs_read_attr.newAttribute
2288 clevermous 1626
        pop     eax
6273 pathoswith 1627
        pop     [ebp+NTFS.cur_offs]
2288 clevermous 1628
        push    eax
1629
        jmp     .dumploop
6297 pathoswith 1630
 
2288 clevermous 1631
.done:
1632
        pop     eax
6408 pathoswith 1633
        pop     eax
1634
        mov     ebx, [eax+4]
1635
        pop     eax
1636
        pop     eax
1637
        test    eax, eax
1638
        jz      .ret
2288 clevermous 1639
        xor     eax, eax
1640
        dec     ecx
1641
        js      @f
1642
        mov     al, ERROR_END_OF_FILE
1643
@@:
6408 pathoswith 1644
        push    eax
3742 clevermous 1645
        call    ntfs_unlock
6408 pathoswith 1646
        pop     eax
2288 clevermous 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
6462 pathoswith 1662
        push    ebx ecx edx
6273 pathoswith 1663
        mov     eax, dword [ebp+NTFS.bitmap_buf]
1664
        mov     edx, dword [ebp+NTFS.bitmap_buf+4]
2288 clevermous 1665
        call    ntfs_datetime_to_bdfe
6273 pathoswith 1666
        mov     eax, dword [ebp+NTFS.bitmap_buf+0x18]
1667
        mov     edx, dword [ebp+NTFS.bitmap_buf+0x1C]
2288 clevermous 1668
        call    ntfs_datetime_to_bdfe
6273 pathoswith 1669
        mov     eax, dword [ebp+NTFS.bitmap_buf+8]
1670
        mov     edx, dword [ebp+NTFS.bitmap_buf+0xC]
2288 clevermous 1671
        call    ntfs_datetime_to_bdfe
6462 pathoswith 1672
        pop     edx ecx ebx
2288 clevermous 1673
        xor     eax, eax
1674
        stosd
1675
        stosd
1676
        mov     al, '.'
1677
        push    edi ecx
1678
        lea     ecx, [esi+1]
6798 pathoswith 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
2288 clevermous 1685
        jz      @f
6798 pathoswith 1686
        add     edi, 264
2288 clevermous 1687
        ret
6297 pathoswith 1688
 
6798 pathoswith 1689
.utf16sp:
1690
        rep stosw
1691
        mov     word [edi], 0
1692
        pop     ecx edi
2288 clevermous 1693
@@:
6798 pathoswith 1694
        add     edi, 520
2288 clevermous 1695
.ret:
1696
        ret
1697
 
1698
.add_entry:
1699
; do not return DOS 8.3 names
6297 pathoswith 1700
        cmp     byte [esi+namespace], 2
2288 clevermous 1701
        jz      .ret
1702
; do not return system files
6798 pathoswith 1703
        cmp     dword[esi+fileRecordReference], 16
2288 clevermous 1704
        jb      .ret
6798 pathoswith 1705
        cmp     byte [esi+fileNameLength], 0
1706
        jz      .ret
2288 clevermous 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
6297 pathoswith 1717
        movzx   ecx, byte [esi+fileNameLength]
1718
        add     esi, fileName
6798 pathoswith 1719
        cmp     dword[edi-36], 2
1720
        jz      .utf16
1721
        cmp     dword[edi-36], 3
1722
        jz      .utf8
2288 clevermous 1723
@@:
1724
        lodsw
1725
        call    uni2ansi_char
1726
        stosb
1727
        loop    @b
6798 pathoswith 1728
        mov     byte [edi], 0
1729
        pop     edi esi ecx
2288 clevermous 1730
        add     edi, 264
1731
        ret
1732
 
6798 pathoswith 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
 
2288 clevermous 1755
ntfs_direntry_to_bdfe:
1756
        mov     [edi+4], eax    ; ANSI/UNICODE name
6297 pathoswith 1757
        mov     eax, [esi+fileFlags]
2288 clevermous 1758
        test    eax, 0x10000000
1759
        jz      @f
1760
        and     eax, not 0x10000000
1761
        or      al, 0x10
1762
@@:
1763
        stosd
1764
        scasd
6462 pathoswith 1765
        push    ebx ecx edx
6297 pathoswith 1766
        mov     eax, [esi+fileCreated]
1767
        mov     edx, [esi+fileCreated+4]
2288 clevermous 1768
        call    ntfs_datetime_to_bdfe
6297 pathoswith 1769
        mov     eax, [esi+fileAccessed]
1770
        mov     edx, [esi+fileAccessed+4]
2288 clevermous 1771
        call    ntfs_datetime_to_bdfe
6297 pathoswith 1772
        mov     eax, [esi+fileModified]
1773
        mov     edx, [esi+fileModified+4]
2288 clevermous 1774
        call    ntfs_datetime_to_bdfe
6462 pathoswith 1775
        pop     edx ecx ebx
6297 pathoswith 1776
        mov     eax, [esi+fileRealSize]
2288 clevermous 1777
        stosd
6297 pathoswith 1778
        mov     eax, [esi+fileRealSize+4]
2288 clevermous 1779
        stosd
1780
        ret
1781
 
1782
ntfs_datetime_to_bdfe:
6462 pathoswith 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
6297 pathoswith 1788
        mov     ecx, 10000000
6462 pathoswith 1789
        cmp     edx, ecx
1790
        jc      @f
2288 clevermous 1791
        xor     edx, edx
6462 pathoswith 1792
@@:
6297 pathoswith 1793
        div     ecx
6462 pathoswith 1794
        jmp     fsTime2bdfe
2288 clevermous 1795
 
1796
;----------------------------------------------------------------
6420 pathoswith 1797
ntfs_GetFileInfo:
6426 pathoswith 1798
        call    ntfs_lock
1799
        mov     edi, [ebx+16]
6420 pathoswith 1800
        cmp     byte [esi], 0
6426 pathoswith 1801
        jz      .volume
6468 pathoswith 1802
        call    ntfs_find_lfn
6420 pathoswith 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
6426 pathoswith 1812
.end:
6420 pathoswith 1813
        call    ntfs_unlock
1814
        xor     eax, eax
1815
        ret
1816
 
6426 pathoswith 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
6428 pathoswith 1841
        jz      .end
6426 pathoswith 1842
@@:
1843
        lodsw
1844
        call    uni2ansi_char
1845
        stosb
6471 pathoswith 1846
        loop    @b
6426 pathoswith 1847
        mov     byte [edi], 0
1848
        jmp     .end
1849
 
6420 pathoswith 1850
;----------------------------------------------------------------
3742 clevermous 1851
ntfs_CreateFolder:
6273 pathoswith 1852
        mov     [ebp+NTFS.bFolder], 1
5954 pathoswith 1853
        jmp     @f
5994 pathoswith 1854
 
5954 pathoswith 1855
ntfs_CreateFile:
6273 pathoswith 1856
        mov     [ebp+NTFS.bFolder], 0
5954 pathoswith 1857
@@:
1858
        cmp     byte [esi], 0
1859
        jnz     @f
2288 clevermous 1860
        xor     ebx, ebx
6019 pathoswith 1861
        movi    eax, ERROR_ACCESS_DENIED
2288 clevermous 1862
        ret
6297 pathoswith 1863
 
5954 pathoswith 1864
@@: ; 1. Search file
1865
        call    ntfs_lock
6468 pathoswith 1866
        call    ntfs_find_lfn
6080 pathoswith 1867
        jc      .notFound
1868
; found, rewrite
6273 pathoswith 1869
        cmp     [ebp+NTFS.cur_iRecord], 16
6019 pathoswith 1870
        jc      ntfsDenied
6273 pathoswith 1871
        cmp     [ebp+NTFS.bFolder], 1
6080 pathoswith 1872
        jz      .folder
6420 pathoswith 1873
        test    byte [eax+fileFlags], 1
1874
        jnz     ntfsDenied
6273 pathoswith 1875
        cmp     [ebp+NTFS.fragmentCount], 1
6080 pathoswith 1876
        jnz     ntfsUnsupported     ; record fragmented
6151 pathoswith 1877
; edit directory node
6080 pathoswith 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
6273 pathoswith 1885
        mov     esi, [ebp+NTFS.attr_offs]
6080 pathoswith 1886
        mov     cl, [esi+attributeOffset]
1887
        sub     esi, [ebp+NTFS.frs_buffer]
1888
        add     eax, ecx
1889
        add     eax, esi
1890
@@:
6420 pathoswith 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
6273 pathoswith 1904
        mov     eax, [ebp+NTFS.LastRead]
6080 pathoswith 1905
        mov     [ebp+NTFS.nodeLastRead], eax
6273 pathoswith 1906
        mov     [ebp+NTFS.cur_attr], 0x80
1907
        mov     [ebp+NTFS.cur_offs], 0
1908
        mov     [ebp+NTFS.cur_size], 0
5994 pathoswith 1909
        call    ntfs_read_attr
6080 pathoswith 1910
        jc      ntfsFail
6420 pathoswith 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
6080 pathoswith 1923
        mov     eax, edx
1924
        xor     edx, edx
6273 pathoswith 1925
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 1926
        cmp     word [ecx+attributeFlags], 0
1927
        jnz     ntfsUnsupported
1928
        push    ebx
1929
        cmp     byte [ecx+nonResidentFlag], 0
1930
        jz      @f
6080 pathoswith 1931
        cmp     [ecx+attributeRealSize+4], edx
1932
        jnz     @f
5994 pathoswith 1933
        cmp     [ecx+attributeRealSize], eax
6080 pathoswith 1934
        jz      ntfs_WriteFile.writeNode
1935
@@:
1936
        jmp     ntfs_WriteFile.resizeAttribute
5994 pathoswith 1937
 
6080 pathoswith 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
6273 pathoswith 1947
        cmp     [ebp+NTFS.fragmentCount], 1
6080 pathoswith 1948
        jnz     ntfsUnsupported     ; record fragmented
6019 pathoswith 1949
; 2. Prepare directory record
6471 pathoswith 1950
        mov     edi, esi
1951
        mov     edx, eax
1952
        xor     ecx, ecx
5954 pathoswith 1953
@@:         ; count characters
6471 pathoswith 1954
        call    utf8to16
1955
        cmp     ax, '/'
1956
        jz      ntfsNotFound    ; path folder not found
5954 pathoswith 1957
        inc     ecx
6471 pathoswith 1958
        test    ax, ax
5954 pathoswith 1959
        jnz     @b
6471 pathoswith 1960
        dec     ecx
1961
        push    ecx     ; name length in chars
1962
        push    edi
6297 pathoswith 1963
        shl     ecx, 1
1964
        add     ecx, fileName+7
1965
        and     ecx, not 7
5954 pathoswith 1966
        mov     edi, [ebp+NTFS.cur_index_buf]
6471 pathoswith 1967
        mov     eax, [ebx+12]
1968
        mov     [ebp+NTFS.fileRealSize], eax
1969
        mov     eax, [ebx+16]
1970
        mov     [ebp+NTFS.fileDataBuffer], eax
6297 pathoswith 1971
        push    ecx     ; index length
6471 pathoswith 1972
        mov     eax, edx
6292 pathoswith 1973
        mov     edx, ecx
6019 pathoswith 1974
        cmp     dword [edi], 'INDX'
5954 pathoswith 1975
        jz      .indexRecord
6019 pathoswith 1976
        mov     esi, [ebp+NTFS.frs_buffer]  ; indexRoot
6292 pathoswith 1977
        mov     ecx, [esi+recordRealSize]
5954 pathoswith 1978
        add     edx, ecx
1979
        cmp     [esi+recordAllocatedSize], edx
6292 pathoswith 1980
        jc      .growTree
5954 pathoswith 1981
        mov     [esi+recordRealSize], edx
1982
        shr     ecx, 2
1983
        rep movsd
6407 pathoswith 1984
        mov     edi, [ebp+NTFS.indexRoot]
5954 pathoswith 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
6019 pathoswith 1990
        mov     cl, [edi+attributeOffset]
5954 pathoswith 1991
        add     edi, ecx
6273 pathoswith 1992
        add     [edi+rootNode+nodeRealSize], esi
1993
        add     [edi+rootNode+nodeAllocatedSize], esi
5954 pathoswith 1994
        sub     eax, [ebp+NTFS.cur_index_buf]
1995
        add     eax, edi
1996
        mov     edi, [ebp+NTFS.cur_index_buf]
1997
        jmp     .common
2288 clevermous 1998
 
6411 pathoswith 1999
.growTree:  ; create indexRecord
2000
        mov     edi, [ebp+NTFS.cur_index_buf]
6292 pathoswith 2001
        mov     ecx, 10
2002
        xor     eax, eax
2003
        rep stosd
6407 pathoswith 2004
        mov     esi, [ebp+NTFS.indexRoot]
6340 pathoswith 2005
        mov     al, [esi+attributeOffset]
2006
        add     esi, eax
6292 pathoswith 2007
        rdtsc
2008
        stosw
2009
        mov     eax, [esi+indexRecordSize]
2010
        cmp     eax, [ebp+NTFS.frs_size]
6407 pathoswith 2011
        jc      .errorPop3
6292 pathoswith 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
6411 pathoswith 2032
        mov     eax, [esi+nonLeafFlag]
2033
        mov     [edi+nonLeafFlag], eax
6292 pathoswith 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
6407 pathoswith 2050
        mov     esi, [ebp+NTFS.indexRoot]
6292 pathoswith 2051
        add     edi, 28h
2052
        mov     eax, edi
2053
        sub     eax, esi
2054
        mov     word [esi+sizeWithoutHeader], 38h
2055
        xchg    [esi+sizeWithHeader], eax
6407 pathoswith 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
@@:
6292 pathoswith 2080
        mov     cl, 32
2081
        xor     eax, eax
2082
        rep stosd
6407 pathoswith 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
6292 pathoswith 2089
        mov     edi, [ebp+NTFS.BitmapStart]
2090
        call    ntfsSpaceAlloc
6407 pathoswith 2091
        movi    eax, ERROR_DISK_FULL
2092
        jc      ntfsErrorPop3
2093
; create $IndexAllocation
2094
        mov     edi, [ebp+NTFS.attr_offs]
6292 pathoswith 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]
6408 pathoswith 2141
        mov     edx, eax
6407 pathoswith 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
6408 pathoswith 2149
        mov     edx, [ebp+NTFS.LastRead]
6407 pathoswith 2150
@@:
6408 pathoswith 2151
        mov     ebx, [ebp+NTFS.cur_index_buf]
6407 pathoswith 2152
        call    writeRecord
6408 pathoswith 2153
        mov     ebx, [ebp+NTFS.frs_buffer]
2154
        mov     edx, [ebp+NTFS.rootLastRead]
6407 pathoswith 2155
        call    writeRecord
2156
        mov     esi, [esp+4]
6468 pathoswith 2157
        call    ntfs_find_lfn.doit2
6407 pathoswith 2158
        test    eax, eax
2159
        jz      .errorPop3
6292 pathoswith 2160
        mov     edi, [ebp+NTFS.cur_index_buf]
2161
        mov     edx, [esp]
5954 pathoswith 2162
.indexRecord:
6273 pathoswith 2163
        add     edi, recordNode
6292 pathoswith 2164
        add     edx, [edi+nodeRealSize]
6273 pathoswith 2165
        cmp     [edi+nodeAllocatedSize], edx
6340 pathoswith 2166
        jc      .arborizeTree
2167
        mov     [edi+nodeRealSize], edx
2168
        jmp     .common
2169
 
2170
.errorPop3:
5954 pathoswith 2171
        add     esp, 12
6340 pathoswith 2172
        jmp     ntfsUnsupported
6292 pathoswith 2173
 
6407 pathoswith 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
6340 pathoswith 2180
        mov     esi, [ebp+NTFS.attr_offs]
2181
        add     esi, [esi+sizeWithHeader]
2182
        cmp     byte [esi], 0xB0
6407 pathoswith 2183
        jnz     .ret
6414 pathoswith 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]
6340 pathoswith 2212
        not     eax
2213
        bsf     eax, eax
6414 pathoswith 2214
        bts     [edi], eax
2215
        sub     edi, edx
2216
        shl     edi, 3
2217
        add     eax, edi
6340 pathoswith 2218
        mul     [ebp+NTFS.cur_subnode_size]
2219
        mov     [ebp+NTFS.newRecord], eax
6407 pathoswith 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
6340 pathoswith 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
6407 pathoswith 2251
        add     eax, 8
6340 pathoswith 2252
        mov     esi, [ebp+NTFS.secondIndexBuffer]
2253
        cmp     dword [esi], 'INDX'
6411 pathoswith 2254
        jz      @f
6340 pathoswith 2255
; move index to the root node
2256
        mov     esi, [ebp+NTFS.frs_buffer]
6407 pathoswith 2257
        mov     ecx, eax
2258
        add     ecx, 8
2259
        add     ecx, [esi+recordRealSize]
6340 pathoswith 2260
        cmp     [esi+recordAllocatedSize], ecx
6411 pathoswith 2261
        jc      .growTree
6409 pathoswith 2262
        push    edi eax
6407 pathoswith 2263
        call    .ntfsNodeAlloc
6414 pathoswith 2264
        jc      ntfsErrorPop5
2265
        pop     eax
6340 pathoswith 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
6405 pathoswith 2274
        add     ecx, [ebp+NTFS.indexPointer]
6340 pathoswith 2275
        sub     ecx, [ebp+NTFS.secondIndexBuffer]
6409 pathoswith 2276
        mov     esi, [ebp+NTFS.frs_buffer]
2277
        add     [esi+recordRealSize], eax
2278
        add     esi, [esi+recordRealSize]
6340 pathoswith 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
6411 pathoswith 2301
        jmp     .splitNode
2302
 
6414 pathoswith 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
6411 pathoswith 2333
@@:         ; move index to the branch node
6414 pathoswith 2334
        push    edi eax
2335
        call    .ntfsNodeAlloc
2336
        jc      ntfsErrorPop5
2337
        mov     eax, [esp]
2338
        mov     esi, [ebp+NTFS.secondIndexBuffer]
6411 pathoswith 2339
        add     esi, recordNode
2340
        mov     ecx, [esi+nodeRealSize]
2341
        add     eax, ecx
2342
        cmp     [esi+nodeAllocatedSize], eax
6414 pathoswith 2343
        jc      .growBranch
6411 pathoswith 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
6414 pathoswith 2354
        pop     ecx
2355
        sub     ecx, 8
2356
        shr     ecx, 2
6411 pathoswith 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:
6340 pathoswith 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
6408 pathoswith 2405
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
2406
        mov     edx, [ebp+NTFS.LastRead]
6340 pathoswith 2407
        call    writeRecord
6407 pathoswith 2408
        jmp     .refresh
6340 pathoswith 2409
 
6292 pathoswith 2410
.common:
6273 pathoswith 2411
        add     edi, edx
2412
        sub     edi, 4
5954 pathoswith 2413
        mov     esi, edi
2414
        sub     esi, [esp]
2415
        mov     ecx, esi
6292 pathoswith 2416
        sub     ecx, eax    ; eax = pointer in the record
5954 pathoswith 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
6420 pathoswith 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
6151 pathoswith 2436
        pop     ecx
5954 pathoswith 2437
        pop     esi
6151 pathoswith 2438
        mov     [edi+indexAllocatedSize], cx    ; fill index with data
5954 pathoswith 2439
        mov     eax, [esp]
6080 pathoswith 2440
        shl     eax, 1
2441
        add     eax, 42h
5954 pathoswith 2442
        mov     [edi+indexRawSize], ax
6418 pathoswith 2443
        mov     eax, [ebp+NTFS.cur_iRecord]
5954 pathoswith 2444
        mov     [edi+directoryRecordReference], eax
2445
        mov     eax, [ebp+NTFS.frs_buffer]
2446
        mov     eax, [eax+reuseCounter]
2447
        mov     [edi+directoryReferenceReuse], ax
6292 pathoswith 2448
        mov     eax, [ebp+NTFS.frs_size]
2449
        shr     eax, 8
6151 pathoswith 2450
        add     ecx, 30h+48h+8+18h+8
2451
        add     ecx, eax
6292 pathoswith 2452
        mov     eax, [ebp+NTFS.fileRealSize]
2453
        add     ecx, eax
5954 pathoswith 2454
        mov     [edi+fileRealSize], eax
6151 pathoswith 2455
        cmp     [ebp+NTFS.frs_size], ecx
2456
        jc      @f
2457
        xor     eax, eax
2458
@@:
5954 pathoswith 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
6405 pathoswith 2469
        mov     [ebp+NTFS.indexPointer], edi
5954 pathoswith 2470
        mov     [edi+fileNameLength], cl
6297 pathoswith 2471
        add     edi, fileName
5954 pathoswith 2472
@@:         ; record filename
6471 pathoswith 2473
        call    utf8to16
5954 pathoswith 2474
        stosw
6471 pathoswith 2475
        loop    @b
6273 pathoswith 2476
        mov     eax, [ebp+NTFS.LastRead]
5954 pathoswith 2477
        mov     [ebp+NTFS.nodeLastRead], eax
6273 pathoswith 2478
        cmp     [ebp+NTFS.bFolder], 0
5954 pathoswith 2479
        jz      @f
6405 pathoswith 2480
        mov     edi, [ebp+NTFS.indexPointer]
6080 pathoswith 2481
        bts     dword [edi+fileFlags], 28
5954 pathoswith 2482
        jmp     .mftBitmap
2483
 
2484
@@: ; 3. File data
6151 pathoswith 2485
        cmp     [ebp+NTFS.fileDataSize], 0
5954 pathoswith 2486
        jz      .mftBitmap
6080 pathoswith 2487
        mov     edi, [ebp+NTFS.BitmapStart]
2488
        call    ntfsSpaceAlloc
2489
        jc      ntfsDiskFull
6292 pathoswith 2490
        mov     eax, [ebp+NTFS.fileDataStart]
5954 pathoswith 2491
        mul     [ebp+NTFS.sectors_per_cluster]
2492
        mov     ecx, [ebp+NTFS.fileRealSize]
2493
        add     ecx, 511
2494
        shr     ecx, 9
6292 pathoswith 2495
        mov     ebx, [ebp+NTFS.fileDataBuffer]
5954 pathoswith 2496
        call    fs_write64_app
2497
        test    eax, eax
6019 pathoswith 2498
        jnz     ntfsDevice
5954 pathoswith 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
6107 pathoswith 2511
        jz      .extendBitmapMFT    ; no free records
6019 pathoswith 2512
        bts     [edi], ecx
2513
; get record location
5954 pathoswith 2514
        sub     edi, [ebp+NTFS.mftBitmapBuffer]
2515
        shl     edi, 3
2516
        add     edi, ecx
6340 pathoswith 2517
        mov     [ebp+NTFS.newRecord], edi
5954 pathoswith 2518
        mov     eax, [ebp+NTFS.frs_size]
2519
        shr     eax, 9
2520
        mul     edi
6273 pathoswith 2521
        mov     [ebp+NTFS.cur_iRecord], 0
2522
        mov     [ebp+NTFS.cur_attr], 0x80
2523
        mov     [ebp+NTFS.cur_offs], eax
6405 pathoswith 2524
        push    eax
2525
        mov     [ebp+NTFS.cur_size], 0
5954 pathoswith 2526
        mov     eax, [ebp+NTFS.frs_buffer]
6273 pathoswith 2527
        mov     [ebp+NTFS.cur_buf], eax
5954 pathoswith 2528
        call    ntfs_read_attr
6405 pathoswith 2529
        pop     eax
2530
        jc      ntfsFail
2531
        cmp     eax, [ebp+NTFS.mftSize]
2532
        jnc     .extendMFT
6107 pathoswith 2533
        jmp     .mftRecord
2534
 
2535
.extendBitmapMFT:
2536
        mov     eax, [ebp+NTFS.sectors_per_cluster]
6405 pathoswith 2537
        mov     [ebp+NTFS.cur_offs], eax
6107 pathoswith 2538
        shl     eax, 9
2539
        cmp     [ebp+NTFS.mftBitmapSize], eax
2540
        jnc     ntfsUnsupported
6273 pathoswith 2541
        mov     [ebp+NTFS.cur_iRecord], 0
2542
        mov     [ebp+NTFS.cur_attr], 0xB0
2543
        mov     [ebp+NTFS.cur_size], 0
6107 pathoswith 2544
        call    ntfs_read_attr
2545
        jc      ntfsFail
5954 pathoswith 2546
        mov     eax, [ebp+NTFS.mft_cluster]
2547
        mul     [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 2548
        cmp     eax, [ebp+NTFS.LastRead]
6019 pathoswith 2549
        jnz     ntfsUnsupported     ; auxiliary record
6107 pathoswith 2550
        mov     edi, [ebp+NTFS.mftBitmapBuffer]
2551
        mov     ecx, [ebp+NTFS.mftBitmapSize]
2552
        add     edi, ecx
2553
        mov     eax, ecx
6273 pathoswith 2554
        mov     edx, [ebp+NTFS.attr_offs]
6107 pathoswith 2555
        add     ecx, 8
2556
        mov     [edx+attributeRealSize], ecx
2557
        mov     [edx+initialDataSize], ecx
2558
        shl     eax, 3
6340 pathoswith 2559
        mov     [ebp+NTFS.newRecord], eax
6107 pathoswith 2560
        mov     dword [edi], 1
2561
        mov     dword [edi+4], 0
6273 pathoswith 2562
        mov     [ebp+NTFS.cur_attr], 0x80
6405 pathoswith 2563
        mov     [ebp+NTFS.cur_offs], 0
6107 pathoswith 2564
        call    ntfs_read_attr.newAttribute
2565
        jc      ntfsFail
2566
        mov     [ebp+NTFS.mftBitmapSize], ecx
2567
.extendMFT:
2568
        mov     eax, [ebp+NTFS.mft_cluster]
5954 pathoswith 2569
        mul     [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 2570
        cmp     eax, [ebp+NTFS.LastRead]
6107 pathoswith 2571
        jnz     ntfsUnsupported     ; auxiliary record
6273 pathoswith 2572
        mov     ecx, [ebp+NTFS.attr_offs]
6107 pathoswith 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
6408 pathoswith 2582
        mov     ebx, [ebp+NTFS.frs_buffer]
2583
        mov     edx, [ebp+NTFS.LastRead]
5954 pathoswith 2584
        call    writeRecord     ; $MFT
2585
        mov     eax, [ebp+NTFS.mftmirr_cluster]
2586
        mul     [ebp+NTFS.sectors_per_cluster]
6408 pathoswith 2587
        mov     ecx, [ebp+NTFS.frs_size]
2588
        shr     ecx, 9
5954 pathoswith 2589
        call    fs_write64_sys  ; $MFTMirr
6405 pathoswith 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
6107 pathoswith 2611
        call    ntfsSpaceClean
2612
        pop     [ebp+NTFS.fileDataSize]
2613
        pop     [ebp+NTFS.fileDataStart]
5954 pathoswith 2614
.mftRecord:
6151 pathoswith 2615
        mov     ecx, [ebp+NTFS.frs_size]
2616
        shr     ecx, 2
5954 pathoswith 2617
        mov     edi, [ebp+NTFS.frs_buffer]
2618
        xor     eax, eax
2619
        rep stosd
6418 pathoswith 2620
        mov     esi, [ebp+NTFS.indexPointer]
2621
        mov     eax, [ebp+NTFS.newRecord]
2622
        mov     [esi+fileRecordReference], eax
2623
        rdtsc
2624
        mov     [esi+fileReferenceReuse], ax
5954 pathoswith 2625
        mov     edi, [ebp+NTFS.frs_buffer]
6019 pathoswith 2626
; record header
6418 pathoswith 2627
        mov     [edi+reuseCounter], ax
6292 pathoswith 2628
        mov     [edi+2ah], ax
6151 pathoswith 2629
        mov     eax, [ebp+NTFS.frs_size]
2630
        mov     [edi+recordAllocatedSize], eax
2631
        shr     eax, 9
2632
        inc     eax
2633
        mov     [edi+updateSequenceSize], al
6292 pathoswith 2634
        shl     eax, 1
2635
        add     eax, 2ah+7
2636
        and     eax, not 7
5954 pathoswith 2637
        mov     dword[edi], 'FILE'
2638
        mov     byte [edi+updateSequenceOffset], 2ah
2639
        mov     byte [edi+hardLinkCounter], 1
2640
        mov     byte [edi+newAttributeID], 3
6292 pathoswith 2641
        mov     [edi+attributeOffset], al
2642
        add     edi, eax
6019 pathoswith 2643
; $StandardInformation
5954 pathoswith 2644
        mov     byte [edi+attributeType], 10h
2645
        mov     byte [edi+sizeWithHeader], 48h
2646
        mov     byte [edi+sizeWithoutHeader], 30h
2647
        mov     byte [edi+attributeOffset], 18h
6420 pathoswith 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]
6019 pathoswith 2654
; $FileName
5954 pathoswith 2655
        mov     byte [edi+attributeType], 30h
2656
        mov     byte [edi+attributeID], 1
6151 pathoswith 2657
        mov     byte [edi+attributeOffset], 18h
2658
        mov     byte [edi+indexedFlag], 1
5954 pathoswith 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
6080 pathoswith 2669
        mov     byte [edi+sizeWithHeader], 50h
2670
        mov     byte [edi+attributeID], 2
6273 pathoswith 2671
        cmp     [ebp+NTFS.bFolder], 1
6151 pathoswith 2672
        jz      .indexRoot
6019 pathoswith 2673
; $Data
5954 pathoswith 2674
        mov     byte [edi+attributeType], 80h
6151 pathoswith 2675
        mov     eax, [ebp+NTFS.fileDataSize]
2676
        test    eax, eax
2677
        jz      .resident
6405 pathoswith 2678
        mov     esi, [ebp+NTFS.indexPointer]
6080 pathoswith 2679
        dec     eax
2680
        mov     [edi+lastVCN], eax
5954 pathoswith 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
6151 pathoswith 2688
        push    edi
6080 pathoswith 2689
        mov     esi, edi
2690
        add     edi, 40h
2691
        call    createMcbEntry
6151 pathoswith 2692
        inc     edi
2693
        jmp     @f
5954 pathoswith 2694
 
6151 pathoswith 2695
.resident:
2696
        mov     ecx, [ebp+NTFS.fileRealSize]
2697
        mov     [edi+sizeWithoutHeader], ecx
5954 pathoswith 2698
        mov     byte [edi+attributeOffset], 18h
6151 pathoswith 2699
        push    edi
6292 pathoswith 2700
        mov     esi, [ebp+NTFS.fileDataBuffer]
6151 pathoswith 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
5954 pathoswith 2711
        mov     al, 1
6292 pathoswith 2712
        jmp     .end
5954 pathoswith 2713
 
6151 pathoswith 2714
.indexRoot:
5954 pathoswith 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
6273 pathoswith 2722
        mov     byte [edi+20h+indexedAttributesType], 30h
6019 pathoswith 2723
        mov     byte [edi+20h+collationRule], 1
5954 pathoswith 2724
        mov     eax, [ebp+NTFS.sectors_per_cluster]
6292 pathoswith 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
6019 pathoswith 2733
        mov     [edi+20h+indexRecordSize], eax
6292 pathoswith 2734
        mov     [edi+20h+indexRecordSizeClus], dl
6019 pathoswith 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
6151 pathoswith 2740
        add     edi, 50h
5954 pathoswith 2741
        mov     al, 3
6292 pathoswith 2742
.end:
6408 pathoswith 2743
        mov     ebx, [ebp+NTFS.frs_buffer]
6151 pathoswith 2744
        mov     dword [edi], -1
2745
        mov     dword [edi+4], 0
2746
        add     edi, 8
6408 pathoswith 2747
        sub     edi, ebx
2748
        mov     [ebx+recordFlags], al
2749
        mov     [ebx+recordRealSize], edi
2750
        mov     edx, [ebp+NTFS.LastRead]
5954 pathoswith 2751
        call    writeRecord
6019 pathoswith 2752
; write MFT bitmap
6340 pathoswith 2753
        mov     eax, [ebp+NTFS.newRecord]
5954 pathoswith 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
6292 pathoswith 2762
; 5. Write directory node
6408 pathoswith 2763
        mov     ebx, [ebp+NTFS.cur_index_buf]
2764
        mov     edx, [ebp+NTFS.nodeLastRead]
5954 pathoswith 2765
        call    writeRecord
6019 pathoswith 2766
        mov     ebx, [ebp+NTFS.fileRealSize]
2767
ntfsDone:
5954 pathoswith 2768
        mov     esi, [ebp+PARTITION.Disk]
2769
        call    disk_sync
2770
        call    ntfs_unlock
6019 pathoswith 2771
        xor     eax, eax
5954 pathoswith 2772
        ret
2773
 
2774
writeRecord:
6080 pathoswith 2775
; make updateSequence and write to disk
5954 pathoswith 2776
;   in:
6408 pathoswith 2777
; ebx -> record
2778
; edx = partition sector
2779
        mov     esi, ebx
2780
        mov     edi, ebx
5954 pathoswith 2781
        movzx   ecx, word [esi+updateSequenceOffset]
2782
        add     edi, ecx
2783
        mov     ax, [edi]
6080 pathoswith 2784
        inc     ax
2785
        stosw
5954 pathoswith 2786
        mov     cx, [esi+updateSequenceSize]
2787
        dec     ecx
2788
        push    ecx
2789
@@:
2790
        add     esi, 510
2791
        movsw
2792
        mov     [esi-2], ax
6471 pathoswith 2793
        loop    @b
6408 pathoswith 2794
        mov     eax, edx
2795
        xor     edx, edx
5954 pathoswith 2796
        pop     ecx
2797
        jmp     fs_write64_sys
2798
 
6080 pathoswith 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
6151 pathoswith 2824
        sub     eax, [esi+sizeWithHeader]
6080 pathoswith 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
6151 pathoswith 2856
        mov     [edi], cl
6080 pathoswith 2857
.end:
2858
        ret
2859
 
2860
resizeAttribute:
2861
;   in:
2862
; [ebp+NTFS.frs_buffer] -> file record
6273 pathoswith 2863
; [ebp+NTFS.attr_offs] -> attribute
6080 pathoswith 2864
; edx:eax = new size
2865
;   out:
6107 pathoswith 2866
; [ebp+NTFS.fileDataSize] = clusters added (positive)
2867
; [ebp+NTFS.fileDataStart] = added block
6080 pathoswith 2868
; CF=1 -> eax = error code
6273 pathoswith 2869
        mov     esi, [ebp+NTFS.attr_offs]
2870
        mov     dword [ebp+NTFS.attr_size], eax
2871
        mov     dword [ebp+NTFS.attr_size+4], edx
6151 pathoswith 2872
        cmp     byte [esi+nonResidentFlag], 0
2873
        jz      .resident
6080 pathoswith 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
6151 pathoswith 2882
        jc      .makeResident
6080 pathoswith 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
6107 pathoswith 2893
        mov     [ebp+NTFS.fileDataSize], edi
6080 pathoswith 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
6820 pathoswith 2913
        cmp     edi, [ebp+NTFS.BitmapStart]
2914
        jnc     @f
6273 pathoswith 2915
        cmp     [ebp+NTFS.cur_iRecord], 0
6107 pathoswith 2916
        jz      @f
6820 pathoswith 2917
        mov     edi, [ebp+NTFS.BitmapStart]
6107 pathoswith 2918
@@:
6080 pathoswith 2919
        call    ntfsSpaceAlloc
2920
        jc      .err1
6292 pathoswith 2921
        mov     eax, [ebp+NTFS.fileDataStart]
6080 pathoswith 2922
        pop     edi
2923
        pop     edx
2924
        cmp     edx, eax
2925
        jnz     .newEntry
2926
        pop     edx
2927
        pop     edi
2928
        pop     [ebp+NTFS.fileDataStart]
2929
        mov     [esp], eax
2930
        push    [ebp+NTFS.fileDataSize]
2931
        add     [ebp+NTFS.fileDataSize], edx
2932
        jmp     @f
2933
 
2934
.newEntry:
2935
        add     esp, 12
2936
        pop     edx
2937
        push    eax
2938
        push    [ebp+NTFS.fileDataSize]
2939
        sub     eax, edx
2940
        mov     [ebp+NTFS.fileDataStart], eax
2941
@@:
6273 pathoswith 2942
        mov     esi, [ebp+NTFS.attr_offs]
6080 pathoswith 2943
        call    createMcbEntry
6292 pathoswith 2944
        pop     [ebp+NTFS.fileDataSize]
2945
        pop     [ebp+NTFS.fileDataStart]
2946
        movi    eax, ERROR_UNSUPPORTED_FS
6151 pathoswith 2947
.done:
2948
        ret
2949
 
6080 pathoswith 2950
.err1:
2951
        add     esp, 24
2952
        stc
6292 pathoswith 2953
.err2:
6151 pathoswith 2954
        movi    eax, ERROR_DISK_FULL
6080 pathoswith 2955
        ret
2956
 
6292 pathoswith 2957
.err3:
2958
        movi    eax, ERROR_FS_FAIL
2959
        add     esp, 20
2960
        stc
6080 pathoswith 2961
        ret
2962
 
2963
.shrinkAttribute:
2964
        add     ecx, edi
2965
        inc     ecx
2966
        add     esi, eax
2967
        xor     edi, edi
2968
        sub     esp, 20
2969
@@:
2970
        mov     [esp+16], esi
2971
        call    ntfs_decode_mcb_entry
2972
        jnc     .err3
2973
        add     edi, [esp+8]
2974
        sub     ecx, [esp]
2975
        jnc     @b
2976
        mov     ebx, ecx
2977
        add     ecx, [esp]
2978
        mov     eax, [esp+8]
2979
        mov     [ebp+NTFS.fileDataSize], ecx
2980
        mov     [ebp+NTFS.fileDataStart], eax
2981
        push    edi
2982
        add     edi, ecx
2983
        neg     ebx
2984
        call    ntfsSpaceFree
2985
        pop     edi
2986
        jc      .end
2987
@@:
2988
        call    ntfs_decode_mcb_entry
2989
        jnc     .end
2990
        cmp     dword[esp+8], 0
2991
        jz      @b
2992
        add     edi, [esp+8]
2993
        mov     ebx, [esp]
2994
        call    ntfsSpaceFree
2995
        jnc     @b
2996
.end:
2997
        add     esp, 16
2998
        pop     edi
2999
        cmp     [ebp+NTFS.fileDataSize], 0
3000
        jz      @f
6273 pathoswith 3001
        mov     esi, [ebp+NTFS.attr_offs]
6080 pathoswith 3002
        call    createMcbEntry
6107 pathoswith 3003
        mov     [ebp+NTFS.fileDataSize], 0
6080 pathoswith 3004
@@:
3005
        ret
3006
 
6151 pathoswith 3007
.resident:
3008
        test    edx, edx
3009
        jnz     .nonResident
3010
        cmp     eax, 8000h
3011
        jnc     .nonResident
3012
        add     ax, [esi+attributeOffset]
3013
        sub     eax, [esi+sizeWithHeader]
3014
        jc      @f
3015
        mov     edi, [ebp+NTFS.frs_buffer]
3016
        mov     ecx, eax
3017
        add     ecx, [edi+recordRealSize]
3018
        cmp     [edi+recordAllocatedSize], ecx
3019
        jc      .nonResident
3020
        add     eax, 7
3021
        and     eax, not 7
3022
        add     [edi+recordRealSize], eax
3023
        add     edi, [edi+recordRealSize]
3024
        add     [esi+sizeWithHeader], eax
3025
        add     esi, [esi+sizeWithHeader]
3026
        mov     ecx, edi
3027
        sub     ecx, esi
3028
        shr     ecx, 2
3029
        sub     edi, 4
3030
        mov     esi, edi
3031
        sub     esi, eax
3032
        std
3033
        rep movsd
3034
        mov     ecx, eax
3035
        shr     ecx, 2
3036
        xor     eax, eax
3037
        rep stosd
3038
        cld
6273 pathoswith 3039
        mov     esi, [ebp+NTFS.attr_offs]
6151 pathoswith 3040
@@:
6273 pathoswith 3041
        mov     eax, dword [ebp+NTFS.attr_size]
6151 pathoswith 3042
        mov     [esi+sizeWithoutHeader], eax
3043
        mov     [ebp+NTFS.fileDataSize], 0
3044
        clc
3045
        ret
3046
 
3047
.nonResident:   ; convert resident to non-resident
6273 pathoswith 3048
        mov     eax, dword [ebp+NTFS.attr_size]
6151 pathoswith 3049
        sub     eax, 1
3050
        sbb     edx, 0
3051
        mov     ecx, [ebp+NTFS.sectors_per_cluster]
3052
        shl     ecx, 9
3053
        div     ecx
3054
        inc     eax
3055
        mov     [ebp+NTFS.fileDataSize], eax
3056
        mov     edi, [ebp+NTFS.BitmapStart]
3057
        push    ecx
3058
        call    ntfsSpaceAlloc
3059
        pop     ecx
6292 pathoswith 3060
        jc      .err2
6273 pathoswith 3061
        mov     esi, [ebp+NTFS.attr_offs]
6151 pathoswith 3062
        xor     eax, eax
3063
        xor     edx, edx
3064
@@:
3065
        add     eax, ecx
3066
        inc     edx
3067
        cmp     eax, [esi+sizeWithoutHeader]
3068
        jc      @b
3069
        push    edx
3070
        push    eax
3071
        stdcall kernel_alloc, eax
3072
        mov     ecx, [esp]
3073
        shr     ecx, 2
3074
        mov     edi, eax
3075
        mov     ebx, eax
3076
        xor     eax, eax
3077
        rep stosd
3078
        mov     al, [esi+attributeOffset]
3079
        mov     ecx, [esi+sizeWithoutHeader]
3080
        add     esi, eax
3081
        mov     edi, ebx
3082
        rep movsb
3083
        mov     eax, [ebp+NTFS.fileDataStart]
3084
        mul     [ebp+NTFS.sectors_per_cluster]
3085
        pop     ecx
3086
        shr     ecx, 9
3087
        call    fs_write64_app
6292 pathoswith 3088
        stdcall kernel_free, ebx
6273 pathoswith 3089
        mov     esi, [ebp+NTFS.attr_offs]
6151 pathoswith 3090
        add     esi, [esi+sizeWithHeader]
3091
        mov     ecx, [ebp+NTFS.frs_buffer]
3092
        add     ecx, [ecx+recordRealSize]
3093
        sub     ecx, esi
3094
        shr     ecx, 2
6273 pathoswith 3095
        lea     edi, [ebp+NTFS.bitmap_buf]
6151 pathoswith 3096
        push    ecx
3097
        rep movsd
6273 pathoswith 3098
        mov     edi, [ebp+NTFS.attr_offs]
6151 pathoswith 3099
        add     edi, 16
3100
        mov     cl, 6
3101
        xor     eax, eax
3102
        rep stosd
6273 pathoswith 3103
        mov     edi, [ebp+NTFS.attr_offs]
6151 pathoswith 3104
        mov     eax, [ebp+NTFS.fileDataSize]
3105
        dec     eax
3106
        mov     [edi+lastVCN], eax
3107
        inc     eax
3108
        mov     ecx, [ebp+NTFS.sectors_per_cluster]
3109
        shl     ecx, 9
3110
        mul     ecx
3111
        mov     byte [edi+sizeWithHeader], 50h
3112
        mov     byte [edi+nonResidentFlag], 1
3113
        mov     byte [edi+dataRunsOffset], 40h
3114
        mov     [edi+attributeAllocatedSize], eax
3115
        mov     [edi+attributeAllocatedSize+4], edx
6273 pathoswith 3116
        mov     eax, dword [ebp+NTFS.attr_size]
3117
        mov     edx, dword [ebp+NTFS.attr_size+4]
6151 pathoswith 3118
        mov     [edi+attributeRealSize], eax
3119
        mov     [edi+attributeRealSize+4], edx
3120
        mov     [edi+initialDataSize], eax
3121
        mov     [edi+initialDataSize+4], edx
3122
        mov     esi, edi
3123
        add     edi, 40h
3124
        call    createMcbEntry
3125
        mov     eax, edi
6273 pathoswith 3126
        mov     edi, [ebp+NTFS.attr_offs]
6151 pathoswith 3127
        sub     eax, edi
3128
        add     eax, 8
3129
        and     eax, not 7
3130
        mov     [edi+sizeWithHeader], eax
3131
        pop     ecx
6273 pathoswith 3132
        lea     esi, [ebp+NTFS.bitmap_buf]
6151 pathoswith 3133
        add     edi, eax
3134
        rep movsd
3135
        mov     esi, [ebp+NTFS.frs_buffer]
3136
        sub     edi, esi
3137
        mov     [esi+recordRealSize], edi
3138
        pop     edx
3139
        sub     [ebp+NTFS.fileDataSize], edx
3140
        add     [ebp+NTFS.fileDataStart], edx
6292 pathoswith 3141
        ret
6151 pathoswith 3142
 
3143
.makeResident:  ; convert non-resident to empty resident
3144
        movzx   eax, byte [esi+dataRunsOffset]
3145
        mov     byte [esi+nonResidentFlag], 0
3146
        mov     dword [esi+sizeWithoutHeader], 0
3147
        mov     dword [esi+attributeOffset], 18h
3148
        add     esi, eax
3149
        xor     edi, edi
3150
        sub     esp, 16
3151
@@:
3152
        call    ntfs_decode_mcb_entry
3153
        jnc     @f
3154
        cmp     dword[esp+8], 0
3155
        jz      @b
3156
        add     edi, [esp+8]
3157
        mov     ebx, [esp]
3158
        call    ntfsSpaceFree
3159
        jnc     @b
3160
@@:
3161
        add     esp, 16
3162
        mov     [ebp+NTFS.fileDataSize], 0
3163
        ret
3164
 
6107 pathoswith 3165
ntfsSpaceClean:
3166
; clean up to 16 Mb of disk space
3167
;   in:
3168
; [ebp+NTFS.fileDataStart] = block to clean
3169
; [ebp+NTFS.fileDataSize] = block size
3170
        mov     eax, [ebp+NTFS.fileDataSize]
3171
        test    eax, eax
3172
        jz      @f
3173
        mul     [ebp+NTFS.sectors_per_cluster]
3174
        cmp     eax, 8001h
3175
        jnc     @f
3176
        push    eax
3177
        shl     eax, 9
3178
        stdcall kernel_alloc, eax
3179
        pop     ecx
3180
        test    eax, eax
3181
        jz      @f
3182
        push    ecx
3183
        shl     ecx, 7
3184
        mov     edi, eax
3185
        mov     ebx, eax
3186
        xor     eax, eax
3187
        rep stosd
3188
        mov     eax, [ebp+NTFS.fileDataStart]
3189
        mul     [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 3190
        mov     [ebp+NTFS.LastRead], eax
6107 pathoswith 3191
        pop     ecx
3192
        call    fs_write64_app
3193
        stdcall kernel_free, ebx
3194
@@:
3195
        ret
3196
 
6080 pathoswith 3197
ntfsSpaceAlloc:
6292 pathoswith 3198
; allocate disk space
6080 pathoswith 3199
;   in:
3200
; edi = offset in bitmap to start search from
3201
; [ebp+NTFS.fileDataSize] = block size in clusters
3202
;   out:
6292 pathoswith 3203
; [ebp+NTFS.fileDataStart] = allocated block starting cluster
6080 pathoswith 3204
; CF=1 -> disk full
3205
        mov     ecx, [ebp+NTFS.BitmapBuffer]
3206
        add     edi, ecx
3207
        add     ecx, [ebp+NTFS.BitmapSize]
3208
        sub     ecx, edi
6781 pathoswith 3209
        ja      @f
3210
        push    eax
6080 pathoswith 3211
        call    bitmapBuffering
6781 pathoswith 3212
        pop     eax
6080 pathoswith 3213
        shl     ecx, 2
3214
@@:
3215
        shr     ecx, 2
6781 pathoswith 3216
        push    ecx
6080 pathoswith 3217
        mov     eax, [ebp+NTFS.fileDataSize]
3218
        shr     eax, 5
3219
        jz      .small
6292 pathoswith 3220
        mov     ebx, eax    ; bitmap dwords
6080 pathoswith 3221
.start:
3222
        mov     ecx, [ebp+NTFS.BitmapBuffer]
3223
        add     ecx, [ebp+NTFS.BitmapSize]
3224
        sub     ecx, edi
3225
        shr     ecx, 2
3226
@@:
3227
        xor     eax, eax
3228
        repnz scasd         ; search for empty dword
3229
        jz      @f
3230
        call    bitmapBuffering
3231
        jmp     @b
3232
@@:
6292 pathoswith 3233
        cmp     ecx, ebx
6080 pathoswith 3234
        jnc     @f
3235
        call    bitmapBuffering
3236
        jmp     @b
3237
@@:
3238
        sub     edi, 4
6292 pathoswith 3239
        mov     ecx, ebx
6080 pathoswith 3240
        mov     esi, edi
3241
        xor     eax, eax
3242
        repz scasd          ; check following dwords
3243
        jnz     .start
3244
        sub     esi, 4
3245
        mov     eax, [esi]
3246
        xor     edx, edx
3247
        bsr     edx, eax
3248
        inc     edx
3249
        push    edx         ; starting bit
3250
        push    esi         ; starting dword
3251
        add     esi, 4
3252
        neg     edx
3253
        add     edx, 32
3254
        mov     eax, [ebp+NTFS.fileDataSize]
3255
        sub     eax, edx
3256
        mov     edx, eax
3257
        shr     eax, 5
3258
        shl     eax, 2
3259
        add     esi, eax
3260
        mov     eax, [esi]
3261
        bsf     ecx, eax    ; check last dword
3262
        jz      .done
3263
        and     edx, 31
3264
        cmp     ecx, edx
3265
        jnc     .done
3266
        add     esp, 8
3267
        jmp     .start
3268
 
3269
@@:
3270
        sub     edi, 4
6781 pathoswith 3271
        call    bitmapBuffering
3272
        push    ecx
3273
.small:     ; less than 32 clusters
3274
        pop     ecx
3275
        or      eax, -1
3276
        repz scasd
3277
        jecxz   @b
3278
        push    ecx
3279
        mov     eax, [edi-4]
6080 pathoswith 3280
        not     eax
3281
@@:
6781 pathoswith 3282
        bsf     ecx, eax    ; first 0
3283
        jz      .small
6080 pathoswith 3284
        not     eax
3285
        shr     eax, cl
3286
        shl     eax, cl
3287
        bsf     edx, eax    ; next 1
3288
        jz      @f
6781 pathoswith 3289
        sub     edx, ecx
6080 pathoswith 3290
        cmp     edx, [ebp+NTFS.fileDataSize]
3291
        jnc     .got        ; fits inside
6781 pathoswith 3292
        bsf     ecx, eax
6080 pathoswith 3293
        not     eax
3294
        shr     eax, cl
3295
        shl     eax, cl
3296
        jmp     @b
6781 pathoswith 3297
 
6080 pathoswith 3298
@@:         ; next dword
6781 pathoswith 3299
        mov     eax, [edi]
6080 pathoswith 3300
        bsf     edx, eax
3301
        jz      .got        ; empty
3302
        add     edx, 32
6781 pathoswith 3303
        sub     edx, ecx
6080 pathoswith 3304
        cmp     edx, [ebp+NTFS.fileDataSize]
6781 pathoswith 3305
        jc      .small
6080 pathoswith 3306
.got:
6781 pathoswith 3307
        sub     edi, 4
3308
        push    ecx         ; starting bit
6080 pathoswith 3309
        push    edi         ; starting dword
3310
.done:      ; mark space
6781 pathoswith 3311
        pop     edi ecx
6080 pathoswith 3312
        cmp     ecx, 32
3313
        jc      @f
3314
        xor     ecx, ecx
6781 pathoswith 3315
        add     edi, 4
6080 pathoswith 3316
@@:
6781 pathoswith 3317
        push    ecx edi
3318
        or      eax, -1
6080 pathoswith 3319
        shr     eax, cl
3320
        shl     eax, cl
3321
        neg     ecx
3322
        add     ecx, 32
3323
        sub     ecx, [ebp+NTFS.fileDataSize]
3324
        jc      @f
3325
        shl     eax, cl     ; fits inside dword
3326
        shr     eax, cl
3327
        or      [edi], eax
3328
        jmp     .end
3329
 
3330
@@:
3331
        or      [edi], eax
3332
        neg     ecx
3333
        push    ecx
3334
        shr     ecx, 5
3335
        add     edi, 4
6781 pathoswith 3336
        or      eax, -1
6080 pathoswith 3337
        rep stosd
3338
        pop     ecx
3339
        and     ecx, 31
3340
        shr     eax, cl
3341
        shl     eax, cl
3342
        not     eax
3343
        or      [edi], eax
3344
.end:
3345
        pop     eax
6292 pathoswith 3346
        pop     ecx
6080 pathoswith 3347
        sub     eax, [ebp+NTFS.BitmapBuffer]
3348
        shl     eax, 3
6292 pathoswith 3349
        add     eax, ecx
3350
        pop     ecx
3351
        mov     ecx, [ebp+NTFS.fileDataSize]
3352
        mov     [ebp+NTFS.fileDataStart], eax
3353
        add     ecx, eax
3354
        add     ecx, 4095
3355
        shr     ecx, 3+9
3356
        shr     eax, 3+9
3357
        sub     ecx, eax
3358
        mov     ebx, eax
3359
        shl     ebx, 9
3360
        add     eax, [ebp+NTFS.BitmapLocation]
3361
        add     ebx, [ebp+NTFS.BitmapBuffer]
3362
        xor     edx, edx
3363
        jmp     fs_write64_app
6080 pathoswith 3364
 
3365
ntfsSpaceFree:
3366
; free disk space
3367
;   in:
3368
; edi = starting cluster
3369
; ebx = size in clusters
3370
        mov     eax, edi
3371
        add     eax, ebx
3372
        shr     eax, 3
3373
        cmp     eax, [ebp+NTFS.BitmapSize]
3374
        jc      @f
3375
        add     eax, [ebp+NTFS.BitmapBuffer]
3376
        push    edi
3377
        mov     edi, eax
3378
        call    bitmapBuffering
3379
        pop     edi
3380
@@:
3381
        push    edi
3382
        mov     ecx, edi
3383
        shr     edi, 5
3384
        shl     edi, 2
3385
        add     edi, [ebp+NTFS.BitmapBuffer]
3386
        and     ecx, 31
3387
        xor     eax, eax
3388
        dec     eax
3389
        shr     eax, cl
3390
        shl     eax, cl
3391
        neg     ecx
3392
        add     ecx, 32
3393
        sub     ecx, ebx
3394
        jc      @f
3395
        shl     eax, cl     ; fits inside dword
3396
        shr     eax, cl
3397
        not     eax
3398
        and     [edi], eax
3399
        jmp     .writeBitmap
3400
 
3401
@@:
3402
        not     eax
3403
        and     [edi], eax
3404
        neg     ecx
3405
        push    ecx
3406
        shr     ecx, 5
3407
        add     edi, 4
3408
        xor     eax, eax
3409
        rep stosd
3410
        pop     ecx
3411
        and     ecx, 31
3412
        dec     eax
3413
        shr     eax, cl
3414
        shl     eax, cl
3415
        and     [edi], eax
3416
.writeBitmap:
3417
        pop     eax
3418
        mov     edi, eax
3419
        lea     ecx, [eax+ebx+4095]
3420
        shr     eax, 3+9
3421
        shr     ecx, 3+9
3422
        sub     ecx, eax
3423
        mov     ebx, eax
3424
        shl     ebx, 9
3425
        add     eax, [ebp+NTFS.BitmapLocation]
3426
        add     ebx, [ebp+NTFS.BitmapBuffer]
3427
        xor     edx, edx
3428
        jmp     fs_write64_app
3429
 
5954 pathoswith 3430
bitmapBuffering:
3431
; Extend BitmapBuffer and read next 32kb of bitmap
3432
; Warning: $Bitmap fragmentation is not foreseen
6080 pathoswith 3433
; in: edi -> position in bitmap buffer
3434
; out: ecx = number of buffered dwords left
5954 pathoswith 3435
        push    ebx
3436
        mov     eax, [ebp+NTFS.BitmapTotalSize]
3437
        cmp     eax, [ebp+NTFS.BitmapSize]
3438
        jz      .end
3439
        stdcall alloc_pages, 8
3440
        test    eax, eax
3441
        jz      .end
3442
        add     eax, 3
3443
        mov     ebx, [ebp+NTFS.BitmapBuffer]
3444
        add     ebx, [ebp+NTFS.BitmapSize]
3445
        push    ebx
3446
        mov     ecx, 8
3447
        call    commit_pages
3448
        mov     eax, [ebp+NTFS.BitmapSize]
3449
        shr     eax, 9
3450
        add     eax, [ebp+NTFS.BitmapLocation]
3451
        pop     ebx
3452
        mov     ecx, 64
3453
        xor     edx, edx
3454
        call    fs_read64_app
3455
        test    eax, eax
3456
        jnz     .err
6781 pathoswith 3457
        mov     eax, [ebp+NTFS.BitmapSize]
3458
        add     eax, 8000h
3459
        cmp     [ebp+NTFS.BitmapTotalSize], eax
3460
        jnc     @f
5954 pathoswith 3461
        mov     eax, [ebp+NTFS.BitmapTotalSize]
6781 pathoswith 3462
@@:
5954 pathoswith 3463
        mov     [ebp+NTFS.BitmapSize], eax
6080 pathoswith 3464
        pop     ebx
3465
        mov     ecx, [ebp+NTFS.BitmapBuffer]
6781 pathoswith 3466
        add     ecx, eax
6019 pathoswith 3467
        sub     ecx, edi
6781 pathoswith 3468
        jbe     bitmapBuffering
5954 pathoswith 3469
        shr     ecx, 2
3470
        ret
3471
 
3472
.err:
3473
        mov     eax, [ebp+NTFS.BitmapBuffer]
3474
        add     eax, [ebp+NTFS.BitmapSize]
3475
        mov     ecx, 8
3476
        call    release_pages
3477
.end:
6292 pathoswith 3478
        add     esp, 12     ; ret
6080 pathoswith 3479
        stc
3480
        ret
5954 pathoswith 3481
 
2288 clevermous 3482
;----------------------------------------------------------------
5994 pathoswith 3483
ntfs_WriteFile:
3484
        cmp     byte [esi], 0
3485
        jnz     @f
2288 clevermous 3486
        xor     ebx, ebx
5994 pathoswith 3487
        movi    eax, ERROR_ACCESS_DENIED
2288 clevermous 3488
        ret
5994 pathoswith 3489
@@:
3490
        call    ntfs_lock
6468 pathoswith 3491
        call    ntfs_find_lfn
6019 pathoswith 3492
        jc      ntfsNotFound
6273 pathoswith 3493
        cmp     [ebp+NTFS.cur_iRecord], 16
6019 pathoswith 3494
        jc      ntfsDenied
6420 pathoswith 3495
        test    dword [eax+fileFlags], 10000001h
3496
        jnz     ntfsDenied
6273 pathoswith 3497
        cmp     [ebp+NTFS.fragmentCount], 1
6080 pathoswith 3498
        jnz     ntfsUnsupported     ; record fragmented
6151 pathoswith 3499
; edit directory node
6080 pathoswith 3500
        mov     edi, [ebp+NTFS.cur_index_buf]
3501
        cmp     dword [edi], 'INDX'
3502
        jz      @f
3503
        mov     esi, [ebp+NTFS.frs_buffer]
3504
        mov     ecx, [esi+recordRealSize]
3505
        shr     ecx, 2
3506
        rep movsd
6273 pathoswith 3507
        mov     esi, [ebp+NTFS.attr_offs]
6080 pathoswith 3508
        mov     cl, [esi+attributeOffset]
3509
        sub     esi, [ebp+NTFS.frs_buffer]
6151 pathoswith 3510
        add     eax, ecx
3511
        add     eax, esi
6080 pathoswith 3512
@@:
6420 pathoswith 3513
        mov     edi, eax
3514
        mov     eax, [ebx+4]
6151 pathoswith 3515
        mov     edx, [ebx+8]
6420 pathoswith 3516
        add     eax, [ebx+12]
6151 pathoswith 3517
        adc     edx, 0
6420 pathoswith 3518
        mov     [edi+fileRealSize], eax
3519
        mov     [edi+fileRealSize+4], edx
3520
        push    edx eax ebx
3521
        call    ntfsGetTime
3522
        mov     [edi+fileModified], eax
3523
        mov     [edi+fileModified+4], edx
3524
        mov     [edi+recordModified], eax
3525
        mov     [edi+recordModified+4], edx
3526
        mov     [edi+fileAccessed], eax
3527
        mov     [edi+fileAccessed+4], edx
3528
        pop     ebx ecx edx
6273 pathoswith 3529
        mov     eax, [ebp+NTFS.LastRead]
6151 pathoswith 3530
        mov     [ebp+NTFS.nodeLastRead], eax
6273 pathoswith 3531
        mov     [ebp+NTFS.cur_attr], 0x80
3532
        mov     [ebp+NTFS.cur_offs], 0
3533
        mov     [ebp+NTFS.cur_size], 0
5994 pathoswith 3534
        call    ntfs_read_attr
6080 pathoswith 3535
        jc      ntfsFail
6420 pathoswith 3536
        mov     esi, edi
3537
        mov     edi, [ebp+NTFS.frs_buffer]
3538
        cmp     word [edi+baseRecordReuse], 0
3539
        jnz     ntfsUnsupported     ; auxiliary record
3540
        mov     al, [edi+attributeOffset]
3541
        add     edi, eax
3542
        mov     al, [edi+attributeOffset]
3543
        add     edi, eax
6151 pathoswith 3544
        mov     eax, ecx
6420 pathoswith 3545
        mov     ecx, 6
3546
        add     esi, fileModified
3547
        add     edi, 8
3548
        rep movsd
6273 pathoswith 3549
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 3550
        cmp     word [ecx+attributeFlags], 0
3551
        jnz     ntfsUnsupported
3552
        push    ebx
3553
        cmp     byte [ecx+nonResidentFlag], 0
3554
        jz      .resizeAttribute
5994 pathoswith 3555
        cmp     edx, [ecx+attributeRealSize+4]
6080 pathoswith 3556
        jc      .writeNode
3557
        jnz     .resizeAttribute
5994 pathoswith 3558
        cmp     [ecx+attributeRealSize], eax
6080 pathoswith 3559
        jnc     .writeNode
3560
.resizeAttribute:
3561
        call    resizeAttribute
6107 pathoswith 3562
        jc      ntfsErrorPop
6273 pathoswith 3563
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 3564
        cmp     byte [ecx+nonResidentFlag], 1
3565
        jz      @f
3566
        mov     ebx, [esp]
3567
        movzx   edi, byte [ecx+attributeOffset]
3568
        add     edi, ecx
3569
        add     edi, [ebx+4]
3570
        mov     ecx, [ebx+12]
3571
        mov     esi, [ebx+16]
3572
        rep movsb
3573
@@:
6408 pathoswith 3574
        mov     ebx, [ebp+NTFS.frs_buffer]
3575
        mov     edx, [ebp+NTFS.mftLastRead]
6080 pathoswith 3576
        call    writeRecord     ; file
3577
        call    ntfs_restore_usa_frs
3578
.writeNode:
6408 pathoswith 3579
        mov     ebx, [ebp+NTFS.cur_index_buf]
3580
        mov     edx, [ebp+NTFS.nodeLastRead]
6080 pathoswith 3581
        call    writeRecord     ; directory
3582
        pop     ebx
6273 pathoswith 3583
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 3584
        cmp     byte [ecx+nonResidentFlag], 0
3585
        jz      .done
3586
        mov     ecx, [ebx+12]
3587
        test    ecx, ecx
3588
        jz      .done
5994 pathoswith 3589
        mov     eax, [ebx+4]
3590
        mov     edx, [ebx+8]
3591
        mov     esi, [ebx+16]
3592
        shrd    eax, edx, 9
3593
        test    dword[ebx+4], 1FFh
3594
        jz      .aligned
6273 pathoswith 3595
        mov     [ebp+NTFS.cur_offs], eax
3596
        mov     [ebp+NTFS.cur_size], 1
3597
        lea     edi, [ebp+NTFS.bitmap_buf]
3598
        mov     [ebp+NTFS.cur_buf], edi
5994 pathoswith 3599
        call    ntfs_read_attr.continue
6019 pathoswith 3600
        jc      ntfsDevice
5994 pathoswith 3601
        mov     eax, [ebx+4]
3602
        and     eax, 1FFh
3603
        add     edi, eax
6273 pathoswith 3604
        sub     eax, [ebp+NTFS.cur_read]
5994 pathoswith 3605
        neg     eax
3606
        push    ecx
3607
        cmp     ecx, eax
3608
        jb      @f
3609
        mov     ecx, eax
3610
@@:
3611
        sub     [esp], ecx
3612
        rep movsb
3613
        push    ebx
6273 pathoswith 3614
        mov     eax, [ebp+NTFS.LastRead]
3615
        lea     ebx, [ebp+NTFS.bitmap_buf]
5994 pathoswith 3616
        mov     ecx, 1
3617
        xor     edx, edx
3618
        call    fs_write64_app
3619
        pop     ebx
3620
        pop     ecx
3621
        test    ecx, ecx
6151 pathoswith 3622
        jz      .done
5994 pathoswith 3623
        mov     eax, [ebx+4]
3624
        mov     edx, [ebx+8]
3625
        shrd    eax, edx, 9
3626
        inc     eax
3627
.aligned:
3628
        push    ecx
3629
        shr     ecx, 9
6273 pathoswith 3630
        mov     [ebp+NTFS.cur_offs], eax
3631
        mov     [ebp+NTFS.cur_size], ecx
3632
        mov     [ebp+NTFS.cur_buf], esi
5994 pathoswith 3633
        add     eax, ecx
3634
        push    eax
6273 pathoswith 3635
        mov     [ebp+NTFS.bWriteAttr], 1
5994 pathoswith 3636
        call    ntfs_read_attr.continue
6273 pathoswith 3637
        mov     [ebp+NTFS.bWriteAttr], 0
3638
        pop     [ebp+NTFS.cur_offs]
5994 pathoswith 3639
        pop     ecx
6019 pathoswith 3640
        jc      ntfsDevice
5994 pathoswith 3641
        and     ecx, 1FFh
6151 pathoswith 3642
        jz      .done
6273 pathoswith 3643
        add     esi, [ebp+NTFS.cur_read]
3644
        mov     [ebp+NTFS.cur_size], 1
3645
        lea     edi, [ebp+NTFS.bitmap_buf]
3646
        mov     [ebp+NTFS.cur_buf], edi
5994 pathoswith 3647
        call    ntfs_read_attr.continue
6019 pathoswith 3648
        jc      ntfsDevice
5994 pathoswith 3649
        rep movsb
3650
        push    ebx
6273 pathoswith 3651
        mov     eax, [ebp+NTFS.LastRead]
3652
        lea     ebx, [ebp+NTFS.bitmap_buf]
5994 pathoswith 3653
        mov     ecx, 1
3654
        xor     edx, edx
3655
        call    fs_write64_app
3656
        pop     ebx
6151 pathoswith 3657
.done:
5994 pathoswith 3658
        mov     ebx, [ebx+12]
6019 pathoswith 3659
        jmp     ntfsDone
3660
 
3661
;----------------------------------------------------------------
3662
ntfs_Delete:
3663
        cmp     byte [esi], 0
3664
        jnz     @f
6418 pathoswith 3665
        xor     ebx, ebx
6019 pathoswith 3666
        movi    eax, ERROR_ACCESS_DENIED
5994 pathoswith 3667
        ret
6401 pathoswith 3668
 
6019 pathoswith 3669
@@:
3670
        call    ntfs_lock
6468 pathoswith 3671
        call    ntfs_find_lfn
6019 pathoswith 3672
        jc      ntfsNotFound
6273 pathoswith 3673
        cmp     [ebp+NTFS.cur_iRecord], 16
6019 pathoswith 3674
        jc      ntfsDenied
6420 pathoswith 3675
        test    byte [eax+fileFlags], 1
3676
        jnz     ntfsDenied
6273 pathoswith 3677
        cmp     [ebp+NTFS.fragmentCount], 1
6019 pathoswith 3678
        jnz     ntfsUnsupported     ; record fragmented
6418 pathoswith 3679
        mov     ebx, [eax+directoryRecordReference]
3680
        mov     [ebp+NTFS.newRecord], ebx
3681
        mov     bx, [eax+fileReferenceReuse]
3682
        mov     [ebp+NTFS.indexPointer], esi
3683
        mov     eax, [ebp+NTFS.cur_iRecord]
3684
        shr     eax, 3
3685
        cmp     eax, [ebp+NTFS.mftBitmapSize]
6019 pathoswith 3686
        jnc     ntfsUnsupported
6418 pathoswith 3687
; examine file record
3688
        mov     [ebp+NTFS.cur_attr], 0x80   ; file?
3689
        mov     [ebp+NTFS.cur_offs], 0
3690
        mov     [ebp+NTFS.cur_size], 0
3691
        call    ntfs_read_attr
3692
        jnc     @f
3693
        xor     eax, eax
3694
        push    ebx eax eax eax eax
3695
        mov     [esp+12], esp
3696
        push    eax
3697
        mov     ebx, esp
3698
        mov     [ebp+NTFS.cur_attr], 0x90   ; folder?
3699
        call    ntfs_ReadFolder.doit
3700
        mov     edx, [esp+12]
3701
        add     esp, 20
3702
        pop     ebx
3703
        test    eax, eax
3704
        jnz     .ret
3705
        cmp     edx, 2
3706
        jnz     ntfsDenied      ; folder is not empty
6401 pathoswith 3707
        mov     [ebp+NTFS.cur_attr], 0xA0
6418 pathoswith 3708
        mov     [ebp+NTFS.cur_offs], 0
3709
        mov     [ebp+NTFS.cur_size], 0
6401 pathoswith 3710
        call    ntfs_read_attr.newAttribute
6418 pathoswith 3711
        jc      .deleteFileRecord
6401 pathoswith 3712
@@:
6418 pathoswith 3713
        mov     esi, [ebp+NTFS.frs_buffer]
3714
        cmp     word [esi+baseRecordReuse], 0
3715
        jnz     ntfsUnsupported     ; auxiliary record
3716
        cmp     word [esi+reuseCounter], bx
3717
        jnz     .backToIndex        ; broken index
6420 pathoswith 3718
        test    byte [esi+recordFlags], 1
6418 pathoswith 3719
        jz      .writeBitmapMFT     ; record deleted
3720
        cmp     byte [esi+hardLinkCounter], 3
3721
        jnc     ntfsUnsupported
3722
        mov     esi, [ebp+NTFS.attr_offs]
3723
        cmp     byte [esi+nonResidentFlag], 0
3724
        jz      .deleteFileRecord
3725
        movzx   eax, byte [esi+dataRunsOffset]
6401 pathoswith 3726
        add     esi, eax
6418 pathoswith 3727
        xor     edi, edi
3728
        sub     esp, 16
3729
@@:
3730
        call    ntfs_decode_mcb_entry
3731
        jnc     @f
3732
        cmp     dword[esp+8], 0
6401 pathoswith 3733
        jz      @b
6418 pathoswith 3734
        add     edi, [esp+8]
3735
        mov     ebx, [esp]
3736
        call    ntfsSpaceFree
3737
        jnc     @b
3738
@@:
3739
        add     esp, 16
3740
.deleteFileRecord:
3741
        mov     ebx, [ebp+NTFS.frs_buffer]
3742
        mov     byte [ebx+recordFlags], 0
3743
        mov     edx, [ebp+NTFS.mftLastRead]
3744
        call    writeRecord
3745
.writeBitmapMFT:
3746
        mov     eax, [ebp+NTFS.cur_iRecord]
3747
        mov     ecx, eax
3748
        shr     eax, 3
3749
        and     ecx, 7
3750
        mov     edi, [ebp+NTFS.mftBitmapBuffer]
3751
        btr     [edi+eax], ecx
3752
        shr     eax, 9
3753
        mov     ebx, eax
3754
        shl     ebx, 9
3755
        add     eax, [ebp+NTFS.mftBitmapLocation]
3756
        add     ebx, edi
3757
        mov     ecx, 1
3758
        xor     edx, edx
3759
        call    fs_write64_sys
3760
.backToIndex:
3761
        mov     eax, [ebp+NTFS.newRecord]
3762
        mov     [ebp+NTFS.cur_iRecord], eax
3763
        mov     esi, [ebp+NTFS.indexPointer]
6468 pathoswith 3764
        call    ntfs_find_lfn.doit2
6418 pathoswith 3765
        jc      ntfsFail
3766
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
3767
        mov     byte [ebx], 0
3768
        mov     ebx, [ebp+NTFS.LastRead]
3769
        mov     [ebp+NTFS.nodeLastRead], ebx
3770
        xor     ebx, ebx
3771
        test    byte [eax+indexFlags], 1
3772
        jz      .deleteIndex    ; no subnode
3773
        mov     edi, eax
3774
        call    .findSubindex
3775
        jc      ntfsFail
6401 pathoswith 3776
        movzx   edx, word [edi+indexAllocatedSize]
6418 pathoswith 3777
        test    esi, esi
3778
        jz      @f
6401 pathoswith 3779
        sub     edx, eax
3780
        sub     edx, 8
6418 pathoswith 3781
@@:
3782
        mov     eax, edi
6401 pathoswith 3783
        mov     ebx, esi
3784
        jmp     @f
3785
 
3786
.deleteIndex:
3787
        movzx   edx, word [eax+indexAllocatedSize]
6107 pathoswith 3788
        mov     ecx, [eax+fileRecordReference]
3789
        cmp     [eax+edx+fileRecordReference], ecx
3790
        jnz     @f
3791
        add     dx, [eax+edx+indexAllocatedSize]
3792
@@:
6019 pathoswith 3793
        mov     edi, [ebp+NTFS.cur_index_buf]
3794
        cmp     dword [edi], 'INDX'
3795
        jz      .indexRecord
6418 pathoswith 3796
        sub     eax, edi
6401 pathoswith 3797
        mov     edi, [ebp+NTFS.indexRoot]
6019 pathoswith 3798
        sub     [edi+sizeWithHeader], edx
3799
        sub     [edi+sizeWithoutHeader], edx
6418 pathoswith 3800
        movzx   ecx, byte [edi+attributeOffset]
6019 pathoswith 3801
        add     edi, ecx
6418 pathoswith 3802
        add     eax, edi
6273 pathoswith 3803
        sub     [edi+rootNode+nodeRealSize], edx
3804
        sub     [edi+rootNode+nodeAllocatedSize], edx
6418 pathoswith 3805
        mov     edi, [ebp+NTFS.frs_buffer]
3806
        sub     [edi+recordRealSize], edx
3807
        mov     ecx, [edi+recordRealSize]
3808
        cmp     [edi+recordAllocatedSize], ecx
6019 pathoswith 3809
        jmp     @f
2288 clevermous 3810
 
6019 pathoswith 3811
.indexRecord:
6401 pathoswith 3812
        add     edi, recordNode
3813
        sub     [edi+nodeRealSize], edx
3814
        mov     ecx, [edi+nodeRealSize]
3815
        cmp     [edi+nodeAllocatedSize], ecx
6418 pathoswith 3816
@@:
6401 pathoswith 3817
        jc      ntfsUnsupported
6418 pathoswith 3818
        add     ecx, edi
6019 pathoswith 3819
        sub     ecx, eax
3820
        mov     esi, eax
3821
        add     esi, edx
3822
        mov     edi, eax
6401 pathoswith 3823
        test    edx, edx
3824
        jns     @f
3825
        neg     edx
3826
        add     edx, ecx
3827
        sub     edx, 4
3828
        add     esi, edx
3829
        add     edi, edx
3830
        std
3831
@@:
3832
        jz      @f
3833
        shr     ecx, 2
6019 pathoswith 3834
        rep movsd
6401 pathoswith 3835
        cld
3836
@@:
3837
        test    ebx, ebx
6418 pathoswith 3838
        jz      .done
6401 pathoswith 3839
; copy index from the subnode to replace deleted pointing index
3840
        movzx   ecx, word [ebx+indexAllocatedSize]
3841
        mov     edx, ecx
6418 pathoswith 3842
        test    byte [ebx+indexFlags], 1
3843
        jz      @f
3844
        sub     ecx, 8
3845
        movzx   edi, word [ebx+edx+indexAllocatedSize]
3846
        add     edi, edx
3847
        mov     esi, [ebx+ecx]
3848
        mov     [ebx+edi-8], esi
3849
        mov     [ebx+indexAllocatedSize], cx
3850
@@:
6401 pathoswith 3851
        shr     ecx, 2
3852
        mov     esi, ebx
3853
        mov     edi, eax
3854
        rep movsd
3855
        add     word [eax+indexAllocatedSize], 8
3856
        mov     byte [eax+indexFlags], 1
6418 pathoswith 3857
        mov     edi, [ebp+NTFS.secondIndexBuffer]
6401 pathoswith 3858
        mov     eax, ebx
3859
        xor     ebx, ebx
3860
        jmp     .indexRecord
3861
 
6418 pathoswith 3862
.done:
6408 pathoswith 3863
        mov     ebx, [ebp+NTFS.frs_buffer]
6418 pathoswith 3864
        mov     edx, [ebp+NTFS.rootLastRead]
6019 pathoswith 3865
        call    writeRecord
6408 pathoswith 3866
        mov     ebx, [ebp+NTFS.cur_index_buf]
6418 pathoswith 3867
        cmp     dword [ebx], 'INDX'
3868
        jnz     @f
6408 pathoswith 3869
        mov     edx, [ebp+NTFS.nodeLastRead]
6401 pathoswith 3870
        call    writeRecord
6418 pathoswith 3871
@@:
6408 pathoswith 3872
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
3873
        cmp     byte [ebx], 0
6401 pathoswith 3874
        jz      ntfsDone
6418 pathoswith 3875
        mov     edx, [ebp+NTFS.LastRead]
6019 pathoswith 3876
        call    writeRecord
6080 pathoswith 3877
        jmp     ntfsDone
6019 pathoswith 3878
 
6418 pathoswith 3879
.findSubindex:
3880
; in: eax -> index
3881
;   out:
3882
; CF=1 -> error
3883
; esi=0 -> subnode deleted
3884
; esi -> replacement index
3885
; eax = index effective size
3886
        movzx   edx, word [eax+indexAllocatedSize]
3887
        mov     eax, [eax+edx-8]
3888
        mov     edx, [ebp+NTFS.cur_size]
3889
        push    edx
3890
        cmp     edx, [ebp+NTFS.cur_subnode_size]
3891
        jz      @f
3892
        mul     [ebp+NTFS.sectors_per_cluster]
3893
@@:
3894
        mov     [ebp+NTFS.cur_attr], 0xA0
3895
        mov     [ebp+NTFS.cur_offs], eax
3896
        push    eax
3897
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
3898
        mov     esi, ebx
3899
        mov     [ebp+NTFS.cur_buf], ebx
3900
        call    ntfs_read_attr.newAttribute
3901
        pop     [ebp+NTFS.cur_offs]
3902
        pop     eax
3903
        jc      .ret
3904
        cmp     dword [esi], 'INDX'
3905
        stc
3906
        jnz     .ret
3907
        mov     [ebp+NTFS.cur_size], eax
3908
        shl     eax, 9
3909
        call    ntfs_restore_usa
3910
        jc      .ret
3911
        add     esi, recordNode
3912
        add     esi, [esi+indexOffset]
3913
        test    byte [esi+indexFlags], 2
3914
        jnz     .emptyNode
3915
        cmp     [ebp+NTFS.fragmentCount], 1
3916
        stc
3917
        jnz     .ret    ; record fragmented
3918
        xor     eax, eax
3919
@@:
3920
        add     esi, eax
3921
        mov     ax, [esi+indexAllocatedSize]
3922
        test    byte [esi+eax+indexFlags], 2
3923
        jz      @b
3924
        test    byte [esi+indexFlags], 1
3925
        jz      .ret
3926
        add     eax, esi
3927
        push    esi
3928
        push    [ebp+NTFS.cur_offs]
3929
        call    .findSubindex
3930
        pop     [ebp+NTFS.cur_offs]
3931
        pop     edx
3932
        jc      .ret
3933
        test    esi, esi
3934
        jnz     .ret
3935
        mov     esi, edx
3936
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
3937
        mov     [ebp+NTFS.cur_buf], ebx
3938
        push    [ebp+NTFS.cur_size]
3939
        call    ntfs_read_attr.continue
3940
        pop     eax
3941
        jc      .ret
3942
        shl     eax, 9
3943
        call    ntfs_restore_usa
3944
        jc      .ret
3945
        movzx   eax, word [esi+indexAllocatedSize]
3946
        sub     eax, 8
3947
.ret:
3948
        ret
3949
 
3950
.emptyNode:
3951
        test    byte [esi+indexFlags], 1
3952
        jz      @f
3953
        mov     eax, esi
3954
        push    [ebp+NTFS.cur_offs]
3955
        call    .findSubindex
3956
        pop     [ebp+NTFS.cur_offs]
3957
        jc      .ret
3958
        test    esi, esi
3959
        jnz     .ret
3960
@@:         ; delete node
3961
        mov     esi, [ebp+NTFS.attr_offs]
3962
        add     esi, [esi+sizeWithHeader]
3963
        cmp     byte [esi], 0xB0
3964
        stc
3965
        jnz     .ret
3966
        movzx   eax, byte [esi+attributeOffset]
3967
        add     esi, eax
3968
        mov     eax, [ebp+NTFS.cur_offs]
3969
        xor     edx, edx
3970
        div     [ebp+NTFS.cur_size]
3971
        mov     edx, eax
3972
        shr     eax, 3
3973
        and     edx, 7
3974
        btr     [esi+eax], edx
3975
        mov     esi, [ebp+NTFS.secondIndexBuffer]
3976
        mov     byte [esi], 0
3977
        xor     esi, esi
3978
        ret
3979
 
5954 pathoswith 3980
;----------------------------------------------------------------
3742 clevermous 3981
ntfs_SetFileEnd:
6107 pathoswith 3982
        cmp     byte [esi], 0
3983
        jnz     @f
3984
        xor     ebx, ebx
3985
        movi    eax, ERROR_ACCESS_DENIED
3986
        ret
3987
@@:
3988
        call    ntfs_lock
6468 pathoswith 3989
        call    ntfs_find_lfn
6107 pathoswith 3990
        jc      ntfsNotFound
6273 pathoswith 3991
        cmp     [ebp+NTFS.cur_iRecord], 16
6107 pathoswith 3992
        jc      ntfsDenied
6420 pathoswith 3993
        test    dword [eax+fileFlags], 10000001h
3994
        jnz     ntfsDenied
6273 pathoswith 3995
        cmp     [ebp+NTFS.fragmentCount], 1
6107 pathoswith 3996
        jnz     ntfsUnsupported     ; record fragmented
6151 pathoswith 3997
; edit directory node
6107 pathoswith 3998
        mov     edi, [ebp+NTFS.cur_index_buf]
3999
        cmp     dword [edi], 'INDX'
4000
        jz      @f
4001
        mov     esi, [ebp+NTFS.frs_buffer]
4002
        mov     ecx, [esi+recordRealSize]
4003
        shr     ecx, 2
4004
        rep movsd
6273 pathoswith 4005
        mov     esi, [ebp+NTFS.attr_offs]
6107 pathoswith 4006
        mov     cl, [esi+attributeOffset]
4007
        sub     esi, [ebp+NTFS.frs_buffer]
4008
        add     eax, ecx
4009
        add     eax, esi
4010
@@:
6420 pathoswith 4011
        mov     edi, eax
4012
        mov     eax, [ebx+4]
6107 pathoswith 4013
        mov     edx, [ebx+8]
6420 pathoswith 4014
        mov     [edi+fileRealSize], eax
4015
        mov     [edi+fileRealSize+4], edx
4016
        push    edx eax ebx
4017
        call    ntfsGetTime
4018
        mov     [edi+fileModified], eax
4019
        mov     [edi+fileModified+4], edx
4020
        mov     [edi+recordModified], eax
4021
        mov     [edi+recordModified+4], edx
4022
        mov     [edi+fileAccessed], eax
4023
        mov     [edi+fileAccessed+4], edx
4024
        pop     ebx ecx edx
6273 pathoswith 4025
        mov     eax, [ebp+NTFS.LastRead]
6107 pathoswith 4026
        mov     [ebp+NTFS.nodeLastRead], eax
6273 pathoswith 4027
        mov     [ebp+NTFS.cur_attr], 0x80
4028
        mov     [ebp+NTFS.cur_offs], 0
4029
        mov     [ebp+NTFS.cur_size], 0
6107 pathoswith 4030
        call    ntfs_read_attr
4031
        jc      ntfsFail
6420 pathoswith 4032
        mov     esi, edi
4033
        mov     edi, [ebp+NTFS.frs_buffer]
4034
        cmp     word [edi+baseRecordReuse], 0
4035
        jnz     ntfsUnsupported     ; auxiliary record
4036
        mov     al, [edi+attributeOffset]
4037
        add     edi, eax
4038
        mov     al, [edi+attributeOffset]
4039
        add     edi, eax
6107 pathoswith 4040
        mov     eax, ecx
6420 pathoswith 4041
        mov     ecx, 6
4042
        add     esi, fileModified
4043
        add     edi, 8
4044
        rep movsd
6273 pathoswith 4045
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 4046
        cmp     word [ecx+attributeFlags], 0
4047
        jnz     ntfsUnsupported
4048
        cmp     byte [ecx+nonResidentFlag], 0
4049
        jz      .resizeAttribute
6107 pathoswith 4050
        cmp     [ecx+attributeRealSize+4], edx
4051
        jnz     .resizeAttribute
4052
        cmp     [ecx+attributeRealSize], eax
4053
        jnc     .resizeAttribute
4054
        mov     eax, [ecx+attributeRealSize]
4055
        mov     ecx, [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 4056
        mov     [ebp+NTFS.cur_size], ecx
6107 pathoswith 4057
        shl     ecx, 9
4058
        div     ecx
4059
        test    edx, edx
4060
        jz      .aligned
4061
        push    edx
4062
        push    ecx
4063
        mul     [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 4064
        mov     [ebp+NTFS.cur_offs], eax
6107 pathoswith 4065
        stdcall kernel_alloc, ecx
4066
        pop     ecx
4067
        pop     edi
6405 pathoswith 4068
        mov     esi, eax
6107 pathoswith 4069
        sub     ecx, edi
4070
        add     edi, eax
6273 pathoswith 4071
        mov     [ebp+NTFS.cur_buf], eax
6107 pathoswith 4072
        call    ntfs_read_attr.continue
4073
        jc      @f
4074
        xor     eax, eax
4075
        rep stosb
4076
        push    ebx
6273 pathoswith 4077
        mov     eax, [ebp+NTFS.LastRead]
6405 pathoswith 4078
        mov     ebx, esi
6107 pathoswith 4079
        mov     ecx, [ebp+NTFS.sectors_per_cluster]
4080
        xor     edx, edx
4081
        call    fs_write64_app
4082
        pop     ebx
4083
@@:
6405 pathoswith 4084
        stdcall kernel_free, esi
6107 pathoswith 4085
.aligned:
4086
        mov     eax, [ebx+4]
4087
        mov     edx, [ebx+8]
4088
.resizeAttribute:
4089
        call    resizeAttribute
4090
        jc      ntfsError
6408 pathoswith 4091
        mov     ebx, [ebp+NTFS.frs_buffer]
4092
        mov     edx, [ebp+NTFS.mftLastRead]
6107 pathoswith 4093
        call    writeRecord     ; file
6408 pathoswith 4094
        mov     ebx, [ebp+NTFS.cur_index_buf]
4095
        mov     edx, [ebp+NTFS.nodeLastRead]
6107 pathoswith 4096
        call    writeRecord     ; directory
4097
        call    ntfsSpaceClean
4098
        jmp     ntfsDone
4099
 
6462 pathoswith 4100
ntfsGetTime:
4101
        call    fsGetTime
4102
        jmp     @f
2288 clevermous 4103
 
6420 pathoswith 4104
ntfsCalculateTime:
4105
; in: esi -> data block
6462 pathoswith 4106
; out: edx:eax = seconds since 01.01.1601 x10000000
4107
        call    fsCalculateTime
6420 pathoswith 4108
@@:
6462 pathoswith 4109
        mov     edx, 10000000
6420 pathoswith 4110
        mul     edx
6462 pathoswith 4111
        add     eax, 3365781504
4112
        adc     edx, 29389701
6420 pathoswith 4113
        ret
4114
 
3742 clevermous 4115
;----------------------------------------------------------------
6420 pathoswith 4116
ntfs_SetFileInfo:
2288 clevermous 4117
        cmp     byte [esi], 0
4118
        jnz     @f
5994 pathoswith 4119
        movi    eax, ERROR_UNSUPPORTED_FS
2288 clevermous 4120
        ret
4121
@@:
3742 clevermous 4122
        call    ntfs_lock
6468 pathoswith 4123
        call    ntfs_find_lfn
6420 pathoswith 4124
        jnc     @f
3742 clevermous 4125
        test    eax, eax
6019 pathoswith 4126
        jz      ntfsFail
4127
        jmp     ntfsNotFound
2288 clevermous 4128
 
6420 pathoswith 4129
@@:
4130
        cmp     [ebp+NTFS.fragmentCount], 1
4131
        jnz     ntfsUnsupported     ; record fragmented
4132
        mov     esi, [ebp+NTFS.cur_index_buf]
4133
        cmp     dword [esi], 'INDX'
4134
        jz      @f
4135
        sub     eax, esi
4136
        mov     esi, [ebp+NTFS.indexRoot]
4137
        movzx   edx, byte [esi+attributeOffset]
4138
        add     eax, esi
4139
        add     eax, edx
4140
@@:
4141
        mov     esi, [ebx+16]
4142
        mov     edi, eax
4143
        mov     eax, [esi]
4144
        and     eax, 27h
4145
        and     byte [edi+fileFlags], -28h
4146
        or      [edi+fileFlags], al
4147
        add     esi, 8
4148
        call    ntfsCalculateTime
4149
        mov     [edi+fileCreated], eax
4150
        mov     [edi+fileCreated+4], edx
4151
        add     esi, 8
4152
        call    ntfsCalculateTime
4153
        mov     [edi+fileAccessed], eax
4154
        mov     [edi+fileAccessed+4], edx
4155
        add     esi, 8
4156
        call    ntfsCalculateTime
4157
        mov     [edi+fileModified], eax
4158
        mov     [edi+fileModified+4], edx
4159
        mov     ebx, [ebp+NTFS.cur_index_buf]
4160
        cmp     dword [ebx], 'INDX'
4161
        jz      @f
4162
        mov     ebx, [ebp+NTFS.frs_buffer]
4163
@@:
4164
        mov     edx, [ebp+NTFS.LastRead]
4165
        call    writeRecord
4166
        jmp     ntfsDone
4167
 
6019 pathoswith 4168
ntfsUnsupported:
4169
        push    ERROR_UNSUPPORTED_FS
6080 pathoswith 4170
        jmp     ntfsOut
6019 pathoswith 4171
ntfsDevice:
4172
        push    ERROR_DEVICE
4173
        jmp     ntfsOut
4174
ntfsNotFound:
4175
        push    ERROR_FILE_NOT_FOUND
4176
        jmp     ntfsOut
4177
ntfsDenied:
4178
        push    ERROR_ACCESS_DENIED
4179
        jmp     ntfsOut
4180
ntfsFail:
4181
        push    ERROR_FS_FAIL
4182
        jmp     ntfsOut
6080 pathoswith 4183
ntfsDiskFull:
4184
        push    ERROR_DISK_FULL
4185
        jmp     ntfsOut
6414 pathoswith 4186
ntfsErrorPop5:
4187
        pop     ebx
4188
        pop     ebx
6340 pathoswith 4189
ntfsErrorPop3:
4190
        pop     ebx
6107 pathoswith 4191
ntfsErrorPop2:
4192
        pop     ebx
4193
ntfsErrorPop:
4194
        pop     ebx
6080 pathoswith 4195
ntfsError:
4196
        push    eax
4197
ntfsOut:
4198
        call    ntfs_unlock
4199
        xor     ebx, ebx
4200
        pop     eax
4201
        ret