summaryrefslogtreecommitdiff
path: root/icqj_mod/icq_packet.c
blob: a0b628bb9c546a4adaa6e21b3cde75ea9bebccd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// ---------------------------------------------------------------------------80
//                ICQ plugin for Miranda Instant Messenger
//                ________________________________________
// 
// Copyright © 2000,2001 Richard Hughes, Roland Rabien, Tristan Van de Vreede
// Copyright © 2001,2002 Jon Keating, Richard Hughes
// Copyright © 2002,2003,2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004,2005,2006 Joe Kucera, Bio
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// -----------------------------------------------------------------------------
//
// File name      : $Source: /cvsroot/miranda/miranda/protocols/IcqOscarJ/icq_packet.c,v $
// Revision       : $Revision: 2991 $
// Last change on : $Date: 2006-05-30 19:55:48 +0200 (Tue, 30 May 2006) $
// Last change by : $Author: jokusoftware $
//
// DESCRIPTION:
//
//  Describe me here please...
//
// -----------------------------------------------------------------------------

#include "icqoscar.h"



void write_httphdr(icq_packet* pPacket, WORD wType, DWORD dwSeq)
{
  pPacket->wPlace = 0;
  pPacket->wLen += 14;
  pPacket->pData = (BYTE*)SAFE_MALLOC(pPacket->wLen);

  packWord(pPacket, (WORD)(pPacket->wLen - 2));
  packWord(pPacket, HTTP_PROXY_VERSION);
  packWord(pPacket, wType);
  packDWord(pPacket, 0); // Flags?
  packDWord(pPacket, dwSeq); // Connection sequence ?
}



void __fastcall write_flap(icq_packet* pPacket, BYTE byFlapChannel)
{
  pPacket->wPlace = 0;
  pPacket->wLen += 6;
  pPacket->pData = (BYTE*)SAFE_MALLOC(pPacket->wLen);

  packByte(pPacket, FLAP_MARKER);
  packByte(pPacket, byFlapChannel);
  packWord(pPacket, 0);                 // This is the sequence ID, it is filled in during the actual sending
  packWord(pPacket, (WORD)(pPacket->wLen - 6)); // This counter should not include the flap header (thus the -6)
}



void __fastcall serverPacketInit(icq_packet* pPacket, WORD wSize)
{
  pPacket->wLen = wSize;
  write_flap(pPacket, ICQ_DATA_CHAN);
}



void __fastcall directPacketInit(icq_packet* pPacket, DWORD dwSize)
{
  pPacket->wPlace = 0;
  pPacket->wLen   = (WORD)dwSize;
  pPacket->pData  = (BYTE *)SAFE_MALLOC(dwSize + 2);

  packLEWord(pPacket, pPacket->wLen);
}



void __fastcall serverCookieInit(icq_packet* pPacket, BYTE* pCookie, WORD wCookieSize)
{
  pPacket->wLen = (WORD)(wCookieSize + 8);

  write_flap(pPacket, ICQ_LOGIN_CHAN);
  packDWord(pPacket, 0x00000001);
  packTLV(pPacket, 0x06, wCookieSize, pCookie);
}



void __fastcall packByte(icq_packet* pPacket, BYTE byValue)
{
  pPacket->pData[pPacket->wPlace++] = byValue;
}



void __fastcall packWord(icq_packet* pPacket, WORD wValue)
{
  pPacket->pData[pPacket->wPlace++] = ((wValue & 0xff00) >> 8);
  pPacket->pData[pPacket->wPlace++] = (wValue & 0x00ff);
}



void __fastcall packDWord(icq_packet* pPacket, DWORD dwValue)
{
  pPacket->pData[pPacket->wPlace++] = (BYTE)((dwValue & 0xff000000) >> 24);
  pPacket->pData[pPacket->wPlace++] = (BYTE)((dwValue & 0x00ff0000) >> 16);
  pPacket->pData[pPacket->wPlace++] = (BYTE)((dwValue & 0x0000ff00) >> 8);
  pPacket->pData[pPacket->wPlace++] = (BYTE) (dwValue & 0x000000ff);
}



