summaryrefslogtreecommitdiffstats
path: root/private/ole32/ole232/util/convert.cpp
blob: cb79c82793b5f627756cde26993f0ef1cfbcee03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
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
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209

//+----------------------------------------------------------------------------
//
//      File:
//              convert.cpp
//
//      Contents:
//              This module contains the code to read/write DIB, metafile,
//              placeable metafiles, olepres stream, etc... This module also
//              contains routines for converting from one format to other.
//
//      Classes:
//
//      Functions:
//
//      History:
//              15-Feb-94 alexgo    fixed a bug in loading placeable metafiles
//                                  from a storage (incorrect size calculation).
//              25-Jan-94 alexgo    first pass at converting to Cairo-style
//                                  memory allocations.
//              01/11/93 - alexgo  - added VDATEHEAP macros to every function
//              12/08/93 - ChrisWe - fixed wPrepareBitmapHeader not to use
//                      (LPOLESTR) cast
//              12/07/93 - ChrisWe - make default params to StSetSize explicit
//              12/02/93 - ChrisWe - more formatting; fixed UtHMFToMFStm,
//                      32 bit version, which was doing questionable things
//                      with the hBits handle;  got rid of the OLESTR
//                      uses in favor of (void *) or (BYTE *) as appropriate
//              11/29/93 - ChrisWe - move CONVERT_SOURCEISICON, returned
//                      by UtOlePresStmToContentsStm(), to utils.h
//              11/28/93 - ChrisWe - begin file inspection and cleanup
//              06/28/93 - SriniK - created
//
//-----------------------------------------------------------------------------

#include <le2int.h>

#pragma SEG(ole2)

NAME_SEG(convert)
ASSERTDATA

#ifndef _MAC
FARINTERNAL_(HMETAFILE) QD2GDI(HANDLE hBits);
#endif

void UtGetHEMFFromContentsStm(LPSTREAM lpstm, HANDLE * phdata);


/************************   FILE FORMATS   **********************************

Normal Metafile (memory or disk based):

	 ------------ ---------------
	| METAHEADER | Metafile bits |
	 ------------ ---------------
	
Placeable Metafile:

	 --------------------- -----------------
	| PLACEABLEMETAHEADER | Normal metafile |
	 --------------------- -----------------

Memory Based DIB:

	 ------------------ --------------- ----------
	| BITMAPINFOHEADER | RGBQUAD array | DIB bits |
	 ------------------ --------------- ----------
	
DIB file format:

	 ------------------ ------------------
	| BITMAPFILEHEADER | Memory based DIB |
	 ------------------ ------------------
	
Ole10NativeStream Format:       
	
	 -------- ----------------------
	| dwSize | Object's Native data |
	 -------- ----------------------
	
PBrush Native data format:

	 -----------------
	| Dib File format |
	 -----------------
	
MSDraw Native data format:

	 --------------------- ------------- ------------- -----------------
	| mapping mode (WORD) | xExt (WORD) | yExt (WORD) | Normal metafile |
	 --------------------- ------------- ------------- -----------------
	
*****************************************************************************/


FARINTERNAL UtGetHGLOBALFromStm(LPSTREAM lpstream, DWORD dwSize,
		HANDLE FAR* lphPres)
{
	VDATEHEAP();

	HANDLE hBits = NULL;
	void FAR *lpBits = NULL;
	HRESULT error;

	// initialize this for error return cases
	*lphPres = NULL;
	
	// allocate a new handle
	if (!(hBits = GlobalAlloc(GMEM_MOVEABLE, dwSize))
			|| !(lpBits = (BYTE *)GlobalLock(hBits)))
	{
		error = ResultFromScode(E_OUTOFMEMORY);
		goto errRtn;
	}
	
	// read the stream into the allocated memory
	if (error = StRead(lpstream, lpBits, dwSize))
		goto errRtn;
	
	// if we got this far, return new handle
	*lphPres = hBits;

errRtn:
	// unlock the handle, if it was successfully locked
	if (lpBits)
		GlobalUnlock(hBits);

	// free the handle if there was an error
	if ((error != NOERROR) && hBits)
		GlobalFree(hBits);

	return(error);
}


#ifndef _MAC

FARINTERNAL UtGetHDIBFromDIBFileStm(LPSTREAM pstm, HANDLE FAR* lphdata)
{
	VDATEHEAP();

	BITMAPFILEHEADER bfh;
	DWORD dwSize; // the size of the data to read
	HRESULT error;
	
	// read the bitmap file header
	if (error = pstm->Read(&bfh, sizeof(BITMAPFILEHEADER), NULL))
	{
		*lphdata = NULL;
		return(error);
	}
	
	// calculate the size of the DIB to read
	dwSize = bfh.bfSize - sizeof(BITMAPFILEHEADER);

	// read the DIB
	return(UtGetHGLOBALFromStm(pstm, dwSize, lphdata));
}


