Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / graphic / GraphicFormatDetector.hxx
blobd6791e377fc1cd67756468126b51ac41362fa9a1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_VCL_INC_GRAPHICFORMATDETECTOR_HXX
21 #define INCLUDED_VCL_INC_GRAPHICFORMATDETECTOR_HXX
23 #include <tools/stream.hxx>
24 #include <vector>
25 #include <vcl/graphic/GraphicMetadata.hxx>
27 namespace vcl
29 static inline OUString getImportFormatShortName(GraphicFileFormat nFormat)
31 const char* pKeyName = nullptr;
33 switch (nFormat)
35 case GraphicFileFormat::BMP:
36 pKeyName = "BMP";
37 break;
38 case GraphicFileFormat::GIF:
39 pKeyName = "GIF";
40 break;
41 case GraphicFileFormat::JPG:
42 pKeyName = "JPG";
43 break;
44 case GraphicFileFormat::PCD:
45 pKeyName = "PCD";
46 break;
47 case GraphicFileFormat::PCX:
48 pKeyName = "PCX";
49 break;
50 case GraphicFileFormat::PNG:
51 pKeyName = "PNG";
52 break;
53 case GraphicFileFormat::APNG:
54 pKeyName = "APNG";
55 break;
56 case GraphicFileFormat::XBM:
57 pKeyName = "XBM";
58 break;
59 case GraphicFileFormat::XPM:
60 pKeyName = "XPM";
61 break;
62 case GraphicFileFormat::PBM:
63 pKeyName = "PBM";
64 break;
65 case GraphicFileFormat::PGM:
66 pKeyName = "PGM";
67 break;
68 case GraphicFileFormat::PPM:
69 pKeyName = "PPM";
70 break;
71 case GraphicFileFormat::RAS:
72 pKeyName = "RAS";
73 break;
74 case GraphicFileFormat::TGA:
75 pKeyName = "TGA";
76 break;
77 case GraphicFileFormat::PSD:
78 pKeyName = "PSD";
79 break;
80 case GraphicFileFormat::EPS:
81 pKeyName = "EPS";
82 break;
83 case GraphicFileFormat::TIF:
84 pKeyName = "TIF";
85 break;
86 case GraphicFileFormat::DXF:
87 pKeyName = "DXF";
88 break;
89 case GraphicFileFormat::MET:
90 pKeyName = "MET";
91 break;
92 case GraphicFileFormat::PCT:
93 pKeyName = "PCT";
94 break;
95 case GraphicFileFormat::SVM:
96 pKeyName = "SVM";
97 break;
98 case GraphicFileFormat::WMF:
99 pKeyName = "WMF";
100 break;
101 case GraphicFileFormat::EMF:
102 pKeyName = "EMF";
103 break;
104 case GraphicFileFormat::SVG:
105 pKeyName = "SVG";
106 break;
107 case GraphicFileFormat::WMZ:
108 pKeyName = "WMZ";
109 break;
110 case GraphicFileFormat::EMZ:
111 pKeyName = "EMZ";
112 break;
113 case GraphicFileFormat::SVGZ:
114 pKeyName = "SVGZ";
115 break;
116 case GraphicFileFormat::WEBP:
117 pKeyName = "WEBP";
118 break;
119 case GraphicFileFormat::MOV:
120 pKeyName = "MOV";
121 break;
122 case GraphicFileFormat::PDF:
123 pKeyName = "PDF";
124 break;
125 default:
126 assert(false);
129 return OUString::createFromAscii(pKeyName);
131 /***
132 * This function is has two modes:
133 * - determine the file format when bTest = false
134 * returns true, success
135 * out rFormatExtension - on success: file format string
136 * - verify file format when bTest = true
137 * returns false, if file type can't be verified
138 * true, if the format is verified or the format is not known
140 VCL_DLLPUBLIC bool peekGraphicFormat(SvStream& rStream, OUString& rFormatExtension, bool bTest);
142 class VCL_DLLPUBLIC GraphicFormatDetector
144 public:
145 SvStream& mrStream;
146 OUString maExtension;
148 std::vector<sal_uInt8> maFirstBytes;
149 sal_uInt32 mnFirstLong;
150 sal_uInt32 mnSecondLong;
152 sal_uInt64 mnStreamPosition;
153 sal_uInt64 mnStreamLength;
155 GraphicFormatDetector(SvStream& rStream, OUString aFormatExtension, bool bExtendedInfo = false);
157 bool detect();
159 bool checkMET();
160 bool checkBMP();
161 bool checkWMF();
162 bool checkEMF();
163 bool checkPCX();
164 bool checkTIF();
165 bool checkGIF();
166 bool checkPNG();
167 bool checkAPNG();
168 bool checkJPG();
169 bool checkSVM();
170 bool checkPCD();
171 bool checkPSD();
172 bool checkEPS();
173 bool checkDXF();
174 bool checkPCT();
175 bool checkPBM();
176 bool checkPGM();
177 bool checkPPM();
178 bool checkRAS();
179 bool checkXPM();
180 bool checkXBM();
181 bool checkSVG();
182 bool checkTGA();
183 bool checkMOV();
184 bool checkPDF();
185 bool checkWEBP();
186 const GraphicMetadata& getMetadata();
188 private:
190 * @brief Checks whether mrStream needs to be uncompressed and returns a pointer to the
191 * to aUncompressedBuffer or a pointer to maFirstBytes if it doesn't need to be uncompressed
193 * @param aUncompressedBuffer the buffer to hold the uncompressed data
194 * @param nSize the amount of bytes to uncompress
195 * @param nRetSize the amount of bytes actually uncompressed
196 * @return sal_uInt8* a pointer to maFirstBytes or aUncompressed buffer
198 sal_uInt8* checkAndUncompressBuffer(sal_uInt8* aUncompressedBuffer, sal_uInt32 nSize,
199 sal_uInt64& nDecompressedSize);
200 bool mbExtendedInfo;
201 bool mbWasCompressed;
202 GraphicMetadata maMetadata;
206 #endif // INCLUDED_VCL_INC_GRAPHICFORMATDETECTOR_HXX
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */