217 Calls::Attach(*
this,
false, 0);
218 Attach_Common(Buffer_, Size_, ExtraBytesAvailable_);
227 Calls::Attach(*
this,
true, ZerosBefore_);
228 Attach_Common(Buffer_, Size_, ExtraBytesAvailable_);
240 if (Buffer_Size<=WindowEnd)
242 if (Calls::Present && Calls::Active(*
this))
243 return Calls::Read(*
this, 1)!=0;
252 return TestBit(Window, Buffer_Size);
257 return (int8u)Get<8>(HowMany);
262 return (int16u)Get<16>(HowMany);
267 return (int32u)Get<32>(HowMany);
273 return Get<32>(HowMany);
276 if (HowMany>Buffer_Size)
279 if ((
size_t)(HowMany-32)<=Buffer_Size)
280 Buffer_Size-=HowMany-32;
284 return Read64(HowMany);
290 return (int32u)Read<32>(HowMany);
296 return Read64(HowMany);
307 if (!PeekUE(Bits, Size))
310 return (int32u)(Bits>>(WordBits-Size))-1;
316 const int32u CodeNum=
GetUE();
317 return (int32s)((CodeNum&1)?(CodeNum>>1)+1:0u-(CodeNum>>1));
325 if (HowMany>Buffer_Size)
330 Buffer_Size-=HowMany;
335 if (Buffer_Size<=WindowEnd)
337 if (Calls::Present && Calls::Active(*
this))
338 return Calls::Peek(*
this, 1)!=0;
346 return TestBit(Window, Buffer_Size-1);
351 return (int8u)Peek<8>(HowMany);
356 return (int16u)Peek<16>(HowMany);
361 return (int32u)Peek<32>(HowMany);
367 return Peek<32>(HowMany);
370 if (HowMany>Buffer_Size)
375 if (Calls::Present && Calls::Active(*
this) && HowMany+WindowEnd>Buffer_Size)
376 return Calls::Peek(*
this, HowMany);
377 if (HowMany<=WordBits)
378 return PeekWide(Buffer_Size, HowMany);
380 const int64u High=PeekWide(Buffer_Size, HowMany-32);
381 return (High<<32)|PeekWide(Buffer_Size-(HowMany-32), 32);
394 if (Calls::Present && Calls::Active(*
this) && Buffer_Size)
402 if (!Calls::Present || !Calls::Active(*
this) || !Buffer_Size)
404 return Calls::Remain_Exact(*
this);
409 Buffer_Size&=~(size_t)7;
414 return (Buffer_Size_Init-Buffer_Size)/8;
419 return Buffer_Size%8;
424 return Buffer_Size%8;
432 if (BufferUnderRun_ && Size_>Buffer_Size)
433 BufferUnderRun_=
false;
434 if (Calls::Present && Calls::Active(*
this))
435 Calls::Resize(*
this);
436 BitEnd=BitEnd-Buffer_Size+Size_;
446 if (Calls::Present && Calls::Active(*
this))
447 Calls::Refresh(*
this);
453 enum { WordBits=
sizeof(Word)*8 };
462 std::memcpy(&Value, Source,
sizeof(Value));
463 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
465 #elif defined(_MSC_VER)
467 return _byteswap_uint64(Value);
469 return _byteswap_ulong(Value);
471 #elif (defined(__GNUC__) || defined(__clang__)) && defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
472 return sizeof(Word)==8?(Word)__builtin_bswap64((int64u)Value):(Word)__builtin_bswap32((int32u)Value);
476 for (
size_t i=0; i<
sizeof(Word); i++)
477 Result=(Result<<8)|Source[i];
486 for (
size_t i=0; i<Size; i++)
487 Value|=(Word)Source[i]<<(WordBits-8-8*i);
493 #if defined(_MSC_VER)
496 _BitScanReverse64(&Index, Value);
498 _BitScanReverse(&Index, Value);
500 return (
unsigned)(WordBits-1-Index);
501 #elif defined(__GNUC__) || defined(__clang__)
502 return sizeof(Word)==8?(
unsigned)__builtin_clzll((
unsigned long long)Value):(unsigned)__builtin_clz((unsigned)Value);
505 while (!(Value>>(WordBits-1))) { Value<<=1; Count++; }
515 #define ZENLIB_BSF_MASK4(N) ((int64u)1<<(N))-1, ((int64u)2<<(N))-1, ((int64u)4<<(N))-1, ((int64u)8<<(N))-1
516 static const int64u Masks[65]=
524 #undef ZENLIB_BSF_MASK4
525 return (Word)Masks[HowMany];
531 #if defined(_MSC_VER) && !defined(__clang__)
534 return _rotr64(Value, (
int)Count);
536 return _rotr(Value, (
int)Count);
539 const unsigned Shift=(unsigned)(Count&(WordBits-1));
540 return (Value>>Shift)|(Value<<((WordBits-Shift)&(WordBits-1)));
549 #if defined(_MSC_VER) && !defined(__clang__)
550 return (RotateRight(Value, Index)&1)!=0;
552 return ((Value>>(Index&(WordBits-1)))&1)!=0;
556 ZENLIB_BSF_INLINE void Attach_Common(
const int8u* Buffer_,
size_t Size_,
size_t ExtraBytesAvailable_)
559 Buffer_Size=Buffer_Size_Init=BitEnd=Size_*8;
561 ExtraBytes=(int8u)
MIN(ExtraBytesAvailable_,
sizeof(Word)-1);
562 BufferUnderRun_=
false;
575 if (Calls::Present && Calls::Active(*
this))
577 Calls::Reload(*
this);
580 size_t Load=(BitEnd-Buffer_Size)/8;
586 const size_t Avail=BitEnd-Load*8;
587 const size_t WindowLow=Avail-WordBits;
588 Window=RotateRight(LoadWord(Buffer+Load), 0-WindowLow);
589 WindowEnd=Avail>WordBits?WindowLow:0;
598 const size_t Logical=BitEnd/8+(BitEnd%8!=0);
599 const size_t Attached=Buffer_Size_Init/8+ExtraBytes;
600 size_t Readable=Logical+ExtraBytes;
601 if (Readable>Attached)
602 Readable=Logical>Attached?Logical:Attached;
603 Calls::Fill(*
this, Readable);
604 if (Readable>=
sizeof(Word))
606 LastLoad=Readable-
sizeof(Word);
618 if (Calls::Present && Calls::Active(*
this))
620 Calls::Reload(*
this);
623 Window=RotateRight(LoadPartial(Buffer, Readable), WordBits-BitEnd);
640 BufferUnderRun_=
true;
651 if (Pos*8+8>BitEnd || Buffer[Pos]!=0x03)
654 return !Buffer[Pos-1] && !Buffer[Pos-2];
656 return !Buffer[0] && this->ZerosBefore;
657 return this->ZerosBefore>=2;
666 size_t Pos=(BitEnd-Buffer_Size)/8;
667 while (this->Checked<=Pos && Buffer_Size)
669 if (IsEmulationPrevention(this->Checked))
679 while (++this->Checked*8<BitEnd)
680 if (IsEmulationPrevention(this->Checked))
687 Pos=(BitEnd-Buffer_Size)/8;
695 const Word Ones=(Word)-1/255;
696 return ((Value-Ones)&~Value&(Ones<<7))!=0;
703 Sync_EmulationPrevention();
704 const size_t Pos=(BitEnd-Buffer_Size)/8;
712 if (this->Readable_>=
sizeof(Word))
714 Load=Pos<LastLoad?Pos:LastLoad;
715 Value=LoadWord(Buffer+Load);
720 Value=LoadPartial(Buffer, this->Readable_);
722 const size_t Avail=BitEnd-Load*8;
723 const size_t WindowLow=Avail-WordBits;
724 Window=RotateRight(Value, 0-WindowLow);
725 WindowEnd=Avail>WordBits?WindowLow:0;
728 if (End>Load+
sizeof(Word))
729 End=Load+
sizeof(Word);
730 if (this->Readable_<
sizeof(Word) || HasZeroByte(Value))
731 for (
size_t i=this->Checked; i<End; i++)
732 if (IsEmulationPrevention(i))
734 WindowEnd=BitEnd-i*8;
738 if (this->Checked<End)
745 const size_t Start_Size=Buffer_Size, Start_End=BitEnd, Start_Checked=this->Checked;
749 if (Buffer_Size<=WindowEnd)
752 Reload_EmulationPrevention();
753 if (Buffer_Size<=WindowEnd)
755 UnderRun_At(Start_Size, Start_End, Start_Checked);
759 size_t Usable=Buffer_Size-WindowEnd;
762 const unsigned Part=(unsigned)Usable;
764 Value=(Part<64?(Value<<Part):0)|(RotateRight(Window, Buffer_Size)&Mask(Part));
776 this->Checked=Checked_;
784 const int64u Value=Copy.Read_EmulationPrevention(HowMany);
785 if (Copy.BufferUnderRun_)
796 const size_t Start_Size=Buffer_Size, Start_End=BitEnd, Start_Checked=this->Checked;
797 unsigned LeadingZeroBits=0;
802 UnderRun_At(Start_Size, Start_End, Start_Checked);
807 if (++LeadingZeroBits>31)
809 UnderRun_At(Start_Size, Start_End, Start_Checked);
813 if (!LeadingZeroBits)
815 const int64u Info=Read_EmulationPrevention(LeadingZeroBits);
818 UnderRun_At(Start_Size, Start_End, Start_Checked);
821 return (int32u)((((int64u)1<<LeadingZeroBits)|Info)-1);
825 ZENLIB_BSF_INLINE void Attach_State_EmulationPrevention(
bool Mode, int8u ZerosBefore_)
827 this->EmulationPrevention_Mode(Mode);
828 this->ZerosBefore=ZerosBefore_;
835 this->Readable_=Readable;
839 size_t Remain_Exact_EmulationPrevention()
841 Sync_EmulationPrevention();
845 const size_t End=BitEnd/8;
846 for (
size_t i=this->Checked; i<End; i++)
847 if (IsEmulationPrevention(i))
849 return Buffer_Size-Count*8;
855 const size_t Pos=(BitEnd-Buffer_Size)/8;
856 if (this->Checked>Pos || !Buffer_Size)
863 if (this->Checked>(BitEnd-Buffer_Size)/8)
864 this->Checked=(BitEnd-Buffer_Size)/8;
875 if ((
unsigned)(HowMany-1)>=(
unsigned)Max)
877 return Read<Max>(HowMany);
883 if ((
unsigned)(HowMany-1)>=(
unsigned)Max)
885 if (HowMany+WindowEnd>Buffer_Size)
887 if (Calls::Present && Calls::Active(*
this))
888 return (Word)Calls::Peek(*
this, HowMany);
889 if (HowMany>Buffer_Size)
896 if (Max>WordBits-7 && HowMany+WindowEnd>Buffer_Size)
897 return PeekDirect(Buffer_Size, HowMany);
899 return RotateRight(Window, Buffer_Size-HowMany)&Mask(HowMany);
906 if (HowMany+WindowEnd>Buffer_Size)
908 if (Calls::Present && Calls::Active(*
this))
909 return (Word)Calls::Read(*
this, HowMany);
910 if (HowMany>Buffer_Size)
917 if (Max>WordBits-7 && HowMany+WindowEnd>Buffer_Size)
918 return Read_Split(HowMany);
920 Buffer_Size-=HowMany;
921 return RotateRight(Window, Buffer_Size)&Mask(HowMany);
928 const unsigned High_Size=(unsigned)(Buffer_Size-WindowEnd);
929 Buffer_Size-=High_Size;
930 const Word High=RotateRight(Window, Buffer_Size)&Mask(High_Size);
932 const unsigned Low_Size=HowMany-High_Size;
933 Buffer_Size-=Low_Size;
934 return (High<<Low_Size)|(RotateRight(Window, Buffer_Size)&Mask(Low_Size));
940 if (HowMany<=WordBits)
941 return ReadWide(HowMany);
942 if (Calls::Present && Calls::Active(*
this))
943 return Calls::Read(*
this, HowMany);
944 const int64u High=ReadWide(HowMany-32);
945 return (High<<32)|ReadWide(32);
953 if (HowMany+WindowEnd<=Buffer_Size)
954 Value=RotateRight(Window, Buffer_Size-HowMany)&Mask(HowMany);
955 else if (Calls::Present && Calls::Active(*
this))
956 return (Word)Calls::Read(*
this, HowMany);
957 else if (HowMany<=Buffer_Size)
958 Value=PeekDirect(Buffer_Size, HowMany);
964 Buffer_Size-=HowMany;
973 if (HowMany+WindowEnd<=Remaining)
974 return RotateRight(Window, Remaining-HowMany)&Mask(HowMany);
975 return PeekDirect(Remaining, HowMany);
984 const size_t BitPos=BitEnd-Remaining;
985 size_t Load=BitPos/8;
988 const unsigned Shift=(unsigned)(BitPos-Load*8);
989 Word Value=LoadWord(Buffer+Load)<<Shift;
990 if (Shift+HowMany>WordBits)
991 Value|=(Word)(Buffer[Load+
sizeof(Word)]>>(8-Shift));
992 return Value>>(WordBits-HowMany);
1002 if (Buffer_Size<=WindowEnd)
1004 Bits=RotateRight(Window, Buffer_Size);
1010 Size=CountLeadingZeros(Bits)*2+1;
1011 return Size<=Buffer_Size-WindowEnd;
1023 if (PeekUE(Bits, Size))
1026 return (int32u)(Bits>>(WordBits-Size))-1;
1029 if (Calls::Present && Calls::Active(*
this))
1030 return Calls::GetUE(*
this);
1033 const int64u Result=GetUE_Bytes(Buffer, BitEnd-Buffer_Size, Buffer_Size);
1034 const size_t Code_Size=(size_t)(Result>>32);
1037 Buffer_Size-=Code_Size;
1038 return (int32u)Result;
1049 const unsigned Peeked=(unsigned)
MIN(
Remain, (
size_t)32);
1050 const int32u Prefix=ReadBytes(Buffer, BitPos, Peeked)<<(32-Peeked);
1053 unsigned LeadingZeroBits=0;
1054 while (!(Prefix>>(31-LeadingZeroBits)))
1056 if (LeadingZeroBits*2+1>
Remain)
1058 const int32u Info=LeadingZeroBits?ReadBytes(Buffer, BitPos+LeadingZeroBits+1, LeadingZeroBits):0;
1059 return ((int64u)(LeadingZeroBits*2+1)<<32)|((((int32u)1<<LeadingZeroBits)|Info)-1);
1063 static int32u ReadBytes(
const int8u* Buffer,
size_t BitPos,
unsigned HowMany)
1065 const int8u* Source=Buffer+BitPos/8;
1066 const unsigned Skipped=(unsigned)(BitPos%8);
1067 const unsigned Bytes=(Skipped+HowMany+7)/8;
1069 for (
unsigned i=0; i<Bytes; i++)
1070 Value=(Value<<8)|Source[i];
1071 return (int32u)((Value>>(Bytes*8-Skipped-HowMany))&(((int64u)1<<HowMany)-1));
1086 const int8u* Buffer;
1089 size_t Buffer_Size_Init;
1091 bool BufferUnderRun_;