Blame SOURCES/0361-libtasn1-changes-for-grub-compatibility.patch

3efed6
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
3efed6
From: Daniel Axtens <dja@axtens.net>
3efed6
Date: Fri, 1 May 2020 20:44:29 +1000
3efed6
Subject: [PATCH] libtasn1: changes for grub compatibility
3efed6
3efed6
Do a few things to make libtasn1 compile as part of grub:
3efed6
3efed6
 - replace strcat. grub removed strcat so replace it with the appropriate
3efed6
   calls to memcpy and strlen.
3efed6
3efed6
 - replace c_isdigit with grub_isdigit (and don't import c-ctype from
3efed6
   gnulib) grub_isdigit provides the same functionality as c_isdigit: it
3efed6
   determines if the input is an ASCII digit without regard for locale.
3efed6
3efed6
 - replace GL_ATTRIBUTE_PURE with __attribute__((pure)) which been
3efed6
   supported since gcc-2.96. This avoids messing around with gnulib.
3efed6
3efed6
 - adjust libtasn1.h: drop the ASN1_API logic, it's not needed for our
3efed6
   modules. Unconditionally support const and pure attributes and adjust
3efed6
   header paths.
3efed6
3efed6
 - adjust header paths to "grub/libtasn1.h".
3efed6
3efed6
 - replace a 64 bit division with a call to grub_divmod64, preventing
3efed6
   creation of __udivdi3 calls on 32 bit platforms.
3efed6
3efed6
Signed-off-by: Daniel Axtens <dja@axtens.net>
3efed6
---
3efed6
 grub-core/lib/libtasn1/lib/decoding.c   | 11 ++++++-----
3efed6
 grub-core/lib/libtasn1/lib/element.c    |  3 ++-
3efed6
 grub-core/lib/libtasn1/lib/gstr.c       |  4 ++--
3efed6
 grub-core/lib/libtasn1/lib/parser_aux.c |  7 ++++---
3efed6
 grub-core/lib/libtasn1/lib/int.h        |  4 ++--
3efed6
 include/grub/libtasn1.h                 | 26 ++++++--------------------
3efed6
 6 files changed, 22 insertions(+), 33 deletions(-)
3efed6
3efed6
diff --git a/grub-core/lib/libtasn1/lib/decoding.c b/grub-core/lib/libtasn1/lib/decoding.c
3efed6
index 42f9a92b5d4..7856858b272 100644
3efed6
--- a/grub-core/lib/libtasn1/lib/decoding.c
3efed6
+++ b/grub-core/lib/libtasn1/lib/decoding.c
3efed6
@@ -32,7 +32,8 @@
3efed6
 #include <element.h>
3efed6
 #include <limits.h>
3efed6
 #include <intprops.h>
3efed6
-#include <c-ctype.h>
3efed6
+
3efed6
+#define c_isdigit grub_isdigit
3efed6
 
3efed6
 #ifdef DEBUG
3efed6
 # define warn() fprintf(stderr, "%s: %d\n", __func__, __LINE__)
3efed6
@@ -2008,8 +2009,8 @@ asn1_expand_octet_string (asn1_node_const definitions, asn1_node * element,
3efed6
 	  (p2->type & CONST_ASSIGN))
