uemf_safe.c revision a1abfc12cd890270e97e668d603c7705f289828f
/**
@file uemf_safe.c
@brief Functions for checking EMF records for memory issues.
EMF records come in a variety of sizes, and some types have variable sizes.
These functions check the record types and report if there are any issues
that could cause a memory access problem. All counts and offsets are examined
and the data structure checked so that no referenced byte is outside of the
declared size of the record.
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_safe.c
Version: 0.0.4
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 <stddef.h> /* for offsetof() macro */
#include "uemf.h"
#include "uemf_endian.h" // for u_emf_record_sizeok
// hide almost everuything in here from Doxygen
//! \cond
/**
\brief Test a U_EXTLOGPEN object.
\param elp PU_EXTLOGPEN object
\param blimit one byte past the end of the record
*/
int extlogpen_safe(
const char *blimit
){
return(1);
}
/**
\brief Test 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.
*/
int emrtext_safe(
const char *record,
const char *blimit
){
int off;
if(!(fOptions & U_ETO_NO_RECT)){
}
return(1);
}
/**
\return 1 on success, 0 on failure
\brief Test a U_RGNDATA object.
\param rd pointer to a U_RGNDATA object.
\param cbRgnData size of the U_RGNDATA object.
*/
int rgndata_safe(
int cbRgnData
){
return(1);
}
/**
\return 1 on success, 0 on failure
\brief Test a U_BITMAPINFO object.
\param Bmi pointer to a U_BITMAPINFO object.
\param blimit one byte past the end of the record.
*/
int bitmapinfo_safe(
const char *Bmi,
const char *blimit
){
int ClrUsed;
if(IS_MEM_UNSAFE(Bmi, offsetof(U_BITMAPINFO,bmiHeader) + sizeof(U_BITMAPINFOHEADER), blimit))return(0);
if(ClrUsed && IS_MEM_UNSAFE(Bmi, offsetof(U_BITMAPINFO,bmiColors) + ClrUsed*sizeof(U_RGBQUAD), blimit))return(0);
return(1);
}
/**
\brief Check that the bitmap in the specified DIB is compatible with the record size
\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.
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_safe(
const char *record,
const char *blimit
){
int bs;
int usedbytes;
if(iUsage == U_DIB_RGB_COLORS){
// next call returns pointers and values, but allocates no memory
// sanity checking
// this is the only DIB type where we can calculate how big it should be when stored in the EMF file
if(bs<1){
}
else {
}
}
}
return(1);
}
/* **********************************************************************************************
These functions contain shared code used by various U_EMR*_safe 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_safe(const char *record){
but some do not actually use torev.
*********************************************************************************************** */
// all core*_safe call this, U_EMRSETMARGN_safe 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
// sizeof(U_ENHMETARECORD) bytes in the record
return(1);
}
// Functions with the same form starting with U_EMRPOLYBEZIER_safe
int core1_safe(const char *record){
return(1);
}
// Functions with the same form starting with U_EMRPOLYPOLYLINE_safe
int core2_safe(const char *record){
return(1);
}
// Functions with the same form starting with U_EMRSETMAPMODE_safe
int core3_safe(const char *record){
return(1);
}
// Functions taking a single U_RECT or U_RECTL, starting with U_EMRELLIPSE_safe, also U_EMRFILLPATH_safe,
int core4_safe(const char *record){
return(1);
}
// Functions with the same form starting with U_EMRPOLYBEZIER16_safe
int core6_safe(const char *record){
return(1);
}
// Records with the same form starting with U_EMRSETWINDOWEXTEX_safe, that is, all with two uint32_t values after the emr
int core7_safe(const char *record){
return(1);
}
// For U_EMREXTTEXTOUTA and U_EMREXTTEXTOUTW, type=0 for the first one
int core8_safe(const char *record){
return(1);
}
// Functions that take a rect and a pair of points, starting with U_EMRARC_safe
int core9_safe(const char *record){
return(1);
}
// Functions with the same form starting with U_EMRPOLYPOLYLINE16_safe
int core10_safe(const char *record){
return(1);
}
// Functions with the same form starting with U_EMRINVERTRGN_safe and U_EMRPAINTRGN_safe,
int core11_safe(const char *record){
}
// common code for U_EMRCREATEMONOBRUSH_safe and U_EMRCREATEDIBPATTERNBRUSHPT_safe,
int core12_safe(const char *record){
}
// common code for U_EMRALPHABLEND_safe and U_EMRTRANSPARENTBLT_safe,
int core13_safe(const char *record){
}
/* **********************************************************************************************
These are the core EMR_safe 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_safe(const char *record){
fprintf(stderr,"EMF WARNING: could not safety check record because that type has not been implemented!\n");
}
// U_EMRHEADER 1
int U_EMRHEADER_safe(const char *record){
// use _MIN form so that it accepts very old EMF files
}
// U_EMRPOLYBEZIER 2
int U_EMRPOLYBEZIER_safe(const char *record){
return(core1_safe(record));
}
// U_EMRPOLYGON 3
int U_EMRPOLYGON_safe(const char *record){
return(core1_safe(record));
}
// U_EMRPOLYLINE 4
int U_EMRPOLYLINE_safe(const char *record){
return(core1_safe(record));
}
// U_EMRPOLYBEZIERTO 5
int U_EMRPOLYBEZIERTO_safe(const char *record){
return(core1_safe(record));
}
// U_EMRPOLYLINETO 6
int U_EMRPOLYLINETO_safe(const char *record){
return(core1_safe(record));
}
// U_EMRPOLYPOLYLINE 7
int U_EMRPOLYPOLYLINE_safe(const char *record){
return(core2_safe(record));
}
// U_EMRPOLYPOLYGON 8
int U_EMRPOLYPOLYGON_safe(const char *record){
return(core2_safe(record));
}
// U_EMRSETWINDOWEXTEX 9
int U_EMRSETWINDOWEXTEX_safe(const char *record){
return(core7_safe(record));
}
// U_EMRSETWINDOWORGEX 10
int U_EMRSETWINDOWORGEX_safe(const char *record){
return(core7_safe(record));
}
// U_EMRSETVIEWPORTEXTEX 11
int U_EMRSETVIEWPORTEXTEX_safe(const char *record){
return(core7_safe(record));
}
// U_EMRSETVIEWPORTORGEX 12
int U_EMRSETVIEWPORTORGEX_safe(const char *record){
return(core7_safe(record));
}
// U_EMRSETBRUSHORGEX 13
int U_EMRSETBRUSHORGEX_safe(const char *record){
return(core7_safe(record));
}
// U_EMREOF 14
int U_EMREOF_safe(const char *record){
if(cbPalEntries){
if(IS_MEM_UNSAFE(record, pEmr->offPalEntries + 2*2, blimit))return(0);// 2 16 bit values in U_LOGPALLETE
}
return(1);
}
// U_EMRSETPIXELV 15
int U_EMRSETPIXELV_safe(const char *record){
}
// U_EMRSETMAPPERFLAGS 16
int U_EMRSETMAPPERFLAGS_safe(const char *record){
}
// U_EMRSETMAPMODE 17
int U_EMRSETMAPMODE_safe(const char *record){
return(core3_safe(record));
}
// U_EMRSETBKMODE 18
int U_EMRSETBKMODE_safe(const char *record){
return(core3_safe(record));
}
// U_EMRSETPOLYFILLMODE 19
int U_EMRSETPOLYFILLMODE_safe(const char *record){
return(core3_safe(record));
}
// U_EMRSETROP2 20
int U_EMRSETROP2_safe(const char *record){
return(core3_safe(record));
}
// U_EMRSETSTRETCHBLTMODE 21
int U_EMRSETSTRETCHBLTMODE_safe(const char *record){
return(core3_safe(record));
}
// U_EMRSETTEXTALIGN 22
int U_EMRSETTEXTALIGN_safe(const char *record){
return(core3_safe(record));
}
// U_EMRSETCOLORADJUSTMENT 23
int U_EMRSETCOLORADJUSTMENT_safe(const char *record){
}
// U_EMRSETTEXTCOLOR 24
int U_EMRSETTEXTCOLOR_safe(const char *record){
}
// U_EMRSETBKCOLOR 25
int U_EMRSETBKCOLOR_safe(const char *record){
}
// U_EMROFFSETCLIPRGN 26
int U_EMROFFSETCLIPRGN_safe(const char *record){
return(core7_safe(record));
}
// U_EMRMOVETOEX 27
int U_EMRMOVETOEX_safe(const char *record){
return(core7_safe(record));
}
// U_EMRSETMETARGN 28
int U_EMRSETMETARGN_safe(const char *record){
}
// U_EMREXCLUDECLIPRECT 29
int U_EMREXCLUDECLIPRECT_safe(const char *record){
return(core4_safe(record));
}
// U_EMRINTERSECTCLIPRECT 30
int U_EMRINTERSECTCLIPRECT_safe(const char *record){
return(core4_safe(record));
}
// U_EMRSCALEVIEWPORTEXTEX 31
int U_EMRSCALEVIEWPORTEXTEX_safe(const char *record){
return(core4_safe(record));
}
// U_EMRSCALEWINDOWEXTEX 32
int U_EMRSCALEWINDOWEXTEX_safe(const char *record){
return(core4_safe(record));
}
// U_EMRSAVEDC 33
int U_EMRSAVEDC_safe(const char *record){
}
// U_EMRRESTOREDC 34
int U_EMRRESTOREDC_safe(const char *record){
return(core3_safe(record));
}
// U_EMRSETWORLDTRANSFORM 35
int U_EMRSETWORLDTRANSFORM_safe(const char *record){
}
// U_EMRMODIFYWORLDTRANSFORM 36
int U_EMRMODIFYWORLDTRANSFORM_safe(const char *record){
}
// U_EMRSELECTOBJECT 37
int U_EMRSELECTOBJECT_safe(const char *record){
}
// U_EMRCREATEPEN 38
int U_EMRCREATEPEN_safe(const char *record){
}
// U_EMRCREATEBRUSHINDIRECT 39
int U_EMRCREATEBRUSHINDIRECT_safe(const char *record){
}
// U_EMRDELETEOBJECT 40
int U_EMRDELETEOBJECT_safe(const char *record){
}
// U_EMRANGLEARC 41
int U_EMRANGLEARC_safe(const char *record){
}
// U_EMRELLIPSE 42
int U_EMRELLIPSE_safe(const char *record){
return(core4_safe(record));
}
// U_EMRRECTANGLE 43
int U_EMRRECTANGLE_safe(const char *record){
return(core4_safe(record));
}
// U_EMRROUNDRECT 44
int U_EMRROUNDRECT_safe(const char *record){
}
// U_EMRARC 45
int U_EMRARC_safe(const char *record){
return(core9_safe(record));
}
// U_EMRCHORD 46
int U_EMRCHORD_safe(const char *record){
return(core9_safe(record));
}
// U_EMRPIE 47
int U_EMRPIE_safe(const char *record){
return(core9_safe(record));
}
// U_EMRSELECTPALETTE 48
int U_EMRSELECTPALETTE_safe(const char *record){
return(core3_safe(record));
}
// U_EMRCREATEPALETTE 49
int U_EMRCREATEPALETTE_safe(const char *record){
}
// U_EMRSETPALETTEENTRIES 50
int U_EMRSETPALETTEENTRIES_safe(const char *record){
}
// U_EMRRESIZEPALETTE 51
int U_EMRRESIZEPALETTE_safe(const char *record){
return(core7_safe(record));
}
// U_EMRREALIZEPALETTE 52
int U_EMRREALIZEPALETTE_safe(const char *record){
}
// U_EMREXTFLOODFILL 53
int U_EMREXTFLOODFILL_safe(const char *record){
}
// U_EMRLINETO 54
int U_EMRLINETO_safe(const char *record){
return(core7_safe(record));
}
// U_EMRARCTO 55
int U_EMRARCTO_safe(const char *record){
return(core9_safe(record));
}
// U_EMRPOLYDRAW 56
int U_EMRPOLYDRAW_safe(const char *record){
return(1);
}
// U_EMRSETARCDIRECTION 57
int U_EMRSETARCDIRECTION_safe(const char *record){
return(core3_safe(record));
}
// U_EMRSETMITERLIMIT 58
int U_EMRSETMITERLIMIT_safe(const char *record){
return(core3_safe(record));
}
// U_EMRBEGINPATH 59
int U_EMRBEGINPATH_safe(const char *record){
}
// U_EMRENDPATH 60
int U_EMRENDPATH_safe(const char *record){
}
// U_EMRCLOSEFIGURE 61
int U_EMRCLOSEFIGURE_safe(const char *record){
}
// U_EMRFILLPATH 62
int U_EMRFILLPATH_safe(const char *record){
return(core4_safe(record));
}
// U_EMRSTROKEANDFILLPATH 63
int U_EMRSTROKEANDFILLPATH_safe(const char *record){
return(core4_safe(record));
}
// U_EMRSTROKEPATH 64
int U_EMRSTROKEPATH_safe(const char *record){
return(core4_safe(record));
}
// U_EMRFLATTENPATH 65
int U_EMRFLATTENPATH_safe(const char *record){
}
// U_EMRWIDENPATH 66
int U_EMRWIDENPATH_safe(const char *record){
}
// U_EMRSELECTCLIPPATH 67
int U_EMRSELECTCLIPPATH_safe(const char *record){
return(core3_safe(record));
}
// U_EMRABORTPATH 68
int U_EMRABORTPATH_safe(const char *record){
}
// U_EMRUNDEF69 69
// U_EMRCOMMENT 70 Comment (any binary data, interpretation is program specific)
int U_EMRCOMMENT_safe(const char *record){
return(1);
}
// U_EMRFILLRGN 71
int U_EMRFILLRGN_safe(const char *record){
}
// U_EMRFRAMERGN 72
int U_EMRFRAMERGN_safe(const char *record){
}
// U_EMRINVERTRGN 73
int U_EMRINVERTRGN_safe(const char *record){
return(core11_safe(record));
}
// U_EMRPAINTRGN 74
int U_EMRPAINTRGN_safe(const char *record){
return(core11_safe(record));
}
// U_EMREXTSELECTCLIPRGN 75
int U_EMREXTSELECTCLIPRGN_safe(const char *record){
}
// U_EMRBITBLT 76
int U_EMRBITBLT_safe(const char *record){
}
// U_EMRSTRETCHBLT 77
int U_EMRSTRETCHBLT_safe(const char *record){
}
// U_EMRMASKBLT 78
int U_EMRMASKBLT_safe(const char *record){
}
// U_EMRPLGBLT 79
int U_EMRPLGBLT_safe(const char *record){
}
// U_EMRSETDIBITSTODEVICE 80
int U_EMRSETDIBITSTODEVICE_safe(const char *record){
}
// U_EMRSTRETCHDIBITS 81
int U_EMRSTRETCHDIBITS_safe(const char *record){
}
// U_EMREXTCREATEFONTINDIRECTW 82
int U_EMREXTCREATEFONTINDIRECTW_safe(const char *record){
/* Panose or logfont, LogFontExDv is not supported. Test smallest to largest */
}
// U_EMREXTTEXTOUTA 83
int U_EMREXTTEXTOUTA_safe(const char *record){
return(core8_safe(record));
}
// U_EMREXTTEXTOUTW 84
int U_EMREXTTEXTOUTW_safe(const char *record){
return(core8_safe(record));
}
// U_EMRPOLYBEZIER16 85
/**
\brief Convert a pointer to a U_EMR_POLYBEZIER16 record.
\param record pointer to a buffer holding the EMR record
*/
int U_EMRPOLYBEZIER16_safe(const char *record){
return(core6_safe(record));
}
// U_EMRPOLYGON16 86
int U_EMRPOLYGON16_safe(const char *record){
return(core6_safe(record));
}
// U_EMRPOLYLINE16 87
int U_EMRPOLYLINE16_safe(const char *record){
return(core6_safe(record));
}
// U_EMRPOLYBEZIERTO16 88
int U_EMRPOLYBEZIERTO16_safe(const char *record){
return(core6_safe(record));
}
// U_EMRPOLYLINETO16 89
/**
\brief Convert a pointer to a U_EMR_POLYLINETO16 record.
\param record pointer to a buffer holding the EMR record
*/
int U_EMRPOLYLINETO16_safe(const char *record){
return(core6_safe(record));
}
// U_EMRPOLYPOLYLINE16 90
int U_EMRPOLYPOLYLINE16_safe(const char *record){
return(core10_safe(record));
}
// U_EMRPOLYPOLYGON16 91
int U_EMRPOLYPOLYGON16_safe(const char *record){
return(core10_safe(record));
}
// U_EMRPOLYDRAW16 92
int U_EMRPOLYDRAW16_safe(const char *record){
return(1);
}
// U_EMRCREATEMONOBRUSH 93
int U_EMRCREATEMONOBRUSH_safe(const char *record){
return(core12_safe(record));
}
// U_EMRCREATEDIBPATTERNBRUSHPT_safe 94
int U_EMRCREATEDIBPATTERNBRUSHPT_safe(const char *record){
return(core12_safe(record));
}
// U_EMREXTCREATEPEN 95
int U_EMREXTCREATEPEN_safe(const char *record){
}
// U_EMRPOLYTEXTOUTA 96 NOT IMPLEMENTED, denigrated after Windows NT
// U_EMRPOLYTEXTOUTW 97 NOT IMPLEMENTED, denigrated after Windows NT
// U_EMRSETICMMODE 98
int U_EMRSETICMMODE_safe(const char *record){
return(core3_safe(record));
}
// U_EMRCREATECOLORSPACE 99
int U_EMRCREATECOLORSPACE_safe(const char *record){
}
// U_EMRSETCOLORSPACE 100
int U_EMRSETCOLORSPACE_safe(const char *record){
return(core3_safe(record));
}
// U_EMRDELETECOLORSPACE 101
int U_EMRDELETECOLORSPACE_safe(const char *record){
return(core3_safe(record));
}
// U_EMRGLSRECORD 102 Not implemented
// U_EMRGLSBOUNDEDRECORD 103 Not implemented
// U_EMRPIXELFORMAT 104
int U_EMRPIXELFORMAT_safe(const char *record){
}
// U_EMRDRAWESCAPE 105 Not implemented
// U_EMREXTESCAPE 106 Not implemented
// U_EMRUNDEF107 107 Not implemented
// U_EMRSMALLTEXTOUT 108
int U_EMRSMALLTEXTOUT_safe(const char *record){
if(!(fuOptions & U_ETO_NO_RECT)){
}
return(1);
}
// U_EMRFORCEUFIMAPPING 109 Not implemented
// U_EMRNAMEDESCAPE 110 Not implemented
// U_EMRCOLORCORRECTPALETTE 111 Not implemented
// U_EMRSETICMPROFILEA 112 Not implemented
// U_EMRSETICMPROFILEW 113 Not implemented
// U_EMRALPHABLEND 114
int U_EMRALPHABLEND_safe(const char *record){
return(core13_safe(record));
}
// U_EMRSETLAYOUT 115
int U_EMRSETLAYOUT_safe(const char *record){
return(core3_safe(record));
}
// U_EMRTRANSPARENTBLT 116
int U_EMRTRANSPARENTBLT_safe(const char *record){
return(core13_safe(record));
}
// U_EMRUNDEF117 117 Not implemented
// U_EMRGRADIENTFILL 118
int U_EMRGRADIENTFILL_safe(const char *record){
if(nGradObj){
if( ulMode == U_GRADIENT_FILL_TRIANGLE){
}
}
}
return(1);
}
// U_EMRSETLINKEDUFIS 119 Not implemented
// U_EMRSETTEXTJUSTIFICATION120 Not implemented (denigrated)
// U_EMRCOLORMATCHTOTARGETW 121 Not implemented
// U_EMRCREATECOLORSPACEW 122
int U_EMRCREATECOLORSPACEW_safe(const char *record){
}
//! \endcond
/**
\brief Test an EMF record in memory from Big Endian to Little Endian.
\return 0 on failure, 1 on success
\param record pointer to the EMF record in memory
Normally this would be called immediately after reading a record from a file
and having called U_emf_record_sizeok().
It is NOT safe to call this routine without first calling U_emf_record_sizeok)()!
If the file has been converted from one endian to another calling this routine is
not necessary, because those routines also perform these checks.
*/
int U_emf_record_safe(const char *record){
int rstatus=1;
if(!record)return(0); // programming error
{
} //end of switch
return(rstatus);
}
#ifdef __cplusplus
}
#endif