Reorganize SHA1 checksum code.
diff --git a/lib/bcc/CacheReader.cpp b/lib/bcc/CacheReader.cpp
index 5dc1f85..c017d36 100644
--- a/lib/bcc/CacheReader.cpp
+++ b/lib/bcc/CacheReader.cpp
@@ -302,18 +302,18 @@
   }
 
   vector<char const *> &strPool = mpResult->mStringPool;
-  map<string, pair<uint32_t, char const *> >::iterator dep;
+  map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
 
   dep = mDependencies.begin();
   for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
     string const &depName = dep->first;
-    char const *depSHA1 = dep->second.second;
     uint32_t depType = dep->second.first;
+    unsigned char const *depSHA1 = dep->second.second;
 
     OBCC_Dependency *depCached =&mpCachedDependTable->table[i];
     char const *depCachedName = strPool[depCached->res_name_strp_index];
-    char const *depCachedSHA1 = depCached->sha1;
     uint32_t depCachedType = depCached->res_type;
+    unsigned char const *depCachedSHA1 = depCached->sha1;
 
     if (depName != depCachedName) {
       LOGE("Cache dependency name mismatch:\n");
diff --git a/lib/bcc/CacheReader.h b/lib/bcc/CacheReader.h
index 5da24b3..cfd32e3 100644
--- a/lib/bcc/CacheReader.h
+++ b/lib/bcc/CacheReader.h
@@ -48,7 +48,8 @@
 
     llvm::OwningPtr<ScriptCached> mpResult;
 
-    std::map<std::string, std::pair<uint32_t, char const *> > mDependencies;
+    std::map<std::string,
+             std::pair<uint32_t, unsigned char const *> > mDependencies;
 
   public:
     CacheReader(Script *owner)
@@ -60,7 +61,7 @@
 
     void addDependency(OBCC_ResourceType resType,
                        std::string const &resName,
-                       char const *sha1) {
+                       unsigned char const *sha1) {
       mDependencies.insert(std::make_pair(resName,
                            std::make_pair((uint32_t)resType, sha1)));
     }
diff --git a/lib/bcc/Compiler.cpp b/lib/bcc/Compiler.cpp
index b4611d8..b0aaef0 100644
--- a/lib/bcc/Compiler.cpp
+++ b/lib/bcc/Compiler.cpp
@@ -67,6 +67,7 @@
 
 #include "ContextManager.h"
 #include "ScriptCompiled.h"
+#include "Sha1Helper.h"
 
 #include "llvm/ADT/StringRef.h"
 
@@ -105,7 +106,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <sha1.h>
+#include <string.h>
 
 #include <string>
 #include <vector>
@@ -236,6 +237,10 @@
      llvm::createFastRegisterAllocator :
      llvm::createLinearScanRegisterAllocator);
 
+  // Calculate the SHA1 checksum of libbcc and libRS.
+  calcFileSHA1(sha1LibBCC, pathLibBCC);
+  calcFileSHA1(sha1LibRS, pathLibRS);
+
   GlobalInitialized = true;
 }
 
@@ -287,11 +292,6 @@
                      size_t bitcodeSize,
                      const BCCchar *resName,
                      const BCCchar *cacheDir) {
-
-  // Compute the SHA1 hash of the input bitcode.  So that we can use the hash
-  // to decide rather to update the cache or not.
-  computeSourceSHA1(bitcode, bitcodeSize);
-
   llvm::OwningPtr<llvm::MemoryBuffer> MEM;
 
   if (bitcode == NULL || bitcodeSize <= 0)
@@ -684,16 +684,4 @@
   // llvm::llvm_shutdown();
 }
 
-
-void Compiler::computeSourceSHA1(char const *bitcode, size_t bitcodeSize) {
-  SHA1_CTX hashContext;
-
-  SHA1Init(&hashContext);
-  SHA1Update(&hashContext,
-             reinterpret_cast<const unsigned char *>(bitcode),
-             static_cast<unsigned long>(bitcodeSize));
-  SHA1Final(mSourceSHA1, &hashContext);
-
-}
-
 }  // namespace bcc
