1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_LITE_SUPPORT_METADATA_CC_UTILS_ZIP_MEM_FILE_H_
17 #define TENSORFLOW_LITE_SUPPORT_METADATA_CC_UTILS_ZIP_MEM_FILE_H_
18 
19 #include <cstdlib>
20 
21 #include "absl/strings/string_view.h"
22 #include "contrib/minizip/ioapi.h"
23 
24 namespace tflite {
25 namespace metadata {
26 
27 // In-memory read-only zip file implementation.
28 //
29 // Adapted from [1], with a few key differences:
30 // * backed by an `absl::string_view` instead of malloc-ed C buffers,
31 // * supports opening the file for reading through `unzOpen2_64`.
32 //
33 // This class is NOT thread-safe.
34 //
35 // [1]:
36 // https://github.com/google/libkml/blob/master/third_party/zlib-1.2.3/contrib/minizip/iomem_simple.c
37 class ZipReadOnlyMemFile {
38  public:
39   // Constructs an in-memory read-only zip file from a buffer. Does not copy or
40   // take ownership over the provided buffer: the caller is responsible for
41   // ensuring the buffer outlives this object.
42   ZipReadOnlyMemFile(const char* buffer, size_t size);
43   // Provides access to the `zlib_filefunc64_def` implementation for the
44   // in-memory zip file.
45   zlib_filefunc64_def& GetFileFunc64Def();
46 
47  private:
48   // The string view backing the in-memory file.
49   absl::string_view data_;
50   // The current offset in the file.
51   ZPOS64_T offset_;
52   // The `zlib_filefunc64_def` implementation for this in-memory zip file.
53   zlib_filefunc64_def zlib_filefunc64_def_;
54 
55   // Convenience function to access the current data size.
Size()56   size_t Size() const { return data_.size(); }
57 
58   // The file function implementations used in the `zlib_filefunc64_def`.
59   static voidpf OpenFile(voidpf opaque, const void* filename, int mode);
60   static uLong ReadFile(voidpf opaque, voidpf stream, void* buf, uLong size);
61   static uLong WriteFile(voidpf opaque, voidpf stream, const void* buf,
62                          uLong size);
63   static ZPOS64_T TellFile(voidpf opaque, voidpf stream);
64   static long SeekFile  // NOLINT
65       (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin);
66   static int CloseFile(voidpf opaque, voidpf stream);
67   static int ErrorFile(voidpf opaque, voidpf stream);
68 };
69 
70 }  // namespace metadata
71 }  // namespace tflite
72 
73 #endif  // TENSORFLOW_LITE_SUPPORT_METADATA_CC_UTILS_ZIP_MEM_FILE_H_