summaryrefslogtreecommitdiff
path: root/exempi/exempi.cpp
blob: 5392047970a2b042a3d27aaa882911d16a12dbad (plain)
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
/*
 * exempi - exempi.cpp
 *
 * Copyright (C) 2007-2016 Hubert Figuière
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1 Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2 Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * 3 Neither the name of the Authors, nor the names of its
 * contributors may be used to endorse or promote products derived
 * from this software wit hout specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @brief this file implement the glue for XMP API
 */

#include "xmpconsts.h"
#include "xmp.h"
#include "xmperrors.h"

#include <string>
#include <iostream>
#include <memory>

#define XMP_INCLUDE_XMPFILES 1
#define TXMP_STRING_TYPE std::string
#include "XMP.hpp"
#include "XMP.incl_cpp"
#include "XMPUtils.hpp"

#if HAVE_NATIVE_TLS

static TLS int g_error = 0;

static void set_error(int err)
{
    g_error = err;
}

#else

#include <pthread.h>

/* Portable thread local storage using pthreads */
static pthread_key_t key = NULL;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;

/* Destructor called when a thread exits - ensure to delete allocated int
 * pointer */
static void destroy_tls_key(void *ptr)
{
    int *err_ptr = static_cast<int *>(ptr);
    delete err_ptr;
}

/* Create a key for use with pthreads local storage */
static void create_tls_key()
{
    (void)pthread_key_create(&key, destroy_tls_key);
}

/* Obtain the latest xmp error for this specific thread - defaults to 0 */
static int get_error_for_thread()
{
    int *err_ptr;

    pthread_once(&key_once, create_tls_key);
    err_ptr = (int *)pthread_getspecific(key);

    if (err_ptr == NULL) {
        return 0;
    }

    return *err_ptr;
}

/* set the current xmp error for this specific thread */
static void set_error(int err)
{
    int *err_ptr;

    // Ensure that create_thread_local_storage_key is only called
    // once, by the first thread, to create the key.
    pthread_once(&key_once, create_tls_key);

    // Retrieve pointer to int for this thread.
    err_ptr = (int *)pthread_getspecific(key);

    // Allocate it, if it does not exists.
    if (err_ptr == NULL) {
        err_ptr = new int;
        pthread_setspecific(key, err_ptr);
    }

    // Save the error for this thread.
    *err_ptr = err;
}

#endif

static void set_error(const XMP_Error &e)
{
    set_error(-e.GetID());
    std::cerr << e.GetErrMsg() << std::endl;
}

#define RESET_ERROR set_error(0)

// Hack: assign between XMP_DateTime and XmpDateTime
#define ASSIGN(dst, src)                                                       \
    (dst).year = (src).year;                                                   \
    (dst).month = (src).month;                                                 \
    (dst).day = (src).day;                                                     \
    (dst).hour = (src).hour;                                                   \
    (dst).minute = (src).minute;                                               \
    (dst).second = (src).second;                                               \
    (dst).tzSign = (src).tzSign;                                               \
    (dst).tzHour = (src).tzHour;                                               \
    (dst).tzMinute = (src).tzMinute;                                           \
    (dst).nanoSecond = (src).nanoSecond;

