Last active
February 25, 2024 18:20
-
-
Save halka9000stg/032676b7021512ccb98546e2bc7b82e1 to your computer and use it in GitHub Desktop.
Magic Code for Cotec Binary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:convert"; | |
import "dart:typed_data"; | |
void main(){ | |
final List<int> mg = CotecBinMagic(0, 18, 3, true).asBinary; | |
print("len: ${mg.length}"); | |
print("mgc: ${mg.map<String>((int e) => e.toRadixString(16)).join(" ")}"); | |
print("chr: «${utf8.decode(mg.skip(4).toList())}»"); | |
print("ch2: «${utf8.decode(mg)}»"); | |
} | |
class CotecBinMagic { | |
final int major; | |
final int middle; | |
final int extensions; | |
final bool forCompressed; | |
CotecBinMagic(this.major, this.middle, this.extensions, this.forCompressed); | |
List<int> get asBinary => <int>[0xef, 0xfe, 0x8c, 0xfd].followedBy(utf8.encode(String.fromCharCodes(<int>[0x1f310].followedBy(toVariationSelector(this.major, true)).followedBy(<int>[0x00ad]).followedBy(toVariationSelector(this.middle, true)).followedBy(<int>[0x05be, 0x8a0f, 0x6c10, 0x2043, 0x0e1a, 0x0e35, 0x0e23, 0x1680]).followedBy(this.forCompressed ? <int>[0x200a] : <int>[]).followedBy(toVariationSelector(this.extensions, false)).followedBy(<int>[0x2001])))).toList(); | |
} | |
Uint8List int32toBytes(int value) => | |
Uint8List(4)..buffer.asByteData().setInt32(0, value, Endian.big); | |
List<int> asint4(int value) => int32toBytes(value).map<List<int>>((int el) => <int>[(el & 240) >>> 4, el & 15]).expand((List<int> e) => e).toList(); | |
List<int> toVariationSelector(int value, bool fe0nOnly) { | |
if(fe0nOnly){ | |
return asint4(value).map<int>((int e) => e + 0xfe00).joiner(0x200d).toList(); | |
}else{ | |
return int32toBytes(value).map<int>((int e) => e < 16 ? e + 0xfe00 : e + 0xe0100).joiner(0x200d).toList(); | |
} | |
} | |
extension Jointing on Iterable<int> { | |
Iterable<int> joiner(int joinerChar) => this.length < 2 ? this : this.take(this.length - 1).map<List<int>>((int e) => <int>[e, joinerChar]).expand((List<int> e) => e).followedBy(<int>[this.last]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment