Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | /* util.c - Miscellaneous support * * Copyright (C) 1997,1999 Martin von Löwis * Copyright (C) 1997 Régis Duchesne * Copyright (C) 2001 Anton Altaparmakov (AIA) * * The utf8 routines are copied from Python wstrop module. */ #include "ntfstypes.h" #include "struct.h" #include "util.h" #include <linux/string.h> #include <linux/errno.h> #include "support.h" /* Converts a single wide character to a sequence of utf8 bytes. * The character is represented in host byte order. * Returns the number of bytes, or 0 on error. */ static int to_utf8(ntfs_u16 c, unsigned char* buf) { if (c == 0) return 0; /* No support for embedded 0 runes. */ if (c < 0x80) { if (buf) buf[0] = (unsigned char)c; return 1; } if (c < 0x800) { if (buf) { buf[0] = 0xc0 | (c >> 6); buf[1] = 0x80 | (c & 0x3f); } return 2; } if (c < 0x10000) { if (buf) { buf[0] = 0xe0 | (c >> 12); buf[1] = 0x80 | ((c >> 6) & 0x3f); buf[2] = 0x80 | (c & 0x3f); } return 3; } /* We don't support characters above 0xFFFF in NTFS. */ return 0; } /* Decodes a sequence of utf8 bytes into a single wide character. * The character is returned in host byte order. * Returns the number of bytes consumed, or 0 on error. */ static int from_utf8(const unsigned char* str, ntfs_u16 *c) { int l = 0, i; if (*str < 0x80) { *c = *str; return 1; } if (*str < 0xc0) /* Lead byte must not be 10xxxxxx. */ return 0; /* Is c0 a possible lead byte? */ if (*str < 0xe0) { /* 110xxxxx */ *c = *str & 0x1f; l = 2; } else if (*str < 0xf0) { /* 1110xxxx */ *c = *str & 0xf; l = 3; } else if (*str < 0xf8) { /* 11110xxx */ *c = *str & 7; l = 4; } else /* We don't support characters above 0xFFFF in NTFS. */ return 0; for (i = 1; i < l; i++) { /* All other bytes must be 10xxxxxx. */ if ((str[i] & 0xc0) != 0x80) return 0; *c <<= 6; *c |= str[i] & 0x3f; } return l; } /* Converts wide string to UTF-8. Expects two in- and two out-parameters. * Returns 0 on success, or error code. * The caller has to free the result string. * There is no support for UTF-16, yet. */ static int ntfs_dupuni2utf8(ntfs_u16* in, int in_len, char **out, int *out_len) { int i, tmp; int len8; unsigned char *result; ntfs_debug(DEBUG_NAME1, "converting l = %d\n", in_len); /* Count the length of the resulting UTF-8. */ for (i = len8 = 0; i < in_len; i++){ tmp = to_utf8(NTFS_GETU16(in + i), 0); if (!tmp) /* invalid character */ return -EILSEQ; len8 += tmp; } *out = result = ntfs_malloc(len8 + 1); /* allow for zero-termination */ if (!result) return -ENOMEM; result[len8] = '\0'; *out_len = len8; for (i = len8 = 0; i < in_len; i++) len8 += to_utf8(NTFS_GETU16(in + i), result + len8); ntfs_debug(DEBUG_NAME1, "result %p:%s\n", result, result); return 0; } /* Converts an UTF-8 sequence to a wide string. Same conventions as the * previous function. */ static int ntfs_duputf82uni(unsigned char* in, int in_len, ntfs_u16** out, int *out_len) { int i, tmp; int len16; ntfs_u16* result; ntfs_u16 wtmp; for (i = len16 = 0; i < in_len; i += tmp, len16++) { tmp = from_utf8(in + i, &wtmp); if (!tmp) return -EILSEQ; } *out = result = ntfs_malloc(2 * (len16 + 1)); if (!result) return -ENOMEM; result[len16] = 0; *out_len = len16; for (i = len16 = 0; i < in_len; i += tmp, len16++) { tmp = from_utf8(in + i, &wtmp); NTFS_PUTU16(result + len16, wtmp); } return 0; } /* See above. Produces ISO-8859-1 from wide strings. */ static int ntfs_dupuni288591(ntfs_u16* in, int in_len, char** out, int *out_len) { int i; char *result; /* Check for characters out of range. */ for (i = 0; i < in_len; i++) if (NTFS_GETU16(in + i) >= 256) return -EILSEQ; *out = result = ntfs_malloc(in_len + 1); if (!result) return -ENOMEM; result[in_len] = '\0'; *out_len = in_len; for (i = 0; i < in_len; i++) result[i] = (unsigned char)NTFS_GETU16(in + i); return 0; } /* See above. */ static int ntfs_dup885912uni(unsigned char* in, int in_len, ntfs_u16 **out, int *out_len) { int i; ntfs_u16* result; *out = result = ntfs_malloc(2 * in_len); if (!result) return -ENOMEM; *out_len = in_len; for (i = 0; i < in_len; i++) NTFS_PUTU16(result + i, in[i]); return 0; } /* Encodings dispatcher */ int ntfs_encodeuni(ntfs_volume *vol, ntfs_u16 *in, int in_len, char **out, int *out_len) { if (vol->nct & nct_utf8) return ntfs_dupuni2utf8(in, in_len, out, out_len); else if (vol->nct & nct_iso8859_1) return ntfs_dupuni288591(in, in_len, out, out_len); else if(vol->nct & (nct_map | nct_uni_xlate)) /* uni_xlate is handled inside map. */ return ntfs_dupuni2map(vol, in, in_len, out, out_len); else return -EINVAL; /* unknown encoding */ } int ntfs_decodeuni(ntfs_volume *vol, char *in, int in_len, ntfs_u16 **out, int *out_len) { if (vol->nct & nct_utf8) return ntfs_duputf82uni(in, in_len, out, out_len); else if (vol->nct & nct_iso8859_1) return ntfs_dup885912uni(in, in_len, out, out_len); else if (vol->nct & (nct_map | nct_uni_xlate)) return ntfs_dupmap2uni(vol, in, in_len, out, out_len); else return -EINVAL; } /* Same address space copies. */ void ntfs_put(ntfs_io *dest, void *src, ntfs_size_t n) { ntfs_memcpy(dest->param, src, n); ((char*)dest->param) += n; } void ntfs_get(void* dest, ntfs_io *src, ntfs_size_t n) { ntfs_memcpy(dest, src->param, n); ((char*)src->param) += n; } void *ntfs_calloc(int size) { void *result = ntfs_malloc(size); if (result) ntfs_bzero(result, size); return result; } #if 0 /* Copy len unicode characters from from to to. :) */ void ntfs_uni2ascii(char *to, short int *from, int len) { int i; for (i = 0; i < len; i++) to[i] = NTFS_GETU16(from + i); to[i] = '\0'; } #endif /* Copy len ascii characters from from to to. :) */ void ntfs_ascii2uni(short int *to, char *from, int len) { int i; for (i = 0; i < len; i++) NTFS_PUTU16(to + i, from[i]); to[i] = 0; } /* strncmp for Unicode strings. */ int ntfs_uni_strncmp(short int* a, short int *b, int n) { int i; for(i = 0; i < n; i++) { if (NTFS_GETU16(a + i) < NTFS_GETU16(b + i)) return -1; if (NTFS_GETU16(b + i) < NTFS_GETU16(a + i)) return 1; if (NTFS_GETU16(a + i) == 0) return 0; } return 0; } /* strncmp between Unicode and ASCII strings. */ int ntfs_ua_strncmp(short int* a, char* b, int n) { int i; for (i = 0; i < n; i++) { if(NTFS_GETU16(a + i) < b[i]) return -1; if(b[i] < NTFS_GETU16(a + i)) return 1; if (b[i] == 0) return 0; } return 0; } /* Convert the NT UTC (based 1.1.1601, in hundred nanosecond units) * into Unix UTC (based 1.1.1970, in seconds). */ ntfs_time_t ntfs_ntutc2unixutc(ntfs_time64_t ntutc) { /* This is very gross because: * 1: We must do 64-bit division on a 32-bit machine. * 2: We can't use libgcc for long long operations in the kernel. * 3: Floating point math in the kernel would corrupt user data. */ const unsigned int D = 10000000; unsigned int H = (unsigned int)(ntutc >> 32); unsigned int L = (unsigned int)ntutc; unsigned int numerator2; unsigned int lowseconds; unsigned int result; /* It is best to subtract 0x019db1ded53e8000 first. */ /* Then the 1601-based date becomes a 1970-based date. */ if (L < (unsigned)0xd53e8000) H--; L -= (unsigned)0xd53e8000; H -= (unsigned)0x019db1de; /* Now divide 64-bit numbers on a 32-bit machine. :-) * With the subtraction already done, the result fits in 32 bits. * The numerator fits in 56 bits and the denominator fits * in 24 bits, so we can shift by 8 bits to make this work. */ numerator2 = (H << 8) | (L >> 24); result = (numerator2 / D); /* shifted 24 right!! */ lowseconds = result << 24; numerator2 = ((numerator2 - result * D) << 8) | ((L >> 16) & 0xff); result = (numerator2 / D); /* shifted 16 right!! */ lowseconds |= result << 16; numerator2 = ((numerator2 - result * D) << 8) | ((L >> 8) & 0xff); result = (numerator2 / D); /* shifted 8 right!! */ lowseconds |= result << 8; numerator2 = ((numerator2 - result * D) << 8) | (L & 0xff); result = (numerator2 / D); /* not shifted */ lowseconds |= result; return lowseconds; } /* Convert the Unix UTC into NT UTC. */ ntfs_time64_t ntfs_unixutc2ntutc(ntfs_time_t t) { return ((t + (ntfs_time64_t)(369 * 365 + 89) * 24 * 3600) * 10000000); } /* Fill index name. */ void ntfs_indexname(char *buf, int type) { char hex[] = "0123456789ABCDEF"; int index; *buf++ = '$'; *buf++ = 'I'; for (index = 24; index > 0; index -= 4) if ((0xF << index) & type) break; while (index >= 0) { *buf++ = hex[(type >> index) & 0xF]; index -= 4; } *buf = '\0'; } |