Dedup checksum calculation routines

I introduced a separate routine to cacluate checksum for ScriptGroup
in my previous CL, in addition to the one we use for regular scripts.
This CL removes the new one and uses the old one.

While I am on it, I made some other minor changes, e.g., changing
mBuildChecksum in RsdCpuScriptIml from char* to uint32_t, and a few
other minor cleanups in ScriptGroup2 implementation.

Change-Id: I168fdbb4e7bd14f1549a687e7b0d0ca6dd4da866
diff --git a/cpu_ref/rsCpuExecutable.cpp b/cpu_ref/rsCpuExecutable.cpp
index 98f9ef8..f45e8f0 100644
--- a/cpu_ref/rsCpuExecutable.cpp
+++ b/cpu_ref/rsCpuExecutable.cpp
@@ -281,7 +281,7 @@
 }
 
 ScriptExecutable* ScriptExecutable::createFromSharedObject(
-    Context* RSContext, void* sharedObj) {
+    Context* RSContext, void* sharedObj, uint32_t expectedChecksum) {
     char line[MAXLINE];
 
     size_t varCount = 0;
@@ -299,7 +299,7 @@
     uint32_t* forEachSignatures = nullptr;
     const char ** pragmaKeys = nullptr;
     const char ** pragmaValues = nullptr;
-    char *checksum = nullptr;
+    uint32_t checksum = 0;
 
     const char *rsInfo = (const char *) dlsym(sharedObj, ".rs.info");
 
@@ -519,25 +519,21 @@
     }
 
     if (strgets(line, MAXLINE, &rsInfo) != nullptr) {
-        if (strncmp(line, CHECKSUM_STR, strlen(CHECKSUM_STR)) != 0) {
+        if (sscanf(line, CHECKSUM_STR "%08x", &checksum) != 1) {
             ALOGE("Invalid checksum flag!: %s", line);
             goto error;
         }
-
-        // consume trailing newline character
-        char *c = strrchr(line, '\n');
-        if (c) {
-            *c = '\0';
-        }
-
-        char *checksumStart = &line[strlen(CHECKSUM_STR)];
-        checksum = new char[strlen(checksumStart) + 1];
-        strcpy(checksum, checksumStart);
     } else {
         ALOGE("Missing checksum in shared obj file");
         goto error;
     }
 
+    if (expectedChecksum != 0 && checksum != expectedChecksum) {
+        ALOGE("Found invalid checksum.  Expected %08x, got %08x\n",
+              expectedChecksum, checksum);
+        goto error;
+    }
+
 #endif  // RS_COMPATIBILITY_LIB
 
     return new ScriptExecutable(
@@ -550,7 +546,6 @@
 error:
 
 #ifndef RS_COMPATIBILITY_LIB
-    delete[] checksum;
 
     for (size_t idx = 0; idx < pragmaCount; ++idx) {
         delete [] pragmaKeys[idx];