|
| 1 | +import 'dart:io'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +import 'exception.dart'; |
| 5 | + |
| 6 | +/// Encode [value] to bytes. |
| 7 | +typedef Encoder = Uint8List Function(String value); |
| 8 | + |
| 9 | +/// Decode [bytes] to String. |
| 10 | +typedef Decoder = String Function(Uint8List bytes); |
| 11 | + |
| 12 | +//////////////////////////////////////////////////////////////////////////////// |
| 13 | +// IP4 Encoder and Decoder |
| 14 | +// TODO: support Web |
| 15 | +//////////////////////////////////////////////////////////////////////////////// |
| 16 | +Uint8List ip4Encoder(String value) { |
| 17 | + InternetAddress addr; |
| 18 | + try { |
| 19 | + addr = InternetAddress(value); |
| 20 | + } catch (_) { |
| 21 | + throw EncodeException('invalid ip4 value: $value'); |
| 22 | + } |
| 23 | + return addr.rawAddress; |
| 24 | +} |
| 25 | + |
| 26 | +String ip4Decoder(Uint8List value) { |
| 27 | + InternetAddress addr; |
| 28 | + try { |
| 29 | + addr = InternetAddress.fromRawAddress(value); |
| 30 | + } catch (_) { |
| 31 | + throw DecodeException('invalid ip4 value: $value'); |
| 32 | + } |
| 33 | + return addr.address; |
| 34 | +} |
| 35 | + |
| 36 | +//////////////////////////////////////////////////////////////////////////////// |
| 37 | +// TCP Encoder and Decoder |
| 38 | +//////////////////////////////////////////////////////////////////////////////// |
| 39 | +Uint8List tcpEncoder(String value) { |
| 40 | + var port = int.tryParse(value); |
| 41 | + if (port == null || port < 0 || port > 65535) { |
| 42 | + throw EncodeException('invalid tcp value: $value'); |
| 43 | + } |
| 44 | + return Uint8List.fromList([port >> 8, port & 0xff]); |
| 45 | +} |
| 46 | + |
| 47 | +String tcpDecoder(Uint8List value) { |
| 48 | + if (value.length != 2) { |
| 49 | + throw DecodeException('invalid tcp value: $value'); |
| 50 | + } |
| 51 | + return '${value[0] << 8 | value[1]}'; |
| 52 | +} |
| 53 | + |
| 54 | +//////////////////////////////////////////////////////////////////////////////// |
| 55 | +// IP6 Encoder and Decoder |
| 56 | +// TODO: support Web |
| 57 | +//////////////////////////////////////////////////////////////////////////////// |
| 58 | +Uint8List ip6Encoder(String value) { |
| 59 | + // TODO: support IPv4-mapped IPv6 addresses |
| 60 | + InternetAddress addr; |
| 61 | + try { |
| 62 | + addr = InternetAddress(value); |
| 63 | + } catch (_) { |
| 64 | + throw EncodeException('invalid ip6 value: $value'); |
| 65 | + } |
| 66 | + return addr.rawAddress; |
| 67 | +} |
| 68 | + |
| 69 | +String ip6Decoder(Uint8List value) { |
| 70 | + InternetAddress addr; |
| 71 | + try { |
| 72 | + addr = InternetAddress.fromRawAddress(value); |
| 73 | + } catch (_) { |
| 74 | + throw DecodeException('invalid ip6 value: $value'); |
| 75 | + } |
| 76 | + return addr.address; |
| 77 | +} |
| 78 | + |
| 79 | +//////////////////////////////////////////////////////////////////////////////// |
| 80 | +// DNS* Encoder and Decoder |
| 81 | +//////////////////////////////////////////////////////////////////////////////// |
| 82 | +Uint8List dnsEncoder(String value) => Uint8List.fromList(value.codeUnits); |
| 83 | + |
| 84 | +String dnsDecoder(Uint8List bytes) => String.fromCharCodes(bytes); |
| 85 | + |
| 86 | +//////////////////////////////////////////////////////////////////////////////// |
| 87 | +// UNIX Encoder and Decoder |
| 88 | +//////////////////////////////////////////////////////////////////////////////// |
| 89 | +Uint8List unixEncoder(String value) => Uint8List.fromList(value.codeUnits); |
| 90 | + |
| 91 | +String unixDecoder(Uint8List bytes) => String.fromCharCodes(bytes); |
0 commit comments