void packTLV(icq_packet* pPacket, WORD wType, WORD wLength, BYTE* pbyValue)
{
  packWord(pPacket, wType);
  packWord(pPacket, wLength);
  packBuffer(pPacket, pbyValue, wLength);
}



void packTLVWord(icq_packet* pPacket, WORD wType, WORD wValue)
{
  packWord(pPacket, wType);
  packWord(pPacket, 0x02);
  packWord(pPacket, wValue);
}



void packTLVDWord(icq_packet* pPacket, WORD wType, DWORD dwValue)
{
  packWord(pPacket, wType);
  packWord(pPacket, 0x04);
  packDWord(pPacket, dwValue);
}



// Pack a preformatted buffer.
// This can be used to pack strings or any type of raw data.
void packBuffer(icq_packet* pPacket, const BYTE* pbyBuffer, WORD wLength)
{

  while (wLength)
  {
    pPacket->pData[pPacket->wPlace++] = *pbyBuffer++;
    wLength--;
  }

}



// Pack a buffer and prepend it with the size as a LE WORD.
// Commented out since its not actually used anywhere right now.
//void packLEWordSizedBuffer(icq_packet* pPacket, const BYTE* pbyBuffer, WORD wLength)
//{
//
//  packLEWord(pPacket, wLength);
//  packBuffer(pPacket, pbyBuffer, wLength);
//
//}


int __fastcall getUINLen(DWORD dwUin)
{ // TODO: invent something more clever
  if (dwUin >= 1000000000) return 10;
  if (dwUin >= 100000000) return 9;
  if (dwUin >= 10000000) return 8;
  if (dwUin >= 1000000) return 7;
  if (dwUin >= 100000) return 6;
  if (dwUin >= 10000) return 5;
  if (dwUin >= 1000) return 4;
  if (dwUin >= 100) return 3;
  if (dwUin >= 10) return 2;
  return 1;
}



int __fastcall getUIDLen(DWORD dwUin, char* szUid)
{
  if (dwUin) 
    return getUINLen(dwUin);
  else
    return strlennull(szUid);
}



void __fastcall packUIN(icq_packet* pPacket, DWORD dwUin)
{
  unsigned char pszUin[UINMAXLEN];
  BYTE nUinLen = getUINLen(dwUin);

  ltoa(dwUin, pszUin, 10);

  packByte(pPacket, nUinLen);           // Length of user id
  packBuffer(pPacket, pszUin, nUinLen); // Receiving user's id
}



void __fastcall packUID(icq_packet* pPacket, DWORD dwUin, char* szUid)
{
  if (dwUin)
    packUIN(pPacket, dwUin);
  else
  {
    BYTE nLen = strlennull(szUid);
    packByte(pPacket, nLen);
    packBuffer(pPacket, szUid, nLen);
  }
}



void packFNACHeader(icq_packet* pPacket, WORD wFamily, WORD wSubtype)
{
  packWord(pPacket, wFamily);   // Family type
  packWord(pPacket, wSubtype);  // Family subtype
  packDWord(pPacket, 0);        // SNAC flags // SNAC request id (sequence)
  packWord(pPacket, wSubtype);  // SNAC request id (command)
}



void packFNACHeaderFull(icq_packet* pPacket, WORD wFamily, WORD wSubtype, WORD wFlags, DWORD dwSeq)
{
  WORD wSeq = (WORD)dwSeq & 0x7FFF; // this is necessary, if that bit is there we get disconnected

  packWord(pPacket, wFamily);   // Family type
  packWord(pPacket, wSubtype);  // Family subtype
  packWord(pPacket, wFlags);    // SNAC flags
  packWord(pPacket, wSeq);      // SNAC request id (sequence)
  packWord(pPacket, (WORD)(dwSeq>>0x10));  // SNAC request id (command)
}



void __fastcall packLEWord(icq_packet* pPacket, WORD wValue)
{
  pPacket->pData[pPacket->wPlace++] =  (wValue & 0x00ff);
  pPacket->pData[pPacket->wPlace++] = ((wValue & 0xff00) >> 8);
}



