@@ -7,16 +7,16 @@ The existing `IFileType`s can be expanded in various ways.
77- Install nuget package into your project:
88
99``` powershell
10- Install-Package MagicBytesValidator -Version 2.1.6
10+ Install-Package MagicBytesValidator -Version 2.2.0
1111```
1212
1313``` bash
14- dotnet add package MagicBytesValidator --version 2.1.6
14+ dotnet add package MagicBytesValidator --version 2.2.0
1515```
1616
1717- Reference in your csproj:
1818``` xml
19- <PackageReference Include =" MagicBytesValidator" Version =" 2.1.6 " />
19+ <PackageReference Include =" MagicBytesValidator" Version =" 2.2.0 " />
2020```
2121
2222### How to use it?
@@ -47,6 +47,140 @@ var fileType = await streamFileTypeProvider.TryFindUnambiguousAsync(fileStream,
4747``` c#
4848var isValid = await validator .IsValidAsync (memoryStream , fileType , CancellationToken .None );
4949```
50+ ## Validation strictness (FileByteType)
51+
52+ Some formats support multiple validation strategies (e.g. strict vs. lazy rules).
53+ For this purpose, the library exposes ` FileByteType ` :
54+
55+ - ` FileByteType.Strict ` (default)
56+ - ` FileByteType.Lazy ` (optional relaxed/"lazy" rules for certain formats)
57+
58+ ### Validate an uploaded IFormFile with a specific validation type
59+
60+ The form file provider accepts an optional ` validationType ` parameter:
61+
62+ ``` c#
63+ var fileType = await formFileTypeProvider .FindValidatedTypeAsync (
64+ formFile ,
65+ null ,
66+ CancellationToken .None ,
67+ validationType : MagicBytesValidator .Models .FileByteType .Lazy
68+ );
69+ ```
70+
71+ Example using ` Lazy ` (lazy rules if the format supports it):
72+
73+ ``` c#
74+ var fileType = await formFileTypeProvider .FindValidatedTypeAsync (
75+ formFile ,
76+ null ,
77+ CancellationToken .None ,
78+ validationType : MagicBytesValidator .Models .FileByteType .Lazy
79+ );
80+ ```
81+
82+ ### Validate a stream with a specific validation type
83+
84+ The validator also accepts an optional ` validationType ` parameter:
85+
86+ ``` c#
87+ var isValid = await validator .IsValidAsync (
88+ memoryStream ,
89+ fileType ,
90+ CancellationToken .None ,
91+ validationType : MagicBytesValidator .Models .FileByteType .Strict
92+ );
93+ ```
94+
95+ Example using ` Lazy ` :
96+
97+ ``` c#
98+ var isValid = await validator .IsValidAsync (
99+ memoryStream ,
100+ fileType ,
101+ CancellationToken .None ,
102+ validationType : MagicBytesValidator .Models .FileByteType .Lazy
103+ );
104+ ```
105+
106+ > Note: If a format does not define any ` Lazy ` -specific checks, ` Lazy ` behaves like “global checks only”.
107+ > This keeps existing formats unchanged unless they opt into mode-specific rules.
108+
109+ ### Example: Lazy ("relaxed") PDF validation
110+
111+ Some PDFs contain additional trailing bytes after the ` %%EOF ` marker. While strict validation may require the file
112+ to end with ` %%EOF ` , lazy validation can accept the ` %%EOF ` marker anywhere within the last 1024 bytes of the file
113+ (behaviour tolerated by common PDF viewers).
114+
115+ ## Expand the file type mapping
116+
117+ - Get mapping:
118+
119+ ``` csharp
120+ // use the validator:
121+ var mapping = validator .Mapping ;
122+
123+ // use the formFileTypeProvider:
124+ var mapping = formFileTypeProvider .Mapping ;
125+
126+ // or create a new instance of the mapping:
127+ var mapping = new MagicBytesValidator .Services .Mapping ();
128+ ```
129+
130+ - Register a single ` FileByteFilter ` :
131+
132+ ``` csharp
133+ mapping .Register (
134+ new FileByteFilter (
135+ " traperto/trp" , // MIME type
136+ new [] { " trp" } // file extensions
137+ ) {
138+ // magic byte sequences
139+ StartsWith (new byte ?[]
140+ {
141+ 0x 78 , 0x 6c , 0x 2f , 0x 5f , 0x 72 , 0x 65
142+ })
143+ .EndsWith (new byte ?[]
144+ {
145+ 0x FF , 0x FF
146+ })
147+ }
148+ );
149+ ```
150+
151+ - ` FileByteFilter ` s with specific offset checks:
152+
153+ ``` csharp
154+ mapping .Register (
155+ new FileByteFilter (
156+ " traperto/trp" , // MIME type
157+ new [] { " trp" } // file extensions
158+ ) {
159+ // magic byte sequences
160+ Specific (new ByteCheck (512 , new byte ?[] { 0x FD }));
161+ }
162+ );
163+ ```
164+
165+ ` ByteCheck ` allows for negative offset values to look for a specific offset counting from the end of file.
166+
167+ ### Optional: register mode-specific magic byte checks (Strict/Lazy)
168+
169+ When configuring a ` FileByteFilter ` , fluent methods accept an optional ` FileByteType ` parameter.
170+ If omitted, the check is global (applies to all validation types). If specified, the check applies only
171+ to that validation type.
172+
173+ Example:
174+
175+ ``` csharp
176+ StartsWith (new byte ?[] { 0x 25 , 0x 50 , 0x 44 , 0x 46 , 0x 2D }) // global
177+ .EndsWithAnyOf (new []
178+ {
179+ new byte ?[] { 0x 25 , 0x 25 , 0x 45 , 0x 4F , 0x 46 }
180+ }, MagicBytesValidator .Models .FileByteType .Strict ) // strict only
181+ .TailContains (1024 , new byte ?[] { 0x 25 , 0x 25 , 0x 45 , 0x 4F , 0x 46 },
182+ MagicBytesValidator .Models .FileByteType .Lazy ); // lazy only
183+ ```
50184
51185#### Expand the file type mapping
52186
@@ -59,7 +193,7 @@ var mapping = validator.Mapping;
59193var mapping = formFileTypeProvider .Mapping ;
60194
61195// or create a new instance of the mapping:
62- var mapping = new MagicBytesValidator .Services .Mapping ();
196+ var mapping = new MagicBytesValidator .Services .Mapping ();
63197```
64198
65199- Register a single ` FileByteFilter ` :
@@ -191,4 +325,4 @@ This can be useful when debugging or validating newly added FileTypes.
191325▓▓ ▓▓▓▓▓▓▓▓▓▓
192326▓▓
193327▓▓▓▓▓▓▓▓▓ ▓▓
194- ```
328+ ```
0 commit comments