1 /*
2  *  Copyright (c) 2015 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __KIS_ASL_READER_UTILS_H
20 #define __KIS_ASL_READER_UTILS_H
21 
22 #include "psd_utils.h"
23 
24 #include <stdexcept>
25 #include <string>
26 
27 #include <QtEndian>
28 
29 /**
30  * Default value for variable read from a file
31  */
32 
33 #define GARBAGE_VALUE_MARK 999
34 
35 namespace KisAslReaderUtils {
36 
37 /**
38  * Exception that is emitted when any parse error appear.
39  * Thanks to KisOffsetOnExitVerifier parsing can be continued
40  * most of the time, based on the offset values written in PSD.
41  */
42 
43 struct KRITAPSD_EXPORT ASLParseException : public std::runtime_error
44 {
ASLParseExceptionASLParseException45     ASLParseException(const QString &msg)
46         : std::runtime_error(msg.toLatin1().data())
47     {
48     }
49 };
50 
51 }
52 
53 #define SAFE_READ_EX(device, varname)                                   \
54     if (!psdread(device, &varname)) {                                   \
55         QString msg = QString("Failed to read \'%1\' tag!").arg(#varname); \
56         throw KisAslReaderUtils::ASLParseException(msg);                \
57     }
58 
59 #define SAFE_READ_SIGNATURE_EX(device, varname, expected)               \
60     if (!psdread(device, &varname) || varname != expected) {            \
61         QString msg = QString("Failed to check signature \'%1\' tag!\n" \
62                               "Value: \'%2\' Expected: \'%3\'")         \
63             .arg(#varname).arg(varname).arg(expected);                  \
64         throw KisAslReaderUtils::ASLParseException(msg);                \
65     }
66 
67 #define SAFE_READ_SIGNATURE_2OPS_EX(device, varname, expected1, expected2) \
68     if (!psdread(device, &varname) || (varname != expected1 && varname != expected2)) { \
69         QString msg = QString("Failed to check signature \'%1\' tag!\n" \
70                               "Value: \'%2\' Expected1: \'%3\' Expected2: \'%4\'") \
71             .arg(#varname).arg(varname).arg(expected1).arg(expected2);  \
72         throw KisAslReaderUtils::ASLParseException(msg);                \
73     }
74 
75 template <typename T>
TRY_READ_SIGNATURE_2OPS_EX(QIODevice * device,T expected1,T expected2)76 inline bool TRY_READ_SIGNATURE_2OPS_EX(QIODevice *device, T expected1, T expected2)
77 {
78     T var;
79 
80     qint64 bytesRead = device->peek((char*)&var, sizeof(T));
81     if (bytesRead != sizeof(T)) {
82         return false;
83     }
84 
85     var = qFromBigEndian<T>(var);
86 
87     bool result = var == expected1 || var == expected2;
88 
89     // If read successfully, adjust current position of the io device
90 
91     if (result) {
92         // read, not seek, to support sequential devices
93         bytesRead = device->read((char*)&var, sizeof(T));
94         if (bytesRead != sizeof(T)) {
95             return false;
96         }
97     }
98 
99     return result;
100 }
101 
102 namespace KisAslReaderUtils {
103 
104 /**
105  * String fetch functions
106  *
107  * ASL has 4 types of strings:
108  *
109  * - fixed length (4 bytes)
110  * - variable length (length (4 bytes) + string (var))
111  * - pascal (length (1 byte) + string (var))
112  * - unicode string (length (4 bytes) + null-terminated unicode string (var)
113  */
114 
115 KRITAPSD_EXPORT QString readFixedString(QIODevice *device);
116 KRITAPSD_EXPORT QString readVarString(QIODevice *device);
117 KRITAPSD_EXPORT QString readPascalString(QIODevice *device);
118 KRITAPSD_EXPORT QString readUnicodeString(QIODevice *device);
119 
120 }
121 
122 #endif /* __KIS_ASL_READER_UTILS_H */
123