// Copyright (C) 2019 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import { decode as b64Decode, encode as b64Encode, length as b64Len } from '@protobufjs/base64'; import { length as utf8Len, read as utf8Read, write as utf8Write } from '@protobufjs/utf8'; import {assertTrue} from './logging'; // TextDecoder/Decoder requires the full DOM and isn't available in all types // of tests. Use fallback implementation from protbufjs. let UTF8Decoder: {decode: (buf: Uint8Array) => string;}; let UTF8Encoder: {encode: (str: string) => Uint8Array;}; try { UTF8Decoder = new TextDecoder('utf-8'); UTF8Encoder = new TextEncoder(); } catch (_) { if (typeof process === 'undefined') { // Silence the warning when we know we are running under NodeJS. console.warn( 'Using fallback UTF8 Encoder/Decoder, This should happen only in ' + 'tests and NodeJS-based environments, not in browsers.'); } UTF8Decoder = {decode: (buf: Uint8Array) => utf8Read(buf, 0, buf.length)}; UTF8Encoder = { encode: (str: string) => { const arr = new Uint8Array(utf8Len(str)); const written = utf8Write(str, arr, 0); assertTrue(written === arr.length); return arr; } }; } export function base64Encode(buffer: Uint8Array): string { return b64Encode(buffer, 0, buffer.length); } export function base64Decode(str: string): Uint8Array { const arr = new Uint8Array(b64Len(str)); const written = b64Decode(str, arr, 0); assertTrue(written === arr.length); return arr; } export function utf8Encode(str: string): Uint8Array { return UTF8Encoder.encode(str); } // Note: not all byte sequences can be converted to<>from UTF8. This can be // used only with valid unicode strings, not arbitrary byte buffers. export function utf8Decode(buffer: Uint8Array): string { return UTF8Decoder.decode(buffer); } // The binaryEncode/Decode functions below allow to encode an arbitrary binary // buffer into a string that can be JSON-encoded. binaryEncode() applies // UTF-16 encoding to each byte individually. // Unlike utf8Encode/Decode, any arbitrary byte sequence can be converted into a // valid string, and viceversa. // This should be only used when a byte array needs to be transmitted over an // interface that supports only JSON serialization (e.g., postmessage to a // chrome extension). export function binaryEncode(buf: Uint8Array): string { let str = ''; for (let i = 0; i < buf.length; i++) { str += String.fromCharCode(buf[i]); } return str; } export function binaryDecode(str: string): Uint8Array { const buf = new Uint8Array(str.length); const strLen = str.length; for (let i = 0; i < strLen; i++) { buf[i] = str.charCodeAt(i); } return buf; }