From: Pierre Ossman Date: Thu, 17 Nov 2022 14:04:20 +0000 (+0100) Subject: Move hex conversion helpers to util X-Git-Tag: v1.13.90~87^2~31 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=07e541678ca8c55c39780f7960c559a3e9c7ee1e;p=tigervnc.git Move hex conversion helpers to util These are used here and there so let's make them more general rather than hiding them in the stream classes. --- diff --git a/common/rdr/HexInStream.cxx b/common/rdr/HexInStream.cxx index 8a88d337..e23974e1 100644 --- a/common/rdr/HexInStream.cxx +++ b/common/rdr/HexInStream.cxx @@ -1,4 +1,5 @@ /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. + * Copyright 2019-2022 Pierre Ossman for Cendio AB * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,9 +23,7 @@ #include #include - -#include -#include +#include using namespace rdr; @@ -39,43 +38,6 @@ HexInStream::~HexInStream() { } -bool HexInStream::readHexAndShift(char c, int* v) { - c=tolower(c); - if ((c >= '0') && (c <= '9')) - *v = (*v << 4) + (c - '0'); - else if ((c >= 'a') && (c <= 'f')) - *v = (*v << 4) + (c - 'a' + 10); - else - return false; - return true; -} - -bool HexInStream::hexStrToBin(const char* s, char** data, size_t* length) { - size_t l=strlen(s); - if ((l % 2) == 0) { - delete [] *data; - *data = 0; *length = 0; - if (l == 0) - return true; - *data = new char[l/2]; - *length = l/2; - for(size_t i=0;i -#include +#include using namespace rdr; @@ -36,39 +37,14 @@ HexOutStream::~HexOutStream() { } -char HexOutStream::intToHex(int i) { - if ((i>=0) && (i<=9)) - return '0'+i; - else if ((i>=10) && (i<=15)) - return 'a'+(i-10); - else - throw rdr::Exception("intToHex failed"); -} - -char* HexOutStream::binToHexStr(const char* data, size_t length) { - char* buffer = new char[length*2+1]; - for (size_t i=0; i> 4) & 15); - buffer[i*2+1] = intToHex((data[i] & 15)); - if (!buffer[i*2] || !buffer[i*2+1]) { - delete [] buffer; - return 0; - } - } - buffer[length*2] = 0; - return buffer; -} - bool HexOutStream::flushBuffer() { while (sentUpTo != ptr) { uint8_t* optr = out_stream.getptr(2); size_t length = min(ptr-sentUpTo, out_stream.avail()/2); - for (size_t i=0; i> 4) & 0xf); - optr[i*2+1] = intToHex(sentUpTo[i] & 0xf); - } + for (size_t i=0; i #endif +#include +#include #include #include #include @@ -112,6 +114,79 @@ namespace rfb { dest[src ? destlen-1 : 0] = 0; } + static char intToHex(uint8_t i) { + if (i<=9) + return '0'+i; + else if ((i>=10) && (i<=15)) + return 'a'+(i-10); + assert(false); + return '\0'; + } + + void binToHex(const uint8_t* in, size_t inlen, + char* out, size_t outlen) { + if (inlen > outlen/2) + inlen = outlen/2; + + if (inlen > 0) { + assert(in); + assert(out); + } + + for (size_t i=0; i> 4) & 15); + out[i*2+1] = intToHex((in[i] & 15)); + } + } + + char* binToHex(const uint8_t* in, size_t inlen) { + char* out = new char[inlen*2+1](); + binToHex(in, inlen, out, inlen*2); + return out; + } + + static bool readHexAndShift(char c, uint8_t* v) { + c=tolower(c); + if ((c >= '0') && (c <= '9')) + *v = (*v << 4) + (c - '0'); + else if ((c >= 'a') && (c <= 'f')) + *v = (*v << 4) + (c - 'a' + 10); + else + return false; + return true; + } + + bool hexToBin(const char* in, size_t inlen, + uint8_t* out, size_t outlen) { + assert(in); + assert(out); + + if (inlen & 1) + return false; + + if (inlen > outlen*2) + inlen = outlen*2; + + for(size_t i=0; i #include +#include struct timeval; @@ -73,6 +74,13 @@ namespace rfb { // Copies src to dest, up to specified length-1, and guarantees termination void strCopy(char* dest, const char* src, int destlen); + // Conversion to and from a hex string + + void binToHex(const uint8_t* in, size_t inlen, char* out, size_t outlen); + char* binToHex(const uint8_t* in, size_t inlen); + bool hexToBin(const char* in, size_t inlen, uint8_t* out, size_t outlen); + uint8_t* hexToBin(const char* in, size_t inlen); + // Makes sure line endings are in a certain format char* convertLF(const char* src, size_t bytes = (size_t)-1); diff --git a/win/rfb_win32/Registry.cxx b/win/rfb_win32/Registry.cxx index f046188f..cb4e6688 100644 --- a/win/rfb_win32/Registry.cxx +++ b/win/rfb_win32/Registry.cxx @@ -175,7 +175,9 @@ TCHAR* RegKey::getString(const TCHAR* valname, const TCHAR* def) const { void RegKey::getBinary(const TCHAR* valname, void** data, size_t* length) const { TCharArray hex(getRepresentation(valname)); - if (!rdr::HexInStream::hexStrToBin(CStr(hex.buf), (char**)data, length)) + *data = hexToBin(CStr(hex.buf), strlen(CStr(hex.buf))); + *length = strlen(CStr(hex.buf))/2; + if (*data == NULL) throw rdr::Exception("getBinary failed"); } void RegKey::getBinary(const TCHAR* valname, void** data, size_t* length, void* def, size_t deflen) const { @@ -233,7 +235,7 @@ TCHAR* RegKey::getRepresentation(const TCHAR* valname) const { switch (type) { case REG_BINARY: { - TCharArray hex(rdr::HexOutStream::binToHexStr(data.buf, length)); + TCharArray hex(binToHex((const uint8_t*)data.buf, length)); return hex.takeBuf(); } case REG_SZ: diff --git a/win/rfb_win32/Win32Util.cxx b/win/rfb_win32/Win32Util.cxx index fc66274f..2fbce0dd 100644 --- a/win/rfb_win32/Win32Util.cxx +++ b/win/rfb_win32/Win32Util.cxx @@ -61,13 +61,13 @@ FileVersionInfo::FileVersionInfo(const TCHAR* filename) { } const TCHAR* FileVersionInfo::getVerString(const TCHAR* name, DWORD langId) { - char langIdBuf[sizeof(langId)]; + uint8_t langIdBuf[sizeof(langId)]; for (int i=sizeof(langIdBuf)-1; i>=0; i--) { - langIdBuf[i] = (char) (langId & 0xff); + langIdBuf[i] = (langId & 0xff); langId = langId >> 8; } - TCharArray langIdStr(rdr::HexOutStream::binToHexStr(langIdBuf, sizeof(langId))); + TCharArray langIdStr(binToHex(langIdBuf, sizeof(langId))); TCharArray infoName(_tcslen(_T("StringFileInfo")) + 4 + _tcslen(name) + _tcslen(langIdStr.buf)); _stprintf(infoName.buf, _T("\\StringFileInfo\\%s\\%s"), langIdStr.buf, name);