Char string overwriteing other string - c

I´m making a program that builds a binary message. I´m using char strings to hold the binary value. So I´ve initialized a bunch of char strings that has the default values. Then I combine them by running a for loop and read them into a large string (aismsg/ais_packet). And everything worked fine untill I added msg14Text[], then the string I´m building (aismsg/ais_packet) gets shortened as shown below (even though I´m not using the variable). Seems like when I add msg14Text[], it changes the value of one of the other strings. Is this maybe a memory allocation problem?
Part of the code:
char ais_packet[257]; //Allokerer array for ais data pakke.
char aismsg[175]; //Allokerer array for meldingen.
int burst_nr = 1; //Indicates with burst it is transmittin (1-7).
char ramp_up[] = "00000000"; //Ramp up buffer.
char train_seq[] = "010101010101010101010101"; //Training sequence 24 bits of alternating 0-1.s
char hdlc_flag[] = "01111110"; //HDLC Start and END flag.
char buffer[] = "000000000000000000000000"; //Data packet buffer.
char msgID1[] = "000001"; //msg. 1.
char msgID14[] ="010100"; //msg. 14.
char repeat[] = "00"; //repetert 0 ganger.
char mmsi[] = "000111010110111100110100010101"; //Gir 123456789 som MMSI.
char nav_stat[] = "1111"; //Gir 15= AIS-SART test, endres til 14 (1110) for aktiv AIS-SART.x'
char rot[] = "10000000"; //Rate of Turn -128 betyr ikkje tilgjengelig.
char sogBin[] = "1111111111"; //Tilsvarer 1023 = not available = default.
char pos_acc[] = "0"; //Posisjonsnøyaktighet over 10m. 1 = under 10m.
char lonBin[] = "0110011110010001101011000000"; // Tilsvarer 181 grader som er default verdi for Longitude.
char latBin[] = "011010000010010000101000000"; // Tilsvarer 91 grader som er default verdi for Latitude.
char cogBin[] = "111000010000"; //Tilsvarer 3600 = not available = default.
char headingBin[] = "111111111"; //511 = not available = default
char timestamp[] = "111100"; //Tid siden melding er generert, 60 = default = ts not available.
char spec_man[] = "01"; //Special manouver 0 = default, 1 = not engaged in special manouver
char spare[] = "000";
char spareMSG14[] = "00"; //Reserved.
char raim[] = "0"; //RAIM 0 = not in use.
char comm_state[] = "00011100000000000000"; // First 2bit: Sync state: 3 = no UTC sync = default, 0 = UTC sync. 0011100000000000000
char msg14Text[] = "100100101101111011111100"; //CAUSING TROUBLE!!!! for AIS melding 14 står "Test" med 6-bit ASCII koding.
The enitre code for the function can be found at pastebin.com/wj0RxyLX
Output of ais packet with msg14Text[]:
00000000
Output of ais packet without msg14Text[]:
0000000001010101010101010101010101111110000001000001110101101111001101000101011111100000000011010000000000000110100011000101111000000101100100000100001100101110000100000000110011111000100000011100000000000000001000100110100101111110000000000000000000000000
aispacket should consist of the following variables:
ramp_up[] + train_seq[] + hdlc_flag[] + Datapacket(168bit) + crc(16bit) + hdlc_flag[] + buffer[] + '\0'

