Codebase list libiptcdata / HEAD libiptcdata / iptc-dataset.c
HEAD

Tree @HEAD (Download .tar.gz)

iptc-dataset.c @HEADraw · history · blame

  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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/* iptc-dataset.c
 *
 * Copyright (c) 2005 David Moore <dcm@acm.org>
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "iptc-dataset.h"
#include "iptc-utils.h"
#include "i18n.h"

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

#undef  MIN
#define MIN(a, b)  (((a) < (b)) ? (a) : (b))

struct _IptcDataSetPrivate
{
	unsigned int ref_count;

	IptcMem *mem;
};

/**
 * iptc_dataset_new:
 *
 * Allocates a new dataset, which is initialized to be empty (undefined tag and
 * empty value).  The default memory allocation functions (malloc, etc.) are
 * used.  If you need custom memory management functions, use
 * iptc_dataset_new_mem() instead.  This allocation will set the #IptcDataSet
 * refcount to 1, so use iptc_dataset_unref() when finished with the pointer.
 *
 * Returns: pointer to the new #IptcDataSet object, NULL on error
 */
IptcDataSet *
iptc_dataset_new (void)
{
	IptcMem *mem = iptc_mem_new_default ();
	IptcDataSet *e = iptc_dataset_new_mem (mem);

	iptc_mem_unref (mem);

	return e;
}

/**
 * iptc_dataset_new_mem:
 * @mem: Pointer to an #IptcMem object that defines custom memory managment
 * functions.  The refcount of @mem will be incremented.  It is decremented
 * when the returned #IptcDataSet object is freed.
 *
 * Allocates a new dataset, which is initialized to be empty (undefined tag and
 * empty value), using custom memory management functions.  This allocation
 * will set the #IptcDataSet refcount to 1, so use iptc_dataset_unref() when
 * finished with the pointer.
 *
 * Returns: pointer to the new #IptcDataSet object, NULL on error
 */
IptcDataSet *
iptc_dataset_new_mem (IptcMem *mem)
{
	IptcDataSet *e = NULL;

	e = iptc_mem_alloc (mem, sizeof (IptcDataSet));
	if (!e) return NULL;
	e->priv = iptc_mem_alloc (mem, sizeof (IptcDataSetPrivate));
	if (!e->priv) { iptc_mem_free (mem, e); return NULL; }
	e->priv->ref_count = 1;

	e->priv->mem = mem;
	iptc_mem_ref (mem);

	return e;
}

/**
 * iptc_dataset_copy:
 * @dataset: the dataset to duplicate
 *
 * Allocates a new dataset and copies the contents of an existing dataset
 * into the new one.  Copied data includes record, tag, and the data payload.
 * This is a "deep copy" so that a new copy of the data payload is created,
 * not just a pointer duplication.  The new dataset has no parent collection,
 * regardless of the parent of the copied dataset.  This allocation will set
 * the #IptcDataSet refcount to 1, so use iptc_dataset_unref() when finished
 * with the pointer.
 *
 * Returns: pointer to the new #IptcDataSet object, NULL on error
 */
IptcDataSet *
iptc_dataset_copy (IptcDataSet *e)
{
	IptcDataSet * copy;
	if (!e)
		return NULL;

	copy = iptc_dataset_new_mem (e->priv->mem);

	copy->record = e->record;
	copy->tag = e->tag;
	copy->info = e->info;
	copy->parent = NULL;
	iptc_dataset_set_data (copy, e->data, e->size, IPTC_DONT_VALIDATE);

	return copy;
}

/**
 * iptc_dataset_ref:
 * @dataset: the referenced pointer
 *
 * Increments the reference count of an #IptcDataSet object.  This function
 * should be called whenever a copy of a pointer is made by the application.
 * iptc_dataset_unref() can then be used when the pointer is no longer needed
 * to ensure that the object is freed once the object is completely unused.
 */
void
iptc_dataset_ref (IptcDataSet *e)
{
	if (!e) return;

	e->priv->ref_count++;
}