3efed6
 	{
3efed6
 	  strcpy (name, definitions->name);
3efed6
-	  strcat (name, ".");
3efed6
-	  strcat (name, p2->name);
3efed6
+	  memcpy (name + strlen(name), ".", sizeof(" . "));
3efed6
+	  memcpy (name + strlen(name), p2->name, strlen(p2->name) + 1);
3efed6
 
3efed6
 	  len = sizeof (value);
3efed6
 	  result = asn1_read_value (definitions, name, value, &len;;
3efed6
@@ -2026,8 +2027,8 @@ asn1_expand_octet_string (asn1_node_const definitions, asn1_node * element,
3efed6
 	      if (p2)
3efed6
 		{
3efed6
 		  strcpy (name, definitions->name);
3efed6
-		  strcat (name, ".");
3efed6
-		  strcat (name, p2->name);
3efed6
+		  memcpy (name + strlen(name), ".", sizeof(" . "));
3efed6
+		  memcpy (name + strlen(name), p2->name, strlen(p2->name) + 1);
3efed6
 
3efed6
 		  result = asn1_create_element (definitions, name, &aux;;
3efed6
 		  if (result == ASN1_SUCCESS)
3efed6
diff --git a/grub-core/lib/libtasn1/lib/element.c b/grub-core/lib/libtasn1/lib/element.c
3efed6
index 539008d8e94..ed761ff56bd 100644
3efed6
--- a/grub-core/lib/libtasn1/lib/element.c
3efed6
+++ b/grub-core/lib/libtasn1/lib/element.c
3efed6
@@ -30,9 +30,10 @@
3efed6
 #include "parser_aux.h"
3efed6
 #include <gstr.h>
3efed6
 #include "structure.h"
3efed6
-#include "c-ctype.h"
3efed6
 #include "element.h"
3efed6
 
3efed6
+#define c_isdigit grub_isdigit
3efed6
+
3efed6
 void
3efed6
 _asn1_hierarchical_name (asn1_node_const node, char *name, int name_size)
3efed6
 {
3efed6
diff --git a/grub-core/lib/libtasn1/lib/gstr.c b/grub-core/lib/libtasn1/lib/gstr.c
3efed6
index e91a3a151c0..e33875c2c7c 100644
3efed6
--- a/grub-core/lib/libtasn1/lib/gstr.c
3efed6
+++ b/grub-core/lib/libtasn1/lib/gstr.c
3efed6
@@ -36,13 +36,13 @@ _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
3efed6
 
3efed6
   if (dest_tot_size - dest_size > str_size)
3efed6
     {
3efed6
-      strcat (dest, src);
3efed6
+      memcpy (dest + dest_size, src, str_size + 1);
3efed6
     }
3efed6
   else
3efed6
     {
3efed6
       if (dest_tot_size - dest_size > 0)
3efed6
 	{
3efed6
-	  strncat (dest, src, (dest_tot_size - dest_size) - 1);
3efed6
+	  memcpy (dest + dest_size, src, (dest_tot_size - dest_size) - 1);
3efed6
 	  dest[dest_tot_size - 1] = 0;
3efed6
 	}
3efed6
     }
3efed6
diff --git a/grub-core/lib/libtasn1/lib/parser_aux.c b/grub-core/lib/libtasn1/lib/parser_aux.c
3efed6
index d5dbbf8765d..89c9be69dc2 100644
3efed6
--- a/grub-core/lib/libtasn1/lib/parser_aux.c
3efed6
+++ b/grub-core/lib/libtasn1/lib/parser_aux.c
3efed6
@@ -26,7 +26,8 @@
3efed6
 #include "gstr.h"
3efed6
 #include "structure.h"
3efed6
 #include "element.h"
3efed6
-#include "c-ctype.h"
3efed6
+
3efed6
+#define c_isdigit grub_isdigit
3efed6
 
3efed6
 char _asn1_identifierMissing[ASN1_MAX_NAME_SIZE + 1];	/* identifier name not found */
3efed6
 
3efed6
@@ -40,7 +41,7 @@ char _asn1_identifierMissing[ASN1_MAX_NAME_SIZE + 1];	/* identifier name not fou
3efed6
 #ifdef __clang__
3efed6
 __attribute__((no_sanitize("integer")))
3efed6
 #endif
3efed6
-_GL_ATTRIBUTE_PURE
3efed6
+__attribute__((__pure__))
3efed6
 static unsigned int
3efed6
 _asn1_hash_name (const char *x)
3efed6
 {
3efed6
@@ -634,7 +635,7 @@ _asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE])
3efed6
   count = 0;
3efed6
   do
3efed6
     {
3efed6
-      d = val / 10;
3efed6
+      d = grub_divmod64(val, 10, NULL);
3efed6
       r = val - d * 10;
3efed6
       temp[start + count] = '0' + (char) r;
3efed6
       count++;
3efed6
diff --git a/grub-core/lib/libtasn1/lib/int.h b/grub-core/lib/libtasn1/lib/int.h
3efed6
index ea1625786c1..4a568efee9c 100644
3efed6
--- a/grub-core/lib/libtasn1/lib/int.h
3efed6
+++ b/grub-core/lib/libtasn1/lib/int.h
3efed6
@@ -35,7 +35,7 @@
3efed6
 #include <sys/types.h>
3efed6
 #endif
3efed6
 
3efed6
-#include <libtasn1.h>
3efed6
+#include "grub/libtasn1.h"
3efed6
 
3efed6
 #define ASN1_SMALL_VALUE_SIZE 16
3efed6
 
3efed6
@@ -115,7 +115,7 @@ extern const tag_and_class_st _asn1_tags[];
3efed6
 #define _asn1_strtoul(n,e,b) strtoul((const char *) n, e, b)
3efed6
 #define _asn1_strcmp(a,b) strcmp((const char *)a, (const char *)b)
3efed6
 #define _asn1_strcpy(a,b) strcpy((char *)a, (const char *)b)
3efed6
-#define _asn1_strcat(a,b) strcat((char *)a, (const char *)b)
3efed6
+#define _asn1_strcat(a,b) memcpy((char *)a + strlen((const char *)a), (const char *)b, strlen((const char *)b) + 1)
3efed6
 
3efed6
 #if SIZEOF_UNSIGNED_LONG_INT == 8
3efed6
 # define _asn1_strtou64(n,e,b) strtoul((const char *) n, e, b)
3efed6
diff --git a/include/grub/libtasn1.h b/include/grub/libtasn1.h
3efed6
index 785eda2ae3f..28dbf16c4e0 100644
3efed6
--- a/include/grub/libtasn1.h
3efed6
+++ b/include/grub/libtasn1.h
3efed6
@@ -38,29 +38,15 @@
3efed6
 #ifndef LIBTASN1_H
3efed6
 #define LIBTASN1_H
3efed6
 
3efed6
-#ifndef ASN1_API
3efed6
-#if defined ASN1_BUILDING && defined HAVE_VISIBILITY && HAVE_VISIBILITY
3efed6
-#define ASN1_API __attribute__((__visibility__("default")))
3efed6
-#elif defined ASN1_BUILDING && defined _MSC_VER && ! defined ASN1_STATIC
3efed6
-#define ASN1_API __declspec(dllexport)
3efed6
-#elif defined _MSC_VER && ! defined ASN1_STATIC
3efed6
-#define ASN1_API __declspec(dllimport)
3efed6
-#else
3efed6
+/* grub: ASN1_API is not used */
3efed6
 #define ASN1_API
3efed6
-#endif
3efed6
-#endif
3efed6
 
3efed6
-#ifdef __GNUC__
3efed6
-# define __LIBTASN1_CONST__  __attribute__((const))
3efed6
-# define __LIBTASN1_PURE__  __attribute__((pure))
3efed6
-#else
3efed6
-# define __LIBTASN1_CONST__
3efed6
-# define __LIBTASN1_PURE__
3efed6
-#endif
3efed6
+/* grub: all our supported compilers support these attributes */
3efed6
+#define __LIBTASN1_CONST__  __attribute__((const))
3efed6
+#define __LIBTASN1_PURE__  __attribute__((pure))
3efed6
 
3efed6
-#include <sys/types.h>
3efed6
-#include <time.h>
3efed6
-#include <stdio.h>		/* for FILE* */
3efed6
+#include <grub/types.h>
3efed6
+#include <grub/time.h>
3efed6
 
3efed6
 #ifdef __cplusplus
3efed6
 extern "C"