Avatar of magdiel linhares
magdiel linhares
 asked on

CONST CHAR* buf TO BYTE*!

I am trying to do a cryptography, but to no avail .... the code does not compile. How do I convert const char * buf to BYTE? Or is there another way?


#include <cstdio>
 
typedef unsigned char BYTE;
 
BYTE server_keys[2][256] = {
	{
		0xFC, 0x77, 0xA1, 0x85, 0x1F, 0x30, 0x51, 0x20, 0x93, 0x4A, 0xE3, 0x10, 0x0E, 0x32, 0x58,
		0x64, 0x36, 0x8C, 0x19, 0xF0, 0x61, 0xE0, 0xDF, 0x9E, 0x9F, 0x90, 0xD0, 0x05, 0xFA, 0xEB,
		0x3D, 0x4B, 0xA5, 0xF1, 0x72, 0x73, 0xD4, 0xB5, 0x70, 0xD7, 0xCD, 0x9A, 0x95, 0x2B, 0xC9,
		0x00, 0x8E, 0xAC, 0x35, 0x1B, 0xE7, 0x7B, 0xC3, 0x15, 0x11, 0xF6, 0xAD, 0x5B, 0x87, 0x86,
		0xB4, 0x9B, 0x26, 0xDB, 0xDE, 0x1C, 0x66, 0x24, 0xD8, 0x27, 0x6A, 0xBD, 0x5D, 0x8D, 0x7D,
		0x0D, 0xAE, 0x8B, 0xA9, 0x62, 0x6B, 0x0B, 0xE2, 0x5C, 0x6C, 0xBE, 0x54, 0x55, 0x6E, 0xBF,
		0x3F, 0x88, 0x99, 0xB0, 0x48, 0x16, 0x5A, 0x34, 0xA6, 0xE8, 0xFD, 0xD3, 0xE4, 0x82, 0xD6,
		0x8A, 0x3B, 0xFE, 0xA4, 0x94, 0xF8, 0x06, 0x97, 0xCB, 0xF5, 0x33, 0x79, 0xD9, 0x83, 0x4F,
		0xB1, 0xAB, 0xA2, 0x69, 0x91, 0xFF, 0xC6, 0x2C, 0x68, 0xC1, 0xAA, 0xC4, 0x1D, 0x18, 0x3A,
		0xBC, 0x04, 0x2F, 0xA3, 0xFB, 0x17, 0x89, 0x25, 0x02, 0xCF, 0xDD, 0x2D, 0x6D, 0xC5, 0xC2,
		0x46, 0x01, 0xE5, 0xED, 0x2E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 0x42, 0xC1, 0x80,
		0x7B, 0x46, 0x50, 0x54, 0x93, 0x58, 0xB3, 0x26, 0xF5, 0x22, 0x23, 0xF2, 0xB4, 0xBD, 0x01,
		0xAC, 0x74, 0xD7, 0x33, 0xA2, 0x4A, 0xE6, 0xD1, 0xDF, 0xD5, 0x67, 0x76, 0xB7, 0x03, 0x3B,
		0x3A, 0x5B, 0x8D, 0x69, 0x4D, 0x11, 0x49, 0x2E, 0xEC, 0x19, 0x7C, 0xBE, 0x08, 0x6D, 0x2A,
		0xBA, 0x70, 0xE1, 0x5C, 0x29, 0x3D, 0xB8, 0xDD, 0x17, 0x18, 0xE0, 0x02, 0x7A, 0x8A, 0x6C,
		0x20, 0x62, 0xE3, 0xCC, 0x4E, 0x82, 0x79, 0x2F, 0x38, 0x4C, 0xF8, 0x5D, 0x78, 0xF1, 0xE2,
		0x3C, 0x25, 0xA0, 0xF7, 0xC3, 0xA9, 0xFF, 0xEB, 0x87, 0x47, 0x55, 0x59, 0xAE, 0x81, 0x95,
		0x34, 0x83, 0x94, 0x7E, 0xD2, 0x9F, 0x2C, 0xC5, 0x71, 0xDA, 0x28, 0xED, 0x90, 0x1A, 0xFE,
		0xFC, 0x65, 0x24, 0xD0, 0x68, 0x27, 0x44, 0x75, 0x9B, 0x3F, 0xE9, 0x91, 0x40, 0x16, 0x15,
		0xC7, 0x52, 0x0A, 0x66, 0x98, 0xF0, 0x32, 0x63, 0xA1, 0xB5, 0x1D, 0xAF, 0x99, 0xCB, 0xB0,
		0x13, 0x21, 0xC0, 0xA4, 0xEF, 0x72, 0x37, 0xD6, 0x6E, 0xA7, 0x1C, 0x8B, 0x00, 0x64, 0x6B,
		0x7D
	}
};
 

typedef enum PROCESS_TYPE
{
	ENCRYPT = 0,
	DECRYPT = 1
};
 
const int KEYS_NUMBER = 256;
 
inline void process_data(const PROCESS_TYPE type, const BYTE(*keys)[KEYS_NUMBER], BYTE *pInData, BYTE *pOutData, const unsigned int uLen)
{
	for (unsigned int i = 0; i < uLen; ++i)
	{
		pOutData[i] = keys[type][ pInData[i] ];
	}
}
 
void printData(BYTE *data, int len)
{
    for (int i=0; i<len; i++)
        printf("0x%02X ", data[i]);
 
    printf("\n");
}

Open in new window


i call crypto here:

