diff options
author | George Hazan <george.hazan@gmail.com> | 2014-07-13 22:29:27 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2014-07-13 22:29:27 +0000 |
commit | 95336fc1843ab1ca7e2131b66ce67635e0ac19d2 (patch) | |
tree | c51b059b139b0e222036ed8eb48920b10f1f852a /protocols/IcqOscarJ/src/tlv.cpp | |
parent | db58e3f45a11aa0b19c5d0638a20cc5d404f40a6 (diff) |
- fix for 64-bit incompatibility;
- STL removed;
- copyright update;
- massive code cleaning;
- version bump
git-svn-id: http://svn.miranda-ng.org/main/trunk@9790 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/IcqOscarJ/src/tlv.cpp')
-rw-r--r-- | protocols/IcqOscarJ/src/tlv.cpp | 120 |
1 files changed, 36 insertions, 84 deletions
diff --git a/protocols/IcqOscarJ/src/tlv.cpp b/protocols/IcqOscarJ/src/tlv.cpp index 7c4eafca3a..ad1846e167 100644 --- a/protocols/IcqOscarJ/src/tlv.cpp +++ b/protocols/IcqOscarJ/src/tlv.cpp @@ -6,6 +6,7 @@ // Copyright © 2001-2002 Jon Keating, Richard Hughes
// Copyright © 2002-2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004-2009 Joe Kucera
+// Copyright © 2012-2014 Miranda NG Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
@@ -20,16 +21,13 @@ // 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.
-//
// -----------------------------------------------------------------------------
// DESCRIPTION:
//
// Helper functions for Oscar TLV chains
-//
// -----------------------------------------------------------------------------
-#include "icqoscar.h"
-
+#include "icqoscar.h"
/* set maxTlvs<=0 to get all TLVs in length, or a positive integer to get at most the first n */
oscar_tlv_chain* readIntoTLVChain(BYTE **buf, WORD wLen, int maxTlvs)
@@ -38,14 +36,13 @@ oscar_tlv_chain* readIntoTLVChain(BYTE **buf, WORD wLen, int maxTlvs) WORD now_tlv_len;
int len = wLen;
- if (!buf || !wLen) return NULL;
+ if (!buf || !wLen)
+ return NULL;
- while (len > 0) /* don't use unsigned variable for this check */
- {
+ while (len > 0) { /* don't use unsigned variable for this check */
now = (oscar_tlv_chain *)SAFE_MALLOC(sizeof(oscar_tlv_chain));
- if (!now)
- {
+ if (!now) {
disposeChain(&chain);
return NULL;
}
@@ -56,17 +53,13 @@ oscar_tlv_chain* readIntoTLVChain(BYTE **buf, WORD wLen, int maxTlvs) len -= 4;
if (now_tlv_len < 1)
- {
now->tlv.pData = NULL;
- }
- else if (now_tlv_len <= len)
- {
+ else if (now_tlv_len <= len) {
now->tlv.pData = (BYTE *)SAFE_MALLOC(now_tlv_len);
if (now->tlv.pData)
memcpy(now->tlv.pData, *buf, now_tlv_len);
}
- else
- { // the packet is shorter than it should be
+ else { // the packet is shorter than it should be
SAFE_FREE((void**)&now);
return chain; // give at least the rest of chain
}
@@ -97,8 +90,7 @@ oscar_tlv* oscar_tlv_chain::getTLV(WORD wType, WORD wIndex) int i = 0;
oscar_tlv_chain *list = this;
- while (list)
- {
+ while (list) {
if (list->tlv.wType == wType)
i++;
if (i >= wIndex)
@@ -109,45 +101,37 @@ oscar_tlv* oscar_tlv_chain::getTLV(WORD wType, WORD wIndex) return NULL;
}
-
WORD oscar_tlv_chain::getChainLength()
{
int len = 0;
oscar_tlv_chain *list = this;
- while (list)
- {
+ while (list) {
len += list->tlv.wLen + 4;
list = list->next;
}
return len;
}
-
oscar_tlv* oscar_tlv_chain::putTLV(WORD wType, WORD wLen, BYTE *pData, BOOL bReplace)
{
oscar_tlv *tlv = getTLV(wType, 1);
if (tlv && bReplace)
- {
SAFE_FREE((void**)&tlv->pData);
- }
- else
- {
+ else {
oscar_tlv_chain *last = this;
while (last && last->next)
last = last->next;
- if (last)
- {
+ if (last) {
last->next = (oscar_tlv_chain*)SAFE_MALLOC(sizeof(oscar_tlv_chain));
tlv = &last->next->tlv;
tlv->wType = wType;
}
}
- if (tlv)
- {
+ if (tlv) {
tlv->wLen = wLen;
tlv->pData = (PBYTE)SAFE_MALLOC(wLen);
memcpy(tlv->pData, pData, wLen);
@@ -155,19 +139,14 @@ oscar_tlv* oscar_tlv_chain::putTLV(WORD wType, WORD wLen, BYTE *pData, BOOL bRep return tlv;
}
-
oscar_tlv_chain* oscar_tlv_chain::removeTLV(oscar_tlv *tlv)
{
oscar_tlv_chain *list = this, *prev = NULL, *chain = this;
-
- while (list)
- {
- if (&list->tlv == tlv)
- {
+ while (list) {
+ if (&list->tlv == tlv) {
if (prev) // relink
prev->next = list->next;
- else if (list->next)
- { // move second item's tlv to the first item
+ else if (list->next) { // move second item's tlv to the first item
list->tlv = list->next->tlv;
list = list->next;
}
@@ -184,7 +163,6 @@ oscar_tlv_chain* oscar_tlv_chain::removeTLV(oscar_tlv *tlv) return chain;
}
-
WORD oscar_tlv_chain::getLength(WORD wType, WORD wIndex)
{
oscar_tlv *tlv = getTLV(wType, wIndex);
@@ -194,7 +172,6 @@ WORD oscar_tlv_chain::getLength(WORD wType, WORD wIndex) return 0;
}
-
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
/* Values are returned in MSB format */
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
@@ -204,53 +181,44 @@ DWORD oscar_tlv_chain::getDWord(WORD wType, WORD wIndex) DWORD dw = 0;
oscar_tlv *tlv = getTLV(wType, wIndex);
- if (tlv && tlv->wLen >= 4)
- {
- dw |= (*((tlv->pData)+0) << 24);
- dw |= (*((tlv->pData)+1) << 16);
- dw |= (*((tlv->pData)+2) << 8);
- dw |= (*((tlv->pData)+3));
+ if (tlv && tlv->wLen >= 4) {
+ dw |= (*((tlv->pData) + 0) << 24);
+ dw |= (*((tlv->pData) + 1) << 16);
+ dw |= (*((tlv->pData) + 2) << 8);
+ dw |= (*((tlv->pData) + 3));
}
return dw;
}
-
WORD oscar_tlv_chain::getWord(WORD wType, WORD wIndex)
{
WORD w = 0;
oscar_tlv *tlv = getTLV(wType, wIndex);
- if (tlv && tlv->wLen >= 2)
- {
- w |= (*((tlv->pData)+0) << 8);
- w |= (*((tlv->pData)+1));
+ if (tlv && tlv->wLen >= 2) {
+ w |= (*((tlv->pData) + 0) << 8);
+ w |= (*((tlv->pData) + 1));
}
return w;
}
-
BYTE oscar_tlv_chain::getByte(WORD wType, WORD wIndex)
{
BYTE b = 0;
oscar_tlv *tlv = getTLV(wType, wIndex);
if (tlv && tlv->wLen)
- {
b = *(tlv->pData);
- }
return b;
}
-
int oscar_tlv_chain::getNumber(WORD wType, WORD wIndex)
{
oscar_tlv *tlv = getTLV(wType, wIndex);
-
- if (tlv)
- {
+ if (tlv) {
if (tlv->wLen == 1)
return getByte(wType, wIndex);
else if (tlv->wLen == 2)
@@ -261,13 +229,10 @@ int oscar_tlv_chain::getNumber(WORD wType, WORD wIndex) return 0;
}
-
double oscar_tlv_chain::getDouble(WORD wType, WORD wIndex)
{
oscar_tlv *tlv = getTLV(wType, wIndex);
-
- if (tlv && tlv->wLen == 8)
- {
+ if (tlv && tlv->wLen == 8) {
BYTE *buf = tlv->pData;
double d = 0;
@@ -278,14 +243,12 @@ double oscar_tlv_chain::getDouble(WORD wType, WORD wIndex) return 0;
}
-
char* oscar_tlv_chain::getString(WORD wType, WORD wIndex)
{
char *str = NULL;
oscar_tlv *tlv = getTLV(wType, wIndex);
- if (tlv)
- {
+ if (tlv) {
str = (char*)SAFE_MALLOC(tlv->wLen + 1); /* For \0 */
if (!str) return NULL;
@@ -297,7 +260,6 @@ char* oscar_tlv_chain::getString(WORD wType, WORD wIndex) return str;
}
-
void disposeChain(oscar_tlv_chain **chain)
{
if (!chain || !*chain)
@@ -305,8 +267,7 @@ void disposeChain(oscar_tlv_chain **chain) oscar_tlv_chain *now = *chain;
- while (now)
- {
+ while (now) {
oscar_tlv_chain *next = now->next;
SAFE_FREE((void**)&now->tlv.pData);
@@ -317,19 +278,16 @@ void disposeChain(oscar_tlv_chain **chain) *chain = NULL;
}
-
oscar_tlv_record_list* readIntoTLVRecordList(BYTE **buf, WORD wLen, int nCount)
{
oscar_tlv_record_list *list = NULL, *last;
- while (wLen >= 2)
- {
+ while (wLen >= 2) {
WORD wRecordSize;
unpackWord(buf, &wRecordSize);
wLen -= 2;
- if (wRecordSize && wRecordSize <= wLen)
- {
+ if (wRecordSize && wRecordSize <= wLen) {
oscar_tlv_record_list *pRecord = (oscar_tlv_record_list*)SAFE_MALLOC(sizeof(oscar_tlv_record_list));
BYTE *pData = *buf;
@@ -337,8 +295,7 @@ oscar_tlv_record_list* readIntoTLVRecordList(BYTE **buf, WORD wLen, int nCount) wLen -= wRecordSize;
pRecord->item = readIntoTLVChain(&pData, wRecordSize, 0);
- if (pRecord->item)
- { // keep the order
+ if (pRecord->item) { // keep the order
if (list)
last->next = pRecord;
else
@@ -346,16 +303,15 @@ oscar_tlv_record_list* readIntoTLVRecordList(BYTE **buf, WORD wLen, int nCount) last = pRecord;
}
- else
- SAFE_FREE((void**)&pRecord);
+ else SAFE_FREE((void**)&pRecord);
}
- if (--nCount == 0) break;
+ if (--nCount == 0)
+ break;
}
return list;
}
-
void disposeRecordList(oscar_tlv_record_list** list)
{
if (!list || !*list)
@@ -363,8 +319,7 @@ void disposeRecordList(oscar_tlv_record_list** list) oscar_tlv_record_list *now = *list;
- while (now)
- {
+ while (now) {
oscar_tlv_record_list *next = now->next;
disposeChain(&now->item);
@@ -375,13 +330,10 @@ void disposeRecordList(oscar_tlv_record_list** list) *list = NULL;
}
-
oscar_tlv_chain* oscar_tlv_record_list::getRecordByTLV(WORD wType, int nValue)
{
oscar_tlv_record_list *list = this;
-
- while (list)
- {
+ while (list) {
if (list->item && list->item->getTLV(wType, 1) && list->item->getNumber(wType, 1) == nValue)
return list->item;
list = list->next;
|