1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1999-2013, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************/
12 /*----------------------------------------------------------------------------
14 * Memory mapped file wrappers for use by the ICU Data Implementation
15 * All of the platform-specific implementation for mapping data files
16 * is here. The rest of the ICU Data implementation uses only the
19 *----------------------------------------------------------------------------*/
20 /* Defines _XOPEN_SOURCE for access to POSIX functions.
21 * Must be before any other #includes. */
22 #include "uposixdefs.h"
24 #include "unicode/putil.h"
28 /* memory-mapping base definitions ------------------------------------------ */
30 #if MAP_IMPLEMENTATION==MAP_WIN32
31 # define WIN32_LEAN_AND_MEAN
40 typedef HANDLE MemoryMap;
42 # define IS_MAP(map) ((map)!=NULL)
43 #elif MAP_IMPLEMENTATION==MAP_POSIX || MAP_IMPLEMENTATION==MAP_390DLL
44 typedef size_t MemoryMap;
46 # define IS_MAP(map) ((map)!=0)
49 # include <sys/mman.h>
50 # include <sys/stat.h>
54 # define MAP_FAILED ((void*)-1)
57 # if MAP_IMPLEMENTATION==MAP_390DLL
58 /* No memory mapping for 390 batch mode. Fake it using dll loading. */
62 # include "unicode/udata.h"
63 # define LIB_PREFIX "lib"
64 # define LIB_SUFFIX ".dll"
65 /* This is inconvienient until we figure out what to do with U_ICUDATA_NAME in utypes.h */
66 # define U_ICUDATA_ENTRY_NAME "icudt" U_ICU_VERSION_SHORT U_LIB_SUFFIX_C_NAME_STRING "_dat"
68 #elif MAP_IMPLEMENTATION==MAP_STDIO
72 typedef void *MemoryMap;
74 # define IS_MAP(map) ((map)!=NULL)
77 /*----------------------------------------------------------------------------*
79 * Memory Mapped File support. Platform dependent implementation of *
80 * functions used by the rest of the implementation.*
82 *----------------------------------------------------------------------------*/
83 #if MAP_IMPLEMENTATION==MAP_NONE
85 uprv_mapFile(UDataMemory *pData, const char *path) {
86 UDataMemory_init(pData); /* Clear the output struct. */
87 return FALSE; /* no file access */
90 U_CFUNC void uprv_unmapFile(UDataMemory *pData) {
93 #elif MAP_IMPLEMENTATION==MAP_WIN32
96 UDataMemory *pData, /* Fill in with info on the result doing the mapping. */
97 /* Output only; any original contents are cleared. */
98 const char *path /* File path to be opened/mapped */
103 SECURITY_ATTRIBUTES mappingAttributes;
104 SECURITY_ATTRIBUTES *mappingAttributesPtr = NULL;
105 SECURITY_DESCRIPTOR securityDesc;
107 UDataMemory_init(pData); /* Clear the output struct. */
109 /* open the input file */
110 file=CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL,
112 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_RANDOM_ACCESS, NULL);
113 if(file==INVALID_HANDLE_VALUE) {
117 /* Declare and initialize a security descriptor.
118 This is required for multiuser systems on Windows 2000 SP4 and beyond */
119 if (InitializeSecurityDescriptor(&securityDesc, SECURITY_DESCRIPTOR_REVISION)) {
120 /* give the security descriptor a Null Dacl done using the "TRUE, (PACL)NULL" here */
121 if (SetSecurityDescriptorDacl(&securityDesc, TRUE, (PACL)NULL, FALSE)) {
122 /* Make the security attributes point to the security descriptor */
123 uprv_memset(&mappingAttributes, 0, sizeof(mappingAttributes));
124 mappingAttributes.nLength = sizeof(mappingAttributes);
125 mappingAttributes.lpSecurityDescriptor = &securityDesc;
126 mappingAttributes.bInheritHandle = FALSE; /* object uninheritable */
127 mappingAttributesPtr = &mappingAttributes;
130 /* else creating security descriptors can fail when we are on Windows 98,
131 and mappingAttributesPtr == NULL for that case. */
133 /* create an unnamed Windows file-mapping object for the specified file */
134 map=CreateFileMapping(file, mappingAttributesPtr, PAGE_READONLY, 0, 0, NULL);
140 /* map a view of the file into our address space */
141 pData->pHeader=(const DataHeader *)MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
142 if(pData->pHeader==NULL) {
151 uprv_unmapFile(UDataMemory *pData) {
152 if(pData!=NULL && pData->map!=NULL) {
153 UnmapViewOfFile(pData->pHeader);
154 CloseHandle(pData->map);
162 #elif MAP_IMPLEMENTATION==MAP_POSIX
164 uprv_mapFile(UDataMemory *pData, const char *path) {
170 UDataMemory_init(pData); /* Clear the output struct. */
172 /* determine the length of the file */
173 if(stat(path, &mystat)!=0 || mystat.st_size<=0) {
176 length=mystat.st_size;
179 fd=open(path, O_RDONLY);
184 /* get a view of the mapping */
185 #if U_PLATFORM != U_PF_HPUX
186 data=mmap(0, length, PROT_READ, MAP_SHARED, fd, 0);
188 data=mmap(0, length, PROT_READ, MAP_PRIVATE, fd, 0);
190 close(fd); /* no longer needed */
191 if(data==MAP_FAILED) {
195 pData->map = (char *)data + length;
196 pData->pHeader=(const DataHeader *)data;
197 pData->mapAddr = data;
198 #if U_PLATFORM == U_PF_IPHONE
199 posix_madvise(data, length, POSIX_MADV_RANDOM);
205 uprv_unmapFile(UDataMemory *pData) {
206 if(pData!=NULL && pData->map!=NULL) {
207 size_t dataLen = (char *)pData->map - (char *)pData->mapAddr;
208 if(munmap(pData->mapAddr, dataLen)==-1) {
218 #elif MAP_IMPLEMENTATION==MAP_STDIO
219 /* copy of the filestrm.c/T_FileStream_size() implementation */
221 umap_fsize(FILE *f) {
222 int32_t savedPos = ftell(f);
225 /*Changes by Bertrand A. D. doesn't affect the current position
226 goes to the end of the file before ftell*/
227 fseek(f, 0, SEEK_END);
228 size = (int32_t)ftell(f);
229 fseek(f, savedPos, SEEK_SET);
234 uprv_mapFile(UDataMemory *pData, const char *path) {
239 UDataMemory_init(pData); /* Clear the output struct. */
240 /* open the input file */
241 file=fopen(path, "rb");
246 /* get the file length */
247 fileLength=umap_fsize(file);
248 if(ferror(file) || fileLength<=20) {
253 /* allocate the memory to hold the file data */
254 p=uprv_malloc(fileLength);
261 if(fileLength!=fread(p, 1, fileLength, file)) {
269 pData->pHeader=(const DataHeader *)p;
275 uprv_unmapFile(UDataMemory *pData) {
276 if(pData!=NULL && pData->map!=NULL) {
277 uprv_free(pData->map);
279 pData->mapAddr = NULL;
280 pData->pHeader = NULL;
285 #elif MAP_IMPLEMENTATION==MAP_390DLL
286 /* 390 specific Library Loading.
287 * This is the only platform left that dynamically loads an ICU Data Library.
288 * All other platforms use .data files when dynamic loading is required, but
289 * this turn out to be awkward to support in 390 batch mode.
291 * The idea here is to hide the fact that 390 is using dll loading from the
292 * rest of ICU, and make it look like there is file loading happening.
296 static char *strcpy_returnEnd(char *dest, const char *src)
298 while((*dest=*src)!=0) {
305 /*------------------------------------------------------------------------------
307 * computeDirPath given a user-supplied path of an item to be opened,
309 * - the full directory path to be used
310 * when opening the file.
311 * - Pointer to null at end of above returned path
314 * path: input path. Buffer is not altered.
315 * pathBuffer: Output buffer. Any contents are overwritten.
318 * Pointer to null termination in returned pathBuffer.
320 * TODO: This works the way ICU historically has, but the
321 * whole data fallback search path is so complicated that
322 * proabably almost no one will ever really understand it,
323 * the potential for confusion is large. (It's not just
324 * this one function, but the whole scheme.)
326 *------------------------------------------------------------------------------*/
327 static char *uprv_computeDirPath(const char *path, char *pathBuffer)
329 char *finalSlash; /* Ptr to last dir separator in input path, or null if none. */
330 int32_t pathLen; /* Length of the returned directory path */
334 finalSlash = uprv_strrchr(path, U_FILE_SEP_CHAR);
338 if (finalSlash == 0) {
339 /* No user-supplied path.
340 * Copy the ICU_DATA path to the path buffer and return that*/
341 const char *icuDataDir;
342 icuDataDir=u_getDataDirectory();
343 if(icuDataDir!=NULL && *icuDataDir!=0) {
344 return strcpy_returnEnd(pathBuffer, icuDataDir);
346 /* there is no icuDataDir either. Just return the empty pathBuffer. */
351 /* User supplied path did contain a directory portion.
352 * Copy it to the output path buffer */
353 pathLen = (int32_t)(finalSlash - path + 1);
354 uprv_memcpy(pathBuffer, path, pathLen);
355 *(pathBuffer+pathLen) = 0;
356 return pathBuffer+pathLen;
360 # define DATA_TYPE "dat"
362 U_CFUNC UBool uprv_mapFile(UDataMemory *pData, const char *path) {
363 const char *inBasename;
365 char pathBuffer[1024];
366 const DataHeader *pHeader;
370 inBasename=uprv_strrchr(path, U_FILE_SEP_CHAR);
371 if(inBasename==NULL) {
376 basename=uprv_computeDirPath(path, pathBuffer);
377 if(uprv_strcmp(inBasename, U_ICUDATA_NAME".dat") != 0) {
378 /* must mmap file... for build */
383 UDataMemory_init(pData); /* Clear the output struct. */
385 /* determine the length of the file */
386 if(stat(path, &mystat)!=0 || mystat.st_size<=0) {
389 length=mystat.st_size;
392 fd=open(path, O_RDONLY);
397 /* get a view of the mapping */
398 data=mmap(0, length, PROT_READ, MAP_PRIVATE, fd, 0);
399 close(fd); /* no longer needed */
400 if(data==MAP_FAILED) {
403 pData->map = (char *)data + length;
404 pData->pHeader=(const DataHeader *)data;
405 pData->mapAddr = data;
410 /* ### hack: we still need to get u_getDataDirectory() fixed
411 for OS/390 (batch mode - always return "//"? )
412 and this here straightened out with LIB_PREFIX and LIB_SUFFIX (both empty?!)
413 This is probably due to the strange file system on OS/390. It's more like
414 a database with short entry names than a typical file system. */
415 /* U_ICUDATA_NAME should always have the correct name */
416 /* BUT FOR BATCH MODE IT IS AN EXCEPTION BECAUSE */
417 /* THE FIRST THREE LETTERS ARE PREASSIGNED TO THE */
419 uprv_strcpy(pathBuffer, "IXMI" U_ICU_VERSION_SHORT "DA");
421 /* set up the library name */
422 uprv_strcpy(basename, LIB_PREFIX U_LIBICUDATA_NAME U_ICU_VERSION_SHORT LIB_SUFFIX);
426 fprintf(stderr, "dllload: %s ", pathBuffer);
429 handle=dllload(pathBuffer);
432 fprintf(stderr, " -> %08X\n", handle );
436 /* we have a data DLL - what kind of lookup do we need here? */
437 /* try to find the Table of Contents */
438 UDataMemory_init(pData); /* Clear the output struct. */
439 val=dllqueryvar((dllhandle*)handle, U_ICUDATA_ENTRY_NAME);
441 /* failed... so keep looking */
445 fprintf(stderr, "dllqueryvar(%08X, %s) -> %08X\n", handle, U_ICUDATA_ENTRY_NAME, val);
448 pData->pHeader=(const DataHeader *)val;
451 return FALSE; /* no handle */
455 U_CFUNC void uprv_unmapFile(UDataMemory *pData) {
456 if(pData!=NULL && pData->map!=NULL) {
457 uprv_free(pData->map);
459 pData->mapAddr = NULL;
460 pData->pHeader = NULL;
465 # error MAP_IMPLEMENTATION is set incorrectly