1
0
mirror of https://github.com/chylex/Brotli-Builder.git synced 2025-08-16 21:31:47 +02:00
Files
BrotliBuilder
BrotliCalc
BrotliImpl
BrotliLib
Brotli
Components
Compressed
Data
Header
BlockTypeInfo.cs
ContextMap.cs
DataLength.cs
DistanceParameters.cs
HuffmanTree.Type.Complex.cs
HuffmanTree.Type.Simple.cs
HuffmanTree.cs
HuffmanTreeLengthCode.cs
LiteralContextMode.cs
VariableLength11Code.cs
MetaBlock.Type.Compressed.cs
MetaBlock.Type.LastEmpty.cs
MetaBlock.Type.PaddedEmpty.cs
MetaBlock.Type.Uncompressed.cs
MetaBlock.cs
WindowSize.cs
Dictionary
Encode
Output
Parameters
Streaming
Utils
BrotliFileStructure.cs
BrotliGlobalState.cs
Collections
Exceptions
Markers
Numbers
Serialization
BrotliLib.csproj
LICENSE-BROTLI.txt
UnitTests
.gitignore
BrotliBuilder.sln
LICENSE
README.md

71 lines
2.2 KiB
C#

using System;
using BrotliLib.Numbers;
using BrotliLib.Serialization;
namespace BrotliLib.Brotli.Components.Header{
/// <summary>
/// Describes a value in the range [1; 256], which can be encoded with a variable 1-11 bit code.
/// https://tools.ietf.org/html/rfc7932#section-9.2
/// </summary>
public sealed class VariableLength11Code{
public const int MinValue = 1;
public const int MaxValue = 256;
public static readonly IntRange Range = new IntRange(MinValue, MaxValue);
// Data
public int Value { get; }
public VariableLength11Code(int value){
if (!Range.Contains(value)){
throw new ArgumentOutOfRangeException(nameof(value), "The value must be in the range " + Range + ".");
}
this.Value = value;
}
// Object
public override bool Equals(object obj){
return obj is VariableLength11Code code &&
Value == code.Value;
}
public override int GetHashCode(){
return HashCode.Combine(Value);
}
public override string ToString(){
return "Value = " + Value;
}
// Serialization
public static readonly BitDeserializer<VariableLength11Code, NoContext> Deserialize = (reader, context) => {
if (!reader.NextBit()){
return new VariableLength11Code(MinValue);
}
else{
int chunkBits = reader.NextChunk(3);
return new VariableLength11Code(MinValue + (1 << chunkBits) + reader.NextChunk(chunkBits));
}
};
public static readonly BitSerializer<VariableLength11Code, NoContext> Serialize = (writer, obj, context) => {
if (obj.Value == MinValue){
writer.WriteBit(false);
}
else{
writer.WriteBit(true);
int offsetValue = obj.Value - MinValue;
int chunkBits = offsetValue == 0 ? 0 : (int)Math.Floor(Math.Log(offsetValue, 2));
writer.WriteChunk(3, chunkBits);
writer.WriteChunk(chunkBits, offsetValue - (1 << chunkBits));
}
};
}
}