Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 1 | /* |
Maksims Svecovs | bed5521 | 2023-05-09 12:03:31 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2023, 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 <stdarg.h> |
Claus Pedersen | 785e66c | 2022-09-12 22:42:58 +0000 | [diff] [blame] | 9 | #include <stdbool.h> |
| 10 | #include <stddef.h> |
Andre Przywara | 94faff8 | 2022-01-27 17:47:55 +0000 | [diff] [blame] | 11 | #include <stdint.h> |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 12 | |
kadabi | 76c3f5b | 2022-02-28 14:21:09 -0800 | [diff] [blame] | 13 | #define get_num_va_args(_args, _lcount) \ |
| 14 | (((_lcount) > 1) ? va_arg(_args, long long int) : \ |
| 15 | (((_lcount) == 1) ? va_arg(_args, long int) : \ |
| 16 | va_arg(_args, int))) |
| 17 | |
| 18 | #define get_unum_va_args(_args, _lcount) \ |
| 19 | (((_lcount) > 1) ? va_arg(_args, unsigned long long int) : \ |
| 20 | (((_lcount) == 1) ? va_arg(_args, unsigned long int) : \ |
| 21 | va_arg(_args, unsigned int))) |
| 22 | |
Heyi Guo | 58bf3bf | 2021-01-20 13:55:25 +0800 | [diff] [blame] | 23 | #define CHECK_AND_PUT_CHAR(buf, size, chars_printed, ch) \ |
| 24 | do { \ |
| 25 | if ((chars_printed) < (size)) { \ |
| 26 | *(buf) = (ch); \ |
| 27 | (buf)++; \ |
| 28 | } \ |
| 29 | (chars_printed)++; \ |
| 30 | } while (false) |
| 31 | |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 32 | static void string_print(char **s, size_t n, size_t *chars_printed, |
| 33 | const char *str) |
| 34 | { |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 35 | while (*str != '\0') { |
Heyi Guo | 58bf3bf | 2021-01-20 13:55:25 +0800 | [diff] [blame] | 36 | CHECK_AND_PUT_CHAR(*s, n, *chars_printed, *str); |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 37 | str++; |
| 38 | } |
| 39 | } |
| 40 | |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 41 | static void unsigned_num_print(char **s, size_t n, size_t *chars_printed, |
| 42 | unsigned long long int unum, |
| 43 | unsigned int radix, char padc, int padn, |
| 44 | bool capitalise) |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 45 | { |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 46 | /* Just need enough space to store 64 bit decimal integer */ |
| 47 | char num_buf[20]; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 48 | int i = 0; |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 49 | int width; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 50 | unsigned int rem; |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 51 | char ascii_a = capitalise ? 'A' : 'a'; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 52 | |
Claus Pedersen | 785e66c | 2022-09-12 22:42:58 +0000 | [diff] [blame] | 53 | /* num_buf is only large enough for radix >= 10 */ |
Andre Przywara | 933cc5d | 2022-01-24 18:16:10 +0000 | [diff] [blame] | 54 | if (radix < 10) { |
Claus Pedersen | 785e66c | 2022-09-12 22:42:58 +0000 | [diff] [blame] | 55 | assert(0); |
| 56 | return; |
Andre Przywara | 933cc5d | 2022-01-24 18:16:10 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 59 | do { |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 60 | rem = unum % radix; |
| 61 | if (rem < 10U) { |
| 62 | num_buf[i] = '0' + rem; |
| 63 | } else { |
| 64 | num_buf[i] = ascii_a + (rem - 10U); |
| 65 | } |
| 66 | i++; |
| 67 | unum /= radix; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 68 | } while (unum > 0U); |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 69 | |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 70 | width = i; |
Andre Przywara | 3d95909 | 2021-12-21 12:35:54 +0000 | [diff] [blame] | 71 | for (i = padn - width; i > 0; i--) { |
| 72 | CHECK_AND_PUT_CHAR(*s, n, *chars_printed, padc); |
| 73 | } |
| 74 | for (i = width; i > 0; i--) { |
| 75 | CHECK_AND_PUT_CHAR(*s, n, *chars_printed, num_buf[i - 1]); |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 76 | } |
Andre Przywara | 3d95909 | 2021-12-21 12:35:54 +0000 | [diff] [blame] | 77 | for (i = width + padn; i < 0; i++) { |
| 78 | CHECK_AND_PUT_CHAR(*s, n, *chars_printed, padc); |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | /******************************************************************* |
Madhukar Pappireddy | 3862970 | 2020-09-08 19:00:00 -0500 | [diff] [blame] | 83 | * Reduced vsnprintf to be used for Trusted firmware. |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 84 | * The following type specifiers are supported: |
| 85 | * |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 86 | * %x (or %X) - hexadecimal format |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 87 | * %d or %i - signed decimal format |
Maksims Svecovs | bed5521 | 2023-05-09 12:03:31 +0100 | [diff] [blame] | 88 | * %c - character format |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 89 | * %s - string format |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 90 | * %u - unsigned decimal format |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 91 | * %p - pointer format |
| 92 | * |
kadabi | 76c3f5b | 2022-02-28 14:21:09 -0800 | [diff] [blame] | 93 | * The following length specifiers are supported by this print |
| 94 | * %l - long int |
| 95 | * %ll - long long int |
| 96 | * %z - size_t sized integer formats |
| 97 | * |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 98 | * The following padding specifiers are supported by this print |
| 99 | * %0NN - Left-pad the number with 0s (NN is a decimal number) |
| 100 | * %NN - Left-pad the number or string with spaces (NN is a decimal number) |
| 101 | * %-NN - Right-pad the number or string with spaces (NN is a decimal number) |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 102 | * |
| 103 | * The function panics on all other formats specifiers. |
| 104 | * |
| 105 | * It returns the number of characters that would be written if the |
| 106 | * buffer was big enough. If it returns a value lower than n, the |
| 107 | * whole string has been written. |
| 108 | *******************************************************************/ |
Madhukar Pappireddy | 3862970 | 2020-09-08 19:00:00 -0500 | [diff] [blame] | 109 | int vsnprintf(char *s, size_t n, const char *fmt, va_list args) |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 110 | { |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 111 | int num; |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 112 | unsigned long long int unum; |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 113 | char *str; |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 114 | char padc; /* Padding character */ |
| 115 | int padn; /* Number of characters to pad */ |
| 116 | bool left; |
| 117 | bool capitalise; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 118 | size_t chars_printed = 0U; |
kadabi | 76c3f5b | 2022-02-28 14:21:09 -0800 | [diff] [blame] | 119 | unsigned int l_count; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 120 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 121 | if (n == 0U) { |
| 122 | /* There isn't space for anything. */ |
| 123 | } else if (n == 1U) { |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 124 | /* Buffer is too small to actually write anything else. */ |
| 125 | *s = '\0'; |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 126 | n = 0U; |
| 127 | } else { |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 128 | /* Reserve space for the terminator character. */ |
| 129 | n--; |
| 130 | } |
| 131 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 132 | while (*fmt != '\0') { |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 133 | left = false; |
| 134 | padc ='\0'; |
| 135 | padn = 0; |
| 136 | capitalise = false; |
kadabi | 76c3f5b | 2022-02-28 14:21:09 -0800 | [diff] [blame] | 137 | l_count = 0; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 138 | |
| 139 | if (*fmt == '%') { |
| 140 | fmt++; |
| 141 | /* Check the format specifier. */ |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 142 | loop: |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 143 | switch (*fmt) { |
Heyi Guo | 11a16c8 | 2020-10-27 08:36:40 +0800 | [diff] [blame] | 144 | case '%': |
Heyi Guo | 58bf3bf | 2021-01-20 13:55:25 +0800 | [diff] [blame] | 145 | CHECK_AND_PUT_CHAR(s, n, chars_printed, '%'); |
Heyi Guo | 11a16c8 | 2020-10-27 08:36:40 +0800 | [diff] [blame] | 146 | break; |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 147 | case '0': |
| 148 | case '1': |
| 149 | case '2': |
| 150 | case '3': |
| 151 | case '4': |
| 152 | case '5': |
| 153 | case '6': |
| 154 | case '7': |
| 155 | case '8': |
| 156 | case '9': |
| 157 | padc = (*fmt == '0') ? '0' : ' '; |
| 158 | for (padn = 0; *fmt >= '0' && *fmt <= '9'; fmt++) { |
| 159 | padn = (padn * 10) + (*fmt - '0'); |
| 160 | } |
| 161 | if (left) { |
| 162 | padn = -padn; |
| 163 | } |
| 164 | goto loop; |
| 165 | case '-': |
| 166 | left = true; |
| 167 | fmt++; |
| 168 | goto loop; |
| 169 | |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 170 | case 'i': |
| 171 | case 'd': |
kadabi | 76c3f5b | 2022-02-28 14:21:09 -0800 | [diff] [blame] | 172 | num = get_num_va_args(args, l_count); |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 173 | |
| 174 | if (num < 0) { |
Heyi Guo | 58bf3bf | 2021-01-20 13:55:25 +0800 | [diff] [blame] | 175 | CHECK_AND_PUT_CHAR(s, n, chars_printed, |
| 176 | '-'); |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 177 | unum = (unsigned int)-num; |
| 178 | } else { |
| 179 | unum = (unsigned int)num; |
| 180 | } |
| 181 | |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 182 | unsigned_num_print(&s, n, &chars_printed, |
| 183 | unum, 10, padc, padn, false); |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 184 | break; |
Maksims Svecovs | bed5521 | 2023-05-09 12:03:31 +0100 | [diff] [blame] | 185 | case 'c': |
| 186 | CHECK_AND_PUT_CHAR(s, n, chars_printed, va_arg(args, int)); |
| 187 | break; |
Antonio Nino Diaz | 9fec10f | 2018-08-09 15:30:47 +0100 | [diff] [blame] | 188 | case 's': |
| 189 | str = va_arg(args, char *); |
| 190 | string_print(&s, n, &chars_printed, str); |
| 191 | break; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 192 | case 'u': |
kadabi | 76c3f5b | 2022-02-28 14:21:09 -0800 | [diff] [blame] | 193 | unum = get_unum_va_args(args, l_count); |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 194 | unsigned_num_print(&s, n, &chars_printed, |
| 195 | unum, 10, padc, padn, false); |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 196 | break; |
kadabi | 76c3f5b | 2022-02-28 14:21:09 -0800 | [diff] [blame] | 197 | case 'z': |
| 198 | l_count = 1; |
| 199 | fmt++; |
| 200 | goto loop; |
| 201 | case 'l': |
| 202 | l_count++; |
| 203 | fmt++; |
| 204 | goto loop; |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 205 | case 'p': |
| 206 | unum = (uintptr_t)va_arg(args, void *); |
| 207 | if (unum > 0U) { |
| 208 | string_print(&s, n, &chars_printed, "0x"); |
| 209 | padn -= 2; |
| 210 | } |
| 211 | unsigned_num_print(&s, n, &chars_printed, |
| 212 | unum, 16, padc, padn, false); |
| 213 | break; |
| 214 | case 'X': |
| 215 | capitalise = true; |
Boyan Karatotev | 8726600 | 2022-11-18 14:17:17 +0000 | [diff] [blame] | 216 | /* fallthrough */ |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 217 | case 'x': |
kadabi | 76c3f5b | 2022-02-28 14:21:09 -0800 | [diff] [blame] | 218 | unum = get_unum_va_args(args, l_count); |
Javier Almansa Sobrino | c58a13e | 2020-08-21 17:32:03 +0100 | [diff] [blame] | 219 | unsigned_num_print(&s, n, &chars_printed, |
| 220 | unum, 16, padc, padn, |
| 221 | capitalise); |
| 222 | break; |
| 223 | |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 224 | default: |
Claus Pedersen | 785e66c | 2022-09-12 22:42:58 +0000 | [diff] [blame] | 225 | CHECK_AND_PUT_CHAR(s, n, chars_printed, '%'); |
| 226 | CHECK_AND_PUT_CHAR(s, n, chars_printed, *fmt); |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 227 | } |
| 228 | fmt++; |
| 229 | continue; |
| 230 | } |
| 231 | |
Heyi Guo | 58bf3bf | 2021-01-20 13:55:25 +0800 | [diff] [blame] | 232 | CHECK_AND_PUT_CHAR(s, n, chars_printed, *fmt); |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 233 | |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 234 | fmt++; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Madhukar Pappireddy | 3862970 | 2020-09-08 19:00:00 -0500 | [diff] [blame] | 237 | if (n > 0U) { |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 238 | *s = '\0'; |
Madhukar Pappireddy | 3862970 | 2020-09-08 19:00:00 -0500 | [diff] [blame] | 239 | } |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 240 | |
Antonio Nino Diaz | 2e74f9b | 2018-08-23 15:11:46 +0100 | [diff] [blame] | 241 | return (int)chars_printed; |
Antonio Nino Diaz | 9c107fa | 2017-05-17 15:34:22 +0100 | [diff] [blame] | 242 | } |
Madhukar Pappireddy | 3862970 | 2020-09-08 19:00:00 -0500 | [diff] [blame] | 243 | |
| 244 | /******************************************************************* |
| 245 | * Reduced snprintf to be used for Trusted firmware. |
| 246 | * The following type specifiers are supported: |
| 247 | * |
| 248 | * %x (or %X) - hexadecimal format |
| 249 | * %d or %i - signed decimal format |
| 250 | * %s - string format |
| 251 | * %u - unsigned decimal format |
| 252 | * %p - pointer format |
| 253 | * |
| 254 | * The following padding specifiers are supported by this print |
| 255 | * %0NN - Left-pad the number with 0s (NN is a decimal number) |
| 256 | * %NN - Left-pad the number or string with spaces (NN is a decimal number) |
| 257 | * %-NN - Right-pad the number or string with spaces (NN is a decimal number) |
| 258 | * |
| 259 | * The function panics on all other formats specifiers. |
| 260 | * |
| 261 | * It returns the number of characters that would be written if the |
| 262 | * buffer was big enough. If it returns a value lower than n, the |
| 263 | * whole string has been written. |
| 264 | *******************************************************************/ |
| 265 | int snprintf(char *s, size_t n, const char *fmt, ...) |
| 266 | { |
| 267 | int count; |
| 268 | va_list all_args; |
| 269 | |
| 270 | va_start(all_args, fmt); |
| 271 | count = vsnprintf(s, n, fmt, all_args); |
| 272 | va_end(all_args); |
| 273 | |
| 274 | return count; |
| 275 | } |