summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/libuemf/uemf_utf.c
blob: 5c6507818f0bf7e501d74d77a07c8106b8a82ee9 (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
/**
  @file uemf_utf.c
  
  @brief Functions for manipulating UTF and various types of text.

  
  Compile with "U_VALGRIND" defined defined to enable code which lets valgrind check each record for
  uninitialized data.
  
  Compile with "SOL8" defined for Solaris 8 or 9 (Sparc).
*/

/*
File:      uemf_utf.c
Version:   0.0.5
Date:      29-JAN-2014
Author:    David Mathog, Biology Division, Caltech
email:     mathog@caltech.edu
Copyright: 2014 David Mathog and California Institute of Technology (Caltech)
*/

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iconv.h>
#include <wchar.h>
#include <errno.h>
#include <string.h>
#include <limits.h> // for INT_MAX, INT_MIN
#include <math.h>   // for U_ROUND()
#include "uemf_utf.h"

//! \cond
/* Prototypes for functions used here and defined in uemf_endian.c, but which are not supposed
to be used in end user code. */

void U_swap2(void *ul, unsigned int count);
//! \endcond

/* ******************************************************************************************** */
      
/** \cond */
/* iconv() has a funny cast on some older systems, on most recent ones
   it is just char **.  This tries to work around the issue.  If you build this
   on another funky system this code may need to be modified, or define ICONV_CAST
   on the compile line(but it may be tricky).
*/
#if _LIBICONV_VERSION == 0x0109
# define ICONV_CAST (const char **)
#endif  // _LIBICONV_VERSION 0x0109
#ifdef SOL8
# define ICONV_CAST (const char **)
#endif  //SOL8
#if !defined(ICONV_CAST)
# define ICONV_CAST (char **)
#endif  //ICONV_CAST
/** \endcond */

/* **********************************************************************************************
These functions are used for development and debugging and should be be includied in production code.
*********************************************************************************************** */

/**
    \brief Dump a UTF8  string.  Not for use in production code.
    \param src string to examine
*/
void wchar8show(
      const char *src
   ){
   if(!src){
      printf("char show <NULL>\n");
   }
   else {
      printf("char show\n");
      size_t srclen = 0;
      while(*src){ printf("%d %d %x\n",(int) srclen,*src,*src); srclen++; src++; }
   }
}

/**
    \brief Dump a UTF16  string.  Not for use in production code.
    \param src string to examine
*/
void wchar16show(
      const uint16_t *src
   ){
   if(!src){
      printf("uint16_t show <NULL>\n");
   }
   else {
      printf("uint16_t show\n");
      size_t srclen = 0;
      while(*src){ printf("%d %d %x\n",(int) srclen,*src,*src); srclen++; src++; }
   }
}

/** 
    \brief Dump a UTF32 string.  Not for use in production code.
*/
void wchar32show(
      const uint32_t *src
   ){
   if(!src){
      printf("uint32_t show <NULL>\n");
   }
   else {
      printf("uint32_t show\n");
      size_t srclen = 0;
      while(*src){ printf("%d %d %x\n",(int) srclen,*src,*src); srclen++; src++; }
   }
}

/**
    \brief Dump a wchar_t string.  Not for use in production code.
    \param src string to examine
*/
void wchartshow(
      const wchar_t *src
   ){
   uint32_t val;
   if(!src){
      printf("wchar_t show <NULL>\n");
   }
   else {
      printf("wchar_t show\n");
      size_t srclen = 0;
      if(!src)return;
      while(*src){
         val = *src;  // because *src is wchar_t is not strictly an integer type, can cause warnings on next line
         printf("%d %d %x\n",(int) srclen,val,val); 
         srclen++; 
         src++;
      }
   }
}

/* **********************************************************************************************
These functions are used for character type conversions, Image conversions, and other
utility operations
*********************************************************************************************** */

/**
    \brief Find the number of (storage) characters in a 16 bit character string, not including terminator.
    \param src string to examine
*/
size_t wchar16len(
      const uint16_t *src
   ){
   size_t srclen = 0;
   if(src){
      while(*src){ srclen++; src++; }
   }
   return(srclen);
}

/**
    \brief Find the number of (storage) characters in a 32 bit character  string, not including terminator.
    \param src string to examine
*/
size_t wchar32len(
      const uint32_t *src
   ){
   size_t srclen = 0;
   if(src){
      while(*src){ srclen++; src++; }
   }
   return(srclen);
}

/**
    \brief Strncpy for wchar16 (UTF16).
    \param dst destination (already allocated)  
    \param src source                           
    \param nchars number of characters to copy  
*/
void   wchar16strncpy(
      uint16_t       *dst,
      const uint16_t *src,
      size_t          nchars
   ){
   if(src){
      for(;nchars;nchars--,dst++,src++){
        *dst = *src;
        if(!*src)break;
      }
   }
}

/**
    \brief Fill the output string with N characters, if the input string is shorter than N, pad with nulls.
    \param dst destination (already allocated) 
    \param src source
    \param nchars number of characters to copy 
    
*/
void   wchar16strncpypad(
      uint16_t       *dst,
      const uint16_t *src,
      size_t          nchars
   ){
   if(src){
      for(;*src && nchars;nchars--,dst++,src++){ *dst = *src; }
      for(;nchars;nchars--,dst++){               *dst = 0;    }  // Pad the remainder
   }
}

/*  For the following converstion functions, remember that iconv() modifies ALL of its parameters,
    so save a pointer to the destination buffer!!!!
    It isn't clear that terminators are being
    copied properly, so be sure allocated space is a bit larger and cleared.
*/

/** 
    \brief Convert a UTF32LE string to a UTF16LE string.
    \returns pointer to new string or NULL if it fails
    \param src wchar_t string to convert
    \param max number of characters to convert, if 0, until terminator
    \param len number of characters in new string, NOT including terminator
*/
uint16_t *U_Utf32leToUtf16le(
      const uint32_t *src,
      size_t          max,
      size_t         *len
   ){
   char *dst,*dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;

   if(!src)return(NULL);
   if(max){ srclen = 4*max; }
   else {   srclen = 4 + 4*wchar32len(src); } //include terminator, length in BYTES
   
   dstlen = 2 + srclen;                     // this will always work, but may waste space
   dst2  = dst = calloc(dstlen,1);          // so there will be at least one terminator
   if(dst){
      iconv_t conv = iconv_open("UTF-16LE", "UTF-32LE");
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=wchar16len((uint16_t *)dst2);
         }
      }
   }
   return((uint16_t *)dst2);
}

