copying unsigned char array to pointer - pointers

I am trying to hash the data string and then copy the result to a pointer in order to use it elsewhere (send it over a URL). Problem is that although hash is correct the pointer does not contain the correct hash.
int main(int argc, char const *argv[])
{
char data[] = "oNOH0hwZ3H6aduK5hnhxl5CI5C9v1EAghfO8maYfHUgKfQGn3NLeJdNEGNZyhqFpvjoIkcOSXUt2BZfvDQAcUeHqRxOZNke9ttHEvmmi7FbzugUXXl0HpxY7UDWyN1x6k5AGhNPfiQECXpqKAgiQE6NpzANcsb9D6AaedPklwOOk35Vtc3aGZOWoezrxBq1xR1CUHM63rLekGZEJSEgIi2XnreLSvCgctJX1mUVDvZO2OjBxOI6XATaSYMBjeImyh9puUaYfZC8ELA2p8YdyHdgvQIFUgRjoRyICzwHpZGUB6MR5BUu9YBvE90pfIytqX2SmoqCeXmFUZmPq79pVAKqzBFFa3YrRQa35rw9eIFYxSEOPE3B4ERu6m07gOoXvpRrGeqKMWzaE3OkxILsdtN9GD7MimAEBiV7mcIZY7Zt1EDpcfHfylf4PcHYp8sQhdNugmj4j9naDRpFWXLk9QeOTMC8KUPSYtc4FmZOldPOU4kGSVQRCVwmxYk8J0QyjTtO5ithm8W636CLSitl4QxsFIrfy7DIRXmM6FTiEFexCG9lPtwK9U2EsjJRhcpY0BBW7l4BR8ZjFYvlhRVgBNKUWjB4mRTcjkZgwTIdSxnnmIytqjASXbCKk4ExLnAUyp1U8AXRYa4aJtuZDUHqVa064vuGIUq69iR8IEPwFKxe4R4yBCPnCFkx1E3zokwosdn1I3odDLiytdWVFB887ivYMoo1yLfRPsJnlXrPzAeTD1E9tD7rLtgoIuf76lOLEoYQcgwCGAl9sP9Li73Uq98ZueWqqB2VPRBRXYjtpvtHasjjpc3Gc2vw7iNoJF9omB69pgsFBLdC4mLkpFRrycOvkrJUWJ98a57qcqWD2Z6XbI7rdOJCRnX2ExMq6Lp7HnoJDadwZaj1IgjMVSeC52uAp7QmJ6kgjzQNzT9pTihspq4b98D50YvgWctv8DCh2iUs2TIL2P4i6Yj5XN0OCmUooeKmHctAkeTcYrOQ6IZ2x9YkNPZf1KufO5rm7LMiP";
unsigned char hash[SHA512_DIGEST_LENGTH];
SHA512(data, sizeof(data) - 1, hash);
for(int i = 0; i < SHA512_DIGEST_LENGTH; ++i) {
printf("%02x", hash[i]);
}
unsigned char* phash = (unsigned char *)malloc(sizeof(hash));
memcpy(phash, hash, (sizeof(hash)));
printf("\nHash is: %p", *phash);
//some other code here
}
OUTPUT:a5274651e4573a7fea8dc1936a26a77a0667f600cad6c0afdd648431fa44b9e1e9128237b8012f103218f743cc422214701af609d22f9cbdf78e7bd5b374b87b (correct hash)
Hash is : 0x564e7a3dc670
What am I missing here?

Related

Freeing an array of pointer but heap block