"Is this maybe a memory allocation problem?"
You don't explicitly allocate any memory in your code. Note that char repeat[] = "00"; is statically allocated array whose size is equal to size of 3 chars and whose content is being initialized by string literal "00".
Problem is most likely in copying of these strings into ais_packet since you do that in nonstandard way (character by character) which causes your code to be hard to read and it's quite easy to make a mistake there:
for(int k=0; k<256; k++)
{
...
if(k==256) // are you sure that value of k will reach 256 ?
I recommend you to use C-style functions that have been created for this purpose: Craete ais_packet by copying first string into it by using strcpy and keep extending content of this ais_packet by appending other strings by using strcat.
This question will also help you: Using strcat in C

At the end of the ugly for (k=0; k < 168; k++) { if ... else if ...} loop
else if(k==168)
{
aismsg[k] = '\0';
k=0;
}
This will make either (k <=168) the loop run forever, or (k <168) never be executed. (there are more instances of this pattern)
BTW another way to do the same (also faster) would be
....
unsigned dst=0;
memcpy (array+dst, src1, 123);
dst += 123;
memcpy(array+dst, src2, 234);
dst += 234;
...
array[dst] = 0;

Just a thought, but if you are building binary messages, why not use actual binary instead of char arrays? Here's a way to use structs inside a union to bit pack binary data.
// declaration
typedef union
{
uint32_t packed;
struct {
uint16_t sample1: 12; // 12 bits long
uint16_t sample2: 14;
uint16_t 6; // unused bits
} data;
} u1;
// instantiation
u1 pack1;
// setting
pack1.data.sample1 = 1234;
//getting
uint16_t newval = pack1.data.sample2;
// setting bit 6 in sample 1
pack1.data.sample1 |= (1 << 6);
// setting lo nibble in sample1 to 0101
pack1.data.sample1 &= 0b11110101;
// getting the whole packed value
uint32_t binmsg = pack1.packed;

Related

Passing a signed char array to function expecting const char pointer results in garbled array value

I cant seem to wrap my head around what is happening here:
I have a function that calculates the difference between two dates in seconds. Then it gets bitshifted around to produce some output:
void GetUTC(unsigned char buffer[5])
{
struct tm str_time;
time_t start;
time_t current = time(NULL);
str_time.tm_year = 2010 - 1900;
str_time.tm_mon = 1;
str_time.tm_mday = 1;
str_time.tm_hour = 0;
str_time.tm_min = 0;
str_time.tm_sec = 0;
str_time.tm_isdst = 0;
start = mktime(&str_time);
printf("%s\n",ctime(&start));
printf("%s\n\n",ctime(&current));
uint32_t someInt = difftime(current,start);
buffer[0] = 2;
buffer[1] = (someInt & 0xff);
someInt = someInt >> 8;
buffer[2] = (someInt & 0xff);
someInt = someInt >> 8;
buffer[3] = someInt;
someInt = someInt >> 8;
buffer[4] = (10 & 0xff);
}
I feed it this array:
unsigned char toWrite[5];
Then call it
GetUTC(toWrite);
All goes well. Now I have a function I am trying to feed the new array into, which takes these parameters:
void gatt_write_attribute(GDBusProxy *proxy, const char *arg)
And I call it with:
gatt_write_attribute(someProxy, toWrite);
However the array I passed to the function gatt_write_attribute shows that it's the jumbled garbage: $2\20023\n23 instead of my expected value of toWrite. (numbers differ since it has to do with time):
[0]: 2
[1]: 23
[2]: 54
[3]: 128
[4]: 10
I tried adding a terminating \0 to the end of toWrite, but it did not change anything. I tried casting it to a const char pointer and that didn't work either.
I feel like I'm missing a very simple detail. Could someone explain why I cant pass this char array to the gatt_write_attribute function?
As noted by Ian Abbott and Chux in the comments to my original question, I was indeed being silly and was not realizing that my debugger was displaying the thing being pointed to as an array of characters instead of numbers. This is what caused the garbled values.
I should have probably allowed myself a bit more sleep in-between work hours.
You need to pass the array to the first function as a pointer. Otherwise it just gets copied to the stack and the function writes to the new copy. The garbage is probably there because it has not been initialised.

Char array to unsigned short conversion issue

I am trying to convert char array to unsigned short but its not working as it should.
char szASCbuf[64] = "123456789123456789123456789";
int StoreToFlash(char szASCbuf[], int StartAddress)
{
int iCtr;
int ErrorCode = 0;
int address = StartAddress;
unsigned short *us_Buf = (unsigned short*)szASCbuf;
// Write to flash
for(iCtr=0;iCtr<28;iCtr++)
{
ErrorCode = Flash_Write(address++, us_Buf[iCtr]);
if((ErrorCode &0x45)!= 0)
{
Flash_ClearError();
}
}
return ErrorCode;
}
When I see the Conversion, on us_Buf[0] I have value 12594, us_Buf[1]= 13108 like that and I have values only uptous_Buf[5]` after that it is "0" all remaining address.
I have tried to declare char array like this also
char szASCbuf[64] = {'1','2','3','4','5','6','7','8','9','1',.....'\0'};
I am passing the parameters to function like this
StoreToFlash(szASCbuf, FlashPointer); //Flashpointe=0
I am using IAR embedded workbench for ARM. Big enedian 32.
Any suggestions where i am doing wrong?
Thanks in advance.
Reinterpreting the char array szASCbuf as an array of short is not safe because of alignment issues. The char type has the least strict alignment requirements and short is usually stricter. This means that szAscBuf might start at address 13, whereas a short should start at either 12 or 14.
This also violates the strict aliasing rule, since szAscBuf and us_Buf are pointing at the same location while having different pointer types. The compiler might perform optimisations which don't take this into account and this could manifest in some very nasty bugs.
The correct way to write this code is to iterate over the original szASCBuf with a step of 2 and then do some bit-twiddling to produce a 2-byte value out of it:
for (size_t i = 0; i < sizeof(szAscbuf); i += 2) {
uint16_t value = (szAscbuf[i] << 8) | szAscbuf[i + 1];
ErrorCode = Flash_Write(address++, value);
if (ErrorCode & 0x45) {
Flash_ClearError();
}
}
If you really intended to treat the digit characters with their numeric value, this will do it:
uint16_t value = (szAscbuf[i] - '0') + (szAscbuf[i + 1] - '0');
In case you just want the numeric value of each character in a 2-byte value (1, 2, 3, 4, ...), iterate over the array with a step of 1 and fetch it this way:
uint16_t value = szAscbuf[i] - '0';
That's normal !
Your char array is "123456789123456789123456789" or {'1','2','3','4','5','6','7','8','9','1',.....'\0'}
But in ASCII '1' is 0x31, so when you read the array as a short * on a big endian architecture, it gives :
{ 0x3132, 0x3334, ... }
say differently in decimal :
{ 12594, 13108, ... }

corner case of parsing a time military time string

usually, most military times are like this
12:34:56
I already figured an method to extract the Hours,mins and secs into their own strings.
However, there is a corner case that i can't seem to figure out
example:
12::
after extracting the number and converting to integers, result is 0. That is good.
However, i need two 0's to properly represent the time
char *hh = argv[2];
char *mm, *ss;
char *array[3];
char *temp =strchr(argv[2],':');
mm = temp + 1;
/*if(*mm == ':'){
*mm = "00";
}*/
*temp = '\0';
temp = strchr(mm, ':');
ss = temp+1;
*temp = '\0';
if you look at the commented if part, that is my attempt to check if the next pointed value is a semicolon. If it is, all i need to do is it with two 0's.
Any clue how to deal with this?
It would be much better to extract this data as integers, not strings.
Something like:
bool parse_hms(int *hours, int *minutes, int *seconds, const char *hms)
{
*hours = *minutes = *seconds = 0;
return sscanf(hms, "%d:%d:%d", hours, minutes, seconds) > 0;
}
should work. Note that the function assumes that all three pointers are valid. It returns true if at least one of the numbers was parsed out. Non-parsed numbers will be set to 0 (which of course is also valid as an element value).

How to put bits into a character Array

I need to know how to put bits into a character array.
for example,
I want to put 0001 bits into a character array using C or C++.
Need your help guys. Thanks.
Maybe this more generic code will give you the idea:
void setBitAt( char* buf, int bufByteSize, int bitPosition, bool value )
{
if(bitPosition < sizeof(char)*8*bufByteSize)
{
int byteOffset= bitPosition/8;
int bitOffset = bitPosition - byteOffset*8;
if(value == true)
{
buf[byteOffset] |= (1 << bitOffset);
}
else
{
buf[byteOffset] &= ~(1 << bitOffset);;
}
}
}
//use it as follow:
char chArray[16];
setBitAt(chArray,16*sizeof(char),5,true); //to set bit at pos 5 to 1
Is that really all?
char buf[1];
buf[0] = char(1);
If you want bit masking then it would be something like
enum Enum
{
MASK_01 = 0x1,
MASK_02 = 0x2,
MASK_03 = 0x4,
MASK_04 = 0x8,
};
char buf[4];
buf[0] = Enum::MASK_01;
buf[1] = Enum::MASK_02;
buf[2] = Enum::MASK_03;
buf[3] = Enum::MASK_04;
If you provide information on what you are actually trying to do, we may be able to help you more.
EDIT: Thanks for the extra information. Does this help:
enum Enum
{
BIT_0000000000000001 = 0x0001,
BIT_0000000000000010 = 0x0002,
BIT_0000000000000100 = 0x0004,
BIT_0000000000001000 = 0x0008,
BIT_0000000000010000 = 0x0010,
BIT_0000000000100000 = 0x0020,
BIT_0000000001000000 = 0x0040,
BIT_0000000010000000 = 0x0080,
BIT_0000000100000000 = 0x0100,
BIT_0000001000000000 = 0x0200,
BIT_0000010000000000 = 0x0400,
BIT_0000100000000000 = 0x0800,
BIT_0001000000000000 = 0x1000,
BIT_0010000000000000 = 0x2000,
BIT_0100000000000000 = 0x4000,
BIT_1000000000000000 = 0x8000,
};
int main( int argc, char* argv[] )
{
char someArray[8];
memset( someArray, 0, 8 );
// create an int with the bits you want set
int combinedBits = BIT_0000000000000001|
BIT_0000000000000010|
BIT_1000000000000000;
// clear first two bytes
memset( someArray, 0, 2 );
// set the first two bytes in the array
*(int*)someArray |= combinedBits;
// retrieve the bytes
int retrievedBytes = *(int*)someArray;
// test if a bit is set
if ( retrievedBytes & BIT_0000000000000001 )
{
//do something
}
}
Now the naming of the enums is intentionally intense for clarity. also you may notice that there are only 16 bits in the enum, instead of a possible 32 for an int. This is because you mentioned the first two bytes. Using this method, only the first two bytes of the array will be changed, using those enums. Im not sure if this code would be messed up by endianess, so you will have to make sure you test on your own machines. HTH.
You put bits in a character array using C or C++ the way you put anything into anything else -- they're all bits anyway.
Since sizeof(char) == 1 by definition, you can only put 8 bits per element of the array.
If you need help with how to twiddle bits, that's an entirely different issue and has nothing to do with chars and arrays.
C doesn't support binary literals, so you'll have to represent the value as hex.
char *p;
*p++ = 0x10;
*p++ = 0xFE;
Take a look at the functions hton() and htonl() for converting multi-byte values to network-byte-order.

Pointer initializiation? for a specific function

Alright, this one's been puzzling me for a bit.
the following function encodes a string into base 64
void Base64Enc(const unsigned char *src, int srclen, unsigned char *dest)
{
static const unsigned char enc[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned char *cp;
int i;
cp = dest;
for(i = 0; i < srclen; i += 3)
{
*(cp++) = enc[((src[i + 0] >> 2))];
*(cp++) = enc[((src[i + 0] << 4) & 0x30)
| ((src[i + 1] >> 4) & 0x0f)];
*(cp++) = enc[((src[i + 1] << 2) & 0x3c)
| ((src[i + 2] >> 6) & 0x03)];
*(cp++) = enc[((src[i + 2] ) & 0x3f)];
}
*cp = '\0';
while (i-- > srclen)
*(--cp) = '=';
return;
}
Now, on the function calling Base64Enc() I have:
unsigned char *B64Encoded;
Which is the argument I pass onto unsigned char *dest in the base 64 encoding function.
I've tried different initializations from mallocs to NULL to other initialization. No matter what I do I alway get an exception and if I don't initialize it then the compiler (VS2005 C compiler) throws a warning telling me that it hasn't been initialized.
If I run this code with the un-initialized variable sometimes it works and some other it doesn't.
How do I initialized that pointer and pass it to the function?
you need to allocate buffer big enough to contain the encoded result. Either allocate it on the stack, like this:
unsigned char B64Encoded[256]; // the number here needs to be big enough to hold all possible variations of the argument
But it is easy to cause stack buffer overflow by allocating too little space using this approach. It would be much better if you allocate it in dynamic memory:
int cbEncodedSize = srclen * 4 / 3 + 1; // cbEncodedSize is calculated from the length of the source string
unsigned char *B64Encoded = (unsigned char*)malloc(cbEncodedSize);
Don't forget to free() the allocated buffer after you're done.
It looks like you would want to use something like this:
// allocate 4/3 bytes per source character, plus one for the null terminator
unsigned char *B64Encoded = malloc(srclen*4/3+1);
Base64Enc(src, srclen, B64Encoded);
It would help if you provided the error.
I can, with your function above, to this successfully:
int main() {
unsigned char *B64Encoded;
B64Encoded = (unsigned char *) malloc (1000);
unsigned char *src = "ABC";
Base64Enc(src, 3, B64Encoded);
}
You definitely need to malloc space for the data. You also need to malloc more space than src (1/4 more I believe).
A base64 encoded string has four bytes per three bytes in-data string, so if srclen is 300 bytes (or characters), the length for the base64 encoded string is 400.
Wikipedia has a brief but quite good article about it.
So, rounding up srclen to the nearest tuple of three, divided by three, times four should be exactly enough memory.
I see a problem in your code in the fact that it may access the byte after the trailing null char, for instance if the string length is one char. The behavior is then undefined and may result in a thrown exception if buffer boundary checking is activated.
This may explain the message related to accessing uninitialized memory.
You should then change your code so that you handle the trailing chars separately.
int len = (scrlen/3)*3;
for( int i = 0; i < len; i += 3 )
{
// your current code here, it is ok with this loop condition.
}
// Handle 0 bits padding if required
if( len != srclen )
{
// add new code here
}
...
PS: Here is a wikipedia page describing Base64 encoding.

Resources