diff options
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/core/github.c | 7 | ||||
| -rw-r--r-- | src/lib/core/include/utils.h | 2 | ||||
| -rw-r--r-- | src/lib/core/utils.c | 51 | 
3 files changed, 58 insertions, 2 deletions
diff --git a/src/lib/core/github.c b/src/lib/core/github.c index c195a28..f0c5199 100644 --- a/src/lib/core/github.c +++ b/src/lib/core/github.c @@ -109,9 +109,12 @@ int get_github_release_notes(const char *api_token, const char *repo, const char              if (last_char == ',') {                  trim++;              } -            data_offset[strlen(data_offset) - trim] = 0; +            // Truncate the trimmed bytes +            memset(&data_offset[strlen(data_offset) - trim], 0, trim);              // Extract release notes -            *output = strdup(data_offset); +            *output = calloc(strlen(data_offset) + 1, sizeof(**output)); +            // Copy output (including terminator) +            strncpy(*output, data_offset, strlen(data_offset) + 1);          } else if ((data_offset = strstr(line, field_message))) {              // Skip past the message field              data_offset += strlen(field_message); diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h index 1906808..b405b2a 100644 --- a/src/lib/core/include/utils.h +++ b/src/lib/core/include/utils.h @@ -417,4 +417,6 @@ int gen_file_extension_str(char *filename, const char *extension);   * Remove [extra]s from a spec string   */  char *remove_extras(char *s); + +void debug_hexdump(char *data, int len);  #endif //STASIS_UTILS_H diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index a248f58..a9c9ea5 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -892,3 +892,54 @@ int gen_file_extension_str(char *filename, const char *extension) {      return replace_text(ext_orig, ext_orig, extension, 0);  } +#define DEBUG_HEXDUMP_ADDR_MAXLEN 20 +#define DEBUG_HEXDUMP_BYTES_MAXLEN (16 * 3 + 2) +#define DEBUG_HEXDUMP_ASCII_MAXLEN (16 + 1) +#define DEBUG_HEXDUMP_OUTPUT_MAXLEN (DEBUG_HEXDUMP_ADDR_MAXLEN + DEBUG_HEXDUMP_BYTES_MAXLEN + DEBUG_HEXDUMP_ASCII_MAXLEN + 1) + +void debug_hexdump(char *data, int len) { +    int count = 0; +    char addr[DEBUG_HEXDUMP_ADDR_MAXLEN] = {0}; +    char bytes[DEBUG_HEXDUMP_BYTES_MAXLEN] = {0}; +    char ascii[DEBUG_HEXDUMP_ASCII_MAXLEN] = {0}; +    char output[DEBUG_HEXDUMP_OUTPUT_MAXLEN] = {0}; +    char *start = data; +    char *end = data + len; + +    char *pos = start; +    while (pos != end) { +        if (count == 0) { +            sprintf(addr + strlen(addr), "%p", pos); +        } +        if (count == 8) { +            strcat(bytes, " "); +        } +        if (count > 15) { +            sprintf(output, "%s | %s | %s", addr, bytes, ascii); +            puts(output); +            memset(output, 0, sizeof(output)); +            memset(addr, 0, sizeof(addr)); +            memset(bytes, 0, sizeof(bytes)); +            memset(ascii, 0, sizeof(ascii)); +            count = 0; +            continue; +        } + +        sprintf(bytes + strlen(bytes), "%02X ", (unsigned char) *pos); +        sprintf(ascii + strlen(ascii), "%c", isprint(*pos) ? *pos : '.'); + +        pos++; +        count++; +    } + +    if (count <= 8) { +        // Add group padding +        strcat(bytes, " "); +    } +    int padding = 16 - count; +    for (int i = 0; i < padding; i++) { +        strcat(bytes, "   "); +    } +    sprintf(output, "%s | %s | %s", addr, bytes, ascii); +    puts(output); +}
\ No newline at end of file  | 