FARINTERNAL_(HANDLE) UtGetHMFPICT(HMETAFILE hMF, BOOL fDeleteOnError,
		DWORD xExt, DWORD yExt)
{
	VDATEHEAP();

	HANDLE hmfp; // handle to the new METAFILEPICT
	LPMETAFILEPICT lpmfp; // pointer to the new METAFILEPICT

	// if no METAFILE, nothing to do
	if (hMF == NULL)
		return(NULL);
	
	// allocate a new handle
	if (!(hmfp = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT))))
		goto errRtn;

	// lock the handle
	if (!(lpmfp = (LPMETAFILEPICT)GlobalLock(hmfp)))
		goto errRtn;

	// make the METAFILEPICT
	lpmfp->hMF = hMF;
	lpmfp->xExt = (int)xExt;
	lpmfp->yExt = (int)yExt;
	lpmfp->mm = MM_ANISOTROPIC;

	GlobalUnlock(hmfp);
	return(hmfp);
	
errRtn:
	if (hmfp)
		GlobalFree(hmfp);
	
	if (fDeleteOnError)
		DeleteMetaFile(hMF);
	
	return(NULL);
}

#endif // _MAC

//+-------------------------------------------------------------------------
//
//  Function:   UtGetHMFFromMFStm
//
//  Synopsis:
//
//  Effects:
//
//  Arguments:  [lpstream] -- stream containing metafile or PICT
//              [dwSize] -- data size within stream
//              [fConvert] -- FALSE for metafile, TRUE for PICT
//              [lphPres] -- placeholder for output metafile
//
//  Requires:   lpstream positioned at start of data
//
//  Returns:    HRESULT
//
//  Modifies:
//
//  Algorithm:
//
//  History:    29-Apr-94 AlexT     Add comment block, enabled Mac conversion
//
//  Notes:
//
//--------------------------------------------------------------------------

FARINTERNAL UtGetHMFFromMFStm(LPSTREAM lpstream, DWORD dwSize,
		BOOL fConvert, HANDLE FAR* lphPres)
{
#ifdef WIN32
	LEDebugOut((DEB_ITRACE, "%p _IN UtGetHMFFromMFStm (%p, %d, %d, %p)\n",
		NULL, lpstream, dwSize, fConvert, lphPres));

	VDATEHEAP();

	BYTE *pbMFData = NULL;
	HRESULT hrError;
	
	// initialize this in case of error return
	*lphPres = NULL;

	// allocate a global handle for the data (since QD2GDI needs a
	// handle)

	pbMFData = (BYTE *) GlobalAlloc(GMEM_FIXED, dwSize);
	if (NULL == pbMFData)
	{
	    hrError = ResultFromScode(E_OUTOFMEMORY);
	    goto errRtn;
	}
	
	// read the stream into the bit storage
	
	ULONG cbRead;
	hrError = lpstream->Read(pbMFData, dwSize, &cbRead);
	if (FAILED(hrError))
	{
	    return(hrError);
	}
	// hrError = StRead(lpstream, pbMFData, dwSize);

	if (hrError != NOERROR)
	{
	    goto errRtn;
	}

	if (fConvert)
	{
	    //  It's a Mac PICT
	    *lphPres = QD2GDI((HGLOBAL) pbMFData);
	}
	else
	{
	    //  It's a Windows metafile
	    *lphPres = SetMetaFileBitsEx(dwSize, pbMFData);
	}

	if (*lphPres == NULL)
	    hrError = ResultFromScode(E_OUTOFMEMORY);

errRtn:
	if (NULL != pbMFData)
	{
	    GlobalFree(pbMFData);
	}

	LEDebugOut((DEB_ITRACE, "%p OUT UtGetHMFFromMFStm ( %lx ) [ %p ]\n",
		    NULL, hrError, *lphPres));

	return(hrError);
#else
	HANDLE hBits; // handle to the new METAFILE
	void FAR* lpBits = NULL;
	HRESULT error;
	
	// initialize this in case of error return
	*lphPres = NULL;

	// allocate a new handle, and lock the bits
	if (!(hBits = GlobalAlloc(GMEM_MOVEABLE, dwSize))
			|| !(lpBits = GlobalLock(hBits)))
	{
		error = ResultFromScode(E_OUTOFMEMORY);
		goto errRtn;
	}
	
	// read the stream into the bit storage
	error = StRead(lpstream, lpBits, dwSize);
	GlobalUnlock(hBits);

	if (error)
		goto errRtn;
	
	if (!fConvert)
		*lphPres = SetMetaFileBits(hBits);
	else
	{
		if (*lphPres = QD2GDI(hBits))
		{
			// Need to free this handle upon success
			GlobalFree(hBits);
			hBits = NULL;
		}
	
	}
	
	if (!*lphPres)
		error = ResultFromScode(E_OUTOFMEMORY);
	
errRtn:
	if (error && hBits)
		GlobalFree(hBits);
	return(error);
#endif // WIN32
}