void __fastcall packLEDWord(icq_packet* pPacket, DWORD dwValue)
{
  pPacket->pData[pPacket->wPlace++] = (BYTE) (dwValue & 0x000000ff);
  pPacket->pData[pPacket->wPlace++] = (BYTE)((dwValue & 0x0000ff00) >> 8);
  pPacket->pData[pPacket->wPlace++] = (BYTE)((dwValue & 0x00ff0000) >> 16);
  pPacket->pData[pPacket->wPlace++] = (BYTE)((dwValue & 0xff000000) >> 24);
}



void ppackByte(PBYTE *buf,int *buflen,BYTE b)
{
  *buf=(PBYTE)realloc(*buf,1+*buflen);
  *(*buf+*buflen)=b;
  ++*buflen;
}



void ppackLEWord(PBYTE *buf,int *buflen,WORD w)
{
  *buf=(PBYTE)realloc(*buf,2+*buflen);
  *(PWORD)(*buf+*buflen)=w;
  *buflen+=2;
}



void ppackLEDWord(PBYTE *buf, int *buflen, DWORD d)
{
  *buf = (PBYTE)realloc(*buf, 4 + *buflen);
  *(PDWORD)(*buf + *buflen) = d;
  *buflen += 4;
}



/*void ppackLNTS(PBYTE *buf, int *buflen, const char *str)
{
  WORD len = strlennull(str);
  ppackWord(buf, buflen, len);
  *buf = (PBYTE)realloc(*buf, *buflen + len);
  memcpy(*buf + *buflen, str, len);
  *buflen += len;
}*/



void ppackLELNTS(PBYTE *buf, int *buflen, const char *str)
{
  WORD len = strlennull(str);
  ppackLEWord(buf, buflen, len);
  *buf = (PBYTE)realloc(*buf, *buflen + len);
  memcpy(*buf + *buflen, str, len);
  *buflen += len;
}



void ppackLELNTSfromDB(PBYTE *buf, int *buflen, const char *szSetting)
{
  DBVARIANT dbv;

  if (ICQGetContactSetting(NULL, szSetting, &dbv))
  {
    ppackLEWord(buf, buflen, 0);
  }
  else
  {
    ppackLELNTS(buf, buflen, dbv.pszVal);
    ICQFreeVariant(&dbv);
  }
}



// *** TLV based (!!! WORDs and DWORDs are LE !!!)
void ppackTLVByte(PBYTE *buf, int *buflen, BYTE b, WORD wType, BYTE always)
{
  if (!always && !b) return;

  *buf = (PBYTE)realloc(*buf, 5 + *buflen);
  *(PWORD)(*buf + *buflen) = wType;
  *(PWORD)(*buf + *buflen + 2) = 1;
  *(*buf + *buflen + 4) = b;
  *buflen += 5;
}



void ppackTLVWord(PBYTE *buf, int *buflen, WORD w, WORD wType, BYTE always)
{
  if (!always && !w) return;

  *buf = (PBYTE)realloc(*buf, 6 + *buflen);
  *(PWORD)(*buf + *buflen) = wType;
  *(PWORD)(*buf + *buflen + 2) = 2;
  *(PWORD)(*buf + *buflen + 4) = w;
  *buflen += 6;
}



void ppackTLVDWord(PBYTE *buf, int *buflen, DWORD d, WORD wType, BYTE always)
{
  if (!always && !d) return;

  *buf = (PBYTE)realloc(*buf, 8 + *buflen);
  *(PWORD)(*buf + *buflen) = wType;
  *(PWORD)(*buf + *buflen + 2) = 4;
  *(PDWORD)(*buf + *buflen + 4) = d;
  *buflen += 8;
}



void packTLVLNTS(PBYTE *buf, int *bufpos, const char *str, WORD wType)
{
  int len = strlennull(str) + 1;

  *(PWORD)(*buf + *bufpos) = wType;
  *(PWORD)(*buf + *bufpos + 2) = len + 2;
  *(PWORD)(*buf + *bufpos + 4) = len;
  memcpy(*buf + *bufpos + 6, str, len);
  *bufpos += len + 6;
}



