Skip to content

Commit 0873fe5

Browse files
Add Guard checks, don't pass null,
1 parent 29b709d commit 0873fe5

File tree

3 files changed

+117
-111
lines changed

3 files changed

+117
-111
lines changed

src/ImageSharp/Image.FromBytes.cs

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ public static IImageFormat DetectFormat(byte[] data)
2626
/// <summary>
2727
/// By reading the header on the provided byte array this calculates the images format.
2828
/// </summary>
29-
/// <param name="config">The configuration.</param>
29+
/// <param name="configuration">The configuration.</param>
3030
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
3131
/// <returns>The mime type or null if none found.</returns>
32-
public static IImageFormat DetectFormat(Configuration config, byte[] data)
32+
public static IImageFormat DetectFormat(Configuration configuration, byte[] data)
3333
{
34-
using (var stream = new MemoryStream(data))
34+
Guard.NotNull(configuration, nameof(configuration));
35+
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
3536
{
36-
return DetectFormat(config, stream);
37+
return DetectFormat(configuration, stream);
3738
}
3839
}
3940

@@ -61,19 +62,19 @@ public static IImageFormat DetectFormat(Configuration config, byte[] data)
6162
/// <summary>
6263
/// Reads the raw image information from the specified stream without fully decoding it.
6364
/// </summary>
64-
/// <param name="config">The configuration.</param>
65+
/// <param name="configuration">The configuration.</param>
6566
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
6667
/// <param name="format">The format type of the decoded image.</param>
6768
/// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
6869
/// <returns>
6970
/// The <see cref="IImageInfo"/> or null if suitable info detector is not found.
7071
/// </returns>
71-
public static IImageInfo Identify(Configuration config, byte[] data, out IImageFormat format)
72+
public static IImageInfo Identify(Configuration configuration, byte[] data, out IImageFormat format)
7273
{
73-
config ??= Configuration.Default;
74-
using (var stream = new MemoryStream(data))
74+
Guard.NotNull(configuration, nameof(configuration));
75+
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
7576
{
76-
return Identify(config, stream, out format);
77+
return Identify(configuration, stream, out format);
7778
}
7879
}
7980

@@ -108,33 +109,33 @@ public static Image<TPixel> Load<TPixel>(byte[] data, out IImageFormat format)
108109
/// <summary>
109110
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte array.
110111
/// </summary>
111-
/// <param name="config">The configuration options.</param>
112+
/// <param name="configuration">The configuration options.</param>
112113
/// <param name="data">The byte array containing encoded image data.</param>
113114
/// <typeparam name="TPixel">The pixel format.</typeparam>
114115
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
115-
public static Image<TPixel> Load<TPixel>(Configuration config, byte[] data)
116+
public static Image<TPixel> Load<TPixel>(Configuration configuration, byte[] data)
116117
where TPixel : unmanaged, IPixel<TPixel>
117118
{
118119
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
119120
{
120-
return Load<TPixel>(config, stream);
121+
return Load<TPixel>(configuration, stream);
121122
}
122123
}
123124

124125
/// <summary>
125126
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte array.
126127
/// </summary>
127-
/// <param name="config">The configuration options.</param>
128+
/// <param name="configuration">The configuration options.</param>
128129
/// <param name="data">The byte array containing encoded image data.</param>
129130
/// <param name="format">The <see cref="IImageFormat"/> of the decoded image.</param>
130131
/// <typeparam name="TPixel">The pixel format.</typeparam>
131132
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
132-
public static Image<TPixel> Load<TPixel>(Configuration config, byte[] data, out IImageFormat format)
133+
public static Image<TPixel> Load<TPixel>(Configuration configuration, byte[] data, out IImageFormat format)
133134
where TPixel : unmanaged, IPixel<TPixel>
134135
{
135136
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
136137
{
137-
return Load<TPixel>(config, stream, out format);
138+
return Load<TPixel>(configuration, stream, out format);
138139
}
139140
}
140141

@@ -157,17 +158,17 @@ public static Image<TPixel> Load<TPixel>(byte[] data, IImageDecoder decoder)
157158
/// <summary>
158159
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte array.
159160
/// </summary>
160-
/// <param name="config">The Configuration.</param>
161+
/// <param name="configuration">The Configuration.</param>
161162
/// <param name="data">The byte array containing encoded image data.</param>
162163
/// <param name="decoder">The decoder.</param>
163164
/// <typeparam name="TPixel">The pixel format.</typeparam>
164165
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
165-
public static Image<TPixel> Load<TPixel>(Configuration config, byte[] data, IImageDecoder decoder)
166+
public static Image<TPixel> Load<TPixel>(Configuration configuration, byte[] data, IImageDecoder decoder)
166167
where TPixel : unmanaged, IPixel<TPixel>
167168
{
168169
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
169170
{
170-
return Load<TPixel>(config, stream, decoder);
171+
return Load<TPixel>(configuration, stream, decoder);
171172
}
172173
}
173174