/**
 * iptc_dataset_unref:
 * @dataset: the unreferenced pointer
 *
 * Decrements the reference count of an #IptcDataSet object.  The object will
 * automatically be freed when the count reaches 0.  This function should
 * be called whenever a pointer is no longer needed by an application.  It
 * is also good practice to set the local copy of the pointer to NULL to
 * shield against accidently reusing the value.
 */
void
iptc_dataset_unref (IptcDataSet *e)
{
	if (!e) return;

	e->priv->ref_count--;
	if (!e->priv->ref_count)
		iptc_dataset_free (e);
}

/**
 * iptc_dataset_free:
 * @dataset: the object to free
 *
 * Frees an #IptcDataSet object and its contained data.  This function should
 * be used only for error handling since iptc_dataset_unref() provides a
 * safer mechanism for freeing that allows multiple components to have access
 * to an object.
 */
void
iptc_dataset_free (IptcDataSet *e)
{
	if (!e) return;

	if (e->priv) {
		IptcMem *mem = e->priv->mem;
		if (e->data)
			iptc_mem_free (mem, e->data);
		iptc_mem_free (mem, e->priv);
		iptc_mem_free (mem, e);
		iptc_mem_unref (mem);
	}
}

/**
 * iptc_dataset_set_tag:
 * @dataset: dataset for which the record/tag numbers should be set
 * @record: record number of the dataset
 * @tag: tag (dataset number) of the dataset
 *
 * Sets the record and tag number for a dataset object.  Each record/tag pair
 * is assigned a specific meaning by the IPTC IIM specification.  This function
 * allows any value to be set for the dataset object, regardless of its
 * meaning.  This function can also be used to change the record/tag number in
 * place even if it has been already set.
 */
void
iptc_dataset_set_tag (IptcDataSet *e, IptcRecord record, IptcTag tag)
{
	if (!e)
		return;

	e->record = record;
	e->tag    = tag;
	e->info   = iptc_tag_get_info (record, tag);
}

/**
 * iptc_dataset_set_data:
 * @dataset: dataset for which the value should be set
 * @buf: buffer containing the value
 * @size: number of bytes to copy
 * @validate: whether or not the passed value should be validated against
 * the IPTC specification for this dataset's tag.
 *
 * Copies bytes from a buffer as the new value for a dataset.  This is the
 * correct method for setting a dataset value to a string or raw binary
 * sequence.  The length of a string should be specified without the trailing
 * NULL.  New memory is allocated for the value and @size bytes are copied
 * from @buf into this new memory (which is freed when the dataset is freed or
 * when a new value is set).  If @validate is set to %IPTC_VALIDATE, the buffer
 * will only be copied if the size does not violate the IPTC specification (it
 * must not be too long or too short).  This check is ignored if the dataset's
 * tag is undefined or no information from the specification is available for
 * that tag.
 *
 * Returns: -1 on error, 0 if validation failed, the number of bytes copied
 * on success
 */
int
iptc_dataset_set_data (IptcDataSet *e, const unsigned char * buf, unsigned int size,
		IptcValidate validate)
{
	if (!e || !buf || !size)
		return -1;

	if (validate && e->info) {
		if (size > e->info->maxbytes || size < e->info->minbytes)
			return 0;
	}

	if (e->data)
		iptc_mem_free (e->priv->mem, e->data);

	e->size = 0;
	e->data = iptc_mem_alloc (e->priv->mem, size);
	if (!e->data)
		return -1;
	memcpy (e->data, buf, size);
	e->size = size;
	return size;
}

/**
 * iptc_dataset_set_value:
 * @dataset: dataset for which the value should be set
 * @value: value for the dataset
 * @validate: whether or not the passed value should be validated against
 * the IPTC specification for this dataset's tag.
 *
 * Sets the value of a dataset as an integer.  This is the correct method for
 * setting a dataset value with type byte, short, or long.  If @validate is
 * set to %IPTC_VALIDATE, the value will only be set if the tag has type
 * byte, short, or long according the IPTC specification.  This check is
 * ignored if the dataset's tag is undefined or no information from the
 * specification is available for that tag.
 *
 * Returns: -1 on error, 0 if validation failed, the number of bytes copied
 * on success
 */
