|
9 | 9 | DataViewPrototypeGetBuffer, |
10 | 10 | DataViewPrototypeGetByteLength, |
11 | 11 | DataViewPrototypeGetByteOffset, |
| 12 | + MathFloor, |
12 | 13 | Number, |
13 | 14 | ObjectDefineProperty, |
14 | 15 | ObjectEntries, |
@@ -549,6 +550,46 @@ function validateMaxBufferLength(data, name, max = kMaxBufferLength) { |
549 | 550 | } |
550 | 551 | } |
551 | 552 |
|
| 553 | +/** |
| 554 | + * Converts a bit length to the number of bytes needed to contain it. |
| 555 | + * Non-byte lengths are rounded up to the next byte. |
| 556 | + * @param {number} length |
| 557 | + * @returns {number} |
| 558 | + */ |
| 559 | +functionnumBitsToBytes(length){ |
| 560 | +returnMathFloor(length/8)+MathFloor((7+(length%8))/8); |
| 561 | +} |
| 562 | + |
| 563 | +/** |
| 564 | + * Copies `bytes` up to the byte length needed for `length` bits, then clears |
| 565 | + * unused least-significant bits in the final byte. |
| 566 | + * @param {number} length |
| 567 | + * @param {ArrayBuffer|ArrayBufferView} bytes |
| 568 | + * @returns {Uint8Array} |
| 569 | + */ |
| 570 | +functiontruncateToBitLength(length,bytes){ |
| 571 | +constlengthBytes=numBitsToBytes(length); |
| 572 | +constisView=ArrayBufferIsView(bytes); |
| 573 | +constbyteView=isView ? |
| 574 | +newUint8Array( |
| 575 | +getDataViewOrTypedArrayBuffer(bytes), |
| 576 | +getDataViewOrTypedArrayByteOffset(bytes), |
| 577 | +getDataViewOrTypedArrayByteLength(bytes), |
| 578 | +) : |
| 579 | +newUint8Array(bytes,0,ArrayBufferPrototypeGetByteLength(bytes)); |
| 580 | +constresult=TypedArrayPrototypeSlice( |
| 581 | +byteView, |
| 582 | +0, |
| 583 | +lengthBytes, |
| 584 | +); |
| 585 | + |
| 586 | +constremainder=length%8; |
| 587 | +if(remainder!==0) |
| 588 | +result[lengthBytes-1]&=(0xff<<(8-remainder))&0xff; |
| 589 | + |
| 590 | +returnresult; |
| 591 | +} |
| 592 | + |
552 | 593 | letwebidl; |
553 | 594 |
|
554 | 595 | // Keep this as a regular object. The WebIDL converters read and spread these |
@@ -608,12 +649,19 @@ function normalizeAlgorithm(algorithm, op) { |
608 | 649 | // 3. |
609 | 650 | if(idlType==='BufferSource'&&idlValue){ |
610 | 651 | constisView=ArrayBufferIsView(idlValue); |
611 | | -normalizedAlgorithm[member]=TypedArrayPrototypeSlice( |
| 652 | +constidlValueBytes=isView ? |
612 | 653 | newUint8Array( |
613 | | -isView ? getDataViewOrTypedArrayBuffer(idlValue) : idlValue, |
614 | | -isView ? getDataViewOrTypedArrayByteOffset(idlValue) : 0, |
615 | | -isView ? getDataViewOrTypedArrayByteLength(idlValue) : ArrayBufferPrototypeGetByteLength(idlValue), |
616 | | -), |
| 654 | +getDataViewOrTypedArrayBuffer(idlValue), |
| 655 | +getDataViewOrTypedArrayByteOffset(idlValue), |
| 656 | +getDataViewOrTypedArrayByteLength(idlValue), |
| 657 | +) : |
| 658 | +newUint8Array( |
| 659 | +idlValue, |
| 660 | +0, |
| 661 | +ArrayBufferPrototypeGetByteLength(idlValue), |
| 662 | +); |
| 663 | +normalizedAlgorithm[member]=TypedArrayPrototypeSlice( |
| 664 | +idlValueBytes, |
617 | 665 | ); |
618 | 666 | }elseif(idlType==='HashAlgorithmIdentifier'){ |
619 | 667 | normalizedAlgorithm[member]=normalizeAlgorithm(idlValue,'digest'); |
@@ -1003,6 +1051,8 @@ module.exports = { |
1003 | 1051 | cleanupWebCryptoResult, |
1004 | 1052 | prepareWebCryptoResult, |
1005 | 1053 | validateMaxBufferLength, |
| 1054 | + numBitsToBytes, |
| 1055 | + truncateToBitLength, |
1006 | 1056 | bigIntArrayToUnsignedBigInt, |
1007 | 1057 | bigIntArrayToUnsignedInt, |
1008 | 1058 | getBlockSize, |
|
0 commit comments