Im reading from dev/random and storing the binary data into an array.
When I write the array into my log its displayed as I want:
DebugLogMsg10 (pDebugLogger, sizeThreadID, "login failed->%s", UserSession.szToken);
But when I put it into my page build function call (the second parameter is expecting char * as pointer to the string which I want to be set as cookie.
PutMainLogIn (UsersAuthentication, UserSession.szToken, &requestFcgx);
If I do for example:
PutMainLogIn (UsersAuthentication, "testtest", &requestFcgx);
It is also working fine and a cookie with the value testtest is set.
So What is the problem?
Is it not allowed to set binary data as cookie? Or Am I missunderstanding something?
ps:
this is the structure looking like:
typedef struct Sesseion_s
{
size_t sizeID;
char szToken[TOKEN_LEN];
} Sesseion_t;
And this is the function prototype:
void PutMainLogIn (UsersAuth_t pUserData, char *szToken, FCGX_Request *Fcgx_Request)
Related
I am trying to write "any" kind of struct to a void array (Basically I'm trying to define an arraylist in C for any kind of datatype. I know that implementations already exist but I'm trying to learn because I'm kind of a beginner in C). I know how big the "data to add" is and I have the location in memory and know where the data should be placed. This is the current function that I came up with it but it doesn't work:
void addData(List *c, void* Item) {
c->data = realloc(c->data, c->amountOfItems+1);
((char*)c->data)[c->dataTypeInBytes * c->amountOfItems - c->dataTypeInBytes] = Item;
c->amountOfItems++;
}
c-> Data is a void malloc pointer. void* Item is the item to be added.
Basically, I try to put the Item value at the last (char*) location in c->data.
What exactly am I doing wrong? Also, what would be the best approach to get the data again from the allocated memory? My approach would be to convert c->data to a char* array and go to c->data[c->dataTypeInBytes * c->amountOfItems - c->dataTypeInBytes] and get c->dataTypeInBytes bytes. put each of those bytes in a char array with c->dataTypeInBytes length and return a void pointer to that, which then gets casted to the correct datatype outside the function. Would there be a better way to do that or straight up "define" a datatype with a specific size in Bits?
I am new in C & I am currently working with structures. I am stuck & I would be very grateful if somebody came to my rescue.
So I have a function to write input data from the main module to the sub-module.
It should return 0 if it writes successfully, & 1 if it doesn't succeed.
I have been reading widely but I am still unsure whether to use memcpy or not.
Then, on the return value I have read that memcpy returns 0 if the operation was successful and 1 if it was not. Here is that part of my code:
typedef struct {
UINT32 carnum_n;
UINT32 carnum_k;
UINT32 results[30];
UINT32 feedback[30];
UINT32 errors[30];
} MAIN;
typedef struct {
UINT32 carnum_n;
UINT32 carnum_k;
UINT32 feedback[30];
} SETTING;
/* function to copy the data from the main to the sub-module */
UINT32 Write( MAIN *data, SETTING *info)
{
sc_memcpy ( &info->carnum_k, &data->carnum_k, sizeof (SETTING));
sc_memcpy ( &info->feedback, &data->feedback, sizeof (SETTING)); //copies the array
}
Please tell me, is this correct? If not, what is the correct way to write it? Or what other alternatives do I have?
As I have an array as well, how should I go about it? What changes when I have to save say upto fb[10] from the main module to the sub-module?
How do I incoorporate the return condition, given that I should seemingly write ONE line of code to copy each type of data from the main module's table to the sub-module's input table?
I really want to get this right. Thanks in Advance :)
Question 1, your code is not correct.
When copy carnum_k, the sizeof(SETTING) is wrong. sizeof (SETTING) means the size of all data in SETTING structure . But you just need copy carnum_k, so you need provide the size of carnum_k, which is sizeof (UINT32), UINT32 is the type of carnum_k.
memcpy ( &info->carnum_k, &data->carnum_k, sizeof (UINT32));
You can also just write an assignment info->carnum_k = data->carnum_k.
The copy of feedback has two problem.
sizeof (SETTING) is wrong just like when you copy carnum_k. You can use sizeof(UINT32 [30]), UINT32 [30] is the type of feedback.
info->feedback is an array address, so no need &info->feedback, just info->feedback.
memcpy(info->feedback, data->feedback, sizeof(UINT32 [30]));
Question 2. I think you mean you may want just copy part of feedback.
When you copy memory you need figure out src memory address, dest memory address, and the size of memory you want to copy.
so if you want copy from feedback[2] to feedback[10].
source address is &(data->feedback[2])
dest address is &(info->feedback[2]).
From 2 to 10, there is 9 value you want to copy, so the total size is 9 * sizeof(UINT32)
memcpy(&(info->feedback[2]), &(data->feedback[2]), 9*sizeof(UINT32));
Question 3.
I don't known the answer.
I want to use a small C wrapper to access a so called LSF-API. LSF is the "load sharing facility" which is something like a platform to dispatch computing jobs on various machines (created by IBM).
I figured out how to do basic job submitting through populating a data structure and passing it to LSF. But when I tried to specify this data structure with some more of the defined attributes, I came to a problem which is connected to basic C issues.
I want to specify a list of hostnames, where the job should be dispatched to.
According to the API, this is done with those two fields:
char ** askedHosts -> The array of names of invoker specified candidate hosts. The number of hosts is given by numAskedHosts.
int numAskedHosts -> length of the previous array
This char ** makes my head aching:
I assumed, that I need to create an array with my hostnames as strings, specify the amount of them and pass this somehow to my data structure:
char *myHostArray[] = {"hostname_1","hostname_2","hostname_3"};
int numberOfMyHosts = 3;
myDatastructure.askedHosts = myHostArray;
myDatastructure.numAskedHosts = 3;
But whatever I try, it doesn't work. The depicted variant is the only one, where the compilation is at least successful and I do not get an "Segmentation fault" at runtime.
But in the end the information seems not to be passed correctly since it has no effects on the job dispatching.
I guess I am messing up something with the pointers.
Do you have any idea how I can pass this array correctly? I tried lots of variations but I was not successful after hours.
Do you know what I could be doing wrong here?
By the way - the API reference can be found here (I am talking about the "submit"-data structure):
https://www.ibm.com/support/knowledgecenter/en/SSWRJV_10.1.0/api_reference/index.html
For any optional parameter a request must tell whether this option is used or not. The options in the submit structure indeed has the flag
#define SUB_HOST 0x04
Flag to indicate numAskedHosts parameter has data.
Equivalent to bsub -m command line option existence.
You must do
submit.options |= SUB_HOST;
I hope this example will help you:
#include <stdio.h>
void func(char **args, int n) {
int i;
for (i=0; i<n; i++)
printf("%s\n", args[i]);
}
int main(void) {
char *askedHosts[] = {"hostname_1","hostname_2","hostname_3"};
int numOfHosts = 3;
func(askedHosts, numOfHosts);
}
I am new to JSON and jansson . I am trying to create a message in JSON using jansson library and send using UDP. Which requires in byte array. Buffer and length of message in bytes. I tried with json_object_size(). But it returns only number of elements in object.Please suggest me a way forward.
json_t *messagebody = json_object();
json_object_set_new(messagebody, "request_id", request_id);
json_object_set_new(messagebody, "process_id", json_string(process_id));
json_object_set_new(messagebody, "process_server_id", json_string(process_server_id));
json_object_set_new(messagebody, "ip_address", json_string(my_ip_address));
json_object_set_new(messagebody, "action", action);
It seems you just call char *json_dumps(const json_t *json, size_t flags).
That will give you a char * to a null terminated string representing the encoded json data. You have to free it after you are finished with it. To get the length in bytes you should simply be able to use strlen() on the result.
The flags are explained in API reference under 'encoding'.
I am using loadlibrary to interface with a DLL that returns image data in a struct containing info about the size of the image and a field where the actual image data is stored:
struct ImageStruct
{
// vertical size of the image [pixel]
int imageHeight;
// horizontal size of the image [pixel]
int imageWidth;
// image data size [byte]
int imageSize;
// pointer to image data
char * imageBuffer;
};
Matlab turns that into the following statement in the proto file:
structs.ImageStruct.members=struct('imageHeight', 'int32', 'imageWidth', 'int32', 'imageSize', 'int32', 'imageBuffer', 'cstring');
I then instantiate such a struct using str = libstruct('ImageStruct',[]) % default initialized and pass it, through calllib to a C function which takes a pointer to such a struct and fills it with image data. The problem is that my image data can contain zeros. Matlab automatically turns the char* member into a char array, stopping at the first 0 it encounters. This means my image data gets truncated. I however know how many values there are, this is returned in the imageSize member. How do I access the whole array?
I have found one solution, changing the type of the imageBuffer member in the proto file to uint8Ptr and then telling matlab what number of elements should be read from the pointer:
str.imageBuffer.setdatatype(str.imageBuffer.DataType,1,str.imageSize);
Is there a better solution?