diff --git a/selfdrive/common/util.cc b/selfdrive/common/util.cc index 40f6566969..63e3f6fa00 100644 --- a/selfdrive/common/util.cc +++ b/selfdrive/common/util.cc @@ -194,22 +194,13 @@ float getenv(const char* key, float default_val) { return val ? atof(val) : default_val; } -std::string tohex(const uint8_t *buf, size_t buf_size) { - std::unique_ptr hexbuf(new char[buf_size * 2 + 1]); - for (size_t i = 0; i < buf_size; i++) { - sprintf(&hexbuf[i * 2], "%02x", buf[i]); +std::string hexdump(const uint8_t* in, const size_t size) { + std::stringstream ss; + ss << std::hex << std::setfill('0'); + for (size_t i = 0; i < size; i++) { + ss << std::setw(2) << static_cast(in[i]); } - hexbuf[buf_size * 2] = 0; - return std::string(hexbuf.get(), hexbuf.get() + buf_size * 2); -} - -std::string hexdump(const std::string& in) { - std::stringstream ss; - ss << std::hex << std::setfill('0'); - for (size_t i = 0; i < in.size(); i++) { - ss << std::setw(2) << static_cast(static_cast(in[i])); - } - return ss.str(); + return ss.str(); } std::string dir_name(std::string const &path) { diff --git a/selfdrive/common/util.h b/selfdrive/common/util.h index aacb7af3b9..4f6d4fa19b 100644 --- a/selfdrive/common/util.h +++ b/selfdrive/common/util.h @@ -66,8 +66,7 @@ std::string getenv(const char* key, const char* default_val = ""); int getenv(const char* key, int default_val); float getenv(const char* key, float default_val); -std::string tohex(const uint8_t* buf, size_t buf_size); -std::string hexdump(const std::string& in); +std::string hexdump(const uint8_t* in, const size_t size); std::string dir_name(std::string const& path); // **** file fhelpers ***** diff --git a/selfdrive/modeld/thneed/thneed.cc b/selfdrive/modeld/thneed/thneed.cc index 242905d9f4..a3588f1d1f 100644 --- a/selfdrive/modeld/thneed/thneed.cc +++ b/selfdrive/modeld/thneed/thneed.cc @@ -11,7 +11,7 @@ #include "selfdrive/common/clutil.h" #include "selfdrive/common/timing.h" - +#include "selfdrive/common/util.h" //#define RUN_DISASSEMBLER //#define RUN_OPTIMIZER @@ -21,14 +21,10 @@ map, string> g_args; map, int> g_args_size; map g_program_source; -void hexdump(uint32_t *d, int len) { +void hexdump(uint8_t *d, int len) { assert((len%4) == 0); printf(" dumping %p len 0x%x\n", d, len); - for (int i = 0; i < len/4; i++) { - if (i != 0 && (i%0x10) == 0) printf("\n"); - printf("%8x ", d[i]); - } - printf("\n"); + printf("%s\n", util::hexdump(d, len).c_str()); } // *********** ioctl interceptor *********** @@ -94,10 +90,10 @@ int ioctl(int filedes, unsigned long request, void *argp) { struct kgsl_device_getproperty *prop = (struct kgsl_device_getproperty *)argp; printf("IOCTL_KGSL_SETPROPERTY: 0x%x sizebytes:%zu\n", prop->type, prop->sizebytes); if (thneed->record & THNEED_VERBOSE_DEBUG) { - hexdump((uint32_t *)prop->value, prop->sizebytes); + hexdump((uint8_t *)prop->value, prop->sizebytes); if (prop->type == KGSL_PROP_PWR_CONSTRAINT) { struct kgsl_device_constraint *constraint = (struct kgsl_device_constraint *)prop->value; - hexdump((uint32_t *)constraint->data, constraint->size); + hexdump((uint8_t *)constraint->data, constraint->size); } } } diff --git a/selfdrive/ui/replay/util.cc b/selfdrive/ui/replay/util.cc index 0560571474..b4fcbf7e6b 100644 --- a/selfdrive/ui/replay/util.cc +++ b/selfdrive/ui/replay/util.cc @@ -9,7 +9,6 @@ #include #include #include -#include #include "selfdrive/common/timing.h" #include "selfdrive/common/util.h" @@ -216,9 +215,5 @@ std::string sha256(const std::string &str) { SHA256_Init(&sha256); SHA256_Update(&sha256, str.c_str(), str.size()); SHA256_Final(hash, &sha256); - std::stringstream ss; - for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { - ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; - } - return ss.str(); + return util::hexdump(hash, SHA256_DIGEST_LENGTH); }