[go: up one dir, main page]

File: meta.c

package info (click to toggle)
libmobi 0.11%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,916 kB
  • sloc: ansic: 19,055; sh: 4,650; makefile: 102
file content (864 lines) | stat: -rw-r--r-- 24,459 bytes parent folder | download | duplicates (2)
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
/** @file meta.c
 *  @brief Functions for metadata manipulation
 *
 * Copyright (c) 2016 Bartek Fabiszewski
 * http://www.fabiszewski.net
 *
 * This file is part of libmobi.
 * Licensed under LGPL, either version 3, or any later.
 * See <http://www.gnu.org/licenses/>
 */

#define _GNU_SOURCE 1
#ifndef __USE_BSD
#define __USE_BSD /* for strdup on linux/glibc */
#endif

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "meta.h"
#include "util.h"

/**
 @brief Get document metadata from exth string
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @param[in] exth_tag MOBIExthTag
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_exthstring(const MOBIData *m, const MOBIExthTag exth_tag) {
    char *string = NULL;
    
    MOBIExthHeader *exth;
    MOBIExthHeader *start = NULL;
    while ((exth = mobi_next_exthrecord_by_tag(m, exth_tag, &start))) {
        char *exth_string = mobi_decode_exthstring(m, exth->data, exth->size);
        if (string == NULL) {
            string = exth_string;
        } else if (exth_string) {
            const char *separator = "; ";
            size_t new_length = strlen(string) + strlen(exth_string) + strlen(separator) + 1;
            char *new = malloc(new_length);
            if (new == NULL) {
                free(string);
                free(exth_string);
                return NULL;
            }
            strcpy(new, string);
            strcat(new, separator);
            strcat(new, exth_string);
            free(string);
            free(exth_string);
            string = new;
        }
        if (start == NULL) {
            break;
        }
    }
    return string;
}

/**
 @brief Get document title metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_title(const MOBIData *m) {
    if (m == NULL) {
        return NULL;
    }
    char *title = mobi_meta_get_exthstring(m, EXTH_UPDATEDTITLE);
    if (title) {
        return title;
    }
    char fullname[MOBI_TITLE_SIZEMAX + 1];
    MOBI_RET ret = mobi_get_fullname(m, fullname, MOBI_TITLE_SIZEMAX);
    if (ret == MOBI_SUCCESS) {
        title = strdup(fullname);
    } else if (m->ph) {
        title = strdup(m->ph->name);
    }
    return title;
}

/**
 @brief Add document title metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] title String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_title(MOBIData *m, const char *title) {
    if (title == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(title), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_UPDATEDTITLE, (uint32_t) size, title);
}

/**
 @brief Delete all title metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_title(MOBIData *m) {
    if (mobi_exists_mobiheader(m) && m->mh->full_name) {
        m->mh->full_name[0] = '\0';
    }
    if (mobi_is_hybrid(m) && mobi_exists_mobiheader(m->next) && m->next->mh->full_name) {
        m->next->mh->full_name[0] = '\0';
    }
    return mobi_delete_exthrecord_by_tag(m, EXTH_UPDATEDTITLE);
}

/**
 @brief Set document title metadata
 
 Replaces all title metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] title String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_title(MOBIData *m, const char *title) {
    if (title == NULL) {
        return MOBI_PARAM_ERR;
    }
    /* set title in mobi header */
    MOBI_RET ret = mobi_set_fullname(m, title);
    if (ret != MOBI_SUCCESS) {
        return ret;
    }
    /* set title in palm header */
    ret = mobi_set_pdbname(m, title);
    if (ret != MOBI_SUCCESS) {
        return ret;
    }
    /* set title in exth header */
    ret = mobi_delete_exthrecord_by_tag(m, EXTH_UPDATEDTITLE);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_title(m, title);
    }
    return ret;
}

/**
 @brief Get document author metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_author(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_AUTHOR);
}

/**
 @brief Add document author metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] author String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_author(MOBIData *m, const char *author) {
    if (author == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(author), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_AUTHOR, (uint32_t) size, author);
}

/**
 @brief Delete all author metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_author(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_AUTHOR);
}

/**
 @brief Set document author metadata
 
 Replaces all author metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] author String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_author(MOBIData *m, const char *author) {
    if (author == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_author(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_author(m, author);
    }
    return ret;
}

/**
 @brief Get document subject metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_subject(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_SUBJECT);
}

/**
 @brief Add document subject metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] subject String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_subject(MOBIData *m, const char *subject) {
    if (subject == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(subject), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_SUBJECT, (uint32_t) size, subject);
}

/**
 @brief Delete all subject metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_subject(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_SUBJECT);
}

/**
 @brief Set document subject metadata
 
 Replaces all subject metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] subject String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_subject(MOBIData *m, const char *subject) {
    if (subject == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_subject(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_subject(m, subject);
    }
    return ret;
}

/**
 @brief Get document publisher metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_publisher(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_PUBLISHER);
}

/**
 @brief Add document publisher metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] publisher String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_publisher(MOBIData *m, const char *publisher) {
    if (publisher == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(publisher), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_PUBLISHER, (uint32_t) size, publisher);
}

/**
 @brief Delete all publisher metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_publisher(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_PUBLISHER);
}

/**
 @brief Set document publisher metadata
 
 Replaces all publisher metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] publisher String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_publisher(MOBIData *m, const char *publisher) {
    if (publisher == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_publisher(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_publisher(m, publisher);
    }
    return ret;
}

/**
 @brief Get document publishing date metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_publishdate(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_PUBLISHINGDATE);
}

/**
 @brief Add document publishdate metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] publishdate String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_publishdate(MOBIData *m, const char *publishdate) {
    if (publishdate == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(publishdate), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_PUBLISHINGDATE, (uint32_t) size, publishdate);
}

/**
 @brief Delete all publishdate metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_publishdate(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_PUBLISHINGDATE);
}

/**
 @brief Set document publishdate metadata
 
 Replaces all publishdate metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] publishdate String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_publishdate(MOBIData *m, const char *publishdate) {
    if (publishdate == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_publishdate(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_publishdate(m, publishdate);
    }
    return ret;
}

/**
 @brief Get document description metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_description(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_DESCRIPTION);
}

/**
 @brief Add document description metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] description String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_description(MOBIData *m, const char *description) {
    if (description == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(description), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_DESCRIPTION, (uint32_t) size, description);
}

/**
 @brief Delete all description metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_description(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_DESCRIPTION);
}

/**
 @brief Set document description metadata
 
 Replaces all description metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] description String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_description(MOBIData *m, const char *description) {
    if (description == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_description(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_description(m, description);
    }
    return ret;
}

/**
 @brief Get document imprint metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_imprint(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_IMPRINT);
}

/**
 @brief Add document imprint metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] imprint String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_imprint(MOBIData *m, const char *imprint) {
    if (imprint == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(imprint), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_IMPRINT, (uint32_t) size, imprint);
}

/**
 @brief Delete all imprint metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_imprint(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_IMPRINT);
}

/**
 @brief Set document imprint metadata
 
 Replaces all imprint metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] imprint String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_imprint(MOBIData *m, const char *imprint) {
    if (imprint == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_imprint(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_imprint(m, imprint);
    }
    return ret;
}

/**
 @brief Get document contributor metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_contributor(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_CONTRIBUTOR);
}

/**
 @brief Add document contributor metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] contributor String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_contributor(MOBIData *m, const char *contributor) {
    if (contributor == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(contributor), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_CONTRIBUTOR, (uint32_t) size, contributor);
}

/**
 @brief Delete all contributor metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_contributor(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_CONTRIBUTOR);
}

/**
 @brief Set document contributor metadata
 
 Replaces all contributor metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] contributor String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_contributor(MOBIData *m, const char *contributor) {
    if (contributor == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_contributor(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_contributor(m, contributor);
    }
    return ret;
}

/**
 @brief Get document review metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_review(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_REVIEW);
}

/**
 @brief Add document review metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] review String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_review(MOBIData *m, const char *review) {
    if (review == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(review), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_REVIEW, (uint32_t) size, review);
}

/**
 @brief Delete all review metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_review(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_REVIEW);
}

/**
 @brief Set document review metadata
 
 Replaces all review metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] review String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_review(MOBIData *m, const char *review) {
    if (review == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_review(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_review(m, review);
    }
    return ret;
}

/**
 @brief Get document copyright metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_copyright(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_RIGHTS);
}

/**
 @brief Add document copyright metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] copyright String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_copyright(MOBIData *m, const char *copyright) {
    if (copyright == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(copyright), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_RIGHTS, (uint32_t) size, copyright);
}

/**
 @brief Delete all copyright metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_copyright(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_RIGHTS);
}

/**
 @brief Set document copyright metadata
 
 Replaces all copyright metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] copyright String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_copyright(MOBIData *m, const char *copyright) {
    if (copyright == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_copyright(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_copyright(m, copyright);
    }
    return ret;
}

/**
 @brief Get document ISBN metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_isbn(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_ISBN);
}

/**
 @brief Add document isbn metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] isbn String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_isbn(MOBIData *m, const char *isbn) {
    if (isbn == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(isbn), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_ISBN, (uint32_t) size, isbn);
}

/**
 @brief Delete all isbn metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_isbn(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_ISBN);
}

/**
 @brief Set document isbn metadata
 
 Replaces all isbn metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] isbn String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_isbn(MOBIData *m, const char *isbn) {
    if (isbn == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_isbn(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_isbn(m, isbn);
    }
    return ret;
}

/**
 @brief Get document ASIN metadata
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_asin(const MOBIData *m) {
    return mobi_meta_get_exthstring(m, EXTH_ASIN);
}

/**
 @brief Add document asin metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] asin String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_asin(MOBIData *m, const char *asin) {
    if (asin == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(asin), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_ASIN, (uint32_t) size, asin);
}

/**
 @brief Delete all asin metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_asin(MOBIData *m) {
    return mobi_delete_exthrecord_by_tag(m, EXTH_ASIN);
}

/**
 @brief Set document asin metadata
 
 Replaces all asin metadata with new string
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] asin String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_asin(MOBIData *m, const char *asin) {
    if (asin == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_asin(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_asin(m, asin);
    }
    return ret;
}

/**
 @brief Get document language code metadata
 
 Locale strings are based on IANA language-subtag registry with some custom Mobipocket modifications.
 See mobi_locale array.
 
 Returned string must be deallocated by caller
 
 @param[in] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
char * mobi_meta_get_language(const MOBIData *m) {
    if (m == NULL) {
        return NULL;
    }
    char *lang = mobi_meta_get_exthstring(m, EXTH_LANGUAGE);
    if(lang == NULL && m->mh && m->mh->locale && *m->mh->locale) {
        const char *locale_string = mobi_get_locale_string(*m->mh->locale);
        if (locale_string) {
            lang = strdup(locale_string);
        }
    }
    return lang;
}

/**
 @brief Add document language code metadata
 
 Locale strings are based on IANA language-subtag registry with some custom Mobipocket modifications.
 See mobi_locale array.
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] language String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_add_language(MOBIData *m, const char *language) {
    if (language == NULL) {
        return MOBI_PARAM_ERR;
    }
    size_t size = min(strlen(language), UINT32_MAX);
    return mobi_add_exthrecord(m, EXTH_LANGUAGE, (uint32_t) size, language);
}

/**
 @brief Delete all language code metadata
 
 @param[in,out] m MOBIData structure with loaded data
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_delete_language(MOBIData *m) {
    if(mobi_exists_mobiheader(m) && m->mh->locale) {
        *m->mh->locale = 0;
    }
    if(mobi_is_hybrid(m) && mobi_exists_mobiheader(m->next) && m->next->mh->locale) {
        *m->next->mh->locale = 0;
    }
    return mobi_delete_exthrecord_by_tag(m, EXTH_LANGUAGE);
}

/**
 @brief Set document language code metadata
 
 Replaces all language metadata with new string
 Locale strings are based on IANA language-subtag registry with some custom Mobipocket modifications.
 See mobi_locale array.
 
 @param[in,out] m MOBIData structure with loaded data
 @param[in] language String value
 @return Pointer to null terminated string, NULL on failure
 */
MOBI_RET mobi_meta_set_language(MOBIData *m, const char *language) {
    if (language == NULL) {
        return MOBI_PARAM_ERR;
    }
    MOBI_RET ret = mobi_meta_delete_language(m);
    if (ret == MOBI_SUCCESS) {
        ret = mobi_meta_add_language(m, language);
    }
    if(mobi_exists_mobiheader(m) && m->mh->locale) {
        *m->mh->locale = (uint32_t) mobi_get_locale_number(language);
    }
    if(mobi_is_hybrid(m) && mobi_exists_mobiheader(m->next) && m->next->mh->locale) {
        *m->next->mh->locale = (uint32_t) mobi_get_locale_number(language);
    }
    return ret;
}