Add uint64_to_str helper.

This commit is contained in:
Milan Broz
2017-08-25 20:14:24 +02:00
parent 64e91951b2
commit a0d2d4c0b1
2 changed files with 16 additions and 0 deletions

View File

@@ -149,6 +149,8 @@ unsigned crypt_cpusonline(void);
int init_crypto(struct crypt_device *ctx); int init_crypto(struct crypt_device *ctx);
const char *uint64_to_str(char *buffer, size_t size, const uint64_t *val);
void logger(struct crypt_device *cd, int class, const char *file, int line, const char *format, ...) __attribute__ ((format (printf, 5, 6))); void logger(struct crypt_device *cd, int class, const char *file, int line, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
#define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x) #define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
#define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x) #define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)

View File

@@ -45,6 +45,20 @@ unsigned crypt_cpusonline(void)
return r < 0 ? 1 : r; return r < 0 ? 1 : r;
} }
const char *uint64_to_str(char *buffer, size_t size, const uint64_t *val)
{
int r = snprintf(buffer, size, "%" PRIu64, *val);
if (r < 0) {
log_dbg("Failed to convert integer to string.");
*buffer = '\0';
} else if ((size_t)r >= size) {
log_dbg("Not enough space to store '%" PRIu64 "' to a string buffer.", *val);
*buffer = '\0';
}
return buffer;
}
ssize_t read_buffer(int fd, void *buf, size_t count) ssize_t read_buffer(int fd, void *buf, size_t count)
{ {
size_t read_size = 0; size_t read_size = 0;