1 /*
2 * Copyright (C) 2007-2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jmemsys.h" /* import the system-dependent declarations */
21
22 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
23 extern void * malloc JPP((size_t size));
24 extern void free JPP((void *ptr));
25 #endif
26
27 #ifndef SEEK_SET /* pre-ANSI systems may not define this; */
28 #define SEEK_SET 0 /* if not, assume 0 is correct */
29 #endif
30
31
32 /*
33 * Memory allocation and freeing are controlled by the regular library
34 * routines malloc() and free().
35 */
36
37 GLOBAL(void *)
jpeg_get_small(j_common_ptr cinfo,size_t sizeofobject)38 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
39 {
40 return (void *) malloc(sizeofobject);
41 }
42
43 GLOBAL(void)
jpeg_free_small(j_common_ptr cinfo,void * object,size_t sizeofobject)44 jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
45 {
46 free(object);
47 }
48
49
50 /*
51 * "Large" objects are treated the same as "small" ones.
52 * NB: although we include FAR keywords in the routine declarations,
53 * this file won't actually work in 80x86 small/medium model; at least,
54 * you probably won't be able to process useful-size images in only 64KB.
55 */
56
57 GLOBAL(void FAR *)
jpeg_get_large(j_common_ptr cinfo,size_t sizeofobject)58 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
59 {
60 return (void FAR *) malloc(sizeofobject);
61 }
62
63 GLOBAL(void)
jpeg_free_large(j_common_ptr cinfo,void FAR * object,size_t sizeofobject)64 jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
65 {
66 free(object);
67 }
68
69
70 /*
71 * This routine computes the total memory space available for allocation.
72 * It's impossible to do this in a portable way; our current solution is
73 * to make the user tell us (with a default value set at compile time).
74 * If you can actually get the available space, it's a good idea to subtract
75 * a slop factor of 5% or so.
76 */
77
78 #ifndef DEFAULT_MAX_MEM /* so can override from makefile */
79 #define DEFAULT_MAX_MEM 10000000L /* default: ten megabyte */
80 #endif
81
82 GLOBAL(long)
jpeg_mem_available(j_common_ptr cinfo,long min_bytes_needed,long max_bytes_needed,long already_allocated)83 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
84 long max_bytes_needed, long already_allocated)
85 {
86 return cinfo->mem->max_memory_to_use - already_allocated;
87 }
88
89
90 /*
91 * Backing store (temporary file) management.
92 * Backing store objects are only used when the value returned by
93 * jpeg_mem_available is less than the total space needed. You can dispense
94 * with these routines if you have plenty of virtual memory; see jmemnobs.c.
95 */
96
97
98 METHODDEF(void)
read_backing_store(j_common_ptr cinfo,backing_store_ptr info,void FAR * buffer_address,long file_offset,long byte_count)99 read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
100 void FAR * buffer_address,
101 long file_offset, long byte_count)
102 {
103 if (fseek(info->temp_file, file_offset, SEEK_SET))
104 ERREXIT(cinfo, JERR_TFILE_SEEK);
105 if (JFREAD(info->temp_file, buffer_address, byte_count)
106 != (size_t) byte_count)
107 ERREXIT(cinfo, JERR_TFILE_READ);
108 }
109
110
111 METHODDEF(void)
write_backing_store(j_common_ptr cinfo,backing_store_ptr info,void FAR * buffer_address,long file_offset,long byte_count)112 write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
113 void FAR * buffer_address,
114 long file_offset, long byte_count)
115 {
116 if (fseek(info->temp_file, file_offset, SEEK_SET))
117 ERREXIT(cinfo, JERR_TFILE_SEEK);
118 if (JFWRITE(info->temp_file, buffer_address, byte_count)
119 != (size_t) byte_count)
120 ERREXIT(cinfo, JERR_TFILE_WRITE);
121 }
122
123
124 METHODDEF(void)
close_backing_store(j_common_ptr cinfo,backing_store_ptr info)125 close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
126 {
127 fclose(info->temp_file);
128 /* Since this implementation uses tmpfile() to create the file,
129 * no explicit file deletion is needed.
130 */
131 }
132
getTempFileFromPath(const char * path)133 static FILE* getTempFileFromPath(const char * path) {
134 FILE * fd = fopen(path, "w+");
135 unlink(path);
136 return fd;
137 }
138
getTempFile()139 static FILE* getTempFile() {
140 char path[1024];
141 snprintf(path, 1023, "/sdcard/.%d.tmp", getpid());
142 FILE * fd = getTempFileFromPath(path);
143 if (fd == NULL) {
144 // anywhere else we can create a temp file?
145 // snprintf(path, 1023, "/data/data/.%d.tmp", getpid());
146 // fd = getTempFileFromPath(path);
147 }
148 return fd;
149 }
150
151 /*
152 * Initial opening of a backing-store object.
153 *
154 * This version uses tmpfile(), which constructs a suitable file name
155 * behind the scenes. We don't have to use info->temp_name[] at all;
156 * indeed, we can't even find out the actual name of the temp file.
157 */
158
159 GLOBAL(void)
jpeg_open_backing_store(j_common_ptr cinfo,backing_store_ptr info,long total_bytes_needed)160 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
161 long total_bytes_needed)
162 {
163 if ((info->temp_file = getTempFile()) == NULL)
164 ERREXITS(cinfo, JERR_TFILE_CREATE, "");
165 info->read_backing_store = read_backing_store;
166 info->write_backing_store = write_backing_store;
167 info->close_backing_store = close_backing_store;
168 }
169
170
171 /*
172 * These routines take care of any system-dependent initialization and
173 * cleanup required.
174 */
175
176 GLOBAL(long)
jpeg_mem_init(j_common_ptr cinfo)177 jpeg_mem_init (j_common_ptr cinfo)
178 {
179 return DEFAULT_MAX_MEM; /* default for max_memory_to_use */
180 }
181
182 GLOBAL(void)
jpeg_mem_term(j_common_ptr cinfo)183 jpeg_mem_term (j_common_ptr cinfo)
184 {
185 /* no work */
186 }
187