xref: /aosp_15_r20/external/zlib/patches/0016-minizip-parse-unicode-path-extra-field.patch (revision 86ee64e75fa5f8bce2c8c356138035642429cd05)
1commit c8834821f452a3d424edd0ed2a1e9ceeda38d0ea
2Author: Alex Danilo <[email protected]>
3Date:   Thu May 12 03:29:52 2022 +0000
4
5    Extract: Parse Unicode Path Extra field in minizip
6
7    Adds parsing of the Info-ZIP Extra field which overrides the
8    file name in the File Header only if the CRC in the extra field
9    is a CRC of the file name in the File Header.
10
11    See https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
12    section 4.6.9 for reference.
13
14    Also tidied up some whitespace indent.
15
16    Bug: 953256, 953599
17    Tests: Manually tested, auto test in follow on CL
18    Change-Id: I1283dcb88a203c3bb56c1d9c504035a2e51aecbd
19    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3641742
20    Reviewed-by: Noel Gordon <[email protected]>
21    Commit-Queue: Alex Danilo <[email protected]>
22    Cr-Commit-Position: refs/heads/main@{#1002476}
23
24diff --git a/third_party/zlib/contrib/minizip/unzip.c b/third_party/zlib/contrib/minizip/unzip.c
25index c8a01b23efd42..42677cff82c96 100644
26--- a/third_party/zlib/contrib/minizip/unzip.c
27+++ b/third_party/zlib/contrib/minizip/unzip.c
28@@ -193,6 +193,26 @@ typedef struct
29    Reads a long in LSB order from the given gz_stream. Sets
30 */
31
32+local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,
33+                             voidpf filestream,
34+                             int *pi) {
35+    unsigned char c;
36+    int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
37+    if (err==1)
38+    {
39+        *pi = (int)c;
40+        return UNZ_OK;
41+    }
42+    else
43+    {
44+        *pi = 0;
45+        if (ZERROR64(*pzlib_filefunc_def,filestream))
46+            return UNZ_ERRNO;
47+        else
48+            return UNZ_EOF;
49+    }
50+}
51+
52 local int unz64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def,
53                               voidpf filestream,
54                               uLong *pX) {
55@@ -948,6 +968,62 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file,
56                 }
57
58             }
59+            else if (headerId == 0x7075) /* Info-ZIP Unicode Path Extra Field */
60+            {
61+                int version = 0;
62+
63+                if (unz64local_getByte(&s->z_filefunc, s->filestream, &version) != UNZ_OK)
64+                {
65+                    err = UNZ_ERRNO;
66+                }
67+                if (version != 1)
68+                {
69+                    if (ZSEEK64(s->z_filefunc, s->filestream,dataSize - 1, ZLIB_FILEFUNC_SEEK_CUR) != 0)
70+                    {
71+                        err = UNZ_ERRNO;
72+                    }
73+                }
74+                else
75+                {
76+                    uLong uCrc, uHeaderCrc, fileNameSize;
77+
78+                    if (unz64local_getLong(&s->z_filefunc, s->filestream, &uCrc) != UNZ_OK)
79+                    {
80+                        err = UNZ_ERRNO;
81+                    }
82+                    uHeaderCrc = crc32(0, (const unsigned char *)szFileName, file_info.size_filename);
83+                    fileNameSize = dataSize - (2 * sizeof (short) + 1);
84+                    /* Check CRC against file name in the header. */
85+                    if (uHeaderCrc != uCrc)
86+                    {
87+                        if (ZSEEK64(s->z_filefunc, s->filestream, fileNameSize, ZLIB_FILEFUNC_SEEK_CUR) != 0)
88+                        {
89+                            err = UNZ_ERRNO;
90+                        }
91+                    }
92+                    else
93+                    {
94+                        uLong uSizeRead;
95+
96+                        if (fileNameSize < fileNameBufferSize)
97+                        {
98+                             *(szFileName + fileNameSize) = '\0';
99+                            uSizeRead = fileNameSize;
100+                        }
101+                        else
102+                        {
103+                            uSizeRead = fileNameBufferSize;
104+                        }
105+                        if ((fileNameSize > 0) && (fileNameBufferSize > 0))
106+                        {
107+                            if (ZREAD64(s->z_filefunc, s->filestream, szFileName, uSizeRead) != uSizeRead)
108+                            {
109+                                err = UNZ_ERRNO;
110+                            }
111+                        }
112+                    }
113+                }
114+            }
115             else
116             {
117                 if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0)
118