diff --git a/lib/bcc/Compiler.h b/lib/bcc/Compiler.h
index 8c83405..1f536b8 100644
--- a/lib/bcc/Compiler.h
+++ b/lib/bcc/Compiler.h
@@ -143,15 +143,6 @@
     ~Compiler();
 
   private:
-    void computeSourceSHA1(char const *bitcode, size_t size);
-
-
-    char *genCacheFileName(const char *cacheDir,
-                           const char *fileName,
-                           const char *subFileName);
-
-
-  private:
 
     bool hasError() const {
       return !mError.empty();
diff --git a/lib/bcc/Script.h b/lib/bcc/Script.h
index c794c7b..8f16de5 100644
--- a/lib/bcc/Script.h
+++ b/lib/bcc/Script.h
@@ -53,6 +53,7 @@
     // ReadBC
     char const *sourceBC;
     char const *sourceResName;
+    unsigned char sourceSHA1[20];
     size_t sourceSize;
 
     // ReadModule
diff --git a/lib/bcc/Sha1Helper.cpp b/lib/bcc/Sha1Helper.cpp
new file mode 100644
index 0000000..b8179e2
--- /dev/null
+++ b/lib/bcc/Sha1Helper.cpp
@@ -0,0 +1,84 @@
+/*
+ * copyright 2010, the android open source project
+ *
+ * licensed under the apache license, version 2.0 (the "license");
+ * you may not use this file except in compliance with the license.
+ * you may obtain a copy of the license at
+ *
+ *     http://www.apache.org/licenses/license-2.0
+ *
+ * unless required by applicable law or agreed to in writing, software
+ * distributed under the license is distributed on an "as is" basis,
+ * without warranties or conditions of any kind, either express or implied.
+ * see the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+#define LOG_TAG "bcc"
+#include <cutils/log.h>
+
+#include "Sha1Helper.h"
+
+#include "FileHandle.h"
+
+#include <string.h>
+
+#include <sha1.h>
+
+namespace bcc {
+
+
+unsigned char sha1LibBCC[20];
+unsigned char sha1LibRS[20];
+
+
+char const *pathLibBCC = "/system/lib/libbcc.so";
+char const *pathLibRS = "/system/lib/libRS.so";
+
+
+void calcSHA1(unsigned char *result, char const *data, size_t size) {
+  SHA1_CTX hashContext;
+
+  SHA1Init(&hashContext);
+  SHA1Update(&hashContext,
+             reinterpret_cast<const unsigned char *>(data),
+             static_cast<unsigned long>(size));
+
+  SHA1Final(result, &hashContext);
+}
+
+
+void calcFileSHA1(unsigned char *result, char const *filename) {
+  FileHandle file;
+
+  if (file.open(filename, OpenMode::Read) < 0) {
+    LOGE("Unable to calculate the sha1 checksum of %s\n", filename);
+    memset(result, '\0', 20);
+    return;
+  }
+
+  SHA1_CTX hashContext;
+  SHA1Init(&hashContext);
+
+  char buf[256];
+  while (true) {
+    ssize_t nread = file.read(buf, sizeof(buf));
+    
+    if (nread < 0) {
+      break;
+    }
+
+    SHA1Update(&hashContext,
+               reinterpret_cast<unsigned char *>(buf),
+               static_cast<unsigned long>(nread));
+
+    if ((size_t)nread < sizeof(buf)) {
+      // finished.
+      break;
+    }
+  }
+
+  SHA1Final(result, &hashContext);
+}
+
+} // namespace bcc
diff --git a/lib/bcc/Sha1Helper.h b/lib/bcc/Sha1Helper.h
new file mode 100644
index 0000000..6ebf967
--- /dev/null
+++ b/lib/bcc/Sha1Helper.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2010, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BCC_SHA1HELPER_H
+#define BCC_SHA1HELPER_H
+
+#include <stdint.h>
+
+namespace bcc {
+  extern unsigned char sha1LibBCC[20];
+  extern unsigned char sha1LibRS[20];
+
+  extern char const *pathLibBCC;
+  extern char const *pathLibRS;
+
+  void calcSHA1(unsigned char *result, char const *data, size_t size);
+
+  void calcFileSHA1(unsigned char *result, char const *filename);
+}
+
+#endif // BCC_SHA1HELPER_H