Coverage Report

Created: 2023-06-07 06:20

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