File: main.cpp

package info (click to toggle)
charls 2.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,636 kB
  • sloc: cpp: 13,392; ansic: 986; makefile: 22
file content (124 lines) | stat: -rw-r--r-- 3,030 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Team CharLS.
// SPDX-License-Identifier: BSD-3-Clause

#include <charls/charls.h>

#include "../src/conditional_static_cast.h"

#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>

#define _read read
#define _open open

#endif

#include <fcntl.h>

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

using namespace charls;
using std::vector;

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

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

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

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


namespace {

vector<uint8_t> generate_once()
{
    const vector<uint8_t> source(3);

    jpegls_encoder encoder;
    encoder.frame_info({1, 1, 8, 3});

    vector<uint8_t> destination(encoder.estimated_destination_size());
    encoder.destination(destination);

    const size_t bytes_written{encoder.encode(source)};
    destination.resize(bytes_written);

    return destination;
}

} // 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
                const auto encoded_data{generate_once()};
#ifdef _MSC_VER
                const int result{_write(1, encoded_data.data(), conditional_static_cast<unsigned int>(encoded_data.size()))};
#else
                const ssize_t result{
                    write(1, encoded_data.data(), conditional_static_cast<unsigned int>(encoded_data.size()))};
#endif
                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';  // NOLINT(concurrency-mt-unsafe)
            return EXIT_FAILURE;
        }
    }

    __AFL_INIT();

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

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

#ifndef AFL_LOOP_FOREVER
    return EXIT_SUCCESS;
#endif
}