Merge changes I2add6b4b,I9b296372,I7af2f1d1 into integration

* changes:
  libc/snprintf: use macro to reduce duplicated code
  libc/snprintf: add support to print "%" character
  libc/printf: add support to print "%" character
diff --git a/lib/libc/printf.c b/lib/libc/printf.c
index 2715a72..45e153e 100644
--- a/lib/libc/printf.c
+++ b/lib/libc/printf.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2014-2021, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -108,6 +108,9 @@
 			/* Check the format specifier */
 loop:
 			switch (*fmt) {
+			case '%':
+				(void)putchar('%');
+				break;
 			case 'i': /* Fall through to next one */
 			case 'd':
 				num = get_num_va_args(args, l_count);
diff --git a/lib/libc/snprintf.c b/lib/libc/snprintf.c
index 6e80d8c..3b175ed 100644
--- a/lib/libc/snprintf.c
+++ b/lib/libc/snprintf.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -10,16 +10,20 @@
 #include <common/debug.h>
 #include <plat/common/platform.h>
 
+#define CHECK_AND_PUT_CHAR(buf, size, chars_printed, ch)	\
+	do {						\
+		if ((chars_printed) < (size)) {		\
+			*(buf) = (ch);			\
+			(buf)++;			\
+		}					\
+		(chars_printed)++;			\
+	} while (false)
+
 static void string_print(char **s, size_t n, size_t *chars_printed,
 			 const char *str)
 {
 	while (*str != '\0') {
-		if (*chars_printed < n) {
-			*(*s) = *str;
-			(*s)++;
-		}
-
-		(*chars_printed)++;
+		CHECK_AND_PUT_CHAR(*s, n, *chars_printed, *str);
 		str++;
 	}
 }
@@ -130,6 +134,9 @@
 			/* Check the format specifier. */
 loop:
 			switch (*fmt) {
+			case '%':
+				CHECK_AND_PUT_CHAR(s, n, chars_printed, '%');
+				break;
 			case '0':
 			case '1':
 			case '2':
@@ -158,12 +165,8 @@
 				num = va_arg(args, int);
 
 				if (num < 0) {
-					if (chars_printed < n) {
-						*s = '-';
-						s++;
-					}
-					chars_printed++;
-
+					CHECK_AND_PUT_CHAR(s, n, chars_printed,
+						'-');
 					unum = (unsigned int)-num;
 				} else {
 					unum = (unsigned int)num;
@@ -210,13 +213,9 @@
 			continue;
 		}
 
-		if (chars_printed < n) {
-			*s = *fmt;
-			s++;
-		}
+		CHECK_AND_PUT_CHAR(s, n, chars_printed, *fmt);
 
 		fmt++;
-		chars_printed++;
 	}
 
 	if (n > 0U) {