Skip to content

Commit

Permalink
Add uint64 formatting helper
Browse files Browse the repository at this point in the history
  • Loading branch information
refi93 committed Dec 22, 2020
1 parent 32032a8 commit dd4533e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/signOpCert.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void signOpCert_ui_runStep()
}
UI_STEP(UI_STEP_DISPLAY_KES_PERIOD) {
char kesPeriodString[50];
snprintf(kesPeriodString, SIZEOF(kesPeriodString), "%llu", ctx->kesPeriod);
str_formatUint64(ctx->kesPeriod, kesPeriodString, SIZEOF(kesPeriodString));
ui_displayPaginatedText(
"KES period",
kesPeriodString,
Expand All @@ -143,7 +143,7 @@ static void signOpCert_ui_runStep()
}
UI_STEP(UI_STEP_DISPLAY_ISSUE_COUNTER) {
char issueCounterString[50];
snprintf(issueCounterString, SIZEOF(issueCounterString), "%llu", ctx->issueCounter);
str_formatUint64(ctx->issueCounter, issueCounterString, SIZEOF(issueCounterString));
ui_displayPaginatedText(
"Issue counter",
issueCounterString,
Expand Down
35 changes: 35 additions & 0 deletions src/textUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,41 @@ size_t str_formatAdaAmount(uint64_t amount, char* out, size_t outSize)
return rawSize + suffixLength;
}

size_t str_formatUint64(uint64_t number, char* out, size_t outSize) {
ASSERT(outSize < BUFFER_SIZE_PARANOIA);

char scratchBuffer[30];
char* ptr = BEGIN(scratchBuffer);
char* end = END(scratchBuffer);

// We print in reverse
// We want at least one iteration
int place = 0;
do {
WRITE_CHAR(ptr, end, '0' + (number % 10));
number /= 10;
place++;
} while (number > 0);

// Size without terminating character
STATIC_ASSERT(sizeof(ptr - scratchBuffer) == sizeof(size_t), "bad size_t size");
size_t rawSize = (size_t) (ptr - scratchBuffer);

if (rawSize + 1 > outSize) {
THROW(ERR_DATA_TOO_LARGE);
}

// Copy reversed & append terminator
for (size_t i = 0; i < rawSize; i++) {
out[i] = scratchBuffer[rawSize - 1 - i];
}
out[rawSize] = 0;

ASSERT(strlen(out) == rawSize);

return rawSize;
}

#ifdef DEVEL
void str_traceAdaAmount(const char* prefix, uint64_t amount)
{
Expand Down
2 changes: 2 additions & 0 deletions src/textUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

size_t str_formatAdaAmount(uint64_t amount, char* out, size_t outSize);

size_t str_formatUint64(uint64_t number, char* out, size_t outSize);

#ifdef DEVEL
void str_traceAdaAmount(const char *prefix, uint64_t amount);
#define TRACE_ADA_AMOUNT(PREFIX, AMOUNT) \
Expand Down
31 changes: 31 additions & 0 deletions src/textUtils_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,41 @@ void test_formatTtl()

}

void testcase_formatUint64(
uint64_t number,
const char* expected
)
{
PRINTF("testcase_formatUint64 %s\n", expected);

{
char tmp[30];
size_t len = str_formatUint64(number, tmp, SIZEOF(tmp));
EXPECT_EQ(len, strlen(expected));
EXPECT_EQ(strcmp(tmp, expected), 0);
}

{
// check for buffer overflows
char tmp[30];
EXPECT_THROWS(str_formatUint64(number, tmp, strlen(expected)), ERR_DATA_TOO_LARGE);
}
}

void test_formatUint64()
{
testcase_formatUint64( 0, "0");
testcase_formatUint64( 1, "1");
testcase_formatUint64( 4924800, "4924800");
testcase_formatUint64( 4924799, "4924799");
testcase_formatUint64( -1ll, "18446744073709551615");
}

void run_textUtils_test()
{
test_formatAda();
test_formatTtl();
test_formatUint64();
}

#endif

0 comments on commit dd4533e

Please sign in to comment.