#ifdef __cplusplus
extern "C" {
#endif

const char NS_XMP_META[] = "adobe:ns:meta/";
const char NS_RDF[] = kXMP_NS_RDF;
const char NS_EXIF[] = kXMP_NS_EXIF;
const char NS_TIFF[] = kXMP_NS_TIFF;
const char NS_XAP[] = kXMP_NS_XMP;
const char NS_XAP_RIGHTS[] = kXMP_NS_XMP_Rights;
const char NS_DC[] = kXMP_NS_DC;
const char NS_EXIF_AUX[] = kXMP_NS_EXIF_Aux;
const char NS_CRS[] = kXMP_NS_CameraRaw;
const char NS_LIGHTROOM[] = "http://ns.adobe.com/lightroom/1.0/";
const char NS_CAMERA_RAW_SETTINGS[] = kXMP_NS_CameraRaw;
const char NS_CAMERA_RAW_SAVED_SETTINGS[] =
    "http://ns.adobe.com/camera-raw-saved-settings/1.0/";
const char NS_PHOTOSHOP[] = kXMP_NS_Photoshop;
const char NS_IPTC4XMP[] = kXMP_NS_IPTCCore;
const char NS_TPG[] = kXMP_NS_XMP_PagedFile;
const char NS_DIMENSIONS_TYPE[] = kXMP_NS_XMP_Dimensions;
const char NS_CC[] = "http://creativecommons.org/ns#";
const char NS_PDF[] = kXMP_NS_PDF;

#define STRING(x) reinterpret_cast<std::string *>(x)

#define CHECK_PTR(p, r)                                                        \
    if (p == NULL) {                                                           \
        set_error(XMPErr_BadObject);                                           \
        return r;                                                              \
    }

int xmp_get_error()
{
#if HAVE_NATIVE_TLS
    return g_error;
#else
    return get_error_for_thread();
#endif
}

bool xmp_init()
{
    RESET_ERROR;
    try {
        // no need to initialize anything else.
        // XMP SDK 5.1.2 needs this because it has been lobotomized of local
        // text
        // conversion
        // the one that was done in Exempi with libiconv.
        return SXMPFiles::Initialize(kXMPFiles_IgnoreLocalText);
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return false;
}

void xmp_terminate()
{
    RESET_ERROR;
    SXMPFiles::Terminate();
}

bool xmp_register_namespace(const char *namespaceURI,
                            const char *suggestedPrefix,
                            XmpStringPtr registeredPrefix)
{
    RESET_ERROR;
    try {
        return SXMPMeta::RegisterNamespace(namespaceURI, suggestedPrefix,
                                           STRING(registeredPrefix));
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return false;
}

bool xmp_namespace_prefix(const char *ns, XmpStringPtr prefix)
{
    CHECK_PTR(ns, false);
    RESET_ERROR;
    try {
        return SXMPMeta::GetNamespacePrefix(ns, STRING(prefix));
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return false;
}

bool xmp_prefix_namespace_uri(const char *prefix, XmpStringPtr ns)
{
    CHECK_PTR(prefix, false);
    RESET_ERROR;
    try {
        return SXMPMeta::GetNamespaceURI(prefix, STRING(ns));
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return false;
}

XmpFilePtr xmp_files_new()
{
    RESET_ERROR;

    try {
        auto txf = std::unique_ptr<SXMPFiles>(new SXMPFiles());

        return reinterpret_cast<XmpFilePtr>(txf.release());
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return NULL;
}

XmpFilePtr xmp_files_open_new(const char *path, XmpOpenFileOptions options)
{
    CHECK_PTR(path, NULL);
    RESET_ERROR;

    try {
        auto txf = std::unique_ptr<SXMPFiles>(new SXMPFiles);

        txf->OpenFile(path, XMP_FT_UNKNOWN, options);

        return reinterpret_cast<XmpFilePtr>(txf.release());
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }

    return NULL;
}

bool xmp_files_open(XmpFilePtr xf, const char *path, XmpOpenFileOptions options)
{
    CHECK_PTR(xf, false);
    RESET_ERROR;
    auto txf = reinterpret_cast<SXMPFiles *>(xf);
    try {
        return txf->OpenFile(path, XMP_FT_UNKNOWN, options);
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return false;
}

bool xmp_files_close(XmpFilePtr xf, XmpCloseFileOptions options)
{
    CHECK_PTR(xf, false);
    RESET_ERROR;
    try {
        auto txf = reinterpret_cast<SXMPFiles *>(xf);
        txf->CloseFile(options);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return false;
    }
    return true;
}

XmpPtr xmp_files_get_new_xmp(XmpFilePtr xf)
{
    CHECK_PTR(xf, NULL);
    RESET_ERROR;
    auto txf = reinterpret_cast<SXMPFiles *>(xf);

    bool result = false;
    try {
        auto xmp = std::unique_ptr<SXMPMeta>(new SXMPMeta);
        result = txf->GetXMP(xmp.get());
        if (result) {
            return reinterpret_cast<XmpPtr>(xmp.release());
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return NULL;
}

bool xmp_files_get_xmp(XmpFilePtr xf, XmpPtr xmp)
{
    CHECK_PTR(xf, false);
    CHECK_PTR(xmp, false);
    RESET_ERROR;
    bool result = false;
    try {
        auto txf = reinterpret_cast<SXMPFiles *>(xf);

        result = txf->GetXMP(reinterpret_cast<SXMPMeta *>(xmp));
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return result;
}

bool xmp_files_get_xmp_xmpstring(XmpFilePtr xf, XmpStringPtr xmp_packet,
                                 XmpPacketInfo* packet_info)
{
  CHECK_PTR(xf, false);
  CHECK_PTR(xmp_packet, false);
  RESET_ERROR;
  bool result = false;
  try {
    SXMPFiles *txf = reinterpret_cast<SXMPFiles*>(xf);

    XMP_PacketInfo xmp_packet_info;
    result = txf->GetXMP(NULL,
                         reinterpret_cast<std::string*>(xmp_packet),
                         &xmp_packet_info);
    if (packet_info) {
      packet_info->offset = xmp_packet_info.offset;
      packet_info->length = xmp_packet_info.length;
      packet_info->padSize = xmp_packet_info.padSize;
      packet_info->charForm = xmp_packet_info.charForm;
      packet_info->writeable = xmp_packet_info.writeable;
      packet_info->hasWrapper = xmp_packet_info.hasWrapper;
      packet_info->pad = xmp_packet_info.pad;
    }
  }
  catch(const XMP_Error & e) {
    set_error(e);
  }
  return result;
}


bool xmp_files_can_put_xmp(XmpFilePtr xf, XmpPtr xmp)
{
    CHECK_PTR(xf, false);
    RESET_ERROR;
    auto txf = reinterpret_cast<SXMPFiles *>(xf);
    bool result = false;

    try {
        result = txf->CanPutXMP(*reinterpret_cast<const SXMPMeta *>(xmp));
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return false;
    }
    return result;
}

bool xmp_files_can_put_xmp_xmpstring(XmpFilePtr xf, XmpStringPtr xmp_packet)
{
  CHECK_PTR(xf, false);
  RESET_ERROR;
  SXMPFiles *txf = reinterpret_cast<SXMPFiles*>(xf);
  bool result = false;

  try {
    result = txf->CanPutXMP(*reinterpret_cast<const std::string*>(xmp_packet));
  }
  catch(const XMP_Error & e) {
    set_error(e);
    return false;
  }
  return result;
}

bool xmp_files_can_put_xmp_cstr(XmpFilePtr xf, const char* xmp_packet, size_t len)
{
  CHECK_PTR(xf, false);
  RESET_ERROR;
  SXMPFiles *txf = reinterpret_cast<SXMPFiles*>(xf);
  bool result = false;

  try {
    result = txf->CanPutXMP(xmp_packet, len);
  }
  catch(const XMP_Error & e) {
    set_error(e);
    return false;
  }
  return result;
}

bool xmp_files_put_xmp(XmpFilePtr xf, XmpPtr xmp)
{
    CHECK_PTR(xf, false);
    CHECK_PTR(xmp, false);
    RESET_ERROR;
    auto txf = reinterpret_cast<SXMPFiles *>(xf);

    try {
        txf->PutXMP(*reinterpret_cast<const SXMPMeta *>(xmp));
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return false;
    }
    return true;
}

bool xmp_files_put_xmp_xmpstring(XmpFilePtr xf, XmpStringPtr xmp_packet)
{
  CHECK_PTR(xf, false);
  CHECK_PTR(xmp_packet, false);
  RESET_ERROR;
  SXMPFiles *txf = reinterpret_cast<SXMPFiles*>(xf);

  try {
    txf->PutXMP(*reinterpret_cast<const std::string*>(xmp_packet));
  }
  catch(const XMP_Error & e) {
    set_error(e);
    return false;
  }
  return true;
}

bool xmp_files_put_xmp_cstr(XmpFilePtr xf, const char* xmp_packet, size_t len)
{
  CHECK_PTR(xf, false);
  CHECK_PTR(xmp_packet, false);
  RESET_ERROR;
  SXMPFiles *txf = reinterpret_cast<SXMPFiles*>(xf);

  try {
    txf->PutXMP(xmp_packet, len);
  }
  catch(const XMP_Error & e) {
    set_error(e);
    return false;
  }
  return true;
}

bool xmp_files_get_file_info(XmpFilePtr xf, XmpStringPtr filePath,
                             XmpOpenFileOptions *options,
                             XmpFileType *file_format,
                             XmpFileFormatOptions *handler_flags)
{
    CHECK_PTR(xf, false);
    RESET_ERROR;

    bool result = false;
    auto txf = reinterpret_cast<SXMPFiles *>(xf);
    try {
        result = txf->GetFileInfo(STRING(filePath), (XMP_OptionBits *)options,
                                  (XMP_FileFormat *)file_format,
                                  (XMP_OptionBits *)handler_flags);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return false;
    }

    return result;
}

bool xmp_files_free(XmpFilePtr xf)
{
    CHECK_PTR(xf, false);
    RESET_ERROR;
    auto txf = reinterpret_cast<SXMPFiles *>(xf);
    try {
        delete txf;
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return false;
    }
    return true;
}

bool xmp_files_get_format_info(XmpFileType format,
                               XmpFileFormatOptions *options)
{
    RESET_ERROR;

    bool result = false;
    try {
        result = SXMPFiles::GetFormatInfo(format, (XMP_OptionBits *)options);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return false;
    }
    return result;
}

XmpFileType xmp_files_check_file_format(const char *filePath)
{
    CHECK_PTR(filePath, XMP_FT_UNKNOWN);
    RESET_ERROR;

    XmpFileType file_type = XMP_FT_UNKNOWN;
    try {
        file_type = (XmpFileType)SXMPFiles::CheckFileFormat(filePath);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return XMP_FT_UNKNOWN;
    }
    return file_type;
}

XmpPtr xmp_new_empty()
{
    RESET_ERROR;
    SXMPMeta *txmp = new SXMPMeta;
    return (XmpPtr)txmp;
}

XmpPtr xmp_new(const char *buffer, size_t len)
{
    CHECK_PTR(buffer, NULL);
    RESET_ERROR;

    try {
        auto txmp = std::unique_ptr<SXMPMeta>(new SXMPMeta(buffer, len));
        return reinterpret_cast<XmpPtr>(txmp.release());
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return NULL;
}

XmpPtr xmp_copy(XmpPtr xmp)
{
    CHECK_PTR(xmp, NULL);
    RESET_ERROR;

    try {
        auto txmp = std::unique_ptr<SXMPMeta>(new SXMPMeta(*(SXMPMeta *)xmp));
        return reinterpret_cast<XmpPtr>(txmp.release());
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return NULL;
}

bool xmp_parse(XmpPtr xmp, const char *buffer, size_t len)
{
    CHECK_PTR(xmp, false);
    CHECK_PTR(buffer, false);

    SXMPMeta *txmp = (SXMPMeta *)xmp;
    try {
        txmp->ParseFromBuffer(buffer, len, kXMP_RequireXMPMeta);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return false;
    }
    return true;
}

bool xmp_serialize(XmpPtr xmp, XmpStringPtr buffer, uint32_t options,
                   uint32_t padding)
{
    RESET_ERROR;
    return xmp_serialize_and_format(xmp, buffer, options, padding, "\n", " ",
                                    0);
}

bool xmp_serialize_and_format(XmpPtr xmp, XmpStringPtr buffer, uint32_t options,
                              uint32_t padding, const char *newline,
                              const char *tab, int32_t indent)
{
    CHECK_PTR(xmp, false);
    CHECK_PTR(buffer, false);
    RESET_ERROR;

    SXMPMeta *txmp = (SXMPMeta *)xmp;
    try {
        txmp->SerializeToBuffer(STRING(buffer), options, padding, newline, tab,
                                indent);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        return false;
    }
    return true;
}

bool xmp_free(XmpPtr xmp)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;
    SXMPMeta *txmp = (SXMPMeta *)xmp;
    delete txmp;
    return true;
}

bool xmp_get_property(XmpPtr xmp, const char *schema, const char *name,
                      XmpStringPtr property, uint32_t *propsBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    try {
        auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
        XMP_OptionBits optionBits;
        ret = txmp->GetProperty(schema, name, STRING(property), &optionBits);
        if (propsBits) {
            *propsBits = optionBits;
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return ret;
}

bool xmp_get_property_date(XmpPtr xmp, const char *schema, const char *name,
                           XmpDateTime *property, uint32_t *propsBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    try {
        auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
        XMP_OptionBits optionBits;
        XMP_DateTime dt;
        //		memset((void*)&dt, 1, sizeof(XMP_DateTime));
        ret = txmp->GetProperty_Date(schema, name, &dt, &optionBits);
        ASSIGN((*property), dt);
        if (propsBits) {
            *propsBits = optionBits;
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return ret;
}

bool xmp_get_property_float(XmpPtr xmp, const char *schema, const char *name,
                            double *property, uint32_t *propsBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    try {
        auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
        XMP_OptionBits optionBits;
        ret = txmp->GetProperty_Float(schema, name, property, &optionBits);
        if (propsBits) {
            *propsBits = optionBits;
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return ret;
}

bool xmp_get_property_bool(XmpPtr xmp, const char *schema, const char *name,
                           bool *property, uint32_t *propsBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    try {
        auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
        XMP_OptionBits optionBits;
        ret = txmp->GetProperty_Bool(schema, name, property, &optionBits);
        if (propsBits) {
            *propsBits = optionBits;
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return ret;
}

bool xmp_get_property_int32(XmpPtr xmp, const char *schema, const char *name,
                            int32_t *property, uint32_t *propsBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    try {
        auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
        XMP_OptionBits optionBits;
        // the long converstion is needed until XMPCore is fixed it use proper
        // types.
        ret = txmp->GetProperty_Int(schema, name, property, &optionBits);
        if (propsBits) {
            *propsBits = optionBits;
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return ret;
}

bool xmp_get_property_int64(XmpPtr xmp, const char *schema, const char *name,
                            int64_t *property, uint32_t *propsBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    try {
        auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
        XMP_OptionBits optionBits;
        ret = txmp->GetProperty_Int64(schema, name, property, &optionBits);
        if (propsBits) {
            *propsBits = optionBits;
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return ret;
}

bool xmp_get_array_item(XmpPtr xmp, const char *schema, const char *name,
                        int32_t index, XmpStringPtr property,
                        uint32_t *propsBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    try {
        auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
        XMP_OptionBits optionBits;
        ret = txmp->GetArrayItem(schema, name, index, STRING(property),
                                 &optionBits);
        if (propsBits) {
            *propsBits = optionBits;
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    return ret;
}

bool xmp_set_property(XmpPtr xmp, const char *schema, const char *name,
                      const char *value, uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    // see bug #16030
    // when it is a struct or an array, get prop return an empty string
    // but it fail if passed an empty string
    if ((optionBits & (XMP_PROP_VALUE_IS_STRUCT | XMP_PROP_VALUE_IS_ARRAY)) &&
        (*value == 0)) {
        value = NULL;
    }
    try {
        txmp->SetProperty(schema, name, value, optionBits);
        ret = true;
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    catch (...) {
    }
    return ret;
}

bool xmp_set_property_date(XmpPtr xmp, const char *schema, const char *name,
                           const XmpDateTime *value, uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        XMP_DateTime dt;
        ASSIGN(dt, (*value));
        txmp->SetProperty_Date(schema, name, dt, optionBits);
        ret = true;
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    catch (...) {
    }
    return ret;
}

bool xmp_set_property_float(XmpPtr xmp, const char *schema, const char *name,
                            double value, uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->SetProperty_Float(schema, name, value, optionBits);
        ret = true;
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    catch (...) {
    }
    return ret;
}

bool xmp_set_property_bool(XmpPtr xmp, const char *schema, const char *name,
                           bool value, uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->SetProperty_Bool(schema, name, value, optionBits);
        ret = true;
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    catch (...) {
    }
    return ret;
}

bool xmp_set_property_int32(XmpPtr xmp, const char *schema, const char *name,
                            int32_t value, uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->SetProperty_Int(schema, name, value, optionBits);
        ret = true;
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    catch (...) {
    }
    return ret;
}

bool xmp_set_property_int64(XmpPtr xmp, const char *schema, const char *name,
                            int64_t value, uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->SetProperty_Int64(schema, name, value, optionBits);
        ret = true;
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    catch (...) {
    }
    return ret;
}

bool xmp_set_array_item(XmpPtr xmp, const char *schema, const char *name,
                        int32_t index, const char *value, uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->SetArrayItem(schema, name, index, value, optionBits);
        ret = true;
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    catch (...) {
    }
    return ret;
}

bool xmp_append_array_item(XmpPtr xmp, const char *schema, const char *name,
                           uint32_t arrayOptions, const char *value,
                           uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->AppendArrayItem(schema, name, arrayOptions, value, optionBits);
        ret = true;
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }
    catch (...) {
    }
    return ret;
}

bool xmp_delete_property(XmpPtr xmp, const char *schema, const char *name)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = true;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->DeleteProperty(schema, name);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        ret = false;
    }
    catch (...) {
        ret = false;
    }
    return ret;
}

bool xmp_has_property(XmpPtr xmp, const char *schema, const char *name)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = true;
    auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
    try {
        ret = txmp->DoesPropertyExist(schema, name);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        ret = false;
    }
    catch (...) {
        ret = false;
    }
    return ret;
}

bool xmp_get_localized_text(XmpPtr xmp, const char *schema, const char *name,
                            const char *genericLang, const char *specificLang,
                            XmpStringPtr actualLang, XmpStringPtr itemValue,
                            uint32_t *propsBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = false;
    try {
        auto txmp = reinterpret_cast<const SXMPMeta *>(xmp);
        XMP_OptionBits optionBits;
        ret = txmp->GetLocalizedText(schema, name, genericLang, specificLang,
                                     STRING(actualLang), STRING(itemValue),
                                     &optionBits);
        if (propsBits) {
            *propsBits = optionBits;
        }
    }
    catch (const XMP_Error &e) {
        set_error(e);
        ret = false;
    }
    return ret;
}

bool xmp_set_localized_text(XmpPtr xmp, const char *schema, const char *name,
                            const char *genericLang, const char *specificLang,
                            const char *value, uint32_t optionBits)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = true;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->SetLocalizedText(schema, name, genericLang, specificLang, value,
                               optionBits);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        ret = false;
    }
    catch (...) {
        ret = false;
    }
    return ret;
}

bool xmp_delete_localized_text(XmpPtr xmp, const char *schema, const char *name,
                               const char *genericLang,
                               const char *specificLang)
{
    CHECK_PTR(xmp, false);
    RESET_ERROR;

    bool ret = true;
    auto txmp = reinterpret_cast<SXMPMeta *>(xmp);
    try {
        txmp->DeleteLocalizedText(schema, name, genericLang, specificLang);
    }
    catch (const XMP_Error &e) {
        set_error(e);
        ret = false;
    }
    catch (...) {
        ret = false;
    }
    return ret;
}

XmpStringPtr xmp_string_new()
{
    return (XmpStringPtr) new std::string;
}

void xmp_string_free(XmpStringPtr s)
{
    auto str = reinterpret_cast<std::string *>(s);
    delete str;
}

const char *xmp_string_cstr(XmpStringPtr s)
{
    CHECK_PTR(s, NULL);
    return reinterpret_cast<const std::string *>(s)->c_str();
}

size_t xmp_string_len(XmpStringPtr s)
{
    CHECK_PTR(s, 0);
    return reinterpret_cast<const std::string *>(s)->size();
}

XmpIteratorPtr xmp_iterator_new(XmpPtr xmp, const char *schema,
                                const char *propName, XmpIterOptions options)
{
    CHECK_PTR(xmp, NULL);
    RESET_ERROR;

    try {
        auto xiter = std::unique_ptr<SXMPIterator>(
            new SXMPIterator(*(SXMPMeta *)xmp, schema, propName, options));

        return reinterpret_cast<XmpIteratorPtr>(xiter.release());
    }
    catch (const XMP_Error &e) {
        set_error(e);
    }

    return NULL;
}

bool xmp_iterator_free(XmpIteratorPtr iter)
{
    CHECK_PTR(iter, false);
    RESET_ERROR;
    auto titer = reinterpret_cast<SXMPIterator *>(iter);
    delete titer;
    return true;
}

bool xmp_iterator_next(XmpIteratorPtr iter, XmpStringPtr schema,
                       XmpStringPtr propName, XmpStringPtr propValue,
                       uint32_t *options)
{
    CHECK_PTR(iter, false);
    RESET_ERROR;
    auto titer = reinterpret_cast<SXMPIterator *>(iter);
    return titer->Next(reinterpret_cast<std::string *>(schema),
                       reinterpret_cast<std::string *>(propName),
                       reinterpret_cast<std::string *>(propValue), options);
}

bool xmp_iterator_skip(XmpIteratorPtr iter, XmpIterSkipOptions options)
{
    CHECK_PTR(iter, false);
    RESET_ERROR;
    auto titer = reinterpret_cast<SXMPIterator *>(iter);
    titer->Skip(options);
    return true;
}

int xmp_datetime_compare(XmpDateTime *left, XmpDateTime *right)
{
    if (!left && !right) {
        return 0;
    }
    if (!left) {
        return -1;
    }
    if (!right) {
        return 1;
    }
    XMP_DateTime _left;
    ASSIGN(_left, *left);
    XMP_DateTime _right;
    ASSIGN(_right, *right);
    return XMPUtils::CompareDateTime(_left, _right);
}

#ifdef __cplusplus
}
#endif