Subversion Repositories Kolibri OS

Rev

Rev 6781 | Rev 6820 | 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: 6798 $
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
6273 pathoswith 2913
        cmp     [ebp+NTFS.cur_iRecord], 0
6107 pathoswith 2914
        jz      @f
6080 pathoswith 2915
        cmp     edi, [ebp+NTFS.BitmapStart]
2916
        jc      .err1
6107 pathoswith 2917
@@:
6080 pathoswith 2918
        call    ntfsSpaceAlloc
2919
        jc      .err1
6292 pathoswith 2920
        mov     eax, [ebp+NTFS.fileDataStart]
6080 pathoswith 2921
        pop     edi
2922
        pop     edx
2923
        cmp     edx, eax
2924
        jnz     .newEntry
2925
        pop     edx
2926
        pop     edi
2927
        pop     [ebp+NTFS.fileDataStart]
2928
        mov     [esp], eax
2929
        push    [ebp+NTFS.fileDataSize]
2930
        add     [ebp+NTFS.fileDataSize], edx
2931
        jmp     @f
2932
 
2933
.newEntry:
2934
        add     esp, 12
2935
        pop     edx
2936
        push    eax
2937
        push    [ebp+NTFS.fileDataSize]
2938
        sub     eax, edx
2939
        mov     [ebp+NTFS.fileDataStart], eax
2940
@@:
6273 pathoswith 2941
        mov     esi, [ebp+NTFS.attr_offs]
6080 pathoswith 2942
        call    createMcbEntry
6292 pathoswith 2943
        pop     [ebp+NTFS.fileDataSize]
2944
        pop     [ebp+NTFS.fileDataStart]
2945
        movi    eax, ERROR_UNSUPPORTED_FS
6151 pathoswith 2946
.done:
2947
        ret
2948
 
6080 pathoswith 2949
.err1:
2950
        add     esp, 24
2951
        stc
6292 pathoswith 2952
.err2:
6151 pathoswith 2953
        movi    eax, ERROR_DISK_FULL
6080 pathoswith 2954
        ret
2955
 
6292 pathoswith 2956
.err3:
2957
        movi    eax, ERROR_FS_FAIL
2958
        add     esp, 20
2959
        stc
6080 pathoswith 2960
        ret
2961
 
2962
.shrinkAttribute:
2963
        add     ecx, edi
2964
        inc     ecx
2965
        add     esi, eax
2966
        xor     edi, edi
2967
        sub     esp, 20
2968
@@:
2969
        mov     [esp+16], esi
2970
        call    ntfs_decode_mcb_entry
2971
        jnc     .err3
2972
        add     edi, [esp+8]
2973
        sub     ecx, [esp]
2974
        jnc     @b
2975
        mov     ebx, ecx
2976
        add     ecx, [esp]
2977
        mov     eax, [esp+8]
2978
        mov     [ebp+NTFS.fileDataSize], ecx
2979
        mov     [ebp+NTFS.fileDataStart], eax
2980
        push    edi
2981
        add     edi, ecx
2982
        neg     ebx
2983
        call    ntfsSpaceFree
2984
        pop     edi
2985
        jc      .end
2986
@@:
2987
        call    ntfs_decode_mcb_entry
2988
        jnc     .end
2989
        cmp     dword[esp+8], 0
2990
        jz      @b
2991
        add     edi, [esp+8]
2992
        mov     ebx, [esp]
2993
        call    ntfsSpaceFree
2994
        jnc     @b
2995
.end:
2996
        add     esp, 16
2997
        pop     edi
2998
        cmp     [ebp+NTFS.fileDataSize], 0
2999
        jz      @f
6273 pathoswith 3000
        mov     esi, [ebp+NTFS.attr_offs]
6080 pathoswith 3001
        call    createMcbEntry
6107 pathoswith 3002
        mov     [ebp+NTFS.fileDataSize], 0
6080 pathoswith 3003
@@:
3004
        ret
3005
 
6151 pathoswith 3006
.resident:
3007
        test    edx, edx
3008
        jnz     .nonResident
3009
        cmp     eax, 8000h
3010
        jnc     .nonResident
3011
        add     ax, [esi+attributeOffset]
3012
        sub     eax, [esi+sizeWithHeader]
3013
        jc      @f
3014
        mov     edi, [ebp+NTFS.frs_buffer]
3015
        mov     ecx, eax
3016
        add     ecx, [edi+recordRealSize]
3017
        cmp     [edi+recordAllocatedSize], ecx
3018
        jc      .nonResident
3019
        add     eax, 7
3020
        and     eax, not 7
3021
        add     [edi+recordRealSize], eax
3022
        add     edi, [edi+recordRealSize]
3023
        add     [esi+sizeWithHeader], eax
3024
        add     esi, [esi+sizeWithHeader]
3025
        mov     ecx, edi
3026
        sub     ecx, esi
3027
        shr     ecx, 2
3028
        sub     edi, 4
3029
        mov     esi, edi
3030
        sub     esi, eax
3031
        std
3032
        rep movsd
3033
        mov     ecx, eax
3034
        shr     ecx, 2
3035
        xor     eax, eax
3036
        rep stosd
3037
        cld
6273 pathoswith 3038
        mov     esi, [ebp+NTFS.attr_offs]
6151 pathoswith 3039
@@:
6273 pathoswith 3040
        mov     eax, dword [ebp+NTFS.attr_size]
6151 pathoswith 3041
        mov     [esi+sizeWithoutHeader], eax
3042
        mov     [ebp+NTFS.fileDataSize], 0
3043
        clc
3044
        ret
3045
 
3046
.nonResident:   ; convert resident to non-resident
6273 pathoswith 3047
        mov     eax, dword [ebp+NTFS.attr_size]
6151 pathoswith 3048
        sub     eax, 1
3049
        sbb     edx, 0
3050
        mov     ecx, [ebp+NTFS.sectors_per_cluster]
3051
        shl     ecx, 9
3052
        div     ecx
3053
        inc     eax
3054
        mov     [ebp+NTFS.fileDataSize], eax
3055
        mov     edi, [ebp+NTFS.BitmapStart]
3056
        push    ecx
3057
        call    ntfsSpaceAlloc
3058
        pop     ecx
6292 pathoswith 3059
        jc      .err2
6273 pathoswith 3060
        mov     esi, [ebp+NTFS.attr_offs]
6151 pathoswith 3061
        xor     eax, eax
3062
        xor     edx, edx
3063
@@:
3064
        add     eax, ecx
3065
        inc     edx
3066
        cmp     eax, [esi+sizeWithoutHeader]
3067
        jc      @b
3068
        push    edx
3069
        push    eax
3070
        stdcall kernel_alloc, eax
3071
        mov     ecx, [esp]
3072
        shr     ecx, 2
3073
        mov     edi, eax
3074
        mov     ebx, eax
3075
        xor     eax, eax
3076
        rep stosd
3077
        mov     al, [esi+attributeOffset]
3078
        mov     ecx, [esi+sizeWithoutHeader]
3079
        add     esi, eax
3080
        mov     edi, ebx
3081
        rep movsb
3082
        mov     eax, [ebp+NTFS.fileDataStart]
3083
        mul     [ebp+NTFS.sectors_per_cluster]
3084
        pop     ecx
3085
        shr     ecx, 9
3086
        call    fs_write64_app
6292 pathoswith 3087
        stdcall kernel_free, ebx
6273 pathoswith 3088
        mov     esi, [ebp+NTFS.attr_offs]
