I am trying to pass a C char array to a function via its pointer and then accessing the char array in the function. At the moment I can only seem to access the first character and the rest is just random bytes. I have been trying to check the variables before and after the function call and they are all correct before the function call.
I think this question has the answer since it refers to dereferencing and only retaining the first character but when I try and implement the solution I am still getting a single character and not the whole string.
This is the code I have:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
void hex_to_bytes(char *hex, char **bytes, size_t *bytes_len) {
int length = strlen(hex) % 2 ? strlen(hex) + 1 : strlen(hex);
unsigned char checkedHex[length];
for (int i = 0; i <= length; i++) {
if (i == 0 && strlen(hex) % 2){
checkedHex[0] = '0';
}
else if (i == length){
checkedHex[i] = '\x00';
}
else {
checkedHex[i] = hex[i];
}
}
*bytes_len = length / 2;
int counter = 0;
for (int i = 0; i < length / 2; i++){
sscanf(&checkedHex[counter], "%2hhx", &bytes[i]);
counter += 2;
}
}
void *b64_encode(unsigned char *bytes, size_t bytes_len)
{
printf("in b64_encode %p\n", bytes);
printf("bytes = %s\n", &bytes[48]);
printf("0x");
for (int i = 0; i < bytes_len; i++) {
printf("%02X", bytes[i]);
}
printf("\n");
}
int main() {
unsigned char hex[] = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
unsigned char *bytes[48] = {0};
size_t bytes_len = 0;
hex_to_bytes(hex, (unsigned char **) &bytes, &bytes_len);
printf("in main %p\n", &bytes);
printf("bytes = %s\n", &bytes[48]);
unsigned char *base64 = b64_encode(bytes, bytes_len);
return 1;
}
The output I get is:
in main 0x7ff7b0ef1350
bytes = 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
in b64_encode 0x7ff7b0ef1350
bytes = l
0x490000000000000027000000000000006D0000000000000020000000000000006B000000000000006900000000000000
>Solution :
unsigned char *bytes[48] is an array of pointers, not an array of chars. Get rid of the *.
And, in hex_to_bytes(), change the bytes parameter from char** to char*. You don’t need a pointer to a pointer.
Try this:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
void hex_to_bytes(char *hex, char *bytes, size_t *bytes_len) {
int length = strlen(hex);
if (length % 2) ++length;
unsigned char checkedHex[length];
for (int i = 0; i <= length; i++) {
if (i == 0 && strlen(hex) % 2){
checkedHex[0] = '0';
}
else if (i == length){
checkedHex[i] = '\x00';
}
else {
checkedHex[i] = hex[i];
}
}
*bytes_len = length / 2;
int counter = 0;
for (int i = 0; i < length / 2; i++){
sscanf(&checkedHex[counter], "%2hhx", &bytes[i]);
counter += 2;
}
}
void b64_encode(unsigned char *bytes, size_t bytes_len)
{
printf("in b64_encode %p\n", bytes);
printf("bytes = %.*s\n", bytes_len, bytes);
printf("0x");
for (int i = 0; i < bytes_len; i++) {
printf("%02X", bytes[i]);
}
printf("\n");
}
int main() {
unsigned char hex[] = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
unsigned char bytes[48] = {0};
size_t bytes_len = 0;
hex_to_bytes(hex, bytes, &bytes_len);
printf("in main %p\n", bytes);
printf("bytes = %.*s\n", bytes_len, bytes);
b64_encode(bytes, bytes_len);
return 1;
}