MagickCache 1.0.0
MagickCache: an Efficient Image Cache
Loading...
Searching...
No Matches
magick-cache-private.h
1/*
2 Copyright 1999 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
4
5 You may not use this file except in compliance with the License. You may
6 obtain a copy of the License at
7
8 https://imagemagick.org/script/license.php
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 MagickCore private utility methods.
17*/
18#ifndef MAGICKCORE_UTILITY_PRIVATE_H
19#define MAGICKCORE_UTILITY_PRIVATE_H
20
21#if defined(__cplusplus) || defined(c_plusplus)
22extern "C" {
23#endif
24
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <dirent.h>
28
29#define MagickCacheSentinel ".magickcache.sentinel"
30#define MagickCacheResourceSentinel ".magickcache.resource.sentinel"
31#define MagickCacheMin(x,y) (((x) < (y)) ? (x) : (y))
32
33#if !defined(O_BINARY)
34#define O_BINARY 0x00
35#endif
36
37#if defined(MAGICKCORE_WINDOWS_SUPPORT)
38#if !defined(readdir)
39# define readdir(directory) NTReadDirectory(directory)
40#endif
41#endif
42
43static inline unsigned int CRC32(const unsigned char *message,
44 const size_t length)
45{
46 ssize_t
47 i;
48
49 static MagickBooleanType
50 crc_initial = MagickFalse;
51
52 static unsigned int
53 crc_xor[256];
54
55 unsigned int
56 crc;
57
58 /*
59 Generate a 32-bit cyclic redundancy check for the message.
60 */
61 if (crc_initial == MagickFalse)
62 {
63 unsigned int
64 j;
65
66 unsigned int
67 alpha;
68
69 for (j=0; j < 256; j++)
70 {
71 ssize_t
72 k;
73
74 alpha=j;
75 for (k=0; k < 8; k++)
76 alpha=(alpha & 0x01) ? (0xEDB88320 ^ (alpha >> 1)) : (alpha >> 1);
77 crc_xor[j]=alpha;
78 }
79 crc_initial=MagickTrue;
80 }
81 crc=0xFFFFFFFF;
82 for (i=0; i < (ssize_t) length; i++)
83 crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
84 return(crc ^ 0xFFFFFFFF);
85}
86
87static inline const struct tm *GetMagickUTCTime(const time_t *timep,
88 struct tm *result)
89{
90#if defined(MAGICKCORE_HAVE_GMTIME_R)
91 (void) gmtime_r(timep,result);
92#else
93 {
94 struct tm
95 *my_time;
96
97 my_time=gmtime(timep);
98 if (my_time != (struct tm *) NULL)
99 (void) memcpy(result,my_time,sizeof(*my_time));
100 }
101#endif
102 return(result);
103}
104
105#if defined(MAGICKCORE_WINDOWS_SUPPORT)
106static inline wchar_t *CreateWidePath(const char *path)
107{
108 int
109 count;
110
111 wchar_t
112 *wide_path;
113
114 /*
115 Create a wide path under Windows.
116 */
117 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
118 if ((count > MAX_PATH) && (strncmp(path,"\\\\?\\",4) != 0) &&
119 (NTLongPathsEnabled() == MagickFalse))
120 {
121 char
122 buffer[MagickPathExtent];
123
124 wchar_t
125 *long_path,
126 short_path[MAX_PATH];
127
128 (void) FormatLocaleString(buffer,MagickPathExtent,"\\\\?\\%s",path);
129 count+=4;
130 long_path=(wchar_t *) AcquireQuantumMemory(count,sizeof(*long_path));
131 if (long_path == (wchar_t *) NULL)
132 return((wchar_t *) NULL);
133 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,long_path,count);
134 if (count != 0)
135 count=GetShortPathNameW(long_path,short_path,MAX_PATH);
136 long_path=(wchar_t *) RelinquishMagickMemory(long_path);
137 if ((count < 5) || (count >= MAX_PATH))
138 return((wchar_t *) NULL);
139 wide_path=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wide_path));
140 wcscpy(wide_path,short_path+4);
141 return(wide_path);
142 }
143 wide_path=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wide_path));
144 if (wide_path == (wchar_t *) NULL)
145 return((wchar_t *) NULL);
146 count=MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,count);
147 if (count == 0)
148 {
149 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
150 return((wchar_t *) NULL);
151 }
152 return(wide_path);
153}
154
155static inline struct dirent *NTReadDirectory(DIR *entry)
156{
157 int
158 status;
159
160 size_t
161 length;
162
163 if (entry == (DIR *) NULL)
164 return((struct dirent *) NULL);
165 if (!entry->firsttime)
166 {
167 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
168 if (status == 0)
169 return((struct dirent *) NULL);
170 }
171 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
172 entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
173 if (length == 0)
174 return((struct dirent *) NULL);
175 entry->firsttime=FALSE;
176 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
177 return(&entry->file_info);
178}
179#endif
180
181static inline MagickBooleanType MagickCreatePath(const char *path)
182{
183 char
184 *directed_path,
185 *directed_walk,
186 *p;
187
188 int
189 status = 0;
190
191 size_t
192 extent;
193
194 struct stat
195 attributes;
196
197 extent=2*strlen(path)+2;
198 directed_walk=(char *) AcquireCriticalMemory(extent*sizeof(*directed_walk));
199 *directed_walk='\0';
200 if (*path == '/')
201 (void) ConcatenateMagickString(directed_walk,"/",extent);
202 directed_path=ConstantString(path);
203 for (p=strtok(directed_path,"/"); p != (char *) NULL; p=strtok(NULL,"/"))
204 {
205 (void) ConcatenateMagickString(directed_walk,p,extent);
206 (void) ConcatenateMagickString(directed_walk,"/",extent);
207 if (GetPathAttributes(directed_walk,&attributes) == MagickFalse)
208 {
209#if defined(MAGICKCORE_WINDOWS_SUPPORT)
210 {
211 wchar_t
212 wide_path;
213
214 wide_path=CreateWidePath(directed_walk);
215 if (wide_path == (wchar_t *) NULL)
216 {
217 status=(-1);
218 break;
219 }
220 status=_wmkdir(wide_path);
221 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
222 }
223#else
224 status=mkdir(directed_walk,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
225#endif
226 if (status < 0)
227 {
228 status=(-1);
229 break;
230 }
231 }
232 }
233 directed_path=DestroyString(directed_path);
234 directed_walk=DestroyString(directed_walk);
235 return(status == 0 ? MagickTrue : MagickFalse);
236}
237
238static inline int open_utf8(const char *path,int flags,mode_t mode)
239{
240#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
241 return(open(path,flags,mode));
242#else
243 int
244 status;
245
246 wchar_t
247 *path_wide;
248
249 path_wide=create_wchar_path(path);
250 if (path_wide == (wchar_t *) NULL)
251 return(-1);
252 status=_wopen(path_wide,flags,mode);
253 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
254 return(status);
255#endif
256}
257
258static inline int remove_utf8(const char *path)
259{
260#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
261 return(remove(path));
262#else
263 int
264 status;
265
266 wchar_t
267 *path_wide;
268
269 path_wide=create_wchar_path(path);
270 if (path_wide == (wchar_t *) NULL)
271 return(-1);
272 status=_wremove(path_wide);
273 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
274 return(status);
275#endif
276}
277
278#if defined(__cplusplus) || defined(c_plusplus)
279}
280#endif
281
282#endif