| 5 | namespace Poseidon |
| 6 | { |
| 7 | bool SSCompress::Decode(char* dst, long lensb, QIStream& in) |
| 8 | { |
| 9 | if (lensb <= 0) |
| 10 | { |
| 11 | return true; |
| 12 | } |
| 13 | |
| 14 | int i, j, r, c, csum = 0, csr; |
| 15 | int flags; |
| 16 | for (i = 0; i < N - F; i++) |
| 17 | { |
| 18 | text_buf[i] = ' '; |
| 19 | } |
| 20 | r = N - F; |
| 21 | flags = 0; |
| 22 | while (lensb > 0) |
| 23 | { |
| 24 | if (((flags >>= 1) & 256) == 0) |
| 25 | { |
| 26 | c = in.get(); |
| 27 | flags = c | 0xff00; |
| 28 | } |
| 29 | if (in.fail() || in.eof()) |
| 30 | { |
| 31 | Fail("LZW: stream read failed"); |
| 32 | return false; |
| 33 | } |
| 34 | if (flags & 1) |
| 35 | { |
| 36 | c = in.get(); |
| 37 | if (in.fail() || in.eof()) |
| 38 | { |
| 39 | Fail("LZW: stream read failed"); |
| 40 | return false; |
| 41 | } |
| 42 | csum += (unsigned char)c; |
| 43 | *dst++ = c; |
| 44 | lensb--; |
| 45 | text_buf[r] = (unsigned char)c; |
| 46 | r++; |
| 47 | r &= (N - 1); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | i = in.get(); |
| 52 | j = in.get(); |
| 53 | if (in.fail() || in.eof()) |
| 54 | { |
| 55 | Fail("LZW: stream read failed"); |
| 56 | return false; |
| 57 | } |
| 58 | i |= (j & 0xf0) << 4; |
| 59 | j &= 0x0f; |
| 60 | j += THRESHOLD; |
| 61 | // Stop at lensb: a back-reference match copies up to F+THRESHOLD bytes in |
| 62 | // one step, but only `lensb` output bytes were requested (and dst is sized |
| 63 | // for exactly that). A crafted stream whose final match runs past the end |
| 64 | // would otherwise overflow the output buffer. |
no test coverage detected