00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
#include "ntrtlp.h"
00031
00032
#if defined(ALLOC_PRAGMA) && defined(NTOS_KERNEL_RUNTIME)
00033
#pragma alloc_text(PAGE,RtlInitializeBitMap)
00034
#endif
00035
00036 #define RightShiftUlong(E1,E2) ((E2) < 32 ? (E1) >> (E2) : 0)
00037 #define LeftShiftUlong(E1,E2) ((E2) < 32 ? (E1) << (E2) : 0)
00038
00039
#if DBG
00040
VOID
00041 DumpBitMap (
00042 PRTL_BITMAP
BitMap
00043 )
00044 {
00045 ULONG i;
00046 BOOLEAN AllZeros, AllOnes;
00047
00048
DbgPrint(
" BitMap:%08lx",
BitMap);
00049
00050 KdPrint((
" (%08x)",
BitMap->SizeOfBitMap));
00051 KdPrint((
" %08lx\n",
BitMap->Buffer));
00052
00053 AllZeros =
FALSE;
00054 AllOnes =
FALSE;
00055
00056
for (i = 0; i < ((
BitMap->SizeOfBitMap + 31) / 32); i += 1) {
00057
00058
if (
BitMap->Buffer[i] == 0) {
00059
00060
if (AllZeros) {
00061
00062 NOTHING;
00063
00064 }
else {
00065
00066
DbgPrint(
"%4d:", i);
00067
DbgPrint(
" %08lx\n",
BitMap->Buffer[i]);
00068 }
00069
00070 AllZeros =
TRUE;
00071 AllOnes =
FALSE;
00072
00073 }
else if (
BitMap->Buffer[i] == 0xFFFFFFFF) {
00074
00075
if (AllOnes) {
00076
00077 NOTHING;
00078
00079 }
else {
00080
00081
DbgPrint(
"%4d:", i);
00082
DbgPrint(
" %08lx\n",
BitMap->Buffer[i]);
00083 }
00084
00085 AllZeros =
FALSE;
00086 AllOnes =
TRUE;
00087
00088 }
else {
00089
00090 AllZeros =
FALSE;
00091 AllOnes =
FALSE;
00092
00093
DbgPrint(
"%4d:", i);
00094
DbgPrint(
" %08lx\n",
BitMap->Buffer[i]);
00095 }
00096 }
00097 }
00098
#endif
00099
00100
00101
00102
00103
00104
00105 #define GET_BYTE_DECLARATIONS() \
00106
PUCHAR _CURRENT_POSITION;
00107
00108 #define GET_BYTE_INITIALIZATION(RTL_BITMAP,BYTE_INDEX) { \
00109
_CURRENT_POSITION = &((PUCHAR)((RTL_BITMAP)->Buffer))[BYTE_INDEX]; \
00110
}
00111
00112 #define GET_BYTE(THIS_BYTE) ( \
00113
THIS_BYTE = *(_CURRENT_POSITION++) \
00114
)
00115
00116
00117
00118
00119
00120
00121
00122 CONST CCHAR
RtlpBitsClearAnywhere[] =
00123 { 8,7,6,6,5,5,5,5,4,4,4,4,4,4,4,4,
00124 4,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
00125 5,4,3,3,2,2,2,2,3,2,2,2,2,2,2,2,
00126 4,3,2,2,2,2,2,2,3,2,2,2,2,2,2,2,
00127 6,5,4,4,3,3,3,3,3,2,2,2,2,2,2,2,
00128 4,3,2,2,2,1,1,1,3,2,1,1,2,1,1,1,
00129 5,4,3,3,2,2,2,2,3,2,1,1,2,1,1,1,
00130 4,3,2,2,2,1,1,1,3,2,1,1,2,1,1,1,
00131 7,6,5,5,4,4,4,4,3,3,3,3,3,3,3,3,
00132 4,3,2,2,2,2,2,2,3,2,2,2,2,2,2,2,
00133 5,4,3,3,2,2,2,2,3,2,1,1,2,1,1,1,
00134 4,3,2,2,2,1,1,1,3,2,1,1,2,1,1,1,
00135 6,5,4,4,3,3,3,3,3,2,2,2,2,2,2,2,
00136 4,3,2,2,2,1,1,1,3,2,1,1,2,1,1,1,
00137 5,4,3,3,2,2,2,2,3,2,1,1,2,1,1,1,
00138 4,3,2,2,2,1,1,1,3,2,1,1,2,1,1,0 };
00139
00140
00141
00142
00143
00144
00145 CONST CCHAR
RtlpBitsClearLow[] =
00146 { 8,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00147 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00148 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00149 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00150 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00151 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00152 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00153 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00154 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00155 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00156 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00157 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00158 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00159 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00160 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
00161 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0 };
00162
00163
00164
00165
00166
00167
00168 CONST CCHAR
RtlpBitsClearHigh[] =
00169 { 8,7,6,6,5,5,5,5,4,4,4,4,4,4,4,4,
00170 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
00171 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00172 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
00173 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00174 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00175 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00176 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
00177 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00178 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00179 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00180 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00181 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00182 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00183 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
00184 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
00185
00186
00187
00188
00189
00190 CONST CCHAR
RtlpBitsClearTotal[] =
00191 { 8,7,7,6,7,6,6,5,7,6,6,5,6,5,5,4,
00192 7,6,6,5,6,5,5,4,6,5,5,4,5,4,4,3,
00193 7,6,6,5,6,5,5,4,6,5,5,4,5,4,4,3,
00194 6,5,5,4,5,4,4,3,5,4,4,3,4,3,3,2,
00195 7,6,6,5,6,5,5,4,6,5,5,4,5,4,4,3,
00196 6,5,5,4,5,4,4,3,5,4,4,3,4,3,3,2,
00197 6,5,5,4,5,4,4,3,5,4,4,3,4,3,3,2,
00198 5,4,4,3,4,3,3,2,4,3,3,2,3,2,2,1,
00199 7,6,6,5,6,5,5,4,6,5,5,4,5,4,4,3,
00200 6,5,5,4,5,4,4,3,5,4,4,3,4,3,3,2,
00201 6,5,5,4,5,4,4,3,5,4,4,3,4,3,3,2,
00202 5,4,4,3,4,3,3,2,4,3,3,2,3,2,2,1,
00203 6,5,5,4,5,4,4,3,5,4,4,3,4,3,3,2,
00204 5,4,4,3,4,3,3,2,4,3,3,2,3,2,2,1,
00205 5,4,4,3,4,3,3,2,4,3,3,2,3,2,2,1,
00206 4,3,3,2,3,2,2,1,3,2,2,1,2,1,1,0 };
00207
00208
00209
00210
00211
00212
00213 static CONST UCHAR
FillMask[] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
00214
00215 static CONST UCHAR
ZeroMask[] = { 0xFF, 0xFE, 0xFC, 0xF8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 };
00216
00217
00218
VOID
00219 RtlInitializeBitMap (
00220 IN PRTL_BITMAP BitMapHeader,
00221 IN PULONG BitMapBuffer,
00222 IN ULONG SizeOfBitMap
00223 )
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 {
00247
RTL_PAGED_CODE();
00248
00249
00250
00251
00252
00253
BitMapHeader->SizeOfBitMap = SizeOfBitMap;
00254
BitMapHeader->Buffer = BitMapBuffer;
00255
00256
00257
00258
00259
00260
00261
return;
00262 }
00263
00264
00265
VOID
00266 RtlClearAllBits (
00267 IN PRTL_BITMAP BitMapHeader
00268 )
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 {
00287
00288
00289
00290
00291 RtlZeroMemory(
BitMapHeader->Buffer,
00292 ((
BitMapHeader->SizeOfBitMap + 31) / 32) * 4
00293 );
00294
00295
00296
00297
00298
00299
00300
return;
00301 }
00302
00303
00304
VOID
00305 RtlSetAllBits (
00306 IN PRTL_BITMAP BitMapHeader
00307 )
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 {
00326
00327
00328
00329
00330 RtlFillMemoryUlong(
BitMapHeader->Buffer,
00331 ((
BitMapHeader->SizeOfBitMap + 31) / 32) * 4,
00332 0xffffffff
00333 );
00334
00335
00336
00337
00338
00339
00340
return;
00341 }
00342
00343
00344 ULONG
00345 RtlFindClearBits (
00346 IN PRTL_BITMAP BitMapHeader,
00347 IN ULONG NumberToFind,
00348 IN ULONG HintIndex
00349 )
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377 {
00378 ULONG SizeOfBitMap;
00379 ULONG SizeInBytes;
00380
00381 ULONG HintBit;
00382 ULONG MainLoopIndex;
00383
00384
GET_BYTE_DECLARATIONS();
00385
00386
00387
00388
00389
00390
00391 SizeOfBitMap =
BitMapHeader->SizeOfBitMap;
00392 SizeInBytes = (SizeOfBitMap + 7) / 8;
00393
00394
00395
00396
00397
00398
00399
if ((SizeOfBitMap % 8) != 0) {
00400
00401
00402
00403
00404
00405
00406 ((PUCHAR)
BitMapHeader->Buffer)[SizeInBytes - 1] |=
00407
ZeroMask[SizeOfBitMap % 8];
00408 }
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
if (HintIndex >= SizeOfBitMap) {
00421
00422 HintIndex = 0;
00423 }
00424
00425 HintBit = HintIndex % 8;
00426
00427
for (MainLoopIndex = 0; MainLoopIndex < 2; MainLoopIndex += 1) {
00428
00429 ULONG StartByteIndex;
00430 ULONG EndByteIndex;
00431
00432 UCHAR CurrentByte;
00433
00434
00435
00436
00437
00438
00439
if (MainLoopIndex == 0) {
00440
00441 StartByteIndex = HintIndex / 8;
00442 EndByteIndex = SizeInBytes;
00443
00444
00445
00446
00447
00448
00449 }
else if (HintIndex != 0) {
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
if (NumberToFind < 2) {
00464
00465 EndByteIndex = HintIndex / 8;
00466
00467 }
else {
00468
00469 EndByteIndex = (HintIndex / 8) + ((NumberToFind - 2) / 8) + 2;
00470
00471
00472
00473
00474
00475
if (EndByteIndex > SizeInBytes) {
00476
00477 EndByteIndex = SizeInBytes;
00478 }
00479 }
00480
00481 HintIndex = 0;
00482 HintBit = 0;
00483 StartByteIndex = 0;
00484
00485
00486
00487
00488
00489
00490 }
else {
00491
00492
return 0xffffffff;
00493 }
00494
00495
00496
00497
00498
00499
GET_BYTE_INITIALIZATION(
BitMapHeader, StartByteIndex);
00500
00501
00502
00503
00504
00505
GET_BYTE( CurrentByte );
00506
00507 CurrentByte |=
FillMask[HintBit];
00508
00509
00510
00511
00512
00513
00514
if (NumberToFind <= 9) {
00515
00516 ULONG CurrentBitIndex;
00517 UCHAR PreviousByte;
00518
00519 PreviousByte = 0xff;
00520
00521
00522
00523
00524
00525
00526 CurrentBitIndex = StartByteIndex * 8;
00527
00528
while (
TRUE) {
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
if (((ULONG)
RtlpBitsClearHigh[PreviousByte] +
00542 (ULONG)
RtlpBitsClearLow[CurrentByte]) >= NumberToFind) {
00543
00544 ULONG StartingIndex;
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 StartingIndex = CurrentBitIndex -
00555 (LONG)
RtlpBitsClearHigh[PreviousByte];
00556
00557
00558
00559
00560
00561
if ((StartingIndex + NumberToFind) <= SizeOfBitMap) {
00562
00563
return StartingIndex;
00564 }
00565 }
00566
00567
00568
00569
00570
00571
if ((ULONG)
RtlpBitsClearAnywhere[CurrentByte] >= NumberToFind) {
00572
00573 UCHAR BitMask;
00574 ULONG i;
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584 BitMask =
FillMask[ NumberToFind ];
00585
for (i = 0; (BitMask & CurrentByte) != 0; i += 1) {
00586
00587 BitMask <<= 1;
00588 }
00589
00590
00591
00592
00593
00594
00595
return CurrentBitIndex + i;
00596 }
00597
00598
00599
00600
00601
00602
00603
00604 PreviousByte = CurrentByte;
00605
00606
00607
00608
00609
00610
00611 CurrentBitIndex += 8;
00612
00613
if ( CurrentBitIndex < EndByteIndex * 8 ) {
00614
00615
GET_BYTE( CurrentByte );
00616
00617 }
else {
00618
00619
break;
00620 }
00621
00622 }
00623
00624
00625
00626
00627
00628
00629
00630 }
else if (NumberToFind < 15) {
00631
00632 ULONG CurrentBitIndex;
00633
00634 UCHAR PreviousPreviousByte;
00635 UCHAR PreviousByte;
00636
00637 PreviousByte = 0xff;
00638
00639
00640
00641
00642
00643
00644 CurrentBitIndex = StartByteIndex * 8;
00645
00646
while (
TRUE) {
00647
00648
00649
00650
00651
00652
00653
00654 PreviousPreviousByte = PreviousByte;
00655 PreviousByte = CurrentByte;
00656
00657
00658
00659
00660
00661
00662 CurrentBitIndex += 8;
00663
00664
if ( CurrentBitIndex < EndByteIndex * 8 ) {
00665
00666
GET_BYTE( CurrentByte );
00667
00668 }
else {
00669
00670
break;
00671 }
00672
00673
00674
00675
00676
00677
00678
00679
if ((PreviousByte == 0)
00680
00681 &&
00682
00683 (((ULONG)
RtlpBitsClearHigh[PreviousPreviousByte] + 8 +
00684 (ULONG)
RtlpBitsClearLow[CurrentByte]) >= NumberToFind)) {
00685
00686 ULONG StartingIndex;
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696 StartingIndex = (CurrentBitIndex - 8) -
00697 (LONG)
RtlpBitsClearHigh[PreviousPreviousByte];
00698
00699
00700
00701
00702
00703
if ((StartingIndex + NumberToFind) <= SizeOfBitMap) {
00704
00705
return StartingIndex;
00706 }
00707 }
00708
00709
00710
00711
00712
00713
00714
if (((ULONG)
RtlpBitsClearHigh[PreviousByte] +
00715 (ULONG)
RtlpBitsClearLow[CurrentByte]) >= NumberToFind) {
00716
00717 ULONG StartingIndex;
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727 StartingIndex = CurrentBitIndex -
00728 (LONG)
RtlpBitsClearHigh[PreviousByte];
00729
00730
00731
00732
00733
00734
if ((StartingIndex + NumberToFind) <= SizeOfBitMap) {
00735
00736
return StartingIndex;
00737 }
00738 }
00739
00740 }
00741
00742
00743
00744
00745
00746
00747 }
else {
00748
00749 ULONG CurrentByteIndex;
00750
00751 ULONG ZeroBytesNeeded;
00752 ULONG ZeroBytesFound;
00753
00754 UCHAR StartOfRunByte;
00755 LONG StartOfRunIndex;
00756
00757
00758
00759
00760
00761 ZeroBytesNeeded = (NumberToFind - 7) / 8;
00762
00763
00764
00765
00766
00767
00768
00769 ZeroBytesFound = 0;
00770 StartOfRunByte = 0xff;
00771 StartOfRunIndex = StartByteIndex - 1;
00772
00773
00774
00775
00776
00777 CurrentByteIndex = StartByteIndex;
00778
00779
while (
TRUE) {
00780
00781
00782
00783
00784
00785
00786
00787
if ((ZeroBytesFound >= ZeroBytesNeeded)
00788
00789 &&
00790
00791 ((ULONG)
RtlpBitsClearHigh[StartOfRunByte] + ZeroBytesFound*8 +
00792 (ULONG)
RtlpBitsClearLow[CurrentByte]) >= NumberToFind) {
00793
00794 ULONG StartingIndex;
00795
00796
00797
00798
00799
00800
00801
00802
00803 StartingIndex = (StartOfRunIndex * 8) +
00804 (8 - (LONG)
RtlpBitsClearHigh[StartOfRunByte]);
00805
00806
00807
00808
00809
00810
if ((StartingIndex + NumberToFind) <= SizeOfBitMap) {
00811
00812
return StartingIndex;
00813 }
00814 }
00815
00816
00817
00818
00819
00820
00821
if (CurrentByte == 0) {
00822
00823 ZeroBytesFound += 1;
00824
00825
00826
00827
00828
00829
00830 }
else {
00831
00832 ZeroBytesFound = 0;
00833 StartOfRunByte = CurrentByte;
00834 StartOfRunIndex = CurrentByteIndex;
00835 }
00836
00837
00838
00839
00840
00841
00842 CurrentByteIndex += 1;
00843
00844
if ( CurrentByteIndex < EndByteIndex ) {
00845
00846
GET_BYTE( CurrentByte );
00847
00848 }
else {
00849
00850
break;
00851 }
00852
00853 }
00854 }
00855 }
00856
00857
00858
00859
00860
00861
return 0xffffffff;
00862 }
00863
00864
00865 ULONG
00866 RtlFindSetBits (
00867 IN PRTL_BITMAP BitMapHeader,
00868 IN ULONG NumberToFind,
00869 IN ULONG HintIndex
00870 )
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896 {
00897 ULONG SizeOfBitMap;
00898 ULONG SizeInBytes;
00899
00900 ULONG HintBit;
00901 ULONG MainLoopIndex;
00902
00903
GET_BYTE_DECLARATIONS();
00904
00905
00906
00907
00908
00909
00910 SizeOfBitMap =
BitMapHeader->SizeOfBitMap;
00911 SizeInBytes = (SizeOfBitMap + 7) / 8;
00912
00913
00914
00915
00916
00917
00918
if ((SizeOfBitMap % 8) != 0) {
00919
00920
00921
00922
00923
00924
00925 ((PUCHAR)
BitMapHeader->Buffer)[SizeInBytes - 1] &=
00926
FillMask[SizeOfBitMap % 8];
00927 }
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
if (HintIndex >= SizeOfBitMap) {
00940
00941 HintIndex = 0;
00942 }
00943
00944 HintBit = HintIndex % 8;
00945
00946
for (MainLoopIndex = 0; MainLoopIndex < 2; MainLoopIndex += 1) {
00947
00948 ULONG StartByteIndex;
00949 ULONG EndByteIndex;
00950
00951 UCHAR CurrentByte;
00952
00953
00954
00955
00956
00957
00958
if (MainLoopIndex == 0) {
00959
00960 StartByteIndex = HintIndex / 8;
00961 EndByteIndex = SizeInBytes;
00962
00963
00964
00965
00966
00967
00968 }
else if (HintIndex != 0) {
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
if (NumberToFind < 2) {
00985
00986 EndByteIndex = HintIndex / 8;
00987
00988 }
else {
00989
00990 EndByteIndex = HintIndex / 8 + ((NumberToFind - 2) / 8) + 1;
00991
00992
00993
00994
00995
00996
if (EndByteIndex > SizeInBytes) {
00997
00998 EndByteIndex = SizeInBytes;
00999 }
01000 }
01001
01002 StartByteIndex = 0;
01003 HintIndex = 0;
01004 HintBit = 0;
01005
01006
01007
01008
01009
01010
01011 }
else {
01012
01013
return 0xffffffff;
01014 }
01015
01016
01017
01018
01019
01020
GET_BYTE_INITIALIZATION(
BitMapHeader, StartByteIndex);
01021
01022
01023
01024
01025
01026
GET_BYTE( CurrentByte );
01027
01028 CurrentByte &=
ZeroMask[HintBit];
01029
01030
01031
01032
01033
01034
01035
if (NumberToFind <= 9) {
01036
01037 ULONG CurrentBitIndex;
01038
01039 UCHAR PreviousByte;
01040
01041 PreviousByte = 0x00;
01042
01043
01044
01045
01046
01047
01048 CurrentBitIndex = StartByteIndex * 8;
01049
01050
while (
TRUE) {
01051
01052
01053
01054
01055
01056
01057
01058
if (((ULONG)
RtlpBitsSetHigh(PreviousByte) +
01059 (ULONG)
RtlpBitsSetLow(CurrentByte)) >= NumberToFind) {
01060
01061 ULONG StartingIndex;
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071 StartingIndex = CurrentBitIndex -
01072 (LONG)
RtlpBitsSetHigh(PreviousByte);
01073
01074
01075
01076
01077
01078
if ((StartingIndex + NumberToFind) <= SizeOfBitMap) {
01079
01080
return StartingIndex;
01081 }
01082 }
01083
01084
01085
01086
01087
01088
if ((ULONG)
RtlpBitSetAnywhere(CurrentByte) >= NumberToFind) {
01089
01090 UCHAR BitMask;
01091 ULONG i;
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101 BitMask =
FillMask[ NumberToFind ];
01102
for (i = 0; (BitMask & CurrentByte) != BitMask; i += 1) {
01103
01104 BitMask <<= 1;
01105 }
01106
01107
01108
01109
01110
01111
01112
return CurrentBitIndex + i;
01113 }
01114
01115
01116
01117
01118
01119
01120
01121 PreviousByte = CurrentByte;
01122
01123
01124
01125
01126
01127
01128 CurrentBitIndex += 8;
01129
01130
if ( CurrentBitIndex < EndByteIndex * 8 ) {
01131
01132
GET_BYTE( CurrentByte );
01133
01134 }
else {
01135
01136
break;
01137 }
01138
01139 }
01140
01141
01142
01143
01144
01145
01146
01147 }
else if (NumberToFind < 15) {
01148
01149 ULONG CurrentBitIndex;
01150
01151 UCHAR PreviousPreviousByte;
01152 UCHAR PreviousByte;
01153
01154 PreviousByte = 0x00;
01155
01156
01157
01158
01159
01160
01161 CurrentBitIndex = StartByteIndex * 8;
01162
01163
while (
TRUE) {
01164
01165
01166
01167
01168
01169
01170
01171
01172 PreviousPreviousByte = PreviousByte;
01173 PreviousByte = CurrentByte;
01174
01175
01176
01177
01178
01179
01180 CurrentBitIndex += 8;
01181
01182
if ( CurrentBitIndex < EndByteIndex * 8 ) {
01183
01184
GET_BYTE( CurrentByte );
01185
01186 }
else {
01187
01188
break;
01189 }
01190
01191
01192
01193
01194
01195
01196
01197
if ((PreviousByte == 0xff)
01198
01199 &&
01200
01201 (((ULONG)
RtlpBitsSetHigh(PreviousPreviousByte) + 8 +
01202 (ULONG)
RtlpBitsSetLow(CurrentByte)) >= NumberToFind)) {
01203
01204 ULONG StartingIndex;
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214 StartingIndex = (CurrentBitIndex - 8) -
01215 (LONG)
RtlpBitsSetHigh(PreviousPreviousByte);
01216
01217
01218
01219
01220
01221
if ((StartingIndex + NumberToFind) <= SizeOfBitMap) {
01222
01223
return StartingIndex;
01224 }
01225 }
01226
01227
01228
01229
01230
01231
01232
if (((ULONG)
RtlpBitsSetHigh(PreviousByte) +
01233 (ULONG)
RtlpBitsSetLow(CurrentByte)) >= NumberToFind) {
01234
01235 ULONG StartingIndex;
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245 StartingIndex = CurrentBitIndex -
01246 (LONG)
RtlpBitsSetHigh(PreviousByte);
01247
01248
01249
01250
01251
01252
if ((StartingIndex + NumberToFind) <= SizeOfBitMap) {
01253
01254
return StartingIndex;
01255 }
01256 }
01257 }
01258
01259
01260
01261
01262
01263
01264 }
else {
01265
01266 ULONG CurrentByteIndex;
01267
01268 ULONG OneBytesNeeded;
01269 ULONG OneBytesFound;
01270
01271 UCHAR StartOfRunByte;
01272 LONG StartOfRunIndex;
01273
01274
01275
01276
01277
01278 OneBytesNeeded = (NumberToFind - 7) / 8;
01279
01280
01281
01282
01283
01284
01285
01286 OneBytesFound = 0;
01287 StartOfRunByte = 0x00;
01288 StartOfRunIndex = StartByteIndex - 1;
01289
01290
01291
01292
01293
01294 CurrentByteIndex = StartByteIndex;
01295
01296
while (
TRUE) {
01297
01298
01299
01300
01301
01302
01303
01304
if ((OneBytesFound >= OneBytesNeeded)
01305
01306 &&
01307
01308 ((ULONG)
RtlpBitsSetHigh(StartOfRunByte) + OneBytesFound*8 +
01309 (ULONG)
RtlpBitsSetLow(CurrentByte)) >= NumberToFind) {
01310
01311 ULONG StartingIndex;
01312
01313
01314
01315
01316
01317
01318
01319
01320 StartingIndex = (StartOfRunIndex * 8) +
01321 (8 - (LONG)
RtlpBitsSetHigh(StartOfRunByte));
01322
01323
01324
01325
01326
01327
if ((StartingIndex + NumberToFind) <= SizeOfBitMap) {
01328
01329
return StartingIndex;
01330 }
01331 }
01332
01333
01334
01335
01336
01337
01338
if (CurrentByte == 0xff) {
01339
01340 OneBytesFound += 1;
01341
01342
01343
01344
01345
01346
01347 }
else {
01348
01349 OneBytesFound = 0;
01350 StartOfRunByte = CurrentByte;
01351 StartOfRunIndex = CurrentByteIndex;
01352 }
01353
01354
01355
01356
01357
01358
01359 CurrentByteIndex += 1;
01360
01361
if ( CurrentByteIndex < EndByteIndex ) {
01362
01363
GET_BYTE( CurrentByte );
01364
01365 }
else {
01366
01367
break;
01368 }
01369 }
01370 }
01371 }
01372
01373
01374
01375
01376
01377
return 0xffffffff;
01378 }
01379
01380
01381 ULONG
01382 RtlFindClearBitsAndSet (
01383 IN PRTL_BITMAP BitMapHeader,
01384 IN ULONG NumberToFind,
01385 IN ULONG HintIndex
01386 )
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414 {
01415 ULONG StartingIndex;
01416
01417
01418
01419
01420
01421 StartingIndex =
RtlFindClearBits(
BitMapHeader,
01422 NumberToFind,
01423 HintIndex );
01424
01425
01426
01427
01428
01429
if (StartingIndex != 0xffffffff) {
01430
01431
01432
01433
01434
01435
RtlSetBits(
BitMapHeader, StartingIndex, NumberToFind );
01436 }
01437
01438
01439
01440
01441
01442
return StartingIndex;
01443
01444 }
01445
01446
01447 ULONG
01448 RtlFindSetBitsAndClear (
01449 IN PRTL_BITMAP BitMapHeader,
01450 IN ULONG NumberToFind,
01451 IN ULONG HintIndex
01452 )
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481 {
01482 ULONG StartingIndex;
01483
01484
01485
01486
01487
01488
if ((StartingIndex =
RtlFindSetBits(
BitMapHeader,
01489 NumberToFind,
01490 HintIndex )) != 0xffffffff) {
01491
01492
01493
01494
01495
01496
RtlClearBits(
BitMapHeader, StartingIndex, NumberToFind );
01497 }
01498
01499
01500
01501
01502
01503
return StartingIndex;
01504 }
01505
01506
01507
VOID
01508 RtlClearBits (
01509 IN PRTL_BITMAP BitMapHeader,
01510 IN ULONG StartingIndex,
01511 IN ULONG NumberToClear
01512 )
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535 {
01536 ULONG BitOffset;
01537 PULONG CurrentLong;
01538
01539
01540
01541
01542
ASSERT( StartingIndex + NumberToClear <= BitMapHeader->SizeOfBitMap );
01543
01544
01545
01546
01547
01548
01549
if (NumberToClear == 0) {
01550
01551
return;
01552 }
01553
01554 BitOffset = StartingIndex % 32;
01555
01556
01557
01558
01559
01560 CurrentLong = &
BitMapHeader->Buffer[ StartingIndex / 32 ];
01561
01562
01563
01564
01565
01566
if ((BitOffset + NumberToClear) <= 32) {
01567
01568
01569
01570
01571
01572
01573
01574
01575 *CurrentLong &= ~
LeftShiftUlong(
RightShiftUlong(((ULONG)0xFFFFFFFF),(32 - NumberToClear)),
01576 BitOffset);
01577
01578
01579
01580
01581
01582
01583
01584
return;
01585 }
01586
01587
01588
01589
01590
01591
01592 *CurrentLong &= ~
LeftShiftUlong(0xFFFFFFFF, BitOffset);
01593
01594
01595
01596
01597
01598
01599 CurrentLong += 1;
01600 NumberToClear -= 32 - BitOffset;
01601
01602
01603
01604
01605
01606
01607
while (NumberToClear >= 32) {
01608
01609 *CurrentLong = 0;
01610 CurrentLong += 1;
01611 NumberToClear -= 32;
01612 }
01613
01614
01615
01616
01617
01618
01619
if (NumberToClear > 0) {
01620
01621 *CurrentLong &=
LeftShiftUlong(0xFFFFFFFF, NumberToClear);
01622 }
01623
01624
01625
01626
01627
01628
01629
01630
return;
01631 }
01632
01633
VOID
01634 RtlSetBits (
01635 IN PRTL_BITMAP BitMapHeader,
01636 IN ULONG StartingIndex,
01637 IN ULONG NumberToSet
01638 )
01639
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660 {
01661 ULONG BitOffset;
01662 PULONG CurrentLong;
01663
01664
01665
01666
01667
ASSERT( StartingIndex + NumberToSet <= BitMapHeader->SizeOfBitMap );
01668
01669
01670
01671
01672
01673
01674
if (NumberToSet == 0) {
01675
01676
return;
01677 }
01678
01679 BitOffset = StartingIndex % 32;
01680
01681
01682
01683
01684
01685 CurrentLong = &
BitMapHeader->Buffer[ StartingIndex / 32 ];
01686
01687
01688
01689
01690
01691
if ((BitOffset + NumberToSet) <= 32) {
01692
01693
01694
01695
01696
01697
01698
01699
01700 *CurrentLong |=
LeftShiftUlong(
RightShiftUlong(((ULONG)0xFFFFFFFF),(32 - NumberToSet)),
01701 BitOffset);
01702
01703
01704
01705
01706
01707
01708
01709
return;
01710 }
01711
01712
01713
01714
01715
01716
01717 *CurrentLong |=
LeftShiftUlong(0xFFFFFFFF, BitOffset);
01718
01719
01720
01721
01722
01723
01724 CurrentLong += 1;
01725 NumberToSet -= 32 - BitOffset;
01726
01727
01728
01729
01730
01731
01732
while (NumberToSet >= 32) {
01733
01734 *CurrentLong = 0xffffffff;
01735 CurrentLong += 1;
01736 NumberToSet -= 32;
01737 }
01738
01739
01740
01741
01742
01743
01744
if (NumberToSet > 0) {
01745
01746 *CurrentLong |= ~
LeftShiftUlong(0xFFFFFFFF, NumberToSet);
01747 }
01748
01749
01750
01751
01752
01753
01754
01755
return;
01756 }
01757
01758
01759
#if DBG
01760
BOOLEAN NtfsDebugIt =
FALSE;
01761
#endif
01762
01763 ULONG
01764 RtlFindClearRuns (
01765 IN PRTL_BITMAP BitMapHeader,
01766 PRTL_BITMAP_RUN RunArray,
01767 ULONG SizeOfRunArray,
01768 BOOLEAN LocateLongestRuns
01769 )
01770
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796
01797
01798
01799
01800 {
01801 ULONG RunIndex;
01802 ULONG i;
01803 LONG j;
01804
01805 ULONG SizeOfBitMap;
01806 ULONG SizeInBytes;
01807
01808 ULONG CurrentRunSize;
01809 ULONG CurrentRunIndex;
01810 ULONG CurrentByteIndex;
01811 UCHAR CurrentByte;
01812
01813 UCHAR BitMask;
01814 UCHAR TempNumber;
01815
01816
GET_BYTE_DECLARATIONS();
01817
01818
01819
01820
01821
01822 SizeOfBitMap =
BitMapHeader->SizeOfBitMap;
01823 SizeInBytes = (SizeOfBitMap + 7) / 8;
01824
01825
01826
01827
01828
01829
01830
if ((SizeOfBitMap % 8) != 0) {
01831
01832
01833
01834
01835
01836
01837 ((PUCHAR)
BitMapHeader->Buffer)[SizeInBytes - 1] |=
ZeroMask[SizeOfBitMap % 8];
01838 }
01839
01840
01841
01842
01843
01844
GET_BYTE_INITIALIZATION(
BitMapHeader, 0);
01845
01846
01847
01848
01849
01850
01851
01852 RunIndex = 0;
01853
for (i = 0; i < SizeOfRunArray; i += 1) { RunArray[i].NumberOfBits = 0; }
01854
01855 CurrentRunSize = 0;
01856 CurrentRunIndex = 0;
01857
01858
01859
01860
01861
01862
for (CurrentByteIndex = 0;
01863 CurrentByteIndex < SizeInBytes;
01864 CurrentByteIndex += 1) {
01865
01866
GET_BYTE( CurrentByte );
01867
01868
#if DBG
01869
if (NtfsDebugIt) {
DbgPrint(
"%d: %08lx %08lx %08lx %08lx %08lx\n",__LINE__,RunIndex,CurrentRunSize,CurrentRunIndex,CurrentByteIndex,CurrentByte); }
01870
#endif
01871
01872
01873
01874
01875
01876
01877
01878
01879
if (CurrentByte != 0x00) {
01880
01881
01882
01883
01884
01885 CurrentRunSize +=
RtlpBitsClearLow[CurrentByte];
01886
01887
01888
01889
01890
01891
01892
01893
if (CurrentRunSize > 0) {
01894
01895
if ((RunIndex < SizeOfRunArray) ||
01896 (RunArray[RunIndex-1].NumberOfBits < CurrentRunSize)) {
01897
01898
01899
01900
01901
01902
01903
01904
if (RunIndex < SizeOfRunArray) { RunIndex += 1; }
01905
01906
for (j = RunIndex-2; LocateLongestRuns && (j >= 0) && (RunArray[j].NumberOfBits < CurrentRunSize); j -= 1) {
01907
01908 RunArray[j+1] = RunArray[j];
01909 }
01910
01911 RunArray[j+1].NumberOfBits = CurrentRunSize;
01912 RunArray[j+1].StartingIndex = CurrentRunIndex;
01913
01914
#if DBG
01915
if (NtfsDebugIt) {
DbgPrint(
"%d: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
01916 __LINE__,RunIndex,CurrentRunSize,CurrentRunIndex,CurrentByteIndex,CurrentByte,j,RunArray[j+1].NumberOfBits,RunArray[j+1].StartingIndex); }
01917
#endif
01918
01919
01920
01921
01922
01923
01924
if (!LocateLongestRuns && (RunIndex >= SizeOfRunArray)) {
01925
01926
return RunIndex;
01927 }
01928 }
01929 }
01930
01931
01932
01933
01934
01935
01936
01937
01938 CurrentRunSize =
RtlpBitsClearHigh[ CurrentByte ];
01939 CurrentRunIndex = (CurrentByteIndex * 8) + (8 - CurrentRunSize);
01940
01941
01942
01943
01944
01945
01946
01947 CurrentByte |=
FillMask[
RtlpBitsClearLow[CurrentByte]] |
01948
ZeroMask[8-
RtlpBitsClearHigh[CurrentByte]];
01949
01950
01951
01952
01953
01954
01955
01956
while ((CurrentByte != 0xff)
01957
01958 &&
01959
01960 ((RunIndex < SizeOfRunArray) ||
01961 (RunArray[RunIndex-1].NumberOfBits < (ULONG)
RtlpBitsClearAnywhere[CurrentByte]))) {
01962
01963 TempNumber =
RtlpBitsClearAnywhere[CurrentByte];
01964
01965
01966
01967
01968
01969
01970 BitMask =
FillMask[ TempNumber ];
01971
01972
for (i = 0; (BitMask & CurrentByte) != 0; i += 1) {
01973
01974 BitMask <<= 1;
01975 }
01976
01977
01978
01979
01980
01981
01982
01983
if (RunIndex < SizeOfRunArray) { RunIndex += 1; }
01984
01985
for (j = RunIndex-2; LocateLongestRuns && (j >= 0) && (RunArray[j].NumberOfBits < TempNumber); j -= 1) {
01986
01987 RunArray[j+1] = RunArray[j];
01988 }
01989
01990 RunArray[j+1].NumberOfBits = TempNumber;
01991 RunArray[j+1].StartingIndex = (CurrentByteIndex * 8) + i;
01992
01993
#if DBG
01994
if (NtfsDebugIt) {
DbgPrint(
"%d: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
01995 __LINE__,RunIndex,CurrentRunSize,CurrentRunIndex,CurrentByteIndex,CurrentByte,j,RunArray[j+1].NumberOfBits,RunArray[j+1].StartingIndex); }
01996
#endif
01997
01998
01999
02000
02001
02002
02003
if (!LocateLongestRuns && (RunIndex >= SizeOfRunArray)) {
02004
02005
return RunIndex;
02006 }
02007
02008
02009
02010
02011
02012 CurrentByte |= BitMask;
02013 }
02014
02015
02016
02017
02018
02019
02020 }
else {
02021
02022 CurrentRunSize += 8;
02023 }
02024 }
02025
02026
#if DBG
02027
if (NtfsDebugIt) {
DbgPrint(
"%d: %08lx %08lx %08lx %08lx %08lx\n",__LINE__,RunIndex,CurrentRunSize,CurrentRunIndex,CurrentByteIndex,CurrentByte); }
02028
#endif
02029
02030
02031
02032
02033
02034
02035
if (CurrentRunSize > 0) {
02036
02037
if ((RunIndex < SizeOfRunArray) ||
02038 (RunArray[RunIndex-1].NumberOfBits < CurrentRunSize)) {
02039
02040
02041
02042
02043
02044
02045
if (RunIndex < SizeOfRunArray) { RunIndex += 1; }
02046
02047
for (j = RunIndex-2; LocateLongestRuns && (j >= 0) && (RunArray[j].NumberOfBits < CurrentRunSize); j -= 1) {
02048
02049 RunArray[j+1] = RunArray[j];
02050 }
02051
02052 RunArray[j+1].NumberOfBits = CurrentRunSize;
02053 RunArray[j+1].StartingIndex = CurrentRunIndex;
02054
02055
#if DBG
02056
if (NtfsDebugIt) {
DbgPrint(
"%d: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
02057 __LINE__,RunIndex,CurrentRunSize,CurrentRunIndex,CurrentByteIndex,CurrentByte,j,RunArray[j+1].NumberOfBits,RunArray[j+1].StartingIndex); }
02058
#endif
02059
}
02060 }
02061
02062
02063
02064
02065
02066
return RunIndex;
02067 }
02068
02069
02070 ULONG
02071 RtlFindLongestRunClear (
02072 IN PRTL_BITMAP BitMapHeader,
02073 OUT PULONG StartingIndex
02074 )
02075
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090
02091
02092
02093
02094
02095
02096
02097 {
02098 RTL_BITMAP_RUN RunArray[1];
02099
02100
02101
02102
02103
02104
02105
if (
RtlFindClearRuns(
BitMapHeader, RunArray, 1,
TRUE ) == 1) {
02106
02107 *StartingIndex = RunArray[0].StartingIndex;
02108
return RunArray[0].NumberOfBits;
02109 }
02110
02111 *StartingIndex = 0;
02112
return 0;
02113 }
02114
02115
02116 ULONG
02117 RtlFindFirstRunClear (
02118 IN PRTL_BITMAP BitMapHeader,
02119 OUT PULONG StartingIndex
02120 )
02121
02122
02123
02124
02125
02126
02127
02128
02129
02130
02131
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143 {
02144
return RtlFindNextForwardRunClear(
BitMapHeader, 0, StartingIndex);
02145 }
02146
02147
02148 ULONG
02149 RtlNumberOfClearBits (
02150 IN PRTL_BITMAP BitMapHeader
02151 )
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167
02168
02169
02170 {
02171 ULONG SizeOfBitMap;
02172 ULONG SizeInBytes;
02173
02174 ULONG i;
02175 UCHAR CurrentByte;
02176
02177 ULONG TotalClear;
02178
02179
GET_BYTE_DECLARATIONS();
02180
02181
02182
02183
02184
02185 SizeOfBitMap =
BitMapHeader->SizeOfBitMap;
02186 SizeInBytes = (SizeOfBitMap + 7) / 8;
02187
02188
02189
02190
02191
02192
02193
if ((SizeOfBitMap % 8) != 0) {
02194
02195
02196
02197
02198
02199
02200 ((PUCHAR)
BitMapHeader->Buffer)[SizeInBytes - 1] |=
02201
ZeroMask[SizeOfBitMap % 8];
02202 }
02203
02204
02205
02206
02207
02208
GET_BYTE_INITIALIZATION(
BitMapHeader, 0 );
02209
02210
02211
02212
02213
02214 TotalClear = 0;
02215
for (i = 0; i < SizeInBytes; i += 1) {
02216
02217
GET_BYTE( CurrentByte );
02218
02219 TotalClear +=
RtlpBitsClearTotal[CurrentByte];
02220 }
02221
02222
return TotalClear;
02223 }
02224
02225
02226 ULONG
02227 RtlNumberOfSetBits (
02228 IN PRTL_BITMAP BitMapHeader
02229 )
02230
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240
02241
02242
02243
02244
02245
02246
02247
02248 {
02249 ULONG SizeOfBitMap;
02250 ULONG SizeInBytes;
02251
02252 ULONG i;
02253 UCHAR CurrentByte;
02254
02255 ULONG TotalSet;
02256
02257
GET_BYTE_DECLARATIONS();
02258
02259
02260
02261
02262
02263 SizeOfBitMap =
BitMapHeader->SizeOfBitMap;
02264 SizeInBytes = (SizeOfBitMap + 7) / 8;
02265
02266
02267
02268
02269
02270
02271
if ((SizeOfBitMap % 8) != 0) {
02272
02273
02274
02275
02276
02277
02278 ((PUCHAR)
BitMapHeader->Buffer)[SizeInBytes - 1] &=
02279
FillMask[SizeOfBitMap % 8];
02280 }
02281
02282
02283
02284
02285
02286
GET_BYTE_INITIALIZATION(
BitMapHeader, 0 );
02287
02288
02289
02290
02291
02292 TotalSet = 0;
02293
for (i = 0; i < SizeInBytes; i += 1) {
02294
02295
GET_BYTE( CurrentByte );
02296
02297 TotalSet +=
RtlpBitsSetTotal(CurrentByte);
02298 }
02299
02300
return TotalSet;
02301 }
02302
02303
02304 BOOLEAN
02305 RtlAreBitsClear (
02306 IN PRTL_BITMAP BitMapHeader,
02307 IN ULONG StartingIndex,
02308 IN ULONG Length
02309 )
02310
02311
02312
02313
02314
02315
02316
02317
02318
02319
02320
02321
02322
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333 {
02334 ULONG SizeOfBitMap;
02335 ULONG SizeInBytes;
02336
02337 ULONG EndingIndex;
02338
02339 ULONG StartingByte;
02340 ULONG EndingByte;
02341
02342 ULONG StartingOffset;
02343 ULONG EndingOffset;
02344
02345 ULONG i;
02346 UCHAR Byte;
02347
02348
GET_BYTE_DECLARATIONS();
02349
02350
02351
02352
02353
02354
02355 SizeOfBitMap =
BitMapHeader->SizeOfBitMap;
02356 SizeInBytes = (SizeOfBitMap + 7) / 8;
02357
02358
02359
02360
02361
02362
02363
if ((StartingIndex + Length > SizeOfBitMap) || (Length == 0)) {
02364
02365
return FALSE;
02366 }
02367
02368
02369
02370
02371
02372
02373 EndingIndex = StartingIndex + Length - 1;
02374
02375 StartingByte = StartingIndex / 8;
02376 EndingByte = EndingIndex / 8;
02377
02378 StartingOffset = StartingIndex % 8;
02379 EndingOffset = EndingIndex % 8;
02380
02381
02382
02383
02384
02385
GET_BYTE_INITIALIZATION(
BitMapHeader, StartingByte );
02386
02387
02388
02389
02390
02391
02392
if (StartingByte == EndingByte) {
02393
02394
02395
02396
02397
02398
GET_BYTE( Byte );
02399
02400
02401
02402
02403
02404
02405
02406
if ((
ZeroMask[StartingOffset] &
FillMask[EndingOffset+1] & Byte) == 0) {
02407
02408
return TRUE;
02409
02410 }
else {
02411
02412
return FALSE;
02413 }
02414
02415 }
else {
02416
02417
02418
02419
02420
02421
02422
02423
GET_BYTE( Byte );
02424
02425
if ((
ZeroMask[StartingOffset] & Byte) != 0) {
02426
02427
return FALSE;
02428 }
02429
02430
02431
02432
02433
02434
02435
for (i = StartingByte+1; i < EndingByte; i += 1) {
02436
02437
GET_BYTE( Byte );
02438
02439
if (Byte != 0) {
02440
02441
return FALSE;
02442 }
02443 }
02444
02445
02446
02447
02448
02449
02450
02451
GET_BYTE( Byte );
02452
02453
if ((
FillMask[EndingOffset+1] & Byte) != 0) {
02454
02455
return FALSE;
02456 }
02457 }
02458
02459
return TRUE;
02460 }
02461
02462
02463 BOOLEAN
02464 RtlAreBitsSet (
02465 IN PRTL_BITMAP BitMapHeader,
02466 IN ULONG StartingIndex,
02467 IN ULONG Length
02468 )
02469
02470
02471
02472
02473
02474
02475
02476
02477
02478
02479
02480
02481
02482
02483
02484
02485
02486
02487
02488
02489
02490
02491
02492 {
02493 ULONG SizeOfBitMap;
02494 ULONG SizeInBytes;
02495
02496 ULONG EndingIndex;
02497
02498 ULONG StartingByte;
02499 ULONG EndingByte;
02500
02501 ULONG StartingOffset;
02502 ULONG EndingOffset;
02503
02504 ULONG i;
02505 UCHAR Byte;
02506
02507
GET_BYTE_DECLARATIONS();
02508
02509
02510
02511
02512
02513
02514 SizeOfBitMap =
BitMapHeader->SizeOfBitMap;
02515 SizeInBytes = (SizeOfBitMap + 7) / 8;
02516
02517
02518
02519
02520
02521
02522
if ((StartingIndex + Length > SizeOfBitMap) || (Length == 0)) {
02523
02524
return FALSE;
02525 }
02526
02527
02528
02529
02530
02531
02532 EndingIndex = StartingIndex + Length - 1;
02533
02534 StartingByte = StartingIndex / 8;
02535 EndingByte = EndingIndex / 8;
02536
02537 StartingOffset = StartingIndex % 8;
02538 EndingOffset = EndingIndex % 8;
02539
02540
02541
02542
02543
02544
GET_BYTE_INITIALIZATION(
BitMapHeader, StartingByte );
02545
02546
02547
02548
02549
02550
02551
if (StartingByte == EndingByte) {
02552
02553
02554
02555
02556
02557
GET_BYTE( Byte );
02558
02559
02560
02561
02562
02563
02564
02565
if ((
ZeroMask[StartingOffset] &
FillMask[EndingOffset+1] & ~Byte) == 0) {
02566
02567
return TRUE;
02568
02569 }
else {
02570
02571
return FALSE;
02572 }
02573
02574 }
else {
02575
02576
02577
02578
02579
02580
02581
02582
GET_BYTE( Byte );
02583
02584
if ((
ZeroMask[StartingOffset] & ~Byte) != 0) {
02585
02586
return FALSE;
02587 }
02588
02589
02590
02591
02592
02593
02594
for (i = StartingByte+1; i < EndingByte; i += 1) {
02595
02596
GET_BYTE( Byte );
02597
02598
if (Byte != 0xff) {
02599
02600
return FALSE;
02601 }
02602 }
02603
02604
02605
02606
02607
02608
02609
02610
GET_BYTE( Byte );
02611
02612
if ((
FillMask[EndingOffset+1] & ~Byte) != 0) {
02613
02614
return FALSE;
02615 }
02616 }
02617
02618
return TRUE;
02619 }
02620
02621 static CONST ULONG
FillMaskUlong[] = {
02622 0x00000000, 0x00000001, 0x00000003, 0x00000007,
02623 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
02624 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
02625 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
02626 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
02627 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
02628 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
02629 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
02630 0xffffffff
02631 };
02632
02633
02634 ULONG
02635 RtlFindNextForwardRunClear (
02636 IN PRTL_BITMAP BitMapHeader,
02637 IN ULONG FromIndex,
02638 IN PULONG StartingRunIndex
02639 )
02640 {
02641 ULONG
Start;
02642 ULONG
End;
02643 PULONG PHunk, BitMapEnd;
02644 ULONG Hunk;
02645
02646
02647
02648
02649
02650
if (
BitMapHeader->SizeOfBitMap == 0) {
02651
02652 *StartingRunIndex = FromIndex;
02653
return 0;
02654 }
02655
02656
02657
02658
02659
02660 BitMapEnd =
BitMapHeader->Buffer + ((
BitMapHeader->SizeOfBitMap - 1) / 32);
02661
02662
02663
02664
02665
02666
Start = FromIndex;
02667
02668
02669
02670
02671
02672
02673 PHunk =
BitMapHeader->Buffer + (
Start / 32);
02674
02675
02676
02677
02678
02679
02680
02681
02682
if (PHunk != BitMapEnd) {
02683
02684
02685
02686
02687
02688 Hunk = *PHunk |
FillMaskUlong[
Start % 32];
02689
02690
if (Hunk == (ULONG)~0) {
02691
02692
02693
02694
02695
02696
Start += 32 - (
Start % 32);
02697 PHunk++;
02698
02699
while ( PHunk < BitMapEnd ) {
02700
02701
02702
02703
02704
02705
if (*PHunk != (ULONG)~0)
break;
02706
02707 PHunk++;
02708
Start += 32;
02709 }
02710 }
02711 }
02712
02713
02714
02715
02716
02717
while ((
Start <
BitMapHeader->SizeOfBitMap) && (RtlCheckBit(
BitMapHeader,
Start ) == 1)) {
Start += 1; }
02718
02719
02720
02721
02722
02723
End =
Start;
02724
02725
02726
02727
02728
02729
02730
if (PHunk != BitMapEnd) {
02731
02732
02733
02734
02735
02736
02737
02738 Hunk = *PHunk & ~
FillMaskUlong[
End % 32];
02739
02740
if (Hunk == (ULONG)0) {
02741
02742
02743
02744
02745
02746
End += 32 - (
End % 32);
02747 PHunk++;
02748
02749
while ( PHunk < BitMapEnd ) {
02750
02751
02752
02753
02754
02755
if (*PHunk != (ULONG)0)
break;
02756
02757 PHunk++;
02758
End += 32;
02759 }
02760 }
02761 }
02762
02763
02764
02765
02766
02767
while ((
End <
BitMapHeader->SizeOfBitMap) && (RtlCheckBit(
BitMapHeader,
End ) == 0)) {
End += 1; }
02768
02769
02770
02771
02772
02773 *StartingRunIndex =
Start;
02774
return (
End -
Start);
02775 }
02776
02777
02778 ULONG
02779 RtlFindLastBackwardRunClear (
02780 IN PRTL_BITMAP BitMapHeader,
02781 IN ULONG FromIndex,
02782 IN PULONG StartingRunIndex
02783 )
02784 {
02785 ULONG
Start;
02786 ULONG
End;
02787 PULONG PHunk;
02788 ULONG Hunk;
02789
02790
RTL_PAGED_CODE();
02791
02792
02793
02794
02795
02796
if (
BitMapHeader->SizeOfBitMap == 0) {
02797
02798 *StartingRunIndex = FromIndex;
02799
return 0;
02800 }
02801
02802
02803
02804
02805
02806
End = FromIndex;
02807
02808
02809
02810
02811
02812
02813
02814
02815 PHunk =
BitMapHeader->Buffer + (
End / 32);
02816 Hunk = *PHunk | ~
FillMaskUlong[(
End % 32) + 1];
02817
02818
02819
02820
02821
02822
02823
02824
if (Hunk == (ULONG)~0) {
02825
02826
02827
02828
02829
02830
End -= (
End % 32) + 1;
02831 PHunk--;
02832
02833
while ( PHunk >
BitMapHeader->Buffer ) {
02834
02835
02836
02837
02838
02839
if (*PHunk != (ULONG)~0)
break;
02840
02841 PHunk--;
02842
End -= 32;
02843 }
02844 }
02845
02846
02847
02848
02849
02850
while ((
End != MAXULONG) && (RtlCheckBit(
BitMapHeader,
End ) == 1)) {
End -= 1; }
02851
02852
02853
02854
02855
02856
Start =
End;
02857
02858
02859
02860
02861
02862
02863
02864 Hunk = *PHunk &
FillMaskUlong[
Start % 32];
02865
02866
02867
02868
02869
02870
if (Hunk == (ULONG)0) {
02871
02872
02873
02874
02875
02876
Start -= (
Start % 32) + 1;
02877 PHunk--;
02878
02879
while ( PHunk >
BitMapHeader->Buffer ) {
02880
02881
02882
02883
02884
02885
if (*PHunk != (ULONG)0)
break;
02886
02887 PHunk--;
02888
Start -= 32;
02889 }
02890 }
02891
02892
02893
02894
02895
02896
while ((
Start != MAXULONG) && (RtlCheckBit(
BitMapHeader,
Start ) == 0)) {
Start -= 1; }
02897
02898
02899
02900
02901
02902 *StartingRunIndex =
Start + 1;
02903
return (
End -
Start);
02904 }
02905
02906 #define BM_4567 0xFFFFFFFF00000000UI64
02907 #define BM_67 0xFFFF000000000000UI64
02908 #define BM_7 0xFF00000000000000UI64
02909 #define BM_5 0x0000FF0000000000UI64
02910 #define BM_23 0x00000000FFFF0000UI64
02911 #define BM_3 0x00000000FF000000UI64
02912 #define BM_1 0x000000000000FF00UI64
02913
02914 #define BM_0123 0x00000000FFFFFFFFUI64
02915 #define BM_01 0x000000000000FFFFUI64
02916 #define BM_0 0x00000000000000FFUI64
02917 #define BM_2 0x0000000000FF0000UI64
02918 #define BM_45 0x0000FFFF00000000UI64
02919 #define BM_4 0x000000FF00000000UI64
02920 #define BM_6 0x00FF000000000000UI64
02921
02922 CCHAR
02923 RtlFindMostSignificantBit (
02924 IN ULONGLONG Set
02925 )
02926
02927
02928
02929
02930
02931
02932
02933
02934
02935
02936
02937
02938
02939
02940
02941
02942
02943
02944
02945
02946 {
02947 UCHAR index;
02948 UCHAR bitOffset;
02949 UCHAR lookup;
02950
02951
if ((Set &
BM_4567) != 0) {
02952
if ((Set &
BM_67) != 0) {
02953
if ((Set &
BM_7) != 0) {
02954 bitOffset = 7 * 8;
02955 }
else {
02956 bitOffset = 6 * 8;
02957 }
02958 }
else {
02959
if ((Set &
BM_5) != 0) {
02960 bitOffset = 5 * 8;
02961 }
else {
02962 bitOffset = 4 * 8;
02963 }
02964 }
02965 }
else {
02966
if ((Set &
BM_23) != 0) {
02967
if ((Set &
BM_3) != 0) {
02968 bitOffset = 3 * 8;
02969 }
else {
02970 bitOffset = 2 * 8;
02971 }
02972 }
else {
02973
if ((Set &
BM_1) != 0) {
02974 bitOffset = 1 * 8;
02975 }
else {
02976
02977
02978
02979
02980
02981
02982
02983
02984
if (Set == 0) {
02985
return -1;
02986 }
02987
02988 bitOffset = 0 * 8;
02989 }
02990 }
02991 }
02992
02993 lookup = (UCHAR)(Set >> bitOffset);
02994 index = (7 -
RtlpBitsClearHigh[lookup]) + bitOffset;
02995
return index;
02996 }
02997
02998 CCHAR
02999 RtlFindLeastSignificantBit (
03000 IN ULONGLONG Set
03001 )
03002
03003
03004
03005
03006
03007
03008
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018
03019
03020
03021
03022 {
03023 UCHAR index;
03024 UCHAR bitOffset;
03025 UCHAR lookup;
03026
03027
if ((Set &
BM_0123) != 0) {
03028
if ((Set &
BM_01) != 0) {
03029
if ((Set &
BM_0) != 0) {
03030 bitOffset = 0 * 8;
03031 }
else {
03032 bitOffset = 1 * 8;
03033 }
03034 }
else {
03035
if ((Set &
BM_2) != 0) {
03036 bitOffset = 2 * 8;
03037 }
else {
03038 bitOffset = 3 * 8;
03039 }
03040 }
03041 }
else {
03042
if ((Set &
BM_45) != 0) {
03043
if ((Set &
BM_4) != 0) {
03044 bitOffset = 4 * 8;
03045 }
else {
03046 bitOffset = 5 * 8;
03047 }
03048 }
else {
03049
if ((Set &
BM_6) != 0) {
03050 bitOffset = 6 * 8;
03051 }
else {
03052
03053
03054
03055
03056
03057
03058
03059
03060
if (Set == 0) {
03061
return -1;
03062 }
03063
03064 bitOffset = 7 * 8;
03065 }
03066 }
03067 }
03068
03069 lookup = (UCHAR)(Set >> bitOffset);
03070 index =
RtlpBitsClearLow[lookup] + bitOffset;
03071
return index;
03072 }
03073