-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuemf_endian.c
More file actions
2270 lines (2058 loc) · 91.6 KB
/
uemf_endian.c
File metadata and controls
2270 lines (2058 loc) · 91.6 KB
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
/**
@file uemf_endian.c
@brief Functions for converting EMF records between Big Endian and Little Endian byte orders.
EMF files use Little Endian order.
On a Big Endian machine the data must be converted to/from Little Endian when it is writen to/read from a file.
On a Little Endian machine no conversion is required, but it is good to be able to test the routines on either platform.
When "torev" is true these routines convert from the native byte order to the reverse.
When "torev" is false these routines convert from the reverse byte order to the native.
Routines that do not use that variable swap byte order, and the way in which they do so does not depend
on the native byte order.
The only function here which should be called directly is U_emf_endian(), and then,except for testing purposes, only on a BE machine.
Many variables are initialized to zero even though they will always be set because
some versions of gcc give spurious "may be used uninitialized" warnings otherwise.
*/
/*
File: uemf_endian.c
Version: 0.0.21
Date: 23-APR-2015
Author: David Mathog, Biology Division, Caltech
email: mathog@caltech.edu
Copyright: 2015 David Mathog and California Institute of Technology (Caltech)
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "uemf.h"
#include "uemf_endian.h"
// hide almost everuything in here from Doxygen
//! \cond
/* **********************************************************************************************
These functions convert standard objects used in the EMR records.
*********************************************************************************************** */
void U_swap2(void *ul, unsigned int count){
uint8_t ctmp;
uint8_t *cl = (uint8_t *) ul;
for(; count; count--,cl+=2){
ctmp = *cl;
*cl = *(cl+1);
*(cl+1) = ctmp;
}
}
/* Note: U_swap4 is also used by uwmf_endian.c, in cases where the 32 bit data is not aligned on a 4 byte boundary */
void U_swap4(void *ul, unsigned int count){
uint8_t ctmp;
uint8_t *cl = (uint8_t *) ul;
for(; count; count--,cl+=4){
ctmp = *(cl+0);
*(cl+0) = *(cl+3);
*(cl+3) = ctmp;
ctmp = *(cl+1);
*(cl+1) = *(cl+2);
*(cl+2) = ctmp;
}
}
/**
U_COLORREF and U_RGBQUAD do NOT need to be swapped, they are always stored in memory in the proper order.
*/
/**
\brief Convert rect and rectl objects from Upper Left and Lower Right corner points.
\param rect U_RECTL object
\param count number to convert
*/
void rectl_swap(
PU_RECTL rect,
unsigned int count
){
U_swap4(rect,4*count);
}
/**
\brief Convert a U_SIZEL object.
\param sz U_SizeL object
\param count number to convert
*/
void sizel_swap(
PU_SIZEL sz,
unsigned int count
){
U_swap4(sz,2*count);
}
/**
\brief Convert a U_POINTL object
\param pt U_POINTL object
\param count number to convert
*/
void pointl_swap(
PU_POINTL pt,
unsigned int count
){
U_swap4(pt,2*count);
}
/**
\brief Convert a U_POINT16 object
\param pt U_POINT16 object
\param count number to convert
*/
void point16_swap(
PU_POINT16 pt,
unsigned int count
){
U_swap2(pt,2*count);
}
/**
\brief Convert a U_TRIVERTEX object.
\param tv U_TRIVERTEX object.
\param count number to convert
*/
void trivertex_swap(
PU_TRIVERTEX tv,
unsigned int count
){
for(;count; count--, tv++){
U_swap4(tv,2); /* x,y */
U_swap2(&(tv->Red),4); /* Red, Green, Blue, Alpha */
}
}
/**
\brief Convert a U_GRADIENT3 object.
\param tv U_GRADIENT3 object.
\param count number to convert
*/
void gradient3_swap(
PU_GRADIENT3 g3,
unsigned int count
){
U_swap4(g3,3*count);
}
/**
\brief Convert a U_GRADIENT4 object.
\param tv U_GRADIENT4 object.
\param count number to convert
*/
void gradient4_swap(
PU_GRADIENT4 g4,
unsigned int count
){
U_swap4(g4,2*count); //a gradient4 object has 2 int4's, NOT 4!
}
/**
\brief Convert a U_LOGBRUSH object.
\param lb U_LOGBRUSH object.
*/
void logbrush_swap(
PU_LOGBRUSH lb
){
U_swap4(&(lb->lbStyle),1); // lbStyle
// ordered bytes: lbColor
U_swap4(&(lb->lbHatch),1); // lbHatch
}
/**
\brief Convert a U_XFORM object.
\param xform U_XFORM object
*/
void xform_swap(
PU_XFORM xform
){
U_swap4(xform,6);
}
/**
\brief Convert a U_CIEXYZTRIPLE object
\param cie3 U_CIEXYZTRIPLE object
*/
void ciexyztriple_swap(
PU_CIEXYZTRIPLE cie3
){
U_swap4(cie3,9);
}
/**
\brief Convert a U_LOGCOLORSPACEA object.
\param lcsa U_LOGCOLORSPACEA object
*/
void logcolorspacea_swap(
PU_LOGCOLORSPACEA lcsa
){
U_swap4(lcsa,5); // lcsSignature lcsVersion lcsSize lcsCSType lcsIntent
ciexyztriple_swap(&(lcsa->lcsEndpoints));
// ordered bytes: lcsGammaRGB
// ordered bytes: lcsFilename
}
/**
\brief Convert a U_LOGCOLORSPACEW object.
\param lcsa U_LOGCOLORSPACEW object
*/
void logcolorspacew_swap(
PU_LOGCOLORSPACEW lcsa
){
U_swap4(lcsa,5); // lcsSignature lcsVersion lcsSize lcsCSType lcsIntent
ciexyztriple_swap(&(lcsa->lcsEndpoints));
// ordered bytes: lcsGammaRGB
// UTF-16LE, already in order: lcsFilename
}
/**
\brief Convert a U_LOGFONT object.
\param lf U_LOGFONT object
*/
void logfont_swap(
PU_LOGFONT lf
){
U_swap4(lf,5); // lfHeight lfWidth lfEscapement lfOrientation lfWeight
// ordered bytes: lfItalic lfUnderline lfStrikeOut lfCharSet lfOutPrecision lfClipPrecision lfQuality lfPitchAndFamily
// UTF16-LE, already in order
}
/**
\brief Convert a U_LOGFONT_PANOSE object.
\return U_LOGFONT_PANOSE object
*/
void logfont_panose_swap(
PU_LOGFONT_PANOSE lfp
){
logfont_swap(&(lfp->elfLogFont)); // elfLogFont
// UTF-16LE, already in order: elfFullName
// UTF-16LE, already in order: elfStyle
U_swap4(&(lfp->elfVersion),4); // elfVersion elfStyleSize elfMatch elfReserved
// ordered bytes: elfVendorId
U_swap4(&(lfp->elfCulture),1); // elfCulture
// ordered bytes: elfPanose
}
/**
\brief Convert a U_BITMAPINFOHEADER object.
\param Bmi U_BITMAPINFOHEADER object
*/
void bitmapinfoheader_swap(
PU_BITMAPINFOHEADER Bmi
){
U_swap4(Bmi,3); // biSize biWidth biHeight
U_swap2(&(Bmi->biPlanes),2); // biPlanes biBitCount
U_swap4(&(Bmi->biCompression),6); // biCompression biSizeImage biXPelsPerMeter biYPelsPerMeter biClrUsed biClrImportant
}
/**
\brief Convert a Pointer to a U_BITMAPINFO object.
\param Bmi Pointer to a U_BITMAPINFO object
*/
void bitmapinfo_swap(
const char *Bmi
){
PU_BITMAPINFO pBmi = (PU_BITMAPINFO)Bmi;
bitmapinfoheader_swap(&(pBmi->bmiHeader)); // bmIHeader
// ordered bytes: bmiColors
}
/**
\brief Swap the ordered bytes in a DIB and verify that the sizes are OK
\return 1 on success, 0 on failure
\param record EMF record that contains a DIB pixel array
\param iUsage DIBcolors Enumeration
\param offBmi offset from the start of the record to the start of the bitmapinfo structure
\param cbBmi declared space for the bitmapinfo structure in the record
\param offBits offset from the start of the record to the start of the bitmap
\param cbBits declared space for the bitmap in the record (amount used may be less than this)
\param blimit one byte past the end of the record.
\param torev 1 for native to reversed, 0 for reversed to native
This method can only test DIBs that hold Microsoft's various bitmap types. PNG or JPG is just a bag
of bytes, and there is no possible way to derive from the known width and height how big it should be.
*/
int DIB_swap(
const char *record,
uint32_t iUsage,
uint32_t offBmi,
uint32_t cbBmi,
uint32_t offBits,
uint32_t cbBits,
const char *blimit,
int torev
){
int dibparams = U_BI_UNKNOWN; // type of image not yet determined
const char *px = NULL; // DIB pixels
const U_RGBQUAD *ct = NULL; // DIB color table
int bs;
int usedbytes;
if(!cbBmi)return(1); // No DIB in a record where it is optional
if(IS_MEM_UNSAFE(record, offBmi + cbBmi, blimit))return(0);
if(cbBits && IS_MEM_UNSAFE(record, offBits + cbBits, blimit))return(0);
if(iUsage == U_DIB_RGB_COLORS){
uint32_t width, height, colortype, numCt, invert; // these values will be set in get_DIB_params
// next call returns pointers and values, but allocates no memory
if(torev){
dibparams = get_DIB_params(record, offBits, offBmi, &px, (const U_RGBQUAD **) &ct,
&numCt, &width, &height, &colortype, &invert);
}
bitmapinfo_swap(record + offBmi); // byte ordered fields in bitmapinfo
if(!torev){
dibparams = get_DIB_params(record, offBits, offBmi, &px, (const U_RGBQUAD **) &ct,
&numCt, &width, &height, &colortype, &invert);
}
// sanity checking
if(numCt && colortype >= U_BCBM_COLOR16)return(0); //color tables not used above 16 bit pixels
if(!numCt && colortype < U_BCBM_COLOR16)return(0); //color tables mandatory for < 16 bit
if(dibparams ==U_BI_RGB){
// this is the only DIB type where we can calculate how big it should be when stored in the EMF file
bs = colortype/8;
if(bs<1){
usedbytes = (width*colortype + 7)/8; // width of line in fully and partially occupied bytes
}
else {
usedbytes = width*bs;
}
if(IS_MEM_UNSAFE(record+offBits, usedbytes, blimit))return(0);
}
}
else {
bitmapinfo_swap(record + offBmi);
}
return(1);
}
/**
\brief Convert a pointer to a U_EXTLOGPEN object.
\param elp PU_EXTLOGPEN object
\param blimit one byte past the end of the record
*/
int extlogpen_swap(
PU_EXTLOGPEN elp,
const char *blimit,
int torev
){
int count=0;
U_swap4(elp,3); // elpPenStyle elpWidth elpBrushStyle
// ordered bytes: elpColor
if(torev){
count = elp->elpNumEntries;
}
U_swap4(&(elp->elpHatch),2); // elpHatch elpNumEntries
if(!torev){
count = elp->elpNumEntries;
}
if(IS_MEM_UNSAFE(&(elp->elpStyleEntry), count*4, blimit))return(0);
U_swap4(&(elp->elpStyleEntry),count); // elpStyleEntry[]
return(1);
}
/**
\brief Convert a U_LOGPEN object.
\param lp U_LOGPEN object
*/
void logpen_swap(
PU_LOGPEN lp
){
U_swap4(lp,1); // lopnStyle
pointl_swap(&(lp->lopnWidth),1); // lopnWidth
// ordered bytes: lopnColor
}
/**
\brief Convert a pointer to a U_LOGPALETTE object.
\param lp Pointer to a U_LOGPALETTE object.
*/
void logpalette_swap(
PU_LOGPALETTE lp
){
U_swap2(lp,2); // palVersion palNumEntries
// ordered bytes: palPalEntry[]
}
/**
\brief Convert a U_RGNDATAHEADER object.
\param rdh U_RGNDATAHEADER object
*/
void rgndataheader_swap(
PU_RGNDATAHEADER rdh
){
U_swap4(rdh,4); // dwSize iType nCount nRgnSize
rectl_swap(&(rdh->rclBounds),1); // rclBounds
}
/**
\return 1 on success, 0 on failure
\brief Convert a pointer to a U_RGNDATA object.
\param rd pointer to a U_RGNDATA object.
\param cbRgnData size of the U_RGNDATA object.
*/
int rgndata_swap(
PU_RGNDATA rd,
int cbRgnData,
int torev
){
int count = 0;
if(torev){
count = rd->rdh.nCount;
}
rgndataheader_swap(&(rd->rdh));
if(!torev){
count = rd->rdh.nCount;
}
if(4*count + (int)sizeof(U_RGNDATAHEADER) > cbRgnData)return(0);
U_swap4(rd->Buffer,4*count);
return(1);
}
/**
\brief Convert a U_COLORADJUSTMENT object.
\param ca U_COLORADJUSTMENT object.
*/
void coloradjustment_swap(
PU_COLORADJUSTMENT ca
){
U_swap2(ca,12); // caSize caFlags caIlluminantIndex caRedGamma caGreenGamma caBlueGamma caReferenceBlack caReferenceWhite caContrast caBrightness caColorfulness caRedGreenTint
}
/**
\brief Convert a pointer to a U_PIXELFORMATDESCRIPTOR object.
\param pfd pointer to a U_PIXELFORMATDESCRIPTOR object.
*/
void pixelformatdescriptor_swap(
PU_PIXELFORMATDESCRIPTOR pfd
){
U_swap2(pfd,2); // nSize nVersion
U_swap4(&(pfd->dwFlags),1); // dwFlags
// ordered bytes: iPixelType cColorBits cRedBits cRedShift cGreenBits cGreenShift cBlueBits cBlueShift cAlphaBits cAlphaShift cAccumBits cAccumRedBits cAccumGreenBits cAccumBlueBits cAccumAlphaBits cDepthBits cStencilBits cAuxBuffers iLayerType bReserved
U_swap4(&(pfd->dwLayerMask),3); // dwLayerMask dwVisibleMask dwDamageMask
}
/**
\brief Convert a Pointer to a U_EMRTEXT record
\param pemt Pointer to a U_EMRTEXT record
\param record Pointer to the start of the record which contains this U_EMRTEXT
\param blimit one byte past the end of the record.
\param torev 1 for native to reversed, 0 for reversed to native
*/
int emrtext_swap(
PU_EMRTEXT pemt,
char *record,
const char *blimit,
int torev
){
int off;
uint32_t count=0;
uint32_t offDx=0;
uint32_t fOptions=0;
pointl_swap(&(pemt->ptlReference),1); // ptlReference
if(torev){
count = pemt->nChars;
fOptions = pemt->fOptions;
}
U_swap4(&(pemt->nChars),3); // nChars offString fOptions
if(!torev){
count = pemt->nChars;
fOptions = pemt->fOptions;
}
off = sizeof(U_EMRTEXT);
if(!(fOptions & U_ETO_NO_RECT)){
if(IS_MEM_UNSAFE(pemt, sizeof(U_RECTL), blimit))return(0);
rectl_swap((PU_RECTL)((char *)pemt + off),1); // optional rectangle
off+=sizeof(U_RECTL);
}
if(torev){
offDx = *(uint32_t *)((char *)pemt +off);
}
// ordered bytes OR UTF16-LE: the string at offString
if(IS_MEM_UNSAFE(pemt, off + 4, blimit))return(0);
U_swap4(((char *)pemt+off),1); // offDx
if(!torev){
offDx = *(uint32_t *)((char *)pemt +off);
}
if(IS_MEM_UNSAFE(record, count*4, blimit))return(0);
U_swap4((record + offDx),count); // Dx[], offset with respect to the Record, NOT the object
return(1);
}
/* **********************************************************************************************
These functions contain shared code used by various U_EMR*_swap functions. These should NEVER be called
by end user code and to further that end prototypes are NOT provided and they are hidden from Doxygen.
These all have this form:
void core1_swap(char *record, int torev){
but some do not actually use torev.
*********************************************************************************************** */
// all core*_swap call this, U_EMRSETMARGN_swap and some others all it directly
// numbered as core5 to be consistent with uemf.c, but must appear before the others as there is no prototype
int core5_swap(char *record, int torev){
UNUSED_PARAMETER(torev);
if(!record)return(0);
PU_EMR pEmr = (PU_EMR)(record);
U_swap4(pEmr,2); // iType nSize
return(1);
}
// Functions with the same form starting with U_EMRPOLYBEZIER_swap
int core1_swap(char *record, int torev){
int count=0;
const char *blimit = NULL;
PU_EMRPOLYLINETO pEmr = (PU_EMRPOLYLINETO) (record);
if(torev){
count = pEmr->cptl;
blimit = record + pEmr->emr.nSize;
}
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBounds),1 ); // rclBounds
U_swap4(&(pEmr->cptl),1); // cptl
if(!torev){
count = pEmr->cptl;
blimit = record + pEmr->emr.nSize;
}
if(IS_MEM_UNSAFE((pEmr->aptl), count*sizeof(U_POINTL), blimit))return(0);
pointl_swap((pEmr->aptl),count); // aptl[]
return(1);
}
// Functions with the same form starting with U_EMRPOLYPOLYLINE_swap
int core2_swap(char *record, int torev){
int count=0;
int nPolys=0;
const char *blimit = NULL;
PU_EMRPOLYPOLYLINE pEmr = (PU_EMRPOLYPOLYLINE) (record);
if(torev){
count = pEmr->cptl;
nPolys = pEmr->nPolys;
blimit = record + pEmr->emr.nSize;
}
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBounds),1); // rclBounds
U_swap4(&(pEmr->nPolys),2); // nPolys cptl
if(!torev){
count = pEmr->cptl;
nPolys = pEmr->nPolys;
blimit = record + pEmr->emr.nSize;
}
if(IS_MEM_UNSAFE((pEmr->aPolyCounts), nPolys*4, blimit))return(0);
U_swap4(pEmr->aPolyCounts,nPolys); // aPolyCounts[]
record += sizeof(U_EMRPOLYPOLYLINE) - 4 + sizeof(uint32_t)* nPolys;
if(IS_MEM_UNSAFE(record, count*sizeof(U_POINTL), blimit))return(0);
pointl_swap((PU_POINT)(record), count); // paptl[]
return(1);
}
// Functions with the same form starting with U_EMRSETMAPMODE_swap
int core3_swap(char *record, int torev){
PU_EMRSETMAPMODE pEmr = (PU_EMRSETMAPMODE)(record);
if(!core5_swap(record, torev))return(0);
U_swap4(&(pEmr->iMode),1); // iMode
return(1);
}
// Functions taking a single U_RECT or U_RECTL, starting with U_EMRELLIPSE_swap, also U_EMRFILLPATH_swap,
int core4_swap(char *record, int torev){
PU_EMRELLIPSE pEmr = (PU_EMRELLIPSE)( record);
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBox),1); // rclBox
return(1);
}
// Functions with the same form starting with U_EMRPOLYBEZIER16_swap
int core6_swap(char *record, int torev){
int count=0;
const char *blimit = NULL;
PU_EMRPOLYBEZIER16 pEmr = (PU_EMRPOLYBEZIER16) (record);
if(torev){
count = pEmr->cpts;
blimit = record + pEmr->emr.nSize;
}
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBounds),1); // rclBounds
U_swap4(&(pEmr->cpts),1); // cpts
if(!torev){
count = pEmr->cpts;
blimit = record + pEmr->emr.nSize;
}
if(IS_MEM_UNSAFE((pEmr->apts), count*sizeof(U_POINT16), blimit))return(0);
point16_swap((pEmr->apts),count); // apts[]
return(1);
}
// Records with the same form starting with U_EMRSETWINDOWEXTEX_swap, that is, all with two uint32_t values after the emr
int core7_swap(char *record, int torev){
PU_EMRGENERICPAIR pEmr = (PU_EMRGENERICPAIR) (record);
if(!core5_swap(record, torev))return(0);
U_swap4(&(pEmr->pair),2);
return(1);
}
// For U_EMREXTTEXTOUTA and U_EMREXTTEXTOUTW, type=0 for the first one
int core8_swap(char *record, int torev){
const char *blimit = NULL;
PU_EMREXTTEXTOUTA pEmr = (PU_EMREXTTEXTOUTA) (record);
if(torev){
blimit = record + pEmr->emr.nSize;
}
if(!core5_swap(record, torev))return(0);
U_swap4(&(pEmr->iGraphicsMode),1); // iGraphicsMode
rectl_swap(&(pEmr->rclBounds),1); // rclBounds
U_swap4(&(pEmr->exScale),2); // exScale eyScale
if(!torev){
blimit = record + pEmr->emr.nSize;
}
if(!emrtext_swap(&(pEmr->emrtext),record,blimit,torev))return(0);
return(1);
}
// Functions that take a rect and a pair of points, starting with U_EMRARC_swap
int core9_swap(char *record, int torev){
PU_EMRARC pEmr = (PU_EMRARC) (record);
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBox),1); // rclBox
U_swap4(&(pEmr->ptlStart),4); // ptlStart ptlEnd
return(1);
}
// Functions with the same form starting with U_EMRPOLYPOLYLINE16_swap
int core10_swap(char *record, int torev){
int count=0;
int nPolys=0;
const char *blimit = NULL;
PU_EMRPOLYPOLYLINE16 pEmr = (PU_EMRPOLYPOLYLINE16) (record);
if(torev){
count = pEmr->cpts;
nPolys = pEmr->nPolys;
blimit = record + pEmr->emr.nSize;
}
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBounds),1); // rclBounds
U_swap4(&(pEmr->nPolys),2); // nPolys cpts
if(!torev){
count = pEmr->cpts;
nPolys = pEmr->nPolys;
blimit = record + pEmr->emr.nSize;
}
if(IS_MEM_UNSAFE((pEmr->aPolyCounts), nPolys*4, blimit))return(0);
U_swap4(pEmr->aPolyCounts,nPolys); // aPolyCounts[]
record += sizeof(U_EMRPOLYPOLYLINE16) - 4 + sizeof(uint32_t)* nPolys;
if(IS_MEM_UNSAFE(record, count*sizeof(U_POINT16), blimit))return(0);
point16_swap((PU_POINT16)(record), count); // apts[]
return(1);
}
// Functions with the same form starting with U_EMRINVERTRGN_swap and U_EMRPAINTRGN_swap,
int core11_swap(char *record, int torev){
int cbRgnData=0;
const char *blimit = NULL;
PU_EMRINVERTRGN pEmr = (PU_EMRINVERTRGN)(record);
if(torev){
cbRgnData= pEmr->cbRgnData;
blimit = record + pEmr->emr.nSize;
}
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBounds),1); // rclBounds
U_swap4(&(pEmr->cbRgnData),1); // cbRgnData
if(!torev){
cbRgnData= pEmr->cbRgnData;
blimit = record + pEmr->emr.nSize;
}
if(IS_MEM_UNSAFE(pEmr->RgnData, cbRgnData, blimit))return(0);
return(rgndata_swap(pEmr->RgnData, cbRgnData, torev));
}
// common code for U_EMRCREATEMONOBRUSH_swap and U_EMRCREATEDIBPATTERNBRUSHPT_swap,
int core12_swap(char *record, int torev){
const char *blimit = NULL;
U_OFFBMI offBmi = 0;
U_CBBMI cbBmi = 0;
U_OFFBITS offBits = 0;
U_CBBITS cbBits = 0;
uint32_t iUsage = 0;
PU_EMRCREATEMONOBRUSH pEmr = (PU_EMRCREATEMONOBRUSH) (record);
if(torev){
offBmi = pEmr->offBmi;
cbBmi = pEmr->cbBmi;
offBits = pEmr->offBits;
cbBits = pEmr->cbBits;
iUsage = pEmr->iUsage;
blimit = record + pEmr->emr.nSize;
if(!DIB_swap(record, iUsage, offBmi, cbBmi, offBits, cbBits, blimit, torev))return(0);
}
if(!core5_swap(record, torev))return(0);
U_swap4(&(pEmr->ihBrush),6); // ihBrush iUsage offBmi cbBmi offBits cbBits
if(!torev){
offBmi = pEmr->offBmi;
cbBmi = pEmr->cbBmi;
offBits = pEmr->offBits;
cbBits = pEmr->cbBits;
iUsage = pEmr->iUsage;
blimit = record + pEmr->emr.nSize;
if(!DIB_swap(record, iUsage, offBmi, cbBmi, offBits, cbBits, blimit, torev))return(0);
}
// ordered bytes: bitmap (including 16 bit 5bit/channel color mode, which is done bytewise).
return(1);
}
// common code for U_EMRALPHABLEND_swap and U_EMRTRANSPARENTBLT_swap,
int core13_swap(char *record, int torev){
const char *blimit = NULL;
U_OFFBMISRC offBmiSrc = 0;
U_CBBMISRC cbBmiSrc = 0;
U_OFFBITSSRC offBitsSrc = 0;
U_CBBITSSRC cbBitsSrc = 0;
uint32_t iUsageSrc = 0;
PU_EMRALPHABLEND pEmr = (PU_EMRALPHABLEND) (record);
if(torev){
offBmiSrc = pEmr->offBmiSrc;
cbBmiSrc = pEmr->cbBmiSrc;
offBitsSrc = pEmr->offBitsSrc;
cbBitsSrc = pEmr->cbBitsSrc;
iUsageSrc = pEmr->iUsageSrc;
blimit = record + pEmr->emr.nSize;
if(!DIB_swap(record, iUsageSrc, offBmiSrc, cbBmiSrc, offBitsSrc, cbBitsSrc, blimit, torev))return(0);
}
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBounds),1); // rclBounds
pointl_swap(&(pEmr->Dest),2); // Dest cDest
pointl_swap(&(pEmr->Dest),2); // Dest cDest
// ordered bytes: Blend
pointl_swap(&(pEmr->Src),2); // Src
xform_swap( &(pEmr->xformSrc)); // xformSrc
// ordered bytes: crBkColorSrc
U_swap4(&(pEmr->iUsageSrc),5); // iUsageSrc offBmiSrc cbBmiSrc offBitsSrc cbBitsSrc
if(!torev){
offBmiSrc = pEmr->offBmiSrc;
cbBmiSrc = pEmr->cbBmiSrc;
offBitsSrc = pEmr->offBitsSrc;
cbBitsSrc = pEmr->cbBitsSrc;
iUsageSrc = pEmr->iUsageSrc;
blimit = record + pEmr->emr.nSize;
if(!DIB_swap(record, iUsageSrc, offBmiSrc, cbBmiSrc, offBitsSrc, cbBitsSrc, blimit, torev))return(0);
}
// ordered bytes: bitmap (including 16 bit 5bit/channel color mode, which is done bytewise).
return(1);
}
/* **********************************************************************************************
These are the core EMR_swap functions, each converts a particular type of record.
All operate in place on the chunk of memory holding that record.
Some of these have offsets or counts which, if corrupt or evil would result in access outside
the record. These cases return a status value of 0 if that happens, 1 on success. Other
records which do not have these issues do not return a status value.
They are listed in order by the corresponding U_EMR_* index number.
*********************************************************************************************** */
/**
All of the record level (hidden) functions have this form:
\brief Convert a pointer to a U_EMR_whatever record which has not been implemented.
\param record pointer to a buffer holding the EMR record
\param torev 1 for native to reversed, 0 for reversed to native
*/
int U_EMRNOTIMPLEMENTED_swap(char *record, int torev){
fprintf(stderr,"EMF WARNING: could not swap data bytes on record because that type has not been implemented!\n");
return(core5_swap(record, torev));
}
// U_EMRHEADER 1
int U_EMRHEADER_swap(char *record, int torev){
int nDesc,offDesc,nSize,cbPix,offPix;
nDesc = offDesc = nSize = cbPix = offPix = 0;
PU_EMRHEADER pEmr = (PU_EMRHEADER)(record);
if(torev){
nSize = pEmr->emr.nSize;
nDesc = pEmr->nDescription;
offDesc = pEmr->offDescription;
}
if(!core5_swap(record, torev))return(0);
rectl_swap(&(pEmr->rclBounds),2); // rclBounds rclFrame
U_swap4(&(pEmr->dSignature), 4); // dSignature nVersion nBytes nRecords
U_swap2(&(pEmr->nHandles), 2); // nHandlessReserved
U_swap4(&(pEmr->nDescription), 3); // nDescription offDescription nPalEntries
if(!torev){
nSize = pEmr->emr.nSize;
nDesc = pEmr->nDescription;
offDesc = pEmr->offDescription;
}
// UTF16-LE Description
sizel_swap(&(pEmr->szlDevice), 2); // szlDevice szlMillimeters
if((nDesc && (offDesc >= 100)) ||
(!offDesc && nSize >= 100)
){
if(torev){
cbPix = pEmr->cbPixelFormat;
offPix = pEmr->offPixelFormat;
}
U_swap4(&(pEmr->cbPixelFormat), 2); // cbPixelFormat offPixelFormat
U_swap4(&(pEmr->bOpenGL), 1); // bOpenGL
if(!torev){
cbPix = pEmr->cbPixelFormat;
offPix = pEmr->offPixelFormat;
}
if(cbPix)pixelformatdescriptor_swap( (PU_PIXELFORMATDESCRIPTOR) (record + pEmr->offPixelFormat));
if((nDesc && (offDesc >= 108)) ||
(cbPix && (offPix >=108)) ||
(!offDesc && !cbPix && nSize >= 108)
){
sizel_swap(&(pEmr->szlMicrometers), 1); // szlMicrometers
}
}
return(1);
}
// U_EMRPOLYBEZIER 2
int U_EMRPOLYBEZIER_swap(char *record, int torev){
return(core1_swap(record, torev));
}
// U_EMRPOLYGON 3
int U_EMRPOLYGON_swap(char *record, int torev){
return(core1_swap(record, torev));
}
// U_EMRPOLYLINE 4
int U_EMRPOLYLINE_swap(char *record, int torev){
return(core1_swap(record, torev));
}
// U_EMRPOLYBEZIERTO 5
int U_EMRPOLYBEZIERTO_swap(char *record, int torev){
return(core1_swap(record, torev));
}
// U_EMRPOLYLINETO 6
int U_EMRPOLYLINETO_swap(char *record, int torev){
return(core1_swap(record, torev));
}
// U_EMRPOLYPOLYLINE 7
int U_EMRPOLYPOLYLINE_swap(char *record, int torev){
return(core2_swap(record, torev));
}
// U_EMRPOLYPOLYGON 8
int U_EMRPOLYPOLYGON_swap(char *record, int torev){
return(core2_swap(record, torev));
}
// U_EMRSETWINDOWEXTEX 9
int U_EMRSETWINDOWEXTEX_swap(char *record, int torev){
return(core7_swap(record, torev));
}
// U_EMRSETWINDOWORGEX 10
int U_EMRSETWINDOWORGEX_swap(char *record, int torev){
return(core7_swap(record, torev));
}
// U_EMRSETVIEWPORTEXTEX 11
int U_EMRSETVIEWPORTEXTEX_swap(char *record, int torev){
return(core7_swap(record, torev));
}
// U_EMRSETVIEWPORTORGEX 12
int U_EMRSETVIEWPORTORGEX_swap(char *record, int torev){
return(core7_swap(record, torev));
}
// U_EMRSETBRUSHORGEX 13
int U_EMRSETBRUSHORGEX_swap(char *record, int torev){
return(core7_swap(record, torev));
}
// U_EMREOF 14
int U_EMREOF_swap(char *record, int torev){
int off=0;
int cbPalEntries=0;
const char *blimit = NULL;
PU_EMREOF pEmr = (PU_EMREOF)(record);
if(torev){
blimit = record + pEmr->emr.nSize;
cbPalEntries = pEmr->cbPalEntries;
}
if(!core5_swap(record, torev))return(0);
U_swap4(&(pEmr->cbPalEntries),2); // cbPalEntries offPalEntries
if(!torev){
blimit = record + pEmr->emr.nSize;
cbPalEntries = pEmr->cbPalEntries;
}
if(cbPalEntries){
if(IS_MEM_UNSAFE(record, pEmr->offPalEntries + 2*2, blimit))return(0); // 2 16 bit values in U_LOGPALLETE
logpalette_swap( (PU_LOGPALETTE)(record + pEmr->offPalEntries));
// U_LOGPLTNTRY values in pallette are ordered data
}
off = sizeof(U_EMREOF) + 4 * cbPalEntries;
if(IS_MEM_UNSAFE(record, off + 4, blimit))return(0);
U_swap4(record + off,1); // nSizeLast
return(1);
}
// U_EMRSETPIXELV 15
int U_EMRSETPIXELV_swap(char *record, int torev){
if(!core5_swap(record, torev))return(0);
PU_EMRSETPIXELV pEmr = (PU_EMRSETPIXELV)(record);
pointl_swap(&(pEmr->ptlPixel),1); // ptlPixel
// ordered bytes: crColor
return(1);
}
// U_EMRSETMAPPERFLAGS 16
int U_EMRSETMAPPERFLAGS_swap(char *record, int torev){
if(!core5_swap(record, torev))return(0);
PU_EMRSETMAPPERFLAGS pEmr = (PU_EMRSETMAPPERFLAGS)(record);
U_swap4(&(pEmr->dwFlags),1); // dwFlags
return(1);
}
// U_EMRSETMAPMODE 17
int U_EMRSETMAPMODE_swap(char *record, int torev){
return(core3_swap(record, torev));
}
// U_EMRSETBKMODE 18
int U_EMRSETBKMODE_swap(char *record, int torev){
return(core3_swap(record, torev));
}
// U_EMRSETPOLYFILLMODE 19
int U_EMRSETPOLYFILLMODE_swap(char *record, int torev){
return(core3_swap(record, torev));
}
// U_EMRSETROP2 20
int U_EMRSETROP2_swap(char *record, int torev){
return(core3_swap(record, torev));
}
// U_EMRSETSTRETCHBLTMODE 21
int U_EMRSETSTRETCHBLTMODE_swap(char *record, int torev){
return(core3_swap(record, torev));
}
// U_EMRSETTEXTALIGN 22
int U_EMRSETTEXTALIGN_swap(char *record, int torev){
return(core3_swap(record, torev));
}
// U_EMRSETCOLORADJUSTMENT 23
int U_EMRSETCOLORADJUSTMENT_swap(char *record, int torev){
if(!core5_swap(record, torev))return(0);
PU_EMRSETCOLORADJUSTMENT pEmr = (PU_EMRSETCOLORADJUSTMENT)(record);
coloradjustment_swap(&(pEmr->ColorAdjustment));
return(1);
}
// U_EMRSETTEXTCOLOR 24
int U_EMRSETTEXTCOLOR_swap(char *record, int torev){
if(!core5_swap(record, torev))return(0);
// ordered bytes: crColor
return(1);
}
// U_EMRSETBKCOLOR 25
int U_EMRSETBKCOLOR_swap(char *record, int torev){
if(!core5_swap(record, torev))return(0);
// ordered bytes: crColor
return(1);
}
// U_EMROFFSETCLIPRGN 26
int U_EMROFFSETCLIPRGN_swap(char *record, int torev){
return(core7_swap(record, torev));
}