http://www.torune.com/lineage2/AuthdC.rar
packet assembling function added (dummy copy from original with small modification)
the following code is from l2server.
Code:
int VAssemble(char* buf, int bufLen, const char* format, va_list ap)
{
//guard(int VAssemble(char* buf, int bufLen, const char* format, va_list ap));
const char* f = format;
char* start = buf;
char* end = buf + bufLen;
int t;
char* p;
int len;
int i;
double d;
while ((t = *format++))
{
switch (t)
{
case 's':
p = va_arg(ap, char*);
if (p)
{
len = strlen(p);
if (buf + len + 1 > end)
{
goto overflow;
}
strcpy(buf, p);
buf += len + 1;
}
else
{
// p is null. Is it correct?
if (buf + 1 > end)
{
goto overflow;
}
*buf++ = 0;
}
break;
case 'S':
{
wchar_t* p = va_arg(ap, wchar_t*);
if (p)
{
len = (wcslen(p) + 1)* sizeof(wchar_t);
if (buf + len > end)
{
goto overflow;
}
memcpy(buf, p, len);
buf += len;
}
else
{
// p is null. Is it correct?
if (buf + 2 > end)
{
goto overflow;
}
*buf++ = 0;
*buf++ = 0;
}
}
break;
case 'b': // blind copy
len = va_arg(ap, int);
p = va_arg(ap, char*);
if (buf + len > end)
{
goto overflow;
}
memcpy(buf, p, len);
buf += len;
break;
case 'c':
i = va_arg(ap, int);
if (buf + 1 > end)
{
goto overflow;
}
*buf++ = i;
break;
case 'h':
i = va_arg(ap, int);
if (buf + 2 > end)
{
goto overflow;
}
#ifdef _WIN32
*((short *)buf) = i;
buf += 2;
#else
*buf++ = i;
*buf++ = i >> 8;
#endif
break;
case 'd':
i = va_arg(ap, int);
if (buf + 4 > end)
{
goto overflow;
}
#ifdef _WIN32
*((int *)buf) = i;
buf += 4;
#else
*buf++ = i;
*buf++ = i >> 8;
*buf++ = i >> 16;
*buf++ = i >> 24;
#endif
break;
case 'f':
d = va_arg(ap, double);
if (buf + 8 > end)
{
goto overflow;
}
#ifdef _WIN32
CopyMemory(buf, &d, 8);
/*
*(((int*)buf)+0) = *(((int*)&d)+0);
*(((int*)buf)+1) = *(((int*)&d)+1);
*/
/*
*((double *)buf) = d;
*/
buf += 8;
#else
*buf++ = d;
*buf++ = d >> 8;
*buf++ = d >> 16;
*buf++ = d >> 24;
*buf++ = d >> 32
*buf++ = d >> 40;
*buf++ = d >> 48;
*buf++ = d >> 56;
#endif
break;
}
}
return (buf - start);
overflow:
Log.Add(LOG_ERROR, L"Buffer overflow in Assemble");
return 0;
//unguard;
}