FARINTERNAL UtGetSizeAndExtentsFromPlaceableMFStm(LPSTREAM pstm,
		DWORD FAR* pdwSize, LONG FAR* plWidth, LONG FAR* plHeight)
{
	VDATEHEAP();

	HRESULT error;
	LARGE_INTEGER large_int; // used to set the seek pointer
	ULARGE_INTEGER ularge_int; // retrieves the new seek position
	LONG xExt; // the x extent of the metafile
	LONG yExt; // the y extent of the metafile
	METAHEADER mfh;
	PLACEABLEMETAHEADER plac_mfh;
	
	// read the placeable metafile header
	if (error = pstm->Read(&plac_mfh, sizeof(plac_mfh), NULL))
		return(error);
	
	// check the magic number in the header
	if (plac_mfh.key != PMF_KEY)
		return ResultFromScode(E_FAIL);

	// remember the seek pointer    
	LISet32(large_int, 0);
	if (error = pstm->Seek(large_int, STREAM_SEEK_CUR, &ularge_int))
		return(error);

	// read metafile header
	if (error = pstm->Read(&mfh, sizeof(mfh), NULL))
		return(error);
	
	// seek back to the begining of metafile header
	LISet32(large_int,  ularge_int.LowPart);
	if (error = pstm->Seek(large_int, STREAM_SEEK_SET, NULL))
		return(error);
	
	// calculate the extents of the metafile
	xExt = (plac_mfh.bbox.right - plac_mfh.bbox.left);// metafile units
	yExt = (plac_mfh.bbox.bottom - plac_mfh.bbox.top);// metafile units
	
	// REVIEW, why aren't there constants for this?
	xExt = (xExt * 2540) / plac_mfh.inch; // HIMETRIC units
	yExt = (yExt * 2540) / plac_mfh.inch; // HIMETRIC units
	
	if (pdwSize)
	{
#ifdef WIN16
		//this code seems to work OK on Win16
		*pdwSize = 2 * (mfh.mtSize + mfh.mtHeaderSize);
				// REVIEW NT: review METAHEADER
#else   //WIN32
		//mt.Size is the size in words of the metafile.
		//this fixes bug 6739 (static objects can't be copied
		//or loaded from a file).
		*pdwSize = sizeof(WORD) * mfh.mtSize;
#endif  //WIN16
	}

	if (plWidth)
		*plWidth = xExt;
	
	if (plHeight)
		*plHeight = yExt;
	
	return NOERROR;
}


FARINTERNAL UtGetHMFPICTFromPlaceableMFStm(LPSTREAM pstm, HANDLE FAR* lphdata)
{
	VDATEHEAP();

	HRESULT error; // error state so far
	DWORD dwSize; // size of the METAFILE we have to read from the stream
	LONG xExt; // x extent of the METAFILE we have to read from the stream
	LONG yExt; // y extent of the METAFILE we have to read from the stream
	HMETAFILE hMF; // handle to the METAFILE read from the stream

	if (lphdata == NULL)
		return ResultFromScode(E_INVALIDARG);
	
	// initialize this in case of error return
	*lphdata = NULL;

	// get the size of the METAFILE
	if (error = UtGetSizeAndExtentsFromPlaceableMFStm(pstm, &dwSize,
			&xExt, &yExt))
		return(error);          

	// fetch the METAFILE
	if (error = UtGetHMFFromMFStm(pstm, dwSize, FALSE /*fConvert*/,
			(HANDLE FAR *)&hMF))
		return(error);
	
	// convert to a METAFILEPICT
	if (!(*lphdata = UtGetHMFPICT(hMF, TRUE /*fDeleteOnError*/, xExt,
			yExt)))
		return ResultFromScode(E_OUTOFMEMORY);

	return NOERROR;
}



/****************************************************************************/
/*****************              Write routines          *********************/
/****************************************************************************/

#ifndef _MAC


