• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 
22 #include <iostream>
23 #include "parser_sample.h"
24 
25 
26 #define FUZZ_PROJECT_NAME "demuxer_fuzzer"
27 using namespace std;
28 using namespace OHOS::Media;
29 using namespace OHOS::MediaAVCodec;
30 namespace OHOS {
31 const char *MP4_PATH = "/data/test/fuzz_create.mp4";
32 
DoReferenceParserWithDemuxerAPI(const uint8_t * data,size_t size)33 bool DoReferenceParserWithDemuxerAPI(const uint8_t *data, size_t size)
34 {
35     if (size < sizeof(int64_t)) {
36         return false;
37     }
38     int32_t fd = open(MP4_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
39     if (fd < 0) {
40         return false;
41     }
42     int len = write(fd, data, size - 7);
43     if (len <= 0) {
44         close(fd);
45         return false;
46     }
47     close(fd);
48     int64_t pts = data[size - 5];
49     int64_t ptsForPtsIndex = data[size - 6];
50     int64_t frameIndex = data[size - 7];
51     uint8_t *dataConver = const_cast<uint8_t *>(data);
52     uint32_t *createSize = reinterpret_cast<uint32_t *>(dataConver + size - 4);
53     shared_ptr<ParserSample> parserSample = make_shared<ParserSample>();
54     parserSample->filePath = MP4_PATH;
55     parserSample->RunReferenceParser(pts, ptsForPtsIndex, frameIndex, *createSize);
56     int ret = remove(MP4_PATH);
57     if (ret != 0) {
58         return false;
59     }
60     return true;
61 }
62 } // namespace OHOS
63 
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
66 {
67     /* Run your code on data */
68     OHOS::DoReferenceParserWithDemuxerAPI(data, size);
69     return 0;
70 }
71