EXIF library (libexif) Internals 0.6.26
exif-utils.c
Go to the documentation of this file.
1/* exif-utils.c
2 *
3 * Copyright (c) 2001 Lutz Mueller <lutz@users.sourceforge.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
19 *
20 * SPDX-License-Identifier: LGPL-2.0-or-later
21 */
22
23#include <config.h>
24
25#include <libexif/exif-utils.h>
26
27void
28exif_array_set_byte_order (ExifFormat f, unsigned char *b, unsigned int n,
29 ExifByteOrder o_orig, ExifByteOrder o_new)
30{
31 unsigned int j;
32 unsigned int fs = exif_format_get_size (f);
33 ExifShort s;
34 ExifSShort ss;
35 ExifLong l;
36 ExifSLong sl;
39
40 if (!b || !n || !fs) return;
41
42 switch (f) {
44 for (j = 0; j < n; j++) {
45 s = exif_get_short (b + j * fs, o_orig);
46 exif_set_short (b + j * fs, o_new, s);
47 }
48 break;
50 for (j = 0; j < n; j++) {
51 ss = exif_get_sshort (b + j * fs, o_orig);
52 exif_set_sshort (b + j * fs, o_new, ss);
53 }
54 break;
56 for (j = 0; j < n; j++) {
57 l = exif_get_long (b + j * fs, o_orig);
58 exif_set_long (b + j * fs, o_new, l);
59 }
60 break;
62 for (j = 0; j < n; j++) {
63 r = exif_get_rational (b + j * fs, o_orig);
64 exif_set_rational (b + j * fs, o_new, r);
65 }
66 break;
68 for (j = 0; j < n; j++) {
69 sl = exif_get_slong (b + j * fs, o_orig);
70 exif_set_slong (b + j * fs, o_new, sl);
71 }
72 break;
74 for (j = 0; j < n; j++) {
75 sr = exif_get_srational (b + j * fs, o_orig);
76 exif_set_srational (b + j * fs, o_new, sr);
77 }
78 break;
82 default:
83 /* Nothing here. */
84 break;
85 }
86}
87
89exif_get_sshort (const unsigned char *buf, ExifByteOrder order)
90{
91 if (!buf) return 0;
92 switch (order) {
94 return (((unsigned int)buf[0] << 8) | buf[1]);
96 return (((unsigned int)buf[1] << 8) | buf[0]);
97 }
98
99 /* Won't be reached */
100 return (0);
101}
102
104exif_get_short (const unsigned char *buf, ExifByteOrder order)
105{
106 return (exif_get_sshort (buf, order) & 0xffff);
107}
108
109void
110exif_set_sshort (unsigned char *b, ExifByteOrder order, ExifSShort value)
111{
112 if (!b) return;
113 switch (order) {
115 b[0] = (unsigned char) (value >> 8);
116 b[1] = (unsigned char) value;
117 break;
119 b[0] = (unsigned char) value;
120 b[1] = (unsigned char) (value >> 8);
121 break;
122 }
123}
124
125void
126exif_set_short (unsigned char *b, ExifByteOrder order, ExifShort value)
127{
128 exif_set_sshort (b, order, value);
129}
130
132exif_get_slong (const unsigned char *b, ExifByteOrder order)
133{
134 if (!b) return 0;
135 switch (order) {
137 return (((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | (uint32_t)b[3]);
139 return (((uint32_t)b[3] << 24) | ((uint32_t)b[2] << 16) | ((uint32_t)b[1] << 8) | (uint32_t)b[0]);
140 }
141
142 /* Won't be reached */
143 return (0);
144}
145
146void
147exif_set_slong (unsigned char *b, ExifByteOrder order, ExifSLong value)
148{
149 if (!b) return;
150 switch (order) {
152 b[0] = (unsigned char) (value >> 24);
153 b[1] = (unsigned char) (value >> 16);
154 b[2] = (unsigned char) (value >> 8);
155 b[3] = (unsigned char) value;
156 break;
158 b[3] = (unsigned char) (value >> 24);
159 b[2] = (unsigned char) (value >> 16);
160 b[1] = (unsigned char) (value >> 8);
161 b[0] = (unsigned char) value;
162 break;
163 }
164}
165
167exif_get_long (const unsigned char *buf, ExifByteOrder order)
168{
169 return (exif_get_slong (buf, order) & 0xffffffff);
170}
171
172void
173exif_set_long (unsigned char *b, ExifByteOrder order, ExifLong value)
174{
175 exif_set_slong (b, order, value);
176}
177
179exif_get_srational (const unsigned char *buf, ExifByteOrder order)
180{
182
183 r.numerator = buf ? exif_get_slong (buf, order) : 0;
184 r.denominator = buf ? exif_get_slong (buf + 4, order) : 0;
185
186 return (r);
187}
188
190exif_get_rational (const unsigned char *buf, ExifByteOrder order)
191{
192 ExifRational r;
193
194 r.numerator = buf ? exif_get_long (buf, order) : 0;
195 r.denominator = buf ? exif_get_long (buf + 4, order) : 0;
196
197 return (r);
198}
199
200void
201exif_set_rational (unsigned char *buf, ExifByteOrder order,
202 ExifRational value)
203{
204 if (!buf) return;
205 exif_set_long (buf, order, value.numerator);
206 exif_set_long (buf + 4, order, value.denominator);
207}
208
209void
210exif_set_srational (unsigned char *buf, ExifByteOrder order,
211 ExifSRational value)
212{
213 if (!buf) return;
214 exif_set_slong (buf, order, value.numerator);
215 exif_set_slong (buf + 4, order, value.denominator);
216}
217
221void
222exif_convert_utf16_to_utf8 (char *out, unsigned int outlen, const unsigned char *in, unsigned int inlen)
223{
224 if (!outlen) {
225 return;
226 }
227 for (;;) {
228 ExifShort v;
229 if (inlen < 2)
230 break;
232 if (!v)
233 break;
234 if (v < 0x80) {
235 if (outlen > 1) {
236 *out++ = (char)v;
237 outlen--;
238 } else {
239 break;
240 }
241 } else if (v < 0x800) {
242 if (outlen > 2) {
243 *out++ = ((v >> 6) & 0x1F) | 0xC0;
244 *out++ = (v & 0x3F) | 0x80;
245 outlen -= 2;
246 } else {
247 break;
248 }
249 } else {
250 if (outlen > 3) {
251 *out++ = ((v >> 12) & 0x0F) | 0xE0;
252 *out++ = ((v >> 6) & 0x3F) | 0x80;
253 *out++ = (v & 0x3F) | 0x80;
254 outlen -= 3;
255 } else {
256 break;
257 }
258 }
259 in += 2;
260 inlen -= 2;
261 }
262 *out = 0;
263}
ExifByteOrder
Which byte order to use.
@ EXIF_BYTE_ORDER_INTEL
Little-endian byte order.
@ EXIF_BYTE_ORDER_MOTOROLA
Big-endian byte order.
unsigned char exif_format_get_size(ExifFormat format)
Return the raw size of the given EXIF data type.
Definition exif-format.c:68
ExifFormat
EXIF tag data formats.
Definition exif-format.h:34
@ EXIF_FORMAT_SLONG
Definition exif-format.h:43
@ EXIF_FORMAT_RATIONAL
Definition exif-format.h:39
@ EXIF_FORMAT_SRATIONAL
Definition exif-format.h:44
@ EXIF_FORMAT_LONG
Definition exif-format.h:38
@ EXIF_FORMAT_UNDEFINED
Definition exif-format.h:41
@ EXIF_FORMAT_SHORT
Definition exif-format.h:37
@ EXIF_FORMAT_SSHORT
Definition exif-format.h:42
@ EXIF_FORMAT_ASCII
Definition exif-format.h:36
@ EXIF_FORMAT_BYTE
Definition exif-format.h:35
void exif_convert_utf16_to_utf8(char *out, unsigned int outlen, const unsigned char *in, unsigned int inlen)
This function converts rather UCS-2LE than UTF-16 to UTF-8.
Definition exif-utils.c:222
ExifRational exif_get_rational(const unsigned char *buf, ExifByteOrder order)
Retrieve an ExifRational value from memory.
Definition exif-utils.c:190
void exif_set_rational(unsigned char *buf, ExifByteOrder order, ExifRational value)
Store an ExifRational value into memory in EXIF format.
Definition exif-utils.c:201
ExifLong exif_get_long(const unsigned char *buf, ExifByteOrder order)
Retrieve an ExifLong value from memory.
Definition exif-utils.c:167
ExifShort exif_get_short(const unsigned char *buf, ExifByteOrder order)
Retrieve an ExifShort value from memory.
Definition exif-utils.c:104
void exif_array_set_byte_order(ExifFormat f, unsigned char *b, unsigned int n, ExifByteOrder o_orig, ExifByteOrder o_new)
Definition exif-utils.c:28
ExifSLong exif_get_slong(const unsigned char *b, ExifByteOrder order)
Retrieve an ExifSLong value from memory.
Definition exif-utils.c:132
void exif_set_sshort(unsigned char *b, ExifByteOrder order, ExifSShort value)
Store an ExifSShort value into memory in EXIF format.
Definition exif-utils.c:110
ExifSShort exif_get_sshort(const unsigned char *buf, ExifByteOrder order)
Retrieve an ExifSShort value from memory.
Definition exif-utils.c:89
void exif_set_long(unsigned char *b, ExifByteOrder order, ExifLong value)
Store an ExifLong value into memory in EXIF format.
Definition exif-utils.c:173
void exif_set_srational(unsigned char *buf, ExifByteOrder order, ExifSRational value)
Store an ExifSRational value into memory in EXIF format.
Definition exif-utils.c:210
void exif_set_short(unsigned char *b, ExifByteOrder order, ExifShort value)
Store an ExifShort value into memory in EXIF format.
Definition exif-utils.c:126
ExifSRational exif_get_srational(const unsigned char *buf, ExifByteOrder order)
Retrieve an ExifSRational value from memory.
Definition exif-utils.c:179
void exif_set_slong(unsigned char *b, ExifByteOrder order, ExifSLong value)
Store an ExifSLong value into memory in EXIF format.
Definition exif-utils.c:147
EXIF data manipulation functions and types.
uint32_t ExifLong
EXIF Unsigned Long data type.
Definition exif-utils.h:56
int32_t ExifSLong
EXIF Signed Long data type.
Definition exif-utils.h:59
uint16_t ExifShort
EXIF Unsigned Short data type.
Definition exif-utils.h:50
int16_t ExifSShort
EXIF Signed Short data type.
Definition exif-utils.h:53
EXIF Unsigned Rational data type.
Definition exif-utils.h:62
ExifLong denominator
Definition exif-utils.h:62
ExifLong numerator
Definition exif-utils.h:62
EXIF Signed Rational data type.
Definition exif-utils.h:67
ExifSLong numerator
Definition exif-utils.h:67
ExifSLong denominator
Definition exif-utils.h:67

libexif Generated by doxygen