176 lines
5.8 KiB
C
176 lines
5.8 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#include <stdio.h>
|
|
#include <limits.h>
|
|
#include "format-internal.h"
|
|
|
|
typedef struct Buffer {
|
|
char *next;
|
|
size_t remaining; // Excludes the final NUL.
|
|
} Buffer;
|
|
|
|
static int storeBuffer(void *context, const char *source, char fill, unsigned count)
|
|
{
|
|
Buffer *buffer = (Buffer *) context;
|
|
unsigned stored = count < buffer->remaining ? count : buffer->remaining;
|
|
if (stored) {
|
|
if (source)
|
|
memcpy(buffer->next, source, stored);
|
|
else
|
|
memset(buffer->next, fill, stored);
|
|
buffer->next += stored;
|
|
buffer->remaining -= stored;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
typedef struct Output {
|
|
VCCFormatWrite write;
|
|
void *context;
|
|
unsigned total; // Saturates one past the largest signed result.
|
|
} Output;
|
|
|
|
static int emit(Output *out, const char *source, char fill, unsigned count)
|
|
{
|
|
if (count == 0) return 0;
|
|
if (out->write(out->context, source, fill, count) < 0) return -1;
|
|
if (out->total <= INT_MAX) {
|
|
if (count > INT_MAX - out->total)
|
|
out->total = (unsigned) INT_MAX + 1;
|
|
else
|
|
out->total += count;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// Consume literal field width or precision. Values outside the supported
|
|
// range cannot be represented reliably on this target.
|
|
static int decimalField(const char **cursor)
|
|
{
|
|
unsigned value = 0;
|
|
while (**cursor >= '0' && **cursor <= '9') {
|
|
unsigned digit = **cursor - '0';
|
|
if (value > (INT_MAX - digit) / 10)
|
|
return -1;
|
|
value = value * 10 + digit;
|
|
++*cursor;
|
|
}
|
|
return (int) value;
|
|
}
|
|
|
|
int vcc_format_to(VCCFormatWrite write, void *context, const char *format, va_list args)
|
|
{
|
|
Output out;
|
|
if (!format) return -1;
|
|
out.write = write;
|
|
out.context = context;
|
|
out.total = 0;
|
|
|
|
while (*format) {
|
|
if (*format != '%') {
|
|
const char *start = format;
|
|
do { ++format; } while (*format && *format != '%');
|
|
if (emit(&out, start, 0, format - start) < 0) return -1;
|
|
continue;
|
|
}
|
|
++format;
|
|
if (*format == '%') {
|
|
if (emit(&out, 0, '%', 1) < 0) return -1;
|
|
++format;
|
|
continue;
|
|
}
|
|
|
|
int left = 0, padZero = 0;
|
|
while (*format == '-' || *format == '0') {
|
|
if (*format == '-') left = 1;
|
|
else padZero = 1;
|
|
++format;
|
|
}
|
|
int width = decimalField(&format);
|
|
if (width < 0) return -1;
|
|
int precision = -1;
|
|
if (*format == '.') {
|
|
++format;
|
|
precision = decimalField(&format);
|
|
if (precision < 0) return -1;
|
|
}
|
|
int wide = *format == 'l';
|
|
if (wide) ++format;
|
|
char conversion = *format++;
|
|
char digits[11]; // A 32-bit unsigned integer has at most 11 octal digits.
|
|
const char *text = digits;
|
|
unsigned length = 0, zeroes = 0;
|
|
int negative = 0;
|
|
if (conversion == 's') {
|
|
if (wide || padZero) return -1;
|
|
text = va_arg(args, const char *);
|
|
// With a precision, the input need not be NUL terminated.
|
|
while ((precision < 0 || length < (unsigned) precision) && text[length])
|
|
++length;
|
|
} else if (conversion == 'c') {
|
|
if (wide || padZero || precision >= 0) return -1;
|
|
digits[0] = (char) va_arg(args, int);
|
|
length = 1;
|
|
} else {
|
|
unsigned base;
|
|
switch (conversion) {
|
|
case 'i': case 'd': case 'u': base = 10; break;
|
|
case 'o': base = 8; break;
|
|
case 'X': case 'x': base = 16; break;
|
|
default: return -1;
|
|
}
|
|
unsigned long value;
|
|
if (conversion == 'i' || conversion == 'd') {
|
|
long number;
|
|
if (wide) number = va_arg(args, long);
|
|
else number = va_arg(args, int);
|
|
negative = number < 0;
|
|
value = (unsigned long) number;
|
|
if (negative) value = 0UL - value;
|
|
} else if (wide) value = va_arg(args, unsigned long);
|
|
else value = va_arg(args, unsigned);
|
|
|
|
if (value || precision != 0) {
|
|
do {
|
|
unsigned digit = (unsigned) (value % base);
|
|
const char *alphabet = conversion == 'X' ? "0123456789ABCDEF" : "0123456789abcdef";
|
|
digits[sizeof(digits) - ++length] = alphabet[digit];
|
|
value /= base;
|
|
} while (value);
|
|
}
|
|
text = digits + sizeof(digits) - length;
|
|
if (precision > (int) length) zeroes = (unsigned) precision - length;
|
|
}
|
|
unsigned occupied = length + zeroes + negative;
|
|
unsigned gap = (unsigned) width > occupied ? (unsigned) width - occupied : 0U;
|
|
if (padZero && !left && precision < 0) {
|
|
zeroes += gap;
|
|
gap = 0;
|
|
}
|
|
if (!left && emit(&out, 0, ' ', gap) < 0) return -1;
|
|
if (negative && emit(&out, 0, '-', 1) < 0) return -1;
|
|
if (emit(&out, 0, '0', zeroes) < 0 || emit(&out, text, 0, length) < 0) return -1;
|
|
if (left && emit(&out, 0, ' ', gap) < 0) return -1;
|
|
}
|
|
return out.total > INT_MAX ? -1 : (int) out.total;
|
|
}
|
|
|
|
int vsnprintf(char *dest, size_t capacity, const char *format, va_list args)
|
|
{
|
|
Buffer buffer;
|
|
if (capacity && !dest) return -1;
|
|
buffer.next = dest;
|
|
buffer.remaining = capacity ? capacity - 1U : 0U;
|
|
int result = vcc_format_to(storeBuffer, &buffer, format, args);
|
|
if (capacity) *buffer.next = 0;
|
|
return result;
|
|
}
|
|
|
|
int snprintf(char *dest, size_t capacity, const char *format, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, format);
|
|
int result = vsnprintf(dest, capacity, format, args);
|
|
va_end(args);
|
|
return result;
|
|
}
|