From c89ce085a6bec211b7213b95d28d07a1cc1644b6 Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Wed, 22 Jan 2020 21:29:44 +0000 Subject: [PATCH] =?UTF-8?q?thermopsh=C3=A8re:=20rewrite=20some=20gdb/net?= =?UTF-8?q?=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- thermosphere/src/gdb/net.c | 135 +++++++++++++------------------------ thermosphere/src/gdb/net.h | 26 +++---- 2 files changed, 61 insertions(+), 100 deletions(-) diff --git a/thermosphere/src/gdb/net.c b/thermosphere/src/gdb/net.c index bc9082f8f..3fea8cc32 100644 --- a/thermosphere/src/gdb/net.c +++ b/thermosphere/src/gdb/net.c @@ -11,22 +11,22 @@ #include "fmt.h" #include "minisoc.h" -u8 GDB_ComputeChecksum(const char *packetData, u32 len) +u8 GDB_ComputeChecksum(const char *packetData, size_t len) { - u8 cksum = 0; - for(u32 i = 0; i < len; i++) - cksum += (u8)packetData[i]; + unsigned long cksum = 0; + for(size_t i = 0; i < len; i++) { + cksum += packetData[i]; + } - return cksum; + return (u8)cksum; } -void GDB_EncodeHex(char *dst, const void *src, u32 len) +void GDB_EncodeHex(char *dst, const void *src, size_t len) { static const char *alphabet = "0123456789abcdef"; - const u8 *src8 = (u8 *)src; + const u8 *src8 = (const u8 *)src; - for(u32 i = 0; i < len; i++) - { + for (size_t i = 0; i < len; i++) { dst[2 * i] = alphabet[(src8[i] & 0xf0) >> 4]; dst[2 * i + 1] = alphabet[src8[i] & 0x0f]; } @@ -35,92 +35,89 @@ void GDB_EncodeHex(char *dst, const void *src, u32 len) static inline u32 GDB_DecodeHexDigit(char src, bool *ok) { *ok = true; - if(src >= '0' && src <= '9') return src - '0'; - else if(src >= 'a' && src <= 'f') return 0xA + (src - 'a'); - else if(src >= 'A' && src <= 'F') return 0xA + (src - 'A'); - else - { - *ok = false; - return 0; + switch (src) { + case '0' ... '9': return 0 + (src - '0'); + case 'a' ... 'f': return 10 + (src - 'a'); + case 'A' ... 'F': return 10 + (src - 'A'); + default: + *ok = false; + return 0; } } -u32 GDB_DecodeHex(void *dst, const char *src, u32 len) -{ - u32 i = 0; +u32 GDB_DecodeHex(void *dst, const char *src, size_t len) { + size_t i = 0; bool ok = true; u8 *dst8 = (u8 *)dst; - for(i = 0; i < len && ok && src[2 * i] != 0 && src[2 * i + 1] != 0; i++) + for (i = 0; i < len && ok && src[2 * i] != 0 && src[2 * i + 1] != 0; i++) { dst8[i] = (GDB_DecodeHexDigit(src[2 * i], &ok) << 4) | GDB_DecodeHexDigit(src[2 * i + 1], &ok); + } return (!ok) ? i - 1 : i; } -u32 GDB_EscapeBinaryData(u32 *encodedCount, void *dst, const void *src, u32 len, u32 maxLen) +u32 GDB_EscapeBinaryData(u32 *encodedCount, void *dst, const void *src, size_t len, size_t maxLen) { u8 *dst8 = (u8 *)dst; const u8 *src8 = (const u8 *)src; maxLen = maxLen >= len ? len : maxLen; - while((uintptr_t)dst8 < (uintptr_t)dst + maxLen) - { - if(*src8 == '$' || *src8 == '#' || *src8 == '}' || *src8 == '*') - { - if ((uintptr_t)dst8 + 1 >= (uintptr_t)dst + maxLen) + while ((uintptr_t)dst8 < (uintptr_t)dst + maxLen) { + if (*src8 == '$' || *src8 == '#' || *src8 == '}' || *src8 == '*') { + if ((uintptr_t)dst8 + 1 >= (uintptr_t)dst + maxLen) { break; + } *dst8++ = '}'; *dst8++ = *src8++ ^ 0x20; } - else + else { *dst8++ = *src8++; + } } *encodedCount = dst8 - (u8 *)dst; return src8 - (u8 *)src; } -u32 GDB_UnescapeBinaryData(void *dst, const void *src, u32 len) +u32 GDB_UnescapeBinaryData(void *dst, const void *src, size_t len) { u8 *dst8 = (u8 *)dst; const u8 *src8 = (const u8 *)src; - while((uintptr_t)src8 < (uintptr_t)src + len) - { - if(*src8 == '}') - { + while ((uintptr_t)src8 < (uintptr_t)src + len) { + if (*src8 == '}') { src8++; *dst8++ = *src8++ ^ 0x20; - } - else + } else { *dst8++ = *src8++; + } } return dst8 - (u8 *)dst; } -const char *GDB_ParseIntegerList(u32 *dst, const char *src, u32 nb, char sep, char lastSep, u32 base, bool allowPrefix) +const char *GDB_ParseIntegerList(unsigned long *dst, const char *src, size_t nb, char sep, char lastSep, u32 base, bool allowPrefix) { const char *pos = src; const char *endpos; bool ok; - for(u32 i = 0; i < nb; i++) - { - u32 n = xstrtoul(pos, (char **)&endpos, (int) base, allowPrefix, &ok); - if(!ok || endpos == pos) + for (size_t i = 0; i < nb; i++) { + unsigned long n = xstrtoul(pos, (char **)&endpos, (int) base, allowPrefix, &ok); + if(!ok || endpos == pos) { return NULL; - - if(i != nb - 1) - { - if(*endpos != sep) - return NULL; - pos = endpos + 1; } - else - { - if(*endpos != lastSep && *endpos != 0) + + if (i != nb - 1) { + if (*endpos != sep) { return NULL; + } + pos = endpos + 1; + } else { + if (*endpos != lastSep && *endpos != 0) { + return NULL; + } pos = endpos; } @@ -130,47 +127,11 @@ const char *GDB_ParseIntegerList(u32 *dst, const char *src, u32 nb, char sep, ch return pos; } -const char *GDB_ParseIntegerList64(u64 *dst, const char *src, u32 nb, char sep, char lastSep, u32 base, bool allowPrefix) -{ - const char *pos = src; - const char *endpos; - bool ok; - - for(u32 i = 0; i < nb; i++) - { - u64 n = xstrtoull(pos, (char **)&endpos, (int) base, allowPrefix, &ok); - if(!ok || endpos == pos) - return NULL; - - if(i != nb - 1) - { - if(*endpos != sep) - return NULL; - pos = endpos + 1; - } - else - { - if(*endpos != lastSep && *endpos != 0) - return NULL; - pos = endpos; - } - - dst[i] = n; - } - - return pos; -} - -const char *GDB_ParseHexIntegerList(u32 *dst, const char *src, u32 nb, char lastSep) +const char *GDB_ParseHexIntegerList(unsigned long *dst, const char *src, size_t nb, char lastSep) { return GDB_ParseIntegerList(dst, src, nb, ',', lastSep, 16, false); } -const char *GDB_ParseHexIntegerList64(u64 *dst, const char *src, u32 nb, char lastSep) -{ - return GDB_ParseIntegerList64(dst, src, nb, ',', lastSep, 16, false); -} - int GDB_ReceivePacket(GDBContext *ctx) { char backupbuf[GDB_BUF_LEN + 4]; @@ -262,7 +223,7 @@ packet_error: return -1; } -static int GDB_DoSendPacket(GDBContext *ctx, u32 len) +static int GDB_DoSendPacket(GDBContext *ctx, size_t len) { int r = soc_send(ctx->super.sockfd, ctx->buffer, len, 0); @@ -271,7 +232,7 @@ static int GDB_DoSendPacket(GDBContext *ctx, u32 len) return r; } -int GDB_SendPacket(GDBContext *ctx, const char *packetData, u32 len) +int GDB_SendPacket(GDBContext *ctx, const char *packetData, size_t len) { ctx->buffer[0] = '$'; diff --git a/thermosphere/src/gdb/net.h b/thermosphere/src/gdb/net.h index c4676a502..992af1201 100644 --- a/thermosphere/src/gdb/net.h +++ b/thermosphere/src/gdb/net.h @@ -7,24 +7,24 @@ #pragma once -#include "gdb.h" +#include "../gdb.h" #define _REENT_ONLY #include -u8 GDB_ComputeChecksum(const char *packetData, u32 len); -void GDB_EncodeHex(char *dst, const void *src, u32 len); -u32 GDB_DecodeHex(void *dst, const char *src, u32 len); -u32 GDB_EscapeBinaryData(u32 *encodedCount, void *dst, const void *src, u32 len, u32 maxLen); -u32 GDB_UnescapeBinaryData(void *dst, const void *src, u32 len); -const char *GDB_ParseIntegerList(u32 *dst, const char *src, u32 nb, char sep, char lastSep, u32 base, bool allowPrefix); -const char *GDB_ParseHexIntegerList(u32 *dst, const char *src, u32 nb, char lastSep); -const char *GDB_ParseIntegerList64(u64 *dst, const char *src, u32 nb, char sep, char lastSep, u32 base, bool allowPrefix); -const char *GDB_ParseHexIntegerList64(u64 *dst, const char *src, u32 nb, char lastSep); +u8 GDB_ComputeChecksum(const char *packetData, size_t len); +void GDB_EncodeHex(char *dst, const void *src, size_t len); +u32 GDB_DecodeHex(void *dst, const char *src, size_t len); +u32 GDB_EscapeBinaryData(u32 *encodedCount, void *dst, const void *src, size_t len, size_t maxLen); +u32 GDB_UnescapeBinaryData(void *dst, const void *src, size_t len); + +const char *GDB_ParseIntegerList(unsigned long *dst, const char *src, size_t nb, char sep, char lastSep, u32 base, bool allowPrefix); +const char *GDB_ParseHexIntegerList(unsigned long *dst, const char *src, size_t nb, char lastSep); + int GDB_ReceivePacket(GDBContext *ctx); -int GDB_SendPacket(GDBContext *ctx, const char *packetData, u32 len); +int GDB_SendPacket(GDBContext *ctx, const char *packetData, size_t len); int GDB_SendFormattedPacket(GDBContext *ctx, const char *packetDataFmt, ...); -int GDB_SendHexPacket(GDBContext *ctx, const void *packetData, u32 len); -int GDB_SendStreamData(GDBContext *ctx, const char *streamData, u32 offset, u32 length, u32 totalSize, bool forceEmptyLast); +int GDB_SendHexPacket(GDBContext *ctx, const void *packetData, size_t len); +int GDB_SendStreamData(GDBContext *ctx, const char *streamData, size_t offset, size_t length, size_t totalSize, bool forceEmptyLast); int GDB_SendDebugString(GDBContext *ctx, const char *fmt, ...); // unsecure int GDB_ReplyEmpty(GDBContext *ctx); int GDB_ReplyOk(GDBContext *ctx);