Autobindings, Remove Server code from Android, Debug mode Fixes
continuous-integration/drone/pr Build is passing
Details
continuous-integration/drone/pr Build is passing
Details
parent
68c50b3c4b
commit
444c70a255
@ -1 +1 @@
|
||||
2023-02-08-16-57-v1.10.5
|
||||
v0.0.2
|
@ -1 +1 @@
|
||||
2023-02-08-21-57-v1.10.5
|
||||
v0.0.2
|
@ -0,0 +1,9 @@
|
||||
The original version of the base32 code in this library is from https://github.com/Daegalus/dart-base32
|
||||
|
||||
Copyright (c) 2012 Yulian Kuncheff
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,196 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:cwtch/third_party/base32/encoding.dart';
|
||||
|
||||
// ignore: camel_case_types
|
||||
class base32 {
|
||||
/// Takes in a [byteList] converts it to a Uint8List so that I can run
|
||||
/// bit operations on it, then outputs a [String] representation of the
|
||||
/// base32.
|
||||
static String encode(Uint8List bytesList, {Encoding encoding = Encoding.standardRFC4648}) {
|
||||
var base32Chars = EncodingUtils.getChars(encoding);
|
||||
var i = 0;
|
||||
var count = (bytesList.length ~/ 5) * 5;
|
||||
var base32str = '';
|
||||
while (i < count) {
|
||||
var v1 = bytesList[i++];
|
||||
var v2 = bytesList[i++];
|
||||
var v3 = bytesList[i++];
|
||||
var v4 = bytesList[i++];
|
||||
var v5 = bytesList[i++];
|
||||
|
||||
base32str += base32Chars[v1 >> 3] +
|
||||
base32Chars[(v1 << 2 | v2 >> 6) & 31] +
|
||||
base32Chars[(v2 >> 1) & 31] +
|
||||
base32Chars[(v2 << 4 | v3 >> 4) & 31] +
|
||||
base32Chars[(v3 << 1 | v4 >> 7) & 31] +
|
||||
base32Chars[(v4 >> 2) & 31] +
|
||||
base32Chars[(v4 << 3 | v5 >> 5) & 31] +
|
||||
base32Chars[v5 & 31];
|
||||
}
|
||||
|
||||
var remain = bytesList.length - count;
|
||||
if (remain == 1) {
|
||||
var v1 = bytesList[i];
|
||||
base32str += base32Chars[v1 >> 3] + base32Chars[(v1 << 2) & 31];
|
||||
if (EncodingUtils.getPadded(encoding)) {
|
||||
base32str += '======';
|
||||
}
|
||||
} else if (remain == 2) {
|
||||
var v1 = bytesList[i++];
|
||||
var v2 = bytesList[i];
|
||||
base32str += base32Chars[v1 >> 3] + base32Chars[(v1 << 2 | v2 >> 6) & 31] + base32Chars[(v2 >> 1) & 31] + base32Chars[(v2 << 4) & 31];
|
||||
if (EncodingUtils.getPadded(encoding)) {
|
||||
base32str += '====';
|
||||
}
|
||||
} else if (remain == 3) {
|
||||
var v1 = bytesList[i++];
|
||||
var v2 = bytesList[i++];
|
||||
var v3 = bytesList[i];
|
||||
base32str += base32Chars[v1 >> 3] + base32Chars[(v1 << 2 | v2 >> 6) & 31] + base32Chars[(v2 >> 1) & 31] + base32Chars[(v2 << 4 | v3 >> 4) & 31] + base32Chars[(v3 << 1) & 31];
|
||||
if (EncodingUtils.getPadded(encoding)) {
|
||||
base32str += '===';
|
||||
}
|
||||
} else if (remain == 4) {
|
||||
var v1 = bytesList[i++];
|
||||
var v2 = bytesList[i++];
|
||||
var v3 = bytesList[i++];
|
||||
var v4 = bytesList[i];
|
||||
base32str += base32Chars[v1 >> 3] +
|
||||
base32Chars[(v1 << 2 | v2 >> 6) & 31] +
|
||||
base32Chars[(v2 >> 1) & 31] +
|
||||
base32Chars[(v2 << 4 | v3 >> 4) & 31] +
|
||||
base32Chars[(v3 << 1 | v4 >> 7) & 31] +
|
||||
base32Chars[(v4 >> 2) & 31] +
|
||||
base32Chars[(v4 << 3) & 31];
|
||||
if (EncodingUtils.getPadded(encoding)) {
|
||||
base32str += '=';
|
||||
}
|
||||
}
|
||||
return base32str;
|
||||
}
|
||||
|
||||
static Uint8List _hexDecode(final String input) => Uint8List.fromList([
|
||||
for (int i = 0; i < input.length; i += 2) int.parse(input.substring(i, i + 2), radix: 16),
|
||||
]);
|
||||
|
||||
static String _hexEncode(final Uint8List input) => [for (int i = 0; i < input.length; i++) input[i].toRadixString(16).padLeft(2, '0')].join();
|
||||
|
||||
/// Takes in a [hex] string, converts the string to a byte list
|
||||
/// and runs a normal encode() on it. Returning a [String] representation
|
||||
/// of the base32.
|
||||
static String encodeHexString(String b32hex, {Encoding encoding = Encoding.standardRFC4648}) {
|
||||
return encode(_hexDecode(b32hex), encoding: encoding);
|
||||
}
|
||||
|
||||
/// Takes in a [utf8string], converts the string to a byte list
|
||||
/// and runs a normal encode() on it. Returning a [String] representation
|
||||
/// of the base32.
|
||||
static String encodeString(String utf8string, {Encoding encoding = Encoding.standardRFC4648}) {
|
||||
return encode(Uint8List.fromList(utf8string.codeUnits), encoding: encoding);
|
||||
}
|
||||
|
||||
/// Takes in a [base32] string and decodes it back to a [String] in hex format.
|
||||
static String decodeAsHexString(String base32, {Encoding encoding = Encoding.standardRFC4648}) {
|
||||
return _hexEncode(decode(base32, encoding: encoding));
|
||||
}
|
||||
|
||||
/// Takes in a [base32] string and decodes it back to a [String].
|
||||
static String decodeAsString(String base32, {Encoding encoding = Encoding.standardRFC4648}) {
|
||||
return decode(base32, encoding: encoding).toList().map((charCode) => String.fromCharCode(charCode)).join();
|
||||
}
|
||||
|
||||
/// Takes in a [base32] string and decodes it back to a [Uint8List] that can be
|
||||
/// converted to a hex string using hexEncode
|
||||
static Uint8List decode(String base32, {Encoding encoding = Encoding.standardRFC4648}) {
|
||||
if (base32.isEmpty) {
|
||||
return Uint8List(0);
|
||||
}
|
||||
|
||||
base32 = _pad(base32, encoding: encoding);
|
||||
|
||||
if (!_isValid(base32, encoding: encoding)) {
|
||||
throw FormatException('Invalid Base32 characters');
|
||||
}
|
||||
|
||||
if (encoding == Encoding.crockford) {
|
||||
base32 = base32.replaceAll('-', '');
|
||||
} // Handle crockford dashes.
|
||||
|
||||
var base32Decode = EncodingUtils.getDecodeMap(encoding);
|
||||
var length = base32.indexOf('=');
|
||||
if (length == -1) {
|
||||
length = base32.length;
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
var count = length >> 3 << 3;
|
||||
var bytes = <int>[];
|
||||
while (i < count) {
|
||||
var v1 = base32Decode[base32[i++]] ?? 0;
|
||||
var v2 = base32Decode[base32[i++]] ?? 0;
|
||||
var v3 = base32Decode[base32[i++]] ?? 0;
|
||||
var v4 = base32Decode[base32[i++]] ?? 0;
|
||||
var v5 = base32Decode[base32[i++]] ?? 0;
|
||||
var v6 = base32Decode[base32[i++]] ?? 0;
|
||||
var v7 = base32Decode[base32[i++]] ?? 0;
|
||||
var v8 = base32Decode[base32[i++]] ?? 0;
|
||||
bytes.add((v1 << 3 | v2 >> 2) & 255);
|
||||
bytes.add((v2 << 6 | v3 << 1 | v4 >> 4) & 255);
|
||||
bytes.add((v4 << 4 | v5 >> 1) & 255);
|
||||
bytes.add((v5 << 7 | v6 << 2 | v7 >> 3) & 255);
|
||||
bytes.add((v7 << 5 | v8) & 255);
|
||||
}
|
||||
|
||||
var remain = length - count;
|
||||
if (remain == 2) {
|
||||
var v1 = base32Decode[base32[i++]] ?? 0;
|
||||
var v2 = base32Decode[base32[i++]] ?? 0;
|
||||
bytes.add((v1 << 3 | v2 >> 2) & 255);
|
||||
} else if (remain == 4) {
|
||||
var v1 = base32Decode[base32[i++]] ?? 0;
|
||||
var v2 = base32Decode[base32[i++]] ?? 0;
|
||||
var v3 = base32Decode[base32[i++]] ?? 0;
|
||||
var v4 = base32Decode[base32[i++]] ?? 0;
|
||||
bytes.add((v1 << 3 | v2 >> 2) & 255);
|
||||
bytes.add((v2 << 6 | v3 << 1 | v4 >> 4) & 255);
|
||||
} else if (remain == 5) {
|
||||
var v1 = base32Decode[base32[i++]] ?? 0;
|
||||
var v2 = base32Decode[base32[i++]] ?? 0;
|
||||
var v3 = base32Decode[base32[i++]] ?? 0;
|
||||
var v4 = base32Decode[base32[i++]] ?? 0;
|
||||
var v5 = base32Decode[base32[i++]] ?? 0;
|
||||
bytes.add((v1 << 3 | v2 >> 2) & 255);
|
||||
bytes.add((v2 << 6 | v3 << 1 | v4 >> 4) & 255);
|
||||
bytes.add((v4 << 4 | v5 >> 1) & 255);
|
||||
} else if (remain == 7) {
|
||||
var v1 = base32Decode[base32[i++]] ?? 0;
|
||||
var v2 = base32Decode[base32[i++]] ?? 0;
|
||||
var v3 = base32Decode[base32[i++]] ?? 0;
|
||||
var v4 = base32Decode[base32[i++]] ?? 0;
|
||||
var v5 = base32Decode[base32[i++]] ?? 0;
|
||||
var v6 = base32Decode[base32[i++]] ?? 0;
|
||||
var v7 = base32Decode[base32[i++]] ?? 0;
|
||||
bytes.add((v1 << 3 | v2 >> 2) & 255);
|
||||
bytes.add((v2 << 6 | v3 << 1 | v4 >> 4) & 255);
|
||||
bytes.add((v4 << 4 | v5 >> 1) & 255);
|
||||
bytes.add((v5 << 7 | v6 << 2 | v7 >> 3) & 255);
|
||||
}
|
||||
return Uint8List.fromList(bytes);
|
||||
}
|
||||
|
||||
static bool _isValid(String b32str, {Encoding encoding = Encoding.standardRFC4648}) {
|
||||
var regex = EncodingUtils.getRegex(encoding);
|
||||
if (b32str.length % 2 != 0 || !regex.hasMatch(b32str)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static String _pad(String base32, {Encoding encoding = Encoding.standardRFC4648}) {
|
||||
if (EncodingUtils.getPadded(encoding)) {
|
||||
int neededPadding = (8 - base32.length % 8) % 8;
|
||||
return base32.padRight(base32.length + neededPadding, '=');
|
||||
}
|
||||
return base32;
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
class EncodingUtils {
|
||||
static String getChars(Encoding encoding) {
|
||||
return _encodeMap[encoding]!;
|
||||
}
|
||||
|
||||
static Map<String, int> getDecodeMap(Encoding encoding) {
|
||||
var map = _decodeMap[encoding];
|
||||
if (map != null) {
|
||||
return map;
|
||||
} else {
|
||||
var chars = _encodeMap[encoding]!;
|
||||
// ignore: omit_local_variable_types
|
||||
Map<String, int> map = {};
|
||||
for (var i = 0; i < 32; i++) {
|
||||
map[chars[i]] = i;
|
||||
}
|
||||
_decodeMap[encoding] = map;
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
static RegExp getRegex(Encoding encoding) {
|
||||
return _regexMap[encoding]!;
|
||||
}
|
||||
|
||||
static bool getPadded(Encoding encoding) {
|
||||
return _padded[encoding]!;
|
||||
}
|
||||
|
||||
static final _regexMap = {
|
||||
Encoding.standardRFC4648: RegExp(r'^[A-Z2-7=]+$'),
|
||||
Encoding.nonStandardRFC4648Lower: RegExp(r'^[a-z2-7=]+$'),
|
||||
Encoding.base32Hex: RegExp(r'^[0-9A-V=]+$'),
|
||||
Encoding.crockford: RegExp(r'^[0123456789ABCDEFGHJKMNPQRSTVWXYZ-]+$'),
|
||||
Encoding.zbase32: RegExp(r'^[ybndrfg8ejkmcpqxot1uwisza345h769]+$'),
|
||||
Encoding.geohash: RegExp(r'^[0123456789bcdefghjkmnpqrstuvwxyz=]+$')
|
||||
};
|
||||
static final _encodeMap = {
|
||||
Encoding.standardRFC4648: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
|
||||
Encoding.nonStandardRFC4648Lower: 'abcdefghijklmnopqrstuvwxyz234567',
|
||||
Encoding.base32Hex: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
|
||||
Encoding.crockford: '0123456789ABCDEFGHJKMNPQRSTVWXYZ',
|
||||
Encoding.zbase32: 'ybndrfg8ejkmcpqxot1uwisza345h769',
|
||||
Encoding.geohash: '0123456789bcdefghjkmnpqrstuvwxyz'
|
||||
};
|
||||
|
||||
static final Map<Encoding, Map<String, int>> _decodeMap = {};
|
||||
|
||||
static final _padded = {Encoding.standardRFC4648: true, Encoding.nonStandardRFC4648Lower: true, Encoding.base32Hex: true, Encoding.crockford: false, Encoding.zbase32: false, Encoding.geohash: true};
|
||||
}
|
||||
|
||||
enum Encoding { standardRFC4648, base32Hex, crockford, zbase32, geohash, nonStandardRFC4648Lower }
|
Loading…
Reference in new issue