Class: IABConsentString::Bits

Inherits:
Object
  • Object
show all
Defined in:
lib/iab_consent_string/bits.rb

Constant Summary collapse

BYTE_POWS =

big endian

[ -128, 64, 32, 16, 8, 4, 2, 1 ]

Instance Method Summary collapse

Constructor Details

#initialize(b) ⇒ Bits

Returns a new instance of Bits.



10
11
12
# File 'lib/iab_consent_string/bits.rb', line 10

def initialize(b)
  @bytes = b
end

Instance Method Details

#getBinaryStringString

Returns a string representation of the byte array passed in the constructor. for example, a bit array of [4] yields a String of “0100”.

Returns:

  • (String)

    a string representation of the byte array passed in the constructor. for example, a bit array of [4] yields a String of “0100”



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/iab_consent_string/bits.rb', line 172

def getBinaryString
  s = String.new()
  size = length()
  for i in (0...size) do
    if (getBit(i))
      s << "1"
    else
      s << "0"
    end
  end
  s
end

#getBit(index) ⇒ Boolean

Get the value of the specified index bit, under a boolean format (1 = true, 0 = false)

Parameters:

  • index (Integer)

    the nth number bit to get from the bit string

Returns:

  • (Boolean)

    , true if the bit is switched to 1, false otherwise



17
18
19
20
21
22
# File 'lib/iab_consent_string/bits.rb', line 17

def getBit(index)
  byteIndex = index / 8
  bitExact = index % 8
  b = @bytes[byteIndex]
  return (b & BYTE_POWS[bitExact]) != 0
end

#getDateTimeFromEpochDeciseconds(startInclusive, size) ⇒ void

This method returns an undefined value.

returns a time derived from interpreting the given interval on the bit string as long representing

the number of demiseconds from the unix epoch

Parameters:

  • startInclusive (Integer)

    the bit from which to begin interpreting

  • size (Integer)

    the number of bits to interpret

Raises:

  • (VendorConsentParseError)

    when the number of bits requested cannot fit in a long



122
123
124
125
# File 'lib/iab_consent_string/bits.rb', line 122

def getDateTimeFromEpochDeciseconds(startInclusive,size)
  epochDemi = getLong(startInclusive, size)
  epochDemi * 100
end

#getInt(startInclusive, size) ⇒ void

This method returns an undefined value.

Interprets n number of bits as a big endiant int

Parameters:

  • startInclusive (Integer)

    the nth to begin interpreting from

  • size (Integer)

    the number of bits to interpret

Raises:

  • (VendorConsentParseError)

    when the bits cannot fit in an int sized field



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/iab_consent_string/bits.rb', line 45

def getInt(startInclusive, size)
  # Integer Size limited to 32 bits
  if (size > 32)
    raise IABConsentString::Error::VendorConsentParseError, "can't fit bit range in int " + size, caller
  end
  val = 0
  sigMask = 1
  sigIndex = size - 1

  for i in (0...size) do
    if getBit(startInclusive + i)
      val += (sigMask << sigIndex)
    end 
    sigIndex -= 1
  end
  val
end

#getLong(startInclusive, size) ⇒ Long

Interprets n bits as a big endian long

Parameters:

  • startInclusive (Integer)

    the nth to begin interpreting from

  • size (Integer)

    the number of bits to interpret

Returns:

  • (Long)

    the long value create by interpretation of provided bits

Raises:

  • (VendorConsentParseError)

    when the bits cannot fit in a long sized field



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/iab_consent_string/bits.rb', line 83

def getLong(startInclusive, size)
  # Long Size limited to 64 bits
  if (size > 64)
    raise IABConsentString::Error::VendorConsentParseError, "can't fit bit range in long: " + size , caller
  end
  val = 0
  sigMask = 1
  sigIndex = size - 1

  for i in (0...size) do
    if (getBit(startInclusive + i))
      val += (sigMask << sigIndex)
    end
    sigIndex -= 1
  end
  val
end

#getSixBitString(startInclusive, size) ⇒ String

Interprets the given interval in the bit string as a series of six bit characters, where 0=A and 26=Z

Parameters:

  • startInclusive (Integer)

    the nth bit in the bitstring from which to start the interpretation

  • size (Integer)

    the number of bits to include in the string

Returns:

  • (String)

    the string given by the above interpretation

Raises:

  • (VendorConsentParseError)

    when the requested interval is not a multiple of six



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/iab_consent_string/bits.rb', line 141

def getSixBitString(startInclusive, size)
  if (size % 6 != 0)
    raise IABConsentString::Error::VendorConsentParseError , "string bit length must be multiple of six: " + size, caller
  end
  charNum = size / 6
  val = String.new()
  for i in (0...charNum) do
    charCode = getInt(startInclusive + (i * 6), 6) + 65
    val << charCode.chr
  end
  val.upcase
