< prev index next >

src/java.desktop/share/native/liblcms/cmserr.c

Print this page

 84 
 85     p = ftell(f); // register current file position
 86     if (p == -1L)
 87         return -1L;
 88 
 89     if (fseek(f, 0, SEEK_END) != 0) {
 90         return -1L;
 91     }
 92 
 93     n = ftell(f);
 94     fseek(f, p, SEEK_SET); // file position restored
 95 
 96     return n;
 97 }
 98 
 99 
100 // Memory handling ------------------------------------------------------------------
101 //
102 // This is the interface to low-level memory management routines. By default a simple
103 // wrapping to malloc/free/realloc is provided, although there is a limit on the max
104 // amount of memoy that can be reclaimed. This is mostly as a safety feature to prevent
105 // bogus or evil code to allocate huge blocks that otherwise lcms would never need.
106 
107 #define MAX_MEMORY_FOR_ALLOC  ((cmsUInt32Number)(1024U*1024U*512U))
108 
109 // User may override this behaviour by using a memory plug-in, which basically replaces
110 // the default memory management functions. In this case, no check is performed and it
111 // is up to the plug-in writer to keep in the safe side. There are only three functions
112 // required to be implemented: malloc, realloc and free, although the user may want to
113 // replace the optional mallocZero, calloc and dup as well.
114 
115 cmsBool   _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
116 
117 // *********************************************************************************
118 
119 // This is the default memory allocation function. It does a very coarse
120 // check of amount of memory, just to prevent exploits
121 static
122 void* _cmsMallocDefaultFn(cmsContext ContextID, cmsUInt32Number size)
123 {
124     if (size > MAX_MEMORY_FOR_ALLOC) return NULL;  // Never allow over maximum

125 
126     return (void*) malloc(size);
127 
128     cmsUNUSED_PARAMETER(ContextID);
129 }
130 
131 // Generic allocate & zero
132 static
133 void* _cmsMallocZeroDefaultFn(cmsContext ContextID, cmsUInt32Number size)
134 {
135     void *pt = _cmsMalloc(ContextID, size);
136     if (pt == NULL) return NULL;
137 
138     memset(pt, 0, size);
139     return pt;
140 }
141 
142 
143 // The default free function. The only check proformed is against NULL pointers
144 static

246         ptr ->MallocZeroPtr= _cmsMallocZeroDefaultFn;
247         ptr ->CallocPtr    = _cmsCallocDefaultFn;
248         ptr ->DupPtr       = _cmsDupDefaultFn;
249 
250         if (Plugin ->MallocZeroPtr != NULL) ptr ->MallocZeroPtr = Plugin -> MallocZeroPtr;
251         if (Plugin ->CallocPtr != NULL)     ptr ->CallocPtr     = Plugin -> CallocPtr;
252         if (Plugin ->DupPtr != NULL)        ptr ->DupPtr        = Plugin -> DupPtr;
253 
254     }
255 }
256 
257 
258 // Plug-in replacement entry
259 cmsBool  _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase *Data)
260 {
261     cmsPluginMemHandler* Plugin = (cmsPluginMemHandler*) Data;
262     _cmsMemPluginChunkType* ptr;
263 
264     // NULL forces to reset to defaults. In this special case, the defaults are stored in the context structure.
265     // Remaining plug-ins does NOT have any copy in the context structure, but this is somehow special as the
266     // context internal data should be malloce'd by using those functions.
267     if (Data == NULL) {
268 
269        struct _cmsContext_struct* ctx = ( struct _cmsContext_struct*) ContextID;
270 
271        // Return to the default allocators
272         if (ContextID != NULL) {
273             ctx->chunks[MemPlugin] = (void*) &ctx->DefaultMemoryManager;
274         }
275         return TRUE;
276     }
277 
278     // Check for required callbacks
279     if (Plugin -> MallocPtr == NULL ||
280         Plugin -> FreePtr == NULL ||
281         Plugin -> ReallocPtr == NULL) return FALSE;
282 
283     // Set replacement functions
284     ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
285     if (ptr == NULL)
286         return FALSE;

 84 
 85     p = ftell(f); // register current file position
 86     if (p == -1L)
 87         return -1L;
 88 
 89     if (fseek(f, 0, SEEK_END) != 0) {
 90         return -1L;
 91     }
 92 
 93     n = ftell(f);
 94     fseek(f, p, SEEK_SET); // file position restored
 95 
 96     return n;
 97 }
 98 
 99 
