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