The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Anatol Pomazau | 5ae3f93 | 2012-02-28 07:21:08 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
| 35 | #include <limits.h> |
| 36 | |
| 37 | #include <windows.h> |
| 38 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 39 | int64_t file_size(const char *fn) |
| 40 | { |
| 41 | HANDLE file; |
| 42 | char *data; |
| 43 | DWORD sz; |
| 44 | |
| 45 | file = CreateFile( fn, |
| 46 | GENERIC_READ, |
| 47 | FILE_SHARE_READ, |
| 48 | NULL, |
| 49 | OPEN_EXISTING, |
| 50 | 0, |
| 51 | NULL ); |
| 52 | |
| 53 | if (file == INVALID_HANDLE_VALUE) |
| 54 | return -1; |
| 55 | |
| 56 | sz = GetFileSize( file, NULL ); |
| 57 | CloseHandle( file ); |
| 58 | |
| 59 | return sz; |
| 60 | } |
| 61 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | void get_my_path(char exe[PATH_MAX]) |
| 63 | { |
| 64 | char* r; |
| 65 | |
| 66 | GetModuleFileName( NULL, exe, PATH_MAX-1 ); |
| 67 | exe[PATH_MAX-1] = 0; |
| 68 | r = strrchr( exe, '\\' ); |
| 69 | if (r) |
| 70 | *r = 0; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void *load_file(const char *fn, unsigned *_sz) |
| 75 | { |
| 76 | HANDLE file; |
| 77 | char *data; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 78 | DWORD sz; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 | |
| 80 | file = CreateFile( fn, |
| 81 | GENERIC_READ, |
| 82 | FILE_SHARE_READ, |
| 83 | NULL, |
| 84 | OPEN_EXISTING, |
| 85 | 0, |
| 86 | NULL ); |
| 87 | |
| 88 | if (file == INVALID_HANDLE_VALUE) |
| 89 | return NULL; |
| 90 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 91 | sz = GetFileSize( file, NULL ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 | data = NULL; |
| 93 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 94 | if (sz > 0) { |
| 95 | data = (char*) malloc( sz ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | if (data == NULL) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 97 | fprintf(stderr, "load_file: could not allocate %ld bytes\n", sz ); |
| 98 | sz = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | } else { |
| 100 | DWORD out_bytes; |
| 101 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 102 | if ( !ReadFile( file, data, sz, &out_bytes, NULL ) || |
| 103 | out_bytes != sz ) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 105 | fprintf(stderr, "load_file: could not read %ld bytes from '%s'\n", sz, fn); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | free(data); |
| 107 | data = NULL; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 108 | sz = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | CloseHandle( file ); |
| 113 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 114 | *_sz = (unsigned) sz; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 115 | return data; |
| 116 | } |