Browse Source

spaces->tabs

Alison Watson 1 month ago
parent
commit
e3eaeeec66
100 changed files with 8834 additions and 8837 deletions
  1. 30
    30
      bundle.sh
  2. 98
    100
      leela/main.rs
  3. 286
    286
      maraiah/bin.rs
  4. 98
    98
      maraiah/bit.rs
  5. 68
    68
      maraiah/cbitfield.rs
  6. 76
    76
      maraiah/cenum.rs
  7. 20
    20
      maraiah/cksum.rs
  8. 336
    336
      maraiah/defl.rs
  9. 3
    3
      maraiah/doc.rs
  10. 84
    84
      maraiah/err.rs
  11. 33
    33
      maraiah/ffi.rs
  12. 25
    25
      maraiah/file.rs
  13. 523
    523
      maraiah/fixed.rs
  14. 122
    122
      maraiah/image.rs
  15. 96
    96
      maraiah/image/pict.rs
  16. 32
    32
      maraiah/image/pict/clut.rs
  17. 10
    10
      maraiah/image/pict/pm/area.rs
  18. 55
    55
      maraiah/image/pict/pm/head.rs
  19. 58
    59
      maraiah/image/pict/pm/ind.rs
  20. 32
    32
      maraiah/image/pict/pm/r5g5b5.rs
  21. 50
    50
      maraiah/image/pict/pm/rgb8.rs
  22. 67
    67
      maraiah/image/pict/rle.rs
  23. 38
    38
      maraiah/image/ppm.rs
  24. 24
    24
      maraiah/image/tga.rs
  25. 89
    89
      maraiah/machdr.rs
  26. 9
    9
      maraiah/map/ambi.rs
  27. 21
    21
      maraiah/map/attk.rs
  28. 32
    32
      maraiah/map/bonk.rs
  29. 77
    77
      maraiah/map/damg.rs
  30. 159
    159
      maraiah/map/data.rs
  31. 35
    35
      maraiah/map/entr.rs
  32. 23
    23
      maraiah/map/epnt.rs
  33. 26
    26
      maraiah/map/fxpx.rs
  34. 115
    115
      maraiah/map/head.rs
  35. 2
    2
      maraiah/map/iidx.rs
  36. 29
    29
      maraiah/map/lins.rs
  37. 356
    356
      maraiah/map/lite.rs
  38. 32
    32
      maraiah/map/ltfn.rs
  39. 44
    44
      maraiah/map/medi.rs
  40. 88
    88
      maraiah/map/minf.rs
  41. 173
    173
      maraiah/map/mnpx.rs
  42. 1
    1
      maraiah/map/name.rs
  43. 11
    11
      maraiah/map/note.rs
  44. 36
    36
      maraiah/map/objs.rs
  45. 26
    26
      maraiah/map/plac.rs
  46. 52
    52
      maraiah/map/plat.rs
  47. 13
    13
      maraiah/map/pnts.rs
  48. 144
    144
      maraiah/map/poly.rs
  49. 67
    67
      maraiah/map/prpx.rs
  50. 60
    60
      maraiah/map/pxpx.rs
  51. 54
    54
      maraiah/map/sids.rs
  52. 13
    13
      maraiah/map/stex.rs
  53. 38
    38
      maraiah/map/term.rs
  54. 53
    53
      maraiah/map/trig.rs
  55. 11
    11
      maraiah/map/trmf.rs
  56. 66
    66
      maraiah/map/trmg.rs
  57. 83
    83
      maraiah/map/wppx.rs
  58. 40
    40
      maraiah/meta.rs
  59. 24
    24
      maraiah/shp.rs
  60. 103
    103
      maraiah/shp/bmap.rs
  61. 79
    79
      maraiah/shp/clut.rs
  62. 42
    42
      maraiah/shp/coll.rs
  63. 42
    42
      maraiah/shp/fram.rs
  64. 55
    55
      maraiah/shp/sequ.rs
  65. 28
    28
      maraiah/snd.rs
  66. 70
    70
      maraiah/snd/defs.rs
  67. 35
    35
      maraiah/snd/snds.rs
  68. 73
    73
      maraiah/sound.rs
  69. 30
    30
      maraiah/sound/wav.rs
  70. 108
    108
      maraiah/text.rs
  71. 28
    28
      maraiah/xfer.rs
  72. 54
    54
      tests/data/map/epnt.out.1
  73. 432
    432
      tests/data/map/epnt.out.2
  74. 7
    7
      tests/data/map/minf.out
  75. 144
    144
      tests/data/map/term.out
  76. 2183
    2183
      tests/data/map/testmap.out
  77. 256
    256
      tests/data/pict/clut.out
  78. 7
    7
      tests/data/rand.rs
  79. 27
    27
      tests/defl.rs
  80. 73
    73
      tests/map.rs
  81. 11
    11
      tests/misc.rs
  82. 7
    7
      tests/pict.rs
  83. 8
    8
      tests/shp.rs
  84. 6
    6
      tests/snd.rs
  85. 45
    45
      tycho/CMakeLists.txt
  86. 67
    67
      tycho/bindings.json
  87. 45
    45
      tycho/build.rs
  88. 18
    18
      tycho/cc/interface.h
  89. 14
    14
      tycho/cc/main.cc
  90. 18
    18
      tycho/cc/mapmodel.cc
  91. 12
    12
      tycho/cc/mapprops.cc
  92. 7
    7
      tycho/cc/mapview.cc
  93. 76
    76
      tycho/cc/menu.cc
  94. 49
    49
      tycho/cc/project.cc
  95. 58
    58
      tycho/cc/tycho.h
  96. 10
    10
      tycho/source/cc.rs
  97. 113
    113
      tycho/source/gui/mapmodel.rs
  98. 17
    17
      tycho/source/gui/mapview.rs
  99. 11
    11
      tycho/source/main.rs
  100. 0
    0
      tycho/source/meta.rs

+ 30
- 30
bundle.sh View File

@@ -5,34 +5,34 @@ err_bad_arg=1
5 5
 err_bad_run=2
6 6
 
7 7
 rm_if() {
8
-   if [[ -d $1 ]];
9
-   then
10
-      rm -rf "$1" && echo "removed dir $1"
11
-   elif [[ -f $1 ]]
12
-   then
13
-      rm -f  "$1" && echo "removed file $1"
8
+	if [[ -d $1 ]];
9
+	then
10
+		rm -rf "$1" && echo "removed dir $1"
11
+	elif [[ -f $1 ]]
12
+	then
13
+		rm -f  "$1" && echo "removed file $1"
14 14
    fi
15 15
 
16 16
    return 0
17 17
 }
18 18
 perish() {
19
-   rm_if "${tmpdir}"
20
-   exit "$1"
19
+	rm_if "${tmpdir}"
20
+	exit "$1"
21 21
 }
22 22
 err() {
23
-   echo "error, dropping build"
24
-   rm_if "${appdir}"
25
-   rm_if "${dmg}"
26
-   perish $1
23
+	echo "error, dropping build"
24
+	rm_if "${appdir}"
25
+	rm_if "${dmg}"
26
+	perish $1
27 27
 }
28 28
 :() {
29
-   echo "$@"
30
-   echo
31
-   eval "$@" || err ${err_bad_run}
29
+	echo "$@"
30
+	echo
31
+	eval "$@" || err ${err_bad_run}
32 32
 }
33 33
 
34 34
 declare -A icon_names=(
35
-   [Tycho]="resources/color/pfhor-hand.png"
35
+	[Tycho]="resources/color/pfhor-hand.png"
36 36
 )
37 37
 
38 38
 name=$1
@@ -40,14 +40,14 @@ exe=$2
40 40
 
41 41
 if [[ ! $name ]]
42 42
 then
43
-   echo "program name needed (available: Tycho)"
44
-   err ${err_bad_arg}
43
+	echo "program name needed (available: Tycho)"
44
+	err ${err_bad_arg}
45 45
 fi
46 46
 
47 47
 if [[ ! $exe ]]
48 48
 then
49
-   echo "full path to executable required (ex. '$0 $name ~/bin/maraiah-tycho')"
50
-   err ${err_bad_arg}
49
+	echo "full path to executable required (ex. '$0 $name ~/bin/maraiah-tycho')"
50
+	err ${err_bad_arg}
51 51
 fi
52 52
 
53 53
 app=${name}.app
@@ -89,25 +89,25 @@ dmg=${exedir}/${name}.dmg
89 89
 
90 90
 while IFS= read -r lnk
91 91
 do
92
-   lnk=$(dirname "${lnk}")
93
-   : cp -r "${lnk}" "${appdir}/Contents/Frameworks"
92
+	lnk=$(dirname "${lnk}")
93
+	: cp -r "${lnk}" "${appdir}/Contents/Frameworks"
94 94
 done < "${exedir}"/build/maraiah-tycho-*/out/etc/link.txt
95 95
 
96 96
 echo "success: bundle written to ${appdir}"
97 97
 
98 98
 if [[ ! "$NO_DMG" ]]
99 99
 then
100
-   echo "creating the disk image..."
100
+	echo "creating the disk image..."
101 101
 
102
-   : rm_if "${dmg}"
102
+	: rm_if "${dmg}"
103 103
 
104
-   : mkdir -p "${diskdir}"
105
-   : cp -r "${appdir}" "${diskdir}"
106
-   : cp "${srcdir}/resources/Image.DS_Store" "${diskdir}/.DS_Store"
107
-   : ln -s /Applications "${diskdir}"
108
-   : hdiutil create -volname "${name}" -srcfolder "${diskdir}" "${dmg}"
104
+	: mkdir -p "${diskdir}"
105
+	: cp -r "${appdir}" "${diskdir}"
106
+	: cp "${srcdir}/resources/Image.DS_Store" "${diskdir}/.DS_Store"
107
+	: ln -s /Applications "${diskdir}"
108
+	: hdiutil create -volname "${name}" -srcfolder "${diskdir}" "${dmg}"
109 109
 
110
-   echo "success: dmg written to ${dmg}"
110
+	echo "success: dmg written to ${dmg}"
111 111
 fi
112 112
 
113 113
 perish ${err_ok}

+ 98
- 100
leela/main.rs View File

@@ -6,158 +6,156 @@ use std::{collections::HashSet, fs, io, slice::from_ref};
6 6
 /*
7 7
 fn open(path: &str) -> ResultS<io::BufReader>
8 8
 {
9
-   let fp = fs::File::open(path)?;
10
-   Ok(io::BufReader::new(fp))
9
+	let fp = fs::File::open(path)?;
10
+	Ok(io::BufReader::new(fp))
11 11
 }
12 12
 
13 13
 fn file_read<T, F>(path: &str, f: F) -> ResultS<T>
14
-   where F: FnOnce(&[u8]) -> ResultS<T>
14
+	where F: FnOnce(&[u8]) -> ResultS<T>
15 15
 {
16
-   let mm = open(path)?;
17
-   let bp = &mm[machdr::try_mac_header(&mm)..];
16
+	let mm = open(path)?;
17
+	let bp = &mm[machdr::try_mac_header(&mm)..];
18 18
 
19
-   f(bp)
19
+	f(bp)
20 20
 }
21 21
 
22 22
 fn exists(path: String) -> Result<(), String>
23 23
 {
24
-   match std::fs::metadata(path) {
25
-      Ok(_) => Ok(()),
26
-      Err(e) => Err(e.to_string()),
27
-   }
24
+	match std::fs::metadata(path) {
25
+		Ok(_) => Ok(()),
26
+		Err(e) => Err(e.to_string()),
27
+	}
28 28
 }
29 29
 
30 30
 fn each_value<F>(opt: &clap::ArgMatches<'_>,
31 31
                  name: &str,
32
-                 mut f: F)
33
-                 -> ResultS<()>
34
-   where F: FnMut(&str) -> ResultS<()>
32
+                 mut f: F) -> ResultS<()>
33
+	where F: FnMut(&str) -> ResultS<()>
35 34
 {
36
-   if let Some(values) = opt.values_of(name) {
37
-      for value in values {
38
-         f(value)?;
39
-      }
40
-   }
35
+	if let Some(values) = opt.values_of(name) {
36
+		for value in values {
37
+			f(value)?;
38
+		}
39
+	}
41 40
 
42
-   Ok(())
41
+	Ok(())
43 42
 }
44 43
 
45 44
 fn dbg_info(data: impl std::fmt::Debug)
46 45
 {
47
-   println!("{:#?}", data);
46
+	println!("{:#?}", data);
48 47
 }
49 48
 
50 49
 fn dump_map(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()>
51 50
 {
52
-   let mut cnks = HashSet::new();
51
+	let mut cnks = HashSet::new();
53 52
 
54
-   if let Some(opt_cnks) = opt.values_of("chunks") {
55
-      for typ in opt_cnks {
56
-         cnks.insert(typ);
57
-      }
58
-   }
53
+	if let Some(opt_cnks) = opt.values_of("chunks") {
54
+		for typ in opt_cnks {
55
+			cnks.insert(typ);
56
+		}
57
+	}
59 58
 
60
-   Ok(())
59
+	Ok(())
61 60
 }
62 61
 
63 62
 fn dump_shp(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()>
64 63
 {
65
-   unimplemented!();
64
+	unimplemented!();
66 65
 }
67 66
 
68 67
 fn dump_snd(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()>
69 68
 {
70
-   unimplemented!();
69
+	unimplemented!();
71 70
 }
72 71
 
73 72
 fn sub_data_c(_opt: &clap::ArgMatches<'_>) -> ResultS<()>
74 73
 {
75
-   unimplemented!();
74
+	unimplemented!();
76 75
 }
77 76
 
78 77
 fn sub_dump_c(opt: &clap::ArgMatches<'_>) -> ResultS<()>
79 78
 {
80
-   each_value(opt, "map", |f| dump_map(opt, f))?;
81
-   each_value(opt, "shp", |f| dump_shp(opt, f))?;
82
-   each_value(opt, "snd", |f| dump_snd(opt, f))?;
79
+	each_value(opt, "map", |f| dump_map(opt, f))?;
80
+	each_value(opt, "shp", |f| dump_shp(opt, f))?;
81
+	each_value(opt, "snd", |f| dump_snd(opt, f))?;
83 82
 
84
-   Ok(())
83
+	Ok(())
85 84
 }
86 85
 
87 86
 fn sub_info_c(opt: &clap::ArgMatches<'_>) -> ResultS<()>
88 87
 {
89
-   each_value(opt, "map", |f| Ok(dbg_info(file_read(f, map::read)?)))?;
90
-   each_value(opt, "shp", |f| Ok(dbg_info(file_read(f, shp::read)?)))?;
91
-   each_value(opt, "snd", |f| Ok(dbg_info(file_read(f, snd::read)?)))?;
88
+	each_value(opt, "map", |f| Ok(dbg_info(file_read(f, map::read)?)))?;
89
+	each_value(opt, "shp", |f| Ok(dbg_info(file_read(f, shp::read)?)))?;
90
+	each_value(opt, "snd", |f| Ok(dbg_info(file_read(f, snd::read)?)))?;
92 91
 
93
-   Ok(())
92
+	Ok(())
94 93
 }
95 94
 */
96 95
 
97 96
 fn main() -> ResultS<()>
98 97
 {
99
-   use std::io::prelude::*;
100
-
101
-   let inp = include_bytes!("../tests/data/map/Test.in");
102
-   let mut rd = std::io::BufReader::new(&inp[..]);
103
-
104
-   let mp = map::head::read(&mut rd).unwrap();
105
-   let en = map::entr::read_all(&mp).unwrap();
106
-   let ed = map::data::read_all(mp.head(), &en).unwrap();
107
-
108
-   write!(&mut std::fs::File::create("dicks.txt").unwrap(), "{:#?}", ed);
109
-
110
-   /*
111
-   use clap::clap_app;
112
-
113
-   let sub_data = clap_app! {
114
-      @subcommand data =>
115
-         (about: "Dumps data into a discrete folder/YAML format")
116
-   };
117
-
118
-   let sub_dump = clap_app! {
119
-      @subcommand dump =>
120
-         (about: "Dumps particular parts of data")
121
-         (@arg chunks: -c --chunks [name]... "Dumps named chunks from an entry")
122
-         (@group files =>
123
-          (@attributes +required +multiple)
124
-          (@arg map: -m --map [file]... {exists} "Loads Map files")
125
-          (@arg shp: -s --shp [file]... {exists} "Loads Shapes files")
126
-          (@arg snd: -n --snd [file]... {exists} "Loads Sounds files"))
127
-   };
128
-
129
-   let sub_info = clap_app! {
130
-      @subcommand info =>
131
-         (about: "Outputs debug info")
132
-         (@group files =>
133
-          (@attributes +required +multiple)
134
-          (@arg map: -m --map [file]... {exists} "Loads Map files")
135
-          (@arg shp: -s --shp [file]... {exists} "Loads Shapes files")
136
-          (@arg snd: -n --snd [file]... {exists} "Loads Sounds files"))
137
-   };
138
-
139
-   let opt = clap_app! {
140
-      (env!("CARGO_PKG_NAME")) =>
141
-         (version:    maraiah::meta::version())
142
-         (author:     maraiah::meta::authors().replace(':', ", "))
143
-         (about:      maraiah::meta::description())
144
-         (setting:    clap::AppSettings::SubcommandRequiredElseHelp)
145
-         (subcommand: sub_data)
146
-         (subcommand: sub_dump)
147
-         (subcommand: sub_info)
148
-   };
149
-
150
-   let opt = opt.get_matches();
151
-
152
-   match opt.subcommand() {
153
-      ("data", Some(opt)) => sub_data_c(opt)?,
154
-      ("dump", Some(opt)) => sub_dump_c(opt)?,
155
-      ("info", Some(opt)) => sub_info_c(opt)?,
156
-      _ => unreachable!(),
157
-   }
158
-   */
159
-
160
-   Ok(())
98
+	use std::io::prelude::*;
99
+
100
+	let inp = include_bytes!("../tests/data/map/Test.in");
101
+	let mut rd = std::io::BufReader::new(&inp[..]);
102
+
103
+	let mp = map::head::read(&mut rd).unwrap();
104
+	let en = map::entr::read_all(&mp).unwrap();
105
+	let ed = map::data::read_all(mp.head(), &en).unwrap();
106
+
107
+	write!(&mut std::fs::File::create("dicks.txt").unwrap(), "{:#?}", ed);
108
+
109
+	use clap::clap_app;
110
+
111
+	let sub_data = clap_app! {
112
+		@subcommand data =>
113
+			(about: "Dumps data into a discrete folder/YAML format")
114
+	};
115
+
116
+	let sub_dump = clap_app! {
117
+		@subcommand dump =>
118
+			(about: "Dumps particular parts of data")
119
+			(@arg chunks: -c --chunks [name]... "Dumps named chunks from an entry")
120
+			(@group files =>
121
+				(@attributes +required +multiple)
122
+				(@arg map: -m --map [file]... {exists} "Loads Map files")
123
+				(@arg shp: -s --shp [file]... {exists} "Loads Shapes files")
124
+				(@arg snd: -n --snd [file]... {exists} "Loads Sounds files"))
125
+	};
126
+
127
+	let sub_info = clap_app! {
128
+		@subcommand info =>
129
+			(about: "Outputs debug info")
130
+			(@group files =>
131
+				(@attributes +required +multiple)
132
+				(@arg map: -m --map [file]... {exists} "Loads Map files")
133
+				(@arg shp: -s --shp [file]... {exists} "Loads Shapes files")
134
+				(@arg snd: -n --snd [file]... {exists} "Loads Sounds files"))
135
+	};
136
+
137
+	let opt = clap_app! {
138
+		(env!("CARGO_PKG_NAME")) =>
139
+			(version:    maraiah::meta::version())
140
+			(author:     maraiah::meta::authors().replace(':', ", "))
141
+			(about:      maraiah::meta::description())
142
+			(setting:    clap::AppSettings::SubcommandRequiredElseHelp)
143
+			(subcommand: sub_data)
144
+			(subcommand: sub_dump)
145
+			(subcommand: sub_info)
146
+	};
147
+
148
+	let opt = opt.get_matches();
149
+
150
+	match opt.subcommand() {
151
+		("data", Some(opt)) => sub_data_c(opt)?,
152
+		("dump", Some(opt)) => sub_dump_c(opt)?,
153
+		("info", Some(opt)) => sub_info_c(opt)?,
154
+		_ => unreachable!(),
155
+	}
156
+	*/
157
+
158
+		Ok(())
161 159
 }
162 160
 
163 161
 // EOF

+ 286
- 286
maraiah/bin.rs View File

@@ -6,136 +6,136 @@ use std::{convert::{TryFrom, TryInto}, fmt, num::NonZeroU16};
6 6
 #[doc(hidden)]
7 7
 #[macro_export]
