1 /* Copyright (C) 2001-2019 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 #if !defined(SHARE_JPEG) || SHARE_JPEG==0
17 
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jerror.h"
21 #include "jmemcust.h"
22 
23 GLOBAL(void *)
jpeg_get_small(j_common_ptr cinfo,size_t sizeofobject)24 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
25 {
26   jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
27 
28   return (void *) (cmem->j_mem_get_small)(cinfo, sizeofobject);
29 }
30 
31 GLOBAL(void)
jpeg_free_small(j_common_ptr cinfo,void * object,size_t sizeofobject)32 jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
33 {
34   jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
35 
36   (cmem->j_mem_free_small)(cinfo, object, sizeofobject);
37 }
38 
39 /*
40  * "Large" objects are treated the same as "small" ones.
41  * NB: although we include FAR keywords in the routine declarations,
42  * this file won't actually work in 80x86 small/medium model; at least,
43  * you probably won't be able to process useful-size images in only 64KB.
44  */
45 
46 GLOBAL(void FAR *)
jpeg_get_large(j_common_ptr cinfo,size_t sizeofobject)47 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
48 {
49   jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
50 
51   return (void *) (cmem->j_mem_get_large)(cinfo, sizeofobject);
52 }
53 
54 GLOBAL(void)
jpeg_free_large(j_common_ptr cinfo,void FAR * object,size_t sizeofobject)55 jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
56 {
57   jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
58 
59   (cmem->j_mem_free_large)(cinfo, object, sizeofobject);
60 }
61 
62 /*
63  * This routine computes the total memory space available for allocation.
64  * Here we always say, "we got all you want bud!"
65  */
66 
67 GLOBAL(long)
jpeg_mem_available(j_common_ptr cinfo,long min_bytes_needed,long max_bytes_needed,long already_allocated)68 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
69                     long max_bytes_needed, long already_allocated)
70 {
71   jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
72   long ret = max_bytes_needed;
73 
74   if (cmem->j_mem_avail)
75     ret = (cmem->j_mem_avail)(cinfo);
76 
77   return ret;
78 }
79 
80 /*
81  * Backing store (temporary file) management.
82  * Since jpeg_mem_available always promised the moon,
83  * this should never be called and we can just error out.
84  */
85 
86 GLOBAL(void)
jpeg_open_backing_store(j_common_ptr cinfo,backing_store_ptr info,long total_bytes_needed)87 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
88                          long total_bytes_needed)
89 {
90   jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
91 
92   if (cmem->j_mem_open_backing_store) {
93     (cmem->j_mem_open_backing_store)(cinfo, info, total_bytes_needed);
94   }
95   else
96     ERREXIT(cinfo, JERR_NO_BACKING_STORE);
97 }
98 
99 /*
100  * These routines take care of any system-dependent initialization and
101  * cleanup required.  Here, there isn't any.
102  */
103 
104 GLOBAL(long)
jpeg_mem_init(j_common_ptr cinfo)105 jpeg_mem_init (j_common_ptr cinfo)
106 {
107   jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
108   long ret = 0;
109 
110   if (cmem->j_mem_init)
111     ret = (cmem->j_mem_init)(cinfo);
112 
113   return ret;
114 }
115 
116 GLOBAL(void)
jpeg_mem_term(j_common_ptr cinfo)117 jpeg_mem_term (j_common_ptr cinfo)
118 {
119   jpeg_cust_mem_data *cmem = GET_CUST_MEM_DATA(cinfo);
120 
121   if (cmem->j_mem_term)
122     (cmem->j_mem_term)(cinfo);
123 }
124 
125 GLOBAL(jpeg_cust_mem_data *)
jpeg_cust_mem_init(jpeg_cust_mem_data * custm,void * priv,j_custmem_init_ptr init,j_custmem_term_ptr term,j_custmem_avail_ptr avail,j_custmem_get_small_ptr get_small,j_custmem_free_small_ptr free_small,j_cust_mem_get_large_ptr get_large,j_custmem_free_large_ptr free_large,j_custmem_open_backing_store_ptr open_backing_store)126 jpeg_cust_mem_init(jpeg_cust_mem_data *custm, void *priv,
127     j_custmem_init_ptr               init,
128     j_custmem_term_ptr               term,
129     j_custmem_avail_ptr              avail,
130     j_custmem_get_small_ptr          get_small,
131     j_custmem_free_small_ptr         free_small,
132     j_cust_mem_get_large_ptr         get_large,
133     j_custmem_free_large_ptr         free_large,
134     j_custmem_open_backing_store_ptr open_backing_store)
135 {
136   jpeg_cust_mem_data *lcustm = NULL;
137 
138   /* We need at least the following for a viable memory manager */
139   if (get_small && free_small && get_large && free_large)
140   {
141     lcustm = custm;
142 
143     lcustm->priv = priv;
144     lcustm->j_mem_init = init;
145     lcustm->j_mem_term = term;
146     lcustm->j_mem_avail = avail;
147     lcustm->j_mem_get_small = get_small;
148     lcustm->j_mem_free_small = free_small;
149     lcustm->j_mem_get_large = get_large;
150     lcustm->j_mem_free_large = free_large;
151     lcustm->j_mem_open_backing_store = open_backing_store;
152   }
153   return lcustm;
154 }
155 
156 GLOBAL(jpeg_cust_mem_data *)
jpeg_cust_mem_set_private(jpeg_cust_mem_data * custm,void * priv)157 jpeg_cust_mem_set_private(jpeg_cust_mem_data *custm, void *priv)
158 {
159   if (custm)
160   {
161     custm->priv = priv;
162   }
163   return custm;
164 }
165 
166 #endif /* !defined(SHARE_JPEG) || SHARE_JPEG==0 */
167