Archive members cannot be larger than 4GB. Return a uint32_t.
[oota-llvm.git] / include / llvm / Object / Archive.h
1 //===- Archive.h - ar archive file format -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ar archive file format class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_ARCHIVE_H
15 #define LLVM_OBJECT_ARCHIVE_H
16
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/MemoryBuffer.h"
21
22 namespace llvm {
23 namespace object {
24 struct ArchiveMemberHeader {
25   char Name[16];
26   char LastModified[12];
27   char UID[6];
28   char GID[6];
29   char AccessMode[8];
30   char Size[10]; ///< Size of data, not including header or padding.
31   char Terminator[2];
32
33   /// Get the name without looking up long names.
34   llvm::StringRef getName() const;
35
36   /// Members are not larger than 4GB.
37   uint32_t getSize() const;
38 };
39
40 class Archive : public Binary {
41   virtual void anchor();
42 public:
43   class Child {
44     const Archive *Parent;
45     /// \brief Includes header but not padding byte.
46     StringRef Data;
47     /// \brief Offset from Data to the start of the file.
48     uint16_t StartOfFile;
49
50     const ArchiveMemberHeader *getHeader() const {
51       return reinterpret_cast<const ArchiveMemberHeader *>(Data.data());
52     }
53
54   public:
55     Child(const Archive *Parent, const char *Start);
56
57     bool operator ==(const Child &other) const {
58       assert(Parent == other.Parent);
59       return Data.begin() == other.Data.begin();
60     }
61
62     bool operator <(const Child &other) const {
63       return Data.begin() < other.Data.begin();
64     }
65
66     Child getNext() const;
67
68     error_code getName(StringRef &Result) const;
69     StringRef getRawName() const { return getHeader()->getName(); }
70     /// \return the size of the archive member without the header or padding.
71     uint64_t getSize() const { return Data.size() - StartOfFile; }
72
73     StringRef getBuffer() const {
74       return StringRef(Data.data() + StartOfFile, getSize());
75     }
76
77     error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
78                                bool FullPath = false) const;
79
80     error_code getAsBinary(OwningPtr<Binary> &Result) const;
81   };
82
83   class child_iterator {
84     Child child;
85   public:
86     child_iterator() : child(Child(0, 0)) {}
87     child_iterator(const Child &c) : child(c) {}
88     const Child* operator->() const {
89       return &child;
90     }
91
92     bool operator==(const child_iterator &other) const {
93       return child == other.child;
94     }
95
96     bool operator!=(const child_iterator &other) const {
97       return !(*this == other);
98     }
99
100     bool operator <(const child_iterator &other) const {
101       return child < other.child;
102     }
103
104     child_iterator& operator++() {  // Preincrement
105       child = child.getNext();
106       return *this;
107     }
108   };
109
110   class Symbol {
111     const Archive *Parent;
112     uint32_t SymbolIndex;
113     uint32_t StringIndex; // Extra index to the string.
114
115   public:
116     bool operator ==(const Symbol &other) const {
117       return (Parent == other.Parent) && (SymbolIndex == other.SymbolIndex);
118     }
119
120     Symbol(const Archive *p, uint32_t symi, uint32_t stri)
121       : Parent(p)
122       , SymbolIndex(symi)
123       , StringIndex(stri) {}
124     error_code getName(StringRef &Result) const;
125     error_code getMember(child_iterator &Result) const;
126     Symbol getNext() const;
127   };
128
129   class symbol_iterator {
130     Symbol symbol;
131   public:
132     symbol_iterator(const Symbol &s) : symbol(s) {}
133     const Symbol *operator->() const {
134       return &symbol;
135     }
136
137     bool operator==(const symbol_iterator &other) const {
138       return symbol == other.symbol;
139     }
140
141     bool operator!=(const symbol_iterator &other) const {
142       return !(*this == other);
143     }
144
145     symbol_iterator& operator++() {  // Preincrement
146       symbol = symbol.getNext();
147       return *this;
148     }
149   };
150
151   Archive(MemoryBuffer *source, error_code &ec);
152
153   enum Kind {
154     K_GNU,
155     K_BSD,
156     K_COFF
157   };
158
159   Kind kind() const { 
160     return Format;
161   }
162
163   child_iterator begin_children(bool skip_internal = true) const;
164   child_iterator end_children() const;
165
166   symbol_iterator begin_symbols() const;
167   symbol_iterator end_symbols() const;
168
169   // Cast methods.
170   static inline bool classof(Binary const *v) {
171     return v->isArchive();
172   }
173
174   // check if a symbol is in the archive
175   child_iterator findSym(StringRef name) const;
176
177 private:
178   child_iterator SymbolTable;
179   child_iterator StringTable;
180   Kind Format;
181 };
182
183 }
184 }
185
186 #endif