File: exifscan.c

package info (click to toggle)
photopc 3.05-7
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 624 kB
  • ctags: 569
  • sloc: ansic: 6,242; sh: 330; makefile: 173
file content (372 lines) | stat: -rw-r--r-- 8,768 bytes parent folder | download | duplicates (6)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#ifndef LINT
static char *rcsid="$Id: exifscan.c,v 1.5 2000/05/09 13:20:54 crosser Exp $";
#endif

/*
	Copyright (c) 1997,1998 Eugene G. Crosser
	Copyright (c) 1998 Bruce D. Lightner (DOS/Windows support)

	You may distribute and/or use for any purpose modified or unmodified
	copies of this software if you preserve the copyright notice above.

	THIS SOFTWARE IS PROVIDED AS IS AND COME WITH NO WARRANTY OF ANY
	KIND, EITHER EXPRESSED OR IMPLIED.  IN NO EVENT WILL THE
	COPYRIGHT HOLDER BE LIABLE FOR ANY DAMAGES RESULTING FROM THE
	USE OF THIS SOFTWARE.
*/

/*
	$Log: exifscan.c,v $
	Revision 1.5  2000/05/09 13:20:54  crosser
	configure read() with alarm() better.
	Address signed vs. unsigned arguments
	other cleanups to make most notorious compilers happy
	
	Revision 1.4  1999/08/01 21:36:54  crosser
	Modify source to suit ansi2knr
	(I hate the style that ansi2knr requires but you don't expect me
	to write another smarter ansi2knr implementation, right?)

	Revision 1.3  1999/03/12 10:06:23  crosser
	avoid strncmp

	Revision 1.2  1999/03/10 22:34:43  crosser
	separate from app12 scan

	Revision 1.1  1999/03/09 18:19:39  crosser
	Initial revision

*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <stdio.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#include <string.h>
#endif
#include "exifscan.h"

int
exifscan(unsigned char **buf,off_t *bufsize)
{
	unsigned char *p;
	off_t length=0;

	p=*buf;
	while (p < ((*buf)+(*bufsize))) {
		while ((p < ((*buf)+(*bufsize))) && (*p != 0xff)) p++;
		while ((p < ((*buf)+(*bufsize))) && (*p == 0xff)) p++;
		if (*p == 0xD8) continue;
		length=(p[1]<<8)+p[2];
		if ((*p == 0xe1) && (strcasecmp(p+3,"Exif")==0)) {
			p+=3;
			length-=3;
			break;
		}
		p+=length;
	}
	if (p >= ((*buf)+(*bufsize))) {
		(*bufsize)=0;
		(*buf)=NULL;
		return -1;
	}

	(*bufsize)=length;
	(*buf)=p;
	return 0;
}

static struct _exifdict {
	int tag;
	char *name;
} exifdict[] = {
	{0xFE,		"NewSubFileType"},
	{0x100,		"ImageWidth"},
	{0x101,		"ImageLength"},
	{0x102,		"BitsPerSample"},
	{0x103,		"Compression"},
	{0x106,		"PhotometricInterpretation"},
	{0x10A,		"FillOrder"},
	{0x10D,		"DocumentName"},
	{0x10E,		"ImageDescription"},
	{0x10F,		"Make"},
	{0x110,		"Model"},
	{0x111,		"StripOffsets"},
	{0x112,		"Orientation"},
	{0x115,		"SamplesPerPixel"},
	{0x116,		"RowsPerStrip"},
	{0x117,		"StripByteCounts"},
	{0x11A,		"XResolution"},
	{0x11B,		"YResolution"},
	{0x11C,		"PlanarConfiguration"},
	{0x128,		"ResolutionUnit"},
	{0x12D,		"TransferFunction"},
	{0x131,		"Software"},
	{0x132,		"DateTime"},
	{0x13B,		"Artist"},
	{0x13E,		"WhitePoint"},
	{0x13F,		"PrimaryChromaticities"},
	{0x156,		"TransferRange"},
	{0x200,		"JPEGProc"},
	{0x201,		"JPEGInterchangeFormat"},
	{0x202,		"JPEGInterchangeFormatLength"},
	{0x211,		"YCbCrCoefficients"},
	{0x212,		"YCbCrSubSampling"},
	{0x213,		"YCbCrPositioning"},
	{0x214,		"ReferenceBlackWhite"},
	{0x828D,	"CFARepeatPatternDim"},
	{0x828E,	"CFAPattern"},
	{0x828F,	"BatteryLevel"},
	{0x8298,	"Copyright"},
	{0x829A,	"ExposureTime"},
	{0x829D,	"FNumber"},
	{0x83BB,	"IPTC/NAA"},
	{0x8769,	"ExifOffset"},
	{0x8773,	"InterColorProfile"},
	{0x8822,	"ExposureProgram"},
	{0x8824,	"SpectralSensitivity"},
	{0x8825,	"GPSInfo"},
	{0x8827,	"ISOSpeedRatings"},
	{0x8828,	"OECF"},
	{0x9000,	"ExifVersion"},
	{0x9003,	"DateTimeOriginal"},
	{0x9004,	"DateTimeDigitized"},
	{0x9101,	"ComponentsConfiguration"},
	{0x9102,	"CompressedBitsPerPixel"},
	{0x9201,	"ShutterSpeedValue"},
	{0x9202,	"ApertureValue"},
	{0x9203,	"BrightnessValue"},
	{0x9204,	"ExposureBiasValue"},
	{0x9205,	"MaxApertureValue"},
	{0x9206,	"SubjectDistance"},
	{0x9207,	"MeteringMode"},
	{0x9208,	"LightSource"},
	{0x9209,	"Flash"},
	{0x920A,	"FocalLength"},
	{0x927C,	"MakerNote"},
	{0x9286,	"UserComment"},
	{0x9290,	"SubSecTime"},
	{0x9291,	"SubSecTimeOriginal"},
	{0x9292,	"SubSecTimeDigitized"},
	{0xA000,	"FlashPixVersion"},
	{0xA001,	"ColorSpace"},
	{0xA002,	"ExifImageWidth"},
	{0xA003,	"ExifImageLength"},
	{0xA005,	"InteroperabilityOffset"},
	{0xA20B,	"FlashEnergy"},
	{0xA20C,	"SpatialFrequencyResponse"},
	{0xA20E,	"FocalPlaneXResolution"},
	{0xA20F,	"FocalPlaneYResolution"},
	{0xA210,	"FocalPlaneResolutionUnit"},
	{0xA214,	"SubjectLocation"},
	{0xA215,	"ExposureIndex"},
	{0xA217,	"SensingMethod"},
	{0xA300,	"FileSource"},
	{0xA301,	"SceneType"},
	{0,		NULL}
};

static int typlens[] = {1,1,2,4,8,1,1,2,4,8};

static int
s2n_intel(unsigned char *data,int length,int sign)
{
	int i,msb,val=0;
	for (i=0;i<length;i++) {
		val |= (data[i] << (i*8));
	}
	if (sign) {
		msb=1<<(length*8-1);
		if (val & msb) val-=(msb<<1);
	}
	return val;
}

static int
s2n_motorola(unsigned char *data,int length,int sign)
{
	int i,msb,val=0;
	for (i=0;i<length;i++) {
		val = (val << 8) | data[i];
	}
	if (sign) {
		msb=1<<(length*8-1);
		if (val & msb) val-=(msb<<1);
	}
	return val;
}

static int (*s2n)(unsigned char *data,int length,int signed);
#define S2N(x,y,z) ((*s2n)(x,y,z))

static unsigned char *
exifwalk(unsigned char *data,int where,
			unsigned char *(*each)(
				unsigned char *data,int offset,int tag,
				int typ,int typlen,int count,void *priv),
			void *priv);

static unsigned char *
exifstr(unsigned char *data,int offset,int tag,int typ,int typlen,int count)
{
	static unsigned char *buf=NULL;
	static int buflen=0;
	int len;

	if (typ == 2) {
		len=count+3;
		if (len > buflen) {
			if (buf) free(buf);
			buf=(unsigned char*)malloc((size_t)len);
		}
		if (buf == NULL) return "<no memory>";
		sprintf(buf,"\"%s\"",data+offset);
	} else {
		int k;
		int sig;

		len=count*22+3;
		if (len > buflen) {
			if (buf) free(buf);
			buf=(unsigned char*)malloc((size_t)len);
		}
		if (buf == NULL) return "<no memory>";
		buf[0]='\0';
		if ((typ == 6) || (typ >= 8)) sig=1;
		else sig=0;
		if (count > 1) sprintf(buf,"(");
		for (k=0;k<count;k++) {
			if ((typ == 5) || (typ == 10)) {
				sprintf(buf+strlen(buf),"%s%d/%d",
					k?",":"",
					S2N(data+offset+typlen*k,4,sig),
					S2N(data+offset+4+typlen*k,4,sig));
			} else {
				sprintf(buf+strlen(buf),"%s%d",
					k?",":"",
					S2N(data+offset+typlen*k,typlen,sig));
			}
		}
		if (count > 1) sprintf(buf+strlen(buf),")");
	}
	return buf;
}

static unsigned char *
exifmatch(unsigned char *data,int offset,int tag,
				int typ,int typlen,int count,void *priv)
{
	int i;
	int needtag=0;
	unsigned char *rc;

	for (i=0;exifdict[i].tag;i++) {
		if (strcmp(exifdict[i].name,(char*)priv) == 0) {
			needtag=exifdict[i].tag;
			break;
		}
	}
	if (tag == 0x8769) {
		rc=exifwalk(data,offset,exifmatch,priv);
		if (rc) return rc;
	} else if (tag == 0xA005) {
		rc=exifwalk(data,offset,exifmatch,priv);
		if (rc) return rc;
	}
	if (needtag == tag)
		return exifstr(data,offset,tag,typ,typlen,count);
	else return NULL;
}

static unsigned char *
exifprt(unsigned char *data,int offset,int tag,
				int typ,int typlen,int count,void *priv)
{
	unsigned char *rc;
	int i;
	char *tagname=NULL,badtag[32];

	for (i=0;exifdict[i].tag;i++) {
		if (exifdict[i].tag == tag) {
			tagname=exifdict[i].name;
			break;
		}
	}
	if (tagname == NULL) {
		sprintf(badtag,"TAG#%d",tag);
		tagname=badtag;
	}
	if (tag == 0x8769) {
		printf("<Exif SubIFD>\n");
		rc=exifwalk(data,offset,exifprt,priv);
		printf("</Exif SubIFD>\n");
		if (rc) return rc;
	} else if (tag == 0xA005) {
		printf("<Interoperability SubIFD>\n");
		rc=exifwalk(data,offset,exifprt,priv);
		printf("</Interoperability SubIFD>\n");
		if (rc) return rc;
	} else {
		printf("%s=%s\n",tagname,
			exifstr(data,offset,tag,typ,typlen,count));
	}
	return NULL;
}

static unsigned char *
exifwalk(unsigned char *data,int where,
			unsigned char *(*each)(
				unsigned char *data,int offset,int tag,
				int typ,int typlen,int count,void *priv),
			void *priv)
{
	unsigned char *rc=NULL;
	int i;

	if (where == 0) {
		switch (data[0]) {
		case 'I':	s2n=s2n_intel; break;
		case 'M':	s2n=s2n_motorola; break;
		default:	return NULL;
		}
		where=S2N(data+4,4,0);
	} else where=S2N(data+where,4,0);
	for (i=where;i;i=S2N(data+i+2+(12*S2N(data+i,2,0)),4,0)) {
		int ifsnum=S2N(data+i,2,0);
		int j;

		for (j=0;j<ifsnum;j++) {
			int typlen,count;
			int offset;
			int entry=i+2+12*j;
			int tag=S2N(data+entry,2,0);
			int typ=S2N(data+entry+2,2,0);
			if ((typ < 1) || (typ > 10)) continue;
			typlen=typlens[typ-1];
			count=S2N(data+entry+4,4,0);
			offset=entry+8;
			if (count*typlen > 4) offset=S2N(data+offset,4,0);
			if ((rc=(*each)(data,offset,tag,typ,
							typlen,count,priv)))
				return rc;
		}
	}
	return NULL;
}

unsigned char *
exifsearch(char *key,unsigned char *buf,off_t bufsize)
{
	if (buf == NULL) return NULL;
	return exifwalk(buf+6,0,exifmatch,(void*)key);
}

void
exifdump(unsigned char *buf,off_t bufsize)
{
	if (buf == NULL) return;
	exifwalk(buf+6,0,exifprt,NULL);
}