OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2008 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can | |
4 * be found in the LICENSE file. | |
5 */ | |
6 | |
7 | |
8 // Portable interface for browser interaction - API invariant portions. | |
9 | |
10 #include "native_client/src/trusted/plugin/npapi/browser_impl_npapi.h" | |
11 | |
12 #include <setjmp.h> | |
13 #include <stdio.h> | |
14 #include <string.h> | |
15 | |
16 #include "native_client/src/include/nacl_string.h" | |
17 #include "native_client/src/include/nacl_elf.h" | |
18 #include "native_client/src/include/portability_io.h" | |
19 #include "native_client/src/shared/npruntime/nacl_npapi.h" | |
20 | |
21 namespace plugin { | |
22 | |
23 const char* BrowserInterface::kNoError = ""; | |
24 | |
25 // TODO(polina,sehr): move elf checking code to service_runtime. | |
26 bool BrowserInterface::MightBeElfExecutable(const nacl::string& filename, | |
27 nacl::string* error) { | |
28 FILE* fp = fopen(filename.c_str(), "rb"); | |
29 if (fp == NULL) { | |
30 *error = "Load failed: cannot open local file for reading."; | |
31 return false; | |
32 } | |
33 char buf[EI_NIDENT]; | |
34 size_t read_amount = fread(buf, sizeof buf, 1, fp); | |
35 fclose(fp); | |
36 if (read_amount != 1) { | |
37 *error = "Load failed: fread should not fail."; | |
38 return false; | |
39 } | |
40 return MightBeElfExecutable(buf, sizeof buf, error); | |
41 } | |
42 | |
43 bool BrowserInterface::MightBeElfExecutable(const char* e_ident_bytes, | |
44 size_t size, | |
45 nacl::string* error) { | |
46 if (size < EI_NIDENT) { | |
47 *error = "Load failed: file too short to be an ELF executable."; | |
48 return false; | |
49 } | |
50 if (strncmp(e_ident_bytes, EI_MAG0123, strlen(EI_MAG0123)) != 0) { | |
51 // This can happen if we read a 404 error page, for example. | |
52 *error = "Load failed: bad magic number; not an ELF executable."; | |
53 return false; | |
54 } | |
55 if (e_ident_bytes[EI_ABIVERSION] != EF_NACL_ABIVERSION) { | |
56 nacl::stringstream ss; | |
57 ss << "Load failed: ABI version mismatch: expected " << EF_NACL_ABIVERSION | |
58 << ", found " << (unsigned) e_ident_bytes[EI_ABIVERSION] << "."; | |
59 *error = ss.str(); | |
60 return false; | |
61 } | |
62 *error = kNoError; | |
63 return true; | |
64 } | |
65 | |
66 } // namespace plugin | |
OLD | NEW |