I wrote a program that has to read a 2D array from a text file and save it into a double pointer which will act as a 2D array.
Here's the code :
#include <stdio.h>
#include <stdlib.h>
char **create_map(char* filename);
int n;
int m;
char **map;
int main(int argc, char* argv[]) {
int i;
map = create_map(argv[1]);
for(i = 0; i < n; i++) {
free(map[i]);
}
free(map);
return 0;
}
char **create_map(char *filename) {
int i = 0;
char *row;
char **map;
FILE *file = fopen(filename, "r");
fscanf(file, "%d %d", &n, &m);
map = malloc(sizeof(char *) * n);
row = malloc(sizeof(char)*m);
while(fscanf(file, "%s\n", row) != EOF) {
map[i] = malloc(sizeof(char)*m);
strcpy(map[i], row);
i++;
}
free(map[9]);
free(row);
fclose(file);
return map;
}
The content of the file is stored successfully in the map variable, but when it comes to freeing some space the debugger prints "warning: Heap block at 0000029967AF5770 modified at 0000029967AF578A past requested size of a".
Why the memory can't be freed?
Where's the error?
Thank you in advance.

How to retrieve an int array that is stored in a table using PROGMEM?

I'm new to Arduino and currently learn to use PROGMEM to store variables so that I can save dynamic memory. I have 13 variables including these three below that I store using PROGMEM.
Here are some of example of variables that I store and use it in my functions :-
const unsigned int raw_0[62] PROGMEM = {2600,850,400,500,400,500,450,850,450,850,1350,850,450,450,400,500,400,450,450,400,450,450,450,450,400,450,900,850,900,850,900,450,450,850,900,850,900,850,450,450,900,450,400,450,400,900,450,450,450,400,450,450,450,450,400,450,450,450,450,400,450,};
const unsigned int raw_1[60] PROGMEM = {2600,850,450,450,450,450,450,850,450,850,1350,850,500,400,450,400,450,450,450,450,400,450,450,450,400,450,900,850,900,900,850,450,450,850,850,900,900,900,400,450,900,450,450,400,450,850,450,450,450,450,400,450,450,450,450,400,450,450,850,};
const unsigned int raw_a[100] PROGMEM = {3500,1700,400,450,450,1250,450,400,450,400,450,400,500,400,450,400,450,400,450,400,450,450,400,400,500,400,450,400,450,1300,400,450,450,400,450,400,450,400,450,400,450,400,500,350,500,400,450,400,450,1300,400,400,500,400,450,400,450,400,450,450,400,450,450,400,450,400,450,400,450,400,450,450,400,450,450,400,450,1250,450,400,450,400,500,400,450,400,450,400,450,400,450,400,450,1300,450,400,450,1250,450,};
Here is the table that store the variables. I learn this approach from Arduino website; https://www.arduino.cc/en/Reference/PROGMEM .
const unsigned int* const myTable[13] PROGMEM = {
raw_0,
raw_1,
raw_2,
raw_3,
raw_4,
raw_5,
raw_6,
raw_7,
raw_8,
raw_9,
raw_a,
raw_b,
raw_c};
My problem is, how do I retrieve these variables using PROGMEM such as raw_1 and raw_a ?
This is what I did but it did not work :-
unsigned int * ptr = (unsigned int *) pgm_read_word (&myTable [1]);
irsend.sendRaw(ptr,62,38);
Most of examples that I found, they use String or char datatype but in my case, I use array integer.
The ptr is also pointer to PROGMEM, so you have to read the value (or values in this case) by pgm_read_word. The IR library doesn't support that at all (I hope it's the correct one).
Anyway sendRaw implementation is this:
void IRsend::sendRaw (const unsigned int buf[], unsigned int len, unsigned int hz)
{
// Set IR carrier frequency
enableIROut(hz);
for (unsigned int i = 0; i < len; i++) {
if (i & 1) space(buf[i]) ;
else mark (buf[i]) ;
}
space(0); // Always end with the LED off
}
And all used methods are public, so you can implement your own function to do the same:
void mySendRaw (IRsend & dev, const unsigned int buf[], unsigned int len, unsigned int khz)
{
// Set IR carrier frequency
dev.devenableIROut(khz);
for (unsigned int i = 0; i < len; i++) {
if (i & 1) dev.space(pgm_read_word(buf+i));
else dev.mark (pgm_read_word(buf+i));
}
dev.space(0); // Always end with the LED off
}
// And usage:
mySendRaw(irsend, (const uint16_t*)pgm_read_word(myTable+1), 62, 38);
However the size of arrays should be stored somewhere too, so you can use something like:
byte cmd = 1;
mySendRaw(irsend, (const uint16_t*)pgm_read_word(myTable+cmd), pgm_read_word(myTableLenghts+cmd), 38);

