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