1 /*
2 * Copyright (c) 2023 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 "stream_capture_fuzzer.h"
17 #include "metadata_utils.h"
18 #include "iconsumer_surface.h"
19 using namespace std;
20
21 namespace OHOS {
22 namespace CameraStandard {
23 const std::u16string FORMMGR_INTERFACE_TOKEN = u"IStreamCapture";
24 const int32_t LIMITSIZE = 2;
25 const int32_t PHOTO_WIDTH = 1280;
26 const int32_t PHOTO_HEIGHT = 960;
27 const int32_t PHOTO_FORMAT = 2000;
28
StreamCaptureFuzzTest(uint8_t * rawData,size_t size)29 void StreamCaptureFuzzTest(uint8_t *rawData, size_t size)
30 {
31 if (rawData == nullptr || size < LIMITSIZE) {
32 return;
33 }
34 uint32_t code = 0;
35 int32_t itemCount = 0;
36 int32_t dataSize = 0;
37 uint8_t *streams = rawData;
38 std::shared_ptr<OHOS::Camera::CameraMetadata> ability;
39 ability = std::make_shared<OHOS::Camera::CameraMetadata>(itemCount, dataSize);
40 ability->addEntry(OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS, streams, size);
41 int32_t compensationRange[2] = {rawData[0], rawData[1]};
42 ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_RANGE, compensationRange,
43 sizeof(compensationRange) / sizeof(compensationRange[0]));
44 float focalLength = rawData[0];
45 ability->addEntry(OHOS_ABILITY_FOCAL_LENGTH, &focalLength, sizeof(float));
46
47 int32_t sensorOrientation = rawData[0];
48 ability->addEntry(OHOS_SENSOR_ORIENTATION, &sensorOrientation, sizeof(int32_t));
49
50 int32_t cameraPosition = rawData[0];
51 ability->addEntry(OHOS_ABILITY_CAMERA_POSITION, &cameraPosition, sizeof(int32_t));
52
53 const camera_rational_t aeCompensationStep[] = {{rawData[0], rawData[1]}};
54 ability->addEntry(OHOS_CONTROL_AE_COMPENSATION_STEP, &aeCompensationStep,
55 sizeof(aeCompensationStep) / sizeof(aeCompensationStep[0]));
56
57 MessageParcel data;
58 data.WriteInterfaceToken(FORMMGR_INTERFACE_TOKEN);
59 if (!(OHOS::Camera::MetadataUtils::EncodeCameraMetadata(ability, data))) {
60 return;
61 }
62 data.RewindRead(0);
63 MessageParcel reply;
64 MessageOption option;
65
66 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
67 if (photoSurface == nullptr) {
68 return;
69 }
70 sptr<IBufferProducer> producer = photoSurface->GetProducer();
71 std::shared_ptr<HStreamCapture> streamcapture =
72 std::make_shared<HStreamCapture>(producer, PHOTO_FORMAT, PHOTO_WIDTH, PHOTO_HEIGHT);
73 streamcapture->OnRemoteRequest(code, data, reply, option);
74 }
75 } // namespace CameraStandard
76 } // namespace OHOS
77
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
80 {
81 /* Run your code on data */
82 OHOS::CameraStandard::StreamCaptureFuzzTest(data, size);
83 return 0;
84 }
85
86