aboutsummaryrefslogtreecommitdiff
path: root/packages/web-util/src/utils/base64.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-08-05 13:18:40 -0300
committerSebastian <sebasjm@gmail.com>2024-08-05 13:18:40 -0300
commitbdb151b8c73e526f72274c5a2d09a06c7f881175 (patch)
tree2f7ba675bd3835df3f1c1f1c6c06742ca2f82d06 /packages/web-util/src/utils/base64.ts
parent03d90d7c94e18704cdeaaf5434de54bdf26b45ff (diff)
encode crock for URI
Diffstat (limited to 'packages/web-util/src/utils/base64.ts')
-rw-r--r--packages/web-util/src/utils/base64.ts75
1 files changed, 51 insertions, 24 deletions
diff --git a/packages/web-util/src/utils/base64.ts b/packages/web-util/src/utils/base64.ts
index 0e075880f..e51591df6 100644
--- a/packages/web-util/src/utils/base64.ts
+++ b/packages/web-util/src/utils/base64.ts
@@ -14,13 +14,25 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
+import { decodeCrock, encodeCrock } from "@gnu-taler/taler-util";
+
+const utf8Encoder = new TextEncoder();
+const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });
+
+export function encodeCrockForURI(string: string): string {
+ return encodeCrock(utf8Encoder.encode(string));
+}
+
+export function decodeCrockFromURI(enc: string): string {
+ return utf8Decoder.decode(decodeCrock(enc));
+}
export function base64encode(str: string): string {
- return base64EncArr(strToUTF8Arr(str))
+ return base64EncArr(strToUTF8Arr(str));
}
export function base64decode(str: string): string {
- return UTF8ArrToStr(base64DecToArr(str))
+ return UTF8ArrToStr(base64DecToArr(str));
}
// from https://developer.mozilla.org/en-US/docs/Glossary/Base64
@@ -103,7 +115,7 @@ function base64EncArr(aBytes: Uint8Array): string {
uint6ToB64((nUint24 >>> 18) & 63),
uint6ToB64((nUint24 >>> 12) & 63),
uint6ToB64((nUint24 >>> 6) & 63),
- uint6ToB64(nUint24 & 63)
+ uint6ToB64(nUint24 & 63),
);
nUint24 = 0;
}
@@ -114,8 +126,13 @@ function base64EncArr(aBytes: Uint8Array): string {
);
}
-/* UTF-8 array to JS string and vice versa */
-
+/**
+ * UTF-8 array to JS string and vice versa
+ *
+ * @param aBytes
+ * @deprecated use textEncoder
+ * @returns
+ */
function UTF8ArrToStr(aBytes: Uint8Array): string {
let sView = "";
let nPart;
@@ -125,40 +142,46 @@ function UTF8ArrToStr(aBytes: Uint8Array): string {
sView += String.fromCodePoint(
nPart > 251 && nPart < 254 && nIdx + 5 < nLen /* six bytes */
? /* (nPart - 252 << 30) may be not so safe in ECMAScript! So…: */
- (nPart - 252) * 1073741824 +
- ((aBytes[++nIdx] - 128) << 24) +
- ((aBytes[++nIdx] - 128) << 18) +
- ((aBytes[++nIdx] - 128) << 12) +
- ((aBytes[++nIdx] - 128) << 6) +
- aBytes[++nIdx] -
- 128
+ (nPart - 252) * 1073741824 +
+ ((aBytes[++nIdx] - 128) << 24) +
+ ((aBytes[++nIdx] - 128) << 18) +
+ ((aBytes[++nIdx] - 128) << 12) +
+ ((aBytes[++nIdx] - 128) << 6) +
+ aBytes[++nIdx] -
+ 128
: nPart > 247 && nPart < 252 && nIdx + 4 < nLen /* five bytes */
? ((nPart - 248) << 24) +
- ((aBytes[++nIdx] - 128) << 18) +
- ((aBytes[++nIdx] - 128) << 12) +
- ((aBytes[++nIdx] - 128) << 6) +
- aBytes[++nIdx] -
- 128
- : nPart > 239 && nPart < 248 && nIdx + 3 < nLen /* four bytes */
- ? ((nPart - 240) << 18) +
+ ((aBytes[++nIdx] - 128) << 18) +
((aBytes[++nIdx] - 128) << 12) +
((aBytes[++nIdx] - 128) << 6) +
aBytes[++nIdx] -
128
- : nPart > 223 && nPart < 240 && nIdx + 2 < nLen /* three bytes */
- ? ((nPart - 224) << 12) +
+ : nPart > 239 && nPart < 248 && nIdx + 3 < nLen /* four bytes */
+ ? ((nPart - 240) << 18) +
+ ((aBytes[++nIdx] - 128) << 12) +
((aBytes[++nIdx] - 128) << 6) +
aBytes[++nIdx] -
128
+ : nPart > 223 && nPart < 240 && nIdx + 2 < nLen /* three bytes */
+ ? ((nPart - 224) << 12) +
+ ((aBytes[++nIdx] - 128) << 6) +
+ aBytes[++nIdx] -
+ 128
: nPart > 191 && nPart < 224 && nIdx + 1 < nLen /* two bytes */
? ((nPart - 192) << 6) + aBytes[++nIdx] - 128
: /* nPart < 127 ? */ /* one byte */
- nPart
+ nPart,
);
}
return sView;
}
+/**
+ *
+ * @param sDOMStr
+ * @deprecated use textEncoder
+ * @returns
+ */
function strToUTF8Arr(sDOMStr: string): Uint8Array {
let nChr;
const nStrLen = sDOMStr.length;
@@ -168,7 +191,9 @@ function strToUTF8Arr(sDOMStr: string): Uint8Array {
for (let nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
nChr = sDOMStr.codePointAt(nMapIdx);
if (nChr === undefined) {
- throw Error(`No char at ${nMapIdx} on string with length: ${sDOMStr.length}`)
+ throw Error(
+ `No char at ${nMapIdx} on string with length: ${sDOMStr.length}`,
+ );
}
if (nChr >= 0x10000) {
@@ -197,7 +222,9 @@ function strToUTF8Arr(sDOMStr: string): Uint8Array {
while (nIdx < nArrLen) {
nChr = sDOMStr.codePointAt(nChrIdx);
if (nChr === undefined) {
- throw Error(`No char at ${nChrIdx} on string with length: ${sDOMStr.length}`)
+ throw Error(
+ `No char at ${nChrIdx} on string with length: ${sDOMStr.length}`,
+ );
}
if (nChr < 128) {
/* one byte */