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