Click here to Skip to main content
15,030,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to combine two character buffers in C but I am losing bytes at the end. My current code is as below:

My output is as below:

first block captured as [4ohizB}/ 6,7:}0,H@FbACm.bL~%=a>x!<v|qrv6pfhgsrvmzv}z;z+nfy47toxdoe;c@% ejpcb="ukbwE9d+nBs.+a4qt^cSWK!D+N@=<Hkv7tLPX1L]|1!HawzKEc-$gQeq4tp6bz<]JSbF_j9/-43e9eRRH+Zk3cpLr4|ZD^^L.&JT$l4nD6">rmlzG%l_H6)qWq[S**puDt2l CmRkr7F^B:!Z5oEXy_{~vV-7N8E-6SA&rCT 1(DbeQhTzJw,8Cp,]@N_T,%}LH7[rDg#)xhc@^ekYI-zsq|m4HFvbq0wJZ8^z{w+B|{bSh3~ugc%:QFt4VP0bV8O8dyQ=EB1=6MvJ;_Hbqr;IJ&7[oMhYS;xYpF>Vh%74&.lOr^!##I@Mg/:AtfJ@VW0Mgo}YLSfBJk+4p6<rw2rgy!x{juin!vkzf8&>CUzGQp;vaFPu[~fos@>@hDJ5-^meWpu/8fVpctX{3Nqn[, |c]T3rN-)kpJkYG.gze[C@|kQRL]

second block captured as [bOv>AHp=|<_;c]73Dud+(1o#FJr~ZB1;2NgI;]}oAcIoqK(;8Pd,0.r6CUQ7xp~+nEELqkZ|9-$vkZ%~k#2N&2LWnA~GZdH][$-:3vytS88Opd8==JG&Db29IqEB8EOpyPJIt?L-e1Vk1|A=kN)+jLAytyekc$ C[|-rP$F8gr}4.&enyg0{@lkG8&~FY|16c(FT)52j|teYT3-og4G?N{JX?tP6|@|oZlGD9LvAH=kA=9d8>vS?9/yzy(tLZYf]9>QA/E|^k,Q#iYE_SIY7&vz^(-h_phap.$Lad1j[fi2<qqr$a[0hb9f&li&5xtm$l-s$9?fsdn$uqea~!gs>a/3@4#bCP5zU]1Hlr.GT9].)|]9[ht2U{NJ1y0,FTteR*QuWmfMH ?YwEgOOP073oE)TCRq;>d;#gJ:ju)eoz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/B|CAIkTsj4<twx:a?s,oiemt6r

as you="" can="" see="" the="" combined="" block="" is="" missing="" bytes="" characters="" $ze*z7stp8x{6-[@{!6="">P=&En+|t5Ww ^Zfq~b&tlk_N which is the last 53 bytes of the second block.

Please review and confirm what I could be doing wrong in combining the two blocks and showing the contents of the combined block.

What I have tried:

C
//firstblock and secondblock are captured at different times from the passed in character buffer variable named buf
firstblock = (char*) malloc(512*sizeof(char));
memcpy(firstblock, buf, 512);
secondblock = (char*) malloc(512*sizeof(char));
memcpy(secondblock,  buf, 512);

//minimum code to test combining:
logMessage(LOG_INFO_LEVEL, "first block captured as [%s]", firstblock);
logMessage(LOG_INFO_LEVEL, "second block captured as [%s]", secondblock);
char *combinedblock;
combinedblock = (char*)malloc(sizeof(char)*(1024));
memcpy(combinedblock, firstblock, 512);
//logMessage(LOG_INFO_LEVEL, "combined block with just the first part [%s]", combinedblock);
memcpy(combinedblock+512, secondblock, 512);
logMessage(LOG_INFO_LEVEL, "combined block with the second block added [%s]", combinedblock);
Posted
Updated 13-Nov-20 5:15am
v2
Comments
Richard MacCutchan 13-Nov-20 11:10am
   
Something odd is happening to your data. The last line of block 2 is getting changed in the combined part. Is the code you have shown us the exact code that you are using?

In addition to the two previous posts, another thing to consider is are you certain both buffers will be 512 bytes? Even if you are, I think it is a bad idea to hard-code the value of 512 into things. I would rewrite this code into a function that takes the two buffers as arguments along with their sizes. You can do everything else as you are but there should be no references to 512 anywhere. That should be the size of the respective buffer that was passed into the function.

I am not going to rewrite your code for you but I will show you what the prototype should look like :
C++
typedef unsigned char uchar;

uchar * CombineBuffers( uchar * buffer1, size_t buffer1size, uchar * buffer2, size_t buffer2size );
The returned value should be the pointer to the combined buffer that you allocate. I made the data types unsigned char to handle binary data correctly.
   
The problem is likely to be that you are printing them as strings using %s: in C, strings are terminated by a null character which your buffers do not allow space for. That doesn't look like character based data, it's more likely to be binary, and if the binary data of the second block contains a null character, the %s output will terminate.
Since the two blocks of data you show above aren't the same size, it's likely that something in one or the other is confusing the issue when treated as a string.
   
Comments
narayan_kumar 13-Nov-20 10:16am
   
Second block likely does not contain null character as it gets printed fully on its own without a problem. It's getting truncated only when I try to print it from its copy in the combinedblock
Dont treat such data as string, but as memory buffer. You need to use memcpy to copy it and work carefully with pointer, sizes and offsets.

When printing it than use base 64 presentation.

You really need to understand C pointer arithemtics. This is heavy lifting, but you NEED to understand it - or you will fail.
   
Comments
Richard MacCutchan 13-Nov-20 9:46am
   
He is using memcpy.
narayan_kumar 13-Nov-20 10:14am
   
Please assume that both the firstblock and secondblock are exactly 512 bytes. How can I print them using the base 64 representation to validate I have gotten everything in the combined block?

Please review my current code and offsets as well while using memcpy.
Rick York 13-Nov-20 11:06am
   
I have no idea who the SFB that downvoted your solution is but it was certainly not justified.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900