//+----------------------------------------------------------------------------
//
//      Function:
//              wPrepareBitmapHeader, static
//
//      Synopsis:
//              Initializes the content of a BITMAPFILEHEADER
//
//      Arguments:
//              [lpbfh] -- pointer to the BITMAPFILEHEADER to initialize
//              [dwSize] -- the size of the file; obtained by dividing
//                      the size of the file by 4 (see win32 documentation.)
//
//      History:
//              12/08/93 - ChrisWe - made static
//
//-----------------------------------------------------------------------------
static INTERNAL_(void) wPrepareBitmapFileHeader(LPBITMAPFILEHEADER lpbfh,
		DWORD dwSize)
{
	VDATEHEAP();

	// NOTE THESE ARE NOT SUPPOSED TO BE UNICODE
	// see win32s documentation
	((char *)(&lpbfh->bfType))[0] = 'B';
	((char *)(&lpbfh->bfType))[1] = 'M';

	lpbfh->bfSize = dwSize + sizeof(BITMAPFILEHEADER);
	lpbfh->bfReserved1 = 0;
	lpbfh->bfReserved2 = 0;
	lpbfh->bfOffBits = 0;
}


FARINTERNAL UtHDIBToDIBFileStm(HANDLE hdata, DWORD dwSize, LPSTREAM pstm)
{
	VDATEHEAP();

	HRESULT error;
	BITMAPFILEHEADER bfh;
	
	wPrepareBitmapFileHeader(&bfh, dwSize);
	
	if (error = pstm->Write(&bfh, sizeof(bfh), NULL))
		return(error);
	
	return UtHGLOBALtoStm(hdata, dwSize, pstm);
}


FARINTERNAL UtDIBStmToDIBFileStm(LPSTREAM pstmDIB, DWORD dwSize,
		LPSTREAM pstmDIBFile)
{
	VDATEHEAP();

	HRESULT error;
	BITMAPFILEHEADER bfh;
	ULARGE_INTEGER ularge_int; // indicates how much to copy
	
	wPrepareBitmapFileHeader(&bfh, dwSize);
	
	if (error = pstmDIBFile->Write(&bfh, sizeof(bfh), NULL))
		return(error);
	
	ULISet32(ularge_int, dwSize);
	if ((error = pstmDIB->CopyTo(pstmDIBFile, ularge_int, NULL,
			NULL)) == NOERROR)
		StSetSize(pstmDIBFile, 0, TRUE);
	
	return(error);
}       


// REVIEW, move these to utils.h so that gen.cpp and mf.cpp can use them for
// the same purposes
// REVIEW, add some more comments; is HDIBFILEHDR a windows structure?
struct tagHDIBFILEHDR
{
	DWORD dwCompression;
	DWORD dwWidth;
	DWORD dwHeight;
	DWORD dwSize;
};
typedef struct tagHDIBFILEHDR HDIBFILEHDR;

struct tagOLEPRESSTMHDR
{
	DWORD dwAspect;
	DWORD dwLindex;
	DWORD dwAdvf;
};
typedef struct tagOLEPRESSTMHDR OLEPRESSTMHDR;

FARINTERNAL UtHDIBFileToOlePresStm(HANDLE hdata, LPSTREAM pstm)
{
	VDATEHEAP();

	HRESULT error;
	HDIBFILEHDR hdfh;
	LPBITMAPFILEHEADER lpbfh;
	LPBITMAPINFOHEADER lpbmi;
		
	if (!(lpbfh = (LPBITMAPFILEHEADER)GlobalLock(hdata)))
		return ResultFromScode(E_OUTOFMEMORY);
		
	lpbmi = (LPBITMAPINFOHEADER)(((BYTE *)lpbfh) +
			sizeof(BITMAPFILEHEADER));

	hdfh.dwCompression = 0;
	// REVIEW, these casts are hosed
	UtGetDibExtents(lpbmi, (LPLONG)&hdfh.dwWidth, (LPLONG)&hdfh.dwHeight);
	
	hdfh.dwSize = lpbfh->bfSize - sizeof(BITMAPFILEHEADER);
	
	// write compesssion, Width, Height, size
	if (error = pstm->Write(&hdfh, sizeof(hdfh), 0))
		goto errRtn;
	
	// write the BITMAPINFOHEADER
	// REVIEW, does this size include the data?
	if ((error = pstm->Write(lpbmi, hdfh.dwSize, NULL)) == NOERROR)
		StSetSize(pstm, 0, TRUE);
	
errRtn: 
	GlobalUnlock(hdata);
	return(error);
}

#endif // _MAC