end

#lengthInteger

Returns the number of bits in the bit string.

Returns:

  • (Integer)

    the number of bits in the bit string



132
133
134
# File 'lib/iab_consent_string/bits.rb', line 132

def length
  @bytes.length * 8
end

#maxOfSize(size) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/iab_consent_string/bits.rb', line 199

def maxOfSize(size)
  max = 0
  for i in (0...size) do
    max += 2**i
  end
  max
end

#setBit(index) ⇒ Object

Set the value of the specified index bit to 1

Parameters:

  • index (Integer)

    set the nth number bit from the bit string



26
27
28
29
30
# File 'lib/iab_consent_string/bits.rb', line 26

def setBit(index)
  byteIndex = index / 8
  shift = (byteIndex + 1) * 8 - index - 1
  @bytes[byteIndex] |= 1 << shift
end

#setDateTimeToEpochDeciseconds(startInclusive, size, dateTime) ⇒ Object



127
128
129
# File 'lib/iab_consent_string/bits.rb', line 127

def setDateTimeToEpochDeciseconds(startInclusive, size, dateTime)
  setLong(startInclusive, size, dateTime / 100)
end

#setInt(startInclusive, size, to) ⇒ void

This method returns an undefined value.

Writes an integer value into a bit array of given size

Parameters:

  • startInclusive (Integer)

    the nth to begin writing to

  • size (Integer)

    the number of bits available to write

  • to (Integer)

    the integer to write out

Raises:

  • (VendorConsentCreateError)

    when the bits cannot fit into the provided size



69
70
71
72
73
74
75
76
# File 'lib/iab_consent_string/bits.rb', line 69

def setInt(startInclusive, size, to)
  # Integer Size limited to 32 bits
  if (size > 32 || to > maxOfSize(size) || to < 0)
    raise IABConsentString::Error::VendorConsentCreateError, "can't fit integer into bit range of size" + size , caller
  end

  setNumber(startInclusive, size, to);
end

#setLong(startInclusive, size, to) ⇒ void

This method returns an undefined value.

Writes a long value into a bit array of given size

Parameters:

  • startInclusive (Integer)

    the nth to begin writing to

  • size (Integer)

    the number of bits available to write

  • to (Integer)

    the long number to write out

Raises:

  • (VendorConsentCreateError)

    when the bits cannot fit into the provided size



107
108
109
110
111
112
113
114
# File 'lib/iab_consent_string/bits.rb', line 107

def setLong(startInclusive, size, to)
  # Long Size limited to 64 bits
  if (size > 64 || to > maxOfSize(size) || to < 0)
    raise IABConsentString::Error::VendorConsentCreateError, "can't fit long into bit range of size " + size , caller
  end

  setNumber(startInclusive, size, to)
end

#setNumber(startInclusive, size, to) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/iab_consent_string/bits.rb', line 189

def setNumber(startInclusive,size,to)
  (size - 1).downto(0) do |i|
    index = startInclusive + i
    byteIndex = index / 8
    shift = (byteIndex + 1) * 8 - index - 1
    @bytes[byteIndex] |= (to % 2) << shift
    to /= 2
  end
end

#setSixBitString(startInclusive, size, to) ⇒ Object

Interprets characters, as 0=A and 26=Z and writes to the given interval in the bit string as a series of six bits

Parameters:

  • startInclusive (Integer)

    the nth bit in the bitstring from which to start writing

  • size (Integer)

    the size of the bitstring

  • to (Integer)

    the string given by the above interpretation

Raises:

  • (VendorConsentCreateError)

    when the requested interval is not a multiple of six



159
160
161
162
163
164
165
166
167
168
# File 'lib/iab_consent_string/bits.rb', line 159

def setSixBitString(startInclusive, size, to)
   if (size % 6 != 0 || size / 6 != to.length())
     raise IABConsentString::Error::VendorConsentCreateError , "bit array size must be multiple of six and equal to 6 times the size of string", caller
   end
   values = to.chars
   for i in (0...values.length) do
     charCode = values[i].ord - 65
     setInt(startInclusive + (i * 6), 6, charCode)
   end
end

#toByteArrayObject



185
186
187
# File 'lib/iab_consent_string/bits.rb', line 185

def toByteArray
  @bytes
end

#unsetBit(index) ⇒ Object

Set the value of the specified index bit to 1

Parameters:

  • index (Integer)

    set the nth number bit from the bit string



34
35
36
37
38
# File 'lib/iab_consent_string/bits.rb', line 34

def unsetBit(index)
  byteIndex = index / 8
  shift = (byteIndex + 1) * 8 - index - 1
  @bytes[byteIndex] &= ~(1 << shift)
end