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
43#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
44#define close_utf8 _close
45#else
46#define close_utf8 close
47#endif
48
49static inline unsigned int CRC32(const unsigned char *message,
50 const size_t length)
51{
52 ssize_t
53 i;
54
55 static MagickBooleanType
56 crc_initial = MagickFalse;
57
58 static unsigned int
59 crc_xor[256];
60
61 unsigned int
62 crc;
63
64 /*
65 Generate a 32-bit cyclic redundancy check for the message.
66 */
67 if (crc_initial == MagickFalse)
68 {
69 unsigned int
70 j;
71
72 unsigned int
73 alpha;
74
75 for (j=0; j < 256; j++)
76 {
77 ssize_t
78 k;
79
80 alpha=j;
81 for (k=0; k < 8; k++)
82 alpha=(alpha & 0x01) ? (0xEDB88320 ^ (alpha >> 1)) : (alpha >> 1);
83 crc_xor[j]=alpha;
84 }
85 crc_initial=MagickTrue;
86 }
87 crc=0xFFFFFFFF;
88 for (i=0; i < (ssize_t) length; i++)
89 crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
90 return(crc ^ 0xFFFFFFFF);
91}
92
93static inline const struct tm *GetMagickUTCTime(const time_t *timep,
94 struct tm *result)
95{
96#if defined(MAGICKCORE_HAVE_GMTIME_R)
97 (void) gmtime_r(timep,result);
98#else
99 {
100 struct tm
101 *my_time;
102
103 my_time=gmtime(timep);
104 if (my_time != (struct tm *) NULL)
105 (void) memcpy(result,my_time,sizeof(*my_time));
106 }
107#endif
108 return(result);
109}
110
111#if defined(MAGICKCORE_WINDOWS_SUPPORT)
112static inline wchar_t *CreateWidePath(const char *path)
113{
114 int
115 count;
116
117 wchar_t
118 *wide_path;
119
120 /*
121 Create a wide path under Windows.
122 */
123 count=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
124 if ((count > MAX_PATH) && (strncmp(path,"\\\\?\\",4) != 0) &&
125 (NTLongPathsEnabled() == MagickFalse))
126 {
127 char
128 buffer[MagickPathExtent];
129
130 wchar_t
131 *long_path,
132 short_path[MAX_PATH];
133
134 (void) FormatLocaleString(buffer,MagickPathExtent,"\\\\?\\%s",path);
135 count+=4;
136 long_path=(wchar_t *) AcquireQuantumMemory(count,sizeof(*long_path));
137 if (long_path == (wchar_t *) NULL)
138 return((wchar_t *) NULL);
139 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,long_path,count);
140 if (count != 0)
141 count=GetShortPathNameW(long_path,short_path,MAX_PATH);
142 long_path=(wchar_t *) RelinquishMagickMemory(long_path);
143 if ((count < 5) || (count >= MAX_PATH))
144 return((wchar_t *) NULL);
145 wide_path=(wchar_t *) AcquireQuantumMemory(count-3,sizeof(*wide_path));
146 wcscpy(wide_path,short_path+4);
147 return(wide_path);
148 }
149 wide_path=(wchar_t *) AcquireQuantumMemory(count,sizeof(*wide_path));
150 if (wide_path == (wchar_t *) NULL)
151 return((wchar_t *) NULL);
152 count=MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,count);
153 if (count == 0)
154 {
155 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
156 return((wchar_t *) NULL);
157 }
158 return(wide_path);
159}
160
161static inline struct dirent *NTReadDirectory(DIR *entry)
162{
163 int
164 status;
165
166 size_t
167 length;
168
169 if (entry == (DIR *) NULL)
170 return((struct dirent *) NULL);
171 if (!entry->firsttime)
172 {
173 status=FindNextFileW(entry->hSearch,&entry->Win32FindData);
174 if (status == 0)
175 return((struct dirent *) NULL);
176 }
177 length=WideCharToMultiByte(CP_UTF8,0,entry->Win32FindData.cFileName,-1,
178 entry->file_info.d_name,sizeof(entry->file_info.d_name),NULL,NULL);
179 if (length == 0)
180 return((struct dirent *) NULL);
181 entry->firsttime=FALSE;
182 entry->file_info.d_namlen=(int) strlen(entry->file_info.d_name);
183 return(&entry->file_info);
184}
185#endif
186
187static inline MagickBooleanType MagickCreatePath(const char *path)
188{
189 char
190 *directed_path,
191 *directed_walk,
192 *p;
193
194 int
195 status = 0;
196
197 size_t
198 extent;
199
200 struct stat
201 attributes;
202
203 extent=2*strlen(path)+2;
204 directed_walk=(char *) AcquireCriticalMemory(extent*sizeof(*directed_walk));
205 *directed_walk='\0';
206 if (*path == '/')
207 (void) ConcatenateMagickString(directed_walk,"/",extent);
208 directed_path=ConstantString(path);
209 for (p=strtok(directed_path,"/"); p != (char *) NULL; p=strtok(NULL,"/"))
210 {
211 (void) ConcatenateMagickString(directed_walk,p,extent);
212 (void) ConcatenateMagickString(directed_walk,"/",extent);
213 if (GetPathAttributes(directed_walk,&attributes) == MagickFalse)
214 {
215#if defined(MAGICKCORE_WINDOWS_SUPPORT)
216 {
217 wchar_t
218 wide_path;
219
220 wide_path=CreateWidePath(directed_walk);
221 if (wide_path == (wchar_t *) NULL)
222 {
223 status=(-1);
224 break;
225 }
226 status=_wmkdir(wide_path);
227 wide_path=(wchar_t *) RelinquishMagickMemory(wide_path);
228 }
229#else
230 status=mkdir(directed_walk,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
231#endif
232 if (status < 0)
233 {
234 status=(-1);
235 break;
236 }
237 }
238 }
239 directed_path=DestroyString(directed_path);
240 directed_walk=DestroyString(directed_walk);
241 return(status == 0 ? MagickTrue : MagickFalse);
242}
243
244static inline int open_utf8(const char *path,int flags,mode_t mode)
245{
246#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
247 return(open(path,flags,mode));
248#else
249 int
250 status;
251
252 wchar_t
253 *path_wide;
254
255 path_wide=create_wchar_path(path);
256 if (path_wide == (wchar_t *) NULL)
257 return(-1);
258 status=_wopen(path_wide,flags,mode);
259 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
260 return(status);
261#endif
262}
263
264static inline int remove_utf8(const char *path)
265{
266#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
267 return(remove(path));
268#else
269 int
270 status;
271
272 wchar_t
273 *path_wide;
274
275 path_wide=create_wchar_path(path);
276 if (path_wide == (wchar_t *) NULL)
277 return(-1);
278 status=_wremove(path_wide);
279 path_wide=(wchar_t *) RelinquishMagickMemory(path_wide);
280 return(status);
281#endif
282}
283
284#if defined(__cplusplus) || defined(c_plusplus)
285}
286#endif
287
288#endif