From 3f97b6f6c344500e0005063d1580afdb32c333e4 Mon Sep 17 00:00:00 2001 From: midipix Date: Sat, 27 Apr 2024 19:48:23 +0000 Subject: mdso_create_implib_objects(): eliminate the use of sprintf(). --- project/common.mk | 1 + project/headers.mk | 1 + src/internal/mdso_hexfmt_impl.c | 35 ++++++++++++++++++++++ src/internal/mdso_hexfmt_impl.h | 14 +++++++++ src/util/mdso_create_implib_objects.c | 55 +++++++++++++++++++++++++++-------- 5 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 src/internal/mdso_hexfmt_impl.c create mode 100644 src/internal/mdso_hexfmt_impl.h diff --git a/project/common.mk b/project/common.mk index a52f593..95f765d 100644 --- a/project/common.mk +++ b/project/common.mk @@ -22,6 +22,7 @@ API_SRCS = \ INTERNAL_SRCS = \ src/internal/$(PACKAGE)_dprintf_impl.c \ src/internal/$(PACKAGE)_errinfo_impl.c \ + src/internal/$(PACKAGE)_hexfmt_impl.c \ APP_SRCS = \ src/mdso.c diff --git a/project/headers.mk b/project/headers.mk index a238cfe..d4a351a 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -14,6 +14,7 @@ INTERNAL_HEADERS = \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_dprintf_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_driver_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_errinfo_impl.h \ + $(PROJECT_DIR)/src/internal/$(PACKAGE)_hexfmt_impl.h \ $(PROJECT_DIR)/src/internal/$(PACKAGE)_object_impl.h \ ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS) diff --git a/src/internal/mdso_hexfmt_impl.c b/src/internal/mdso_hexfmt_impl.c new file mode 100644 index 0000000..33d337b --- /dev/null +++ b/src/internal/mdso_hexfmt_impl.c @@ -0,0 +1,35 @@ +/****************************************************************/ +/* mdso: midipix dso scavenger */ +/* Copyright (C) 2015--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.MDSO. */ +/****************************************************************/ + +#include + +char buf[256] = {0}; + +static unsigned char mdso_hexfmt[16] = { + '0','1','2','3','4','5','6','7', + '8','9','a','b','c','d','e','f', +}; + +void mdso_write_hex_64(char * ch, uint64_t val) +{ + ch[0xf] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0xe] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0xd] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0xc] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0xb] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0xa] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x9] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x8] = mdso_hexfmt[val % 16]; val >>= 4; + + ch[0x7] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x6] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x5] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x4] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x3] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x2] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x1] = mdso_hexfmt[val % 16]; val >>= 4; + ch[0x0] = mdso_hexfmt[val % 16]; val >>= 4; +} diff --git a/src/internal/mdso_hexfmt_impl.h b/src/internal/mdso_hexfmt_impl.h new file mode 100644 index 0000000..1ad74d1 --- /dev/null +++ b/src/internal/mdso_hexfmt_impl.h @@ -0,0 +1,14 @@ +/****************************************************************/ +/* mdso: midipix dso scavenger */ +/* Copyright (C) 2015--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.MDSO. */ +/****************************************************************/ + +#ifndef MDSO_HEXFMT_IMPL_H +#define MDSO_HEXFMT_IMPL_H + +#include + +void mdso_write_hex_64(char * ch, uint64_t val); + +#endif diff --git a/src/util/mdso_create_implib_objects.c b/src/util/mdso_create_implib_objects.c index 0da806d..d165c34 100644 --- a/src/util/mdso_create_implib_objects.c +++ b/src/util/mdso_create_implib_objects.c @@ -9,19 +9,50 @@ #include #include +#include "mdso_hexfmt_impl.h" #include "mdso_errinfo_impl.h" -static void mdso_init_objname(char * buf, const char * fmt, const char * str) +static void mdso_init_symentry(char * ch, const char * str) { - char hexstr[24]; - long long unsigned int crc64; - - if (strlen(str) + strlen(fmt) > (PATH_MAX - 1)) { - crc64 = mdso_crc64_mbstr((const unsigned char *)str,0); - sprintf(hexstr,"%llx",crc64); - sprintf(buf,fmt,hexstr); - } else - sprintf(buf,fmt,str); + uint64_t crc64; + unsigned slen = 11; + + ch[0] = '.'; + ch++; + + if (strlen(str) > (PATH_MAX - slen - 1)) { + crc64 = mdso_crc64_mbstr((unsigned char *)str,0); + mdso_write_hex_64(ch,crc64); + ch = &ch[16]; + } else { + for (; *str; ) + *ch++ = *str++; + } + + memcpy(ch,"_symentry.o",slen); + ch[slen] = '\0'; +} + +static void mdso_init_dsometa(char * ch, const char * str) +{ + uint64_t crc64; + unsigned slen = 9; + + memcpy(ch,".dsometa_",slen); + ch = &ch[slen]; + + if (strlen(str) > (PATH_MAX - slen - 1)) { + crc64 = mdso_crc64_mbstr((unsigned char *)str,0); + mdso_write_hex_64(ch,crc64); + ch = &ch[16]; + } else { + for (; *str; ) + *ch++ = *str++; + } + + ch[0] = '.'; + ch[1] = 'o'; + ch[2] = '\0'; } static void mdso_init_object(struct mdso_object * obj, const char * objname) @@ -44,7 +75,7 @@ int mdso_create_implib_objects(const struct mdso_driver_ctx * dctx) return MDSO_NESTED_ERROR(dctx); for (sym=uctx->syms; *sym; sym++) { - mdso_init_objname(objname,".%s_symentry.o",*sym); + mdso_init_symentry(objname,*sym); mdso_init_object(&obj,objname); if (mdso_objgen_symentry(dctx,*sym,&obj) < 0) @@ -55,7 +86,7 @@ int mdso_create_implib_objects(const struct mdso_driver_ctx * dctx) } /* dsometa */ - mdso_init_objname(objname,".dsometa_%s.o",dctx->cctx->libname); + mdso_init_dsometa(objname,dctx->cctx->libname); mdso_init_object(&obj,objname); if (mdso_objgen_dsometa(dctx,&obj) < 0) -- cgit v1.2.3