File size: 8,438 Bytes
fc87544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* exif-mnote-data-apple.c
 *
 * Copyright (c) 2018 zhanwang-sky <[email protected]>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA.
 */

#include <config.h>
#include "exif-mnote-data-apple.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libexif/exif-data.h>
#include <libexif/exif-utils.h>

static void
exif_mnote_data_apple_free(ExifMnoteData *md) {
    ExifMnoteDataApple *d = (ExifMnoteDataApple *) md;
    unsigned int i;

    /*printf("%s\n", __FUNCTION__);*/

    if (!d) {
        return;
    }

    if (d->entries) {
        for (i = 0; i < d->count; i++) {
            if (d->entries[i].data) {
                exif_mem_free(md->mem, d->entries[i].data);
            }
        }
        exif_mem_free(md->mem, d->entries);
        d->entries = NULL;
        d->count = 0;
    }

    return;
}

static void
exif_mnote_data_apple_load(ExifMnoteData *md, const unsigned char *buf, unsigned int buf_size) {
    ExifMnoteDataApple *d = (ExifMnoteDataApple *) md;
    unsigned int tcount, i;
    unsigned int dsize;
    unsigned int ofs, dofs;

    /*printf("%s\n", __FUNCTION__);*/

    if (!d || !buf || (buf_size < 6 + 16)) {
        exif_log(md->log, EXIF_LOG_CODE_CORRUPT_DATA,
                 "ExifMnoteDataApple", "Short MakerNote");
        return;
    }

    /* Start of interesting data */
    ofs = d->offset + 6;

    if ((buf[ofs + 12] == 'M') && (buf[ofs + 13] == 'M')) {
        d->order = EXIF_BYTE_ORDER_MOTOROLA;
    } else if ((buf[ofs + 12] == 'I') && (buf[ofs + 13] == 'I')) {
        d->order = EXIF_BYTE_ORDER_INTEL;
    } else {
        exif_log(md->log, EXIF_LOG_CODE_CORRUPT_DATA,
                "ExifMnoteDataApple", "Unrecognized byte order");
        /*printf("%s(%d)\n", __FUNCTION__, __LINE__);*/
        return;
    }

    tcount = (unsigned int) exif_get_short(buf + ofs + 14, d->order);

    /* Sanity check the offset */
    if (buf_size < d->offset + 6 + 16 + tcount * 12 + 4) {
        exif_log(md->log, EXIF_LOG_CODE_CORRUPT_DATA,
                 "ExifMnoteDataApple", "Short MakerNote");
        /*printf("%s(%d)\n", __FUNCTION__, __LINE__);*/
        return;
    }
    memset(d->entries, 0, sizeof(MnoteAppleEntry) * tcount);

    /* printf("%s(%d): total %d tags\n", __FUNCTION__, __LINE__, tcount); */

    ofs += 16;

    exif_mnote_data_apple_free(md);

    /* Reserve enough space for all the possible MakerNote tags */
    d->entries = exif_mem_alloc(md->mem, sizeof(MnoteAppleEntry) * tcount);
    if (!d->entries) {
        EXIF_LOG_NO_MEMORY(md->log, "ExifMnoteApple", sizeof(MnoteAppleEntry) * tcount);
        /*printf("%s(%d)\n", __FUNCTION__, __LINE__);*/
        return;
    }

    for (i = 0; i < tcount; i++) {
	if (ofs + 12 > buf_size) {
		exif_log (md->log, EXIF_LOG_CODE_CORRUPT_DATA,
                                  "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", ofs + 12, buf_size);
		break;
	}
        d->entries[i].tag = exif_get_short(buf + ofs, d->order);
        d->entries[i].format = exif_get_short(buf + ofs + 2, d->order);
        d->entries[i].components = exif_get_long(buf + ofs + 4, d->order);
        d->entries[i].order = d->order;
        dsize = exif_format_get_size(d->entries[i].format) * d->entries[i].components;
        if (dsize > 4) {
            dofs = d->offset + exif_get_long(buf + ofs + 8, d->order);
        } else {
            dofs = ofs + 8;
        }
	if (dofs > buf_size) {
		exif_log (md->log, EXIF_LOG_CODE_CORRUPT_DATA,
                                  "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", dofs, buf_size);
		continue;
	}
        ofs += 12;
        d->entries[i].data = exif_mem_alloc(md->mem, dsize);
        if (!d->entries[i].data) {
            EXIF_LOG_NO_MEMORY(md->log, "ExifMnoteApple", dsize);
            continue;
        }
        if (dofs + dsize > buf_size) {
            exif_log(md->log, EXIF_LOG_CODE_CORRUPT_DATA,
                "ExifMnoteApplet", "Tag size overflow detected (%u vs size %u)", dofs + dsize, buf_size);
            continue;
        }
        memcpy(d->entries[i].data, buf + dofs, dsize);
        d->entries[i].size = dsize;
    }
    d->count = tcount;

    return;
}

