Decompression by JNI failed #373

Closed
opened 2026-01-29 20:42:51 +00:00 by claunia · 6 comments
Owner

Originally created by @wlwenming on GitHub (Feb 7, 2021).

Decompressing did not complete, expected to return 4 (OK), but returned 2 (NEEDS_MORE_INPUT).

The debugging information is as follows:
image

Please help to analyze the reasons. Thanks.

Originally created by @wlwenming on GitHub (Feb 7, 2021). Decompressing did not complete, expected to return 4 (OK), but returned 2 (NEEDS_MORE_INPUT). The debugging information is as follows: ![image](https://user-images.githubusercontent.com/7542421/107138857-0d853300-6952-11eb-9691-58f367e1d6a0.png) Please help to analyze the reasons. Thanks.
Author
Owner

@wlwenming commented on GitHub (Feb 8, 2021):

I found a solution.Make sure the status is correct.

JNIEXPORT jobject JNICALL
Java_org_brotli_wrapper_dec_DecoderJNI_nativePull(
    JNIEnv* env, jobject /*jobj*/, jlongArray ctx) {
  jlong context[3];
  env->GetLongArrayRegion(ctx, 0, 3, context);
  DecoderHandle* handle = getHandle(reinterpret_cast<void*>(context[0]));
  size_t data_length = 0;
  const uint8_t* data = BrotliDecoderTakeOutput(handle->state, &data_length);
  bool hasMoreOutput = !!BrotliDecoderHasMoreOutput(handle->state);
  if (hasMoreOutput) {
    context[1] = 3;
  } else if (BrotliDecoderIsFinished(handle->state)) {
    /* Bytes after stream end are not allowed. */
    context[1] = (handle->input_offset == handle->input_length) ? 1 : 0;
  } else {
    /* Pull decompressed data is ok */
    context[1] = 4;
  }
  context[2] = hasMoreOutput ? 1 : 0;
  env->SetLongArrayRegion(ctx, 0, 3, context);
  return env->NewDirectByteBuffer(const_cast<uint8_t*>(data), data_length);
}
@wlwenming commented on GitHub (Feb 8, 2021): I found a solution.Make sure the status is correct. ``` JNIEXPORT jobject JNICALL Java_org_brotli_wrapper_dec_DecoderJNI_nativePull( JNIEnv* env, jobject /*jobj*/, jlongArray ctx) { jlong context[3]; env->GetLongArrayRegion(ctx, 0, 3, context); DecoderHandle* handle = getHandle(reinterpret_cast<void*>(context[0])); size_t data_length = 0; const uint8_t* data = BrotliDecoderTakeOutput(handle->state, &data_length); bool hasMoreOutput = !!BrotliDecoderHasMoreOutput(handle->state); if (hasMoreOutput) { context[1] = 3; } else if (BrotliDecoderIsFinished(handle->state)) { /* Bytes after stream end are not allowed. */ context[1] = (handle->input_offset == handle->input_length) ? 1 : 0; } else { /* Pull decompressed data is ok */ context[1] = 4; } context[2] = hasMoreOutput ? 1 : 0; env->SetLongArrayRegion(ctx, 0, 3, context); return env->NewDirectByteBuffer(const_cast<uint8_t*>(data), data_length); } ```
Author
Owner

@eustas commented on GitHub (Jun 23, 2021):

So the situation happens when output size is exactly the power of 2.
In this case native decoder reports that output is ready.
Decoder saves the remaining part of last byte, which contains the "no more input" section (it is just 2 bits IIRC).
"take output" does not flip "is finished" state.
Going to think a little bit more, on what the right fix should be.
Next invocation to "process" would have detected that input is over...

@eustas commented on GitHub (Jun 23, 2021): So the situation happens when output size is exactly the power of 2. In this case native decoder reports that output is ready. Decoder saves the remaining part of last byte, which contains the "no more input" section (it is just 2 bits IIRC). "take output" does not flip "is finished" state. Going to think a little bit more, on what the right fix should be. Next invocation to "process" would have detected that input is over...
Author
Owner

@eustas commented on GitHub (Jun 24, 2021):

Pinpointed the problem. Fix is coming.

@eustas commented on GitHub (Jun 24, 2021): Pinpointed the problem. Fix is coming.
Author
Owner

@slandelle commented on GitHub (Jul 24, 2021):

@eustas gentle ping

@slandelle commented on GitHub (Jul 24, 2021): @eustas gentle ping
Author
Owner

@eustas commented on GitHub (Jul 29, 2021):

Should be fixed with the latest update.

@eustas commented on GitHub (Jul 29, 2021): Should be fixed with the latest update.
Author
Owner

@slandelle commented on GitHub (Jul 30, 2021):

Thanks a bunch!

@slandelle commented on GitHub (Jul 30, 2021): Thanks a bunch!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/brotli#373