100 // Memory handling ------------------------------------------------------------------
101 //
102 // This is the interface to low-level memory management routines. By default a simple
103 // wrapping to malloc/free/realloc is provided, although there is a limit on the max
104 // amount of memory that can be reclaimed. This is mostly as a safety feature to prevent
105 // bogus or evil code to allocate huge blocks that otherwise lcms would never need.
106 
107 #define MAX_MEMORY_FOR_ALLOC  ((cmsUInt32Number)(1024U*1024U*512U))
108 
109 // User may override this behaviour by using a memory plug-in, which basically replaces
110 // the default memory management functions. In this case, no check is performed and it
111 // is up to the plug-in writer to keep in the safe side. There are only three functions
112 // required to be implemented: malloc, realloc and free, although the user may want to
113 // replace the optional mallocZero, calloc and dup as well.
114 
115 cmsBool   _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase* Plugin);
116 
117 // *********************************************************************************
118 
119 // This is the default memory allocation function. It does a very coarse
120 // check of amount of memory, just to prevent exploits
121 static
122 void* _cmsMallocDefaultFn(cmsContext ContextID, cmsUInt32Number size)
123 {
124     // Never allow 0 or over maximum
125     if (size == 0 || size > MAX_MEMORY_FOR_ALLOC) return NULL;
126 
127     return (void*) malloc(size);
128 
129     cmsUNUSED_PARAMETER(ContextID);
130 }
131 
132 // Generic allocate & zero
133 static
134 void* _cmsMallocZeroDefaultFn(cmsContext ContextID, cmsUInt32Number size)
135 {
136     void *pt = _cmsMalloc(ContextID, size);
137     if (pt == NULL) return NULL;
138 
139     memset(pt, 0, size);
140     return pt;
141 }
142 
143 
144 // The default free function. The only check proformed is against NULL pointers
145 static

247         ptr ->MallocZeroPtr= _cmsMallocZeroDefaultFn;
248         ptr ->CallocPtr    = _cmsCallocDefaultFn;
249         ptr ->DupPtr       = _cmsDupDefaultFn;
250 
251         if (Plugin ->MallocZeroPtr != NULL) ptr ->MallocZeroPtr = Plugin -> MallocZeroPtr;
252         if (Plugin ->CallocPtr != NULL)     ptr ->CallocPtr     = Plugin -> CallocPtr;
253         if (Plugin ->DupPtr != NULL)        ptr ->DupPtr        = Plugin -> DupPtr;
254 
255     }
256 }
257 
258 
259 // Plug-in replacement entry
260 cmsBool  _cmsRegisterMemHandlerPlugin(cmsContext ContextID, cmsPluginBase *Data)
261 {
262     cmsPluginMemHandler* Plugin = (cmsPluginMemHandler*) Data;
263     _cmsMemPluginChunkType* ptr;
264 
265     // NULL forces to reset to defaults. In this special case, the defaults are stored in the context structure.
266     // Remaining plug-ins does NOT have any copy in the context structure, but this is somehow special as the
267     // context internal data should be malloc'ed by using those functions.
268     if (Data == NULL) {
269 
270        struct _cmsContext_struct* ctx = ( struct _cmsContext_struct*) ContextID;
271 
272        // Return to the default allocators
273         if (ContextID != NULL) {
274             ctx->chunks[MemPlugin] = (void*) &ctx->DefaultMemoryManager;
275         }
276         return TRUE;
277     }
278 
279     // Check for required callbacks
280     if (Plugin -> MallocPtr == NULL ||
281         Plugin -> FreePtr == NULL ||
282         Plugin -> ReallocPtr == NULL) return FALSE;
283 
284     // Set replacement functions
285     ptr = (_cmsMemPluginChunkType*) _cmsContextGetClientChunk(ContextID, MemPlugin);
286     if (ptr == NULL)
287         return FALSE;
< prev index next >