|
| 1 | +import 'dart:typed_data'; |
| 2 | +import 'package:ur/utils.dart'; |
| 3 | +import 'package:collection/collection.dart'; |
| 4 | + |
| 5 | +const String BYTEWORDS = |
| 6 | + 'ableacidalsoapexaquaarchatomauntawayaxisbackbaldbarnbeltbetabiasbluebodybragbrewbulbbuzzcalmcashcatschefcityclawcodecolacookcostcruxcurlcuspcyandarkdatadaysdelidicedietdoordowndrawdropdrumdulldutyeacheasyechoedgeepicevenexamexiteyesfactfairfernfigsfilmfishfizzflapflewfluxfoxyfreefrogfuelfundgalagamegeargemsgiftgirlglowgoodgraygrimgurugushgyrohalfhanghardhawkheathelphighhillholyhopehornhutsicedideaidleinchinkyintoirisironitemjadejazzjoinjoltjowljudojugsjumpjunkjurykeepkenokeptkeyskickkilnkingkitekiwiknoblamblavalazyleaflegsliarlimplionlistlogoloudloveluaulucklungmainmanymathmazememomenumeowmildmintmissmonknailnavyneednewsnextnoonnotenumbobeyoboeomitonyxopenovalowlspaidpartpeckplaypluspoempoolposepuffpumapurrquadquizraceramprealredorichroadrockroofrubyruinrunsrustsafesagascarsetssilkskewslotsoapsolosongstubsurfswantacotasktaxitenttiedtimetinytoiltombtoystriptunatwinuglyundouniturgeuservastveryvetovialvibeviewvisavoidvowswallwandwarmwaspwavewaxywebswhatwhenwhizwolfworkyankyawnyellyogayurtzapszerozestzinczonezoom'; |
| 7 | + |
| 8 | +List<int>? _wordArray; |
| 9 | + |
| 10 | +enum Style { |
| 11 | + standard, |
| 12 | + uri, |
| 13 | + minimal, |
| 14 | +} |
| 15 | + |
| 16 | +int decodeWord(String word, int wordLen) { |
| 17 | + if (word.length != wordLen) { |
| 18 | + throw ArgumentError('Invalid Bytewords.'); |
| 19 | + } |
| 20 | + |
| 21 | + const int dim = 26; |
| 22 | + |
| 23 | + // Since the first and last letters of each Byteword are unique, |
| 24 | + // we can use them as indexes into a two-dimensional lookup table. |
| 25 | + // This table is generated lazily. |
| 26 | + if (_wordArray == null) { |
| 27 | + _wordArray = List.generate(dim * dim, (i) => -1); |
| 28 | + |
| 29 | + for (int i = 0; i < 256; i++) { |
| 30 | + int bytewordOffset = i * 4; |
| 31 | + int x = BYTEWORDS[bytewordOffset].codeUnitAt(0) - 'a'.codeUnitAt(0); |
| 32 | + int y = BYTEWORDS[bytewordOffset + 3].codeUnitAt(0) - 'a'.codeUnitAt(0); |
| 33 | + int arrayOffset = y * dim + x; |
| 34 | + _wordArray![arrayOffset] = i; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // If the coordinates generated by the first and last letters are out of bounds, |
| 39 | + // or the lookup table contains -1 at the coordinates, then the word is not valid. |
| 40 | + int x = word[0].toLowerCase().codeUnitAt(0) - 'a'.codeUnitAt(0); |
| 41 | + int y = word[wordLen == 4 ? 3 : 1].toLowerCase().codeUnitAt(0) - |
| 42 | + 'a'.codeUnitAt(0); |
| 43 | + if (x < 0 || x >= dim || y < 0 || y >= dim) { |
| 44 | + throw ArgumentError('Invalid Bytewords.'); |
| 45 | + } |
| 46 | + |
| 47 | + int value = _wordArray![y * dim + x]; |
| 48 | + if (value == -1) { |
| 49 | + throw ArgumentError('Invalid Bytewords.'); |
| 50 | + } |
| 51 | + |
| 52 | + // If we're decoding a full four-letter word, verify that the two middle letters are correct. |
| 53 | + if (wordLen == 4) { |
| 54 | + int bytewordOffset = value * 4; |
| 55 | + String c1 = word[1].toLowerCase(); |
| 56 | + String c2 = word[2].toLowerCase(); |
| 57 | + if (c1 != BYTEWORDS[bytewordOffset + 1] || |
| 58 | + c2 != BYTEWORDS[bytewordOffset + 2]) { |
| 59 | + throw ArgumentError('Invalid Bytewords.'); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Successful decode. |
| 64 | + return value; |
| 65 | +} |
| 66 | + |
| 67 | +String getWord(int index) { |
| 68 | + int bytewordOffset = index * 4; |
| 69 | + return BYTEWORDS.substring(bytewordOffset, bytewordOffset + 4); |
| 70 | +} |
| 71 | + |
| 72 | +String getMinimalWord(int index) { |
| 73 | + int bytewordOffset = index * 4; |
| 74 | + return BYTEWORDS[bytewordOffset] + BYTEWORDS[bytewordOffset + 3]; |
| 75 | +} |
| 76 | + |
| 77 | +String encode(Uint8List buf, String separator) { |
| 78 | + return buf.map((byte) => getWord(byte)).join(separator); |
| 79 | +} |
| 80 | + |
| 81 | +Uint8List addCrc(Uint8List buf) { |
| 82 | + Uint8List crcBuf = crc32Bytes(buf); |
| 83 | + return Uint8List.fromList([...buf, ...crcBuf]); |
| 84 | +} |
| 85 | + |
| 86 | +String encodeWithSeparator(Uint8List buf, String separator) { |
| 87 | + Uint8List crcBuf = addCrc(buf); |
| 88 | + return encode(crcBuf, separator); |
| 89 | +} |
| 90 | + |
| 91 | +String encodeMinimal(Uint8List buf) { |
| 92 | + Uint8List crcBuf = addCrc(buf); |
| 93 | + return crcBuf.map((byte) => getMinimalWord(byte)).join(); |
| 94 | +} |
| 95 | + |
| 96 | +Uint8List decode(String s, String separator, int wordLen) { |
| 97 | + List<String> words; |
| 98 | + if (wordLen == 4) { |
| 99 | + words = s.split(separator); |
| 100 | + } else { |
| 101 | + words = partition(s, 2); |
| 102 | + } |
| 103 | + |
| 104 | + Uint8List buf = Uint8List(words.length); |
| 105 | + for (int i = 0; i < words.length; i++) { |
| 106 | + buf[i] = decodeWord(words[i], wordLen); |
| 107 | + } |
| 108 | + |
| 109 | + if (buf.length < 5) { |
| 110 | + throw ArgumentError('Invalid Bytewords.'); |
| 111 | + } |
| 112 | + |
| 113 | + // Validate checksum |
| 114 | + Uint8List body = buf.sublist(0, buf.length - 4); |
| 115 | + Uint8List bodyChecksum = buf.sublist(buf.length - 4); |
| 116 | + Uint8List checksum = crc32Bytes(body); |
| 117 | + Function listEquals = const ListEquality().equals; |
| 118 | + if (!listEquals(checksum, bodyChecksum)) { |
| 119 | + throw ArgumentError('Invalid Bytewords.'); |
| 120 | + } |
| 121 | + |
| 122 | + return body; |
| 123 | +} |
| 124 | + |
| 125 | +String encodeStyle(Style style, Uint8List bytes) { |
| 126 | + switch (style) { |
| 127 | + case Style.standard: |
| 128 | + return encodeWithSeparator(bytes, ' '); |
| 129 | + case Style.uri: |
| 130 | + return encodeWithSeparator(bytes, '-'); |
| 131 | + case Style.minimal: |
| 132 | + return encodeMinimal(bytes); |
| 133 | + default: |
| 134 | + throw ArgumentError('Invalid Bytewords style.'); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +Uint8List decodeStyle(Style style, String str) { |
| 139 | + switch (style) { |
| 140 | + case Style.standard: |
| 141 | + return decode(str, ' ', 4); |
| 142 | + case Style.uri: |
| 143 | + return decode(str, '-', 4); |
| 144 | + case Style.minimal: |
| 145 | + return decode(str, '', 2); |
| 146 | + default: |
| 147 | + throw ArgumentError('Invalid Bytewords style.'); |
| 148 | + } |
| 149 | +} |
0 commit comments