reader.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. use std::{fs,io,iter,slice,vec};
  2. /// A `ByteReader` is just a tiny wrapper over a mutable byte iterator, so we
  3. /// can parse things more easily.
  4. pub struct ByteReader<Rd> {
  5. bytes: Rd,
  6. }
  7. const MK_OK: &'static Fn(io::Result<u8>) -> Option<u8> = &|s| s.ok();
  8. impl<R: io::Read>
  9. ByteReader<iter::FilterMap<io::Bytes<R>,
  10. &'static Fn(io::Result<u8>) -> Option<u8>>>
  11. {
  12. /// Create a ByteReader from any type that implement Read
  13. pub fn from_reader(r: R) -> Self {
  14. let bytes = r.bytes().filter_map(MK_OK);
  15. ByteReader { bytes: bytes }
  16. }
  17. }
  18. impl ByteReader<iter::FilterMap<io::Bytes<fs::File>,
  19. &'static Fn(io::Result<u8>) -> Option<u8>>>
  20. {
  21. /// Create a reader by opening a named file for reading
  22. pub fn from_file(path: &str) -> io::Result<Self> {
  23. let f = try!(fs::File::open(path));
  24. Ok(ByteReader::from_reader(f))
  25. }
  26. }
  27. impl ByteReader<vec::IntoIter<u8>> {
  28. /// Create a reader from a vector of u8s
  29. pub fn from_vec(lst: Vec<u8>) -> Self {
  30. ByteReader { bytes: lst.into_iter() }
  31. }
  32. }
  33. const DEREF: &'static Fn(&u8) -> u8 = &|s| *s;
  34. impl<'a> ByteReader<iter::Map<slice::Iter<'a, u8>, &'static Fn(&u8) -> u8>> {
  35. /// Create a reader from a borrowed slice, with a copy on each access
  36. pub fn from_slice(lst: &'a [u8]) -> Self {
  37. ByteReader { bytes: lst.iter().map(DEREF) }
  38. }
  39. }
  40. #[test]
  41. fn reader_tests() {
  42. assert!(ByteReader::from_slice(&[0x00]).read_twip() == Ok(-32.0));
  43. assert!(ByteReader::from_slice(&[0x00]).read_prefix_int() == Ok(0x00));
  44. assert!(ByteReader::from_slice(&[0x7f]).read_prefix_int() == Ok(0x7f));
  45. assert!(ByteReader::from_slice(&[0x80,0xff]).read_prefix_int() == Ok(0xff));
  46. assert!(ByteReader::from_slice(&[0x03,0x00,0x00,0x00])
  47. .read_several(|r| r.read_twip()) ==
  48. Ok(vec![-32.0,-32.0,-32.0]));
  49. }
  50. impl<T> ByteReader<T> where T: Iterator<Item=u8> {
  51. /// This gets the next byte, or fails if it's out of input.
  52. pub fn next(&mut self) -> Result<u8, String> {
  53. Ok(try!(self.bytes.next().ok_or("out of input")))
  54. }
  55. /// This reads a one-byte or two-byte float in the rough range of
  56. /// (192 .. -128). We're going to treat all models as if they're
  57. /// supposed to be centered in a 64x64x64 square, so this gives
  58. /// us 128 units on either side of the central square, as well.
  59. pub fn read_twip(&mut self) -> Result<f32, String> {
  60. let b1 = try!(self.next());
  61. if (b1 & 0x80) != 0 {
  62. let b2 = try!(self.next());
  63. let val = ((b1 as u16 & 0x7f) << 8) | b2 as u16;
  64. Ok((val as f32 / 102.0) - 128.0)
  65. } else {
  66. Ok((b1 as f32) - 32.0)
  67. }
  68. }
  69. /// This reads a single byte and treats it as a ratio.
  70. pub fn read_ratio(&mut self) -> Result<f32, String> {
  71. let b = try!(self.next());
  72. Ok(b as f32 / 255.0)
  73. }
  74. /// This reads a 64-bit int with a packed PrefixInteger representation.
  75. /// The shorter the int, the shorter the representation.
  76. pub fn read_prefix_int(&mut self) -> Result<u64, String> {
  77. fn match_bits(n: u8, mask: u8) -> bool {
  78. n & mask == mask
  79. }
  80. let b = try!(self.next());
  81. if match_bits(b, 0xff) { self.continue_prefix_int(8, 0) }
  82. else if match_bits(b, 0xfe) { self.continue_prefix_int(7, 0) }
  83. else if match_bits(b, 0xfc) { self.continue_prefix_int(6, b & 0x01) }
  84. else if match_bits(b, 0xf8) { self.continue_prefix_int(5, b & 0x03) }
  85. else if match_bits(b, 0xf0) { self.continue_prefix_int(4, b & 0x07) }
  86. else if match_bits(b, 0xe0) { self.continue_prefix_int(3, b & 0x0f) }
  87. else if match_bits(b, 0xc0) { self.continue_prefix_int(2, b & 0x1f) }
  88. else if match_bits(b, 0x80) { self.continue_prefix_int(1, b & 0x3f) }
  89. else { self.continue_prefix_int(0, b) }
  90. }
  91. /// This is a helper function for parsing prefix ints, too.
  92. fn continue_prefix_int(&mut self, mut left: u8, upper: u8) -> Result<u64, String> {
  93. let mut ret = upper as u64;
  94. while left > 0 {
  95. left -= 1;
  96. ret = (ret << 8) | try!(self.next()) as u64;
  97. }
  98. Ok(ret)
  99. }
  100. /// This reads a PrefixInteger to find out how many other things to read,
  101. /// and then reads that number of things.
  102. pub fn read_several<F, R>(&mut self, reader: F) -> Result<Vec<R>, String>
  103. where F: Fn(&mut ByteReader<T>) -> Result<R, String>
  104. {
  105. let ct = try!(self.read_prefix_int());
  106. let mut ret = Vec::with_capacity(ct as usize);
  107. for _ in 0..ct {
  108. ret.push(try!(reader(self)))
  109. }
  110. Ok(ret)
  111. }
  112. /// This reads a PrefixInteger number of bytes, and then parses those
  113. /// bytes as a UTF-8 string. This means, importantly, that we cannot
  114. /// naïvely produce values intended to be parsed with this using a
  115. /// basic string length.
  116. pub fn read_string(&mut self) -> Result<String, String> {
  117. let raw_bytes = try!(self.read_several(|r| r.next()));
  118. match String::from_utf8(raw_bytes) {
  119. Ok(s) => Ok(s),
  120. Err(e) => Err(format!("Exception when parsing UTF-8: {:?}", e)),
  121. }
  122. }
  123. }