• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <ziparchive/zip_archive.h>
20 
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 
25 #include <memory>
26 #include <utility>
27 #include <vector>
28 
29 #include "android-base/macros.h"
30 #include "android-base/mapped_file.h"
31 #include "android-base/memory.h"
32 #include "zip_cd_entry_map.h"
33 #include "zip_error.h"
34 
35 class MappedZipFile {
36  public:
37   explicit MappedZipFile(int fd, off64_t length = -1, off64_t offset = 0);
38 
MappedZipFile(const void * address,size_t length)39   explicit MappedZipFile(const void* address, size_t length)
40       : base_ptr_(address), data_length_(static_cast<off64_t>(length)) {}
41 
42   int GetFileDescriptor() const;
43 
44   const void* GetBasePtr() const;
45 
46   off64_t GetFileOffset() const;
47 
48   off64_t GetFileLength() const;
49 
50   const uint8_t* ReadAtOffset(uint8_t* buf, size_t len, off64_t off) const;
51 
52  private:
53   std::unique_ptr<android::base::MappedFile> mapped_file_;
54 
55   const int fd_ = -1;
56   const off64_t fd_offset_ = 0;
57 
58   const void* base_ptr_ = nullptr;
59   mutable off64_t data_length_ = -1;
60 };
61 
62 class CentralDirectory {
63  public:
CentralDirectory(void)64   CentralDirectory(void) : base_ptr_(nullptr), length_(0) {}
65 
GetBasePtr()66   const uint8_t* GetBasePtr() const { return base_ptr_; }
67 
GetMapLength()68   size_t GetMapLength() const { return length_; }
69 
70   void Initialize(const void* map_base_ptr, off64_t cd_start_offset, size_t cd_size);
71 
72  private:
73   const uint8_t* base_ptr_;
74   size_t length_;
75 };
76 
77 struct ZipArchive {
78   // open Zip archive
79   mutable MappedZipFile mapped_zip;
80   const bool close_file;
81 
82   // mapped central directory area
83   off64_t directory_offset;
84   CentralDirectory central_directory;
85   std::unique_ptr<android::base::MappedFile> directory_map;
86 
87   // number of entries in the Zip archive
88   uint64_t num_entries;
89   std::unique_ptr<CdEntryMapInterface> cd_entry_map;
90 
91   ZipArchive(MappedZipFile&& map, bool assume_ownership);
92   ZipArchive(const void* address, size_t length);
93   ~ZipArchive();
94 
95   bool InitializeCentralDirectory(off64_t cd_start_offset, size_t cd_size);
96 };
97 
98 // Reads the unaligned data of type |T| and auto increment the offset.
99 template <typename T>
ConsumeUnaligned(uint8_t ** address)100 static T ConsumeUnaligned(uint8_t** address) {
101   auto ret = android::base::get_unaligned<T>(*address);
102   *address += sizeof(T);
103   return ret;
104 }
105 
106 template <typename T>
ConsumeUnaligned(const uint8_t ** address)107 static T ConsumeUnaligned(const uint8_t** address) {
108   return ConsumeUnaligned<T>(const_cast<uint8_t**>(address));
109 }
110 
111 // Writes the unaligned data of type |T| and auto increment the offset.
112 template <typename T>
EmitUnaligned(uint8_t ** address,T data)113 void EmitUnaligned(uint8_t** address, T data) {
114   android::base::put_unaligned<T>(*address, data);
115   *address += sizeof(T);
116 }
117