void process_send(SOCKET s, const char* buf, int *len, int flags) {
	unsigned int command = (*(unsigned short*)buf);
		
			process_data(ENCRYPT, server_keys, buf, buf,*len);
		}

}

Open in new window


return this error:

Error	1	error C2664: 'process_data' : cannot convert parameter 3 from 'const char *' to 'BYTE *'

Open in new window




I cannot seem to adapt this... please help me :(
System ProgrammingC++

Avatar of undefined
Last Comment
sarabande

8/22/2022 - Mon
Fabrice Lambert

Well ... first, unless you have a very valid reason, never use those jurrassic C-Style arrays in C++.
The type std::array exist for a reason, and is more safe, less error prone and don't decay to troublemaking raw pointers.

Concerning your issue:
Did you try static_cast ?
Zoppo

Yes, this error is correct, the buf passed to the function is a const char *, in words this means it's a pointer to const char, so the compiler has to throw an error in case the type is (implicit) changed to a none-const type.

This is good and correct, because when I call a function and pass a string to it I usually expect the string stays unchanged. Further there are cases where changing such a const char can lead to crash, i.e. when such a pointer is initialized using a string literal, i.e.:
const char* p = "hello world.";
char* p1 = (char*)p; // cast the 'const' away
p1[0] = 'H'

Open in new window

I.e. compiled with VisualStudio this crashs due to an access violation because the compiler puts those string literals into memory segments which are read-only.

This even demonstrates that just casting away the const is not a good idea.

A type-safe solution would be to remove the const from the function declaration:
void process_send(SOCKET s, char* buf, int *len, int flags) { ... }

Open in new window

Of course this will cause compile errors in cases where this function is called with a const char *, you have to fix these calls in another way, ensuring no real const texts are passed.

Const-ness is an important aspect in C/C++ and should always be considered.

Hope that helps,

ZOPPO
magdiel linhares

ASKER
Hey Zoppo, i changed my cod to


void process_send(SOCKET s, char* buf, int *len, int flags) {

Open in new window


How can I convert from char* to BYTE*?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Fabrice Lambert

typedef unsigned char BYTE;

void myFunction(BYTE* in, size_t length);

/////////// MAIN ///////////
int main()
{
	
	char data[10];
	myFunction((BYTE*)data, 10);
	return 0;
}

void myFunction(BYTE* in, size_t length)
{

}

Open in new window

But beware that if you persist with C-style arrays, you're running yourself into troubles.
Zoppo

>> How can I convert from char* to BYTE*?

You can simply cast is, i.e.:

char* text = strdup( "Hello world." );
BYTE* data = (BYTE*)text;


BTW: Just for interest I'd like to ask if there is any reason why you don't use any C++ functionality? Except the leading #include <cdstdio> everything seems to be pure C. Therefor I wrote the code I posted above in the same way, but the complete problem could probably be solved better/nicer/safer when using standard C++ functionalities and STL.

Best regards,

ZOPPO
sarabande

never use those jurrassic C-Style arrays in C++.
that is not true in general. for example there is no better way in c++ to initialize  arrays with plain data like the crypto tables but to use c arrays.

and if those arrays are constant there is no reason to convert to std::array or std::vector.

if you need to change values you can combine both:

static BYTE keys[] = { 0x12, 0x34, 0xab, 0x00, ... };
static size_t NUMKEYS = sizeof(keys)/sizeof(keys[0]));
std::vector<BYTE> vkeys(&keys[0], &keys[NUMKEYS]);

Open in new window


here the vkeys uses the fact that pointers to plain c arrays can be used as iterators for to copy the static data ino dynamic c++ template arrays.

Sara
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sarabande

but the complete problem could probably be solved better/nicer/safer when using standard C++ functionalities and STL.

the sample code is too short to decide whether c++ classes and STL give advantage. for example socket programming is highly made in c and using c++ types would require mcuh more efforts to convert from c++ to c types and reverse as if all is done with c types. that must not mean that you would use the ansi c compiler but simply use c++ where it fits better and use c where you have to provide data for a c interface.

Sara
sarabande

void process_send(SOCKET s, char* buf, int *len, int flags) {

How can I convert from char* to BYTE*?

as Zoppo showed in his sample code you simply could cast the char* to BYTE* because char size is 1 byte same as BYTE which is typedef'd as

typedef unsigned char BYTE;

Open in new window


i. e. a BYTE simply is an unsigned char what is an 8-bit unsigned integer (range 0 to 255) while char can be used as a signed 8-bit integer (if not used for text) where the highest bit (bit 7) is used as sign bit and the range is from -128 to +127.

void process_send(SOCKET s, char* buf, int *len, int flags) 
{
      BYTE * pbuf = (BYTE*)buf;  
      // now pbuf can be used wherever a writeable BYTE array was required
      // alternatively use buf if a char* or const char* is required
      // not 'len' should be the size of the buffer and not the length of any text within the buffer
      ...

Open in new window


Sara
ASKER CERTIFIED SOLUTION
Fabrice Lambert

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sarabande

the code you posted is c++-11 extension where you cannot rely on that it was available at the user's environment.

the initialization of c structures and arrays also would work for 2d and 3 d arrays and for complex and even nested structures.

you may try to initialize a std::array as it was done iin the original posted code for variable

     
BYTE server_keys[2][256]

Open in new window


i personally don't use c++-11 enhancements (so it could well be that i am wrong) but have doubts that they are as mighty as c initializations.

Sara
Your help has saved me hundreds of hours of internet surfing.
fblack61