|
| 1 | +//===--------------- Bits.swift - Bitstream helpers ----------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import struct TSCBasic.ByteString |
| 14 | + |
| 15 | +struct Bits: RandomAccessCollection { |
| 16 | + var buffer: ByteString |
| 17 | + |
| 18 | + var startIndex: Int { return 0 } |
| 19 | + var endIndex: Int { return buffer.count * 8 } |
| 20 | + |
| 21 | + subscript(index: Int) -> UInt8 { |
| 22 | + let byte = buffer.contents[index / 8] |
| 23 | + return (byte >> UInt8(index % 8)) & 1 |
| 24 | + } |
| 25 | + |
| 26 | + func readBits(atOffset offset: Int, count: Int) -> UInt64 { |
| 27 | + precondition(count >= 0 && count <= 64) |
| 28 | + precondition(offset >= 0) |
| 29 | + precondition(offset &+ count >= offset) |
| 30 | + precondition(offset &+ count <= self.endIndex) |
| 31 | + |
| 32 | + return buffer.contents.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in |
| 33 | + let upperBound = offset &+ count |
| 34 | + let topByteIndex = upperBound >> 3 |
| 35 | + var result: UInt64 = 0 |
| 36 | + if upperBound & 7 != 0 { |
| 37 | + let mask: UInt8 = (1 << UInt8(upperBound & 7)) &- 1 |
| 38 | + result = UInt64(bytes[topByteIndex] & mask) |
| 39 | + } |
| 40 | + for i in ((offset >> 3)..<(upperBound >> 3)).reversed() { |
| 41 | + result <<= 8 |
| 42 | + result |= UInt64(bytes[i]) |
| 43 | + } |
| 44 | + if offset & 7 != 0 { |
| 45 | + result >>= UInt64(offset & 7) |
| 46 | + } |
| 47 | + return result |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +extension Bits { |
| 53 | + struct Cursor { |
| 54 | + enum Error: Swift.Error { case bufferOverflow } |
| 55 | + |
| 56 | + let buffer: Bits |
| 57 | + private var offset: Int = 0 |
| 58 | + |
| 59 | + init(buffer: Bits) { |
| 60 | + self.buffer = buffer |
| 61 | + } |
| 62 | + |
| 63 | + init(buffer: ByteString) { |
| 64 | + self.init(buffer: Bits(buffer: buffer)) |
| 65 | + } |
| 66 | + |
| 67 | + var isAtStart: Bool { |
| 68 | + return offset == buffer.startIndex |
| 69 | + } |
| 70 | + |
| 71 | + var isAtEnd: Bool { |
| 72 | + return offset == buffer.count |
| 73 | + } |
| 74 | + |
| 75 | + func peek(_ count: Int) throws -> UInt64 { |
| 76 | + if buffer.count - offset < count { throw Error.bufferOverflow } |
| 77 | + return buffer.readBits(atOffset: offset, count: count) |
| 78 | + } |
| 79 | + |
| 80 | + mutating func read(_ count: Int) throws -> UInt64 { |
| 81 | + defer { offset += count } |
| 82 | + return try peek(count) |
| 83 | + } |
| 84 | + |
| 85 | + mutating func read(bytes count: Int) throws -> ArraySlice<UInt8> { |
| 86 | + precondition(count >= 0) |
| 87 | + precondition(offset & 0b111 == 0) |
| 88 | + let newOffset = offset &+ (count << 3) |
| 89 | + precondition(newOffset >= offset) |
| 90 | + if newOffset > buffer.count { throw Error.bufferOverflow } |
| 91 | + defer { offset = newOffset } |
| 92 | + return buffer.buffer.contents.dropFirst(offset >> 3).prefix((newOffset - offset) >> 3) |
| 93 | + } |
| 94 | + |
| 95 | + mutating func skip(bytes count: Int) throws { |
| 96 | + precondition(count >= 0) |
| 97 | + precondition(offset & 0b111 == 0) |
| 98 | + let newOffset = offset &+ (count << 3) |
| 99 | + precondition(newOffset >= offset) |
| 100 | + if newOffset > buffer.count { throw Error.bufferOverflow } |
| 101 | + offset = newOffset |
| 102 | + } |
| 103 | + |
| 104 | + mutating func advance(toBitAlignment align: Int) throws { |
| 105 | + precondition(align > 0) |
| 106 | + precondition(offset &+ (align&-1) >= offset) |
| 107 | + precondition(align & (align &- 1) == 0) |
| 108 | + if offset % align == 0 { return } |
| 109 | + offset = (offset &+ align) & ~(align &- 1) |
| 110 | + if offset > buffer.count { throw Error.bufferOverflow } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments