|
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | 6 | import { isString } from 'vs/base/common/types'; |
| 7 | +import { toUint8ArrayBuffer } from 'vs/base/common/uint'; |
7 | 8 |
|
8 | 9 | /** |
9 | 10 | * Return a hash value for an object. |
@@ -76,7 +77,7 @@ export class Hasher { |
76 | 77 | //#region SHA1 |
77 | 78 |
|
78 | 79 | export function computeSHA1Hash(value: string): string { |
79 | | - const data = encodeToArrayBuffer(value); |
| 80 | + const data = toUint8ArrayBuffer(value); |
80 | 81 | const hash = new SHA1(); |
81 | 82 |
|
82 | 83 | if (data.byteLength) { |
@@ -137,7 +138,7 @@ class SHA1 { |
137 | 138 | let data: Uint8Array; |
138 | 139 |
|
139 | 140 | if (isString(arg)) { |
140 | | - data = new Uint8Array(encodeToArrayBuffer(<string>arg)); |
| 141 | + data = new Uint8Array(toUint8ArrayBuffer(<string>arg)); |
141 | 142 | } else if (arg instanceof ArrayBuffer) { |
142 | 143 | data = new Uint8Array(arg); |
143 | 144 | } else if (arg instanceof DataView) { |
@@ -301,101 +302,6 @@ function multiply64(a: number, b: number): number[] { |
301 | 302 | return [(c3 << 16 | c2) >>> 0, (c1 << 16 | c0) >>> 0]; |
302 | 303 | } |
303 | 304 |
|
304 | | -function encodeToArrayBuffer(str: string): ArrayBuffer { |
305 | | - let i: number, len: number, length = 0, charCode = 0, trailCharCode = 0, codepoint = 0; |
306 | | - |
307 | | - // First pass, for the size |
308 | | - for (i = 0, len = str.length; i < len; i++) { |
309 | | - charCode = str.charCodeAt(i); |
310 | | - |
311 | | - // Surrogate pair |
312 | | - if (charCode >= 0xD800 && charCode < 0xDC00) { |
313 | | - trailCharCode = str.charCodeAt(++i); |
314 | | - |
315 | | - if (!(trailCharCode >= 0xDC00 && trailCharCode < 0xE000)) { |
316 | | - throw new Error('Invalid char code'); |
317 | | - } |
318 | | - |
319 | | - // Code point can be obtained by subtracting 0xD800 and 0xDC00 from both char codes respectively |
320 | | - // and joining the 10 least significant bits from each, finally adding 0x10000. |
321 | | - codepoint = ((((charCode - 0xD800) & 0x3FF) << 10) | ((trailCharCode - 0xDC00) & 0x3FF)) + 0x10000; |
322 | | - |
323 | | - } else { |
324 | | - codepoint = charCode; |
325 | | - } |
326 | | - |
327 | | - length += byteSizeInUTF8(codepoint); |
328 | | - } |
329 | | - |
330 | | - let result = new ArrayBuffer(length); |
331 | | - let view = new Uint8Array(result); |
332 | | - let pos = 0; |
333 | | - |
334 | | - // Second pass, for the data |
335 | | - for (i = 0, len = str.length; i < len; i++) { |
336 | | - charCode = str.charCodeAt(i); |
337 | | - |
338 | | - if (charCode >= 0xD800 && charCode < 0xDC00) { |
339 | | - trailCharCode = str.charCodeAt(++i); |
340 | | - codepoint = ((((charCode - 0xD800) & 0x3FF) << 10) | ((trailCharCode - 0xDC00) & 0x3FF)) + 0x10000; |
341 | | - } else { |
342 | | - codepoint = charCode; |
343 | | - } |
344 | | - |
345 | | - pos += writeUTF8(codepoint, view, pos); |
346 | | - } |
347 | | - |
348 | | - return result; |
349 | | -} |
350 | | - |
351 | | -function byteSizeInUTF8(codePoint: number): number { |
352 | | - codePoint = codePoint >>> 0; |
353 | | - |
354 | | - if (codePoint < 0x80) { |
355 | | - return 1; |
356 | | - } else if (codePoint < 0x800) { |
357 | | - return 2; |
358 | | - } else if (codePoint < 0x10000) { |
359 | | - return 3; |
360 | | - } else if (codePoint < 0x200000) { |
361 | | - return 4; |
362 | | - } else if (codePoint < 0x4000000) { |
363 | | - return 5; |
364 | | - } else if (codePoint < 0x80000000) { |
365 | | - return 6; |
366 | | - } else { |
367 | | - throw new Error('Code point 0x' + toHexString(codePoint) + ' not encodable in UTF8.'); |
368 | | - } |
369 | | -} |
370 | | - |
371 | | -function writeUTF8(codePoint: number, buffer: Uint8Array, pos: number): number { |
372 | | - |
373 | | - // How many bits needed for codePoint |
374 | | - let byteSize = byteSizeInUTF8(codePoint); |
375 | | - |
376 | | - // 0xxxxxxx |
377 | | - if (byteSize === 1) { |
378 | | - buffer[pos] = codePoint; |
379 | | - return 1; |
380 | | - } |
381 | | - |
382 | | - // 110xxxxx 10xxxxxx |
383 | | - // 1110xxxx 10xxxxxx 10xxxxxx |
384 | | - // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
385 | | - // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
386 | | - // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
387 | | - |
388 | | - // first byte |
389 | | - buffer[pos] = ((0xFC << (6 - byteSize)) | (codePoint >>> (6 * (byteSize - 1)))) & 0xFF; |
390 | | - |
391 | | - // successive bytes |
392 | | - for (let i = 1; i < byteSize; i++) { |
393 | | - buffer[pos + i] = (0x80 | (0x3F & (codePoint >>> (6 * (byteSize - i - 1))))) & 0xFF; |
394 | | - } |
395 | | - |
396 | | - return byteSize; |
397 | | -} |
398 | | - |
399 | 305 | function copy(dest: Uint8Array, destIndex: number, src: Uint8Array, srcIndex: number, count: number): number { |
400 | 306 | const len = Math.min(dest.byteLength - destIndex, src.byteLength - srcIndex, count); |
401 | 307 |
|
|
0 commit comments