<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:SimSun;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@SimSun";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Created a PR for this patch. <a href="https://github.com/tianocore/edk2/pull/1648">
https://github.com/tianocore/edk2/pull/1648</a><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal">Bob<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> devel@edk2.groups.io <devel@edk2.groups.io> <b>
On Behalf Of </b>Bob Feng<br>
<b>Sent:</b> Friday, May 14, 2021 3:58 PM<br>
<b>To:</b> Chen, Christine <yuwei.chen@intel.com>; devel@edk2.groups.io<br>
<b>Subject:</b> Re: [edk2-devel] [PATCH 1/1] BaseTools/Brotli: Fix compressed data loss issue<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Reviewed-by: Bob Feng <<a href="mailto:bob.c.feng@intel.com">bob.c.feng@intel.com</a>><br>
<br>
On Fri, May 14, 2021 at 02:04 PM, Yuwei Chen wrote:<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal">REF: <a href="https://bugzilla.tianocore.org/show_bug.cgi?id=2946" target="_blank">
https://bugzilla.tianocore.org/show_bug.cgi?id=2946</a><br>
<br>
Currenly, when using the Brotli tool to compress data, the output<br>
compressed binary file does not record complete compressed data<br>
when size of input file is too large, which makes the data loss and<br>
will trigger decompress-check issue.<br>
<br>
The Brotli document mentioned:<br>
The brotli tool use BrotliEncoderCompressStream method to compresses<br>
input stream to output stream. Under some circumstances (e.g. lack of<br>
output stream capacity) the BrotliEncoderOperation would require<br>
several calls to BrotliEncoderCompressStream. The method must be<br>
called again until both input stream is depleted and encoder has no<br>
more output after the method is called.<br>
<br>
This patch fixes this issue based on the Brotli document.<br>
<br>
Cc: Bob Feng <<a href="mailto:bob.c.feng@intel.com">bob.c.feng@intel.com</a>><br>
Cc: Liming Gao <<a href="mailto:gaoliming@byosoft.com.cn">gaoliming@byosoft.com.cn</a>><br>
Signed-off-by: Yuwei Chen <<a href="mailto:yuwei.chen@intel.com">yuwei.chen@intel.com</a>><br>
---<br>
.../Source/C/BrotliCompress/BrotliCompress.c | 61 ++++++++++++-------<br>
1 file changed, 38 insertions(+), 23 deletions(-)<br>
<br>
diff --git a/BaseTools/Source/C/BrotliCompress/BrotliCompress.c b/BaseTools/Source/C/BrotliCompress/BrotliCompress.c<br>
index 5a1400fda310..62a6aed3dbd0 100644<br>
--- a/BaseTools/Source/C/BrotliCompress/BrotliCompress.c<br>
+++ b/BaseTools/Source/C/BrotliCompress/BrotliCompress.c<br>
@@ -156,6 +156,7 @@ int CompressFile(char *InputFile, uint8_t *InputBuffer, char *OutputFile, uint8_<br>
uint8_t *NextOut;<br>
uint8_t *Input;<br>
uint8_t *Output;<br>
+ size_t TotalOut;<br>
size_t OutSize;<br>
uint32_t SizeHint;<br>
BROTLI_BOOL IsOk;<br>
@@ -214,39 +215,53 @@ int CompressFile(char *InputFile, uint8_t *InputBuffer, char *OutputFile, uint8_<br>
IsEof = !HasMoreInput(InputFileHandle);<br>
}<br>
<br>
- if (!BrotliEncoderCompressStream(EncodeState,<br>
- IsEof ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS,<br>
- &AvailableIn, &NextIn, &AvailableOut, &NextOut, NULL)) {<br>
- printf("Failed to compress data [%s]\n", InputFile);<br>
- IsOk = BROTLI_FALSE;<br>
- goto Finish;<br>
- }<br>
- if (AvailableOut == 0) {<br>
- OutSize = (size_t)(NextOut - Output);<br>
- if (OutSize > 0) {<br>
- fwrite(Output, 1, OutSize, OutputFileHandle);<br>
- if (ferror(OutputFileHandle)) {<br>
- printf("Failed to write output [%s]\n", OutputFile);<br>
+ if (!IsEof){<br>
+ do{<br>
+ if (!BrotliEncoderCompressStream(EncodeState,<br>
+ BROTLI_OPERATION_FLUSH,<br>
+ &AvailableIn, &NextIn, &AvailableOut, &NextOut, &TotalOut)) {<br>
+ printf("Failed to compress data [%s]\n", InputFile);<br>
IsOk = BROTLI_FALSE;<br>
goto Finish;<br>
}<br>
+ OutSize = (size_t)(NextOut - Output);<br>
+ if (OutSize > 0) {<br>
+ fwrite(Output, 1, OutSize, OutputFileHandle);<br>
+ if (ferror(OutputFileHandle)) {<br>
+ printf("Failed to write output [%s]\n", OutputFile);<br>
+ IsOk = BROTLI_FALSE;<br>
+ goto Finish;<br>
+ }<br>
+ }<br>
+ NextOut = Output;<br>
+ AvailableOut = kFileBufferSize;<br>
}<br>
- AvailableOut = kFileBufferSize;<br>
- NextOut = Output;<br>
+ while (AvailableIn > 0 || BrotliEncoderHasMoreOutput(EncodeState));<br>
}<br>
- if (BrotliEncoderIsFinished(EncodeState)) {<br>
- OutSize = (size_t)(NextOut - Output);<br>
- if (OutSize > 0) {<br>
- fwrite(Output, 1, OutSize, OutputFileHandle);<br>
- if (ferror(OutputFileHandle)) {<br>
- printf("Failed to write output [%s]\n", OutputFile);<br>
+ else{<br>
+ do{<br>
+ if (!BrotliEncoderCompressStream(EncodeState,<br>
+ BROTLI_OPERATION_FINISH,<br>
+ &AvailableIn, &NextIn, &AvailableOut, &NextOut, &TotalOut)) {<br>
+ printf("Failed to compress data [%s]\n", InputFile);<br>
IsOk = BROTLI_FALSE;<br>
goto Finish;<br>
}<br>
- AvailableOut = 0;<br>
+ OutSize = (size_t)(NextOut - Output);<br>
+ if (OutSize > 0) {<br>
+ fwrite(Output, 1, OutSize, OutputFileHandle);<br>
+ if (ferror(OutputFileHandle)) {<br>
+ printf("Failed to write output [%s]\n", OutputFile);<br>
+ IsOk = BROTLI_FALSE;<br>
+ goto Finish;<br>
+ }<br>
+ }<br>
+ NextOut = Output;<br>
+ AvailableOut = kFileBufferSize;<br>
}<br>
+ while (AvailableIn > 0 || BrotliEncoderHasMoreOutput(EncodeState));<br>
}<br>
- if (IsEof) {<br>
+ if (BrotliEncoderIsFinished(EncodeState)){<br>
break;<br>
}<br>
}<br>
-- <br>
2.27.0.windows.1<o:p></o:p></p>
</blockquote>
<div>
<p class="MsoNormal"></o:p></span></p>
</div>
</div>
</body>
</html>


 <div width="1" style="color:white;clear:both">_._,_._,_</div> <hr>   Groups.io Links:<p>   You receive all messages sent to this group.    <p> <a target="_blank" href="https://edk2.groups.io/g/devel/message/75162">View/Reply Online (#75162)</a> |    |  <a target="_blank" href="https://groups.io/mt/82817984/1813853">Mute This Topic</a>  | <a href="https://edk2.groups.io/g/devel/post">New Topic</a><br>    <a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> | <a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |  <a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>  [edk2-devel-archive@redhat.com]<br> <div width="1" style="color:white;clear:both">_._,_._,_</div>