public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Yuwei Chen" <yuwei.chen@intel.com>
To: devel@edk2.groups.io
Cc: Bob Feng <bob.c.feng@intel.com>, Liming Gao <gaoliming@byosoft.com.cn>
Subject: [PATCH 1/1] BaseTools/Brotli: Fix compressed data loss issue
Date: Fri, 14 May 2021 14:04:44 +0800	[thread overview]
Message-ID: <20210514060444.1224-1-yuwei.chen@intel.com> (raw)

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2946

Currenly, when using the Brotli tool to compress data, the output
compressed binary file does not record complete compressed data
when size of input file is too large, which makes the data loss and
will trigger decompress-check issue.

The Brotli document mentioned:
The brotli tool use BrotliEncoderCompressStream method to compresses
input stream to output stream. Under some circumstances (e.g. lack of
output stream capacity) the BrotliEncoderOperation would require
several calls to BrotliEncoderCompressStream. The method must be
called again until both input stream is depleted and encoder has no
more output after the method is called.

This patch fixes this issue based on the Brotli document.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Signed-off-by: Yuwei Chen <yuwei.chen@intel.com>
---
 .../Source/C/BrotliCompress/BrotliCompress.c  | 61 ++++++++++++-------
 1 file changed, 38 insertions(+), 23 deletions(-)

diff --git a/BaseTools/Source/C/BrotliCompress/BrotliCompress.c b/BaseTools/Source/C/BrotliCompress/BrotliCompress.c
index 5a1400fda310..62a6aed3dbd0 100644
--- a/BaseTools/Source/C/BrotliCompress/BrotliCompress.c
+++ b/BaseTools/Source/C/BrotliCompress/BrotliCompress.c
@@ -156,6 +156,7 @@ int CompressFile(char *InputFile, uint8_t *InputBuffer, char *OutputFile, uint8_
   uint8_t *NextOut;
   uint8_t *Input;
   uint8_t *Output;
+  size_t TotalOut;
   size_t OutSize;
   uint32_t SizeHint;
   BROTLI_BOOL IsOk;
@@ -214,39 +215,53 @@ int CompressFile(char *InputFile, uint8_t *InputBuffer, char *OutputFile, uint8_
       IsEof = !HasMoreInput(InputFileHandle);
     }
 
-    if (!BrotliEncoderCompressStream(EncodeState,
-      IsEof ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS,
-      &AvailableIn, &NextIn, &AvailableOut, &NextOut, NULL)) {
-        printf("Failed to compress data [%s]\n", InputFile);
-        IsOk = BROTLI_FALSE;
-        goto Finish;
-    }
-    if (AvailableOut == 0) {
-      OutSize = (size_t)(NextOut - Output);
-      if (OutSize > 0) {
-        fwrite(Output, 1, OutSize, OutputFileHandle);
-        if (ferror(OutputFileHandle)) {
-          printf("Failed to write output [%s]\n", OutputFile);
+    if (!IsEof){
+      do{
+        if (!BrotliEncoderCompressStream(EncodeState,
+        BROTLI_OPERATION_FLUSH,
+        &AvailableIn, &NextIn, &AvailableOut, &NextOut, &TotalOut)) {
+          printf("Failed to compress data [%s]\n", InputFile);
           IsOk = BROTLI_FALSE;
           goto Finish;
         }
+        OutSize = (size_t)(NextOut - Output);
+        if (OutSize > 0) {
+          fwrite(Output, 1, OutSize, OutputFileHandle);
+          if (ferror(OutputFileHandle)) {
+            printf("Failed to write output [%s]\n", OutputFile);
+            IsOk = BROTLI_FALSE;
+            goto Finish;
+          }
+        }
+        NextOut = Output;
+        AvailableOut = kFileBufferSize;
       }
-      AvailableOut = kFileBufferSize;
-      NextOut = Output;
+      while (AvailableIn > 0 || BrotliEncoderHasMoreOutput(EncodeState));
     }
-    if (BrotliEncoderIsFinished(EncodeState)) {
-      OutSize = (size_t)(NextOut - Output);
-      if (OutSize > 0) {
-        fwrite(Output, 1, OutSize, OutputFileHandle);
-        if (ferror(OutputFileHandle)) {
-          printf("Failed to write output [%s]\n", OutputFile);
+    else{
+      do{
+        if (!BrotliEncoderCompressStream(EncodeState,
+        BROTLI_OPERATION_FINISH,
+        &AvailableIn, &NextIn, &AvailableOut, &NextOut, &TotalOut)) {
+          printf("Failed to compress data [%s]\n", InputFile);
           IsOk = BROTLI_FALSE;
           goto Finish;
         }
-        AvailableOut = 0;
+        OutSize = (size_t)(NextOut - Output);
+        if (OutSize > 0) {
+          fwrite(Output, 1, OutSize, OutputFileHandle);
+          if (ferror(OutputFileHandle)) {
+            printf("Failed to write output [%s]\n", OutputFile);
+            IsOk = BROTLI_FALSE;
+            goto Finish;
+          }
+        }
+        NextOut = Output;
+        AvailableOut = kFileBufferSize;
       }
+      while (AvailableIn > 0 || BrotliEncoderHasMoreOutput(EncodeState));
     }
-    if (IsEof) {
+    if (BrotliEncoderIsFinished(EncodeState)){
       break;
     }
   }
-- 
2.27.0.windows.1


             reply	other threads:[~2021-05-14  6:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-14  6:04 Yuwei Chen [this message]
2021-05-14  7:56 ` [edk2-devel] [PATCH 1/1] BaseTools/Brotli: Fix compressed data loss issue Bob Feng
2021-05-14  7:58 ` Bob Feng
2021-05-17  3:18   ` Bob Feng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210514060444.1224-1-yuwei.chen@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox