llvm.org GIT mirror llvm / f283a5e
pull some win32 code into common code, add bitcode identification support. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36846 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
3 changed file(s) with 29 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
4242 assert(magic && "Invalid magic number string");
4343 assert(length >=4 && "Invalid magic number length");
4444 switch (magic[0]) {
45 case 'B':
46 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
47 return Bitcode_FileType;
48 break;
4549 case 'l':
4650 if (magic[1] == 'l' && magic[2] == 'v') {
4751 if (magic[3] == 'c')
159163 return LTDL_SHLIB_EXT;
160164 }
161165
166 bool
167 Path::isBytecodeFile() const {
168 std::string actualMagic;
169 if (!getMagicNumber(actualMagic, 4))
170 return false;
171 return actualMagic == "llvc" || actualMagic == "llvm";
172 }
173
174 bool
175 Path::isBitcodeFile() const {
176 std::string actualMagic;
177 if (!getMagicNumber(actualMagic, 4))
178 return false;
179 return actualMagic == "BC\xC0\xDE";
180 }
181
182 bool Path::hasMagicNumber(const std::string &Magic) const {
183 std::string actualMagic;
184 if (getMagicNumber(actualMagic, Magic.size()))
185 return Magic == actualMagic;
186 return false;
187 }
188
189
190
162191 // Include the truly platform-specific parts of this class.
163192 #if defined(LLVM_ON_UNIX)
164193 #include "Unix/Path.inc"
266266 return path.substr(slash, dot - slash);
267267 }
268268
269 bool Path::hasMagicNumber(const std::string &Magic) const {
270 size_t len = Magic.size();
271 assert(len < 1024 && "Request for magic string too long");
272 char* buf = (char*) alloca(1 + len);
273 int fd = ::open(path.c_str(), O_RDONLY);
274 if (fd < 0)
275 return false;
276 size_t read_len = ::read(fd, buf, len);
277 close(fd);
278 if (len != read_len)
279 return false;
280 buf[len] = '\0';
281 return Magic == buf;
282 }
283
284269 bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
285270 assert(len < 1024 && "Request for magic string too long");
286271 char* buf = (char*) alloca(1 + len);
295280 }
296281 Magic.assign(buf,len);
297282 return true;
298 }
299
300 bool
301 Path::isBytecodeFile() const {
302 char buffer[4];
303 buffer[0] = 0;
304 int fd = ::open(path.c_str(), O_RDONLY);
305 if (fd < 0)
306 return false;
307 ssize_t bytes_read = ::read(fd, buffer, 4);
308 ::close(fd);
309 if (4 != bytes_read)
310 return false;
311
312 return (buffer[0] == 'l' && buffer[1] == 'l' && buffer[2] == 'v' &&
313 (buffer[3] == 'c' || buffer[3] == 'm'));
314283 }
315284
316285 bool
246246 return path.substr(slash, dot - slash);
247247 }
248248
249 bool Path::hasMagicNumber(const std::string &Magic) const {
250 std::string actualMagic;
251 if (getMagicNumber(actualMagic, Magic.size()))
252 return Magic == actualMagic;
253 return false;
254 }
255
256 bool
257 Path::isBytecodeFile() const {
258 std::string actualMagic;
259 if (!getMagicNumber(actualMagic, 4))
260 return false;
261 return actualMagic == "llvc" || actualMagic == "llvm";
262 }
263
264249 bool
265250 Path::exists() const {
266251 DWORD attr = GetFileAttributes(path.c_str());