Arduino hexadecimal string to unsigned long int conversion

I'm trying to convert a hex decimal string value to an unsigned long int.
For example:
String s="0x4d14" --> unsigned long int B = 0x4D14
OK, I solved the previous problem,
I have another problem:
I read hexcodes from the serial monitor like this:
char c[10];
char c[i]=serial.read();
String s=c[i];
i++;
if (i==10)
s="";
But now I can't do this:
const char* string = s;
To use the strtoul function, so what should I do?
Whatever logic of my code, I didn't post my full code, but my problem now is string to const char* conversion.
This works for me
const char* string = "0x4d14";
long unsigned int b = strtoul(string, 0, 16);
Update:
char c[10];
... // read values
strtoul(c, 0, 16);
...
Have a look at some C language tutorial

How to create a Queue of unsigned char array in Qt?

I am new in Queue (FIFO) and Qt. I want to create a Queue of unsigned char array in Qt. How to do it? Please help
unsigned char buffer[1024];
If you want to use the Qt API, then you can use the QQueue class -
QQueue<unsigned char> queue;
queue.enqueue(65);
queue.enqueue(66);
queue.enqueue(67);
while (!queue.isEmpty())
cout << queue.dequeue() << endl;
If you want to build the queue on your own, then I guess you can declare a Queue class like this -
class Queue
{
private:
enum{SIZE=1024, EMPTY=0};
unsigned char buffer[SIZE];
int readHead, writeHead;
public:
Queue()
{
readHead = writeHead = EMPTY;
}
void push(unsigned char data);
unsigned char pop();
unsigned char peek();
bool isEmpty();
};
void Queue::push(unsigned char data)
{
if((readHead - writeHead) >= SIZE)
{
// You should handle Queue overflow the way you want here.
return;
}
buffer[writeHead++ % SIZE] = data;
}
unsigned char Queue::pop()
{
unsigned char item = peek();
readHead++;
return item;
}
unsigned char Queue::peek()
{
if(isEmpty())
{
// You should handle Queue underflow the way you want here.
return;
}
return buffer[readHead % SIZE];
}
bool Queue::isEmpty()
{
return (readHead == writeHead);
}
If you want to maintain a Queue of unsigned char array, then you will have to maintain a queue of unsigned char pointers -
QQueue<unsigned char *> queue;
unsigned char *array1 = new unsigned char[10]; // array of 10 items
array1[0] = 65;
array1[1] = 66;
queue.enqueue(array1);
unsigned char *array2 = new unsigned char[20]; // an array of 20 items
queue.enqueue(array2);
unsigned char *arr = queue.dequeue();
qDebug() << arr[0] << ", " << arr[1];
Note: You should take care of the memory cleanup after you are done with this queue. IMHO, you better avoid this type of design though.

QByteArray convert to/from unsigned char *

QByteArray inArray = " ... ";
unsigned char *in = convert1(inArray);
unsigned char *out;
someFunction(in, out);
QByteArray outArray = convert2(out);
the question is how can I correctly make these conversions (convert1 and convert2).
I cannot change someFunction(unsigned char *, unsigned char *), but I have to work with QByteArray here.
Qt has really great docs, you should use them.
If someFunction doesn't modify or store pointer to in data you can use this:
QByteArray inArray = " ... ";
unsigned char *out;
someFunction((unsigned char*)(inArray.data()), out);
QByteArray outArray((char*)out);
Otherwise you have to make a deep copy of the char* returned by QByteArray::data() (see the docs for code snippet).
if someFunction takes a const char* args then just use ConstData() or data() in QByteArray class.
if you need a char*, you can then use strdup(). This method is doing this
char *strdup (const char *s) {
char *d = malloc (strlen (s) + 1); // Space for length plus nul
if (d == NULL) return NULL; // No memory
strcpy (d,s); // Copy the characters
return d; // Return the new string
}
more info here: strdup() - what does it do in C?

Resources