FARINTERNAL UtHMFToMFStm(HANDLE FAR* lphMF, DWORD dwSize, LPSTREAM lpstream)
{
	VDATEHEAP();

	HRESULT error;
	
	// if there's no handle, there's nothing to do
	if (*lphMF == 0)
		return ResultFromScode(OLE_E_BLANK);
	
#ifdef _MAC

	AssertSz(GetHandleSize((Handle)*lphMF) == dwSize,
			"pic hdl size not correct");
	HLock( (HANDLE)*lphMF );

	error = StWrite(lpstream, * (*lphMF), dwSize);
		
	// Eric: We should be unlocking, right?
	HUnlock((HANDLE)(*lphMF));
		
	if (error != NOERROR)
		AssertSz(0, "StWrite failure" );
		
#else

	HANDLE hBits = NULL;
	void *lpBits;

#ifdef WIN32

	// allocate memory to hold the METAFILE bits
	// Bug 18346 - OLE16 use to get the Handle size of the Metafile which was a METAHEADER bigger than the 
	//   actual Metafile.  Need to write out this much more worth of data so 16 bit dlls can read the Picture.

	dwSize += sizeof(METAHEADER);

	hBits = GlobalAlloc(GPTR, dwSize);
	if (hBits == NULL)
		return ResultFromScode(E_OUTOFMEMORY);

	if (!(lpBits = GlobalLock(hBits)))
		goto errRtn;

	// REVIEW, shouldn't we check the returned size?
	// REVIEW, what should we do about enhanced metafiles?  If we
	// convert and write those out (which have more features that 32 bit
	// apps might use,) then you can't read the same document on a win16
	// machine....
	GetMetaFileBitsEx((HMETAFILE)*lphMF, dwSize, lpBits);

	// write the metafile bits out to the stream
	error = StWrite(lpstream, lpBits, dwSize);

	GlobalUnlock(hBits);

errRtn:
	// free the metafile bits
	GlobalFree(hBits);

#else
	if (!(hBits = GetMetaFileBits(*lphMF)))
	{
		error = ResultFromScode(E_OUTOFMEMORY);
			goto errRtn;
	}

	if (lpBits = GlobalLock(hBits))
	{
		error = StWrite(lpstream, lpBits, dwSize);
		GlobalUnlock(hBits);
	}
	else
		error = ResultFromScode(E_OUTOFMEMORY);

	if (hBits)
		*lphMF = SetMetaFileBits(hBits);
errRtn:

#endif // WIN32
#endif // _MAC

	// set the stream size
	if (error == NOERROR)
		StSetSize(lpstream, 0, TRUE);

	return(error);
}


//+----------------------------------------------------------------------------
//
//      Function:
//              wPreparePlaceableMFHeader, static
//
//      Synopsis:
//              Initializes a PLACEABLEMETAHEADER.
//
//      Arguments:
//              [lpplac_mfh] -- pointer to the PLACEABLEMETAHEADER to initialize
//              [lWidth] -- Width of the metafile
//                      REVIEW, in what units?
//                      REVIEW, why is this not unsigned?
//              [lHeight] -- Height of the metafile
//                      REVIEW, in what units?
//                      REVIEW, why is this not unsigned?
//
//      Notes:
//
//      History:
//              12/08/93 - ChrisWe - made static
//
//-----------------------------------------------------------------------------
static INTERNAL_(void) wPreparePlaceableMFHeader(
		PLACEABLEMETAHEADER FAR* lpplac_mfh, LONG lWidth, LONG lHeight)
{
	VDATEHEAP();

	WORD FAR* lpw; // roves over the words included in the checksum

	lpplac_mfh->key = PMF_KEY;
	lpplac_mfh->hmf = 0;
	lpplac_mfh->inch = 576; // REVIEW, where's this magic number from?
	lpplac_mfh->bbox.left = 0;
	lpplac_mfh->bbox.top = 0;
	lpplac_mfh->bbox.right = (int) ((lWidth * lpplac_mfh->inch) / 2540);
			// REVIEW, more magic
	lpplac_mfh->bbox.bottom = (int) ((lHeight * lpplac_mfh->inch) / 2540);  
			// REVIEW, more magic
	lpplac_mfh->reserved = NULL;
	
	// Compute the checksum of the 10 words that precede the checksum field.
	// It is calculated by XORing zero with those 10 words.
	for(lpplac_mfh->checksum = 0, lpw = (WORD FAR*)lpplac_mfh;
			lpw < (WORD FAR*)&lpplac_mfh->checksum; ++lpw)
		lpplac_mfh->checksum ^= *lpw;
}


FARINTERNAL UtHMFToPlaceableMFStm(HANDLE FAR* lphMF, DWORD dwSize,
		LONG lWidth, LONG lHeight, LPSTREAM pstm)
{
	VDATEHEAP();

	PLACEABLEMETAHEADER plac_mfh;
	HRESULT error;
	
	wPreparePlaceableMFHeader(&plac_mfh, lWidth, lHeight);
	
	// write the placeable header to the stream
	if (error = pstm->Write(&plac_mfh, sizeof(plac_mfh), NULL))
		return(error);
	
	// write the rest of the METAFILE to the stream
	return UtHMFToMFStm(lphMF, dwSize, pstm);       
}


