Class: LZ4::BasicStream::Header

Inherits:
Struct
  • Object
show all
Defined in:
lib/extlz4/oldstream.rb,
lib/extlz4/oldstream.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockchecksumObject

Returns the value of attribute blockchecksum

Returns:

  • (Object)

    the current value of blockchecksum



121
122
123
# File 'lib/extlz4/oldstream.rb', line 121

def blockchecksum
  @blockchecksum
end

#blockindependenceObject

Returns the value of attribute blockindependence

Returns:

  • (Object)

    the current value of blockindependence



121
122
123
# File 'lib/extlz4/oldstream.rb', line 121

def blockindependence
  @blockindependence
end

#blocksizeObject

Returns the value of attribute blocksize

Returns:

  • (Object)

    the current value of blocksize



121
122
123
# File 'lib/extlz4/oldstream.rb', line 121

def blocksize
  @blocksize
end

#magicObject

Returns the value of attribute magic

Returns:

  • (Object)

    the current value of magic



121
122
123
# File 'lib/extlz4/oldstream.rb', line 121

def magic
  @magic
end

#predictidObject

Returns the value of attribute predictid

Returns:

  • (Object)

    the current value of predictid



121
122
123
# File 'lib/extlz4/oldstream.rb', line 121

def predictid
  @predictid
end

#streamchecksumObject

Returns the value of attribute streamchecksum

Returns:

  • (Object)

    the current value of streamchecksum



121
122
123
# File 'lib/extlz4/oldstream.rb', line 121

def streamchecksum
  @streamchecksum
end

#streamsizeObject

Returns the value of attribute streamsize

Returns:

  • (Object)

    the current value of streamsize



121
122
123
# File 'lib/extlz4/oldstream.rb', line 121

def streamsize
  @streamsize
end

#versionObject

Returns the value of attribute version

Returns:

  • (Object)

    the current value of version



121
122
123
# File 'lib/extlz4/oldstream.rb', line 121

def version
  @version
end

Class Method Details

.load(io) ⇒ Object



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
# File 'lib/extlz4/oldstream.rb', line 131

def self.load(io)
  case magic = io.read(4).unpack("V")
  when MAGIC_NUMBER_LEGACY
    new(magic, -1, true, false, false, 8 * 1024 * 1024, nil, nil)
  when MAGIC_NUMBER
    (sf, bd) = io.read(2).unpack("CC")
    version = (sf >> 6) & 0x03
    raise "stream header error - wrong version number" unless version == 0x01
    blockindependence = ((sf >> 5) & 0x01) == 0 ? false : true
    blockchecksum = ((sf >> 4) & 0x01) == 0 ? false : true
    streamsize = ((sf >> 3) & 0x01) == 0 ? false : true
    streamchecksum = ((sf >> 2) & 0x01) == 0 ? false : true
    # reserved = (sf >> 1) & 0x01
    predictid = ((sf >> 0) & 0x01) == 0 ? false : true

    # reserved = (bd >> 7) & 0x01
    blockmax = (bd >> 4) & 0x07
    # reserved = (bd >> 0) & 0x0f

    blocksize = BLOCK_MAXIMUM_SIZES[blockmax]
    raise Error, "stream header error - wrong block maximum size (#{blockmax} for 4 .. 7)" unless blocksize

    streamsize = io.read(8).unpack("Q<")[0] if streamsize
    predictid = io.read(4).unpack("V")[0] if predictid

    headerchecksum = io.getbyte

    new(magic, version, blockindependence, blockchecksum, streamchecksum, blocksize, streamsize, predictid)
  else
    raise "could not recognized magic number (0x%08x)" % (magic || nil)
  end
end

.pack(*args) ⇒ Object



164
165
166
# File 'lib/extlz4/oldstream.rb', line 164

def self.pack(*args)
  new(*args).pack
end

Instance Method Details

#packObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/extlz4/oldstream.rb', line 168

def pack
  raise "wrong magic number" unless magic == MAGIC_NUMBER
  raise "wrong version number" unless version == VERSION_NUMBER

  header = [magic].pack("V")
  sd = version |
       (blockindependence ? BLOCK_INDEPENDENCY : 0) |
       (blockchecksum ? BLOCK_CHECKSUM : 0) |
       (streamsize ? STREAM_SIZE : 0) |
       (streamchecksum ? STREAM_CHECKSUM : 0) |
       (predictid ? PRESET_DICTIONARY : 0)
  bd = (BLOCK_MAXIMUM_SIZES.rassoc(blocksize)[0] << 4)
  desc = [sd, bd].pack("CC")
  header << desc
  header << [streamsize].pack("Q<") if streamsize
  header << [predictid].pack("V") if predictid
  header << [XXhash.xxh32(desc) >> 8].pack("C")
end