add a str_append fct. To handle some string manipulation issues in the client
This commit is contained in:
1 parent
10dafd8048
commit
58580c690f
2 files changed
+33
No files matched your search
@@ -742,6 +742,38 @@ bool bytes_equal_not_null(const void *a, size_t a_len, const void *b, size_t b_l
|
||||
return a_len == b_len && a != NULL && b != NULL && memcmp(a, b, a_len) == 0;
|
||||
}
|
||||
|
||||
// Append a formatted string to buf without ever writing past buf_len.
|
||||
// Returns the length of the resulting string.
|
||||
// buf must already be a valid NULL terminated string.
|
||||
size_t str_append(char *buf, size_t buf_len, const char *fmt, ...) {
|
||||
|
||||
if (buf == NULL || buf_len == 0 || fmt == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t len = strlen(buf);
|
||||
|
||||
// no room for another character and its terminator
|
||||
if (len + 1 >= buf_len) {
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t avail = buf_len - len;
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
int n = vsnprintf(buf + len, avail, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
if (n < 0) {
|
||||
// encoding error. leave the string as we found it
|
||||
buf[len] = '\0';
|
||||
return len;
|
||||
}
|
||||
|
||||
return ((size_t)n < avail) ? (len + (size_t)n) : (buf_len - 1);
|
||||
}
|
||||
|
||||
int buffer_append_bytes_with_offset(uint8_t *buf, size_t buf_len, size_t *offset, const void *data, size_t data_len) {
|
||||
if (buf == NULL || offset == NULL || (data_len > 0 && data == NULL)) {
|
||||
return PM3_EINVARG;
|
||||
|
||||
@@ -107,6 +107,7 @@ int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValue
|
||||
int parse_uint32_hex_or_dec(const char *text, uint32_t *out);
|
||||
bool bytes_equal_not_null(const void *a, size_t a_len, const void *b, size_t b_len);
|
||||
int buffer_append_bytes_with_offset(uint8_t *buf, size_t buf_len, size_t *offset, const void *data, size_t data_len);
|
||||
size_t str_append(char *buf, size_t buf_len, const char *fmt, ...);
|
||||
void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest);
|
||||
void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest);
|
||||
void bytes_to_bytebits(const void *src, const size_t srclen, void *dest);
|
||||
|
||||
Reference in new issue
Block a user