From 892ac1efc8dac5dfb0434c953c66d379533ea0f4 Mon Sep 17 00:00:00 2001
From: Weili Qian <qianweili@huawei.com>
Date: Sat, 23 Jul 2022 10:48:58 +0800
Subject: [PATCH 172/183] drv/hpre: move format check to is_hpre_bin_fmt()

Move the check format from crypto_bin_to_hpre_bin()
to is_hpre_bin_fmt(), all check are done by is_hpre_bin_fmt().

Signed-off-by: Weili Qian <qianweili@huawei.com>
---
 drv/hisi_hpre.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c
index 76529e8..c3c74a0 100644
--- a/drv/hisi_hpre.c
+++ b/drv/hisi_hpre.c
@@ -100,18 +100,24 @@ struct hisi_hpre_ctx {
 	struct wd_ctx_config_internal	config;
 };
 
-static bool is_hpre_bin_fmt(const char *data, int dsz, int bsz)
+static bool is_hpre_bin_fmt(char *dst, const char *src, int dsz, int bsz)
 {
-	const char *temp = data + dsz;
+	const char *temp = src + dsz;
 	int lens = bsz - dsz;
 	int i = 0;
 
+	if (!lens)
+		return true;
+
 	while (i < lens) {
-		if (temp[i] && !data[i])
-			return true;
+		if (temp[i] && !src[i])
+			break;
 		i++;
 	}
 
+	if (dst == src && i != lens)
+		return true;
+
 	return false;
 }
 
@@ -119,7 +125,6 @@ static int crypto_bin_to_hpre_bin(char *dst, const char *src,
 				  __u32 b_size, __u32 d_size, const char *p_name)
 {
 	int i = d_size - 1;
-	bool is_hpre_bin;
 	int j;
 
 	if (!dst || !src || b_size <= 0 || d_size <= 0) {
@@ -132,8 +137,7 @@ static int crypto_bin_to_hpre_bin(char *dst, const char *src,
 		return  -WD_EINVAL;
 	}
 
-	is_hpre_bin = is_hpre_bin_fmt(src, d_size, b_size);
-	if (b_size == d_size || (dst == src && is_hpre_bin))
+	if (is_hpre_bin_fmt(dst, src, d_size, b_size))
 		return WD_SUCCESS;
 
 	for (j = b_size - 1; j >= 0; j--, i--) {
-- 
2.27.0

