Home | History | Annotate | Download | only in ExecutionEngine
      1 /*
      2  * copyright 2010, the android open source project
      3  *
      4  * licensed under the apache license, version 2.0 (the "license");
      5  * you may not use this file except in compliance with the license.
      6  * you may obtain a copy of the license at
      7  *
      8  *     http://www.apache.org/licenses/license-2.0
      9  *
     10  * unless required by applicable law or agreed to in writing, software
     11  * distributed under the license is distributed on an "as is" basis,
     12  * without warranties or conditions of any kind, either express or implied.
     13  * see the license for the specific language governing permissions and
     14  * limitations under the license.
     15  */
     16 
     17 #include "Sha1Helper.h"
     18 
     19 #include "Config.h"
     20 
     21 #include "DebugHelper.h"
     22 #include "FileHandle.h"
     23 
     24 #include <string.h>
     25 
     26 #include <utils/StopWatch.h>
     27 
     28 #include <sha1.h>
     29 
     30 namespace bcc {
     31 
     32 unsigned char sha1LibBCC_SHA1[20];
     33 char const *pathLibBCC_SHA1 = "/system/lib/libbcc.so.sha1";
     34 
     35 unsigned char sha1LibRS[20];
     36 char const *pathLibRS = "/system/lib/libRS.so";
     37 
     38 void calcSHA1(unsigned char *result, char const *data, size_t size) {
     39   SHA1_CTX hashContext;
     40 
     41   SHA1Init(&hashContext);
     42   SHA1Update(&hashContext,
     43              reinterpret_cast<const unsigned char *>(data),
     44              static_cast<unsigned long>(size));
     45 
     46   SHA1Final(result, &hashContext);
     47 }
     48 
     49 
     50 void calcFileSHA1(unsigned char *result, char const *filename) {
     51 #if defined(__arm__)
     52   android::StopWatch calcFileSHA1Timer("calcFileSHA1 time");
     53 #endif
     54 
     55   FileHandle file;
     56 
     57   if (file.open(filename, OpenMode::Read) < 0) {
     58     LOGE("Unable to calculate the sha1 checksum of %s\n", filename);
     59     memset(result, '\0', 20);
     60     return;
     61   }
     62 
     63   SHA1_CTX hashContext;
     64   SHA1Init(&hashContext);
     65 
     66   char buf[256];
     67   while (true) {
     68     ssize_t nread = file.read(buf, sizeof(buf));
     69 
     70     if (nread < 0) {
     71       break;
     72     }
     73 
     74     SHA1Update(&hashContext,
     75                reinterpret_cast<unsigned char *>(buf),
     76                static_cast<unsigned long>(nread));
     77 
     78     if ((size_t)nread < sizeof(buf)) {
     79       // finished.
     80       break;
     81     }
     82   }
     83 
     84   SHA1Final(result, &hashContext);
     85 }
     86 
     87 void readSHA1(unsigned char *result, int result_size, char const *filename) {
     88   FileHandle file;
     89   if (file.open(filename, OpenMode::Read) < 0) {
     90     LOGE("Unable to read binary sha1 file %s\n", filename);
     91     memset(result, '\0', result_size);
     92     return;
     93   }
     94   file.read((char *)result, result_size);
     95 }
     96 
     97 } // namespace bcc
     98