6151 pathoswith 3089
        add     esi, [esi+sizeWithHeader]
3090
        mov     ecx, [ebp+NTFS.frs_buffer]
3091
        add     ecx, [ecx+recordRealSize]
3092
        sub     ecx, esi
3093
        shr     ecx, 2
6273 pathoswith 3094
        lea     edi, [ebp+NTFS.bitmap_buf]
6151 pathoswith 3095
        push    ecx
3096
        rep movsd
6273 pathoswith 3097
        mov     edi, [ebp+NTFS.attr_offs]
6151 pathoswith 3098
        add     edi, 16
3099
        mov     cl, 6
3100
        xor     eax, eax
3101
        rep stosd
6273 pathoswith 3102
        mov     edi, [ebp+NTFS.attr_offs]
6151 pathoswith 3103
        mov     eax, [ebp+NTFS.fileDataSize]
3104
        dec     eax
3105
        mov     [edi+lastVCN], eax
3106
        inc     eax
3107
        mov     ecx, [ebp+NTFS.sectors_per_cluster]
3108
        shl     ecx, 9
3109
        mul     ecx
3110
        mov     byte [edi+sizeWithHeader], 50h
3111
        mov     byte [edi+nonResidentFlag], 1
3112
        mov     byte [edi+dataRunsOffset], 40h
3113
        mov     [edi+attributeAllocatedSize], eax
3114
        mov     [edi+attributeAllocatedSize+4], edx
6273 pathoswith 3115
        mov     eax, dword [ebp+NTFS.attr_size]
3116
        mov     edx, dword [ebp+NTFS.attr_size+4]
6151 pathoswith 3117
        mov     [edi+attributeRealSize], eax
3118
        mov     [edi+attributeRealSize+4], edx
3119
        mov     [edi+initialDataSize], eax
3120
        mov     [edi+initialDataSize+4], edx
3121
        mov     esi, edi
3122
        add     edi, 40h
3123
        call    createMcbEntry
3124
        mov     eax, edi
6273 pathoswith 3125
        mov     edi, [ebp+NTFS.attr_offs]
6151 pathoswith 3126
        sub     eax, edi
3127
        add     eax, 8
3128
        and     eax, not 7
3129
        mov     [edi+sizeWithHeader], eax
3130
        pop     ecx
6273 pathoswith 3131
        lea     esi, [ebp+NTFS.bitmap_buf]
6151 pathoswith 3132
        add     edi, eax
3133
        rep movsd
3134
        mov     esi, [ebp+NTFS.frs_buffer]
3135
        sub     edi, esi
3136
        mov     [esi+recordRealSize], edi
3137
        pop     edx
3138
        sub     [ebp+NTFS.fileDataSize], edx
3139
        add     [ebp+NTFS.fileDataStart], edx
6292 pathoswith 3140
        ret
6151 pathoswith 3141
 
3142
.makeResident:  ; convert non-resident to empty resident
3143
        movzx   eax, byte [esi+dataRunsOffset]
3144
        mov     byte [esi+nonResidentFlag], 0
3145
        mov     dword [esi+sizeWithoutHeader], 0
3146
        mov     dword [esi+attributeOffset], 18h
3147
        add     esi, eax
3148
        xor     edi, edi
3149
        sub     esp, 16
3150
@@:
3151
        call    ntfs_decode_mcb_entry
3152
        jnc     @f
3153
        cmp     dword[esp+8], 0
3154
        jz      @b
3155
        add     edi, [esp+8]
3156
        mov     ebx, [esp]
3157
        call    ntfsSpaceFree
3158
        jnc     @b
3159
@@:
3160
        add     esp, 16
3161
        mov     [ebp+NTFS.fileDataSize], 0
3162
        ret
3163
 
6107 pathoswith 3164
ntfsSpaceClean:
3165
; clean up to 16 Mb of disk space
3166
;   in:
3167
; [ebp+NTFS.fileDataStart] = block to clean
3168
; [ebp+NTFS.fileDataSize] = block size
3169
        mov     eax, [ebp+NTFS.fileDataSize]
3170
        test    eax, eax
3171
        jz      @f
3172
        mul     [ebp+NTFS.sectors_per_cluster]
3173
        cmp     eax, 8001h
3174
        jnc     @f
3175
        push    eax
3176
        shl     eax, 9
3177
        stdcall kernel_alloc, eax
3178
        pop     ecx
3179
        test    eax, eax
3180
        jz      @f
3181
        push    ecx
3182
        shl     ecx, 7
3183
        mov     edi, eax
3184
        mov     ebx, eax
3185
        xor     eax, eax
3186
        rep stosd
3187
        mov     eax, [ebp+NTFS.fileDataStart]