void ppackTLVLNTS(PBYTE *buf, int *buflen, const char *str, WORD wType, BYTE always)
{
  int len = strlennull(str) + 1;

  if (!always && len < 2) return;

  *buf = (PBYTE)realloc(*buf, 6 + *buflen + len);
  packTLVLNTS(buf, buflen, str, wType);
}



void ppackTLVWordLNTS(PBYTE *buf, int *buflen, WORD w, const char *str, WORD wType, BYTE always)
{
  int len = strlennull(str) + 1;

  if (!always && len < 2 && !w) return;

  *buf = (PBYTE)realloc(*buf, 8 + *buflen + len);
  *(PWORD)(*buf + *buflen) = wType;
  *(PWORD)(*buf + *buflen + 2) = len + 4;
  *(PWORD)(*buf + *buflen + 4) = w;
  *(PWORD)(*buf + *buflen + 6) = len;
  memcpy(*buf + *buflen + 8, str, len);
  *buflen += len + 8;
}



void ppackTLVLNTSByte(PBYTE *buf, int *buflen, const char *str, BYTE b, WORD wType)
{
  int len = strlennull(str) + 1;

  *buf = (PBYTE)realloc(*buf, 7 + *buflen + len);
  *(PWORD)(*buf + *buflen) = wType;
  *(PWORD)(*buf + *buflen + 2) = len + 3;
  *(PWORD)(*buf + *buflen + 4) = len;
  memcpy(*buf + *buflen + 6, str, len);
  *(*buf + *buflen + 6 + len) = b;
  *buflen += len + 7;
}



void ppackTLVLNTSfromDB(PBYTE *buf, int *buflen, const char *szSetting, WORD wType)
{
  char szTmp[1024];
  char *str = "";

  if (!ICQGetContactStaticString(NULL, szSetting, szTmp, sizeof(szTmp)))
    str = szTmp;

  ppackTLVLNTS(buf, buflen, str, wType, 1);
}



void ppackTLVWordLNTSfromDB(PBYTE *buf, int *buflen, WORD w, const char *szSetting, WORD wType)
{
  char szTmp[1024];
  char *str = "";

  if (!ICQGetContactStaticString(NULL, szSetting, szTmp, sizeof(szTmp)))
    str = szTmp;

  ppackTLVWordLNTS(buf, buflen, w, str, wType, 1);
}



void ppackTLVLNTSBytefromDB(PBYTE *buf, int *buflen, const char *szSetting, BYTE b, WORD wType)
{
  char szTmp[1024];
  char *str = "";

  if (!ICQGetContactStaticString(NULL, szSetting, szTmp, sizeof(szTmp)))
    str = szTmp;

  ppackTLVLNTSByte(buf, buflen, str, b, wType);
}



void __fastcall unpackByte(BYTE** pSource, BYTE* byDestination)
{
  if (byDestination)
  {
    *byDestination = *(*pSource)++;
  }
  else
  {
    *pSource += 1;
  }
}



void __fastcall unpackWord(BYTE** pSource, WORD* wDestination)
{
  unsigned char *tmp = *pSource;

  if (wDestination)
  {
    *wDestination  = *tmp++ << 8;
    *wDestination |= *tmp++;

    *pSource = tmp;
  }
  else
  {
    *pSource += 2;
  }
}



void __fastcall unpackDWord(BYTE** pSource, DWORD* dwDestination)
{
  unsigned char *tmp = *pSource;

  if (dwDestination)
  {
    *dwDestination  = *tmp++ << 24;
    *dwDestination |= *tmp++ << 16;
    *dwDestination |= *tmp++ << 8;
    *dwDestination |= *tmp++;

    *pSource = tmp;
  }
  else
  {
    *pSource += 4;
  }
}



void __fastcall unpackLEWord(unsigned char **buf, WORD *w)
{
  unsigned char *tmp = *buf;

  if (w)
  {
    *w = (*tmp++);
    *w |= ((*tmp++) << 8);
  }
  else
    tmp += 2;

  *buf = tmp;
}



