Class: VariableByteCode
- Inherits:
-
Object
- Object
- VariableByteCode
- Defined in:
- lib/ls4/lib/vbcode.rb
Overview
VariableByteCode
Copyright © 2010 FURUHASHI Sadayuki. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Class Method Summary collapse
- .decode(raw) ⇒ Object
- .decode_index(raw, from = 0) ⇒ Object
- .decode_n(raw, from = 0, length = raw.length-from) ⇒ Object
- .decode_stream(io) ⇒ Object
- .encode(value, raw = "") ⇒ Object
- .encode_n(array, out = "") ⇒ Object
Class Method Details
.decode(raw) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ls4/lib/vbcode.rb', line 55 def self.decode(raw) i = 0 value = 0 while raw.length > i v = raw[i].unpack('C')[0] if v & 0b10000000 != 0 v &= 0b01111111 v <<= (i*7) value |= v else v <<= (i*7) value |= v return value end i += 1 end return value end |
.decode_index(raw, from = 0) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ls4/lib/vbcode.rb', line 96 def self.decode_index(raw, from=0) i = from value = 0 while raw.length > i v = raw[i].unpack('C')[0] if v & 0b10000000 != 0 v &= 0b01111111 v <<= ((i-from)*7) value |= v else v <<= ((i-from)*7) value |= v return value, i+1 end i += 1 end return value, i end |
.decode_n(raw, from = 0, length = raw.length-from) ⇒ Object
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ls4/lib/vbcode.rb', line 187 def self.decode_n(raw, from=0, length=raw.length-from) upto = from + length upto = raw.length if upto > raw.length values = [] while upto > from value, from = decode_index(raw, from) values << value end values end |
.decode_stream(io) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ls4/lib/vbcode.rb', line 137 def self.decode_stream(io) i = 0 value = 0 b = " " while b = io.read(1, b) v = b.unpack('C')[0] if v & 0b10000000 != 0 v &= 0b01111111 v <<= (i*7) value |= v else v <<= (i*7) value |= v return value end i += 1 end return value end |
.encode(value, raw = "") ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/ls4/lib/vbcode.rb', line 32 def self.encode(value, raw="") begin v = value & 0b01111111 | 0b10000000 value >>= 7 raw << [v].pack('C') end while value > 0 raw[raw.length-1] = [raw[raw.length-1].unpack('C')[0] & 0b01111111].pack('C') raw end |
.encode_n(array, out = "") ⇒ Object
179 180 181 182 183 184 |
# File 'lib/ls4/lib/vbcode.rb', line 179 def self.encode_n(array, out="") array.each {|value| encode(value, out) } out end |