/**
    \brief  Convert a UTF16LE string to a UTF32LE string.
    \return pointer to new string or NULL if it fails
    \param src UTF16LE string to convert
    \param max number of characters to convert, if 0, until terminator
    \param len number of characters in new string, NOT including terminator
*/
uint32_t *U_Utf16leToUtf32le(
      const uint16_t *src,
      size_t          max,
      size_t         *len
   ){
   char *dst,*dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;

   if(!src)return(NULL);
   if(max){ srclen = 2*max; }
   else {   srclen = 2*wchar16len(src)+2; } // include terminator, length in BYTES
   dstlen = 2*(2 + srclen);                 // This should always work
   dst2 = dst = calloc(dstlen,1);
   if(dst){
      iconv_t conv = iconv_open("UTF-32LE",   "UTF-16LE");
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=wchar32len((uint32_t *)dst2);
         }
      }
   }
   return((uint32_t *) dst2);
}

/**
    \brief  Convert a Latin1 string to a UTF32LE string.
    \return pointer to new string or NULL if it fails
    \param src Latin1 string to convert
    \param max number of characters to convert, if 0, until terminator
    \param len number of characters in new string, NOT including terminator
    
    
    U_EMR_EXTTEXTOUTA records are "8 bit ASCII".  In theory that is ASCII in an 8
    bit character, but numerous applications store Latin1 in them, and some
    _may_ store UTF-8 in them.  Since very vew Latin1 strings are valid UTF-8 strings,
    call U_Utf8ToUtf32le first, and if it fails, then call this function.
*/
uint32_t *U_Latin1ToUtf32le(
      const char *src,
      size_t      max,
      size_t     *len
   ){
   char *dst,*dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;

   if(!src)return(NULL);
   if(max){ srclen = max; }
   else {   srclen = strlen(src)+1; }       // include terminator, length in BYTES
   dstlen = sizeof(uint32_t)*(1 + srclen);  // This should always work but might waste some space
   dst2 = dst = calloc(dstlen,1);
   if(dst){
      iconv_t conv = iconv_open("UTF-32LE",   "LATIN1");
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=wchar32len((uint32_t *)dst2);
         }
      }
   }
   return((uint32_t *) dst2);
}

