• Welcome to Valhalla Legends Archive.
 

Hey all need some help.

Started by Final, September 29, 2006, 06:15 PM

Previous topic - Next topic

MyndFyre

Why don't you do something like this:


// Please note: I'm assuming packet is the char* containing your packet
// data, and ps is the offset into the packet that you're currently at.
int debuff::nextStrLen() const
{
  char* pCur = packet + ps;
  char* pStart = pCur;
  // assume at least a size of 1 for a null terminator (empty string)
  int nLen = 1;
  while (*pCur != 0)
    pCur++;

  return pStart - pCur;
}

#define E_BUFFER_TOO_SMALL  0xDEADC0DE

int debuf::getStr(char* result, int bufferSize)
{
  char* pStart = packet + ps;
  int strLen = nextStrLen();
  if (bufferSize < strLen)
    return E_BUFFER_TOO_SMALL;

  memcpy(result, const_cast<const char*>(packet), strLen);

  ps += strLen;
  return strLen;
}


To get the length of the next string, use nextStrLen().  This function returns the length of the next null-terminated string and includes the additional null terminaotr.

To get the next string, use getStr(char*, int).  You are required to pass the buffer to which the string will be copied (the function does not allocate a new string), as well as the length of the buffer.  The buffer must be at least nextStrLen() bytes long; if the function detects that this is not the case, it will return E_BUFFER_TOO_SMALL, a negative value.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Final

#61
//message sent to userloser added after mynd's sorry.

Ok watch i did this because sizeof() dosent stop at a null
so sizeof(packet+ps);
would get size of everything after packet+ps without stoping giving me me all info.

//to mynd

the packet isint a char* its a char var[MAX_PATH];<---sorry about the max_path;

but i use that to get the pos to the next data in packet unused. heres my code.


WORD debuff::getword(){
     WORD value=*(unsigned long*)(packet+ps);
     ps=ps+2;
     memcpy(packet,packet+ps,sizeof(packet+ps)+1);
     return value; 
}
DWORD debuff::getdword(){
     DWORD value=*(unsigned long*)(packet+ps);
     ps=ps+4;     
     memcpy(packet,packet+ps,sizeof(packet+ps)+1);
     return value;         
}



UserLoser


const int MAX_PATH = 260;

int ps = 3;
char *test = "this is a test";
char packet[MAX_PATH] = { 0xff, 0x25, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04 };

cout << "sizeof(test) = " << sizeof(test) << endl;
cout << "sizeof(*test) = " << sizeof(*test) << endl;
cout << "sizeof(packet) = " << sizeof(packet) << endl;
cout << "sizeof(packet+ps) = " << sizeof(packet+ps) << endl;
cout << "MAX_PATH = " << MAX_PATH << endl;


Quote
   sizeof(test) = 4
   sizeof(*test) = 1
   sizeof(packet) = 260
   sizeof(packet+ps) = 4
   MAX_PATH = 260

Do not use sizeof, that is not what it is for.

Final

#63
ok i guess i wasent passing the buffer right it just does not like so i just did this

functionname(buffer){
//data
}
Im having issues though how would i get the size because of the pos like wtf do i do


char* debuff::getstring(char packet[MAX_PATH]){
char* value=packet+pos;
pos=pos+strlen(value);
return value;
}

It wont add or i d k because when i get a message from someone it dosent get the message only username
?

--Never mind i just  added a  1 after strlen() forgot about the 0x00

MyndFyre

Quote from: Final on October 03, 2006, 07:23 PM
the packet isint a char* its a char var[MAX_PATH];<---sorry about the max_path;

This is precisely what UserLoser means by his remark that you should learn the language.  A char array (like you said char var[MAX_PATH]) is exactly the same as a char*.  For instance, you could:


char packet[255];
char* pck = &packet[0];


The brackets used in array notation are just implicit pointer arithmetic.  For instance, the next two expressions are equivalent:

char value = packet[220];

char value = *(packet + (sizeof(char) * 220));

So, as you can see, the bracket operator just adds the product of the size of the value and the index to the base location in memory of the array and dereferences it.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Final

#65
i understand their the same but i was just saying i wassent declaring it as a char* i was using char [] dude chill out.

warz

It's also worth noting that MAX_PATH is already defined in windows.h.

UserLoser

Quote from: warz on October 04, 2006, 12:17 AM
It's also worth noting that MAX_PATH is already defined in windows.h.

I didn't include windows.h because I used an already existing project for school for this example and didn't want to modify the included files

|