(arr, off, val)
| 403 | } |
| 404 | |
| 405 | export function writeUint16(arr, off, val) { |
| 406 | if(arr.length < 4 || off > arr.length || off < 0) { //We should never underflow or overflow our buffer |
| 407 | throw new Error('Cannot write OOB.'); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | if(val.length > 0xffff) //Values that after serialization never will be a 16 bit integer should never be treated |
| 412 | throw new Error('Invalid argument. This should be a valid 16-bit integer.'); |
| 413 | |
| 414 | let old = new Array(arr[off], arr[off+1]); |
| 415 | |
| 416 | if(val <= 0xff) { //Integers below 256 are always valid |
| 417 | arr[off] = val; |
| 418 | |
| 419 | } else if(val > 0xff && val <= 0xfff) { //Integers bigger than 256 but below 4096 should be treated as two bytes instead of one and fixed with a leading zero |
| 420 | arr[off] = parseInt('0x'+(val.toString(16).split('')[0] + val.toString(16).split('')[1])); |
| 421 | arr[off+1] = parseInt('0x0'+val.toString(16).split('')[2]); |
| 422 | |
| 423 | } else if(val > 0xff && val <= 0xffff) { //Integers bigger than 256 and below 65535 should be treated as two bytes and can be splitted without having to fix them with a leading zero |
| 424 | arr[off] = parseInt('0x'+(val.toString(16).split('')[0] + val.toString(16).split('')[1])); |
| 425 | arr[off+1] = parseInt('0x'+val.toString(16).split('')[2] + val.toString(16).split('')[3]); |
| 426 | |
| 427 | } |
| 428 | |
| 429 | } |
nothing calls this directly
no outgoing calls
no test coverage detected