| 338 | } |
| 339 | |
| 340 | ULONG Compressor::truncateTail(ULONG outLength) |
| 341 | { |
| 342 | /************************************** |
| 343 | * |
| 344 | * Reset to pack the input excluding a trailing fragment, restricted by the output length. |
| 345 | * Return the number of trailing input bytes that fit the given output length. |
| 346 | * |
| 347 | **************************************/ |
| 348 | fb_assert(m_length > outLength); |
| 349 | |
| 350 | if (m_runs.isEmpty()) |
| 351 | { |
| 352 | m_length -= outLength; |
| 353 | return outLength; |
| 354 | } |
| 355 | |
| 356 | auto space = (int) outLength; |
| 357 | ULONG inLength = 0; |
| 358 | |
| 359 | while (m_runs.hasData()) |
| 360 | { |
| 361 | if (--space <= 0) |
| 362 | break; |
| 363 | |
| 364 | auto length = m_runs.back(); |
| 365 | |
| 366 | if (length < 0) |
| 367 | { |
| 368 | const auto zipLength = (unsigned) -length; |
| 369 | const auto runLength = 1 + adjustRunLength(zipLength); |
| 370 | |
| 371 | if ((space -= runLength) < 0) |
| 372 | break; |
| 373 | |
| 374 | m_length -= runLength + 1; |
| 375 | inLength += zipLength; |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | fb_assert(length > 0 && length <= MAX_NONCOMP_RUN); |
| 380 | |
| 381 | if ((space -= length) < 0) |
| 382 | { |
| 383 | length += space; // how many bytes fit |
| 384 | |
| 385 | m_runs.back() -= length; |
| 386 | m_length -= length; |
| 387 | inLength += length; |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | m_length -= length + 1; |
| 392 | inLength += length; |
| 393 | } |
| 394 | |
| 395 | m_runs.pop(); |
| 396 | } |
| 397 | |