private static void EncodeMatch(CompressedBinaryWriter output, int matchSize, int matchPos) { if (matchPos < 0x100) { output.WriteFlag(true); output.Write((byte)matchPos); output.WriteFlag(false); } else { var high = matchPos >> 8; var low = matchPos & 0xFF; output.WriteFlag(true); output.WriteFlag(true); var flagValue = ((high >> 4) & 0x01) == 0x01; output.WriteFlag(flagValue); flagValue = ((high >> 3) & 0x01) == 0x01; output.WriteFlag(flagValue); flagValue = ((high >> 2) & 0x01) == 0x01; output.WriteFlag(flagValue); flagValue = ((high >> 1) & 0x01) == 0x01; output.WriteFlag(flagValue); output.Write((byte)low); flagValue = (high & 0x01) == 0x01; output.WriteFlag(flagValue); } output.WriteCopyCount(matchSize); }
private static void EncodeRepeat(CompressedBinaryWriter output, byte repeatByte, int size) { if (size < 0x0E) { output.Write(repeatByte); output.WriteFlag(false); EncodeMatch(output, size - 1, 1); } else { size -= 0x0E; output.WriteFlagValue(0b110000, 6); output.Write((byte)0x01); output.WriteFlag(false); if (size < 0x10) { output.WriteFlag(false); var flagValue = ((size >> 3) & 0x01) == 0x01; output.WriteFlag(flagValue); flagValue = ((size >> 2) & 0x01) == 0x01; output.WriteFlag(flagValue); flagValue = ((size >> 1) & 0x01) == 0x01; output.WriteFlag(flagValue); output.Write(repeatByte); flagValue = (size & 0x01) == 0x01; output.WriteFlag(flagValue); } else { var high = size >> 8; var low = size & 0xFF; output.WriteFlag(true); var flagValue = ((high >> 3) & 0x01) == 0x01; output.WriteFlag(flagValue); flagValue = ((high >> 2) & 0x01) == 0x01; output.WriteFlag(flagValue); flagValue = ((high >> 1) & 0x01) == 0x01; output.WriteFlag(flagValue); output.Write((byte)low); output.Write(repeatByte); flagValue = (high & 0x01) == 0x01; output.WriteFlag(flagValue); } } }
public byte[] ToBinary() { using (MemoryStream ms = new MemoryStream()) { var bw = new CompressedBinaryWriter(ms); bw.Write(File); bw.Write(Scope); bw.Write(ServerId); bw.Flush(); return(ms.ToArray()); } }
public byte[] GetIdSource(Package package) { using (MemoryStream ms = new MemoryStream()) { var bw = new CompressedBinaryWriter(ms); bw.Write(package.Type.ToLowerSafe()); bw.Write(package.Server.Type.ToLowerSafe()); bw.Write(package.Server.Id); bw.Write(package.Server.Path); bw.Write(package.Created); //ms.WriteClaimString(package.Server.Id); // Id can always be calculated bw.Flush(); return(ms.ToArray()); } }
/// <summary> /// Gets the full binary data of the claim. /// Cannot not be used for ID calculation, as it contains the proof from Issuer and subject. /// </summary> /// <param name="claim"></param> /// <returns></returns> public byte[] Serialize(Claim claim) { using (MemoryStream ms = new MemoryStream()) { var bw = new CompressedBinaryWriter(ms); BuildSource(claim, bw); bw.Write(claim.Issuer.Proof); bw.Write(claim.Subject.Proof); bw.Write(claim.TemplateId); // ID is not added as it is dependen on the issuer/subject type foreach (var timestamp in claim.Timestamps) { // TODO: Need to add timestamps here! } bw.Flush(); return(ms.ToArray()); } }
/// <summary> /// Gets the full binary data of the claim. /// Cannot not be used for ID calculation, as it contains the proof from Issuer and subject. /// </summary> /// <param name="claim"></param> /// <returns></returns> public byte[] Serialize(QueryRequest queryRequest) { using (MemoryStream ms = new MemoryStream()) { var bw = new CompressedBinaryWriter(ms); BuildSource(queryRequest, bw); bw.Write(queryRequest.Issuer.Proof); bw.Flush(); return(((MemoryStream)bw.BaseStream).ToArray()); } }
public void WriteString() { using (var stream = new MemoryStream()) { var writer = new CompressedBinaryWriter(stream); var source = "Hello"; writer.Write(source); var total = stream.Length; Console.WriteLine($"Text length: {source.Length} - Stream length: {stream.Length}"); stream.Position = 0; var reader = new CompressedBinaryReader(stream); var target = reader.ReadString(); Assert.IsTrue(source == target); Assert.IsTrue(stream.Length == total); } }
public void WriteBytes() { using (var stream = new MemoryStream()) { var bytes = Encoding.UTF8.GetBytes("Hello"); var writer = new CompressedBinaryWriter(stream); var total = writer.Write(bytes); stream.Position = 0; var reader = new CompressedBinaryReader(stream); var readBytes = reader.ReadBytes(); Assert.IsTrue(readBytes.Length == bytes.Length); Assert.IsTrue(ByteExtensions.Equals(readBytes, bytes)); Assert.IsTrue(stream.Length == total); } }
/// <summary> /// Get the binary data for ID calculation. Do not include the proof of Issuer and subject. /// </summary> /// <param name="claim"></param> /// <param name="ms"></param> private static void BuildSource(QueryRequest queryRequest, CompressedBinaryWriter bw) { bw.Write(queryRequest.Issuer.Type.ToLowerSafe()); bw.Write(queryRequest.Issuer.Id); bw.Write(queryRequest.Scope); bw.Write((byte)queryRequest.Flags); bw.Write(queryRequest.Types.Count); foreach (var type in queryRequest.Types) { bw.Write(type); } bw.Write(queryRequest.Subjects.Count); foreach (var subject in queryRequest.Subjects) { bw.Write(subject); } }
private static byte[] CompressChunk(byte[] data, ref int pos) { var CHUNK_SIZE_CMP_MAX = 0xFFC0; var startPos = pos; var endLimit = Math.Min(startPos + CHUNK_SIZE_UNC_MAX, data.Length); using (var outputMemoryStream = new MemoryStream()) { using (var output = new CompressedBinaryWriter(outputMemoryStream)) { output.InitializeFlags(); while (pos < data.Length) { if (output.Length >= CHUNK_SIZE_CMP_MAX || pos - startPos >= CHUNK_SIZE_UNC_MAX) { break; } var match = FindMatch(data, pos, startPos, endLimit); var repeat = FindRepeat(data, pos, endLimit); if (repeat.Length > 0 && match.Length > 0) { if (repeat.Length >= 0x40) { //Debug.WriteLine($"Repeat\t{repeat.Length}\t{match.Length}\t{match.Position}"); EncodeRepeat(output, data[pos], repeat.Length); pos += repeat.Length; } else if (match.Length >= 0x40) { //Debug.WriteLine($"Match\t{match.Length}\t{match.Position}\t{repeat.Length}"); EncodeMatch(output, match.Length, match.Position); pos += match.Length; } else if (match.Length >= repeat.Length) { //Debug.WriteLine($"Match\t{match.Length}\t{match.Position}\t{repeat.Length}"); EncodeMatch(output, match.Length, match.Position); pos += match.Length; } else if (repeat.Length >= 0x0E) { //Debug.WriteLine($"Repeat\t{repeat.Length}\t{match.Length}\t{match.Position}"); EncodeRepeat(output, data[pos], repeat.Length); pos += repeat.Length; } else { //Debug.WriteLine($"Match\t{match.Length}\t{match.Position}\t{repeat.Length}"); EncodeMatch(output, match.Length, match.Position); pos += match.Length; } } else if (repeat.Length > 0) { //Debug.WriteLine($"Repeat\t{repeat.Length}"); EncodeRepeat(output, data[pos], repeat.Length); pos += repeat.Length; } else if (match.Length > 0) { //Debug.WriteLine($"Match\t{match.Length}\t{match.Position}"); EncodeMatch(output, match.Length, match.Position); pos += match.Length; } else { output.Write(data[pos]); pos++; output.WriteFlag(false); } } output.WriteFlagValue(0b110000, 6); output.Write((byte)0x00); output.WriteFlag(false); } return(outputMemoryStream.ToArray()); } }
/// <summary> /// Get the binary data for ID calculation. Do not include the proof of Issuer and subject. /// </summary> /// <param name="claim"></param> /// <param name="bw"></param> private static void BuildSource(Claim claim, CompressedBinaryWriter bw) { bw.Write(claim.Type.ToLowerSafe()); // First type! bw.Write(claim.Issuer.Type.ToLowerSafe()); bw.Write(claim.Issuer.Id); bw.Write(claim.Issuer.Path); bw.Write(claim.Subject.Type.ToLowerSafe()); bw.Write(claim.Subject.Id); bw.Write(claim.Subject.Path); bw.Write(claim.Value); bw.Write(claim.Scope); bw.Write(claim.Note); bw.Write(claim.Created); bw.Write(claim.Activate); bw.Write(claim.Expire); }