1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <cstdarg>
#ifdef _WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif
#include <cmath>
#include <cstring>
#include "mozilla/Assertions.h"
#include "FdPrintf.h"
/* Template class allowing a limited number of increments on a value */
template <typename T>
class CheckedIncrement {
public:
CheckedIncrement(T aValue, size_t aMaxIncrement)
: mValue(aValue), mMaxIncrement(aMaxIncrement) {}
T operator++(int) {
if (!mMaxIncrement) {
MOZ_CRASH("overflow detected");
}
mMaxIncrement--;
return mValue++;
}
T& operator++() {
(*this)++;
return mValue;
}
void advance(T end) {
// Only makes sense if T is a pointer type.
size_t diff = end - mValue;
if (diff > mMaxIncrement) {
MOZ_CRASH("overflow detected");
}
mMaxIncrement -= diff;
mValue = end;
}
void rewind(T pos) {
size_t diff = mValue - pos;
mMaxIncrement += diff;
mValue = pos;
}
operator T() { return mValue; }
T value() { return mValue; }
private:
T mValue;
size_t mMaxIncrement;
};
template <typename T>
static unsigned NumDigits(T n) {
if (n < 1) {
// We want one digit, it will be 0.
return 1;
}
double l = log10(static_cast<double>(n));
double cl = ceil(l);
return l == cl ? unsigned(cl) + 1 : unsigned(cl);
}
static void LeftPad(CheckedIncrement<char*>& b, size_t pad) {
while (pad-- > 0) {
*(b++) = ' ';
}
}
// Write the digits into the buffer.
static void WriteDigits(CheckedIncrement<char*>& b, size_t i,
size_t num_digits) {
size_t x = pow(10, double(num_digits - 1));
do {
*(b++) = "0123456789"[(i / x) % 10];
x /= 10;
} while (x > 0);
}
int VSNPrintf(char* aBuf, size_t aSize, const char* aFormat, va_list aArgs) {
CheckedIncrement<char*> b(aBuf, aSize);
CheckedIncrement<const char*> f(aFormat, strlen(aFormat) + 1);
while (true) {
switch (*f) {
case '\0':
return b - aBuf;
case '%': {
// The start of the format specifier is used if this specifier is
// invalid.
const char* start = f;
// Read the field width
f++;
char* end = nullptr;
size_t width = strtoul(f, &end, 10);
// If strtol can't find a number that's okay, that means 0 in our
// case, but we must advance f).
f.advance(end);
switch (*f) {
case 'z': {
if (*(++f) == 'u') {
size_t i = va_arg(aArgs, size_t);
size_t num_digits = NumDigits(i);
LeftPad(b, width > num_digits ? width - num_digits : 0);
WriteDigits(b, i, num_digits);
} else {
// If the format specifier is unknown then write out '%' and
// rewind to the beginning of the specifier causing it to be
// printed normally.
*(b++) = '%';
f.rewind(start);
}
break;
}
case 'p': {
intptr_t ptr = va_arg(aArgs, intptr_t);
*(b++) = '0';
*(b++) = 'x';
int x = sizeof(intptr_t) * 8;
bool wrote_msb = false;
do {
x -= 4;
size_t hex_digit = ptr >> x & 0xf;
if (hex_digit || wrote_msb) {
*(b++) = "0123456789abcdef"[hex_digit];
wrote_msb = true;
}
} while (x > 0);
if (!wrote_msb) {
*(b++) = '0';
}
break;
}
case 's': {
const char* str = va_arg(aArgs, const char*);
size_t len = strlen(str);
LeftPad(b, width > len ? width - len : 0);
while (*str) {
*(b++) = *(str++);
}
break;
}
case '%':
// Print a single raw '%'.
*(b++) = '%';
break;
default:
// If the format specifier is unknown then write out '%' and
// rewind to the beginning of the specifier causing it to be
// printed normally.
*(b++) = '%';
f.rewind(start);
break;
}
break;
}
default:
*(b++) = *f;
break;
}
f++;
}
}
int SNPrintf(char* aBuf, size_t aSize, const char* aFormat, ...) {
va_list args;
va_start(args, aFormat);
int ret = VSNPrintf(aBuf, aSize, aFormat, args);
va_end(args);
return ret;
}
void VFdPrintf(platform_handle_t aFd, const char* aFormat, va_list aArgs) {
char buf[256];
int len = VSNPrintf(buf, 256, aFormat, aArgs);
FdPuts(aFd, buf, len);
}
void FdPrintf(platform_handle_t aFd, const char* aFormat, ...) {
va_list args;
va_start(args, aFormat);
VFdPrintf(aFd, aFormat, args);
va_end(args);
}
void FdPuts(platform_handle_t aFd, const char* aBuf, size_t aSize) {
if (aFd == 0) {
return;
}
#ifdef _WIN32
// See comment in FdPrintf.h as to why WriteFile is used.
DWORD written;
WriteFile(aFd, aBuf, aSize, &written, nullptr);
#else
[[maybe_unused]] ssize_t _ = write(aFd, aBuf, aSize);
#endif
}
|