@@ -184,18 +185,18 @@ public static IImageFormat DetectFormat(ReadOnlySpan<byte> data)
184185
/// <summary>
185186
/// By reading the header on the provided byte array this calculates the images format.
186187
/// </summary>
187-
/// <param name="config">The configuration.</param>
188+
/// <param name="configuration">The configuration.</param>
188189
/// <param name="data">The byte array containing encoded image data to read the header from.</param>
189190
/// <returns>The mime type or null if none found.</returns>
190-
public static IImageFormat DetectFormat(Configuration config, ReadOnlySpan<byte> data)
191+
public static IImageFormat DetectFormat(Configuration configuration, ReadOnlySpan<byte> data)
191192
{
192-
int maxHeaderSize = config.MaxHeaderSize;
193+
int maxHeaderSize = configuration.MaxHeaderSize;
193194
if (maxHeaderSize <= 0)
194195
{
195196
return null;
196197
}
197198

198-
foreach (IImageFormatDetector detector in config.ImageFormatsManager.FormatDetectors)
199+
foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors)
199200
{
200201
IImageFormat f = detector.DetectFormat(data);
201202

@@ -243,32 +244,32 @@ public static Image<TPixel> Load<TPixel>(ReadOnlySpan<byte> data, IImageDecoder
243244
/// <summary>
244245
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte span.
245246
/// </summary>
246-
/// <param name="config">The configuration options.</param>
247+
/// <param name="configuration">The configuration options.</param>
247248
/// <param name="data">The byte span containing encoded image data.</param>
248249
/// <typeparam name="TPixel">The pixel format.</typeparam>
249250
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
250-
public static unsafe Image<TPixel> Load<TPixel>(Configuration config, ReadOnlySpan<byte> data)
251+
public static unsafe Image<TPixel> Load<TPixel>(Configuration configuration, ReadOnlySpan<byte> data)
251252
where TPixel : unmanaged, IPixel<TPixel>
252253
{
253254
fixed (byte* ptr = &data.GetPinnableReference())
254255
{
255256
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
256257
{
257-
return Load<TPixel>(config, stream);
258+
return Load<TPixel>(configuration, stream);
258259
}
259260
}
260261
}
261262

262263
/// <summary>
263264
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte span.
264265
/// </summary>
265-
/// <param name="config">The Configuration.</param>
266+
/// <param name="configuration">The Configuration.</param>
266267
/// <param name="data">The byte span containing image data.</param>
267268
/// <param name="decoder">The decoder.</param>
268269
/// <typeparam name="TPixel">The pixel format.</typeparam>
269270
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
270271
public static unsafe Image<TPixel> Load<TPixel>(
271-
Configuration config,
272+
Configuration configuration,
272273
ReadOnlySpan<byte> data,
273274
IImageDecoder decoder)
274275
where TPixel : unmanaged, IPixel<TPixel>
@@ -277,21 +278,21 @@ public static unsafe Image<TPixel> Load<TPixel>(
277278
{
278279
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
279280
{
280-
return Load<TPixel>(config, stream, decoder);
281+
return Load<TPixel>(configuration, stream, decoder);
281282
}
282283
}
283284
}
284285

285286
/// <summary>
286287
/// Load a new instance of <see cref="Image{TPixel}"/> from the given encoded byte span.
287288
/// </summary>
288-
/// <param name="config">The configuration options.</param>
289+
/// <param name="configuration">The configuration options.</param>
289290
/// <param name="data">The byte span containing image data.</param>
290291
/// <param name="format">The <see cref="IImageFormat"/> of the decoded image.</param>
291292
/// <typeparam name="TPixel">The pixel format.</typeparam>
292293
/// <returns>A new <see cref="Image{TPixel}"/>.</returns>
293294
public static unsafe Image<TPixel> Load<TPixel>(
294-
Configuration config,
295+
Configuration configuration,
295296
ReadOnlySpan<byte> data,
296297
out IImageFormat format)
297298
where TPixel : unmanaged, IPixel<TPixel>
@@ -300,7 +301,7 @@ public static unsafe Image<TPixel> Load<TPixel>(
300301
{
301302
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
302303
{
303-
return Load<TPixel>(config, stream, out format);
304+
return Load<TPixel>(configuration, stream, out format);
304305
}
305306
}
306307
}
@@ -325,38 +326,38 @@ public static Image Load(byte[] data, out IImageFormat format) =>
325326
/// <summary>
326327
/// Load a new instance of <see cref="Image"/> from the given encoded byte array.
327328
/// </summary>
328-
/// <param name="config">The config for the decoder.</param>
329+
/// <param name="configuration">The configuration for the decoder.</param>
329330
/// <param name="data">The byte array containing encoded image data.</param>
330331
/// <returns>The <see cref="Image"/>.</returns>
331-
public static Image Load(Configuration config, byte[] data) => Load(config, data, out _);
332+
public static Image Load(Configuration configuration, byte[] data) => Load(configuration, data, out _);
332333

333334
/// <summary>
334335
/// Load a new instance of <see cref="Image"/> from the given encoded byte array.
335336
/// </summary>
336-
/// <param name="config">The config for the decoder.</param>
337+
/// <param name="configuration">The configuration for the decoder.</param>
337338
/// <param name="data">The byte array containing image data.</param>
338339
/// <param name="decoder">The decoder.</param>
339340
/// <returns>The <see cref="Image"/>.</returns>
340-
public static Image Load(Configuration config, byte[] data, IImageDecoder decoder)
341+
public static Image Load(Configuration configuration, byte[] data, IImageDecoder decoder)
341342
{
342343
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
343344
{
344-
return Load(config, stream, decoder);
345+
return Load(configuration, stream, decoder);
345346
}
346347
}
347348

348349
/// <summary>
349350
/// Load a new instance of <see cref="Image"/> from the given encoded byte array.
350351
/// </summary>
351-
/// <param name="config">The config for the decoder.</param>
352+
/// <param name="configuration">The configuration for the decoder.</param>
352353
/// <param name="data">The byte array containing image data.</param>
353354
/// <param name="format">The mime type of the decoded image.</param>
354355
/// <returns>The <see cref="Image"/>.</returns>
355-
public static Image Load(Configuration config, byte[] data, out IImageFormat format)
356+
public static Image Load(Configuration configuration, byte[] data, out IImageFormat format)
356357
{
357358
using (var stream = new MemoryStream(data, 0, data.Length, false, true))
358359
{
359-
return Load(config, stream, out format);
360+
return Load(configuration, stream, out format);
360361
}
361362
}
362363

@@ -388,49 +389,49 @@ public static Image Load(ReadOnlySpan<byte> data, out IImageFormat format) =>
388389
/// <summary>
389390
/// Decodes a new instance of <see cref="Image"/> from the given encoded byte span.
390391
/// </summary>
391-
/// <param name="config">The configuration options.</param>
392+
/// <param name="configuration">The configuration options.</param>
392393
/// <param name="data">The byte span containing image data.</param>
393394
/// <returns>The <see cref="Image"/>.</returns>
394-
public static Image Load(Configuration config, ReadOnlySpan<byte> data) => Load(config, data, out _);
395+
public static Image Load(Configuration configuration, ReadOnlySpan<byte> data) => Load(configuration, data, out _);
395396

396397
/// <summary>
397398
/// Load a new instance of <see cref="Image"/> from the given encoded byte span.
398399
/// </summary>
399-
/// <param name="config">The Configuration.</param>
400+
/// <param name="configuration">The Configuration.</param>
400401
/// <param name="data">The byte span containing image data.</param>
401402
/// <param name="decoder">The decoder.</param>
402403
/// <returns>The <see cref="Image"/>.</returns>
403404
public static unsafe Image Load(
404-
Configuration config,
405+
Configuration configuration,
405406
ReadOnlySpan<byte> data,
406407
IImageDecoder decoder)
407408
{
408409
fixed (byte* ptr = &data.GetPinnableReference())
409410
{
410411
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
411412
{
412-
return Load(config, stream, decoder);
413+
return Load(configuration, stream, decoder);
413414
}
414415
}
415416
}
416417

417418
/// <summary>
418419
/// Load a new instance of <see cref="Image"/> from the given encoded byte span.
419420
/// </summary>
420-
/// <param name="config">The configuration options.</param>
421+
/// <param name="configuration">The configuration options.</param>
421422
/// <param name="data">The byte span containing image data.</param>
422423
/// <param name="format">The <see cref="IImageFormat"/> of the decoded image.</param>>
423424
/// <returns>The <see cref="Image"/>.</returns>
424425
public static unsafe Image Load(
425-
Configuration config,
426+
Configuration configuration,
426427
ReadOnlySpan<byte> data,
427428
out IImageFormat format)
428429
{
429430
fixed (byte* ptr = &data.GetPinnableReference())
430431
{
431432
using (var stream = new UnmanagedMemoryStream(ptr, data.Length))
432433
{
433-
return Load(config, stream, out format);
434+
return Load(configuration, stream, out format);
434435
}
435436
}
436437
}

0 commit comments

Comments
 (0)