libtar: dynamically choose fscrypt policy [1/2]

Change-Id: I0e08365cc1500bc67bc8cf9cc07bafc607333d49
Signed-off-by: Mohd Faraz <androiabledroid@gmail.com>
(cherry picked from commit 97b849918cf578eec5419de6a8061f11164527af)
diff --git a/libtar/block.c b/libtar/block.c
index 91342c2..e94c24c 100755
--- a/libtar/block.c
+++ b/libtar/block.c
@@ -354,11 +354,7 @@
 #ifdef USE_FSCRYPT
 			start = strstr(buf, FSCRYPT_TAG);
 			if (start && start+FSCRYPT_TAG_LEN < buf+len) {
-#ifdef USE_FSCRYPT_POLICY_V1
-				t->th_buf.fep = (struct fscrypt_policy_v1*)malloc(sizeof(struct fscrypt_policy_v1));
-#else
-				t->th_buf.fep = (struct fscrypt_policy_v2*)malloc(sizeof(struct fscrypt_policy_v2));
-#endif
+				t->th_buf.fep = (fscrypt_policy*)malloc(sizeof(fscrypt_policy));
 				if (!t->th_buf.fep) {
 					LOG("malloc failed for fscrypt policy\n");
 					return -1;
@@ -366,29 +362,14 @@
 				start += FSCRYPT_TAG_LEN;
 				if (*start == '0') {
 					start++;
-#ifdef USE_FSCRYPT_POLICY_V1
-					char *newline_check = start + sizeof(struct fscrypt_policy_v1);
-#else
-					char *newline_check = start + sizeof(struct fscrypt_policy_v2);
-#endif
-					if (*newline_check != '\n')
-						LOG("did not find newline char in expected location, continuing anyway...\n");
-#ifdef USE_FSCRYPT_POLICY_V1
-					memcpy(t->th_buf.fep, start, sizeof(struct fscrypt_policy_v1));
-#else
-					memcpy(t->th_buf.fep, start, sizeof(struct fscrypt_policy_v2));
-#endif
+					memcpy(get_policy(t->th_buf.fep), start, fscrypt_policy_size(t->th_buf.fep));
 #ifdef DEBUG
-					LOG("    th_read(): FSCrypt policy detected: %i %i %i %i %s\n",
-						(int)t->th_buf.fep->version,
-						(int)t->th_buf.fep->contents_encryption_mode,
-						(int)t->th_buf.fep->filenames_encryption_mode,
-						(int)t->th_buf.fep->flags,
-#ifdef USE_FSCRYPT_POLICY_V1
-						t->th_buf.fep->master_key_descriptor);
-#else
-						t->th_buf.fep->master_key_identifier);
-#endif
+					uint8_t version;
+					char content[50];
+					memcpy(&version, start, sizeof(version));
+					get_policy_content(t->th_buf.fep, content);
+					LOG("version: %u\n", version);
+					LOG("    th_read(): FSCrypt policy detected: %s\n", content);
 #endif
 				}
 				else {
@@ -597,23 +578,15 @@
 #ifdef USE_FSCRYPT
 	if((t->options & TAR_STORE_FSCRYPT_POL) && t->th_buf.fep != NULL)
 	{
-#ifdef DEBUG
-#ifdef USE_FSCRYPT_POLICY_V1
-		LOG("th_write(): using fscrypt_policy %s\n",
-		       t->th_buf.fep->master_key_descriptor);
-#else
-		LOG("th_write(): using fscrypt_policy %s\n",
-		       t->th_buf.fep->master_key_identifier);
-#endif
-#endif
 		/* setup size - EXT header has format "*size of this whole tag as ascii numbers* *space* *version code* *content* *newline* */
 		//                                                       size   newline
-#ifdef USE_FSCRYPT_POLICY_V1
-		sz = FSCRYPT_TAG_LEN + sizeof(struct fscrypt_policy_v1) + 1 + 3  +    1;
-#else
-		sz = FSCRYPT_TAG_LEN + sizeof(struct fscrypt_policy_v2) + 1 + 3  +    1;
+		uint8_t size, *descriptor;
+		size = fscrypt_policy_size(t->th_buf.fep);
+		descriptor = get_policy_descriptor(t->th_buf.fep);
+#ifdef DEBUG
+		LOG("th_write(): using fscrypt_policy %s\n", descriptor);
 #endif
-
+		sz = FSCRYPT_TAG_LEN + size + 1 + 3  +    1;
 		if(sz >= 100) // another ascci digit for size
 			++sz;
 
@@ -628,11 +601,7 @@
 			total_sz += sz;
 
 		snprintf(ptr, T_BLOCKSIZE, "%d "FSCRYPT_TAG"0", (int)sz);
-#ifdef USE_FSCRYPT_POLICY_V1
-		memcpy(ptr + sz - sizeof(struct fscrypt_policy_v1) - 1, t->th_buf.fep, sizeof(struct fscrypt_policy_v1));
-#else
-		memcpy(ptr + sz - sizeof(struct fscrypt_policy_v2) - 1, t->th_buf.fep, sizeof(struct fscrypt_policy_v2));
-#endif
+		memcpy(ptr + sz - size - 1, get_policy(t->th_buf.fep), size);
 		char *nlptr = ptr + sz - 1;
 		*nlptr = '\n';
 		ptr += sz;