That fuck shit the fascists are using
1/**
2 * Source: https://github.com/hsch/blurhash-java
3 *
4 * Copyright (c) 2019 Hendrik Schnepel
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21package org.tm.archive.blurhash;
22
23import androidx.annotation.Nullable;
24
25final class Base83 {
26
27 private static final int MAX_LENGTH = 90;
28
29 private static final char[]ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~".toCharArray();
30
31 private static int indexOf(char[] a, char key) {
32 for (int i = 0; i < a.length; i++) {
33 if (a[i] == key) {
34 return i;
35 }
36 }
37 return -1;
38 }
39
40 static void encode(long value, int length, char[] buffer, int offset) {
41 int exp = 1;
42 for (int i = 1; i <= length; i++, exp *= 83) {
43 int digit = (int)(value / exp % 83);
44 buffer[offset + length - i] = ALPHABET[digit];
45 }
46 }
47
48 static int decode(String value, int fromInclusive, int toExclusive) {
49 int result = 0;
50 char[] chars = value.toCharArray();
51 for (int i = fromInclusive; i < toExclusive; i++) {
52 result = result * 83 + indexOf(ALPHABET, chars[i]);
53 }
54 return result;
55 }
56
57 static boolean isValid(@Nullable String value) {
58 if (value == null) return false;
59 final int length = value.length();
60
61 if (length == 0 || length > MAX_LENGTH) return false;
62
63 for (int i = 0; i < length; i++) {
64 if (indexOf(ALPHABET, value.charAt(i)) == -1) return false;
65 }
66
67 return true;
68 }
69
70 private Base83() {
71 }
72}
73