1 /*
2 * Copyright Samsung Electronics Co.,LTD.
3 * Copyright (C) 2015 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 #ifndef __HARDWARE_SAMSUNG_SLSI_EXYNOS_IFDWRITER_H__
18 #define __HARDWARE_SAMSUNG_SLSI_EXYNOS_IFDWRITER_H__
19
20 #include "hwjpeg-internal.h"
21
22 class CEndianessChecker {
23 bool __little;
24
25 public:
26 CEndianessChecker();
27 operator bool() { return __little; }
28 };
29 #ifndef __LITTLE_ENDIAN__
30 extern CEndianessChecker __LITTLE_ENDIAN__;
31 #endif
32
33 template <typename T>
WriteDataInBig(char * p,T val)34 char *WriteDataInBig(char *p, T val) {
35 if (sizeof(val) == 1) {
36 *p++ = val;
37 } else if (__LITTLE_ENDIAN__) {
38 switch (sizeof(val)) {
39 case 2:
40 *p++ = static_cast<char>((val >> 8) & 0xFF);
41 *p++ = static_cast<char>(val & 0xFF);
42 break;
43 case 4:
44 *p++ = static_cast<char>((val >> 24) & 0xFF);
45 *p++ = static_cast<char>((val >> 16) & 0xFF);
46 *p++ = static_cast<char>((val >> 8) & 0xFF);
47 *p++ = static_cast<char>(val & 0xFF);
48 break;
49 }
50 } else {
51 switch (sizeof(val)) {
52 case 2:
53 *p++ = static_cast<char>(val & 0xFF);
54 *p++ = static_cast<char>((val >> 8) & 0xFF);
55 break;
56 case 4:
57 *p++ = static_cast<char>(val & 0xFF);
58 *p++ = static_cast<char>((val >> 8) & 0xFF);
59 *p++ = static_cast<char>((val >> 16) & 0xFF);
60 *p++ = static_cast<char>((val >> 24) & 0xFF);
61 break;
62 }
63 }
64
65 return p;
66 }
67
68 template <typename T>
WriteData(char * p,T val)69 char *WriteData(char *p, T val) {
70 const char *pt = reinterpret_cast<char *>(&val);
71 for (size_t i = 0; i < sizeof(val); i++) *p++ = *pt++;
72 return p;
73 }
74
75 class CIFDWriter {
76 char *m_pBase;
77 char *m_pIFDBase;
78 char *m_pValue;
79 unsigned int m_nTags;
80
WriteOffset(char * target,char * addr)81 char *WriteOffset(char *target, char *addr) {
82 uint32_t val = Offset(addr);
83 const char *p = reinterpret_cast<char *>(&val);
84 *target++ = *p++;
85 *target++ = *p++;
86 *target++ = *p++;
87 *target++ = *p++;
88 return target;
89 }
90
WriteTagTypeCount(uint16_t tag,uint16_t type,uint32_t count)91 void WriteTagTypeCount(uint16_t tag, uint16_t type, uint32_t count) {
92 const char *p = reinterpret_cast<char *>(&tag);
93 *m_pIFDBase++ = *p++;
94 *m_pIFDBase++ = *p++;
95
96 p = reinterpret_cast<char *>(&type);
97 *m_pIFDBase++ = *p++;
98 *m_pIFDBase++ = *p++;
99
100 p = reinterpret_cast<char *>(&count);
101 *m_pIFDBase++ = *p++;
102 *m_pIFDBase++ = *p++;
103 *m_pIFDBase++ = *p++;
104 *m_pIFDBase++ = *p++;
105
106 m_nTags--;
107 }
108
109 public:
CIFDWriter(char * offset_base,char * ifdbase,uint16_t tagcount)110 CIFDWriter(char *offset_base, char *ifdbase, uint16_t tagcount) {
111 m_nTags = tagcount;
112 m_pBase = offset_base;
113 m_pIFDBase = ifdbase;
114 m_pValue = m_pIFDBase + IFD_FIELDCOUNT_SIZE + IFD_FIELD_SIZE * tagcount +
115 IFD_NEXTIFDOFFSET_SIZE;
116
117 // COUNT field of IFD
118 const char *pval = reinterpret_cast<char *>(&m_nTags);
119 *m_pIFDBase++ = *pval++;
120 *m_pIFDBase++ = *pval++;
121 }
122
Offset(char * p)123 uint32_t Offset(char *p) {
124 return static_cast<uint32_t>(PTR_TO_ULONG(p) - PTR_TO_ULONG(m_pBase));
125 }
126
WriteByte(uint16_t tag,uint32_t count,const uint8_t value[])127 void WriteByte(uint16_t tag, uint32_t count, const uint8_t value[]) {
128 ALOG_ASSERT(m_nTags == 0);
129
130 WriteTagTypeCount(tag, EXIF_TYPE_BYTE, count);
131
132 if (count > IFD_VALOFF_SIZE) {
133 m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue);
134 for (uint32_t i = 0; i < count; i++) {
135 *m_pValue++ = static_cast<char>(value[i]);
136 }
137 } else {
138 for (uint32_t i = 0; i < count; i++) *m_pIFDBase++ = static_cast<char>(value[i]);
139 m_pIFDBase += IFD_VALOFF_SIZE - count;
140 }
141 }
142
WriteShort(uint16_t tag,uint32_t count,const uint16_t value[])143 void WriteShort(uint16_t tag, uint32_t count, const uint16_t value[]) {
144 ALOG_ASSERT(m_nTags == 0);
145
146 WriteTagTypeCount(tag, EXIF_TYPE_SHORT, count);
147
148 const char *p = reinterpret_cast<const char *>(&value[0]);
149
150 if (count > (IFD_VALOFF_SIZE / sizeof(value[0]))) {
151 m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue);
152 for (uint32_t i = 0; i < count; i++) {
153 *m_pValue++ = *p++;
154 *m_pValue++ = *p++;
155 }
156 } else {
157 for (uint32_t i = 0; i < count; i++) {
158 *m_pIFDBase++ = *p++;
159 *m_pIFDBase++ = *p++;
160 }
161 m_pIFDBase += IFD_VALOFF_SIZE - count * sizeof(value[0]);
162 }
163 }
164
WriteLong(uint16_t tag,uint32_t count,const uint32_t value[])165 void WriteLong(uint16_t tag, uint32_t count, const uint32_t value[]) {
166 ALOG_ASSERT(m_nTags == 0);
167
168 WriteTagTypeCount(tag, EXIF_TYPE_LONG, count);
169
170 const char *p = reinterpret_cast<const char *>(&value[0]);
171 if (count > (IFD_VALOFF_SIZE / sizeof(value[0]))) {
172 m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue);
173 *m_pValue++ = *p++;
174 } else {
175 *m_pIFDBase++ = *p++;
176 *m_pIFDBase++ = *p++;
177 *m_pIFDBase++ = *p++;
178 *m_pIFDBase++ = *p++;
179 }
180 }
181
WriteASCII(uint16_t tag,uint32_t count,const char * value)182 void WriteASCII(uint16_t tag, uint32_t count, const char *value) {
183 ALOG_ASSERT(m_nTags == 0);
184
185 WriteTagTypeCount(tag, EXIF_TYPE_ASCII, count);
186
187 if (count > IFD_VALOFF_SIZE) {
188 m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue);
189 memcpy(m_pValue, value, count);
190 m_pValue[count - 1] = '\0';
191 m_pValue += count;
192 } else {
193 for (uint32_t i = 0; i < count; i++) *m_pIFDBase++ = value[i];
194 *(m_pIFDBase - 1) = '\0';
195 m_pIFDBase += IFD_VALOFF_SIZE - count;
196 }
197 }
198
WriteCString(uint16_t tag,uint32_t count,const char * string)199 void WriteCString(uint16_t tag, uint32_t count, const char *string) {
200 ALOG_ASSERT(m_nTags == 0);
201
202 WriteTagTypeCount(tag, EXIF_TYPE_ASCII, count);
203
204 if (count > IFD_VALOFF_SIZE) {
205 m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue);
206 strncpy(m_pValue, string, count);
207 m_pValue[count - 1] = '\0';
208 m_pValue += count;
209 } else {
210 uint32_t i;
211
212 for (i = 0; (i < (count - 1)) && (string[i] != '\0'); i++) *m_pIFDBase++ = string[i];
213
214 while (i++ < count) *m_pIFDBase++ = '\0';
215
216 m_pIFDBase += IFD_VALOFF_SIZE - count;
217 }
218 }
219
WriteRational(uint16_t tag,uint32_t count,const rational_t value[])220 void WriteRational(uint16_t tag, uint32_t count, const rational_t value[]) {
221 ALOG_ASSERT(m_nTags == 0);
222
223 WriteTagTypeCount(tag, EXIF_TYPE_RATIONAL, count);
224 m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue);
225
226 for (uint32_t i = 0; i < count; i++) {
227 const char *pt;
228 pt = reinterpret_cast<const char *>(&value[i].num);
229 *m_pValue++ = *pt++;
230 *m_pValue++ = *pt++;
231 *m_pValue++ = *pt++;
232 *m_pValue++ = *pt++;
233 pt = reinterpret_cast<const char *>(&value[i].den);
234 *m_pValue++ = *pt++;
235 *m_pValue++ = *pt++;
236 *m_pValue++ = *pt++;
237 *m_pValue++ = *pt++;
238 }
239 }
240
WriteSRational(uint16_t tag,uint32_t count,const srational_t value[])241 void WriteSRational(uint16_t tag, uint32_t count, const srational_t value[]) {
242 ALOG_ASSERT(m_nTags == 0);
243
244 WriteTagTypeCount(tag, EXIF_TYPE_SRATIONAL, count);
245 m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue);
246
247 const char *pt = reinterpret_cast<const char *>(value);
248 for (uint32_t i = 0; i < sizeof(srational_t) * count; i++) *m_pValue++ = *pt++;
249 }
250
WriteUndef(uint16_t tag,uint32_t count,const unsigned char * value)251 void WriteUndef(uint16_t tag, uint32_t count, const unsigned char *value) {
252 ALOG_ASSERT(m_nTags == 0);
253
254 WriteTagTypeCount(tag, EXIF_TYPE_UNDEFINED, count);
255 if (count > IFD_VALOFF_SIZE) {
256 m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue);
257 memcpy(m_pValue, value, count);
258 m_pValue += count;
259 } else {
260 for (uint32_t i = 0; i < count; i++) *m_pIFDBase++ = static_cast<char>(value[i]);
261 m_pIFDBase += IFD_VALOFF_SIZE - count;
262 }
263 }
264
BeginSubIFD(uint16_t tag)265 char *BeginSubIFD(uint16_t tag) {
266 ALOG_ASSERT(m_nTags == 0);
267
268 WriteTagTypeCount(tag, EXIF_TYPE_LONG, 1);
269
270 uint32_t offset = Offset(m_pValue);
271 const char *poff = reinterpret_cast<char *>(&offset);
272 *m_pIFDBase++ = *poff++;
273 *m_pIFDBase++ = *poff++;
274 *m_pIFDBase++ = *poff++;
275 *m_pIFDBase++ = *poff++;
276
277 return m_pValue;
278 }
279
EndSubIFD(char * end_of_subIFD)280 void EndSubIFD(char *end_of_subIFD) { m_pValue = end_of_subIFD; }
CancelSubIFD()281 void CancelSubIFD() { m_pIFDBase -= IFD_FIELD_SIZE; }
282
Finish(bool last)283 void Finish(bool last) {
284 ALOG_ASSERT(m_nTags > 0);
285
286 uint32_t offset = last ? 0 : Offset(m_pValue);
287 const char *pv = reinterpret_cast<char *>(&offset);
288 *m_pIFDBase++ = *pv++;
289 *m_pIFDBase++ = *pv++;
290 *m_pIFDBase++ = *pv++;
291 *m_pIFDBase++ = *pv++;
292 }
293
GetNextIFDBase()294 char *GetNextIFDBase() { return m_pValue; }
GetNextTagAddress()295 char *GetNextTagAddress() { return m_pIFDBase; }
296 };
297
298 #endif //__HARDWARE_SAMSUNG_SLSI_EXYNOS_IFDWRITER_H__
299