int
iptc_dataset_set_value (IptcDataSet *e, unsigned int value,
		IptcValidate validate)
{
	IptcFormat format = IPTC_FORMAT_LONG;
	int size;

	if (!e)
		return -1;

	if (e->info)
		format = e->info->format;

	switch (format) {
		case IPTC_FORMAT_BYTE:
			size = 1;
			break;
		case IPTC_FORMAT_SHORT:
			size = 2;
			break;
		case IPTC_FORMAT_LONG:
			size = 4;
			break;
		default:
			size = 4;
			if (validate)
				return 0;
			break;
	}
	
	if (e->data)
		iptc_mem_free (e->priv->mem, e->data);

	e->size = 0;
	e->data = iptc_mem_alloc (e->priv->mem, size);
	if (!e->data)
		return -1;
	
	e->size = size;
	switch (format) {
		case IPTC_FORMAT_BYTE:
			e->data[0] = value;
			break;
		case IPTC_FORMAT_SHORT:
			iptc_set_short (e->data, IPTC_BYTE_ORDER_MOTOROLA, value);
			break;
		default:
			iptc_set_long (e->data, IPTC_BYTE_ORDER_MOTOROLA, value);
			break;
	}
	return size;
}

/**
 * iptc_dataset_set_date:
 * @dataset: pointer to an #IptcDataSet
 * @year: the year (0-9999)
 * @month: the month (1-12)
 * @day: the day of the month (1-31)
 * @validate: if set to %IPTC_VALIDATE, the dataset date will only be set if
 * the IPTC specification says it is of type date.
 *
 * Sets the contents of the dataset to be a date with the specified value.
 * IPTC dates are stored as a string in the format YYYYMMDD.
 *
 * Returns: the number of bytes written on success (always 8 in this case); 0
 * if validation fails; -1 for other failures
 */
int
iptc_dataset_set_date (IptcDataSet *e, int year, int month, int day,
		IptcValidate validate)
{
	char str[9];

	if (!e)
		return -1;
	if (year < 0 || month < 1 || day < 1)
		return -1;
	if (year > 9999 || month > 12 || day > 31)
		return -1;
	if (validate && e->info && e->info->format != IPTC_FORMAT_DATE)
		return 0;

	if (e->data)
		iptc_mem_free (e->priv->mem, e->data);

	e->size = 0;
	e->data = iptc_mem_alloc (e->priv->mem, 8);
	if (!e->data)
		return -1;
	
	e->size = 8;
	sprintf (str, "%04d%02d%02d", year, month, day);
	memcpy (e->data, str, 8);

	return 8;
}

/**
 * iptc_dataset_set_time:
 * @dataset: pointer to an #IptcDataSet
 * @hour: the hour (0-23)
 * @min: the minutes (0-59)
 * @sec: the seconds (0-61)
 * @tz: the timezone expressed as the number of minutes offset from GMT.
 * For example, EST is -300 (-5 hours).
 * @validate: if set to %IPTC_VALIDATE, the dataset time will only be set if
 * the IPTC specification says it is of type time.
 *
 * Sets the contents of the dataset to be a time with the specified value.
 * IPTC times are stored as a string in the format HHMMSS&plusmn;HHMM.
 *
 * Returns: the number of bytes written on success (always 11 in this case); 0
 * if validation fails; -1 for other failures
 */
int
iptc_dataset_set_time (IptcDataSet *e, int hour, int min, int sec, int tz,
		IptcValidate validate)
{
	char str[12];

	if (!e)
		return -1;
	if (hour < 0 || min < 0 || sec < 0 || tz < -1439)
		return -1;
	if (hour > 23 || min > 59 || sec > 61 || tz > 1439)
		return -1;
	if (validate && e->info && e->info->format != IPTC_FORMAT_TIME)
		return 0;

	if (e->data)
		iptc_mem_free (e->priv->mem, e->data);

	e->size = 0;
	e->data = iptc_mem_alloc (e->priv->mem, 11);
	if (!e->data)
		return -1;
	
	e->size = 11;
	sprintf (str, "%02d%02d%02d%+03d%02d", hour, min, sec, tz / 60, abs (tz % 60));
	memcpy (e->data, str, 11);

	return 11;
}

