libgptutils: Replace libsparse.so with libz.so

This commit replaces sparse_crc32() with crc32() from libz.so because
libsparse.so won't be vendor available.

Bug: 38303202
Test: Sailfish builds, boots, and can boot into recovery mode with
adb reboot recovery

Change-Id: I13c4d8f38bce09f7cd944949231a54e10855687b
(cherry picked from commit c90dcfe0a0ca14d5c546b951bae236b42c10e344)
diff --git a/recovery/gpt-utils/Android.mk b/recovery/gpt-utils/Android.mk
index 8ef53f9..1d801b5 100644
--- a/recovery/gpt-utils/Android.mk
+++ b/recovery/gpt-utils/Android.mk
@@ -17,25 +17,23 @@
 LOCAL_PATH := $(call my-dir)
 
 include $(CLEAR_VARS)
-LOCAL_C_INCLUDES := system/core/libsparse
 LOCAL_SRC_FILES := gpt-utils.cpp
 ifeq ($(TARGET_COMPILE_WITH_MSM_KERNEL),true)
 LOCAL_C_INCLUDES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
 LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
 endif
-LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_SHARED_LIBRARIES := liblog libz
 LOCAL_MODULE := libgptutils
 LOCAL_MODULE_OWNER := qti
 include $(BUILD_STATIC_LIBRARY)
 
 include $(CLEAR_VARS)
-LOCAL_C_INCLUDES := system/core/libsparse
 LOCAL_SRC_FILES := gpt-utils.cpp
 ifeq ($(TARGET_COMPILE_WITH_MSM_KERNEL),true)
 LOCAL_C_INCLUDES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
 LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
 endif
-LOCAL_SHARED_LIBRARIES += liblog libsparse libcutils
+LOCAL_SHARED_LIBRARIES += liblog libcutils libz
 LOCAL_MODULE := libgptutils
 LOCAL_MODULE_OWNER := qti
 LOCAL_PROPRIETARY_MODULE := true
diff --git a/recovery/gpt-utils/gpt-utils.cpp b/recovery/gpt-utils/gpt-utils.cpp
index e3c23f7..e4651c4 100644
--- a/recovery/gpt-utils/gpt-utils.cpp
+++ b/recovery/gpt-utils/gpt-utils.cpp
@@ -54,14 +54,8 @@
 #include <cutils/log.h>
 #include <cutils/properties.h>
 #include "gpt-utils.h"
-#ifdef __cplusplus
-extern "C" {
-#endif
-#include "sparse_crc32.h"
-#ifdef __cplusplus
-}
-#endif
 #include <endian.h>
+#include <zlib.h>
 
 
 /******************************************************************************
@@ -344,7 +338,7 @@
     if (r)
         goto EXIT;
 
-    crc = sparse_crc32(0, pentries, pentries_array_size);
+    crc = crc32(0, pentries, pentries_array_size);
     if (GET_4_BYTES(gpt_header + PARTITION_CRC_OFFSET) != crc) {
         fprintf(stderr, "Primary GPT partition entries array CRC invalid\n");
         r = -1;
@@ -367,12 +361,12 @@
             goto EXIT;
     }
 
-    crc = sparse_crc32(0, pentries, pentries_array_size);
+    crc = crc32(0, pentries, pentries_array_size);
     PUT_4_BYTES(gpt_header + PARTITION_CRC_OFFSET, crc);
 
     /* header CRC is calculated with this field cleared */
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
-    crc = sparse_crc32(0, gpt_header, gpt_header_size);
+    crc = crc32(0, gpt_header, gpt_header_size);
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, crc);
 
     /* Write the modified GPT header back to block dev */
@@ -444,7 +438,7 @@
     crc = GET_4_BYTES(gpt_header + HEADER_CRC_OFFSET);
     /* header CRC is calculated with this field cleared */
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
-    if (sparse_crc32(0, gpt_header, gpt_header_size) != crc)
+    if (crc32(0, gpt_header, gpt_header_size) != crc)
         *state = GPT_BAD_CRC;
     free(gpt_header);
     return 0;
@@ -513,7 +507,7 @@
 
     /* header CRC is calculated with this field cleared */
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
-    crc = sparse_crc32(0, gpt_header, gpt_header_size);
+    crc = crc32(0, gpt_header, gpt_header_size);
     PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, crc);
 
     if (blk_rw(fd, 1, gpt_header_offset, gpt_header, blk_size)) {
@@ -1397,13 +1391,13 @@
                 goto error;
         }
         gpt_header_size = GET_4_BYTES(disk->hdr + HEADER_SIZE_OFFSET);
-        disk->hdr_crc = sparse_crc32(0, disk->hdr, gpt_header_size);
+        disk->hdr_crc = crc32(0, disk->hdr, gpt_header_size);
         disk->hdr_bak = gpt_get_header(dev, PRIMARY_GPT);
         if (!disk->hdr_bak) {
                 ALOGE("%s: Failed to get backup header", __func__);
                 goto error;
         }
-        disk->hdr_bak_crc = sparse_crc32(0, disk->hdr_bak, gpt_header_size);
+        disk->hdr_bak_crc = crc32(0, disk->hdr_bak, gpt_header_size);
 
         //Descriptor for the block device. We will use this for further
         //modifications to the partition table
@@ -1483,11 +1477,11 @@
                 goto error;
         }
         //Recalculate the CRC of the primary partiton array
-        disk->pentry_arr_crc = sparse_crc32(0,
+        disk->pentry_arr_crc = crc32(0,
                         disk->pentry_arr,
                         disk->pentry_arr_size);
         //Recalculate the CRC of the backup partition array
-        disk->pentry_arr_bak_crc = sparse_crc32(0,
+        disk->pentry_arr_bak_crc = crc32(0,
                         disk->pentry_arr_bak,
                         disk->pentry_arr_size);
         //Update the partition CRC value in the primary GPT header
@@ -1500,8 +1494,8 @@
         //Header CRC is calculated with its own CRC field set to 0
         PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, 0);
         PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, 0);
-        disk->hdr_crc = sparse_crc32(0, disk->hdr, gpt_header_size);
-        disk->hdr_bak_crc = sparse_crc32(0, disk->hdr_bak, gpt_header_size);
+        disk->hdr_crc = crc32(0, disk->hdr, gpt_header_size);
+        disk->hdr_bak_crc = crc32(0, disk->hdr_bak, gpt_header_size);
         PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, disk->hdr_crc);
         PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, disk->hdr_bak_crc);
         return 0;