Login
bitreader.cr at tip
Login

File src/remilib/bitreader.cr from the latest check-in


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
   150
   151
   152
   153
   154
   155
   156
   157
   158
   159
   160
   161
   162
   163
   164
   165
   166
   167
   168
   169
   170
   171
   172
   173
   174
   175
   176
   177
   178
   179
   180
   181
   182
   183
   184
   185
   186
   187
   188
   189
   190
   191
   192
   193
   194
   195
   196
   197
   198
   199
   200
   201
   202
   203
   204
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232
   233
   234
   235
   236
   237
   238
   239
   240
   241
   242
   243
   244
   245
   246
   247
   248
   249
   250
   251
   252
   253
   254
   255
   256
   257
   258
   259
   260
   261
   262
   263
   264
   265
   266
   267
   268
   269
   270
   271
   272
   273
   274
   275
   276
   277
   278
   279
   280
   281
   282
   283
   284
   285
   286
   287
   288
   289
   290
   291
   292
   293
   294
   295
   296
   297
   298
   299
   300
   301
   302
   303
   304
   305
   306
   307
   308
   309
   310
   311
   312
   313
   314
   315
   316
   317
   318
   319
   320
   321
   322
   323
   324
   325
   326
   327
   328
   329
   330
   331
   332
   333
   334
   335
   336
   337
   338
   339
   340
   341
   342
   343
   344
   345
   346
   347
   348
   349
   350
   351
   352
   353
   354
   355
   356
   357
   358
   359
   360
   361
   362
   363
   364
   365
   366
   367
   368
   369
   370
   371
   372
   373
   374
   375
   376
   377
   378
   379
   380
   381
   382
   383
   384
   385
   386
   387
   388
   389
   390
   391
   392
   393
   394
   395
   396
   397
   398
   399
   400
   401
   402
   403
   404
   405
   406
   407
   408
   409
   410
   411
   412
   413
   414
   415
   416
   417
   418
   419
   420
   421
   422
   423
#### libremiliacr
#### Copyright(C) 2020-2024 Remilia Scarlet <remilia@posteo.jp>
####
#### This program is free software: you can redistribute it and/or modify
#### it under the terms of the GNU General Public License as published
#### the Free Software Foundation, either version 3 of the License, or
#### (at your option) any later version.
####
#### This program is distributed in the hope that it will be useful,
#### but WITHOUT ANY WARRANTY; without even the implied warranty of
#### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
#### GNU General Public License for more details.
####
#### You should have received a copy of the GNU General Public License
#### along with this program.If not, see<http:####www.gnu.org/licenses/.>
require "math"