3188
        mul     [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 3189
        mov     [ebp+NTFS.LastRead], eax
6107 pathoswith 3190
        pop     ecx
3191
        call    fs_write64_app
3192
        stdcall kernel_free, ebx
3193
@@:
3194
        ret
3195
 
6080 pathoswith 3196
ntfsSpaceAlloc:
6292 pathoswith 3197
; allocate disk space
6080 pathoswith 3198
;   in:
3199
; edi = offset in bitmap to start search from
3200
; [ebp+NTFS.fileDataSize] = block size in clusters
3201
;   out:
6292 pathoswith 3202
; [ebp+NTFS.fileDataStart] = allocated block starting cluster
6080 pathoswith 3203
; CF=1 -> disk full
3204
        mov     ecx, [ebp+NTFS.BitmapBuffer]
3205
        add     edi, ecx
3206
        add     ecx, [ebp+NTFS.BitmapSize]
3207
        sub     ecx, edi
6781 pathoswith 3208
        ja      @f
3209
        push    eax
6080 pathoswith 3210
        call    bitmapBuffering
6781 pathoswith 3211
        pop     eax
6080 pathoswith 3212
        shl     ecx, 2
3213
@@:
3214
        shr     ecx, 2
6781 pathoswith 3215
        push    ecx
6080 pathoswith 3216
        mov     eax, [ebp+NTFS.fileDataSize]
3217
        shr     eax, 5
3218
        jz      .small
6292 pathoswith 3219
        mov     ebx, eax    ; bitmap dwords
6080 pathoswith 3220
.start:
3221
        mov     ecx, [ebp+NTFS.BitmapBuffer]
3222
        add     ecx, [ebp+NTFS.BitmapSize]
3223
        sub     ecx, edi
3224
        shr     ecx, 2
3225
@@:
3226
        xor     eax, eax
3227
        repnz scasd         ; search for empty dword
3228
        jz      @f
3229
        call    bitmapBuffering
3230
        jmp     @b
3231
@@:
6292 pathoswith 3232
        cmp     ecx, ebx
6080 pathoswith 3233
        jnc     @f
3234
        call    bitmapBuffering
3235
        jmp     @b
3236
@@:
3237
        sub     edi, 4
6292 pathoswith 3238
        mov     ecx, ebx
6080 pathoswith 3239
        mov     esi, edi
3240
        xor     eax, eax
3241
        repz scasd          ; check following dwords
3242
        jnz     .start
3243
        sub     esi, 4
3244
        mov     eax, [esi]
3245
        xor     edx, edx
3246
        bsr     edx, eax
3247
        inc     edx
3248
        push    edx         ; starting bit
3249
        push    esi         ; starting dword
3250
        add     esi, 4
3251
        neg     edx
3252
        add     edx, 32
3253
        mov     eax, [ebp+NTFS.fileDataSize]
3254
        sub     eax, edx
3255
        mov     edx, eax
3256
        shr     eax, 5
3257
        shl     eax, 2
3258
        add     esi, eax
3259
        mov     eax, [esi]
3260
        bsf     ecx, eax    ; check last dword
3261
        jz      .done
3262
        and     edx, 31
3263
        cmp     ecx, edx
3264
        jnc     .done
3265
        add     esp, 8
3266
        jmp     .start
3267
 
3268
@@:
3269
        sub     edi, 4
6781 pathoswith 3270
        call    bitmapBuffering
3271
        push    ecx
3272
.small:     ; less than 32 clusters
3273
        pop     ecx
3274
        or      eax, -1
3275
        repz scasd
3276
        jecxz   @b
3277
        push    ecx
3278
        mov     eax, [edi-4]
6080 pathoswith 3279
        not     eax
3280
@@:
6781 pathoswith 3281
        bsf     ecx, eax    ; first 0
3282
        jz      .small
6080 pathoswith 3283
        not     eax
3284
        shr     eax, cl
3285
        shl     eax, cl
3286
        bsf     edx, eax    ; next 1
3287
        jz      @f
6781 pathoswith 3288
        sub     edx, ecx
6080 pathoswith 3289
        cmp     edx, [ebp+NTFS.fileDataSize]
3290
        jnc     .got        ; fits inside
6781 pathoswith 3291
        bsf     ecx, eax
6080 pathoswith 3292
        not     eax
3293
        shr     eax, cl
3294
        shl     eax, cl
3295
        jmp     @b
6781 pathoswith 3296
 
6080 pathoswith 3297
@@:         ; next dword
6781 pathoswith 3298
        mov     eax, [edi]
6080 pathoswith 3299
        bsf     edx, eax
3300
        jz      .got        ; empty
3301
        add     edx, 32
6781 pathoswith 3302
        sub     edx, ecx
6080 pathoswith 3303
        cmp     edx, [ebp+NTFS.fileDataSize]
6781 pathoswith 3304
        jc      .small
6080 pathoswith 3305
.got:
6781 pathoswith 3306
        sub     edi, 4
3307
        push    ecx         ; starting bit
6080 pathoswith 3308
        push    edi         ; starting dword
3309
.done:      ; mark space
6781 pathoswith 3310
        pop     edi ecx
6080 pathoswith 3311
        cmp     ecx, 32
3312
        jc      @f
3313
        xor     ecx, ecx
6781 pathoswith 3314
        add     edi, 4
6080 pathoswith 3315
@@:
6781 pathoswith 3316
        push    ecx edi
3317
        or      eax, -1
6080 pathoswith 3318
        shr     eax, cl
3319
        shl     eax, cl
3320
        neg     ecx
3321
        add     ecx, 32
3322
        sub     ecx, [ebp+NTFS.fileDataSize]
3323
        jc      @f
3324
        shl     eax, cl     ; fits inside dword
3325
        shr     eax, cl
3326
        or      [edi], eax
3327
        jmp     .end
3328
 
3329
@@:
3330
        or      [edi], eax
3331
        neg     ecx
3332
        push    ecx
3333
        shr     ecx, 5
3334
        add     edi, 4
6781 pathoswith 3335
        or      eax, -1
6080 pathoswith 3336
        rep stosd
3337
        pop     ecx
3338
        and     ecx, 31
3339
        shr     eax, cl
3340
        shl     eax, cl
3341
        not     eax
3342
        or      [edi], eax
3343
.end:
3344
        pop     eax
6292 pathoswith 3345
        pop     ecx
6080 pathoswith 3346
        sub     eax, [ebp+NTFS.BitmapBuffer]
3347
        shl     eax, 3
6292 pathoswith 3348
        add     eax, ecx
3349
        pop     ecx
3350
        mov     ecx, [ebp+NTFS.fileDataSize]
3351
        mov     [ebp+NTFS.fileDataStart], eax
3352
        add     ecx, eax
3353
        add     ecx, 4095
3354
        shr     ecx, 3+9
3355
        shr     eax, 3+9
3356
        sub     ecx, eax
3357
        mov     ebx, eax
3358
        shl     ebx, 9
3359
        add     eax, [ebp+NTFS.BitmapLocation]
3360
        add     ebx, [ebp+NTFS.BitmapBuffer]
3361
        xor     edx, edx
3362
        jmp     fs_write64_app
6080 pathoswith 3363
 
3364
ntfsSpaceFree:
3365
; free disk space
3366
;   in:
3367
; edi = starting cluster
3368
; ebx = size in clusters
3369
        mov     eax, edi
3370
        add     eax, ebx
3371
        shr     eax, 3
3372
        cmp     eax, [ebp+NTFS.BitmapSize]
3373
        jc      @f
3374
        add     eax, [ebp+NTFS.BitmapBuffer]
3375
        push    edi
3376
        mov     edi, eax
3377
        call    bitmapBuffering
3378
        pop     edi
3379
@@:
3380
        push    edi
3381
        mov     ecx, edi
3382
        shr     edi, 5
3383
        shl     edi, 2
3384
        add     edi, [ebp+NTFS.BitmapBuffer]
3385
        and     ecx, 31
3386
        xor     eax, eax
3387
        dec     eax
3388
        shr     eax, cl
3389
        shl     eax, cl
3390
        neg     ecx
3391
        add     ecx, 32
3392
        sub     ecx, ebx
3393
        jc      @f
3394
        shl     eax, cl     ; fits inside dword
3395
        shr     eax, cl
3396
        not     eax
3397
        and     [edi], eax
3398
        jmp     .writeBitmap
3399
 
3400
@@:
3401
        not     eax
3402
        and     [edi], eax
3403
        neg     ecx
3404
        push    ecx
3405
        shr     ecx, 5
3406
        add     edi, 4
3407
        xor     eax, eax
3408
        rep stosd
3409
        pop     ecx
3410
        and     ecx, 31
3411
        dec     eax
3412
        shr     eax, cl
3413
        shl     eax, cl
3414
        and     [edi], eax
3415
.writeBitmap:
3416
        pop     eax
3417
        mov     edi, eax
3418
        lea     ecx, [eax+ebx+4095]
3419
        shr     eax, 3+9
3420
        shr     ecx, 3+9
3421
        sub     ecx, eax
3422
        mov     ebx, eax
3423
        shl     ebx, 9
3424
        add     eax, [ebp+NTFS.BitmapLocation]
3425
        add     ebx, [ebp+NTFS.BitmapBuffer]
3426
        xor     edx, edx
3427
        jmp     fs_write64_app
3428
 
5954 pathoswith 3429
bitmapBuffering:
3430
; Extend BitmapBuffer and read next 32kb of bitmap
3431
; Warning: $Bitmap fragmentation is not foreseen
6080 pathoswith 3432
; in: edi -> position in bitmap buffer
3433
; out: ecx = number of buffered dwords left
5954 pathoswith 3434
        push    ebx
3435
        mov     eax, [ebp+NTFS.BitmapTotalSize]
3436
        cmp     eax, [ebp+NTFS.BitmapSize]
3437
        jz      .end
3438
        stdcall alloc_pages, 8
3439
        test    eax, eax
3440
        jz      .end
3441
        add     eax, 3
3442
        mov     ebx, [ebp+NTFS.BitmapBuffer]
3443
        add     ebx, [ebp+NTFS.BitmapSize]
3444
        push    ebx
3445
        mov     ecx, 8
3446
        call    commit_pages
3447
        mov     eax, [ebp+NTFS.BitmapSize]
3448
        shr     eax, 9
3449
        add     eax, [ebp+NTFS.BitmapLocation]
3450
        pop     ebx
3451
        mov     ecx, 64
3452
        xor     edx, edx
3453
        call    fs_read64_app
3454
        test    eax, eax
3455
        jnz     .err
6781 pathoswith 3456
        mov     eax, [ebp+NTFS.BitmapSize]
3457
        add     eax, 8000h
3458
        cmp     [ebp+NTFS.BitmapTotalSize], eax
3459
        jnc     @f
5954 pathoswith 3460
        mov     eax, [ebp+NTFS.BitmapTotalSize]
6781 pathoswith 3461
@@:
5954 pathoswith 3462
        mov     [ebp+NTFS.BitmapSize], eax
6080 pathoswith 3463
        pop     ebx
3464
        mov     ecx, [ebp+NTFS.BitmapBuffer]
6781 pathoswith 3465
        add     ecx, eax
6019 pathoswith 3466
        sub     ecx, edi
6781 pathoswith 3467
        jbe     bitmapBuffering
5954 pathoswith 3468
        shr     ecx, 2
3469
        ret
3470
 
3471
.err:
3472
        mov     eax, [ebp+NTFS.BitmapBuffer]
3473
        add     eax, [ebp+NTFS.BitmapSize]
3474
        mov     ecx, 8
3475
        call    release_pages
3476
.end:
6292 pathoswith 3477
        add     esp, 12     ; ret
6080 pathoswith 3478
        stc
3479
        ret
5954 pathoswith 3480
 
2288 clevermous 3481
;----------------------------------------------------------------
5994 pathoswith 3482
ntfs_WriteFile:
3483
        cmp     byte [esi], 0
3484
        jnz     @f
2288 clevermous 3485
        xor     ebx, ebx
5994 pathoswith 3486
        movi    eax, ERROR_ACCESS_DENIED
2288 clevermous 3487
        ret
5994 pathoswith 3488
@@:
3489
        call    ntfs_lock
6468 pathoswith 3490
        call    ntfs_find_lfn
6019 pathoswith 3491
        jc      ntfsNotFound
6273 pathoswith 3492
        cmp     [ebp+NTFS.cur_iRecord], 16
6019 pathoswith 3493
        jc      ntfsDenied
6420 pathoswith 3494
        test    dword [eax+fileFlags], 10000001h
3495
        jnz     ntfsDenied
6273 pathoswith 3496
        cmp     [ebp+NTFS.fragmentCount], 1
6080 pathoswith 3497
        jnz     ntfsUnsupported     ; record fragmented
6151 pathoswith 3498
; edit directory node
6080 pathoswith 3499
        mov     edi, [ebp+NTFS.cur_index_buf]
3500
        cmp     dword [edi], 'INDX'
3501
        jz      @f
3502
        mov     esi, [ebp+NTFS.frs_buffer]
3503
        mov     ecx, [esi+recordRealSize]
3504
        shr     ecx, 2
3505
        rep movsd
6273 pathoswith 3506
        mov     esi, [ebp+NTFS.attr_offs]
6080 pathoswith 3507
        mov     cl, [esi+attributeOffset]
3508
        sub     esi, [ebp+NTFS.frs_buffer]
6151 pathoswith 3509
        add     eax, ecx
3510
        add     eax, esi
6080 pathoswith 3511
@@:
6420 pathoswith 3512
        mov     edi, eax
3513
        mov     eax, [ebx+4]
6151 pathoswith 3514
        mov     edx, [ebx+8]
6420 pathoswith 3515
        add     eax, [ebx+12]
6151 pathoswith 3516
        adc     edx, 0
6420 pathoswith 3517
        mov     [edi+fileRealSize], eax
3518
        mov     [edi+fileRealSize+4], edx
3519
        push    edx eax ebx
3520
        call    ntfsGetTime
3521
        mov     [edi+fileModified], eax
3522
        mov     [edi+fileModified+4], edx
3523
        mov     [edi+recordModified], eax
3524
        mov     [edi+recordModified+4], edx
3525
        mov     [edi+fileAccessed], eax
3526
        mov     [edi+fileAccessed+4], edx
3527
        pop     ebx ecx edx
6273 pathoswith 3528
        mov     eax, [ebp+NTFS.LastRead]
6151 pathoswith 3529
        mov     [ebp+NTFS.nodeLastRead], eax
6273 pathoswith 3530
        mov     [ebp+NTFS.cur_attr], 0x80
3531
        mov     [ebp+NTFS.cur_offs], 0
3532
        mov     [ebp+NTFS.cur_size], 0
5994 pathoswith 3533
        call    ntfs_read_attr
6080 pathoswith 3534
        jc      ntfsFail
6420 pathoswith 3535
        mov     esi, edi
3536
        mov     edi, [ebp+NTFS.frs_buffer]
3537
        cmp     word [edi+baseRecordReuse], 0
3538
        jnz     ntfsUnsupported     ; auxiliary record
3539
        mov     al, [edi+attributeOffset]
3540
        add     edi, eax
3541
        mov     al, [edi+attributeOffset]
3542
        add     edi, eax
6151 pathoswith 3543
        mov     eax, ecx
6420 pathoswith 3544
        mov     ecx, 6
3545
        add     esi, fileModified
3546
        add     edi, 8
3547
        rep movsd
6273 pathoswith 3548
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 3549
        cmp     word [ecx+attributeFlags], 0
3550
        jnz     ntfsUnsupported
3551
        push    ebx
3552
        cmp     byte [ecx+nonResidentFlag], 0
3553
        jz      .resizeAttribute
5994 pathoswith 3554
        cmp     edx, [ecx+attributeRealSize+4]
6080 pathoswith 3555
        jc      .writeNode
3556
        jnz     .resizeAttribute
5994 pathoswith 3557
        cmp     [ecx+attributeRealSize], eax
6080 pathoswith 3558
        jnc     .writeNode
3559
.resizeAttribute:
3560
        call    resizeAttribute
6107 pathoswith 3561
        jc      ntfsErrorPop
6273 pathoswith 3562
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 3563
        cmp     byte [ecx+nonResidentFlag], 1
3564
        jz      @f
3565
        mov     ebx, [esp]
3566
        movzx   edi, byte [ecx+attributeOffset]
3567
        add     edi, ecx
3568
        add     edi, [ebx+4]
3569
        mov     ecx, [ebx+12]
3570
        mov     esi, [ebx+16]
3571
        rep movsb
3572
@@:
6408 pathoswith 3573
        mov     ebx, [ebp+NTFS.frs_buffer]
3574
        mov     edx, [ebp+NTFS.mftLastRead]
6080 pathoswith 3575
        call    writeRecord     ; file
3576
        call    ntfs_restore_usa_frs
3577
.writeNode:
6408 pathoswith 3578
        mov     ebx, [ebp+NTFS.cur_index_buf]
3579
        mov     edx, [ebp+NTFS.nodeLastRead]
6080 pathoswith 3580
        call    writeRecord     ; directory
3581
        pop     ebx
6273 pathoswith 3582
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 3583
        cmp     byte [ecx+nonResidentFlag], 0
3584
        jz      .done
3585
        mov     ecx, [ebx+12]
3586
        test    ecx, ecx
3587
        jz      .done
5994 pathoswith 3588
        mov     eax, [ebx+4]
3589
        mov     edx, [ebx+8]
3590
        mov     esi, [ebx+16]
3591
        shrd    eax, edx, 9
3592
        test    dword[ebx+4], 1FFh
3593
        jz      .aligned
6273 pathoswith 3594
        mov     [ebp+NTFS.cur_offs], eax
3595
        mov     [ebp+NTFS.cur_size], 1
3596
        lea     edi, [ebp+NTFS.bitmap_buf]
3597
        mov     [ebp+NTFS.cur_buf], edi
5994 pathoswith 3598
        call    ntfs_read_attr.continue
6019 pathoswith 3599
        jc      ntfsDevice
5994 pathoswith 3600
        mov     eax, [ebx+4]
3601
        and     eax, 1FFh
3602
        add     edi, eax
6273 pathoswith 3603
        sub     eax, [ebp+NTFS.cur_read]
5994 pathoswith 3604
        neg     eax
3605
        push    ecx
3606
        cmp     ecx, eax
3607
        jb      @f
3608
        mov     ecx, eax
3609
@@:
3610
        sub     [esp], ecx
3611
        rep movsb
3612
        push    ebx
6273 pathoswith 3613
        mov     eax, [ebp+NTFS.LastRead]
3614
        lea     ebx, [ebp+NTFS.bitmap_buf]
5994 pathoswith 3615
        mov     ecx, 1
3616
        xor     edx, edx
3617
        call    fs_write64_app
3618
        pop     ebx
3619
        pop     ecx
3620
        test    ecx, ecx
6151 pathoswith 3621
        jz      .done
5994 pathoswith 3622
        mov     eax, [ebx+4]
3623
        mov     edx, [ebx+8]
3624
        shrd    eax, edx, 9
3625
        inc     eax
3626
.aligned:
3627
        push    ecx
3628
        shr     ecx, 9
6273 pathoswith 3629
        mov     [ebp+NTFS.cur_offs], eax
3630
        mov     [ebp+NTFS.cur_size], ecx
3631
        mov     [ebp+NTFS.cur_buf], esi
5994 pathoswith 3632
        add     eax, ecx
3633
        push    eax
6273 pathoswith 3634
        mov     [ebp+NTFS.bWriteAttr], 1
5994 pathoswith 3635
        call    ntfs_read_attr.continue
6273 pathoswith 3636
        mov     [ebp+NTFS.bWriteAttr], 0
3637
        pop     [ebp+NTFS.cur_offs]
5994 pathoswith 3638
        pop     ecx
6019 pathoswith 3639
        jc      ntfsDevice
5994 pathoswith 3640
        and     ecx, 1FFh
6151 pathoswith 3641
        jz      .done
6273 pathoswith 3642
        add     esi, [ebp+NTFS.cur_read]
3643
        mov     [ebp+NTFS.cur_size], 1
3644
        lea     edi, [ebp+NTFS.bitmap_buf]
3645
        mov     [ebp+NTFS.cur_buf], edi
5994 pathoswith 3646
        call    ntfs_read_attr.continue
6019 pathoswith 3647
        jc      ntfsDevice
5994 pathoswith 3648
        rep movsb
3649
        push    ebx
6273 pathoswith 3650
        mov     eax, [ebp+NTFS.LastRead]
3651
        lea     ebx, [ebp+NTFS.bitmap_buf]
5994 pathoswith 3652
        mov     ecx, 1
3653
        xor     edx, edx
3654
        call    fs_write64_app
3655
        pop     ebx
6151 pathoswith 3656
.done:
5994 pathoswith 3657
        mov     ebx, [ebx+12]
6019 pathoswith 3658
        jmp     ntfsDone
3659
 
3660
;----------------------------------------------------------------
3661
ntfs_Delete:
3662
        cmp     byte [esi], 0
3663
        jnz     @f
6418 pathoswith 3664
        xor     ebx, ebx
6019 pathoswith 3665
        movi    eax, ERROR_ACCESS_DENIED
5994 pathoswith 3666
        ret
6401 pathoswith 3667
 
6019 pathoswith 3668
@@:
3669
        call    ntfs_lock
6468 pathoswith 3670
        call    ntfs_find_lfn
6019 pathoswith 3671
        jc      ntfsNotFound
6273 pathoswith 3672
        cmp     [ebp+NTFS.cur_iRecord], 16
6019 pathoswith 3673
        jc      ntfsDenied
6420 pathoswith 3674
        test    byte [eax+fileFlags], 1
3675
        jnz     ntfsDenied
6273 pathoswith 3676
        cmp     [ebp+NTFS.fragmentCount], 1
6019 pathoswith 3677
        jnz     ntfsUnsupported     ; record fragmented
6418 pathoswith 3678
        mov     ebx, [eax+directoryRecordReference]
3679
        mov     [ebp+NTFS.newRecord], ebx
3680
        mov     bx, [eax+fileReferenceReuse]
3681
        mov     [ebp+NTFS.indexPointer], esi
3682
        mov     eax, [ebp+NTFS.cur_iRecord]
3683
        shr     eax, 3
3684
        cmp     eax, [ebp+NTFS.mftBitmapSize]
6019 pathoswith 3685
        jnc     ntfsUnsupported
6418 pathoswith 3686
; examine file record
3687
        mov     [ebp+NTFS.cur_attr], 0x80   ; file?
3688
        mov     [ebp+NTFS.cur_offs], 0
3689
        mov     [ebp+NTFS.cur_size], 0
3690
        call    ntfs_read_attr
3691
        jnc     @f
3692
        xor     eax, eax
3693
        push    ebx eax eax eax eax
3694
        mov     [esp+12], esp
3695
        push    eax
3696
        mov     ebx, esp
3697
        mov     [ebp+NTFS.cur_attr], 0x90   ; folder?
3698
        call    ntfs_ReadFolder.doit
3699
        mov     edx, [esp+12]
3700
        add     esp, 20
3701
        pop     ebx
3702
        test    eax, eax
3703
        jnz     .ret
3704
        cmp     edx, 2
3705
        jnz     ntfsDenied      ; folder is not empty
6401 pathoswith 3706
        mov     [ebp+NTFS.cur_attr], 0xA0
6418 pathoswith 3707
        mov     [ebp+NTFS.cur_offs], 0
3708
        mov     [ebp+NTFS.cur_size], 0
6401 pathoswith 3709
        call    ntfs_read_attr.newAttribute
6418 pathoswith 3710
        jc      .deleteFileRecord
6401 pathoswith 3711
@@:
6418 pathoswith 3712
        mov     esi, [ebp+NTFS.frs_buffer]
3713
        cmp     word [esi+baseRecordReuse], 0
3714
        jnz     ntfsUnsupported     ; auxiliary record
3715
        cmp     word [esi+reuseCounter], bx
3716
        jnz     .backToIndex        ; broken index
6420 pathoswith 3717
        test    byte [esi+recordFlags], 1
6418 pathoswith 3718
        jz      .writeBitmapMFT     ; record deleted
3719
        cmp     byte [esi+hardLinkCounter], 3
3720
        jnc     ntfsUnsupported
3721
        mov     esi, [ebp+NTFS.attr_offs]
3722
        cmp     byte [esi+nonResidentFlag], 0
3723
        jz      .deleteFileRecord
3724
        movzx   eax, byte [esi+dataRunsOffset]
6401 pathoswith 3725
        add     esi, eax
6418 pathoswith 3726
        xor     edi, edi
3727
        sub     esp, 16
3728
@@:
3729
        call    ntfs_decode_mcb_entry
3730
        jnc     @f
3731
        cmp     dword[esp+8], 0
6401 pathoswith 3732
        jz      @b
6418 pathoswith 3733
        add     edi, [esp+8]
3734
        mov     ebx, [esp]
3735
        call    ntfsSpaceFree
3736
        jnc     @b
3737
@@:
3738
        add     esp, 16
3739
.deleteFileRecord:
3740
        mov     ebx, [ebp+NTFS.frs_buffer]
3741
        mov     byte [ebx+recordFlags], 0
3742
        mov     edx, [ebp+NTFS.mftLastRead]
3743
        call    writeRecord
3744
.writeBitmapMFT:
3745
        mov     eax, [ebp+NTFS.cur_iRecord]
3746
        mov     ecx, eax
3747
        shr     eax, 3
3748
        and     ecx, 7
3749
        mov     edi, [ebp+NTFS.mftBitmapBuffer]
3750
        btr     [edi+eax], ecx
3751
        shr     eax, 9
3752
        mov     ebx, eax
3753
        shl     ebx, 9
3754
        add     eax, [ebp+NTFS.mftBitmapLocation]
3755
        add     ebx, edi
3756
        mov     ecx, 1
3757
        xor     edx, edx
3758
        call    fs_write64_sys
3759
.backToIndex:
3760
        mov     eax, [ebp+NTFS.newRecord]
3761
        mov     [ebp+NTFS.cur_iRecord], eax
3762
        mov     esi, [ebp+NTFS.indexPointer]
6468 pathoswith 3763
        call    ntfs_find_lfn.doit2
6418 pathoswith 3764
        jc      ntfsFail
3765
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
3766
        mov     byte [ebx], 0
3767
        mov     ebx, [ebp+NTFS.LastRead]
3768
        mov     [ebp+NTFS.nodeLastRead], ebx
3769
        xor     ebx, ebx
3770
        test    byte [eax+indexFlags], 1
3771
        jz      .deleteIndex    ; no subnode
3772
        mov     edi, eax
3773
        call    .findSubindex
3774
        jc      ntfsFail
6401 pathoswith 3775
        movzx   edx, word [edi+indexAllocatedSize]
6418 pathoswith 3776
        test    esi, esi
3777
        jz      @f
6401 pathoswith 3778
        sub     edx, eax
3779
        sub     edx, 8
6418 pathoswith 3780
@@:
3781
        mov     eax, edi
6401 pathoswith 3782
        mov     ebx, esi
3783
        jmp     @f
3784
 
3785
.deleteIndex:
3786
        movzx   edx, word [eax+indexAllocatedSize]
6107 pathoswith 3787
        mov     ecx, [eax+fileRecordReference]
3788
        cmp     [eax+edx+fileRecordReference], ecx
3789
        jnz     @f
3790
        add     dx, [eax+edx+indexAllocatedSize]
3791
@@:
6019 pathoswith 3792
        mov     edi, [ebp+NTFS.cur_index_buf]
3793
        cmp     dword [edi], 'INDX'
3794
        jz      .indexRecord
6418 pathoswith 3795
        sub     eax, edi
6401 pathoswith 3796
        mov     edi, [ebp+NTFS.indexRoot]
6019 pathoswith 3797
        sub     [edi+sizeWithHeader], edx
3798
        sub     [edi+sizeWithoutHeader], edx
6418 pathoswith 3799
        movzx   ecx, byte [edi+attributeOffset]
6019 pathoswith 3800
        add     edi, ecx
6418 pathoswith 3801
        add     eax, edi
6273 pathoswith 3802
        sub     [edi+rootNode+nodeRealSize], edx
3803
        sub     [edi+rootNode+nodeAllocatedSize], edx
6418 pathoswith 3804
        mov     edi, [ebp+NTFS.frs_buffer]
3805
        sub     [edi+recordRealSize], edx
3806
        mov     ecx, [edi+recordRealSize]
3807
        cmp     [edi+recordAllocatedSize], ecx
6019 pathoswith 3808
        jmp     @f
2288 clevermous 3809
 
6019 pathoswith 3810
.indexRecord:
6401 pathoswith 3811
        add     edi, recordNode
3812
        sub     [edi+nodeRealSize], edx
3813
        mov     ecx, [edi+nodeRealSize]
3814
        cmp     [edi+nodeAllocatedSize], ecx
6418 pathoswith 3815
@@:
6401 pathoswith 3816
        jc      ntfsUnsupported
6418 pathoswith 3817
        add     ecx, edi
6019 pathoswith 3818
        sub     ecx, eax
3819
        mov     esi, eax
3820
        add     esi, edx
3821
        mov     edi, eax
6401 pathoswith 3822
        test    edx, edx
3823
        jns     @f
3824
        neg     edx
3825
        add     edx, ecx
3826
        sub     edx, 4
3827
        add     esi, edx
3828
        add     edi, edx
3829
        std
3830
@@:
3831
        jz      @f
3832
        shr     ecx, 2
6019 pathoswith 3833
        rep movsd
6401 pathoswith 3834
        cld
3835
@@:
3836
        test    ebx, ebx
6418 pathoswith 3837
        jz      .done
6401 pathoswith 3838
; copy index from the subnode to replace deleted pointing index
3839
        movzx   ecx, word [ebx+indexAllocatedSize]
3840
        mov     edx, ecx
6418 pathoswith 3841
        test    byte [ebx+indexFlags], 1
3842
        jz      @f
3843
        sub     ecx, 8
3844
        movzx   edi, word [ebx+edx+indexAllocatedSize]
3845
        add     edi, edx
3846
        mov     esi, [ebx+ecx]
3847
        mov     [ebx+edi-8], esi
3848
        mov     [ebx+indexAllocatedSize], cx
3849
@@:
6401 pathoswith 3850
        shr     ecx, 2
3851
        mov     esi, ebx
3852
        mov     edi, eax
3853
        rep movsd
3854
        add     word [eax+indexAllocatedSize], 8
3855
        mov     byte [eax+indexFlags], 1
6418 pathoswith 3856
        mov     edi, [ebp+NTFS.secondIndexBuffer]
6401 pathoswith 3857
        mov     eax, ebx
3858
        xor     ebx, ebx
3859
        jmp     .indexRecord
3860
 
6418 pathoswith 3861
.done:
6408 pathoswith 3862
        mov     ebx, [ebp+NTFS.frs_buffer]
6418 pathoswith 3863
        mov     edx, [ebp+NTFS.rootLastRead]
6019 pathoswith 3864
        call    writeRecord
6408 pathoswith 3865
        mov     ebx, [ebp+NTFS.cur_index_buf]
6418 pathoswith 3866
        cmp     dword [ebx], 'INDX'
3867
        jnz     @f
6408 pathoswith 3868
        mov     edx, [ebp+NTFS.nodeLastRead]
6401 pathoswith 3869
        call    writeRecord
6418 pathoswith 3870
@@:
6408 pathoswith 3871
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
3872
        cmp     byte [ebx], 0
6401 pathoswith 3873
        jz      ntfsDone
6418 pathoswith 3874
        mov     edx, [ebp+NTFS.LastRead]
6019 pathoswith 3875
        call    writeRecord
6080 pathoswith 3876
        jmp     ntfsDone
6019 pathoswith 3877
 
6418 pathoswith 3878
.findSubindex:
3879
; in: eax -> index
3880
;   out:
3881
; CF=1 -> error
3882
; esi=0 -> subnode deleted
3883
; esi -> replacement index
3884
; eax = index effective size
3885
        movzx   edx, word [eax+indexAllocatedSize]
3886
        mov     eax, [eax+edx-8]
3887
        mov     edx, [ebp+NTFS.cur_size]
3888
        push    edx
3889
        cmp     edx, [ebp+NTFS.cur_subnode_size]
3890
        jz      @f
3891
        mul     [ebp+NTFS.sectors_per_cluster]
3892
@@:
3893
        mov     [ebp+NTFS.cur_attr], 0xA0
3894
        mov     [ebp+NTFS.cur_offs], eax
3895
        push    eax
3896
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
3897
        mov     esi, ebx
3898
        mov     [ebp+NTFS.cur_buf], ebx
3899
        call    ntfs_read_attr.newAttribute
3900
        pop     [ebp+NTFS.cur_offs]
3901
        pop     eax
3902
        jc      .ret
3903
        cmp     dword [esi], 'INDX'
3904
        stc
3905
        jnz     .ret
3906
        mov     [ebp+NTFS.cur_size], eax
3907
        shl     eax, 9
3908
        call    ntfs_restore_usa
3909
        jc      .ret
3910
        add     esi, recordNode
3911
        add     esi, [esi+indexOffset]
3912
        test    byte [esi+indexFlags], 2
3913
        jnz     .emptyNode
3914
        cmp     [ebp+NTFS.fragmentCount], 1
3915
        stc
3916
        jnz     .ret    ; record fragmented
3917
        xor     eax, eax
3918
@@:
3919
        add     esi, eax
3920
        mov     ax, [esi+indexAllocatedSize]
3921
        test    byte [esi+eax+indexFlags], 2
3922
        jz      @b
3923
        test    byte [esi+indexFlags], 1
3924
        jz      .ret
3925
        add     eax, esi
3926
        push    esi
3927
        push    [ebp+NTFS.cur_offs]
3928
        call    .findSubindex
3929
        pop     [ebp+NTFS.cur_offs]
3930
        pop     edx
3931
        jc      .ret
3932
        test    esi, esi
3933
        jnz     .ret
3934
        mov     esi, edx
3935
        mov     ebx, [ebp+NTFS.secondIndexBuffer]
3936
        mov     [ebp+NTFS.cur_buf], ebx
3937
        push    [ebp+NTFS.cur_size]
3938
        call    ntfs_read_attr.continue
3939
        pop     eax
3940
        jc      .ret
3941
        shl     eax, 9
3942
        call    ntfs_restore_usa
3943
        jc      .ret
3944
        movzx   eax, word [esi+indexAllocatedSize]
3945
        sub     eax, 8
3946
.ret:
3947
        ret
3948
 
3949
.emptyNode:
3950
        test    byte [esi+indexFlags], 1
3951
        jz      @f
3952
        mov     eax, esi
3953
        push    [ebp+NTFS.cur_offs]
3954
        call    .findSubindex
3955
        pop     [ebp+NTFS.cur_offs]
3956
        jc      .ret
3957
        test    esi, esi
3958
        jnz     .ret
3959
@@:         ; delete node
3960
        mov     esi, [ebp+NTFS.attr_offs]
3961
        add     esi, [esi+sizeWithHeader]
3962
        cmp     byte [esi], 0xB0
3963
        stc
3964
        jnz     .ret
3965
        movzx   eax, byte [esi+attributeOffset]
3966
        add     esi, eax
3967
        mov     eax, [ebp+NTFS.cur_offs]
3968
        xor     edx, edx
3969
        div     [ebp+NTFS.cur_size]
3970
        mov     edx, eax
3971
        shr     eax, 3
3972
        and     edx, 7
3973
        btr     [esi+eax], edx
3974
        mov     esi, [ebp+NTFS.secondIndexBuffer]
3975
        mov     byte [esi], 0
3976
        xor     esi, esi
3977
        ret
3978
 
5954 pathoswith 3979
;----------------------------------------------------------------
3742 clevermous 3980
ntfs_SetFileEnd:
6107 pathoswith 3981
        cmp     byte [esi], 0
3982
        jnz     @f
3983
        xor     ebx, ebx
3984
        movi    eax, ERROR_ACCESS_DENIED
3985
        ret
3986
@@:
3987
        call    ntfs_lock
6468 pathoswith 3988
        call    ntfs_find_lfn
6107 pathoswith 3989
        jc      ntfsNotFound
6273 pathoswith 3990
        cmp     [ebp+NTFS.cur_iRecord], 16
6107 pathoswith 3991
        jc      ntfsDenied
6420 pathoswith 3992
        test    dword [eax+fileFlags], 10000001h
3993
        jnz     ntfsDenied
6273 pathoswith 3994
        cmp     [ebp+NTFS.fragmentCount], 1
6107 pathoswith 3995
        jnz     ntfsUnsupported     ; record fragmented
6151 pathoswith 3996
; edit directory node
6107 pathoswith 3997
        mov     edi, [ebp+NTFS.cur_index_buf]
3998
        cmp     dword [edi], 'INDX'
3999
        jz      @f
4000
        mov     esi, [ebp+NTFS.frs_buffer]
4001
        mov     ecx, [esi+recordRealSize]
4002
        shr     ecx, 2
4003
        rep movsd
6273 pathoswith 4004
        mov     esi, [ebp+NTFS.attr_offs]
6107 pathoswith 4005
        mov     cl, [esi+attributeOffset]
4006
        sub     esi, [ebp+NTFS.frs_buffer]
4007
        add     eax, ecx
4008
        add     eax, esi
4009
@@:
6420 pathoswith 4010
        mov     edi, eax
4011
        mov     eax, [ebx+4]
6107 pathoswith 4012
        mov     edx, [ebx+8]
6420 pathoswith 4013
        mov     [edi+fileRealSize], eax
4014
        mov     [edi+fileRealSize+4], edx
4015
        push    edx eax ebx
4016
        call    ntfsGetTime
4017
        mov     [edi+fileModified], eax
4018
        mov     [edi+fileModified+4], edx
4019
        mov     [edi+recordModified], eax
4020
        mov     [edi+recordModified+4], edx
4021
        mov     [edi+fileAccessed], eax
4022
        mov     [edi+fileAccessed+4], edx
4023
        pop     ebx ecx edx
6273 pathoswith 4024
        mov     eax, [ebp+NTFS.LastRead]
6107 pathoswith 4025
        mov     [ebp+NTFS.nodeLastRead], eax
6273 pathoswith 4026
        mov     [ebp+NTFS.cur_attr], 0x80
4027
        mov     [ebp+NTFS.cur_offs], 0
4028
        mov     [ebp+NTFS.cur_size], 0
6107 pathoswith 4029
        call    ntfs_read_attr
4030
        jc      ntfsFail
6420 pathoswith 4031
        mov     esi, edi
4032
        mov     edi, [ebp+NTFS.frs_buffer]
4033
        cmp     word [edi+baseRecordReuse], 0
4034
        jnz     ntfsUnsupported     ; auxiliary record
4035
        mov     al, [edi+attributeOffset]
4036
        add     edi, eax
4037
        mov     al, [edi+attributeOffset]
4038
        add     edi, eax
6107 pathoswith 4039
        mov     eax, ecx
6420 pathoswith 4040
        mov     ecx, 6
4041
        add     esi, fileModified
4042
        add     edi, 8
4043
        rep movsd
6273 pathoswith 4044
        mov     ecx, [ebp+NTFS.attr_offs]
6151 pathoswith 4045
        cmp     word [ecx+attributeFlags], 0
4046
        jnz     ntfsUnsupported
4047
        cmp     byte [ecx+nonResidentFlag], 0
4048
        jz      .resizeAttribute
6107 pathoswith 4049
        cmp     [ecx+attributeRealSize+4], edx
4050
        jnz     .resizeAttribute
4051
        cmp     [ecx+attributeRealSize], eax
4052
        jnc     .resizeAttribute
4053
        mov     eax, [ecx+attributeRealSize]
4054
        mov     ecx, [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 4055
        mov     [ebp+NTFS.cur_size], ecx
6107 pathoswith 4056
        shl     ecx, 9
4057
        div     ecx
4058
        test    edx, edx
4059
        jz      .aligned
4060
        push    edx
4061
        push    ecx
4062
        mul     [ebp+NTFS.sectors_per_cluster]
6273 pathoswith 4063
        mov     [ebp+NTFS.cur_offs], eax
6107 pathoswith 4064
        stdcall kernel_alloc, ecx
4065
        pop     ecx
4066
        pop     edi
6405 pathoswith 4067
        mov     esi, eax
6107 pathoswith 4068
        sub     ecx, edi
4069
        add     edi, eax
6273 pathoswith 4070
        mov     [ebp+NTFS.cur_buf], eax
6107 pathoswith 4071
        call    ntfs_read_attr.continue
4072
        jc      @f
4073
        xor     eax, eax
4074
        rep stosb
4075
        push    ebx
6273 pathoswith 4076
        mov     eax, [ebp+NTFS.LastRead]
6405 pathoswith 4077
        mov     ebx, esi
6107 pathoswith 4078
        mov     ecx, [ebp+NTFS.sectors_per_cluster]
4079
        xor     edx, edx
4080
        call    fs_write64_app
4081
        pop     ebx
4082
@@:
6405 pathoswith 4083
        stdcall kernel_free, esi
6107 pathoswith 4084
.aligned:
4085
        mov     eax, [ebx+4]
4086
        mov     edx, [ebx+8]
4087
.resizeAttribute:
4088
        call    resizeAttribute
4089
        jc      ntfsError
6408 pathoswith 4090
        mov     ebx, [ebp+NTFS.frs_buffer]
4091
        mov     edx, [ebp+NTFS.mftLastRead]
6107 pathoswith 4092
        call    writeRecord     ; file
6408 pathoswith 4093
        mov     ebx, [ebp+NTFS.cur_index_buf]
4094
        mov     edx, [ebp+NTFS.nodeLastRead]
6107 pathoswith 4095
        call    writeRecord     ; directory
4096
        call    ntfsSpaceClean
4097
        jmp     ntfsDone
4098
 
6462 pathoswith 4099
ntfsGetTime:
4100
        call    fsGetTime
4101
        jmp     @f
2288 clevermous 4102
 
6420 pathoswith 4103
ntfsCalculateTime:
4104
; in: esi -> data block
6462 pathoswith 4105
; out: edx:eax = seconds since 01.01.1601 x10000000
4106
        call    fsCalculateTime
6420 pathoswith 4107
@@:
6462 pathoswith 4108
        mov     edx, 10000000
6420 pathoswith 4109
        mul     edx
6462 pathoswith 4110
        add     eax, 3365781504
4111
        adc     edx, 29389701
6420 pathoswith 4112
        ret
4113
 
3742 clevermous 4114
;----------------------------------------------------------------
6420 pathoswith 4115
ntfs_SetFileInfo:
2288 clevermous 4116
        cmp     byte [esi], 0
4117
        jnz     @f
5994 pathoswith 4118
        movi    eax, ERROR_UNSUPPORTED_FS
2288 clevermous 4119
        ret
4120
@@:
3742 clevermous 4121
        call    ntfs_lock
6468 pathoswith 4122
        call    ntfs_find_lfn
6420 pathoswith 4123
        jnc     @f
3742 clevermous 4124
        test    eax, eax
6019 pathoswith 4125
        jz      ntfsFail
4126
        jmp     ntfsNotFound
2288 clevermous 4127
 
6420 pathoswith 4128
@@:
4129
        cmp     [ebp+NTFS.fragmentCount], 1
4130
        jnz     ntfsUnsupported     ; record fragmented
4131
        mov     esi, [ebp+NTFS.cur_index_buf]
4132
        cmp     dword [esi], 'INDX'
4133
        jz      @f
4134
        sub     eax, esi
4135
        mov     esi, [ebp+NTFS.indexRoot]
4136
        movzx   edx, byte [esi+attributeOffset]
4137
        add     eax, esi
4138
        add     eax, edx
4139
@@:
4140
        mov     esi, [ebx+16]
4141
        mov     edi, eax
4142
        mov     eax, [esi]
4143
        and     eax, 27h
4144
        and     byte [edi+fileFlags], -28h
4145
        or      [edi+fileFlags], al
4146
        add     esi, 8
4147
        call    ntfsCalculateTime
4148
        mov     [edi+fileCreated], eax
4149
        mov     [edi+fileCreated+4], edx
4150
        add     esi, 8
4151
        call    ntfsCalculateTime
4152
        mov     [edi+fileAccessed], eax
4153
        mov     [edi+fileAccessed+4], edx
4154
        add     esi, 8
4155
        call    ntfsCalculateTime
4156
        mov     [edi+fileModified], eax
4157
        mov     [edi+fileModified+4], edx
4158
        mov     ebx, [ebp+NTFS.cur_index_buf]
4159
        cmp     dword [ebx], 'INDX'
4160
        jz      @f
4161
        mov     ebx, [ebp+NTFS.frs_buffer]
4162
@@:
4163
        mov     edx, [ebp+NTFS.LastRead]
4164
        call    writeRecord
4165
        jmp     ntfsDone
4166
 
6019 pathoswith 4167
ntfsUnsupported:
4168
        push    ERROR_UNSUPPORTED_FS
6080 pathoswith 4169
        jmp     ntfsOut
6019 pathoswith 4170
ntfsDevice:
4171
        push    ERROR_DEVICE
4172
        jmp     ntfsOut
4173
ntfsNotFound:
4174
        push    ERROR_FILE_NOT_FOUND
4175
        jmp     ntfsOut
4176
ntfsDenied:
4177
        push    ERROR_ACCESS_DENIED
4178
        jmp     ntfsOut
4179
ntfsFail:
4180
        push    ERROR_FS_FAIL
4181
        jmp     ntfsOut
6080 pathoswith 4182
ntfsDiskFull:
4183
        push    ERROR_DISK_FULL
4184
        jmp     ntfsOut
6414 pathoswith 4185
ntfsErrorPop5:
4186
        pop     ebx
4187
        pop     ebx
6340 pathoswith 4188
ntfsErrorPop3:
4189
        pop     ebx
6107 pathoswith 4190
ntfsErrorPop2:
4191
        pop     ebx
4192
ntfsErrorPop:
4193
        pop     ebx
6080 pathoswith 4194
ntfsError:
4195
        push    eax
4196
ntfsOut:
4197
        call    ntfs_unlock
4198
        xor     ebx, ebx
4199
        pop     eax
4200
        ret