void __fastcall unpackLEDWord(unsigned char **buf, DWORD *dw)
{
  unsigned char *tmp = *buf;

  if (dw)
  {
    *dw = (*tmp++);
    *dw |= ((*tmp++) << 8);
    *dw |= ((*tmp++) << 16);
    *dw |= ((*tmp++) << 24);
  }
  else
    tmp += 4;

  *buf = tmp;
}



void unpackString(unsigned char **buf, char *string, WORD len)
{
  unsigned char *tmp = *buf;

  if (string)
  {
    while (len)  /* Can have 0x00 so go by len */
    {
      *string++ = *tmp++;
      len--;
    }
  }
  else
    tmp += len;

  *buf = tmp;
}



void unpackWideString(unsigned char **buf, WCHAR *string, WORD len)
{
  unsigned char *tmp = *buf;

  while (len > 1)
  {
    *string = (*tmp++ << 8);
    *string |= *tmp++;

    string++;
    len -= 2;
  }

  // We have a stray byte at the end, this means that the buffer had an odd length
  // which indicates an error.
  _ASSERTE(len == 0);
  if (len != 0)
  {
    // We dont copy the last byte but we still need to increase the buffer pointer
    // (we assume that 'len' was correct) since the calling function expects
    // that it is increased 'len' bytes.
    *tmp += len;
  }

  *buf = tmp;
}



void unpackTypedTLV(unsigned char **buf, int buflen, WORD type, WORD *ttype, WORD *tlen, char **tlv)
{
  WORD wType, wLen;
  unsigned char *tmp = *buf;

NextTLV:
  // Unpack type and length
  unpackWord(&tmp, &wType);
  unpackWord(&tmp, &wLen);
  buflen -= 4;

  if (wType != type && buflen >= wLen + 4)
  { // Not the right TLV, try next
    buflen -= wLen;
    tmp += wLen;
    goto NextTLV;
  }
  // Check buffer size
  if (wLen > buflen) wLen = buflen;

  // Make sure we have a good pointer
  if (tlv)
  {
    // Unpack and save value
    *tlv = (char *)SAFE_MALLOC(wLen + 1); // Add 1 for \0
    unpackString(&tmp, *tlv, wLen);
    *(*tlv + wLen) = '\0';
  }
  else
  {
    *tmp += wLen;
  }

  // Save type and length
  if (ttype)
    *ttype = wType;
  if (tlen)
    *tlen = wLen;

  // Increase source pointer
  *buf = tmp;
}



BOOL unpackUID(unsigned char** ppBuf, WORD* pwLen, DWORD *pdwUIN, uid_str* ppszUID)
{
  BYTE nUIDLen;
  char szUIN[UINMAXLEN+1];
  
  // Sender UIN
  unpackByte(ppBuf, &nUIDLen);
  *pwLen -= 1;
  
  if ((nUIDLen > *pwLen) || (nUIDLen == 0))
    return FALSE;

  if (nUIDLen <= UINMAXLEN)
  { // it can be uin, check
    unpackString(ppBuf, szUIN, nUIDLen);
    szUIN[nUIDLen] = '\0';
    *pwLen -= nUIDLen;

    if (IsStringUIN(szUIN))
    {
      *pdwUIN = atoi(szUIN);
      return TRUE;
    }
    else if (!ppszUID || !gbAimEnabled)
    {
      NetLog_Server("Malformed UIN in packet");
      return FALSE;
    }
    else
    { // go back
      *ppBuf -= nUIDLen;
      *pwLen += nUIDLen;
    }
  }
  else if (!ppszUID || ! gbAimEnabled)
  {
    NetLog_Server("Malformed UIN in packet");
    return FALSE;
  }
  if (!(*ppszUID)) return FALSE;

  unpackString(ppBuf, *ppszUID, nUIDLen);
  *pwLen -= nUIDLen;
  (*ppszUID)[nUIDLen] = '\0';

  *pdwUIN = 0; // this is how we determine aim contacts internally

  return TRUE;
}