/src/FreeRDP/libfreerdp/codec/test/TestFuzzNSCodec.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * libFuzzer harness for the NSCodec decoder |
4 | | */ |
5 | | |
6 | | #include <stddef.h> |
7 | | #include <stdint.h> |
8 | | |
9 | | #include <winpr/crt.h> |
10 | | #include <winpr/wlog.h> |
11 | | |
12 | | #include <freerdp/codec/color.h> |
13 | | #include <freerdp/codec/nsc.h> |
14 | | |
15 | | static int fuzz_nsc_message(UINT16 bpp, UINT32 width, UINT32 height, UINT32 dstFormat, |
16 | | const uint8_t* data, size_t size) |
17 | 1.74k | { |
18 | 1.74k | BYTE* dst = nullptr; |
19 | | |
20 | 1.74k | if (size > UINT32_MAX) |
21 | 0 | return 0; |
22 | | |
23 | 1.74k | NSC_CONTEXT* ctx = nsc_context_new(); |
24 | 1.74k | if (!ctx) |
25 | 0 | return 0; |
26 | | |
27 | 1.74k | SSIZE_T stride = (SSIZE_T)width * FreeRDPGetBytesPerPixel(dstFormat); |
28 | 1.74k | if (stride <= 0) |
29 | 0 | goto fail; |
30 | | |
31 | 1.74k | dst = calloc((size_t)height, (size_t)stride); |
32 | 1.74k | if (!dst) |
33 | 0 | goto fail; |
34 | | |
35 | 1.74k | (void)nsc_process_message(ctx, bpp, width, height, data, (UINT32)size, dst, dstFormat, |
36 | 1.74k | (UINT32)stride, 0, 0, width, height, FREERDP_FLIP_NONE); |
37 | | |
38 | 1.74k | fail: |
39 | 1.74k | free(dst); |
40 | 1.74k | nsc_context_free(ctx); |
41 | 1.74k | return 0; |
42 | 1.74k | } |
43 | | |
44 | | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) |
45 | 589 | { |
46 | 589 | static BOOL loggingInitialized = FALSE; |
47 | | |
48 | 589 | if (!loggingInitialized) |
49 | 1 | { |
50 | 1 | (void)WLog_SetLogLevel(WLog_GetRoot(), WLOG_TRACE); |
51 | 1 | loggingInitialized = TRUE; |
52 | 1 | } |
53 | | |
54 | 589 | if (size < 20) |
55 | 9 | return 0; |
56 | | |
57 | 580 | (void)fuzz_nsc_message(32, 64, 64, PIXEL_FORMAT_BGRA32, data, size); |
58 | 580 | (void)fuzz_nsc_message(24, 32, 32, PIXEL_FORMAT_RGBX32, data, size); |
59 | 580 | (void)fuzz_nsc_message(16, 17, 13, PIXEL_FORMAT_RGB16, data, size); |
60 | 580 | return 0; |
61 | 589 | } |