• Welcome to Valhalla Legends Archive.
 

[C++] Inserting Null-Terminating Strings. (Cont.)

Started by Sorc.Polgara, December 04, 2004, 10:44 AM

Previous topic - Next topic

Mephisto


Kp

size_t is traditionally unsigned, so it looks rather silly to use <= when relating it to zero.  Also, why use an 'int' for a position?  It's not exactly common to have negative positions in a packetbuffer.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Sorc.Polgara

#32
Ok, I'm running into trouble inserting a nt string into another nt string with my packet buffer.  I get tons of "Abort" "Retry" "Cancel" errors when I trid doing the following:


strncpy(buffer + pos, insNTSTR, strlen(insNTSTR));
strcpy(buffer + pos, insNTSTR, strlen(insNTSTR));
memcpy(buffer + pos, insNTSTR, _msize(insNTSTR));


I also tried modifiying the code that peeps i.e. Adron, Yoni etc. showed me, it won't compile though.

(-1)[(char*)(buffer + (pos += _msize(insNTSTR)))] = insNTSTR;


also I tired some other string manipulating routines from the MSDN Library, the packet buffer doesn't like it.

What I've done is basically set my buffer to the size of 32 from the gitgo.  So that I won't have to resize it everytime I add something.  (Suggested by some  peeps)

I've inserted a WORD + BYTE + DWORD into the buffer and next I want to add an NT string.

I can add WORDs, BYTEs, DWORDs as much as I need. however, since some packets require for me to send and recieve data/strings with unspecified lengths which I learned when I had made a bot back in VB6 and first handling packets.

Must I have to cut off that extra buffer space and then catengate (spelling wrong too lazy to look up)  it somehow to the buffer? or is there a way to just do it like with DWORDs WORDs BYTEs etc?

Thanks.  Once I get my NTString thingy going, then I will post my packet buffer for criticism and I can fix it b/c j00 guys are pro programmars.  Thanks.

Mephisto

why on earth would you copy the string three times w/ three different functions?  And how does that call to strcpy() even compile?  Also make sure that you have the proper amount of space allocated to insert the size of those strings into your buffer.

shadypalm88

Quote from: Sorc.Polgara on December 08, 2004, 06:27 PM

strncpy(buffer + pos, insNTSTR, strlen(insNTSTR));
strcpy(buffer + pos, insNTSTR, strlen(insNTSTR));
memcpy(buffer + pos, insNTSTR, _msize(insNTSTR));
Well the second one shouldn't even compile.  Realize that the following two lines of code are really equivalent:
strcpy(buffer + pos, insNTSTR);
strncpy(buffer + pos, insNTSTR, strlen(insNTSTR));

Whichever one you use, you will still need to get the length of the string in order to update your position.  My full routine is:PacketBuffer& PacketBuffer::operator<<(const unsigned char* n) {
    size_t l = std::strlen((const char*) n) + 1;
    ensure(l);
    std::strcpy((buffer + position), (const char*) n);
    position += l;
    bufferLength += l;
}
It's really simple.  Notice the + 1 on the first line, this is so the updated position takes the null-terminator into account.

Sorc.Polgara

Quote from: Mephisto on December 08, 2004, 06:35 PM
why on earth would you copy the string three times w/ three different functions?  And how does that call to strcpy() even compile?  Also make sure that you have the proper amount of space allocated to insert the size of those strings into your buffer.

nonono I'm showing hte different things I've tried.  nono each line I've used separately in my trial and error stuff.

no way would i use all three of those at the same time errr consecutively.

OnlyMeat

Quote from: Sorc.Polgara on December 08, 2004, 06:27 PM

strncpy(buffer + pos, insNTSTR, strlen(insNTSTR));
strcpy(buffer + pos, insNTSTR, strlen(insNTSTR));
memcpy(buffer + pos, insNTSTR, _msize(insNTSTR));


First of all strcpy/strncpy only applies to null terminated strings, and as your packet buffer is clearly not going to be a null terminated string  ( remember you are using it to store byte sequences independant of datatype ) you should use memcpy.

Second was insNTSTR allocated on the heap? i ask this because _msize(insNTSTR) is used to determinate the size of a block allocated on the heap not on the stack, so if insNTSTR was not allocated dyamically that call will fail, try this instead:-


memcpy(buffer + pos, insNTSTR, strlen(insNTSTR));


And btw in the world of c++ we use hungarian notation on variables and for a string it's called zero terminated denoted by this variable example:-


char szMyString[12];



Skywing

People need not conform to your coding style to write valid code.

Besides, that's not even real Hungarian, which names variables based on their meaning, not type.

Mephisto

Quote from: OnlyMeat on December 08, 2004, 07:54 PMFirst of all strcpy/strncpy only applies to null terminated strings, and as your packet buffer is clearly not going to be a null terminated string  ( remember you are using it to store byte sequences independant of datatype ) you should use memcpy.

And btw in the world of c++ we use hungarian notation on variables and for a string it's called zero terminated denoted by this variable example:-

Using strcpy() in your packet buffer is just fine; you don't need to use memcpy(); and in fact, a string is suppose to be NULL terminated when you send it to Battle.net.

And in the world of C++ we don't always use Hungarian notation.  How many Linux users do you see using it?  I don't see a lot (could be mistaken though); and I myself who use Windows don't use Hungarian notation.  It's a coding style which was invented at Microsoft, and last time I checked, coding style was never standardized in ANSI-C++.

[Kp edit: cleaned up quote tags so Mephisto wouldn't get blamed for the stupid things OnlyMeat said.]

Adron

Steps to insert a null-terminated string:

1. Make sure the buffer is large enough. If it's full now, enlarge it by strlen(str) + 1;
2. Copy the null-terminated string into the buffer at the current position. Use strcpy(buffer + pos, str); or memcpy(buffer + pos, str, strlen(str) +1); or whatever you like.
3. Update pos: pos += strlen(str) + 1;

|