Wed, 31 Dec 2014 06:09:35 +0100
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-2010, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: unewdata.c |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 1999oct25 |
michael@0 | 14 | * created by: Markus W. Scherer |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #include <stdio.h> |
michael@0 | 18 | #include "unicode/utypes.h" |
michael@0 | 19 | #include "unicode/putil.h" |
michael@0 | 20 | #include "unicode/ustring.h" |
michael@0 | 21 | #include "cmemory.h" |
michael@0 | 22 | #include "cstring.h" |
michael@0 | 23 | #include "filestrm.h" |
michael@0 | 24 | #include "unicode/udata.h" |
michael@0 | 25 | #include "unewdata.h" |
michael@0 | 26 | |
michael@0 | 27 | struct UNewDataMemory { |
michael@0 | 28 | FileStream *file; |
michael@0 | 29 | uint16_t headerSize; |
michael@0 | 30 | uint8_t magic1, magic2; |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | U_CAPI UNewDataMemory * U_EXPORT2 |
michael@0 | 34 | udata_create(const char *dir, const char *type, const char *name, |
michael@0 | 35 | const UDataInfo *pInfo, |
michael@0 | 36 | const char *comment, |
michael@0 | 37 | UErrorCode *pErrorCode) { |
michael@0 | 38 | UNewDataMemory *pData; |
michael@0 | 39 | uint16_t headerSize, commentLength; |
michael@0 | 40 | char filename[512]; |
michael@0 | 41 | uint8_t bytes[16]; |
michael@0 | 42 | int length; |
michael@0 | 43 | |
michael@0 | 44 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { |
michael@0 | 45 | return NULL; |
michael@0 | 46 | } else if(name==NULL || *name==0 || pInfo==NULL) { |
michael@0 | 47 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 48 | return NULL; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | /* allocate the data structure */ |
michael@0 | 52 | pData=(UNewDataMemory *)uprv_malloc(sizeof(UNewDataMemory)); |
michael@0 | 53 | if(pData==NULL) { |
michael@0 | 54 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 55 | return NULL; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | /* Check that the full path won't be too long */ |
michael@0 | 59 | length = 0; /* Start with nothing */ |
michael@0 | 60 | if(dir != NULL && *dir !=0) /* Add directory length if one was given */ |
michael@0 | 61 | { |
michael@0 | 62 | length += strlen(dir); |
michael@0 | 63 | |
michael@0 | 64 | /* Add 1 if dir doesn't end with path sep */ |
michael@0 | 65 | if (dir[strlen(dir) - 1]!= U_FILE_SEP_CHAR) { |
michael@0 | 66 | length++; |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | length += strlen(name); /* Add the filename length */ |
michael@0 | 70 | |
michael@0 | 71 | if(type != NULL && *type !=0) { /* Add directory length if given */ |
michael@0 | 72 | length += strlen(type); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | |
michael@0 | 76 | /* LDH buffer Length error check */ |
michael@0 | 77 | if(length > (sizeof(filename) - 1)) |
michael@0 | 78 | { |
michael@0 | 79 | *pErrorCode = U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 80 | uprv_free(pData); |
michael@0 | 81 | return NULL; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /* open the output file */ |
michael@0 | 85 | if(dir!=NULL && *dir!=0) { /* if dir has a value, we prepend it to the filename */ |
michael@0 | 86 | char *p=filename+strlen(dir); |
michael@0 | 87 | uprv_strcpy(filename, dir); |
michael@0 | 88 | if (*(p-1)!=U_FILE_SEP_CHAR) { |
michael@0 | 89 | *p++=U_FILE_SEP_CHAR; |
michael@0 | 90 | *p=0; |
michael@0 | 91 | } |
michael@0 | 92 | } else { /* otherwise, we'll output to the current dir */ |
michael@0 | 93 | filename[0]=0; |
michael@0 | 94 | } |
michael@0 | 95 | uprv_strcat(filename, name); |
michael@0 | 96 | if(type!=NULL && *type!=0) { |
michael@0 | 97 | uprv_strcat(filename, "."); |
michael@0 | 98 | uprv_strcat(filename, type); |
michael@0 | 99 | } |
michael@0 | 100 | pData->file=T_FileStream_open(filename, "wb"); |
michael@0 | 101 | if(pData->file==NULL) { |
michael@0 | 102 | uprv_free(pData); |
michael@0 | 103 | *pErrorCode=U_FILE_ACCESS_ERROR; |
michael@0 | 104 | return NULL; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | /* write the header information */ |
michael@0 | 108 | headerSize=(uint16_t)(pInfo->size+4); |
michael@0 | 109 | if(comment!=NULL && *comment!=0) { |
michael@0 | 110 | commentLength=(uint16_t)(uprv_strlen(comment)+1); |
michael@0 | 111 | headerSize+=commentLength; |
michael@0 | 112 | } else { |
michael@0 | 113 | commentLength=0; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /* write the size of the header, take padding into account */ |
michael@0 | 117 | pData->headerSize=(uint16_t)((headerSize+15)&~0xf); |
michael@0 | 118 | pData->magic1=0xda; |
michael@0 | 119 | pData->magic2=0x27; |
michael@0 | 120 | T_FileStream_write(pData->file, &pData->headerSize, 4); |
michael@0 | 121 | |
michael@0 | 122 | /* write the information data */ |
michael@0 | 123 | T_FileStream_write(pData->file, pInfo, pInfo->size); |
michael@0 | 124 | |
michael@0 | 125 | /* write the comment */ |
michael@0 | 126 | if(commentLength>0) { |
michael@0 | 127 | T_FileStream_write(pData->file, comment, commentLength); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | /* write padding bytes to align the data section to 16 bytes */ |
michael@0 | 131 | headerSize&=0xf; |
michael@0 | 132 | if(headerSize!=0) { |
michael@0 | 133 | headerSize=(uint16_t)(16-headerSize); |
michael@0 | 134 | uprv_memset(bytes, 0, headerSize); |
michael@0 | 135 | T_FileStream_write(pData->file, bytes, headerSize); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | return pData; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | U_CAPI uint32_t U_EXPORT2 |
michael@0 | 142 | udata_finish(UNewDataMemory *pData, UErrorCode *pErrorCode) { |
michael@0 | 143 | uint32_t fileLength=0; |
michael@0 | 144 | |
michael@0 | 145 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { |
michael@0 | 146 | return 0; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | if(pData!=NULL) { |
michael@0 | 150 | if(pData->file!=NULL) { |
michael@0 | 151 | /* fflush(pData->file);*/ |
michael@0 | 152 | fileLength=T_FileStream_size(pData->file); |
michael@0 | 153 | if(T_FileStream_error(pData->file)) { |
michael@0 | 154 | *pErrorCode=U_FILE_ACCESS_ERROR; |
michael@0 | 155 | } else { |
michael@0 | 156 | fileLength-=pData->headerSize; |
michael@0 | 157 | } |
michael@0 | 158 | T_FileStream_close(pData->file); |
michael@0 | 159 | } |
michael@0 | 160 | uprv_free(pData); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | return fileLength; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | /* dummy UDataInfo cf. udata.h */ |
michael@0 | 167 | static const UDataInfo dummyDataInfo = { |
michael@0 | 168 | sizeof(UDataInfo), |
michael@0 | 169 | 0, |
michael@0 | 170 | |
michael@0 | 171 | U_IS_BIG_ENDIAN, |
michael@0 | 172 | U_CHARSET_FAMILY, |
michael@0 | 173 | U_SIZEOF_UCHAR, |
michael@0 | 174 | 0, |
michael@0 | 175 | |
michael@0 | 176 | { 0, 0, 0, 0 }, /* dummy dataFormat */ |
michael@0 | 177 | { 0, 0, 0, 0 }, /* dummy formatVersion */ |
michael@0 | 178 | { 0, 0, 0, 0 } /* dummy dataVersion */ |
michael@0 | 179 | }; |
michael@0 | 180 | |
michael@0 | 181 | U_CAPI void U_EXPORT2 |
michael@0 | 182 | udata_createDummy(const char *dir, const char *type, const char *name, UErrorCode *pErrorCode) { |
michael@0 | 183 | if(U_SUCCESS(*pErrorCode)) { |
michael@0 | 184 | udata_finish(udata_create(dir, type, name, &dummyDataInfo, NULL, pErrorCode), pErrorCode); |
michael@0 | 185 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 186 | fprintf(stderr, "error %s writing dummy data file %s" U_FILE_SEP_STRING "%s.%s\n", |
michael@0 | 187 | u_errorName(*pErrorCode), dir, name, type); |
michael@0 | 188 | exit(*pErrorCode); |
michael@0 | 189 | } |
michael@0 | 190 | } |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | U_CAPI void U_EXPORT2 |
michael@0 | 194 | udata_write8(UNewDataMemory *pData, uint8_t byte) { |
michael@0 | 195 | if(pData!=NULL && pData->file!=NULL) { |
michael@0 | 196 | T_FileStream_write(pData->file, &byte, 1); |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | U_CAPI void U_EXPORT2 |
michael@0 | 201 | udata_write16(UNewDataMemory *pData, uint16_t word) { |
michael@0 | 202 | if(pData!=NULL && pData->file!=NULL) { |
michael@0 | 203 | T_FileStream_write(pData->file, &word, 2); |
michael@0 | 204 | } |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | U_CAPI void U_EXPORT2 |
michael@0 | 208 | udata_write32(UNewDataMemory *pData, uint32_t wyde) { |
michael@0 | 209 | if(pData!=NULL && pData->file!=NULL) { |
michael@0 | 210 | T_FileStream_write(pData->file, &wyde, 4); |
michael@0 | 211 | } |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | U_CAPI void U_EXPORT2 |
michael@0 | 215 | udata_writeBlock(UNewDataMemory *pData, const void *s, int32_t length) { |
michael@0 | 216 | if(pData!=NULL && pData->file!=NULL) { |
michael@0 | 217 | if(length>0) { |
michael@0 | 218 | T_FileStream_write(pData->file, s, length); |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | U_CAPI void U_EXPORT2 |
michael@0 | 224 | udata_writePadding(UNewDataMemory *pData, int32_t length) { |
michael@0 | 225 | static const uint8_t padding[16]={ |
michael@0 | 226 | 0xaa, 0xaa, 0xaa, 0xaa, |
michael@0 | 227 | 0xaa, 0xaa, 0xaa, 0xaa, |
michael@0 | 228 | 0xaa, 0xaa, 0xaa, 0xaa, |
michael@0 | 229 | 0xaa, 0xaa, 0xaa, 0xaa |
michael@0 | 230 | }; |
michael@0 | 231 | if(pData!=NULL && pData->file!=NULL) { |
michael@0 | 232 | while(length>=16) { |
michael@0 | 233 | T_FileStream_write(pData->file, padding, 16); |
michael@0 | 234 | length-=16; |
michael@0 | 235 | } |
michael@0 | 236 | if(length>0) { |
michael@0 | 237 | T_FileStream_write(pData->file, padding, length); |
michael@0 | 238 | } |
michael@0 | 239 | } |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | U_CAPI void U_EXPORT2 |
michael@0 | 243 | udata_writeString(UNewDataMemory *pData, const char *s, int32_t length) { |
michael@0 | 244 | if(pData!=NULL && pData->file!=NULL) { |
michael@0 | 245 | if(length==-1) { |
michael@0 | 246 | length=(int32_t)uprv_strlen(s); |
michael@0 | 247 | } |
michael@0 | 248 | if(length>0) { |
michael@0 | 249 | T_FileStream_write(pData->file, s, length); |
michael@0 | 250 | } |
michael@0 | 251 | } |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | U_CAPI void U_EXPORT2 |
michael@0 | 255 | udata_writeUString(UNewDataMemory *pData, const UChar *s, int32_t length) { |
michael@0 | 256 | if(pData!=NULL && pData->file!=NULL) { |
michael@0 | 257 | if(length==-1) { |
michael@0 | 258 | length=u_strlen(s); |
michael@0 | 259 | } |
michael@0 | 260 | if(length>0) { |
michael@0 | 261 | T_FileStream_write(pData->file, s, length*sizeof(UChar)); |
michael@0 | 262 | } |
michael@0 | 263 | } |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | /* |
michael@0 | 267 | * Hey, Emacs, please set the following: |
michael@0 | 268 | * |
michael@0 | 269 | * Local Variables: |
michael@0 | 270 | * indent-tabs-mode: nil |
michael@0 | 271 | * End: |
michael@0 | 272 | * |
michael@0 | 273 | */ |
michael@0 | 274 |