static void
exif_mnote_data_apple_set_offset(ExifMnoteData *md, unsigned int o) {
    /*printf("%s\n", __FUNCTION__);*/

    if (md) {
        ((ExifMnoteDataApple *) md)->offset = o;
    }

    return;
}

static void
exif_mnote_data_apple_set_byte_order(ExifMnoteData *md , ExifByteOrder o) {
    ExifMnoteDataApple *d = (ExifMnoteDataApple *) md;
    unsigned int i;

    /*printf("%s\n", __FUNCTION__);*/

    if (!d || d->order == o) {
        return;
    }

    for (i = 0; i < d->count; i++) {
	if (d->entries[i].components && (d->entries[i].size/d->entries[i].components < exif_format_get_size (d->entries[i].format)))
		continue;
        exif_array_set_byte_order(d->entries[i].format, d->entries[i].data,
                                  d->entries[i].components, d->entries[i].order, o);
        d->entries[i].order = o;
    }
    d->order = o;

    return;
}

static unsigned int
exif_mnote_data_apple_count(ExifMnoteData *md){
    /*printf("%s\n", __FUNCTION__);*/

    return md ? ((ExifMnoteDataApple *) md)->count : 0;
}

static unsigned int
exif_mnote_data_apple_get_id(ExifMnoteData *md, unsigned int i) {
    ExifMnoteDataApple *d = (ExifMnoteDataApple *) md;

    if (!d || (d->count <= i)) {
        return 0;
    }

    return d->entries[i].tag;
}

static const char *
exif_mnote_data_apple_get_name(ExifMnoteData *md, unsigned int i) {
    ExifMnoteDataApple *d = (ExifMnoteDataApple *) md;

    if (!d || (d->count <= i)) {
        return NULL;
    }

    return mnote_apple_tag_get_name(d->entries[i].tag);
}

static const char *
exif_mnote_data_apple_get_title(ExifMnoteData *md, unsigned int i) {
    ExifMnoteDataApple *d = (ExifMnoteDataApple *) md;

    if (!d || (d->count <= i)) {
        return NULL;
    }

    return mnote_apple_tag_get_title(d->entries[i].tag);
}

static const char *
exif_mnote_data_apple_get_description(ExifMnoteData *md, unsigned int i) {
    ExifMnoteDataApple *d = (ExifMnoteDataApple *) md;

    if (!d || (d->count <= i)) {
        return NULL;
    }

    return mnote_apple_tag_get_description(d->entries[i].tag);
}

static char *
exif_mnote_data_apple_get_value(ExifMnoteData *md, unsigned int i, char *val, unsigned int maxlen) {
    ExifMnoteDataApple *d = (ExifMnoteDataApple *) md;

    if (!val || !d || (d->count <= i)) {
        return NULL;
    }

    return mnote_apple_entry_get_value(&d->entries[i], val, maxlen);
}

int
exif_mnote_data_apple_identify(const ExifData *ed, const ExifEntry *e) {
    int variant;

    if (!strcmp((const char *) e->data, "Apple iOS")) {
        variant = 1;
    } else {
        variant = 0;
    }

    return variant;
}

ExifMnoteData *
exif_mnote_data_apple_new(ExifMem *mem) {
    ExifMnoteData *md;

    /*printf("%s\n", __FUNCTION__);*/

    if (!mem) {
        return NULL;
    }

    md = exif_mem_alloc(mem, sizeof(ExifMnoteDataApple));
    if (!md) {
        return NULL;
    }

    exif_mnote_data_construct(md, mem);

    md->methods.free = exif_mnote_data_apple_free;
    md->methods.load = exif_mnote_data_apple_load;
    md->methods.set_offset = exif_mnote_data_apple_set_offset;
    md->methods.set_byte_order = exif_mnote_data_apple_set_byte_order;
    md->methods.count = exif_mnote_data_apple_count;
    md->methods.get_id = exif_mnote_data_apple_get_id;
    md->methods.get_name = exif_mnote_data_apple_get_name;
    md->methods.get_title = exif_mnote_data_apple_get_title;
    md->methods.get_description = exif_mnote_data_apple_get_description;
    md->methods.get_value = exif_mnote_data_apple_get_value;

    return md;
}