intl/icu/source/common/udatamem.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 1999-2011, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 ******************************************************************************/
michael@0 8
michael@0 9
michael@0 10 /*----------------------------------------------------------------------------------
michael@0 11 *
michael@0 12 * UDataMemory A class-like struct that serves as a handle to a piece of memory
michael@0 13 * that contains some ICU data (resource, converters, whatever.)
michael@0 14 *
michael@0 15 * When an application opens ICU data (with udata_open, for example,
michael@0 16 * a UDataMemory * is returned.
michael@0 17 *
michael@0 18 *----------------------------------------------------------------------------------*/
michael@0 19
michael@0 20 #include "unicode/utypes.h"
michael@0 21 #include "cmemory.h"
michael@0 22 #include "unicode/udata.h"
michael@0 23
michael@0 24 #include "udatamem.h"
michael@0 25
michael@0 26 U_CFUNC void UDataMemory_init(UDataMemory *This) {
michael@0 27 uprv_memset(This, 0, sizeof(UDataMemory));
michael@0 28 This->length=-1;
michael@0 29 }
michael@0 30
michael@0 31
michael@0 32 U_CFUNC void UDatamemory_assign(UDataMemory *dest, UDataMemory *source) {
michael@0 33 /* UDataMemory Assignment. Destination UDataMemory must be initialized first. */
michael@0 34 UBool mallocedFlag = dest->heapAllocated;
michael@0 35 uprv_memcpy(dest, source, sizeof(UDataMemory));
michael@0 36 dest->heapAllocated = mallocedFlag;
michael@0 37 }
michael@0 38
michael@0 39 U_CFUNC UDataMemory *UDataMemory_createNewInstance(UErrorCode *pErr) {
michael@0 40 UDataMemory *This;
michael@0 41
michael@0 42 if (U_FAILURE(*pErr)) {
michael@0 43 return NULL;
michael@0 44 }
michael@0 45 This = uprv_malloc(sizeof(UDataMemory));
michael@0 46 if (This == NULL) {
michael@0 47 *pErr = U_MEMORY_ALLOCATION_ERROR; }
michael@0 48 else {
michael@0 49 UDataMemory_init(This);
michael@0 50 This->heapAllocated = TRUE;
michael@0 51 }
michael@0 52 return This;
michael@0 53 }
michael@0 54
michael@0 55
michael@0 56 U_CFUNC const DataHeader *
michael@0 57 UDataMemory_normalizeDataPointer(const void *p) {
michael@0 58 /* allow the data to be optionally prepended with an alignment-forcing double value */
michael@0 59 const DataHeader *pdh = (const DataHeader *)p;
michael@0 60 if(pdh==NULL || (pdh->dataHeader.magic1==0xda && pdh->dataHeader.magic2==0x27)) {
michael@0 61 return pdh;
michael@0 62 } else {
michael@0 63 #if U_PLATFORM == U_PF_OS400
michael@0 64 /*
michael@0 65 TODO: Fix this once the compiler implements this feature. Keep in sync with genccode.c
michael@0 66
michael@0 67 This is here because this platform can't currently put
michael@0 68 const data into the read-only pages of an object or
michael@0 69 shared library (service program). Only strings are allowed in read-only
michael@0 70 pages, so we use char * strings to store the data.
michael@0 71
michael@0 72 In order to prevent the beginning of the data from ever matching the
michael@0 73 magic numbers we must skip the initial double.
michael@0 74 [grhoten 4/24/2003]
michael@0 75 */
michael@0 76 return (const DataHeader *)*((const void **)p+1);
michael@0 77 #else
michael@0 78 return (const DataHeader *)((const double *)p+1);
michael@0 79 #endif
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83
michael@0 84 U_CFUNC void UDataMemory_setData (UDataMemory *This, const void *dataAddr) {
michael@0 85 This->pHeader = UDataMemory_normalizeDataPointer(dataAddr);
michael@0 86 }
michael@0 87
michael@0 88
michael@0 89 U_CAPI void U_EXPORT2
michael@0 90 udata_close(UDataMemory *pData) {
michael@0 91 if(pData!=NULL) {
michael@0 92 uprv_unmapFile(pData);
michael@0 93 if(pData->heapAllocated ) {
michael@0 94 uprv_free(pData);
michael@0 95 } else {
michael@0 96 UDataMemory_init(pData);
michael@0 97 }
michael@0 98 }
michael@0 99 }
michael@0 100
michael@0 101 U_CAPI const void * U_EXPORT2
michael@0 102 udata_getMemory(UDataMemory *pData) {
michael@0 103 if(pData!=NULL && pData->pHeader!=NULL) {
michael@0 104 return (char *)(pData->pHeader)+udata_getHeaderSize(pData->pHeader);
michael@0 105 } else {
michael@0 106 return NULL;
michael@0 107 }
michael@0 108 }
michael@0 109
michael@0 110 /**
michael@0 111 * Get the length of the data item if possible.
michael@0 112 * The length may be up to 15 bytes larger than the actual data.
michael@0 113 *
michael@0 114 * TODO Consider making this function public.
michael@0 115 * It would have to return the actual length in more cases.
michael@0 116 * For example, the length of the last item in a .dat package could be
michael@0 117 * computed from the size of the whole .dat package minus the offset of the
michael@0 118 * last item.
michael@0 119 * The size of a file that was directly memory-mapped could be determined
michael@0 120 * using some system API.
michael@0 121 *
michael@0 122 * In order to get perfect values for all data items, we may have to add a
michael@0 123 * length field to UDataInfo, but that complicates data generation
michael@0 124 * and may be overkill.
michael@0 125 *
michael@0 126 * @param pData The data item.
michael@0 127 * @return the length of the data item, or -1 if not known
michael@0 128 * @internal Currently used only in cintltst/udatatst.c
michael@0 129 */
michael@0 130 U_CAPI int32_t U_EXPORT2
michael@0 131 udata_getLength(const UDataMemory *pData) {
michael@0 132 if(pData!=NULL && pData->pHeader!=NULL && pData->length>=0) {
michael@0 133 /*
michael@0 134 * subtract the header size,
michael@0 135 * return only the size of the actual data starting at udata_getMemory()
michael@0 136 */
michael@0 137 return pData->length-udata_getHeaderSize(pData->pHeader);
michael@0 138 } else {
michael@0 139 return -1;
michael@0 140 }
michael@0 141 }
michael@0 142
michael@0 143 /**
michael@0 144 * Get the memory including the data header.
michael@0 145 * Used in cintltst/udatatst.c
michael@0 146 * @internal
michael@0 147 */
michael@0 148 U_CAPI const void * U_EXPORT2
michael@0 149 udata_getRawMemory(const UDataMemory *pData) {
michael@0 150 if(pData!=NULL && pData->pHeader!=NULL) {
michael@0 151 return pData->pHeader;
michael@0 152 } else {
michael@0 153 return NULL;
michael@0 154 }
michael@0 155 }
michael@0 156
michael@0 157 U_CFUNC UBool UDataMemory_isLoaded(const UDataMemory *This) {
michael@0 158 return This->pHeader != NULL;
michael@0 159 }

mercurial