File: main.cpp

package info (click to toggle)
charls 2.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,740 kB
  • sloc: cpp: 10,109; ansic: 878; makefile: 23
file content (103 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Team CharLS.
// SPDX-License-Identifier: BSD-3-Clause

#include <charls/charls.h>

#ifdef _MSC_VER

#include <io.h>

#else
#include <unistd.h>

#define _write write
#define _read read
#define _open open

#endif

#include <fcntl.h>

#include <cstring>
#include <iostream>
#include <vector>

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#endif

#ifndef __AFL_INIT
#define __AFL_INIT() // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
#endif

#ifndef __AFL_LOOP
#define __AFL_LOOP(a) true // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
#endif

#if defined(__clang__)
#pragma clang diagnostic pop
#endif


namespace {

auto generate_once()
{
    const std::vector<uint8_t> source(3);
    return charls::jpegls_encoder::encode(source, {1, 1, 8, 3});
}

} // namespace


int main(const int argc, const char* const argv[]) // NOLINT(bugprone-exception-escape)
{
    int fd{};
    if (argc == 2)
    {
        if (argv[1][0] == '\0')
        {
            try
            {
                // Write some small-ish JPEG-LS file to stdout
                auto encoded_data{generate_once()};
                const int result{
                    static_cast<int>(_write(1, encoded_data.data(), static_cast<unsigned int>(encoded_data.size())))};
                return result != -1 && result == static_cast<int>(encoded_data.size()) ? EXIT_SUCCESS : EXIT_FAILURE;
            }
            catch (const std::exception& error)
            {
                std::cerr << "Failed to create the once: " << error.what() << '\n';
            }
            return EXIT_FAILURE;
        }

        fd = _open(argv[1], O_RDONLY);
        if (fd < 0)
        {
            std::cerr << "Failed to open: " << argv[1] << strerror(errno) << '\n';
            return EXIT_FAILURE;
        }
    }

    __AFL_INIT();

    while (__AFL_LOOP(100))
    {
        std::vector<uint8_t> source(1024 * 1024);
        const size_t input_length = _read(fd, source.data(), static_cast<unsigned int>(source.size()));
        source.resize(input_length);

        try
        {
            std::vector<uint8_t> destination;
            charls::jpegls_decoder::decode(source, destination);
        }
        catch (const charls::jpegls_error&)
        {
        }
    }

    return EXIT_SUCCESS;
}