module RemiLib
  # Encapsulates an `IO` to enable the reading of individual bits.
  class BitReader
    class NotOnByteError < Exception
    end

    @io : IO
    @bitpos : UInt8 = 0
    @byte : UInt8? = nil

    # Creates a new `BitReader` that will read data from `stream`.  This will
    # always call `IO#read_byte` exactly once to populate the initial `#byte`.
    def initialize(stream : IO)
      @io = stream
      @byte = @io.read_byte || raise IO::EOFError.new("No initial byte to read")
    end

    # Reinitializes this `BitReader` with a new `IO`.  This completely resets
    # this instance.  This will always call `IO#read_byte` exactly once to
    # populate the initial `#byte`.
    def reinitialize(stream : IO)
      @bitpos = 0u8
      @io = stream
      @byte = @io.read_byte || raise IO::EOFError.new("No initial byte to read")
    end

    # Rewinds the stream to the beginning so that the next bit read is the very
    # first bit of the stream.  Returns `self`.
    def rewind : self
      self.pos = 0
    end

    # Returns the current bit position within the current byte.
    @[AlwaysInline]
    def bitpos
      @bitpos % 8
    end

    # Returns the current byte position in the underlying `IO`.
    @[AlwaysInline]
    def pos
      @io.pos - 1
    end

    # Returns the current byte position in the underlying `IO`.  Returns `self`.
    @[AlwaysInline]
    def pos=(value) : self
      @io.pos = value
      @bitpos = 0u8
      @byte = @io.read_byte || raise IO::EOFError.new("No initial byte to read")
      self
    end

    # Returns the current byte that the `BitReader` is reading bits from.
    @[AlwaysInline]
    def byte : UInt8
      if @byte.nil?
        raise IO::EOFError.new("No bytes left to read")
      else
        @byte.not_nil!
      end
    end

    # Returns the current byte that the `BitReader` is reading bits from, or
    # `nil` if were no more bytes to read.
    @[AlwaysInline]
    def byte? : UInt8?
      @byte
    end

    # Sets what the `BitReader` considers the last byte read.  This does not
    # affect the underlying stream.
    @[AlwaysInline]
    def byte=(val : UInt8)
      @byte = val
    end

    # Reads `count` bits, then returns the value as an `Int64`.
    def read(count : Int) : Int64
      ret : Int64 = 0

      # Try to easy out
      if count == 8 && @bitpos == 0
        ret = @byte.not_nil!.to_i64!
        @byte = @io.read_byte
        return ret
      end

      bitsToRead = 0
      while count > 0
        bitsToRead = Math.min(count, 8 - @bitpos)
        if count > 0 && @byte.nil?
          raise IO::EOFError.new("End of stream, no more bits")
        end

        count -= bitsToRead
        @bitpos += bitsToRead
        ret |= ((@byte.not_nil! >> (8 - @bitpos)) & (0xFF_u8 >> (8 - bitsToRead))).to_i64! << count

        if @bitpos >= 8
          @bitpos = 0
          @byte = @io.read_byte
        end
      end

      ret
    end

    # Try to read `count` bits, then returns the value as an `Int64?`.  If there
    # were not `count` bits available, this returns `nil`.
    def read?(count : Int) : Int64?
      return nil if @byte.nil?
      ret : Int64 = 0

      # Try to easy out
      if count == 8 && @bitpos == 0
        rbyte = @byte.not_nil!.to_i64!
        @byte = @io.read_byte
        return rbyte
      end

      bitsToRead = 0
      while count > 0
        bitsToRead = Math.min(count, 8 - @bitpos)
        if count > 0 && @byte.nil?
          return nil
        end

        count -= bitsToRead
        @bitpos += bitsToRead
        ret |= ((@byte.not_nil! >> (8 - @bitpos)) & (0xFF_u8 >> (8 - bitsToRead))).to_i64! << count

        if @bitpos >= 8
          @bitpos = 0
          @byte = @io.read_byte
        end
      end

      ret
    end

    # Reads `count` bytes into `dest` starting at `dest[offset]`.  The
    # `BitReader` must be on a byte boundary, or this will raise a
    # `NotOnByteError`.  This returns the number of bytes read, which maybe 0 if
    # nothing was read.  The current `#byte` is always the first byte put into
    # `dest`.
    def read(dest : Bytes, offset : Int, count : Int) : Int
      return 0 if count == 0

      # Bunch of checks
      raise NotOnByteError.new("BitReader is not on a byte boundary") unless @bitpos == 0
      raise ArgumentError.new("Offset cannot be negative") if offset < 0
      raise IO::EOFError.new("End of stream, no more bits") if @byte.nil?
      if dest.size < offset + count
        raise ArgumentError.new("offset + count is too large for array")
      end

      # Put the current byte into the destination.
      dest[offset] = @byte.not_nil!
      numRead = 1
      newlyRead = 0
      while numRead < count
        newlyRead = @io.read(dest[offset + numRead, count - numRead])
        break if newlyRead == 0
        numRead += newlyRead
      end

      @byte = @io.read_byte
      numRead
    end

    # :ditto:
    def read(dest : Array(UInt8), offset : Int, count : Int) : Int forall T
      return 0 if count == 0

      # Bunch of checks
      raise NotOnByteError.new("BitReader is not on a byte boundary") unless @bitpos == 0
      raise ArgumentError.new("Offset cannot be negative") if offset < 0
      raise IO::EOFError.new("End of stream, no more bits") if @byte.nil?
      if dest.size < offset + count
        raise ArgumentError.new("offset + count is too large for array")
      end

      # Put the current byte into the destination.
      dest[offset] = @byte.not_nil!
      numRead = 1
      newlyRead = 0
      while numRead < count
        newlyRead = @io.read(Slice.new(dest.to_unsafe + (offset + numRead), count - numRead))
        break if newlyRead == 0
        numRead += newlyRead
      end

      @byte = @io.read_byte
      numRead
    end

    # Reads `dest.size` bytes into `dest`.  The `BitReader` must be on a byte
    # boundary, or this will raise a `NotOnByteError`.  This returns the number
    # of bytes read, which maybe 0 if nothing was read.  The current `#byte` is
    # always the first byte put into `dest`.
    @[AlwaysInline]
    def read(dest : Bytes|Array(UInt8)) : Int
      self.read(dest, 0, dest.size)
    end

    # Reads `count` bytes into a new array, then returns that array.  The number
    # of elements in the returned array may be less than `count` if the end of
    # the file was reached.
    def readByteArray(count : Int) : Array(UInt8)
      ret = [] of UInt8
      count.times do |_|
        byte = self.read?(8)
        if byte
          ret << byte.to_u8!
        else
          break
        end
      end

      ret
    end

    # Reads `count` bytes into a new array, then returns that `Bytes`.  If there
    # are not `count` bytes remaining, this will raise an `IO::EOFError`.
    @[AlwaysInline]
    def readBytes(count : Int32) : Bytes
      Bytes.new(count) do |_|
        self.read(8).to_u8!
      end
    end

    # Peeks at `count` bits, returning an `Int64`.
    def peek(count : Int) : Int64
      raise IO::EOFError.new if @byte.nil?
      return @byte.not_nil!.to_i64! if @bitpos == 0 && count == 8

      oldPos = @io.pos
      oldBitPos = @bitpos
      oldByte = @byte
      ret : Int64? = nil

      begin
        ret = self.read(count)
      ensure
        @io.pos = oldPos
        @bitpos = oldBitPos
        @byte = oldByte
      end

      ret
    end

    # Peeks at `count` bits, if possible.  On success, this returns an `Int64`,
    # otherwise this returns `nil`.
    def peek?(count : Int) : Int64?
      return nil if @byte.nil?
      return @byte.not_nil!.to_i64! if @bitpos == 0 && count == 8

      oldPos = @io.pos
      oldBitPos = @bitpos
      oldByte = @byte
      ret : Int64? = nil

      begin
        ret = self.read?(count)
      ensure
        @io.pos = oldPos
        @bitpos = oldBitPos
        @byte = oldByte
      end

      ret
    end

    # Peeks `count` bytes into a new array, then returns that array.  The number
    # of elements in the returned array may be less than `count` if the end of
    # the file was reached.
    def peekBytes(count : Int) : Array(UInt8)
      return [] of UInt8 if @byte.nil?

      if @bitpos == 0 && count % 8 == 0
        # Easy peek
        ret = Array(UInt8).new(count, 0)
        @io.read(Slice.new(ret.to_unsafe, count))
        return ret
      end

      oldPos = @io.pos
      oldBitPos = @bitpos
      oldByte = @byte
      ret = [] of UInt8

      begin
        count.times do |_|
          byte = self.read?(8)
          if byte
            ret << byte.to_u8
          else
            break
          end
        end
      ensure
        @io.pos = oldPos
        @bitpos = oldBitPos
        @byte = oldByte
      end

      ret
    end

    # Reads bits until a `1` is encountered, counting the number of zeros that
    # are read.  This returns the number of zeros that were read before the `1`
    # was encountered.
    #
    # If `discardFirstOne` is `true`, then the first `1` bit is read and
    # discarded before returning.  Otherwise it remains unread.
    def countZeros(*, discardFirstOne : Bool = false) : Int
      ret = 0

      if discardFirstOne
        # Slightly faster since we don't do a peek
        while self.read(1) == 0
          ret += 1
        end
        return ret
      end

      loop do
        break if self.peek(1) == 1
        ret += 1
        self.read(1)
      end

      ret
    end

    # Reads bits until a `0` is encountered, counting the number of ones that
    # are read.  This returns the number of zeros that were read before the `0`
    # was encountered.
    #
    # If `discardFirstZero` is `true`, then the first `0` bit is read and
    # discarded before returning.  Otherwise it remains unread.
    def countOnes(*, discardFirstZero : Bool = false) : Int
      ret = 0

      if discardFirstZero
        # Slightly faster since we don't do a peek
        while self.read(1) == 1
          ret += 1
        end
        return ret
      end

      loop do
        break if self.peek(1) == 0
        ret += 1
        self.read(1)
      end

      ret
    end

    # Advances this `BitReader` to the next byte boundary, discarding bits as it
    # goes.  If the reader is already on a byte boundary, this does nothing.
    @[AlwaysInline]
    def advanceToNextByte : Nil
      return if @bitpos == 0
      while @bitpos != 0
        self.read(1)
      end
    end

    # Reads up to `sizeInBytes`, then attempts to convert those bytes into a
    # string.  On success, this returns the new string, which may be smaller
    # than `sizeInBytes` if there was not enough data left to read.
    @[AlwaysInline]
    def readString(sizeInBytes : Int) : String
      String.new(self.readByteArray(sizeInBytes).toSlice)
    end

    # Reads up to `sizeInBytes`, then attempts to convert those bytes into a
    # string.  On success, this returns the new string.  This raises an
    # `IO::EOFError` if there was not enough data left to read.
    @[AlwaysInline]
    def readString!(sizeInBytes : Int) : String
      String.new(self.readBytes(sizeInBytes))
    end

    {% begin %}
      {% types = [:Int16, :UInt16, :Int32, :UInt32, :Int64, :UInt64, :Int128, :UInt128] %}
      {% sizes = [2, 2, 4, 4, 8, 8, 16, 16] %}
      {% for i in 0...types.size %}
        @[AlwaysInline]
        def read{{types[i].id}} : {{types[i].id}}
          IO::ByteFormat::LittleEndian.decode({{types[i].id}}, readBytes({{sizes[i]}}))
        end

        @[AlwaysInline]
        def read{{types[i].id}}BE : {{types[i].id}}
          IO::ByteFormat::BigEndian.decode({{types[i].id}}, readBytes({{sizes[i]}}))
        end
      {% end %}
    {% end %}
  end
end