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