12.06.2015 Views

The Annoyance Filter.pdf - Fourmilab

The Annoyance Filter.pdf - Fourmilab

The Annoyance Filter.pdf - Fourmilab

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

88 FLASH STREAM DECODER ANNOYANCE-FILTER §109<br />

109. Flash files include quantities packed into bit fields, the width of some of which are specified<br />

by other fields in the file. <strong>The</strong> following methods decode these packed fields. Call initBits to initialise<br />

decoding of a bit field which begins in the next (as yet unread) byte. <strong>The</strong>n call getBits or getSignedBits<br />

to return an n bit field without or with sign extension respectively.<br />

〈 Class implementations 11 〉 +≡<br />

void flashStream ::initBits (void)<br />

{ /∗ Reset the bit position and buffer. ∗/<br />

bitPos = 0;<br />

bitBuf = 0;<br />

} /∗ Get n bits from the stream. ∗/<br />

unsigned int flashStream ::getBits (int n)<br />

{<br />

unsigned int v = 0;<br />

while (true ) {<br />

int s = n − bitPos ;<br />

if (s > 0) { /∗ Consume the entire buffer ∗/<br />

v |= bitBuf ≪ s;<br />

n −= bitPos ; /∗ Get the next buffer ∗/<br />

bitBuf = get8 ( );<br />

bitPos = 8;<br />

}<br />

else { /∗ Consume a portion of the buffer ∗/<br />

v |= bitBuf ≫ −s;<br />

bitPos −= n;<br />

bitBuf &= # FF ≫ (8 − bitPos ); /∗ mask off the consumed bits ∗/<br />

return v;<br />

}<br />

}<br />

} /∗ Get n bits from the string with sign extension. ∗/<br />

int flashStream ::getSignedBits (const int n)<br />

{<br />

signed int v = static cast〈int〉(getBits (n)); /∗ Is the number negative? ∗/<br />

}<br />

if (v & (1 ≪ (n − 1))) { /∗ Yes. Extend the sign. ∗/<br />

v |= −1 ≪ n;<br />

}<br />

return v;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!