Home | History | Annotate | Download | only in fastboot
      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
     12  *    the documentation and/or other materials provided with the
     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
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     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 
     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 
     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;
     78     DWORD     sz;
     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 
     91     sz = GetFileSize( file, NULL );
     92     data      = NULL;
     93 
     94     if (sz > 0) {
     95         data = (char*) malloc( sz );
     96         if (data == NULL) {
     97             fprintf(stderr, "load_file: could not allocate %ld bytes\n", sz );
     98             sz = 0;
     99         } else {
    100             DWORD  out_bytes;
    101 
    102             if ( !ReadFile( file, data, sz, &out_bytes, NULL ) ||
    103                  out_bytes != sz )
    104             {
    105                 fprintf(stderr, "load_file: could not read %ld bytes from '%s'\n", sz, fn);
    106                 free(data);
    107                 data      = NULL;
    108                 sz = 0;
    109             }
    110         }
    111     }
    112     CloseHandle( file );
    113 
    114     *_sz = (unsigned) sz;
    115     return  data;
    116 }
    117