/**
    \brief  Convert a UTF8 string to a UTF32LE string.
    \return pointer to new string or NULL if it fails
    \param src UTF8 string to convert
    \param max number of characters to convert, if 0, until terminator
    \param len number of characters in new string, NOT including terminator
*/
uint32_t *U_Utf8ToUtf32le(
      const char *src,
      size_t      max,
      size_t     *len
   ){
   char *dst,*dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;

   if(!src)return(NULL);
   if(max){ srclen = max; }
   else {   srclen = strlen(src)+1; }       // include terminator, length in BYTES
   dstlen = sizeof(uint32_t)*(1 + srclen);  // This should always work but might waste some space
   dst2 = dst = calloc(dstlen,1);
   if(dst){
      iconv_t conv = iconv_open("UTF-32LE",   "UTF-8");
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=wchar32len((uint32_t *)dst2);
         }
      }
   }
   return((uint32_t *) dst2);
}

/**
    \brief  Convert a UTF32LE string to a UTF8 string.
    \return pointer to new string or NULL if it fails
    \param src wchar_t string to convert                                       
    \param max number of characters to convert, if 0, until terminator         
    \param len number of characters in new string, NOT including terminator    
*/
char *U_Utf32leToUtf8(
      const uint32_t *src,
      size_t          max,
      size_t         *len
   ){
   char *dst,*dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;

   if(!src)return(NULL);
   if(max){ srclen = 4*max; }
   else {   srclen = 4*(1 + wchar32len(src)); } //include terminator, length in BYTES
   dstlen = 1 + srclen;                         // This should always work but might waste some space
   dst2 = dst = calloc(dstlen,1);
   if(dst){
      iconv_t conv = iconv_open("UTF-8",   "UTF-32LE");
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=strlen(dst2);
         }
      }
   }
   return(dst2);
}

/**
   \brief Convert a UTF-8 string to a UTF16-LE string.
   \return pointer to new string or NULL if it fails
   \param src UTF8 string to convert
   \param max number of characters to convert, if 0, until terminator
   \param len number of characters in new string, NOT including terminator
*/
uint16_t *U_Utf8ToUtf16le(
      const char   *src,
      size_t        max,
      size_t       *len
   ){
   char *dst,*dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;

   if(!src)return(NULL);
   if(max){ srclen = max; }
   else {   srclen = strlen(src)+1; }       // include terminator, length in BYTES
   dstlen = 2 * (1 + srclen);               // this will always work, but may waste space
   dst2 = dst =calloc(dstlen,1);            // so there will always be a terminator
   if(dst){
      iconv_t conv = iconv_open("UTF-16LE", "UTF-8");
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=wchar16len((uint16_t *)dst2);
         }
      }
   }
   return((uint16_t *)dst2);
}

/**
    \brief Convert a UTF16LE string to a UTF8 string.
    \return pointer to new UTF8 string or NULL if it fails
    \param src UTF16LE string to convert
    \param max number of characters to convert, if 0, until terminator
    \param len number of characters in new string, NOT including terminator
*/
char *U_Utf16leToUtf8(
      const uint16_t *src,
      size_t          max,
      size_t         *len
   ){
   char *dst, *dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;

   if(!src)return(NULL);
   if(max){ srclen = 2*max; }
   else {   srclen = 2*(1 +wchar16len(src)); } //include terminator, length in BYTES
   dstlen = 1 + 2*srclen;                      // this will always work, but may waste space
                                               // worst case is all glyphs (==max) need 4 UTF-8 encoded bytes + terminator.
   dst2 = dst = (char *) calloc(dstlen,1);
   if(dst){
      iconv_t conv = iconv_open("UTF-8", "UTF-16LE");
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=strlen(dst2);
            dst = dst2;
            dst2 = U_strdup(dst); // make a string of exactly the right size
            free(dst);            // free the one which was probably too big
         }
      }
   }
   return(dst2);
}

