1// Copyright (C) 2019 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import { 16 decode as b64Decode, 17 encode as b64Encode, 18 length as b64Len 19} from '@protobufjs/base64'; 20import { 21 length as utf8Len, 22 read as utf8Read, 23 write as utf8Write 24} from '@protobufjs/utf8'; 25 26import {assertTrue} from './logging'; 27 28// TextDecoder/Decoder requires the full DOM and isn't available in all types 29// of tests. Use fallback implementation from protbufjs. 30let UTF8Decoder: {decode: (buf: Uint8Array) => string;}; 31let UTF8Encoder: {encode: (str: string) => Uint8Array;}; 32try { 33 UTF8Decoder = new TextDecoder('utf-8'); 34 UTF8Encoder = new TextEncoder(); 35} catch (_) { 36 if (typeof process === 'undefined') { 37 // Silence the warning when we know we are running under NodeJS. 38 console.warn( 39 'Using fallback UTF8 Encoder/Decoder, This should happen only in ' + 40 'tests and NodeJS-based environments, not in browsers.'); 41 } 42 UTF8Decoder = {decode: (buf: Uint8Array) => utf8Read(buf, 0, buf.length)}; 43 UTF8Encoder = { 44 encode: (str: string) => { 45 const arr = new Uint8Array(utf8Len(str)); 46 const written = utf8Write(str, arr, 0); 47 assertTrue(written === arr.length); 48 return arr; 49 } 50 }; 51} 52 53export function base64Encode(buffer: Uint8Array): string { 54 return b64Encode(buffer, 0, buffer.length); 55} 56 57export function base64Decode(str: string): Uint8Array { 58 const arr = new Uint8Array(b64Len(str)); 59 const written = b64Decode(str, arr, 0); 60 assertTrue(written === arr.length); 61 return arr; 62} 63 64export function utf8Encode(str: string): Uint8Array { 65 return UTF8Encoder.encode(str); 66} 67 68// Note: not all byte sequences can be converted to<>from UTF8. This can be 69// used only with valid unicode strings, not arbitrary byte buffers. 70export function utf8Decode(buffer: Uint8Array): string { 71 return UTF8Decoder.decode(buffer); 72} 73 74// The binaryEncode/Decode functions below allow to encode an arbitrary binary 75// buffer into a string that can be JSON-encoded. binaryEncode() applies 76// UTF-16 encoding to each byte individually. 77// Unlike utf8Encode/Decode, any arbitrary byte sequence can be converted into a 78// valid string, and viceversa. 79// This should be only used when a byte array needs to be transmitted over an 80// interface that supports only JSON serialization (e.g., postmessage to a 81// chrome extension). 82 83export function binaryEncode(buf: Uint8Array): string { 84 let str = ''; 85 for (let i = 0; i < buf.length; i++) { 86 str += String.fromCharCode(buf[i]); 87 } 88 return str; 89} 90 91export function binaryDecode(str: string): Uint8Array { 92 const buf = new Uint8Array(str.length); 93 const strLen = str.length; 94 for (let i = 0; i < strLen; i++) { 95 buf[i] = str.charCodeAt(i); 96 } 97 return buf; 98} 99