/src/pjsip/tests/fuzz/fuzz-h264.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2023 Teluu Inc. (http://www.teluu.com) |
3 | | * |
4 | | * This program is free software; you can redistribute it and/or modify |
5 | | * it under the terms of the GNU General Public License as published by |
6 | | * the Free Software Foundation; either version 2 of the License, or |
7 | | * (at your option) any later version. |
8 | | * |
9 | | * This program is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU General Public License |
15 | | * along with this program; if not, write to the Free Software |
16 | | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | | */ |
18 | | #include <stdio.h> |
19 | | #include <stdint.h> |
20 | | #include <stdlib.h> |
21 | | |
22 | | #include <pjlib.h> |
23 | | #include <pjmedia-codec/h264_packetizer.h> |
24 | | |
25 | | #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
26 | 246 | #define kMinInputLength 10 |
27 | 117 | #define kMaxInputLength 5120 |
28 | | |
29 | | pj_pool_factory *mem; |
30 | | |
31 | | int h264_unpacketizer(const uint8_t *data, size_t size, |
32 | | uint8_t *output, size_t output_size) |
33 | 98 | { |
34 | 98 | int ret = 0; |
35 | 98 | pj_pool_t *pool; |
36 | 98 | pj_status_t status; |
37 | 98 | pjmedia_h264_packetizer_cfg cfg; |
38 | 98 | pjmedia_h264_packetizer *pktz; |
39 | 98 | unsigned bits_pos = 0; |
40 | | |
41 | 98 | pool = pj_pool_create(mem, "h264_test", 1000, 1000, NULL); |
42 | | |
43 | 98 | pj_bzero(&cfg, sizeof(cfg)); |
44 | 98 | cfg.mtu = 1500; |
45 | 98 | cfg.unpack_nal_start = 4; |
46 | 98 | cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED; |
47 | | |
48 | 98 | status = pjmedia_h264_packetizer_create(pool, &cfg, &pktz); |
49 | | |
50 | 98 | if (status == PJ_SUCCESS) { |
51 | 98 | status = pjmedia_h264_unpacketize(pktz, data, size, output, |
52 | 98 | output_size, &bits_pos); |
53 | 98 | } |
54 | | |
55 | 98 | pj_pool_release(pool); |
56 | | |
57 | 98 | return ret; |
58 | 98 | } |
59 | | |
60 | | extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
61 | 123 | { |
62 | 123 | int ret = 0; |
63 | 123 | uint8_t *data; |
64 | 123 | uint8_t *output; |
65 | 123 | size_t output_size; |
66 | 123 | pj_caching_pool caching_pool; |
67 | | |
68 | 123 | if (Size < kMinInputLength || Size > kMaxInputLength) { |
69 | 25 | return 1; |
70 | 25 | } |
71 | | |
72 | | /* Add null termination for the data */ |
73 | 98 | data = (uint8_t *)calloc((Size+1), sizeof(uint8_t)); |
74 | 98 | memcpy((void *)data, (void *)Data, Size); |
75 | 98 | output_size = Size + 32; |
76 | 98 | output = (uint8_t *)calloc(output_size, sizeof(uint8_t)); |
77 | | |
78 | | /* Init */ |
79 | 98 | pj_init(); |
80 | 98 | pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0); |
81 | 98 | pj_log_set_level(0); |
82 | | |
83 | 98 | mem = &caching_pool.factory; |
84 | | |
85 | | /* Fuzz */ |
86 | 98 | ret = h264_unpacketizer(data, Size, output, output_size); |
87 | | |
88 | 98 | free(data); |
89 | 98 | free(output); |
90 | 98 | pj_caching_pool_destroy(&caching_pool); |
91 | | |
92 | 98 | return ret; |
93 | 123 | } |
94 | | #else |
95 | | extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
96 | | { |
97 | | PJ_UNUSED_ARG(Data); |
98 | | PJ_UNUSED_ARG(Size); |
99 | | return 0; |
100 | | } |
101 | | #endif |