/**
 * iptc_dataset_get_format:
 * @dataset: pointer to an #IptcDataSet
 *
 * Retrieves the data format of a dataset tag according to the IPTC
 * specification.
 *
 * Returns: the format of the tag which will be %IPTC_FORMAT_UNKNOWN if the tag
 * number has not been set or the tag is not in the specification
 */
IptcFormat
iptc_dataset_get_format (IptcDataSet *e)
{
	if (!e || !e->info)
		return IPTC_FORMAT_UNKNOWN;

	return e->info->format;
}

/**
 * iptc_dataset_get_data:
 * @dataset: pointer to an #IptcDataSet
 * @buf: buffer to which the data will be copied
 * @size: maximum size of the buffer
 *
 * Copies the value of a dataset into a buffer.  If the buffer has extra
 * space, the data will be NULL-terminated to assist in dealing with
 * strings.
 *
 * Returns: the number of bytes copied, or -1 on failure
 */
int
iptc_dataset_get_data (IptcDataSet *e, unsigned char *val, unsigned int size)
{
	int s;
	if (!e || !val || !size)
		return -1;
	s = MIN(e->size+1, size);
	memcpy (val, e->data, MIN(e->size, size));
	val[s-1] = '\0';

	return s;
}

/**
 * iptc_dataset_get_value:
 * @dataset: pointer to an #IptcDataSet
 *
 * Gets the value of a dataset as an unsigned integer.  This function is meant
 * to be used when the format of a tag is a byte, short, or long.  Undefined
 * results occur when this function is used on a tag containing data in another
 * format.
 *
 * Returns: value contained in the dataset
 */
unsigned int
iptc_dataset_get_value (IptcDataSet *e)
{
	if (!e || !e->data || !e->size)
		return 0;
	switch (e->size) {
		case 1:
			return e->data[0];
		case 2:
			return iptc_get_short (e->data, IPTC_BYTE_ORDER_MOTOROLA);
		case 3:
			return (iptc_get_short (e->data, IPTC_BYTE_ORDER_MOTOROLA) << 8)
				| e->data[2];
		default:
			return iptc_get_long (e->data, IPTC_BYTE_ORDER_MOTOROLA);
	}
}

/**
 * iptc_dataset_get_date:
 * @dataset: pointer to an #IptcDataSet
 * @year: output variable to store the year (0-9999)
 * @month: output variable to store the month (1-12)
 * @day: output variable to store the day of the month (1-31)
 *
 * Interprets the contents of the dataset as an IPTC date and parses
 * the year, month, and day into the given output variables.  If any
 * variable is NULL, that value is skipped.  IPTC dates are stored as
 * a string in the format YYYYMMDD.
 *
 * Returns: 0 on success; -1 on failure
 */
int
iptc_dataset_get_date (IptcDataSet *e, int *year, int *month, int *day)
{
	if (!e || !e->data || e->size < 8)
		return -1;

	if (year) {
		*year = (e->data[0]-'0')*1000 + (e->data[1]-'0')*100 +
			(e->data[2]-'0')*10 + (e->data[3]-'0');
	}

	if (month) {
		*month = (e->data[4]-'0')*10 + (e->data[5]-'0');
	}

	if (day) {
		*day = (e->data[6]-'0')*10 + (e->data[7]-'0');
	}

	return 0;
}

/**
 * iptc_dataset_get_time:
 * @dataset: pointer to an #IptcDataSet
 * @hour: output variable to store the hour (0-23)
 * @min: output variable to store the minute (0-59)
 * @sec: output variable to store the second (0-59)
 * @tz: output variable to store the timezone (offset in minutes from GMT)
 *
 * Interprets the contents of the dataset as an IPTC time and parses
 * the hour, minute, second, and timezone into the given output variables.
 * If any variable is NULL, that value is skipped.  IPTC times are stored as
 * a string in the format HHMMSS&plusmn;HHMM.
 *
 * Returns: 0 on success; -1 on failure
 */
