Actual decompress implementation.
(State s)
| 1235 | * Actual decompress implementation. |
| 1236 | */ |
| 1237 | static int decompress(State s) { |
| 1238 | int result; |
| 1239 | if (s.runningState == UNINITIALIZED) { |
| 1240 | return Utils.makeError(s, BROTLI_PANIC_STATE_NOT_INITIALIZED); |
| 1241 | } |
| 1242 | if (s.runningState < 0) { |
| 1243 | return Utils.makeError(s, BROTLI_PANIC_UNEXPECTED_STATE); |
| 1244 | } |
| 1245 | if (s.runningState == CLOSED) { |
| 1246 | return Utils.makeError(s, BROTLI_PANIC_ALREADY_CLOSED); |
| 1247 | } |
| 1248 | if (s.runningState == INITIALIZED) { |
| 1249 | final int windowBits = decodeWindowBits(s); |
| 1250 | if (windowBits == -1) { /* Reserved case for future expansion. */ |
| 1251 | return Utils.makeError(s, BROTLI_ERROR_INVALID_WINDOW_BITS); |
| 1252 | } |
| 1253 | s.maxRingBufferSize = 1 << windowBits; |
| 1254 | s.maxBackwardDistance = s.maxRingBufferSize - 16; |
| 1255 | s.runningState = BLOCK_START; |
| 1256 | } |
| 1257 | |
| 1258 | int fence = calculateFence(s); |
| 1259 | int ringBufferMask = s.ringBufferSize - 1; |
| 1260 | byte[] ringBuffer = s.ringBuffer; |
| 1261 | |
| 1262 | while (s.runningState != FINISHED) { |
| 1263 | // TODO(eustas): extract cases to methods for the better readability. |
| 1264 | switch (s.runningState) { |
| 1265 | case BLOCK_START: |
| 1266 | if (s.metaBlockLength < 0) { |
| 1267 | return Utils.makeError(s, BROTLI_ERROR_INVALID_METABLOCK_LENGTH); |
| 1268 | } |
| 1269 | result = readNextMetablockHeader(s); |
| 1270 | if (result < BROTLI_OK) { |
| 1271 | return result; |
| 1272 | } |
| 1273 | /* Ring-buffer would be reallocated here. */ |
| 1274 | fence = calculateFence(s); |
| 1275 | ringBufferMask = s.ringBufferSize - 1; |
| 1276 | ringBuffer = s.ringBuffer; |
| 1277 | continue; |
| 1278 | |
| 1279 | case COMPRESSED_BLOCK_START: { |
| 1280 | result = readMetablockHuffmanCodesAndContextMaps(s); |
| 1281 | if (result < BROTLI_OK) { |
| 1282 | return result; |
| 1283 | } |
| 1284 | s.runningState = MAIN_LOOP; |
| 1285 | continue; |
| 1286 | } |
| 1287 | |
| 1288 | case MAIN_LOOP: |
| 1289 | if (s.metaBlockLength <= 0) { |
| 1290 | s.runningState = BLOCK_START; |
| 1291 | continue; |
| 1292 | } |
| 1293 | if (s.halfOffset > BitReader.HALF_WATERLINE) { |
| 1294 | result = BitReader.readMoreInput(s); |
no test coverage detected