8 8
 macro_rules! rd_impl {
9
-   // worker, creates let statement for 16 bit numbers
10
-   (W $b:expr, $pth:path, $nam:ident, $n:expr) => {
11
-      let $nam = $pth([$b[$n], $b[$n + 1]]);
12
-   };
13
-
14
-   // worker, creates let statement for 32 bit numbers
15
-   (D $b:expr, $pth:path, $nam:ident, $n:expr) => {
16
-      let $nam = $pth([$b[$n], $b[$n + 1], $b[$n + 2], $b[$n + 3]]);
17
-   };
18
-
19
-   // big endian, u16
20
-   (BIG, $b:expr, $at:expr, $n:expr; $nam:ident, u16) => {
21
-      $crate::rd_impl!(W $b, u16::from_be_bytes, $nam, $n + $at);
22
-   };
23
-
24
-   // big endian, i16
25
-   (BIG, $b:expr, $at:expr, $n:expr; $nam:ident, i16) => {
26
-      $crate::rd_impl!(W $b, i16::from_be_bytes, $nam, $n + $at);
27
-   };
28
-
29
-   // big endian, u32
30
-   (BIG, $b:expr, $at:expr, $n:expr; $nam:ident, u32) => {
31
-      $crate::rd_impl!(D $b, u32::from_be_bytes, $nam, $n + $at);
32
-   };
33
-
34
-   // big endian, i32
35
-   (BIG, $b:expr, $at:expr, $n:expr; $nam:ident, i32) => {
36
-      $crate::rd_impl!(D $b, i32::from_be_bytes, $nam, $n + $at);
37
-   };
38
-
39
-   // little endian, u16
40
-   (LITTLE, $b:expr, $at:expr, $n:expr; $nam:ident, u16) => {
41
-      $crate::rd_impl!(W $b, u16::from_le_bytes, $nam, $n + $at);
42
-   };
43
-
44
-   // little endian, i16
45
-   (LITTLE, $b:expr, $at:expr, $n:expr; $nam:ident, i16) => {
46
-      $crate::rd_impl!(W $b, i16::from_le_bytes, $nam, $n + $at);
47
-   };
48
-
49
-   // little endian, u32
50
-   (LITTLE, $b:expr, $at:expr, $n:expr; $nam:ident, u32) => {
51
-      $crate::rd_impl!(D $b, u32::from_le_bytes, $nam, $n + $at);
52
-   };
53
-
54
-   // little endian, i32
55
-   (LITTLE, $b:expr, $at:expr, $n:expr; $nam:ident, i32) => {
56
-      $crate::rd_impl!(D $b, i32::from_le_bytes, $nam, $n + $at);
57
-   };
58
-
59
-   // either endianness, Angle
60
-   ($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, Angle) => {
61
-      $crate::rd_impl!($e, $b, $at, $n; $nam, u16);
62
-      let $nam = $crate::fixed::Angle::from_bits($nam);
63
-   };
64
-
65
-   // either endianness, Fixed
66
-   ($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, Fixed) => {
67
-      $crate::rd_impl!($e, $b, $at, $n; $nam, u32);
68
-      let $nam = $crate::fixed::Fixed::from_bits($nam);
69
-   };
70
-
71
-   // either endianness, Unit
72
-   ($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, Unit) => {
73
-      $crate::rd_impl!($e, $b, $at, $n; $nam, u16);
74
-      let $nam = $crate::fixed::Unit::from_bits($nam);
75
-   };
76
-
77
-   // either endianness, OptU16
78
-   ($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, OptU16) => {
79
-      $crate::rd_impl!($e, $b, $at, $n; $nam, u16);
80
-      let $nam = $crate::bin::OptU16::from($nam);
81
-   };
82
-
83
-   // either endianness, u16 -> usize
84
-   ($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, usize, u16) => {
85
-      $crate::rd_impl!($e, $b, $at, $n; $nam, u16);
86
-      let $nam = usize::from($nam);
87
-   };
88
-
89
-   // either endianness, u32 -> usize
90
-   ($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, usize, u32) => {
91
-      $crate::rd_impl!($e, $b, $at, $n; $nam, u32);
92
-      let $nam = $crate::bin::usize_from_u32($nam);
93
-   };
94
-
95
-   // either endianness, enum type with TryFrom
96
-   ($e:ident, $b:expr, $at:expr, $n:expr;
97
-    $nam:ident, enum, $et:ident$(::$etc:ident)*, $t:ident) => {
98
-      $crate::rd_impl!($e, $b, $at, $n; $nam, $t);
99
-      let $nam: $et$(::$etc)* = std::convert::TryFrom::try_from($nam)?;
100
-   };
101
-
102
-   // either endianness, bitflag type
103
-   ($e:ident, $b:expr, $at:expr, $n:expr;
104
-    $nam:ident, flag, $ft:ident$(::$ftc:ident)*, $t:ident) => {
105
-      $crate::rd_impl!($e, $b, $at, $n; $nam, $t);
106
-      let $nam = flag_ok!($ft$(::$ftc)*, $nam)?;
107
-   };
108
-
109
-   // no endianness, u8
110
-   ($_:ident, $b:expr, $at:expr, $n:expr; $nam:ident, u8) => {
111
-      let $nam = $b[$n + $at];
112
-   };
113
-
114
-   // no endianness, i8
115
-   ($_:ident, $b:expr, $at:expr, $n:expr; $nam:ident, i8) => {
116
-      let $nam = $b[$n + $at] as i8;
117
-   };
118
-
119
-   // no endianness, [u8]
120
-   ($_:ident, $b:expr, $at:expr, $n:expr; $rn:expr; $nam:ident, u8) => {
121
-      let $nam = &$b[$n + $at..$n + $at + $rn];
122
-   };
123
-
124
-   // no endianness, Ident
125
-   ($_:ident, $b:expr, $at:expr, $n:expr; $nam:ident, Ident) => {
126
-      $crate::rd_impl!(D $b, Ident, $nam, $n + $at);
127
-   };
128
-
129
-   // no endianness, fn([u8]) -> T
130
-   ($_:ident, $b:expr, $at:expr, $n:expr; $rn:expr;
131
-    $nam:ident, no_try, $f:expr) => {
132
-      let $nam = $f(&$b[$n + $at..$n + $at + $rn]);
133
-   };
134
-
135
-   // no endianness, fn([u8]) -> Result<T>
136
-   ($_:ident, $b:expr, $at:expr, $n:expr; $rn:expr; $nam:ident, $f:expr) => {
137
-      let $nam = $f(&$b[$n + $at..$n + $at + $rn])?;
138
-   };
9
+	// worker, creates let statement for 16 bit numbers
10
+	(W $b:expr, $pth:path, $nam:ident, $n:expr) => {
11
+		let $nam = $pth([$b[$n], $b[$n + 1]]);
12
+	};
13
+
14
+	// worker, creates let statement for 32 bit numbers
15
+	(D $b:expr, $pth:path, $nam:ident, $n:expr) => {
16
+		let $nam = $pth([$b[$n], $b[$n + 1], $b[$n + 2], $b[$n + 3]]);
17
+	};
18
+
19
+	// big endian, u16
20
+	(BIG, $b:expr, $at:expr, $n:expr; $nam:ident, u16) => {
21
+		$crate::rd_impl!(W $b, u16::from_be_bytes, $nam, $n + $at);
22
+	};
23
+
24
+	// big endian, i16
25
+	(BIG, $b:expr, $at:expr, $n:expr; $nam:ident, i16) => {
26
+		$crate::rd_impl!(W $b, i16::from_be_bytes, $nam, $n + $at);
27
+	};
28
+
29
+	// big endian, u32
30
+	(BIG, $b:expr, $at:expr, $n:expr; $nam:ident, u32) => {
31
+		$crate::rd_impl!(D $b, u32::from_be_bytes, $nam, $n + $at);
32
+	};
33
+
34
+	// big endian, i32
35
+	(BIG, $b:expr, $at:expr, $n:expr; $nam:ident, i32) => {
36
+		$crate::rd_impl!(D $b, i32::from_be_bytes, $nam, $n + $at);
37
+	};
38
+
39
+	// little endian, u16
40
+	(LITTLE, $b:expr, $at:expr, $n:expr; $nam:ident, u16) => {
41
+		$crate::rd_impl!(W $b, u16::from_le_bytes, $nam, $n + $at);
42
+	};
43
+
44
+	// little endian, i16
45
+	(LITTLE, $b:expr, $at:expr, $n:expr; $nam:ident, i16) => {
46
+		$crate::rd_impl!(W $b, i16::from_le_bytes, $nam, $n + $at);
47
+	};
48
+
49
+	// little endian, u32
50
+	(LITTLE, $b:expr, $at:expr, $n:expr; $nam:ident, u32) => {
51
+		$crate::rd_impl!(D $b, u32::from_le_bytes, $nam, $n + $at);
52
+	};
53
+
54
+	// little endian, i32
55
+	(LITTLE, $b:expr, $at:expr, $n:expr; $nam:ident, i32) => {
56
+		$crate::rd_impl!(D $b, i32::from_le_bytes, $nam, $n + $at);
57
+	};
58
+
59
+	// either endianness, Angle
60
+	($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, Angle) => {
61
+		$crate::rd_impl!($e, $b, $at, $n; $nam, u16);
62
+		let $nam = $crate::fixed::Angle::from_bits($nam);
63
+	};
64
+
65
+	// either endianness, Fixed
66
+	($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, Fixed) => {
67
+		$crate::rd_impl!($e, $b, $at, $n; $nam, u32);
68
+		let $nam = $crate::fixed::Fixed::from_bits($nam);
69
+	};
70
+
71
+	// either endianness, Unit
72
+	($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, Unit) => {
73
+		$crate::rd_impl!($e, $b, $at, $n; $nam, u16);
74
+		let $nam = $crate::fixed::Unit::from_bits($nam);
75
+	};
76
+
77
+	// either endianness, OptU16
78
+	($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, OptU16) => {
79
+		$crate::rd_impl!($e, $b, $at, $n; $nam, u16);
80
+		let $nam = $crate::bin::OptU16::from($nam);
81
+	};
82
+
83
+	// either endianness, u16 -> usize
84
+	($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, usize, u16) => {
85
+		$crate::rd_impl!($e, $b, $at, $n; $nam, u16);
86
+		let $nam = usize::from($nam);
87
+	};
88
+
89
+	// either endianness, u32 -> usize
90
+	($e:ident, $b:expr, $at:expr, $n:expr; $nam:ident, usize, u32) => {
91
+		$crate::rd_impl!($e, $b, $at, $n; $nam, u32);
92
+		let $nam = $crate::bin::usize_from_u32($nam);
93
+	};
94
+
95
+	// either endianness, enum type with TryFrom
96
+	($e:ident, $b:expr, $at:expr, $n:expr;
97
+	 $nam:ident, enum, $et:ident$(::$etc:ident)*, $t:ident) => {
98
+		$crate::rd_impl!($e, $b, $at, $n; $nam, $t);
99
+		let $nam: $et$(::$etc)* = std::convert::TryFrom::try_from($nam)?;
100
+	};
101
+
102
+	// either endianness, bitflag type
103
+	($e:ident, $b:expr, $at:expr, $n:expr;
104
+	 $nam:ident, flag, $ft:ident$(::$ftc:ident)*, $t:ident) => {
105
+		$crate::rd_impl!($e, $b, $at, $n; $nam, $t);
106
+		let $nam = flag_ok!($ft$(::$ftc)*, $nam)?;
107
+	};
108
+
109
+	// no endianness, u8
110
+	($_:ident, $b:expr, $at:expr, $n:expr; $nam:ident, u8) => {
111
+		let $nam = $b[$n + $at];
112
+	};
113
+
114
+	// no endianness, i8
115
+	($_:ident, $b:expr, $at:expr, $n:expr; $nam:ident, i8) => {
116
+		let $nam = $b[$n + $at] as i8;
117
+	};
118
+
119
+	// no endianness, [u8]
120
+	($_:ident, $b:expr, $at:expr, $n:expr; $rn:expr; $nam:ident, u8) => {
121
+		let $nam = &$b[$n + $at..$n + $at + $rn];
122
+	};
123
+
124
+	// no endianness, Ident
125
+	($_:ident, $b:expr, $at:expr, $n:expr; $nam:ident, Ident) => {
126
+		$crate::rd_impl!(D $b, Ident, $nam, $n + $at);
127
+	};
128
+
129
+	// no endianness, fn([u8]) -> T
130
+	($_:ident, $b:expr, $at:expr, $n:expr; $rn:expr;
131
+	 $nam:ident, no_try, $f:expr) => {
132
+		let $nam = $f(&$b[$n + $at..$n + $at + $rn]);
133
+	};
134
+
135
+	// no endianness, fn([u8]) -> Result<T>
136
+	($_:ident, $b:expr, $at:expr, $n:expr; $rn:expr; $nam:ident, $f:expr) => {
137
+		let $nam = $f(&$b[$n + $at..$n + $at + $rn])?;
138
+	};
139 139
 }
140 140
 
141 141
 /// Reads structured data from a byte slice.
