/src/libheif/libheif/codecs/jpeg_boxes.cc
Line | Count | Source |
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 | | void Box_jpgC::dump(std::ostream& sstr, Indent& indent, bool full_log) const |
42 | 0 | { |
43 | 0 | Box::dump(sstr, indent, full_log); |
44 | |
|
45 | 0 | sstr << indent << "num bytes: " << m_data.size() << "\n"; |
46 | |
|
47 | 0 | return; |
48 | 0 | } |
49 | | |
50 | | |
51 | | Error Box_jpgC::write(StreamWriter& writer) const |
52 | 0 | { |
53 | 0 | size_t box_start = reserve_box_header_space(writer); |
54 | |
|
55 | 0 | writer.write(m_data); |
56 | |
|
57 | 0 | prepend_header(writer, box_start); |
58 | |
|
59 | 0 | return Error::Ok; |
60 | 0 | } |
61 | | |
62 | | |
63 | | Error Box_jpgC::parse(BitstreamRange& range, const heif_security_limits* limits) |
64 | 418 | { |
65 | 418 | if (!has_fixed_box_size()) { |
66 | 2 | return Error{heif_error_Unsupported_feature, heif_suberror_Unspecified, "jpgC with unspecified size are not supported"}; |
67 | 2 | } |
68 | | |
69 | 416 | size_t nBytes = range.get_remaining_bytes(); |
70 | 416 | if (limits->max_memory_block_size && nBytes > limits->max_memory_block_size) { |
71 | 0 | return Error{heif_error_Invalid_input, heif_suberror_Unspecified, "jpgC block exceeds maximum size"}; |
72 | 0 | } |
73 | | |
74 | 416 | m_data.resize(nBytes); |
75 | 416 | range.read(m_data.data(), nBytes); |
76 | 416 | return range.get_error(); |
77 | 416 | } |