Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Daniel Boulby | 8942a1b | 2018-06-22 14:16:03 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 8 | #include <debug.h> |
| 9 | #include <platform.h> |
| 10 | #include <stdarg.h> |
| 11 | |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 12 | static void string_print(char **s, size_t n, size_t *chars_printed, |
| 13 | const char *str) |
| 14 | { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 15 | while (*str != '\0') { |
| 16 | if (*chars_printed < n) { |
| 17 | *(*s) = *str; |
| 18 | (*s)++; |
| 19 | } |
| 20 | |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 21 | (*chars_printed)++; |
| 22 | str++; |
| 23 | } |
| 24 | } |
| 25 | |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 26 | static void unsigned_dec_print(char **s, size_t n, size_t *chars_printed, |
| 27 | unsigned int unum) |
| 28 | { |
| 29 | /* Enough for a 32-bit unsigned decimal integer (4294967295). */ |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 30 | char num_buf[10]; |
| 31 | int i = 0; |
| 32 | unsigned int rem; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 33 | |
| 34 | do { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 35 | rem = unum % 10U; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 36 | num_buf[i++] = '0' + rem; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 37 | unum /= 10U; |
| 38 | } while (unum > 0U); |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 39 | |
| 40 | while (--i >= 0) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 41 | if (*chars_printed < n) { |
| 42 | *(*s) = num_buf[i]; |
| 43 | (*s)++; |
| 44 | } |
| 45 | |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 46 | (*chars_printed)++; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /******************************************************************* |
| 51 | * Reduced snprintf to be used for Trusted firmware. |
| 52 | * The following type specifiers are supported: |
| 53 | * |
| 54 | * %d or %i - signed decimal format |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 55 | * %s - string format |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 56 | * %u - unsigned decimal format |
| 57 | * |
| 58 | * The function panics on all other formats specifiers. |
| 59 | * |
| 60 | * It returns the number of characters that would be written if the |
| 61 | * buffer was big enough. If it returns a value lower than n, the |
| 62 | * whole string has been written. |
| 63 | *******************************************************************/ |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 64 | int snprintf(char *s, size_t n, const char *fmt, ...) |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 65 | { |
| 66 | va_list args; |
| 67 | int num; |
| 68 | unsigned int unum; |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 69 | char *str; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 70 | size_t chars_printed = 0U; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 71 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 72 | if (n == 0U) { |
| 73 | /* There isn't space for anything. */ |
| 74 | } else if (n == 1U) { |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 75 | /* Buffer is too small to actually write anything else. */ |
| 76 | *s = '\0'; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 77 | n = 0U; |
| 78 | } else { |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 79 | /* Reserve space for the terminator character. */ |
| 80 | n--; |
| 81 | } |
| 82 | |
| 83 | va_start(args, fmt); |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 84 | while (*fmt != '\0') { |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 85 | |
| 86 | if (*fmt == '%') { |
| 87 | fmt++; |
| 88 | /* Check the format specifier. */ |
| 89 | switch (*fmt) { |
| 90 | case 'i': |
| 91 | case 'd': |
| 92 | num = va_arg(args, int); |
| 93 | |
| 94 | if (num < 0) { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 95 | if (chars_printed < n) { |
| 96 | *s = '-'; |
| 97 | s++; |
| 98 | } |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 99 | chars_printed++; |
| 100 | |
| 101 | unum = (unsigned int)-num; |
| 102 | } else { |
| 103 | unum = (unsigned int)num; |
| 104 | } |
| 105 | |
| 106 | unsigned_dec_print(&s, n, &chars_printed, unum); |
| 107 | break; |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 108 | case 's': |
| 109 | str = va_arg(args, char *); |
| 110 | string_print(&s, n, &chars_printed, str); |
| 111 | break; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 112 | case 'u': |
| 113 | unum = va_arg(args, unsigned int); |
| 114 | unsigned_dec_print(&s, n, &chars_printed, unum); |
| 115 | break; |
| 116 | default: |
| 117 | /* Panic on any other format specifier. */ |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 118 | ERROR("snprintf: specifier with ASCII code '%d' not supported.", |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 119 | *fmt); |
| 120 | plat_panic_handler(); |
Daniel Boulby | 8942a1b | 2018-06-22 14:16:03 +0100 | [diff] [blame] | 121 | assert(0); /* Unreachable */ |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 122 | } |
| 123 | fmt++; |
| 124 | continue; |
| 125 | } |
| 126 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 127 | if (chars_printed < n) { |
| 128 | *s = *fmt; |
| 129 | s++; |
| 130 | } |
| 131 | |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 132 | fmt++; |
| 133 | chars_printed++; |
| 134 | } |
| 135 | |
| 136 | va_end(args); |
| 137 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 138 | if (n > 0U) |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 139 | *s = '\0'; |
| 140 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 141 | return (int)chars_printed; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 142 | } |