Snowberry.IO.Common
5.0.1
See the version list below for details.
dotnet add package Snowberry.IO.Common --version 5.0.1
NuGet\Install-Package Snowberry.IO.Common -Version 5.0.1
<PackageReference Include="Snowberry.IO.Common" Version="5.0.1" />
<PackageVersion Include="Snowberry.IO.Common" Version="5.0.1" />
<PackageReference Include="Snowberry.IO.Common" />
paket add Snowberry.IO.Common --version 5.0.1
#r "nuget: Snowberry.IO.Common, 5.0.1"
#:package Snowberry.IO.Common@5.0.1
#addin nuget:?package=Snowberry.IO.Common&version=5.0.1
#tool nuget:?package=Snowberry.IO.Common&version=5.0.1
Snowberry.IO.Common
The shared contracts and primitives of Snowberry.IO. This package defines the IEndianReader and IEndianWriter interfaces, the EndianType byte order, the Sha1 value type, RegionRange, the Analyzer base class, BinaryEndianConverter and the BinaryUtils alignment helpers. Reference it directly when a library only needs the contracts or the converter; use Snowberry.IO for the stream-based implementations. It targets .NET 10 plus .NET Standard 2.1 and 2.0; the .NET Standard 2.0 build depends on System.Memory and IndexRange for spans and Range.
Table of Contents
- Installation
- Quick Start
- Top Features at a Glance
- Core Types
- Reader and Writer Contracts
- Pitfalls
- Related Packages
Installation
dotnet add package Snowberry.IO.Common
Quick Start
- Read and write bytes in a chosen byte order.
- Align a position.
- Parse and format a SHA-1 hash.
using System;
using Snowberry.IO.Common;
byte[] data = new byte[] { 0x00, 0x00, 0x00, 0x2A, 0x2A, 0x00, 0x00, 0x00 };
int big = BinaryEndianConverter.ToInt32(data, EndianType.BIG); // 42, from the start of the span
int little = BinaryEndianConverter.ToInt32(data, 4, EndianType.LITTLE); // 42, from offset 4
BinaryEndianConverter.WriteInt32(data, 0, 7, EndianType.LITTLE); // data starts with 07 00 00 00
long position = 13;
long padding = BinaryUtils.CalculatePadding(position, 8); // 3
BinaryUtils.ApplyAlignment(ref position, 8); // position is now 16
var hash = new Sha1("da39a3ee5e6b4b0d3255bfef95601890afd80709");
Console.WriteLine(hash); // DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
Top Features at a Glance
BinaryEndianConverter. Bounds-checked conversion between bytes andshort,ushort,int,uint,long,ulong,float,doubleandGuidin either byte order:To…reads from aReadOnlySpan<byte>andWrite…writes to aSpan<byte>. Every type has an overload that takes an offset; all butGuidalso have one that uses the start of the span.Sha1. A 20-byte hash value with equality, uppercase hex formatting throughToString(), parsing from a 40-character hex string,GetHashBuffer()and allocation-freeTryWriteBytes. It also builds from a 20-bytebyte[]or span (Fromrefills an existing value), ands_Zerois the all-zero hash.IEndianReaderandIEndianWriter. The contracts implemented by the Snowberry.IO readers and writers, so code can accept any reader or writer. See Reader and Writer Contracts.Analyzer. A base class for observing or rewriting bytes right after a reader reads them.BinaryUtils.CalculatePadding(long position, byte alignment)returns the padding bytes needed, andApplyAlignment(ref long position, byte alignment)moves the position forward to the next aligned value. The alignment must be a power of two, so the largest is 128.
Core Types
| Type | Namespace | Purpose |
|---|---|---|
EndianType |
Snowberry.IO.Common |
Byte order: LITTLE or BIG. |
BinaryEndianConverter |
Snowberry.IO.Common |
Converts between spans and primitives or Guid in either byte order. |
BinaryUtils |
Snowberry.IO.Common |
Alignment and padding helpers. |
Sha1 |
Snowberry.IO.Common |
20-byte SHA-1 hash value. |
RegionRange |
Snowberry.IO.Common.Reader |
Start position and size of a reader region view. |
Analyzer |
Snowberry.IO.Common.Reader |
Base class that sees, and may change, every read byte. |
IEndianReader |
Snowberry.IO.Common.Reader.Interfaces |
The reader contract. |
IEndianWriter |
Snowberry.IO.Common.Writer.Interfaces |
The writer contract. |
Reader and Writer Contracts
IEndianReadermirrorsBinaryReader.ReadInt32()reads little-endian, andReadInt32(EndianType.BIG)picks the byte order. On top of that it addsTryRead*methods that returnfalseinstead of throwing at the end of the data,ReadExactly,ReadCString,ReadSizedCString,ReadLine,ReadSha1,ReadAlignment,ReadUntilEnd,CopyTo, region views (EnableRegionView,DisableRegionView) and theAnalyzerproperty.IEndianWriterfollowsBinaryWriterwith two differences. EveryWritereturns the writer, so calls chain, and the byte order is an optional parameter, as inWrite(int value, EndianType endian = EndianType.LITTLE). It addsWriteCString,WriteSizedCString,WriteLine,WriteStringCharacters,WritePaddingandWrite(Sha1).Analyzerhas two required members.Initialize(IEndianReader)runs when a reader is created with the analyzer, andAnalyzeReadBytes(IEndianReader, Span<byte>, int)receives each read. OverrideModifiesBytesto returnfalsewhen the analyzer only observes.
For worked examples of region views and a custom analyzer, see Region Views and Custom Analyzer in the Snowberry.IO README.
Pitfalls
- Converters throw on short input. Every
BinaryEndianConverteroverload throwsArgumentOutOfRangeExceptionwhen fewer bytes are available than the type needs, or when the offset is negative, instead of reading or writing past the span. A failed write leaves the span unchanged. - Alignment must be a power of two.
BinaryUtilsthrowsArgumentOutOfRangeExceptionfor0or values such as3. Rangeconverts toRegionRangeonly when both ends count from the start.(RegionRange)(20..80)andRegionRange.ToRegionRange(20..80)work, but^16..and open ranges such as20..(whose end is^0) throwArgumentException. Pass those toIEndianReader.EnableRegionView(Range), which resolves them against the reader's length.Sha1components are not the SHA-1 state words. Each component A to E is the little-endian reading of a 4-byte slice of the digest, not H0 to H4. Compare hashes with==,ToString()or the bytes fromTryWriteBytes, not with hand-built components.ModifiesBytesmust staytrueif the analyzer changes bytes. Null-terminated string and line reads search for the terminator after the analyzer has run; returningfalselets them search first and report bytes in larger pieces, so an analyzer that decrypts would be searched before decryption.Halfis only on the .NET 10 build.ReadHalfandWrite(Half)are not part of the interfaces in the .NET Standard builds.
Related Packages
| Package | Purpose |
|---|---|
| Snowberry.IO | Stream and process-memory readers and the stream writer. |
| Snowberry.IO.SingleFile | Reads, modifies and creates bundles of single-file published .NET applications. |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- IndexRange (>= 1.1.1)
- System.Memory (>= 4.6.3)
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Snowberry.IO.Common:
| Package | Downloads |
|---|---|
|
Snowberry.IO
An endian-aware binary reader and writer for .NET: reads and writes primitives, strings, Guid and SHA-1 values in little- or big-endian order, with region views, signature scanning and Windows process memory reading. |
GitHub repositories
This package is not used by any popular GitHub repositories.