/src/libheif/libheif/codecs/jpeg_boxes.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HEIF JPEG codec. |
3 | | * Copyright (c) 2023 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "jpeg_boxes.h" |
22 | | #include <string> |
23 | | #include "security_limits.h" |
24 | | #include <cstring> |
25 | | #include "libheif/heif_experimental.h" |
26 | | |
27 | | |
28 | | // returns 0 if the marker_type was not found |
29 | | size_t find_jpeg_marker_start(const std::vector<uint8_t>& data, uint8_t marker_type) |
30 | 0 | { |
31 | 0 | for (size_t i = 0; i < data.size() - 1; i++) { |
32 | 0 | if (data[i] == 0xFF && data[i + 1] == marker_type) { |
33 | 0 | return i; |
34 | 0 | } |
35 | 0 | } |
36 | | |
37 | 0 | return 0; |
38 | 0 | } |
39 | | |
40 | | |
41 | | std::string Box_jpgC::dump(Indent& indent) const |
42 | 0 | { |
43 | 0 | std::ostringstream sstr; |
44 | 0 | sstr << Box::dump(indent); |
45 | |
|
46 | 0 | sstr << indent << "num bytes: " << m_data.size() << "\n"; |
47 | |
|
48 | 0 | return sstr.str(); |
49 | 0 | } |
50 | | |
51 | | |
52 | | Error Box_jpgC::write(StreamWriter& writer) const |
53 | 0 | { |
54 | 0 | size_t box_start = reserve_box_header_space(writer); |
55 | |
|
56 | 0 | writer.write(m_data); |
57 | |
|
58 | 0 | prepend_header(writer, box_start); |
59 | |
|
60 | 0 | return Error::Ok; |
61 | 0 | } |
62 | | |
63 | | |
64 | | Error Box_jpgC::parse(BitstreamRange& range, const heif_security_limits* limits) |
65 | 38 | { |
66 | 38 | if (!has_fixed_box_size()) { |
67 | 1 | return Error{heif_error_Unsupported_feature, heif_suberror_Unspecified, "jpgC with unspecified size are not supported"}; |
68 | 1 | } |
69 | | |
70 | 37 | size_t nBytes = range.get_remaining_bytes(); |
71 | 37 | if (limits->max_memory_block_size && nBytes > limits->max_memory_block_size) { |
72 | 0 | return Error{heif_error_Invalid_input, heif_suberror_Unspecified, "jpgC block exceeds maximum size"}; |
73 | 0 | } |
74 | | |
75 | 37 | m_data.resize(nBytes); |
76 | 37 | range.read(m_data.data(), nBytes); |
77 | 37 | return range.get_error(); |
78 | 37 | } |