@@ -201,12 +201,12 @@ macro_rules! rd_impl {
201 201
 /// let buffer = &[4, 0, 2, 0, 0, 0, 6];
202 202
 ///
203 203
 /// read_data! {
204
-///    endian: LITTLE, buf: buffer, size: 7, start: 0, data {
205
-///       let four = u16[0];
206
-///       let two  = u32[2];
207
-///       let six  = u8[6];
208
-///       let byte = u8[2; 4];
209
-///    }
204
+/// 	endian: LITTLE, buf: buffer, size: 7, start: 0, data {
205
+/// 		let four = u16[0];
206
+/// 		let two  = u32[2];
207
+/// 		let six  = u8[6];
208
+/// 		let byte = u8[2; 4];
209
+/// 	}
210 210
 /// }
211 211
 ///
212 212
 /// assert_eq!(four, 4_u16);
@@ -218,16 +218,16 @@ macro_rules! rd_impl {
218 218
 /// ```
219 219
 #[macro_export]
220 220
 macro_rules! read_data {
221
-   (
222
-      endian: $e:ident, buf: $b:expr, size: $sz:expr, start: $at:expr, data {
223
-         $(let $nam:ident = $t:ident$(::$tc:ident)*[$n:expr $(; $rn:expr)?]
224
-           $($ex:ident$(::$exc:ident)*)*;)*
225
-      }
226
-   ) => {
227
-      $crate::bin::check_data($b, $at + $sz)?;
228
-      $($crate::rd_impl!($e, $b, $at, $n;
229
-                         $($rn;)? $nam, $($ex$(::$exc)*,)* $t$(::$tc)*);)*
230
-   };
221
+	(
222
+		endian: $e:ident, buf: $b:expr, size: $sz:expr, start: $at:expr, data {
223
+			$(let $nam:ident = $t:ident$(::$tc:ident)*[$n:expr $(; $rn:expr)?]
224
+			  $($ex:ident$(::$exc:ident)*)*;)*
225
+		}
226
+	) => {
227
+		$crate::bin::check_data($b, $at + $sz)?;
228
+		$($crate::rd_impl!($e, $b, $at, $n;
229
+		                   $($rn;)? $nam, $($ex$(::$exc)*,)* $t$(::$tc)*);)*
230
+	};
231 231
 }
232 232
 
233 233
 /// Checks if there is enough data in `b`.
@@ -238,11 +238,11 @@ macro_rules! read_data {
238 238
 #[inline]
239 239
 pub fn check_data<T>(b: &[T], sz: usize) -> ResultS<()>
240 240
 {
241
-   if b.len() < sz {
242
-      Err(err_msg("not enough data"))
243
-   } else {
244
-      Ok(())
245
-   }
241
+	if b.len() < sz {
242
+		Err(err_msg("not enough data"))
243
+	} else {
244
+		Ok(())
245
+	}
246 246
 }
247 247
 
248 248
 /// Casts a `u32` to a `usize`.
@@ -262,7 +262,7 @@ pub fn check_data<T>(b: &[T], sz: usize) -> ResultS<()>
262 262
 #[inline]
263 263
 pub fn usize_from_u32(n: u32) -> usize
264 264
 {
265
-   usize::try_from(n).expect("platform is 16-bit")
265
+	usize::try_from(n).expect("platform is 16-bit")
266 266
 }
267 267
 
268 268
 /// Creates an `Ident` from a slice.
@@ -281,7 +281,7 @@ pub fn usize_from_u32(n: u32) -> usize
281 281
 #[inline]
282 282
 pub fn ident(b: &[u8]) -> Ident
283 283
 {
284
-   Ident(b[0..4].try_into().expect("not enough data"))
284
+	Ident(b[0..4].try_into().expect("not enough data"))
285 285
 }
286 286
 
287 287
 /// Applies `u32::from_be_bytes` to a slice.
@@ -300,7 +300,7 @@ pub fn ident(b: &[u8]) -> Ident
300 300
 #[inline]
301 301
 pub fn u32b(b: &[u8]) -> u32
302 302
 {
303
-   u32::from_be_bytes(b[0..4].try_into().expect("not enough data"))
303
+	u32::from_be_bytes(b[0..4].try_into().expect("not enough data"))
304 304
 }
305 305
 
306 306
 /// Applies `u16::from_be_bytes` to a slice.
@@ -319,7 +319,7 @@ pub fn u32b(b: &[u8]) -> u32
319 319
 #[inline]
320 320
 pub fn u16b(b: &[u8]) -> u16
321 321
 {
322
-   u16::from_be_bytes(b[0..2].try_into().expect("not enough data"))
322
+	u16::from_be_bytes(b[0..2].try_into().expect("not enough data"))
323 323
 }
324 324
 
325 325
 /// Applies `i32::from_be_bytes` to a slice.
@@ -338,7 +338,7 @@ pub fn u16b(b: &[u8]) -> u16
338 338
 #[inline]
339 339
 pub fn i32b(b: &[u8]) -> i32
340 340
 {
341
-   i32::from_be_bytes(b[0..4].try_into().expect("not enough data"))
341
+	i32::from_be_bytes(b[0..4].try_into().expect("not enough data"))
342 342
 }
343 343
 
344 344
 /// Applies `i16::from_be_bytes` to a slice.
@@ -357,7 +357,7 @@ pub fn i32b(b: &[u8]) -> i32
357 357
 #[inline]
358 358
 pub fn i16b(b: &[u8]) -> i16
359 359
 {
360
-   i16::from_be_bytes(b[0..2].try_into().expect("not enough data"))
360
+	i16::from_be_bytes(b[0..2].try_into().expect("not enough data"))
361 361
 }
362 362
 
363 363
 /// Applies a read function over a slice.
@@ -387,19 +387,19 @@ pub fn i16b(b: &[u8]) -> i16
387 387
 /// assert_eq!(rd_array(inp, read_a_u16).unwrap(), vec![7_777u16, 777u16]);
388 388
 /// ```
389 389
 pub fn rd_array<T, F>(b: &[u8], read: F) -> ResultS<Vec<T>>
390
-   where T: Sized,
391
-         F: Fn(&[u8]) -> ResultS<(T, usize)>
390
+	where T: Sized,
391
+	      F: Fn(&[u8]) -> ResultS<(T, usize)>
392 392
 {
393
-   let mut v = Vec::new();
394
-   let mut p = 0;
393
+	let mut v = Vec::new();
394
+	let mut p = 0;
395 395
 
396
-   while p < b.len() {
397
-      let (r, s) = read(&b[p..])?;
398
-      v.push(r);
399
-      p += s;
400
-   }
396
+	while p < b.len() {
397
+		let (r, s) = read(&b[p..])?;
398
+		v.push(r);
399
+		p += s;
400
+	}
401 401
 
402
-   Ok(v)
402
+	Ok(v)
403 403
 }
404 404
 
405 405
 /// Applies a read function a number of times over a slice.
@@ -420,19 +420,19 @@ pub fn rd_array<T, F>(b: &[u8], read: F) -> ResultS<Vec<T>>
420 420
 pub fn rd_array_num<T, F>(b: &[u8],
421 421
                           n: usize,
422 422
                           read: F) -> ResultS<(Vec<T>, usize)>
423
-   where T: Sized,
424
-         F: Fn(&[u8]) -> ResultS<(T, usize)>
423
+	where T: Sized,
424
+	      F: Fn(&[u8]) -> ResultS<(T, usize)>
425 425
 {
426
-   let mut v = Vec::with_capacity(n);
427
-   let mut p = 0;
426
+	let mut v = Vec::with_capacity(n);
427
+	let mut p = 0;
428 428
 
429
-   for _ in 0..n {
430
-      let (r, s) = read(&b[p..])?;
431
-      v.push(r);
432
-      p += s;
433
-   }
429
+	for _ in 0..n {
430
+		let (r, s) = read(&b[p..])?;
431
+		v.push(r);
432
+		p += s;
433
+	}
434 434
 
435
-   Ok((v, p))
435
+	Ok((v, p))
436 436
 }
437 437
 
438 438
 /// Applies a read function over a slice with an offset table.
@@ -454,150 +454,150 @@ pub fn rd_ofstable<T, F>(b: &[u8],
454 454
                          mut p: usize,
455 455
                          num: usize,
456 456
                          read: F) -> ResultS<Vec<T>>
457
-   where T: Sized,
458
-         F: Fn(&[u8]) -> ResultS<T>
457
+	where T: Sized,
458
+	      F: Fn(&[u8]) -> ResultS<T>
459 459
 {
460
-   let mut v = Vec::with_capacity(num);
460
+	let mut v = Vec::with_capacity(num);
461 461
 
462
-   for _ in 0..num {
463
-      let ofs = usize_from_u32(u32b(&b[p..p + 4]));
464
-      check_data(b, ofs)?;
465
-      v.push(read(&b[ofs..])?);
466
-      p += 4;
467
-   }
462
+	for _ in 0..num {
463
+		let ofs = usize_from_u32(u32b(&b[p..p + 4]));
464
+		check_data(b, ofs)?;
465
+		v.push(read(&b[ofs..])?);
466
+		p += 4;
467
+	}
468 468
 
469
-   Ok(v)
469
+	Ok(v)
470 470
 }
471 471
 
472 472
 impl From<u16> for OptU16
473 473
 {
474
-   #[inline]
475
-   fn from(n: u16) -> Self
476
-   {
477
-      if n == u16::max_value() {
478
-         Self(None)
479
-      } else {
480
-         Self(NonZeroU16::new(n + 1))
481
-      }
482
-   }
474
+	#[inline]
475
+	fn from(n: u16) -> Self
476
+	{
477
+		if n == u16::max_value() {
478
+			Self(None)
479
+		} else {
480
+			Self(NonZeroU16::new(n + 1))
481
+		}
482
+	}
483 483
 }
484 484
 
485 485
 impl Into<u16> for OptU16
486 486
 {
487
-   /// Returns the `u16` representation.
488
-   ///
489
-   /// # Examples
490
-   ///
491
-   /// ```
492
-   /// use maraiah::bin::OptU16;
493
-   ///
494
-   /// let u16_max = u16::max_value();
495
-   ///
496
-   /// // These type annotations are necessary.
497
-   ///
498
-   /// assert_eq!(<OptU16 as Into<u16>>::into(OptU16::from(500u16)), 500u16);
499
-   /// assert_eq!(<OptU16 as Into<u16>>::into(OptU16::from(u16_max)), u16_max);
500
-   /// assert_eq!(<OptU16 as Into<u16>>::into(OptU16::from(0u16)), 0u16);
501
-   /// ```
502
-   #[inline]
503
-   fn into(self) -> u16
504
-   {
505
-      match self.0 {
506
-         None    => u16::max_value(),
507
-         Some(n) => n.get() - 1,
508
-      }
509
-   }
487
+	/// Returns the `u16` representation.
488
+	///
489
+	/// # Examples
490
+	///
491
+	/// ```
492
+	/// use maraiah::bin::OptU16;
493
+	///
494
+	/// let u16_max = u16::max_value();
495
+	///
496
+	/// // These type annotations are necessary.
497
+	///
498
+	/// assert_eq!(<OptU16 as Into<u16>>::into(OptU16::from(500u16)), 500u16);
499
+	/// assert_eq!(<OptU16 as Into<u16>>::into(OptU16::from(u16_max)), u16_max);
500
+	/// assert_eq!(<OptU16 as Into<u16>>::into(OptU16::from(0u16)), 0u16);
501
+	/// ```
502
+	#[inline]
503
+	fn into(self) -> u16
504
+	{
505
+		match self.0 {
506
+			None    => u16::max_value(),
507
+			Some(n) => n.get() - 1,
508
+		}
509
+	}
510 510
 }
511 511
 
512 512
 impl OptU16
513 513
 {
514
-   /// Creates an `OptU16` representing `None`.
515
-   ///
516
-   /// # Examples
517
-   ///
518
-   /// ```
519
-   /// use maraiah::bin::OptU16;
520
-   ///
521
-   /// assert_eq!(OptU16::none(), OptU16::from(u16::max_value()));
522
-   /// ```
523
-   #[inline]
524
-   pub const fn none() -> Self {Self(None)}
525
-
526
-   /// Returns the `Option` representation.
527
-   ///
528
-   /// # Examples
529
-   ///
530
-   /// ```
531
-   /// use maraiah::bin::OptU16;
532
-   ///
533
-   /// assert_eq!(OptU16::from(500u16).get(), Some(500u16));
534
-   /// assert_eq!(OptU16::from(u16::max_value()).get(), None);
535
-   /// assert_eq!(OptU16::from(0u16).get(), Some(0u16));
536
-   /// ```
537
-   #[inline]
538
-   pub fn get(self) -> Option<u16>
539
-   {
540
-      match self.0 {
541
-         None    => None,
542
-         Some(n) => Some(n.get() - 1),
543
-      }
544
-   }
545
-
546
-   /// Return the memory representation of this integer as a byte array
547
-   /// in big-endian (network) byte order.
548
-   #[inline]
549
-   pub fn to_be_bytes(self) -> [u8; 2]
550
-   {
551
-      <Self as Into<u16>>::into(self).to_be_bytes()
552
-   }
553
-
554
-   /// Return the memory representation of this integer as a byte array
555
-   /// in little-endian byte order.
556
-   #[inline]
557
-   pub fn to_le_bytes(self) -> [u8; 2]
558
-   {
559
-      <Self as Into<u16>>::into(self).to_le_bytes()
560
-   }
514
+	/// Creates an `OptU16` representing `None`.
515
+	///
516
+	/// # Examples
517
+	///
518
+	/// ```
519
+	/// use maraiah::bin::OptU16;
520
+	///
521
+	/// assert_eq!(OptU16::none(), OptU16::from(u16::max_value()));
522
+	/// ```
523
+	#[inline]
524
+	pub const fn none() -> Self {Self(None)}
525
+
526
+	/// Returns the `Option` representation.
527
+	///
528
+	/// # Examples
529
+	///
530
+	/// ```
531
+	/// use maraiah::bin::OptU16;
532
+	///
533
+	/// assert_eq!(OptU16::from(500u16).get(), Some(500u16));
534
+	/// assert_eq!(OptU16::from(u16::max_value()).get(), None);
535
+	/// assert_eq!(OptU16::from(0u16).get(), Some(0u16));
536
+	/// ```
537
+	#[inline]
538
+	pub fn get(self) -> Option<u16>
539
+	{
540
+		match self.0 {
541
+			None    => None,
542
+			Some(n) => Some(n.get() - 1),
543
+		}
544
+	}
545
+
546
+	/// Return the memory representation of this integer as a byte array
547
+	/// in big-endian (network) byte order.
548
+	#[inline]
549
+	pub fn to_be_bytes(self) -> [u8; 2]
550
+	{
551
+		<Self as Into<u16>>::into(self).to_be_bytes()
552
+	}
553
+
554
+	/// Return the memory representation of this integer as a byte array
555
+	/// in little-endian byte order.
556
+	#[inline]
557
+	pub fn to_le_bytes(self) -> [u8; 2]
558
+	{
559
+		<Self as Into<u16>>::into(self).to_le_bytes()
560
+	}
561 561
 }
562 562
 
563 563
 impl fmt::Display for OptU16
564 564
 {
565
-   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
566
-   {
567
-      match self.get() {
568
-         None    => write!(f, "None"),
569
-         Some(n) => write!(f, "Some({})", n),
570
-      }
571
-   }
565
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
566
+	{
567
+		match self.get() {
568
+			None    => write!(f, "None"),
569
+			Some(n) => write!(f, "Some({})", n),
570
+		}
571
+	}
572 572
 }
573 573
 
574 574
 impl fmt::Debug for OptU16
575 575
 {
576
-   fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
577
-   {
578
-      match self.get() {
579
-         None    => write!(f, "OptU16::none()"),
580
-         Some(n) => write!(f, "OptU16::from({})", n),
581
-      }
582
-   }
576
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
577
+	{
578
+		match self.get() {
579
+			None    => write!(f, "OptU16::none()"),
580
+			Some(n) => write!(f, "OptU16::from({})", n),
581
+		}
582
+	}
583 583
 }
584 584
 
585 585
 impl PartialEq<[u8; 4]> for Ident
586 586
 {
587
-   #[inline]
588
-   fn eq(&self, o: &[u8; 4]) -> bool {self.0 == *o}
587
+	#[inline]
588
+	fn eq(&self, o: &[u8; 4]) -> bool {self.0 == *o}
589 589
 }
590 590
 
591 591
 impl<'a> PartialEq<[u8; 4]> for &'a Ident
592 592
 {
593
-   #[inline]
594
-   fn eq(&self, o: &[u8; 4]) -> bool {PartialEq::eq(*self, o)}
593
+	#[inline]
594
+	fn eq(&self, o: &[u8; 4]) -> bool {PartialEq::eq(*self, o)}
595 595
 }
596 596
 
597 597
 impl<'a> PartialEq<&'a [u8; 4]> for Ident
598 598
 {
599
-   #[inline]
600
-   fn eq(&self, o: &&'a [u8; 4]) -> bool {PartialEq::eq(self, *o)}
599
+	#[inline]
600
+	fn eq(&self, o: &&'a [u8; 4]) -> bool {PartialEq::eq(self, *o)}
601 601
 }
602 602
 
603 603
 /// A four-character-code identifier.

+ 98
- 98
maraiah/bit.rs View File

@@ -11,152 +11,152 @@ use crate::err::*;
11 11
 /// `(cr_bit + width - 1) / 8` would overflow an index into `b`.
12 12
 pub fn read_bits_b(b: &[u8], cr_bit: usize, width: u8) -> ResultS<u64>
13 13
 {
14
-   if width == 0 {
15
-      return Ok(0);
16
-   }
14
+	if width == 0 {
15
+		return Ok(0);
16
+	}
17 17
 
18
-   if width > 64 {
19
-      bail!("invalid number of bits");
20
-   }
18
+	if width > 64 {
19
+		bail!("invalid number of bits");
20
+	}
21 21
 
22
-   let last = (cr_bit + usize::from(width) - 1) / 8;
22
+	let last = (cr_bit + usize::from(width) - 1) / 8;
23 23
 
24
-   if last >= b.len() {
25
-      bail!("not enough data");
26
-   }
24
+	if last >= b.len() {
25
+		bail!("not enough data");
26
+	}
27 27
 
28
-   let mut byte_ptr = cr_bit / 8;
29
-   let mut bits_ptr = cr_bit % 8;
28
+	let mut byte_ptr = cr_bit / 8;
29
+	let mut bits_ptr = cr_bit % 8;
30 30
 
31
-   let mut res = 0;
31
+	let mut res = 0;
32 32
 
33
-   for _ in 0..width {
34
-      res <<= 1;
33
+	for _ in 0..width {
34
+		res <<= 1;
35 35
 
36
-      if b[byte_ptr] & (1 << bits_ptr) != 0 {
37
-         res |= 1;
38
-      }
36
+		if b[byte_ptr] & (1 << bits_ptr) != 0 {
37
+			res |= 1;
38
+		}
39 39
 
40
-      bits_ptr += 1;
40
+		bits_ptr += 1;
41 41
 
42
-      if bits_ptr > 7 {
43
-         bits_ptr  = 0;
44
-         byte_ptr += 1;
45
-      }
46
-   }
42
+		if bits_ptr > 7 {
43
+			bits_ptr  = 0;
44
+			byte_ptr += 1;
45
+		}
46
+	}
47 47
 
48
-   Ok(res)
48
+	Ok(res)
49 49
 }
50 50
 
51 51
 /// The same as `read_bits_b`, but least-significant bit first.
52 52
 pub fn read_bits_l(b: &[u8], cr_bit: usize, width: u8) -> ResultS<u64>
53 53
 {
54
-   if width == 0 {
55
-      Ok(0)
56
-   } else {
57
-      let res = read_bits_b(b, cr_bit, width)?;
54
+	if width == 0 {
55
+		Ok(0)
56
+	} else {
57
+		let res = read_bits_b(b, cr_bit, width)?;
58 58
 
59
-      Ok(reverse_bits(res) >> (64 - width))
60
-   }
59
+		Ok(reverse_bits(res) >> (64 - width))
60
+	}
61 61
 }
62 62
 
63 63
 // FIXME: change this to u64::reverse_bits when stabilized
64 64
 const fn reverse_bits(v: u64) -> u64
65 65
 {
66
-   let v = v >> 1 & 0x5555_5555_5555_5555 | ((v & 0x5555_5555_5555_5555) << 1);
67
-   let v = v >> 2 & 0x3333_3333_3333_3333 | ((v & 0x3333_3333_3333_3333) << 2);
68
-   let v = v >> 4 & 0x0F0F_0F0F_0F0F_0F0F | ((v & 0x0F0F_0F0F_0F0F_0F0F) << 4);
69
-   v.swap_bytes()
66
+	let v = v >> 1 & 0x5555_5555_5555_5555 | ((v & 0x5555_5555_5555_5555) << 1);
67
+	let v = v >> 2 & 0x3333_3333_3333_3333 | ((v & 0x3333_3333_3333_3333) << 2);
68
+	let v = v >> 4 & 0x0F0F_0F0F_0F0F_0F0F | ((v & 0x0F0F_0F0F_0F0F_0F0F) << 4);
69
+	v.swap_bytes()
70 70
 }
71 71
 
72 72
 #[test]
73 73
 fn bit_tests()
74 74
 {
75
-   const INPUT: &[u8] = &[0b01100101, 0b10101010, 0b00010000, 0b00000000,
76
-                          0b11111111, 0b11100001, 0b10101100, 0b00110011,
77
-                          0b10100101, 0b11100000, 0b00000111, 0b00000001,
78
-                          0b11001010, 0b10101111, 0b00101011, 0b01101010,
79
-                          0b11010101, 0b10100011, 0b01010101, 0b11000001];
75
+	const INPUT: &[u8] = &[0b01100101, 0b10101010, 0b00010000, 0b00000000,
76
+	                       0b11111111, 0b11100001, 0b10101100, 0b00110011,
77
+	                       0b10100101, 0b11100000, 0b00000111, 0b00000001,
78
+	                       0b11001010, 0b10101111, 0b00101011, 0b01101010,
79
+	                       0b11010101, 0b10100011, 0b01010101, 0b11000001];
80 80
 
81
-   let mut p = 0;
81
+	let mut p = 0;
82 82
 
83
-   let n = read_bits_b(INPUT, p, 3).unwrap();
84
-   assert_eq!(n, 0b101);
85
-   p += 3;
83
+	let n = read_bits_b(INPUT, p, 3).unwrap();
84
+	assert_eq!(n, 0b101);
85
+	p += 3;
86 86
 
87
-   let n = read_bits_b(INPUT, p, 63).unwrap();
88
-   assert_eq!(n, 0b001100101010100001000000000001111111110000111001101011100110010);
89
-   p += 63;
87
+	let n = read_bits_b(INPUT, p, 63).unwrap();
88
+	assert_eq!(n, 0b001100101010100001000000000001111111110000111001101011100110010);
89
+	p += 63;
90 90
 
91
-   let n = read_bits_b(INPUT, p, 4).unwrap();
92
-   assert_eq!(n, 0b1001);
93
-   p += 4;
91
+	let n = read_bits_b(INPUT, p, 4).unwrap();
92
+	assert_eq!(n, 0b1001);
93
+	p += 4;
94 94
 
95
-   let n = read_bits_b(INPUT, p, 7).unwrap();
96
-   assert_eq!(n, 0b0100000);
97
-   p += 7;
95
+	let n = read_bits_b(INPUT, p, 7).unwrap();
96
+	assert_eq!(n, 0b0100000);
97
+	p += 7;
98 98
 
99
-   let n = read_bits_b(INPUT, p, 17).unwrap();
100
-   assert_eq!(n, 0b11111100000100000);
101
-   p += 17;
99
+	let n = read_bits_b(INPUT, p, 17).unwrap();
100
+	assert_eq!(n, 0b11111100000100000);
101
+	p += 17;
102 102
 
103
-   let n = read_bits_b(INPUT, p, 27).unwrap();
104
-   assert_eq!(n, 0b000101001111110101110101000);
105
-   p += 27;
103
+	let n = read_bits_b(INPUT, p, 27).unwrap();
104
+	assert_eq!(n, 0b000101001111110101110101000);
105
+	p += 27;
106 106
 
107
-   let n = read_bits_b(INPUT, p, 33).unwrap();
108
-   assert_eq!(n, 0b101011010101011110001011010101010);
109
-   p += 33;
107
+	let n = read_bits_b(INPUT, p, 33).unwrap();
108
+	assert_eq!(n, 0b101011010101011110001011010101010);
109
+	p += 33;
110 110
 
111
-   let n = read_bits_b(INPUT, p, 6).unwrap();
112
-   assert_eq!(n, 0b000011);
113
-   p += 6;
111
+	let n = read_bits_b(INPUT, p, 6).unwrap();
112
+	assert_eq!(n, 0b000011);
113
+	p += 6;
114 114
 
115
-   let e = read_bits_b(INPUT, p, 1);
116
-   assert!(if let Err(_) = e {true} else {false});
115
+	let e = read_bits_b(INPUT, p, 1);
116
+	assert!(if let Err(_) = e {true} else {false});
117 117
 
118
-   let e = read_bits_b(INPUT, p, 2);
119
-   assert!(if let Err(_) = e {true} else {false});
118
+	let e = read_bits_b(INPUT, p, 2);
119
+	assert!(if let Err(_) = e {true} else {false});
120 120
 
121
-   let mut p = 0;
121
+	let mut p = 0;
122 122
 
123
-   let n = read_bits_l(INPUT, 0, 3).unwrap();
124
-   assert_eq!(n, 0b101);
125
-   p += 3;
123
+	let n = read_bits_l(INPUT, 0, 3).unwrap();
124
+	assert_eq!(n, 0b101);
125
+	p += 3;
126 126
 
127
-   let n = read_bits_l(INPUT, p, 63).unwrap();
128
-   assert_eq!(n, 0b010011001110101100111000011111111100000000000100001010101001100);
129
-   p += 63;
127
+	let n = read_bits_l(INPUT, p, 63).unwrap();
128
+	assert_eq!(n, 0b010011001110101100111000011111111100000000000100001010101001100);
129
+	p += 63;
130 130
 
131
-   let n = read_bits_l(INPUT, p, 4).unwrap();
132
-   assert_eq!(n, 0b1001);
133
-   p += 4;
131
+	let n = read_bits_l(INPUT, p, 4).unwrap();
132
+	assert_eq!(n, 0b1001);
133
+	p += 4;
134 134
 
135
-   let n = read_bits_l(INPUT, p, 7).unwrap();
136
-   assert_eq!(n, 0b0000010);
137
-   p += 7;
135
+	let n = read_bits_l(INPUT, p, 7).unwrap();
136
+	assert_eq!(n, 0b0000010);
137
+	p += 7;
138 138
 
139
-   let n = read_bits_l(INPUT, p, 17).unwrap();
140
-   assert_eq!(n, 0b00000100000111111);
141
-   p += 17;
139
+	let n = read_bits_l(INPUT, p, 17).unwrap();
140
+	assert_eq!(n, 0b00000100000111111);
141
+	p += 17;
142 142
 
143
-   let n = read_bits_l(INPUT, p, 27).unwrap();
144
-   assert_eq!(n, 0b000101011101011111100101000);
145
-   p += 27;
143
+	let n = read_bits_l(INPUT, p, 27).unwrap();
144
+	assert_eq!(n, 0b000101011101011111100101000);
145
+	p += 27;
146 146
 
147
-   let n = read_bits_l(INPUT, p, 33).unwrap();
148
-   assert_eq!(n, 0b010101010110100011110101010110101);
149
-   p += 33;
147
+	let n = read_bits_l(INPUT, p, 33).unwrap();
148
+	assert_eq!(n, 0b010101010110100011110101010110101);
149
+	p += 33;
150 150
 
151
-   let n = read_bits_l(INPUT, p, 6).unwrap();
152
-   assert_eq!(n, 0b110000);
153
-   p += 6;
151
+	let n = read_bits_l(INPUT, p, 6).unwrap();
152
+	assert_eq!(n, 0b110000);
153
+	p += 6;
154 154
 
155
-   let e = read_bits_l(INPUT, p, 1);
156
-   assert!(if let Err(_) = e {true} else {false});
155
+	let e = read_bits_l(INPUT, p, 1);
156
+	assert!(if let Err(_) = e {true} else {false});
157 157
 
158
-   let e = read_bits_l(INPUT, p, 2);
159
-   assert!(if let Err(_) = e {true} else {false});
158
+	let e = read_bits_l(INPUT, p, 2);
159
+	assert!(if let Err(_) = e {true} else {false});
160 160
 }
161 161
 
162 162
 // EOF

+ 68
- 68
maraiah/cbitfield.rs View File

@@ -4,85 +4,85 @@
4 4
 #[macro_export]
5 5
 macro_rules! c_bitfield
6 6
 {
7
-   (
8
-      $(#[$outer:meta])*
9
-      pub struct $t:ident: $ti:ty {
10
-         $(
11
-            $(#[$inner:ident $($args:tt)*])*
12
-            $f:ident = $v:expr
13
-         ),+
14
-         $(,)?
15
-      }
16
-   ) => {
17
-      bitflags! {
18
-         $(#[$outer])*
19
-         pub struct $t: $ti {
20
-            $(
21
-               $(#[$inner $($args)*])*
22
-               const $f = 1 << $v;
23
-            )+
24
-         }
25
-      }
7
+	(
8
+		$(#[$outer:meta])*
9
+		pub struct $t:ident: $ti:ty {
10
+			$(
11
+				$(#[$inner:ident $($args:tt)*])*
12
+				$f:ident = $v:expr
13
+			),+
14
+			$(,)?
15
+		}
16
+	) => {
17
+		bitflags! {
18
+			$(#[$outer])*
19
+			pub struct $t: $ti {
20
+				$(
21
+					$(#[$inner $($args)*])*
22
+					const $f = 1 << $v;
23
+				)+
24
+			}
25
+		}
26 26
 
27
-      #[allow(unused_qualifications)]
28
-      impl std::str::FromStr for $t
29
-      {
30
-         type Err = $crate::err::ParseFlagError;
27
+		#[allow(unused_qualifications)]
28
+		impl std::str::FromStr for $t
29
+		{
30
+			type Err = $crate::err::ParseFlagError;
31 31
 
32
-         fn from_str(s: &str) -> Result<Self, Self::Err>
33
-         {
34
-            let mut flags = Self::empty();
32
+			fn from_str(s: &str) -> Result<Self, Self::Err>
33
+			{
34
+				let mut flags = Self::empty();
35 35
 
36
-            for s in s.split('|') {
37
-               match s {
38
-                  $(
39
-                     stringify!($f) => flags.insert(Self::$f),
40
-                  )+
41
-                  "(none)" => (),
42
-                  _        => return Err(Self::Err::new(stringify!($t)))
43
-               }
44
-            }
36
+				for s in s.split('|') {
37
+					match s {
38
+						$(
39
+							stringify!($f) => flags.insert(Self::$f),
40
+						)+
41
+						"(none)" => (),
42
+						_        => return Err(Self::Err::new(stringify!($t)))
43
+					}
44
+				}
45 45
 
46
-            Ok(flags)
47
-         }
48
-      }
49
-   }
46
+				Ok(flags)
47
+			}
48
+		}
49
+	}
50 50
 }
51 51
 
52 52
 #[cfg(test)]
53 53
 mod test
54 54
 {
55
-   use crate::err::ParseFlagError;
56
-   use std::str::FromStr;
55
+	use crate::err::ParseFlagError;
56
+	use std::str::FromStr;
57 57
 
58
-   c_bitfield! {
59
-      pub struct TestFlag: u16 {
60
-         ZERO = 0,
61
-         ONE  = 1,
62
-         TWO  = 2,
63
-      }
64
-   }
58
+	c_bitfield! {
59
+		pub struct TestFlag: u16 {
60
+			ZERO = 0,
61
+			ONE  = 1,
62
+			TWO  = 2,
63
+		}
64
+	}
65 65
 
66
-   #[test]
67
-   fn c_bitfield()
68
-   {
69
-      assert_eq!(TestFlag::from_bits(0), Some(TestFlag::empty()));
70
-      assert_eq!(TestFlag::from_bits(1), Some(TestFlag::ZERO));
71
-      assert_eq!(TestFlag::from_bits(2), Some(TestFlag::ONE));
72
-      assert_eq!(TestFlag::from_bits(4), Some(TestFlag::TWO));
73
-      assert_eq!(TestFlag::from_bits(8), None);
74
-      assert_eq!(TestFlag::from_str("(none)"), Ok(TestFlag::empty()));
75
-      assert_eq!(TestFlag::from_str("ZERO"), Ok(TestFlag::ZERO));
76
-      assert_eq!(TestFlag::from_str("ONE"), Ok(TestFlag::ONE));
77
-      assert_eq!(TestFlag::from_str("TWO"), Ok(TestFlag::TWO));
78
-      assert_eq!(TestFlag::from_str("ZERO|ONE|TWO"), Ok(TestFlag::all()));
79
-      assert_eq!(TestFlag::from_str("TWO|ZERO|ONE"), Ok(TestFlag::all()));
80
-      assert_eq!(TestFlag::from_str("ONE|ONE|ONE"), Ok(TestFlag::ONE));
81
-      assert_eq!(TestFlag::from_str("(none)|(none)"), Ok(TestFlag::empty()));
82
-      assert_eq!(TestFlag::from_str("(none)|ONE"), Ok(TestFlag::ONE));
83
-      assert_eq!(TestFlag::from_str("THREE"),
84
-                 Err(ParseFlagError::new("TestFlag")));
85
-   }
66
+	#[test]
67
+	fn c_bitfield()
68
+	{
69
+		assert_eq!(TestFlag::from_bits(0), Some(TestFlag::empty()));
70
+		assert_eq!(TestFlag::from_bits(1), Some(TestFlag::ZERO));
71
+		assert_eq!(TestFlag::from_bits(2), Some(TestFlag::ONE));
72
+		assert_eq!(TestFlag::from_bits(4), Some(TestFlag::TWO));
73
+		assert_eq!(TestFlag::from_bits(8), None);
74
+		assert_eq!(TestFlag::from_str("(none)"), Ok(TestFlag::empty()));
75
+		assert_eq!(TestFlag::from_str("ZERO"), Ok(TestFlag::ZERO));
76
+		assert_eq!(TestFlag::from_str("ONE"), Ok(TestFlag::ONE));
77
+		assert_eq!(TestFlag::from_str("TWO"), Ok(TestFlag::TWO));
78
+		assert_eq!(TestFlag::from_str("ZERO|ONE|TWO"), Ok(TestFlag::all()));
79
+		assert_eq!(TestFlag::from_str("TWO|ZERO|ONE"), Ok(TestFlag::all()));
80
+		assert_eq!(TestFlag::from_str("ONE|ONE|ONE"), Ok(TestFlag::ONE));
81
+		assert_eq!(TestFlag::from_str("(none)|(none)"), Ok(TestFlag::empty()));
82
+		assert_eq!(TestFlag::from_str("(none)|ONE"), Ok(TestFlag::ONE));
83
+		assert_eq!(TestFlag::from_str("THREE"),
84
+		           Err(ParseFlagError::new("TestFlag")));
85
+	}
86 86
 }
87 87
 
88 88
 // EOF

+ 76
- 76
maraiah/cenum.rs View File

@@ -16,11 +16,11 @@
16 16
 /// use std::convert::TryFrom;
17 17
 ///
18 18
 /// c_enum! {
19
-///    enum MyEnum: u16 {
20
-///       Zero = 0,
21
-///       One  = 1,
22
-///       Two  = 2
23
-///    }
19
+/// 	enum MyEnum: u16 {
20
+/// 		Zero = 0,
21
+/// 		One  = 1,
22
+/// 		Two  = 2
23
+/// 	}
24 24
 /// }
25 25
 ///
26 26
 /// assert_eq!(MyEnum::try_from(0), Ok(MyEnum::Zero));
@@ -33,88 +33,88 @@
33 33
 #[macro_export]
34 34
 macro_rules! c_enum
35 35
 {
36
-   (
37
-      $(#[$outer:meta])*
38
-      $vi:vis enum $t:ident: $ti:ident {
39
-         $(
40
-            $(#[$inner:meta])*
41
-            $en:ident = $va:expr
42
-         ),+
43
-         $(,)?
44
-      }
45
-   ) => {
46
-      $(#[$outer])*
47
-      #[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
48
-      #[repr($ti)]
49
-      $vi enum $t {
50
-         $(
51
-            $(#[$inner])*
52
-            $en = $va,
53
-         )+
54
-      }
36
+	(
37
+		$(#[$outer:meta])*
38
+		$vi:vis enum $t:ident: $ti:ident {
39
+			$(
40
+				$(#[$inner:meta])*
41
+				$en:ident = $va:expr
42
+			),+
43
+			$(,)?
44
+		}
45
+	) => {
46
+		$(#[$outer])*
47
+		#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
48
+		#[repr($ti)]
49
+		$vi enum $t {
50
+			$(
51
+				$(#[$inner])*
52
+				$en = $va,
53
+			)+
54
+		}
55 55
 
56
-      #[allow(unused_qualifications)]
57
-      impl std::convert::TryFrom<$ti> for $t
58
-      {
59
-         type Error = $crate::err::ReprError;
56
+		#[allow(unused_qualifications)]
57
+		impl std::convert::TryFrom<$ti> for $t
58
+		{
59
+			type Error = $crate::err::ReprError;
60 60
 
61
-         /// Returns, if representable, the variant of `Self` from `n`.
62
-         fn try_from(n: $ti) -> Result<Self, Self::Error>
63
-         {
64
-            match n {
65
-               $($va => Ok($t::$en),)+
66
-               n => Err(Self::Error::new(stringify!($t), n))
67
-            }
68
-         }
69
-      }
61
+			/// Returns, if representable, the variant of `Self` from `n`.
62
+			fn try_from(n: $ti) -> Result<Self, Self::Error>
63
+			{
64
+				match n {
65
+					$($va => Ok($t::$en),)+
66
+					n => Err(Self::Error::new(stringify!($t), n))
67
+				}
68
+			}
69
+		}
70 70
 
71
-      #[allow(unused_qualifications)]
72
-      impl std::str::FromStr for $t
73
-      {
74
-         type Err = $crate::err::ParseEnumError;
71
+		#[allow(unused_qualifications)]
72
+		impl std::str::FromStr for $t
73
+		{
74
+			type Err = $crate::err::ParseEnumError;
75 75
 
76
-         fn from_str(s: &str) -> Result<Self, Self::Err>
77
-         {
78
-            match s {
79
-               $(
80
-                  stringify!($en) => Ok($t::$en),
81
-               )+
82
-               _ => Err(Self::Err::new(stringify!($t)))
83
-            }
84
-         }
85
-      }
86
-   };
76
+			fn from_str(s: &str) -> Result<Self, Self::Err>
77
+			{
78
+				match s {
79
+					$(
80
+						stringify!($en) => Ok($t::$en),
81
+					)+
82
+					_ => Err(Self::Err::new(stringify!($t)))
83
+				}
84
+			}
85
+		}
86
+	};
87 87
 }
88 88
 
89 89
 #[cfg(test)]
90 90
 mod test
91 91
 {
92
-   use crate::err::{ParseEnumError, ReprError};
93
-   use std::{convert::TryFrom, str::FromStr};
92
+	use crate::err::{ParseEnumError, ReprError};
93
+	use std::{convert::TryFrom, str::FromStr};
94 94
 
95
-   c_enum! {
96
-      enum TestEnum: u16 {
97
-         Zero = 0,
98
-         One  = 1,
99
-         Two  = 2,
100
-      }
101
-   }
95
+	c_enum! {
96
+		enum TestEnum: u16 {
97
+			Zero = 0,
98
+			One  = 1,
99
+			Two  = 2,
100
+		}
101
+	}
102 102
 
103
-   #[test]
104
-   fn c_enum()
105
-   {
106
-      assert_eq!(TestEnum::try_from(0), Ok(TestEnum::Zero));
107
-      assert_eq!(TestEnum::try_from(1), Ok(TestEnum::One));
108
-      assert_eq!(TestEnum::try_from(2), Ok(TestEnum::Two));
109
-      assert_eq!(TestEnum::try_from(3), Err(ReprError::new("TestEnum", 3)));
110
-      assert_eq!(TestEnum::try_from(4), Err(ReprError::new("TestEnum", 4)));
111
-      assert_eq!(TestEnum::try_from(5), Err(ReprError::new("TestEnum", 5)));
112
-      assert_eq!(TestEnum::from_str("Zero"), Ok(TestEnum::Zero));
113
-      assert_eq!(TestEnum::from_str("One"), Ok(TestEnum::One));
114
-      assert_eq!(TestEnum::from_str("Two"), Ok(TestEnum::Two));
115
-      assert_eq!(TestEnum::from_str("Three"),
116
-                 Err(ParseEnumError::new("TestEnum")));
117
-   }
103
+	#[test]
104
+	fn c_enum()
105
+	{
106
+		assert_eq!(TestEnum::try_from(0), Ok(TestEnum::Zero));
107
+		assert_eq!(TestEnum::try_from(1), Ok(TestEnum::One));
108
+		assert_eq!(TestEnum::try_from(2), Ok(TestEnum::Two));
109
+		assert_eq!(TestEnum::try_from(3), Err(ReprError::new("TestEnum", 3)));
110
+		assert_eq!(TestEnum::try_from(4), Err(ReprError::new("TestEnum", 4)));
111
+		assert_eq!(TestEnum::try_from(5), Err(ReprError::new("TestEnum", 5)));
112
+		assert_eq!(TestEnum::from_str("Zero"), Ok(TestEnum::Zero));
113
+		assert_eq!(TestEnum::from_str("One"), Ok(TestEnum::One));
114
+		assert_eq!(TestEnum::from_str("Two"), Ok(TestEnum::Two));
115
+		assert_eq!(TestEnum::from_str("Three"),
116
+		           Err(ParseEnumError::new("TestEnum")));
117
+	}
118 118
 }
119 119
 
120 120
 // EOF

+ 20
- 20
maraiah/cksum.rs View File

@@ -3,22 +3,22 @@
3 3
 // Accumulator for CRC function.
4 4
 fn crc_accum(a: u32, _: u32) -> u32
5 5
 {
6
-   if a & 1 == 1 {
7
-      ISO_3309_POLYNOMIAL ^ a >> 1
8
-   } else {
9
-      a >> 1
10
-   }
6
+	if a & 1 == 1 {
7
+		ISO_3309_POLYNOMIAL ^ a >> 1
8
+	} else {
9
+		a >> 1
10
+	}
11 11
 }
12 12
 
13 13
 // Initializes a CRC array.
14 14
 // FIXME: use const fn when stabilized
15 15
 fn crc_init() -> [u32; 256]
16 16
 {
17
-   let mut t = [0; 256];
18
-   for (n, v) in t.iter_mut().enumerate() {
19
-      *v = (0..8).fold(n as u32, crc_accum);
20
-   }
21
-   t
17
+	let mut t = [0; 256];
18
+	for (n, v) in t.iter_mut().enumerate() {
19
+		*v = (0..8).fold(n as u32, crc_accum);
20
+	}
21
+	t
22 22
 }
23 23
 
24 24
 /// Creates an ADLER32 of all bytes in `b`.
@@ -32,16 +32,16 @@ fn crc_init() -> [u32; 256]
32 32
 /// ```
33 33
 pub fn adler32(b: &[u8]) -> u32
34 34
 {
35
-   let mut x = 1;
36
-   let mut y = 0;
35
+	let mut x = 1;
36
+	let mut y = 0;
37 37
 
38
-   for &z in b {
39
-      let z = u32::from(z);
40
-      x = (x + z) % ADLER32_MODULO;
41
-      y = (y + x) % ADLER32_MODULO;
42
-   }
38
+	for &z in b {
39
+		let z = u32::from(z);
40
+		x = (x + z) % ADLER32_MODULO;
41
+		y = (y + x) % ADLER32_MODULO;
42
+	}
43 43
 
44
-   (y << 16) | x
44
+	(y << 16) | x
45 45
 }
46 46
 
47 47
 /// Creates a CRC-32 of all bytes in `b` with the starting sum `s`. The
@@ -56,8 +56,8 @@ pub fn adler32(b: &[u8]) -> u32
56 56
 /// ```
57 57
 pub fn crc32(b: &[u8], s: u32) -> u32
58 58
 {
59
-   let t = crc_init();
60
-   !b.iter().fold(s, |a, &o| a >> 8 ^ t[usize::from(a as u8 ^ o)])
59
+	let t = crc_init();
60
+	!b.iter().fold(s, |a, &o| a >> 8 ^ t[usize::from(a as u8 ^ o)])
61 61
 }
62 62
 
63 63
 const ISO_3309_POLYNOMIAL: u32 = 0xEDB8_8320;

+ 336
- 336
maraiah/defl.rs View File

@@ -6,209 +6,209 @@ use std::cmp::Ordering;
6 6
 /// Loads a ZLIB file header.
7 7
 pub fn load_zlib_header(b: &[u8]) -> ResultS<usize>
8 8
 {
9
-   const CM:    u8 = 0b0000_1111;
10
-   const CINFO: u8 = 0b1111_0000;
11
-   const FDICT: u8 = 0b0010_0000;
12
-
13
-   read_data! {
14
-      endian: BIG, buf: b, size: 2, start: 0, data {
15
-         let fcheck = u16[0];
16
-         let cmf = u8[0];
17
-         let flg = u8[1];
18
-      }
19
-   }
20
-
21
-   let cm    = cmf & CM;
22
-   let cinfo = cmf & CINFO;
23
-
24
-   if cm != 8 {
25
-      bail!("unknown compression method");
26
-   }
27
-
28
-   if cinfo > 7 << 4 {
29
-      bail!("lz77 window size logarithm is invalid");
30
-   }
31
-
32
-   if fcheck % 31 != 0 {
33
-      bail!("invalid fcheck");
34
-   }
35
-
36
-   if flg & FDICT != 0 {
37
-      bail!("dictionary not supported");
38
-   }
39
-
40
-   Ok(2)
9
+	const CM:    u8 = 0b0000_1111;
10
+	const CINFO: u8 = 0b1111_0000;
11
+	const FDICT: u8 = 0b0010_0000;
12
+
13
+	read_data! {
14
+		endian: BIG, buf: b, size: 2, start: 0, data {
15
+			let fcheck = u16[0];
16
+			let cmf = u8[0];
17
+			let flg = u8[1];
18
+		}
19
+	}
20
+
21
+	let cm    = cmf & CM;
22
+	let cinfo = cmf & CINFO;
23
+
24
+	if cm != 8 {
25
+		bail!("unknown compression method");
26
+	}
27
+
28
+	if cinfo > 7 << 4 {
29
+		bail!("lz77 window size logarithm is invalid");
30
+	}
31
+
32
+	if fcheck % 31 != 0 {
33
+		bail!("invalid fcheck");
34
+	}
35
+
36
+	if flg & FDICT != 0 {
37
+		bail!("dictionary not supported");
38
+	}
39
+
40
+	Ok(2)
41 41
 }
42 42
 
43 43
 /// Loads a GZIP file header.
44 44
 pub fn load_gzip_header(b: &[u8]) -> ResultS<usize>
45 45
 {
46
-   const FHCRC:     u8 = 1 << 1;
47
-   const FEXTRA:    u8 = 1 << 2;
48
-   const FNAME:     u8 = 1 << 3;
49
-   const FCOMMENT:  u8 = 1 << 4;
50
-   const FRESERVED: u8 = 0xe0;
46
+	const FHCRC:     u8 = 1 << 1;
47
+	const FEXTRA:    u8 = 1 << 2;
48
+	const FNAME:     u8 = 1 << 3;
49
+	const FCOMMENT:  u8 = 1 << 4;
50
+	const FRESERVED: u8 = 0xe0;
51 51
 
52
-   read_data! {
53
-      endian: LITTLE, buf: b, size: 10, start: 0, data {
54
-         let id = u16[0];
55
-         let cm = u8[2];
56
-         let fl = u8[3];
57
-      }
58
-   }
52
+	read_data! {
53
+		endian: LITTLE, buf: b, size: 10, start: 0, data {
54
+			let id = u16[0];
55
+			let cm = u8[2];
56
+			let fl = u8[3];
57
+		}
58
+	}
59 59
 
60
-   if id != 0x8b1f || cm != 8 {
61
-      bail!("not gzip format");
62
-   }
60
+	if id != 0x8b1f || cm != 8 {
61
+		bail!("not gzip format");
62
+	}
63 63
 
64
-   let mut p = 10;
64
+	let mut p = 10;
65 65
 
66
-   if fl & FRESERVED != 0 {
67
-      bail!("reserved flags set");
68
-   }
66
+	if fl & FRESERVED != 0 {
67
+		bail!("reserved flags set");
68
+	}
69 69
 
70
-   if fl & FEXTRA != 0 {
71
-      read_data! {
72
-         endian: LITTLE, buf: b, size: 2, start: p, data {
73
-            let xlen = u16[0] usize;
74
-         }
75
-      }
70
+	if fl & FEXTRA != 0 {
71
+		read_data! {
72
+			endian: LITTLE, buf: b, size: 2, start: p, data {
73
+				let xlen = u16[0] usize;
74
+			}
75
+		}
76 76
 
77
-      p += 2 + xlen;
77
+		p += 2 + xlen;
78 78
 
79
-      check_data(b, p)?;
80
-   }
79
+		check_data(b, p)?;
80
+	}
81 81
 
82
-   if fl & FNAME != 0 {
83
-      p += skip_zero_terminated_item(&b[p..])?;
84
-   }
82
+	if fl & FNAME != 0 {
83
+		p += skip_zero_terminated_item(&b[p..])?;
84
+	}
85 85
 
86
-   if fl & FCOMMENT != 0 {
87
-      p += skip_zero_terminated_item(&b[p..])?;
88
-   }
86
+	if fl & FCOMMENT != 0 {
87
+		p += skip_zero_terminated_item(&b[p..])?;
88
+	}
89 89
 
90
-   if fl & FHCRC != 0 {
91
-      p += 2;
90
+	if fl & FHCRC != 0 {
91
+		p += 2;
92 92
 
93
-      check_data(b, p)?;
94
-   }
93
+		check_data(b, p)?;
94
+	}
95 95
 
96
-   Ok(p)
96
+	Ok(p)
97 97
 }
98 98
 
99 99
 fn skip_zero_terminated_item(b: &[u8]) -> ResultS<usize>
100 100
 {
101
-   if let Some(i) = b.iter().position(|&n| n == 0) {
102
-      Ok(i + 1)
103
-   } else {
104
-      bail!("no end of zero terminated item");
105
-   }
101
+	if let Some(i) = b.iter().position(|&n| n == 0) {
102
+		Ok(i + 1)
103
+	} else {
104
+		bail!("no end of zero terminated item");
105
+	}
106 106
 }
107 107
 
108 108
 /// Decompresses a DEFLATE compressed bitstream.
109 109
 pub fn load_deflate(b: &[u8]) -> ResultS<(usize, Vec<u8>)>
110 110
 {
111
-   let mut v = Vec::new();
112
-   let mut p = 0;
113
-
114
-   loop {
115
-      let bfinal = read_bits_l(b, p, 1)?;
116
-      p += 1;
117
-      let btype = read_bits_l(b, p, 2)?;
118
-      p += 2;
119
-
120
-      match btype {
121
-         0b10 => p = stream_dynamic(&mut v, b, p)?,
122
-         0b01 => p = stream_s_table(&mut v, b, p)?,
123
-         0b00 => p = stream_literal(&mut v, b, p)?,
124
-         _    => bail!("bad btype"),
125
-      }
126
-
127
-      if bfinal == 1 {
128
-         return Ok((p / 8, v));
129
-      }
130
-   }
111
+	let mut v = Vec::new();
112
+	let mut p = 0;
113
+
114
+	loop {
115
+		let bfinal = read_bits_l(b, p, 1)?;
116
+		p += 1;
117
+		let btype = read_bits_l(b, p, 2)?;
118
+		p += 2;
119
+
120
+		match btype {
121
+			0b10 => p = stream_dynamic(&mut v, b, p)?,
122
+			0b01 => p = stream_s_table(&mut v, b, p)?,
123
+			0b00 => p = stream_literal(&mut v, b, p)?,
124
+			_    => bail!("bad btype"),
125
+		}
126
+
127
+		if bfinal == 1 {
128
+			return Ok((p / 8, v));
129
+		}
130
+	}
131 131
 }
132 132
 
133 133
 fn stream_dynamic(v: &mut Vec<u8>, b: &[u8], mut p: usize) -> ResultS<usize>
134 134
 {
135
-   const CODE_ORDERING: [usize; 19] =
136
-      [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
135
+	const CODE_ORDERING: [usize; 19] =
136
+		[16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];
137 137
 
138
-   // read header (number of literal alphabet codes, number of distance
139
-   // alphabet codes, and number of lengths for decoding the alphabet)
140
-   let hlit = read_bits_l(b, p, 5)?;
141
-   p += 5;
142
-   let hdist = read_bits_l(b, p, 5)?;
143
-   p += 5;
144
-   let hclen = read_bits_l(b, p, 4)?;
145
-   p += 4;
138
+	// read header (number of literal alphabet codes, number of distance
139
+	// alphabet codes, and number of lengths for decoding the alphabet)
140
+	let hlit = read_bits_l(b, p, 5)?;
141
+	p += 5;
142
+	let hdist = read_bits_l(b, p, 5)?;
143
+	p += 5;
144
+	let hclen = read_bits_l(b, p, 4)?;
145
+	p += 4;
146 146
 
147
-   let hlit  = 257 + hlit as usize;
148
-   let hdist = 1 + hdist as usize;
149
-   let hclen = 4 + hclen as usize;
147
+	let hlit  = 257 + hlit as usize;
148
+	let hdist = 1 + hdist as usize;
149
+	let hclen = 4 + hclen as usize;
150 150
 
151
-   // first, get the huffman coding for the alphabet (which is also compressed)
152
-   let mut code_table = [0; 19];
151
+	// first, get the huffman coding for the alphabet (which is also compressed)
152
+	let mut code_table = [0; 19];
153 153
 
154
-   for i in 0..hclen {
155
-      let len = read_bits_l(b, p, 3)? as u16;
156
-      p += 3;
154
+	for i in 0..hclen {
155
+		let len = read_bits_l(b, p, 3)? as u16;
156
+		p += 3;
157 157
 
158
-      code_table[CODE_ORDERING[i]] = len;
159
-   }
158
+		code_table[CODE_ORDERING[i]] = len;
159
+	}
160 160
 
161
-   // then, we decode the alphabet (doing both types at the same time, because
162
-   // they're encoded the same anyways)
163
-   let code_table = HuffmanTable::new(&code_table)?;
164
-   let mut alphabet = vec![0; hlit + hdist];
161
+	// then, we decode the alphabet (doing both types at the same time, because
162
+	// they're encoded the same anyways)
163
+	let code_table = HuffmanTable::new(&code_table)?;
164
+	let mut alphabet = vec![0; hlit + hdist];
165 165
 
166
-   p = read_alphabet(b, p, &mut alphabet, code_table)?;
166
+	p = read_alphabet(b, p, &mut alphabet, code_table)?;
167 167
 
168
-   if alphabet[256] == 0 {
169
-      bail!("no way to end block");
170
-   }
168
+	if alphabet[256] == 0 {
169
+		bail!("no way to end block");
170
+	}
171 171
 
172
-   // build the length and distance tables from this information
173
-   let table_len = HuffmanTable::new(&alphabet[0..hlit])?;
174
-   let table_dst = HuffmanTable::new(&alphabet[hlit..hlit + hdist])?;
172
+	// build the length and distance tables from this information
173
+	let table_len = HuffmanTable::new(&alphabet[0..hlit])?;
174
+	let table_dst = HuffmanTable::new(&alphabet[hlit..hlit + hdist])?;
175 175
 
176
-   output_tables(v, b, p, table_len, table_dst)
176
+	output_tables(v, b, p, table_len, table_dst)
177 177
 }
178 178
 
179 179
 #[allow(clippy::needless_range_loop)]
180 180
 fn stream_s_table(v: &mut Vec<u8>, b: &[u8], p: usize) -> ResultS<usize>
181 181
 {
182
-   let mut len = [0; 288];
182
+	let mut len = [0; 288];
183 183
 
184
-   for i in   0..144 {len[i] = 8;}
185
-   for i in 144..256 {len[i] = 9;}
186
-   for i in 256..280 {len[i] = 7;}
187
-   for i in 280..288 {len[i] = 8;}
184
+	for i in   0..144 {len[i] = 8;}
185
+	for i in 144..256 {len[i] = 9;}
186
+	for i in 256..280 {len[i] = 7;}
187
+	for i in 280..288 {len[i] = 8;}
188 188
 
189
-   let dst = [5; 30];
189
+	let dst = [5; 30];
190 190
 
191
-   let table_len = HuffmanTable::new(&len)?;
192
-   let table_dst = HuffmanTable::new(&dst)?;
191
+	let table_len = HuffmanTable::new(&len)?;
192
+	let table_dst = HuffmanTable::new(&dst)?;
193 193
 
194
-   output_tables(v, b, p, table_len, table_dst)
194
+	output_tables(v, b, p, table_len, table_dst)
195 195
 }
196 196
 
197 197
 fn stream_literal(v: &mut Vec<u8>, b: &[u8], p: usize) -> ResultS<usize>
198 198
 {
199
-   // copy data directly from byte boundary
200
-   let mut p = p / 8 + 1;
199
+	// copy data directly from byte boundary
200
+	let mut p = p / 8 + 1;
201 201
 
202
-   read_data! {
203
-      endian: LITTLE, buf: b, size: 4, start: p, data {
204
-         let len = u16[0] usize;
205
-      }
206
-   }
202
+	read_data! {
203
+		endian: LITTLE, buf: b, size: 4, start: p, data {
204
+			let len = u16[0] usize;
205
+		}
206
+	}
207 207
 
208
-   p += 4;
209
-   v.extend(ok!(b.get(p..p + len), "not enough data")?);
208
+	p += 4;
209
+	v.extend(ok!(b.get(p..p + len), "not enough data")?);
210 210
 
211
-   Ok((p + len) * 8)
211
+	Ok((p + len) * 8)
212 212
 }
213 213
 
214 214
 fn read_alphabet(b: &[u8],
@@ -216,63 +216,63 @@ fn read_alphabet(b: &[u8],
216 216
                  alphabet: &mut [u16],
217 217
                  code_table: HuffmanTable) -> ResultS<usize>
218 218
 {
219
-   let mut i = 0;
220
-   while i < alphabet.len() {
221
-      let (bits, sym) = code_table.decode(b, p)?;
222
-      p += bits;
223
-
224
-      match sym {
225
-         0..=15 => {
226
-            // raw code
227
-            alphabet[i] = sym;
228
-            i += 1;
229
-         }
230
-         16 => {
231
-            // copy previous code 3-6 times
232
-            if i == 0 {
233
-               bail!("cannot copy on first alphabet code");
234
-            }
235
-
236
-            let len = usize::from(read_bits_l(b, p, 2)? as u8 + 3);
237
-            let lst = alphabet[i - 1];
238
-            p += 2;
239
-
240
-            for _ in 0..len {
241
-               alphabet[i] = lst;
242
-               i += 1;
243
-            }
244
-         }
245
-         17 => {
246
-            // repeat '0' 3-10 times
247
-            let len = usize::from(read_bits_l(b, p, 3)? as u8 + 3);
248
-            p += 3;
249
-
250
-            for _ in 0..len {
251
-               alphabet[i] = 0;
252
-               i += 1;
253
-            }
254
-         }
255
-         18 => {
256
-            // repeat '0' 11-138 times
257
-            let len = usize::from(read_bits_l(b, p, 7)? as u8 + 11);
258
-            p += 7;
259
-
260
-            for _ in 0..len {
261
-               alphabet[i] = 0;
262
-               i += 1;
263
-            }
264
-         }
265
-         _ => {
266
-            bail!("bad symbol in alphabet");
267
-         }
268
-      }
269
-
270
-      if i > alphabet.len() {
271
-         bail!("too many codes");
272
-      }
273
-   }
274
-
275
-   Ok(p)
219
+	let mut i = 0;
220
+	while i < alphabet.len() {
221
+		let (bits, sym) = code_table.decode(b, p)?;
222
+		p += bits;
223
+
224
+		match sym {
225
+			0..=15 => {
226
+				// raw code
227
+				alphabet[i] = sym;
228
+				i += 1;
229
+			}
230
+			16 => {
231
+				// copy previous code 3-6 times
232
+				if i == 0 {
233
+					bail!("cannot copy on first alphabet code");
234
+				}
235
+
236
+				let len = usize::from(read_bits_l(b, p, 2)? as u8 + 3);
237
+				let lst = alphabet[i - 1];
238
+				p += 2;
239
+
240
+				for _ in 0..len {
241
+					alphabet[i] = lst;
242
+					i += 1;
243
+				}
244
+			}
245
+			17 => {
246
+				// repeat '0' 3-10 times
247
+				let len = usize::from(read_bits_l(b, p, 3)? as u8 + 3);
248
+				p += 3;
249
+
250
+				for _ in 0..len {
251
+					alphabet[i] = 0;
252
+					i += 1;
253
+				}
254
+			}
255
+			18 => {
256
+				// repeat '0' 11-138 times
257
+				let len = usize::from(read_bits_l(b, p, 7)? as u8 + 11);
258
+				p += 7;
259
+
260
+				for _ in 0..len {
261
+					alphabet[i] = 0;
262
+					i += 1;
263
+				}
264
+			}
265
+			_ => {
266
+				bail!("bad symbol in alphabet");
267
+			}
268
+		}
269
+
270
+		if i > alphabet.len() {
271
+			bail!("too many codes");
272
+		}
273
+	}
274
+
275
+	Ok(p)
276 276
 }
277 277
 
278 278
 fn output_tables(v: &mut Vec<u8>,
@@ -281,145 +281,145 @@ fn output_tables(v: &mut Vec<u8>,
281 281
                  table_len: HuffmanTable,
282 282
                  table_dst: HuffmanTable) -> ResultS<usize>
283 283
 {
284
-   const LEN_BASE: [usize; 29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19,
285
-                                  23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115,
286
-                                  131, 163, 195, 227, 258];
287
-
288
-   const LEN_EXTRA_BITS: [u8; 29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2,
289
-                                     2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
290
-                                     0];
291
-
292
-   const DST_BASE: [usize; 30] = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65,
293
-                                  97, 129, 193, 257, 385, 513, 769, 1025,
294
-                                  1537, 2049, 3073, 4097, 0x1801, 0x2001,
295
-                                  0x3001, 0x4001, 0x6001];
296
-
297
-   const DST_EXTRA_BITS: [u8; 30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
298
-                                     6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
299
-                                     12, 12, 13, 13];
300
-
301
-   loop {
302
-      let (bits, sym) = table_len.decode(b, p)?;
303
-      p += bits;
304
-
305
-      match sym.cmp(&256) {
306
-         Ordering::Less => {
307
-            // direct byte
308
-            v.push(sym as u8);
309
-         }
310
-         Ordering::Equal => {
311
-            return Ok(p);
312
-         }
313
-         Ordering::Greater => {
314
-            // this is a <len, dst> pair
315
-            let sym = sym - 257;
316
-
317
-            if sym > 29 {
318
-               bail!("invalid fixed code");
319
-            }
320
-
321
-            let sym = usize::from(sym);
322
-
323
-            // first get the actual length and any extra bits it may have
324
-            let bits = LEN_EXTRA_BITS[sym];
325
-            let leng = LEN_BASE[sym] + read_bits_l(b, p, bits)? as usize;
326
-            p += usize::from(bits);
327
-
328
-            // decode the distance with its alphabet
329
-            let (bits, sym) = table_dst.decode(b, p)?;
330
-            p += bits;
331
-
332
-            let sym = usize::from(sym);
333
-
334
-            // get the actual distance and any extra bits it may have
335
-            let bits = DST_EXTRA_BITS[sym];
336
-            let dist = DST_BASE[sym] + read_bits_l(b, p, bits)? as usize;
337
-            p += usize::from(bits);
338
-
339
-            if dist > v.len() {
340
-               bail!("bad distance");
341
-            }
342
-
343
-            // copy bytes from earlier
344
-            for _ in 0..leng {
345
-               v.push(v[v.len() - dist]);
346
-            }
347
-         }
348
-      }
349
-   }
284
+	const LEN_BASE: [usize; 29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19,
285
+	                               23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115,
286
+	                               131, 163, 195, 227, 258];
287
+
288
+	const LEN_EXTRA_BITS: [u8; 29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2,
289
+	                                  2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
290
+	                                  0];
291
+
292
+	const DST_BASE: [usize; 30] = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65,
293
+	                               97, 129, 193, 257, 385, 513, 769, 1025,
294
+	                               1537, 2049, 3073, 4097, 0x1801, 0x2001,
295
+	                               0x3001, 0x4001, 0x6001];
296
+
297
+	const DST_EXTRA_BITS: [u8; 30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
298
+	                                  6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
299
+	                                  12, 12, 13, 13];
300
+
301
+	loop {
302
+		let (bits, sym) = table_len.decode(b, p)?;
303
+		p += bits;
304
+
305
+		match sym.cmp(&256) {
306
+			Ordering::Less => {
307
+				// direct byte
308
+				v.push(sym as u8);
309
+			}
310
+			Ordering::Equal => {
311
+				return Ok(p);
312
+			}
313
+			Ordering::Greater => {
314
+				// this is a <len, dst> pair
315
+				let sym = sym - 257;
316
+
317
+				if sym > 29 {
318
+					bail!("invalid fixed code");
319
+				}
320
+
321
+				let sym = usize::from(sym);
322
+
323
+				// first get the actual length and any extra bits it may have
324
+				let bits = LEN_EXTRA_BITS[sym];
325
+				let leng = LEN_BASE[sym] + read_bits_l(b, p, bits)? as usize;
326
+				p += usize::from(bits);
327
+
328
+				// decode the distance with its alphabet
329
+				let (bits, sym) = table_dst.decode(b, p)?;
330
+				p += bits;
331
+
332
+				let sym = usize::from(sym);
333
+
334
+				// get the actual distance and any extra bits it may have
335
+				let bits = DST_EXTRA_BITS[sym];
336
+				let dist = DST_BASE[sym] + read_bits_l(b, p, bits)? as usize;
337
+				p += usize::from(bits);
338
+
339
+				if dist > v.len() {
340
+					bail!("bad distance");
341
+				}
342
+
343
+				// copy bytes from earlier
344
+				for _ in 0..leng {
345
+					v.push(v[v.len() - dist]);
346
+				}
347
+			}
348
+		}
349
+	}
350 350
 }
351 351
 
352 352
 impl HuffmanTable
353 353
 {
354
-   fn new(table: &[u16]) -> ResultS<Self>
355
-   {
356
-      let mut syms = vec![0; table.len()];
357
-      let mut nums = [0; 16];
358
-
359
-      // count the number of symbols for each bit length
360
-      for &length in table {
361
-         nums[usize::from(length)] += 1;
362
-      }
363