/**
    \brief Convert a UTF16LE string to a LATIN1 string.
    \return pointer to new UTF8 string or NULL if it fails
    \param src UTF16LE string to convert
    \param max number of characters to convert, if 0, until terminator
    \param len number of characters in new string, NOT including terminator
*/
char *U_Utf16leToLatin1(
      const uint16_t *src,
      size_t          max,
      size_t         *len
   ){
   char *dst, *dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;

   if(!src)return(NULL);
   if(max){ srclen = 2*max; }
   else {   srclen = 2*(1 +wchar16len(src)); } //include terminator, length in BYTES
   dstlen = 1 + srclen;                        // this will always work as latin1 is always 1 byte/character
   dst2 = dst = (char *) calloc(dstlen,1);
   if(dst){
      iconv_t conv = iconv_open("LATIN1//TRANSLIT",   "UTF-16LE");
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=strlen(dst2);
            dst = dst2;
            dst2 = U_strdup(dst); // make a string of exactly the right size
            free(dst);            // free the one which was probably too big
         }
      }
   }
   return(dst2);
}
/**
    \brief Put a single 16 bit character into UTF-16LE form.
    
    Used in conjunction with U_Utf16leEdit(), because the character
    representation would otherwise be dependent on machine Endianness.
  
    \return UTF16LE representation of the character.
    \param src 16 bit character
   
*/
uint16_t U_Utf16le(const uint16_t src){
    uint16_t dst=src;
#if U_BYTE_SWAP
    U_swap2(&dst,1);
#endif
    return(dst);
}

/**
    \brief  Convert a UTF8 string to a Latin1 string.
    \return pointer to new string or NULL if it fails
    \param src Latin1 string to convert
    \param max number of characters to convert, if 0, until terminator
    \param len number of characters in new string, NOT including terminator
    
    
    WMF uses latin1, others UTF-8, only some utf-8 can be converted to latin1.
    
*/
char *U_Utf8ToLatin1(
      const char *src,
      size_t      max,
      size_t     *len
   ){
   char *dst,*dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;
   if(max){ srclen = max; }
   else {   srclen = strlen(src)+1; }       // include terminator, length in BYTES
   dstlen = (1 + srclen);                   // This should always work but might waste some space
   dst2 = dst = calloc(dstlen,1);
   if(dst){
      iconv_t conv = iconv_open("LATIN1//TRANSLIT",   "UTF-8"); // translate what can be, fill in with something close for the rest
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=strlen(dst2);
         }
      }
   }
   return((char *) dst2);
}

/**
    \brief  Convert a Latin1 string to a UTF8 string.
    \return pointer to new string or NULL if it fails
    \param src Latin1 string to convert
    \param max number of characters to convert, if 0, until terminator
    \param len number of characters in new string, NOT including terminator
    
    
    WMF uses latin1, others UTF-8, all Latin1 should be able to convert to utf-8.
    
*/
char *U_Latin1ToUtf8(
      const char *src,
      size_t      max,
      size_t     *len
   ){
   char *dst,*dst2;
   char *src2 = (char *) src;
   size_t srclen,dstlen,status;
   if(max){ srclen = max; }
   else {   srclen = strlen(src)+1; }       // include terminator, will waste some space
   dstlen = (1 + 2*srclen);                 // This should always work because all latin1 convert to 1 or 2 byte UTF8, it might waste some space
   dst2 = dst = calloc(dstlen,1);
   if(dst){
      iconv_t conv = iconv_open("UTF-8", "LATIN1"); // everything should translate
      if ( conv == (iconv_t) -1){
         free(dst2);
         dst2=NULL;
      }
      else {
         status = iconv(conv, ICONV_CAST &src2, &srclen, &dst, &dstlen);
         iconv_close(conv);
         if(status == (size_t) -1){
            free(dst2);
            dst2 = NULL;
         }
         else if(len){
            *len=strlen(dst2);
         }
      }
   }
   return((char *) dst2);
}

/**
    \brief Single character replacement in a UTF-16LE string.
    
    Used solely for the Description field which contains
    embedded nulls, which makes it difficult to manipulate.  Use some other character and then swap it.
  
    \return number of substitutions, or -1 if src is not defined 
    \param src UTF16LE string to edit
    \param find character to replace
    \param replace replacestitute character
   
*/
int U_Utf16leEdit(
      uint16_t *src,
      uint16_t  find,
      uint16_t  replace
   ){
   int count=0;
   if(!src)return(-1);
   while(*src){ 
     if(*src == find){ *src = replace; count++; } 
     src++;
   }
   return(count);
}

/**
    \brief strdup for when strict C99 compliance is enforced
    \returns duplicate string or NULL on error
    \param s string to duplicate
*/
char *U_strdup(const char *s){
   char   *news=NULL;
   size_t  slen;
   if(s){
      slen = strlen(s) + 1; //include the terminator!
      news = malloc(slen);
      if(news){
         memcpy(news,s,slen);
      }
   }
   return(news);
   
}


#ifdef __cplusplus
}
#endif