FARINTERNAL UtMFStmToPlaceableMFStm(LPSTREAM pstmMF, DWORD dwSize,
		LONG lWidth, LONG lHeight, LPSTREAM pstmPMF)
{
	VDATEHEAP();

	PLACEABLEMETAHEADER plac_mfh;
	HRESULT error;
	ULARGE_INTEGER ularge_int; // indicates how much data to copy
	
	wPreparePlaceableMFHeader(&plac_mfh, lWidth, lHeight);
	
	// write the placeable header to the stream
	if (error = pstmPMF->Write(&plac_mfh, sizeof(plac_mfh), NULL))
		return(error);

	// copy the METAFILE data from one stream to the other
	ULISet32(ularge_int, dwSize);
	if ((error = pstmMF->CopyTo(pstmPMF, ularge_int, NULL, NULL)) ==
			NOERROR)
		StSetSize(pstmPMF, 0, TRUE);
	
	return(error);
}

//+-------------------------------------------------------------------------
//
//  Function:   UtWriteOlePresStmHeader, private
//
//  Synopsis:   Write the presentation stream header
//
//  Effects:
//
//  Arguments:  [lpstream] -- destination stream
//              [pforetc]  -- FORMATETC for this presentation
//              [dwAdvf]   -- advise flags for the presentation
//
//  Requires:
//
//  Returns:    HRESULT
//
//  Modifies:
//
//  Algorithm:
//
//  History:    11-May-94 AlexT     Added function header, translate to
//                                  ANSI before saving ptd
//
//  Notes:      This function can fail in low memory for presentations with
//              non-NULL ptds (since we allocate memory to do the conversion
//              to the persistent format).  NtIssue #2789
//
//--------------------------------------------------------------------------

FARINTERNAL UtWriteOlePresStmHeader(LPSTREAM lpstream, LPFORMATETC pforetc,
		DWORD dwAdvf)
{
    VDATEHEAP();

    HRESULT error;
    OLEPRESSTMHDR opsh;
	
    // write clip format
    // REVIEW, change name of this function?
    if (error = WriteClipformatStm(lpstream, pforetc->cfFormat))
	return(error);

    // write target device info
    if (pforetc->ptd)
    {
	DVTDINFO dvtdInfo;
	DVTARGETDEVICE *ptdA;

	error = UtGetDvtd32Info(pforetc->ptd, &dvtdInfo);
	if (FAILED(error))
	{
	    return(error);
	}

	ptdA = (DVTARGETDEVICE *) PrivMemAlloc(dvtdInfo.cbConvertSize);
	if (NULL == ptdA)
	{
	    return(E_OUTOFMEMORY);
	}

	error = UtConvertDvtd32toDvtd16(pforetc->ptd, &dvtdInfo, ptdA);

	if (SUCCEEDED(error))
	{
	    error = StWrite(lpstream, ptdA, ptdA->tdSize);
	}

	PrivMemFree(ptdA);

	if (FAILED(error))
	{
	    return(error);
	}
    }
    else
    {
	// if ptd is null then write 4 as size.
	// REVIEW, what is that the sizeof()?
	DWORD dwNullPtdLength = 4;

	if (error = StWrite(lpstream, &dwNullPtdLength, sizeof(DWORD)))
	    return(error);
    }

    opsh.dwAspect = pforetc->dwAspect;
    opsh.dwLindex = pforetc->lindex;
    opsh.dwAdvf = dwAdvf;
	
    // write DVASPECT, lindex, advise flags
    return StWrite(lpstream, &opsh, sizeof(opsh));
}

//+-------------------------------------------------------------------------
//
//  Function:   UtReadOlePresStmHeader
//
//  Synopsis:   Reads in a presentation stream header
//
//  Arguments:  [pstm] -- source stream
//              [pforetc] -- FORMATETC to be filled in
//              [pdwAdvf] -- advise flags to be filled in
//              [pfConvert] -- Mac conversion required, to be filled in
//
//  Requires:
//
//  Returns:    HRESULT
//
//  Modifies:
//
//  Algorithm:
//
//  History:    11-May-94 AlexT     Added function header, translate ptd
//                                  from ANSI when loading
//
//  Notes:
//
//--------------------------------------------------------------------------

