• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  }
43  Utf8Decoder = {decode: (buf: Uint8Array) => utf8Read(buf, 0, buf.length)};
44  Utf8Encoder = {
45    encode: (str: string) => {
46      const arr = new Uint8Array(utf8Len(str));
47      const written = utf8Write(str, arr, 0);
48      assertTrue(written === arr.length);
49      return arr;
50    },
51  };
52}
53
54export function base64Encode(buffer: Uint8Array): string {
55  return b64Encode(buffer, 0, buffer.length);
56}
57
58export function base64Decode(str: string): Uint8Array {
59  // if the string is in base64url format, convert to base64
60  const b64 = str.replaceAll('-', '+').replaceAll('_', '/');
61  const arr = new Uint8Array(b64Len(b64));
62  const written = b64Decode(b64, arr, 0);
63  assertTrue(written === arr.length);
64  return arr;
65}
66
67// encode binary array to hex string
68export function hexEncode(bytes: Uint8Array): string {
69  return bytes.reduce(
70    (prev, cur) => prev + ('0' + cur.toString(16)).slice(-2),
71    '',
72  );
73}
74
75export function utf8Encode(str: string): Uint8Array {
76  return Utf8Encoder.encode(str);
77}
78
79// Note: not all byte sequences can be converted to<>from UTF8. This can be
80// used only with valid unicode strings, not arbitrary byte buffers.
81export function utf8Decode(buffer: Uint8Array): string {
82  return Utf8Decoder.decode(buffer);
83}
84
85// The binaryEncode/Decode functions below allow to encode an arbitrary binary
86// buffer into a string that can be JSON-encoded. binaryEncode() applies
87// UTF-16 encoding to each byte individually.
88// Unlike utf8Encode/Decode, any arbitrary byte sequence can be converted into a
89// valid string, and viceversa.
90// This should be only used when a byte array needs to be transmitted over an
91// interface that supports only JSON serialization (e.g., postmessage to a
92// chrome extension).
93
94export function binaryEncode(buf: Uint8Array): string {
95  let str = '';
96  for (let i = 0; i < buf.length; i++) {
97    str += String.fromCharCode(buf[i]);
98  }
99  return str;
100}
101
102export function binaryDecode(str: string): Uint8Array {
103  const buf = new Uint8Array(str.length);
104  const strLen = str.length;
105  for (let i = 0; i < strLen; i++) {
106    buf[i] = str.charCodeAt(i);
107  }
108  return buf;
109}
110
111// A function used to interpolate strings into SQL query. The only replacement
112// is done is that single quote replaced with two single quotes, according to
113// SQLite documentation:
114// https://www.sqlite.org/lang_expr.html#literal_values_constants_
115//
116// The purpose of this function is to use in simple comparisons, to escape
117// strings used in GLOB clauses see escapeQuery function.
118export function sqliteString(str: string): string {
119  return `'${str.replaceAll("'", "''")}'`;
120}
121
122// Chat apps (including G Chat) sometimes replace ASCII characters with similar
123// looking unicode characters that break code snippets.
124// This function attempts to undo these replacements.
125export function undoCommonChatAppReplacements(str: string): string {
126  // Replace non-breaking spaces with normal spaces.
127  return str.replaceAll('\u00A0', ' ');
128}
129