int
iptc_dataset_get_time (IptcDataSet *e, int *hour, int *min, int *sec, int *tz)
{
	if (!e || !e->data || e->size < 11)
		return -1;

	if (hour) {
		*hour = (e->data[0]-'0')*10 + (e->data[1]-'0');
	}

	if (min) {
		*min = (e->data[2]-'0')*10 + (e->data[3]-'0');
	}

	if (sec) {
		*sec = (e->data[4]-'0')*10 + (e->data[5]-'0');
	}

	if (tz) {
		*tz = (e->data[7]-'0')*600 + (e->data[8]-'0')*60 +
			(e->data[9]-'0')*10 + (e->data[10]-'0');
		if (e->data[6] == '-')
			*tz = -*tz;
	}

	return 0;
}


/**
 * iptc_dataset_dump:
 * @dataset: dataset to print
 * @indent: indentation level of the printout
 *
 * A debugging aid that prints out the contents of a dataset to
 * standard output.
 */
void
iptc_dataset_dump (IptcDataSet *e, unsigned int indent)
{
	char value[1024];
	const char *name;

	if (!e)
		return;

	name = iptc_tag_get_name (e->record, e->tag);
	printf ("%*sTag: %d:%d ('%s')\n", 2*indent, "", e->record, e->tag,
		name ? name : "");
/*	printf ("%s  Format: %i ('%s')\n", buf, e->format,
		iptc_format_get_name (e->format)); */
	printf ("%*s  Size: %i\n", 2*indent, "", e->size);
	printf ("%*s  Value: %s\n", 2*indent, "", iptc_dataset_get_as_str (e, value, sizeof(value)));
}

#define TAG_ID(r,t)	(((r) << 8) | (t))

/**
 * iptc_dataset_get_as_str:
 * @dataset: pointer to an #IptcDataSet
 * @buf: buffer to which the string will be copied
 * @size: maximum size of the buffer
 *
 * Copies the value of a dataset into a buffer as a printable, NULL-terminated
 * string.  For tags of type byte, short, or long, the value will be formatted
 * as a string.  For tags of type string (including dates and times), the
 * string will simply be copied into the output buffer.  For tags containing
 * binary data, the data will be formatted as a string in hexidecimal.
 *
 * Returns: the output string on success, or NULL on failure
 */
const char *
iptc_dataset_get_as_str (IptcDataSet *e, char *val, unsigned int maxlen)
{
	unsigned int i;
	IptcByte v_byte;
	IptcShort v_short;
	IptcLong v_long;
	IptcFormat format = IPTC_FORMAT_BINARY;

	if (!e || !val || !maxlen)
		return NULL;

	if (e->info)
		format = e->info->format;

	if (!e->size)
		format = IPTC_FORMAT_STRING;

	switch (TAG_ID(e->record, e->tag)) {
	default:
		switch (format) {
		case IPTC_FORMAT_BYTE:
			v_byte = e->data[0];
			snprintf (val, maxlen, "%hhu", v_byte);
			break;
		case IPTC_FORMAT_SHORT:
			v_short = iptc_get_short (e->data, IPTC_BYTE_ORDER_MOTOROLA);
			snprintf (val, maxlen, "%hu", v_short);
			break;
		case IPTC_FORMAT_LONG:
			v_long = iptc_get_long (e->data, IPTC_BYTE_ORDER_MOTOROLA);
			snprintf (val, maxlen, "%u", v_long);
			break;
		case IPTC_FORMAT_STRING:
		case IPTC_FORMAT_NUMERIC_STRING:
		case IPTC_FORMAT_DATE:
		case IPTC_FORMAT_TIME:
			strncpy (val, (char *)e->data, MIN (maxlen, e->size));
			if (maxlen > e->size)
				val[e->size] = '\0';
			break;
		default:
			for (i=0; i < MIN (maxlen/3, e->size); i++) {
				if (i > 0)
					val[i*3-1] = ' ';
				sprintf(val+i*3, "%02x", e->data[i]);
			}
			break;
		}
	}

	return val;
}