2 ******************************************************************************
4 * Copyright (C) 1999-2011, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************/
10 /*----------------------------------------------------------------------------
12 * Memory mapped file wrappers for use by the ICU Data Implementation
13 * All of the platform-specific implementation for mapping data files
14 * is here. The rest of the ICU Data implementation uses only the
17 *----------------------------------------------------------------------------*/
19 #include "unicode/putil.h"
23 /* memory-mapping base definitions ------------------------------------------ */
25 #if MAP_IMPLEMENTATION==MAP_WIN32
26 # define WIN32_LEAN_AND_MEAN
35 typedef HANDLE MemoryMap
;
37 # define IS_MAP(map) ((map)!=NULL)
38 #elif MAP_IMPLEMENTATION==MAP_POSIX || MAP_IMPLEMENTATION==MAP_390DLL
39 typedef size_t MemoryMap
;
41 # define IS_MAP(map) ((map)!=0)
44 # include <sys/mman.h>
45 # include <sys/stat.h>
49 # define MAP_FAILED ((void*)-1)
52 # if MAP_IMPLEMENTATION==MAP_390DLL
53 /* No memory mapping for 390 batch mode. Fake it using dll loading. */
57 # include "unicode/udata.h"
58 # define LIB_PREFIX "lib"
59 # define LIB_SUFFIX ".dll"
60 /* This is inconvienient until we figure out what to do with U_ICUDATA_NAME in utypes.h */
61 # define U_ICUDATA_ENTRY_NAME "icudt" U_ICU_VERSION_SHORT U_LIB_SUFFIX_C_NAME_STRING "_dat"
63 #elif MAP_IMPLEMENTATION==MAP_STDIO
67 typedef void *MemoryMap
;
69 # define IS_MAP(map) ((map)!=NULL)
72 /*----------------------------------------------------------------------------*
74 * Memory Mapped File support. Platform dependent implementation of *
75 * functions used by the rest of the implementation.*
77 *----------------------------------------------------------------------------*/
78 #if MAP_IMPLEMENTATION==MAP_NONE
80 uprv_mapFile(UDataMemory
*pData
, const char *path
) {
81 UDataMemory_init(pData
); /* Clear the output struct. */
82 return FALSE
; /* no file access */
85 U_CFUNC
void uprv_unmapFile(UDataMemory
*pData
) {
88 #elif MAP_IMPLEMENTATION==MAP_WIN32
91 UDataMemory
*pData
, /* Fill in with info on the result doing the mapping. */
92 /* Output only; any original contents are cleared. */
93 const char *path
/* File path to be opened/mapped */
98 SECURITY_ATTRIBUTES mappingAttributes
;
99 SECURITY_ATTRIBUTES
*mappingAttributesPtr
= NULL
;
100 SECURITY_DESCRIPTOR securityDesc
;
102 UDataMemory_init(pData
); /* Clear the output struct. */
104 /* open the input file */
105 file
=CreateFileA(path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
107 FILE_ATTRIBUTE_NORMAL
|FILE_FLAG_RANDOM_ACCESS
, NULL
);
108 if(file
==INVALID_HANDLE_VALUE
) {
112 /* Declare and initialize a security descriptor.
113 This is required for multiuser systems on Windows 2000 SP4 and beyond */
114 if (InitializeSecurityDescriptor(&securityDesc
, SECURITY_DESCRIPTOR_REVISION
)) {
115 /* give the security descriptor a Null Dacl done using the "TRUE, (PACL)NULL" here */
116 if (SetSecurityDescriptorDacl(&securityDesc
, TRUE
, (PACL
)NULL
, FALSE
)) {
117 /* Make the security attributes point to the security descriptor */
118 uprv_memset(&mappingAttributes
, 0, sizeof(mappingAttributes
));
119 mappingAttributes
.nLength
= sizeof(mappingAttributes
);
120 mappingAttributes
.lpSecurityDescriptor
= &securityDesc
;
121 mappingAttributes
.bInheritHandle
= FALSE
; /* object uninheritable */
122 mappingAttributesPtr
= &mappingAttributes
;
125 /* else creating security descriptors can fail when we are on Windows 98,
126 and mappingAttributesPtr == NULL for that case. */
128 /* create an unnamed Windows file-mapping object for the specified file */
129 map
=CreateFileMapping(file
, mappingAttributesPtr
, PAGE_READONLY
, 0, 0, NULL
);
135 /* map a view of the file into our address space */
136 pData
->pHeader
=(const DataHeader
*)MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
137 if(pData
->pHeader
==NULL
) {
146 uprv_unmapFile(UDataMemory
*pData
) {
147 if(pData
!=NULL
&& pData
->map
!=NULL
) {
148 UnmapViewOfFile(pData
->pHeader
);
149 CloseHandle(pData
->map
);
157 #elif MAP_IMPLEMENTATION==MAP_POSIX
159 uprv_mapFile(UDataMemory
*pData
, const char *path
) {
165 UDataMemory_init(pData
); /* Clear the output struct. */
167 /* determine the length of the file */
168 if(stat(path
, &mystat
)!=0 || mystat
.st_size
<=0) {
171 length
=mystat
.st_size
;
174 fd
=open(path
, O_RDONLY
);
179 /* get a view of the mapping */
180 #if U_PLATFORM != U_PF_HPUX
181 data
=mmap(0, length
, PROT_READ
, MAP_SHARED
, fd
, 0);
183 data
=mmap(0, length
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
185 close(fd
); /* no longer needed */
186 if(data
==MAP_FAILED
) {
190 pData
->map
= (char *)data
+ length
;
191 pData
->pHeader
=(const DataHeader
*)data
;
192 pData
->mapAddr
= data
;
193 #if U_PLATFORM == U_PF_IPHONE
194 posix_madvise(data
, length
, POSIX_MADV_RANDOM
);
200 uprv_unmapFile(UDataMemory
*pData
) {
201 if(pData
!=NULL
&& pData
->map
!=NULL
) {
202 size_t dataLen
= (char *)pData
->map
- (char *)pData
->mapAddr
;
203 if(munmap(pData
->mapAddr
, dataLen
)==-1) {
213 #elif MAP_IMPLEMENTATION==MAP_STDIO
214 /* copy of the filestrm.c/T_FileStream_size() implementation */
216 umap_fsize(FILE *f
) {
217 int32_t savedPos
= ftell(f
);
220 /*Changes by Bertrand A. D. doesn't affect the current position
221 goes to the end of the file before ftell*/
222 fseek(f
, 0, SEEK_END
);
223 size
= (int32_t)ftell(f
);
224 fseek(f
, savedPos
, SEEK_SET
);
229 uprv_mapFile(UDataMemory
*pData
, const char *path
) {
234 UDataMemory_init(pData
); /* Clear the output struct. */
235 /* open the input file */
236 file
=fopen(path
, "rb");
241 /* get the file length */
242 fileLength
=umap_fsize(file
);
243 if(ferror(file
) || fileLength
<=20) {
248 /* allocate the memory to hold the file data */
249 p
=uprv_malloc(fileLength
);
256 if(fileLength
!=fread(p
, 1, fileLength
, file
)) {
264 pData
->pHeader
=(const DataHeader
*)p
;
270 uprv_unmapFile(UDataMemory
*pData
) {
271 if(pData
!=NULL
&& pData
->map
!=NULL
) {
272 uprv_free(pData
->map
);
274 pData
->mapAddr
= NULL
;
275 pData
->pHeader
= NULL
;
280 #elif MAP_IMPLEMENTATION==MAP_390DLL
281 /* 390 specific Library Loading.
282 * This is the only platform left that dynamically loads an ICU Data Library.
283 * All other platforms use .data files when dynamic loading is required, but
284 * this turn out to be awkward to support in 390 batch mode.
286 * The idea here is to hide the fact that 390 is using dll loading from the
287 * rest of ICU, and make it look like there is file loading happening.
291 static char *strcpy_returnEnd(char *dest
, const char *src
)
293 while((*dest
=*src
)!=0) {
300 /*------------------------------------------------------------------------------
302 * computeDirPath given a user-supplied path of an item to be opened,
304 * - the full directory path to be used
305 * when opening the file.
306 * - Pointer to null at end of above returned path
309 * path: input path. Buffer is not altered.
310 * pathBuffer: Output buffer. Any contents are overwritten.
313 * Pointer to null termination in returned pathBuffer.
315 * TODO: This works the way ICU historically has, but the
316 * whole data fallback search path is so complicated that
317 * proabably almost no one will ever really understand it,
318 * the potential for confusion is large. (It's not just
319 * this one function, but the whole scheme.)
321 *------------------------------------------------------------------------------*/
322 static char *uprv_computeDirPath(const char *path
, char *pathBuffer
)
324 char *finalSlash
; /* Ptr to last dir separator in input path, or null if none. */
325 int32_t pathLen
; /* Length of the returned directory path */
329 finalSlash
= uprv_strrchr(path
, U_FILE_SEP_CHAR
);
333 if (finalSlash
== 0) {
334 /* No user-supplied path.
335 * Copy the ICU_DATA path to the path buffer and return that*/
336 const char *icuDataDir
;
337 icuDataDir
=u_getDataDirectory();
338 if(icuDataDir
!=NULL
&& *icuDataDir
!=0) {
339 return strcpy_returnEnd(pathBuffer
, icuDataDir
);
341 /* there is no icuDataDir either. Just return the empty pathBuffer. */
346 /* User supplied path did contain a directory portion.
347 * Copy it to the output path buffer */
348 pathLen
= (int32_t)(finalSlash
- path
+ 1);
349 uprv_memcpy(pathBuffer
, path
, pathLen
);
350 *(pathBuffer
+pathLen
) = 0;
351 return pathBuffer
+pathLen
;
355 # define DATA_TYPE "dat"
357 U_CFUNC UBool
uprv_mapFile(UDataMemory
*pData
, const char *path
) {
358 const char *inBasename
;
360 char pathBuffer
[1024];
361 const DataHeader
*pHeader
;
365 inBasename
=uprv_strrchr(path
, U_FILE_SEP_CHAR
);
366 if(inBasename
==NULL
) {
371 basename
=uprv_computeDirPath(path
, pathBuffer
);
372 if(uprv_strcmp(inBasename
, U_ICUDATA_NAME
".dat") != 0) {
373 /* must mmap file... for build */
378 UDataMemory_init(pData
); /* Clear the output struct. */
380 /* determine the length of the file */
381 if(stat(path
, &mystat
)!=0 || mystat
.st_size
<=0) {
384 length
=mystat
.st_size
;
387 fd
=open(path
, O_RDONLY
);
392 /* get a view of the mapping */
393 data
=mmap(0, length
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
394 close(fd
); /* no longer needed */
395 if(data
==MAP_FAILED
) {
398 pData
->map
= (char *)data
+ length
;
399 pData
->pHeader
=(const DataHeader
*)data
;
400 pData
->mapAddr
= data
;
405 /* ### hack: we still need to get u_getDataDirectory() fixed
406 for OS/390 (batch mode - always return "//"? )
407 and this here straightened out with LIB_PREFIX and LIB_SUFFIX (both empty?!)
408 This is probably due to the strange file system on OS/390. It's more like
409 a database with short entry names than a typical file system. */
410 /* U_ICUDATA_NAME should always have the correct name */
411 /* BUT FOR BATCH MODE IT IS AN EXCEPTION BECAUSE */
412 /* THE FIRST THREE LETTERS ARE PREASSIGNED TO THE */
414 uprv_strcpy(pathBuffer
, "IXMI" U_ICU_VERSION_SHORT
"DA");
416 /* set up the library name */
417 uprv_strcpy(basename
, LIB_PREFIX U_LIBICUDATA_NAME U_ICU_VERSION_SHORT LIB_SUFFIX
);
421 fprintf(stderr
, "dllload: %s ", pathBuffer
);
424 handle
=dllload(pathBuffer
);
427 fprintf(stderr
, " -> %08X\n", handle
);
431 /* we have a data DLL - what kind of lookup do we need here? */
432 /* try to find the Table of Contents */
433 UDataMemory_init(pData
); /* Clear the output struct. */
434 val
=dllqueryvar((dllhandle
*)handle
, U_ICUDATA_ENTRY_NAME
);
436 /* failed... so keep looking */
440 fprintf(stderr
, "dllqueryvar(%08X, %s) -> %08X\n", handle
, U_ICUDATA_ENTRY_NAME
, val
);
443 pData
->pHeader
=(const DataHeader
*)val
;
446 return FALSE
; /* no handle */
450 U_CFUNC
void uprv_unmapFile(UDataMemory
*pData
) {
451 if(pData
!=NULL
&& pData
->map
!=NULL
) {
452 uprv_free(pData
->map
);
454 pData
->mapAddr
= NULL
;
455 pData
->pHeader
= NULL
;
460 # error MAP_IMPLEMENTATION is set incorrectly