FARINTERNAL UtReadOlePresStmHeader(LPSTREAM pstm, LPFORMATETC pforetc,
		DWORD FAR* pdwAdvf, BOOL FAR* pfConvert)
{
    VDATEHEAP();

    HRESULT error;
    DWORD dwRead;
    OLEPRESSTMHDR opsh;

    // initialize this for error return cases
    // Check for NULL ptr, as caller may not need this information

    if (pfConvert)
    {
	*pfConvert = FALSE;
    }

    // there's no target device information yet
    pforetc->ptd = NULL;

    // REVIEW, rename this function to indicate its origin?
    error = ReadClipformatStm(pstm, &dwRead);

    if (error == NOERROR)
	pforetc->cfFormat = (CLIPFORMAT)dwRead;
    else
    {
#ifndef _MAC            
	if (GetScode(error) == OLE_S_MAC_CLIPFORMAT)
	{
	   // check whether the clipformat is "pict"
	   // REVIEW, what's this cuteness?
	   if (dwRead != *((DWORD *)"TCIP"))
		return(error);
	
	   if (pfConvert)
		*pfConvert = TRUE;
	   else
		return ResultFromScode(DV_E_CLIPFORMAT);
	
	   pforetc->cfFormat = CF_METAFILEPICT;
	}       
	else    
#endif          
	    return(error);
    }
	
    // set the proper tymed
    if (pforetc->cfFormat == CF_METAFILEPICT)
    {
	pforetc->tymed = TYMED_MFPICT;
    }
    else if (pforetc->cfFormat == CF_ENHMETAFILE)
    {
	pforetc->tymed = TYMED_ENHMF;
    }
    else if (pforetc->cfFormat == CF_BITMAP)
    {
	AssertSz(0, "We don't read/save CF_BITMAP anymore");
	return ResultFromScode(DV_E_CLIPFORMAT);
    }           
    else if (pforetc->cfFormat == NULL)
    {
	pforetc->tymed = TYMED_NULL;
    }
    else
    {
	pforetc->tymed = TYMED_HGLOBAL;
    }

    // Read targetdevice info.
    if (error = StRead(pstm, &dwRead, sizeof(dwRead)))
	return(error);

    // if the tdSize of ptd is non-null and is > 4, then go ahead read the
    // remaining data of the target device info
    if (dwRead > 4)
    {
	DVTARGETDEVICE *ptdA;
	DVTDINFO dvtdInfo;

	ptdA = (DVTARGETDEVICE *) PrivMemAlloc(dwRead);
	if (NULL == ptdA)
	{
	    return ResultFromScode(E_OUTOFMEMORY);
	}

	ptdA->tdSize = dwRead;
	error = StRead(pstm, ((BYTE*)ptdA) + sizeof(dwRead),
		       dwRead - sizeof(dwRead));

	if (SUCCEEDED(error))
	{
	    error = UtGetDvtd16Info(ptdA, &dvtdInfo);
	    if (SUCCEEDED(error))
	    {
		pforetc->ptd = (DVTARGETDEVICE *) PubMemAlloc(dvtdInfo.cbConvertSize);
		if (NULL == pforetc->ptd)
		{
		    error = E_OUTOFMEMORY;
		}
		else
		{
		    error = UtConvertDvtd16toDvtd32(ptdA, &dvtdInfo, pforetc->ptd);
		}
	    }
	}

	PrivMemFree(ptdA);

	if (FAILED(error))
	{
	    goto errRtn;
	}
    }
    else
	pforetc->ptd = NULL;

    // Read DVASPECT, lindex, advise flags
    if ((error = StRead(pstm, &opsh, sizeof(opsh))) != NOERROR)
	goto errRtn;

    pforetc->dwAspect = opsh.dwAspect;
    pforetc->lindex = opsh.dwLindex;
    if (pdwAdvf)
	*pdwAdvf = opsh.dwAdvf;

    return NOERROR;

errRtn:
    if (pforetc->ptd)
    {
	PubMemFree(pforetc->ptd);
	pforetc->ptd = NULL;            
    }

    return(error);
}


