Class: Wiris::BaseCode
- Inherits:
-
Object
- Object
- Wiris::BaseCode
- Defined in:
- lib/src-generic/BaseCode.rb
Instance Method Summary collapse
- #decodeBytes(b) ⇒ Object
- #encodeBytes(b) ⇒ Object
-
#initialize(base) ⇒ BaseCode
constructor
A new instance of BaseCode.
- #initTable ⇒ Object
Constructor Details
#initialize(base) ⇒ BaseCode
Returns a new instance of BaseCode.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/src-generic/BaseCode.rb', line 9 def initialize(base) @base = base @charSet = base.toString() n = base.length() @nbits = 0 while(n>1) @nbits+=1 n = n / 2 end end |
Instance Method Details
#decodeBytes(b) ⇒ Object
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 |
# File 'lib/src-generic/BaseCode.rb', line 39 def decodeBytes(b) if @tbl.nil? initTable() end bitsize = b.length() * @nbits size = bitsize/8 o = [] buf = 0 curbits = 0 pin = 0 pout = 0 while (pout < size) while (curbits < 8) curbits += @nbits buf <<= @nbits i = @tbl[b.get(pin)] pin+=1 buf |= i end curbits -= 8 o[pout] = ((buf >> curbits) & 0xFF) pout += 1; end return Bytes.ofData(o) end |
#encodeBytes(b) ⇒ Object
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 |
# File 'lib/src-generic/BaseCode.rb', line 67 def encodeBytes(b) bas = @base size = (b.length()*8)/@nbits o = [] buf = 0 curbits = 0 mask = (1 << @nbits) -1 pin = 0 pout = 0 while (pout < size) while (curbits < @nbits) curbits += 8 buf <<= 8 buf |= b.get(pin) pin +=1 end curbits -= @nbits o[pout] = bas.get((buf >> curbits) & mask) pout+=1 end if (curbits > 0) o[pout] = bas.get((buf << (@nbits - curbits)) & mask) pout +=1 end return Bytes.ofData(o) end |
#initTable ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/src-generic/BaseCode.rb', line 20 def initTable() @tbl = [] for i in 0..255 @tbl[i] = -1 i+=1 end i = 0 for i in 0..@base.length() if !@base.get(i).nil? @tbl[@base.get(i)] = i end i+=1 end end |