FARINTERNAL UtOlePresStmToContentsStm(LPSTORAGE pstg, LPOLESTR lpszPresStm,
		BOOL fDeletePresStm, UINT FAR* puiStatus)
{
	VDATEHEAP();

	LPSTREAM pstmOlePres;
	LPSTREAM pstmContents = NULL;
	HRESULT error;
	FORMATETC foretc;
	HDIBFILEHDR hdfh;
	
	// there's no status yet
	*puiStatus = 0;

	// POSTPPC:
	//
	// This function needs to be rewritten to correctly handle the case described in 
	// the comments below (rather than just skipping out of the function if the contents
	// stream already exists).  The best course of action will probably be to convert
	// DIBs->Metafiles and Metafiles->DIBs in the needed cases.

	// The code inside the #ifdef below is used to determine if the contents
	// stream has already been created (which is the case for an object that has
	// been converted to a bitmap) because in the case of an object that has been
	// converted to a static DIB and the object has a METAFILE presentation stream
	// we already have a cachenode created as a DIB and we will read the contents
	// stream after this call to get the DIB data.  However, this function sees
	// the metafile presentation, and converts it into the contents stream (which
	// when then try to load as a DIB) and bad things happen (it doesn't work).  If
	// the stream already exists, then we bail out of this function.
	if (pstg->CreateStream(OLE_CONTENTS_STREAM,(STGM_READWRITE | STGM_SHARE_EXCLUSIVE), NULL,
			 0, &pstmContents) != NOERROR)
	{
	    return NOERROR;
	}
        
	// created stream, it must not have existed
        pstmContents->Release();
	pstg->DestroyElement(OLE_CONTENTS_STREAM);
	
	if ((error = pstg->OpenStream(lpszPresStm, NULL,
			(STGM_READ | STGM_SHARE_EXCLUSIVE), 0, &pstmOlePres)) !=
			NOERROR)
	{
		// we can't open the source stream
		*puiStatus |= CONVERT_NOSOURCE;
		
		// check whether "CONTENTS" stream exits
		if (pstg->OpenStream(OLE_CONTENTS_STREAM, NULL,
				(STGM_READ | STGM_SHARE_EXCLUSIVE), 0,
				&pstmContents) != NOERROR)
		{
			// we can't open the destination stream either
			// REVIEW, since we can't open the source, who cares?
			// REVIEW, is there a cheaper way to test existence
			// other than opening?
			*puiStatus |= CONVERT_NODESTINATION;
		}
		else
			pstmContents->Release();
		
		return(error);
	}
	
	foretc.ptd = NULL;
	if (error = UtReadOlePresStmHeader(pstmOlePres, &foretc, NULL, NULL))
		goto errRtn;
			
	if (error = pstmOlePres->Read(&hdfh, sizeof(hdfh), 0))
		goto errRtn;
			
	AssertSz(hdfh.dwCompression == 0,
			"Non-zero compression not supported");
	
	if (error = OpenOrCreateStream(pstg, OLE_CONTENTS_STREAM,
			&pstmContents))
	{
		*puiStatus |= CONVERT_NODESTINATION;            
		goto errRtn;
	}
	
	if (foretc.dwAspect == DVASPECT_ICON)
	{
		*puiStatus |= CONVERT_SOURCEISICON;                             
		fDeletePresStm = FALSE;
		error = NOERROR;
		goto errRtn;
	}
	
	if (foretc.cfFormat == CF_DIB)
		error = UtDIBStmToDIBFileStm(pstmOlePres, hdfh.dwSize,
				pstmContents);
	else if (foretc.cfFormat == CF_METAFILEPICT)
		error = UtMFStmToPlaceableMFStm(pstmOlePres,
				hdfh.dwSize, hdfh.dwWidth, hdfh.dwHeight,
				pstmContents);
	else
		error = ResultFromScode(DV_E_CLIPFORMAT);
	
errRtn:
	if (pstmOlePres)
		pstmOlePres->Release();
			
	if (pstmContents)
		pstmContents->Release();

	if (foretc.ptd)
		PubMemFree(foretc.ptd);

	if (error == NOERROR)
	{
		if (fDeletePresStm && lpszPresStm)                              
			pstg->DestroyElement(lpszPresStm);
	}
	else
	{
		pstg->DestroyElement(OLE_CONTENTS_STREAM);
	}

	return(error);
}       


FARINTERNAL_(HANDLE) UtGetHPRESFromNative(LPSTORAGE pstg, CLIPFORMAT cfFormat,
		BOOL fOle10Native)
{
	VDATEHEAP();

	LPSTREAM pstm;
	HGLOBAL hdata = NULL;
	
	if ((cfFormat != CF_METAFILEPICT) && 
	    (cfFormat != CF_DIB) &&
	    (cfFormat != CF_ENHMETAFILE))
	{
		return(NULL);
	}

	if (fOle10Native)
	{
		DWORD dwSize;

		if (pstg->OpenStream(OLE10_NATIVE_STREAM, NULL,
				(STGM_READ | STGM_SHARE_EXCLUSIVE), 0,
				&pstm) != NOERROR)
			return(NULL);
		
		if (pstm->Read(&dwSize, sizeof(DWORD), NULL) == NOERROR)
		{
			// is it PBrush native data?
			if (cfFormat == CF_DIB)
				UtGetHDIBFromDIBFileStm(pstm, &hdata);
			else
			{
				// MSDraw native data or PaintBrush
				//
				UtGetHMFPICTFromMSDrawNativeStm(pstm, dwSize,
						&hdata);
			}
		}
	}
	else
	{
		if (pstg->OpenStream(OLE_CONTENTS_STREAM, NULL,
				(STGM_READ | STGM_SHARE_EXCLUSIVE), 0,
				&pstm) != NOERROR)
			return(NULL);
		
		if (cfFormat == CF_DIB)
		{
			UtGetHDIBFromDIBFileStm(pstm, &hdata);
		}
		else if (cfFormat == CF_METAFILEPICT)
		{
			UtGetHMFPICTFromPlaceableMFStm(pstm, &hdata);
		}
		else
		{
			UtGetHEMFFromContentsStm(pstm, &hdata);
		}
	}
	
	pstm->Release();
	return(hdata);
}