-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

i was going to reinvent char szUin[13]; snprintf yet another time... but i 
said to my self: `What the hell', so here is a clean up of the code 
instead of reinventing uin2ascii in different ways each times.

some of the call were potentially wrong as:
- -    len[0] = snprintf(uin, 12, "%lu", *iter);
because snprintf can return -1 if the uin is larger than 12 digits. i know 
that this can't happen in an ideal licq, but just in case :)

 -juan
-----BEGIN PGP SIGNATURE-----

iD8DBQE9VX3/UMlRieHkprgRAuWjAJ0SHlkYeuO0PsCwSan2ZcUwJcFM1QCeJjg7
h3x/7iI7+q85NxkSNkwNkow=
=s54g
-----END PGP SIGNATURE-----
Index: src/icqd-srv.cpp
===================================================================
RCS file: /cvsroot/licq/licq/src/icqd-srv.cpp,v
retrieving revision 1.54
diff -u -d -p -r1.54 icqd-srv.cpp
--- src/icqd-srv.cpp	6 Aug 2002 21:31:37 -0000	1.54
+++ src/icqd-srv.cpp	10 Aug 2002 20:44:56 -0000
@@ -39,9 +39,8 @@ void CICQDaemon::icqAddUser(unsigned lon
 
     SendEvent_Server(pStart);
 
-    char szUin[13];
-    snprintf(szUin, 12, "%lu", _nUin);
-    szUin[12] = 0;
+    char szUin[MAX_UIN_DIGITS];
+    uin2ascii(szUin,sizeof(szUin), _nUin);
 
     CPU_AddToServerList *pAdd = new CPU_AddToServerList(szUin, ICQ_ROSTxNORMAL, _bServerOnly);
     gLog.Info("%sAdding %ld to server list...\n", L_SRVxSTR, _nUin);
@@ -114,8 +113,8 @@ void CICQDaemon::icqRemoveUser(unsigned 
     ICQUser *u = gUserManager.FetchUser(_nUin, LOCK_W);
     unsigned short nGSID = u->GetGSID();
 		unsigned short nSID = u->GetSID();
-		char szUin[13];
-		snprintf(szUin, 13, "%ld", _nUin);
+		char szUin[MAX_UIN_DIGITS];
+		uin2ascii(szUin, sizeof(szUin), _nUin);
     u->SetGSID(0);
     gUserManager.DropUser(u);
 
@@ -855,9 +854,8 @@ unsigned long CICQDaemon::icqLogon(unsig
     gLog.Error("%sNo password set.  Edit ~/.licq/owner.uin and fill in the password field.\n", L_ERRORxSTR);
     return 0;
   }
-  char szUin[13];
-  snprintf(szUin, 12, "%ld", o->Uin());
-  szUin[12] = 0;
+  char szUin[MAX_UIN_DIGITS];
+  uin2ascii(szUin, sizeof(szUin), o->Uin());
   char *passwd = strdup(o->Password());
   unsigned long status = o->AddStatusFlags(logonStatus);
   gUserManager.DropOwner();
Index: src/icqpacket.cpp
===================================================================
RCS file: /cvsroot/licq/licq/src/icqpacket.cpp,v
retrieving revision 1.51
diff -u -d -p -r1.51 icqpacket.cpp
--- src/icqpacket.cpp	7 Aug 2002 06:35:22 -0000	1.51
+++ src/icqpacket.cpp	10 Aug 2002 20:44:58 -0000
@@ -897,13 +897,12 @@ CPU_GenericUinList::CPU_GenericUinList(U
 {
   char len[2];
   len[1] = '\0';
-  char contacts[uins.size()*13+1];
+  char contacts[uins.size()*MAX_UIN_DIGITS+1];
   contacts[0] = '\0';
 
   for (UinList::iterator iter = uins.begin(); iter != uins.end(); iter++) {
-    char uin[13];
-    uin[12] = '\0';
-    len[0] = snprintf(uin, 12, "%lu", *iter);
+    char uin[MAX_UIN_DIGITS];
+    len[0] = uin2ascii(uin, sizeof(uin),*iter);
     strcat(contacts, len);
     strcat(contacts, uin);
   }
@@ -917,9 +916,8 @@ CPU_GenericUinList::CPU_GenericUinList(U
 CPU_GenericUinList::CPU_GenericUinList(unsigned long _nUin, unsigned short family, unsigned short Subtype)
   : CPU_CommonFamily(family, Subtype)
 {
-  char uin[13];
-  uin[12] = '\0';
-  int n = snprintf(uin, 12, "%lu", _nUin);
+  char uin[MAX_UIN_DIGITS];
+  int n = uin2ascii( uin, sizeof(uin), _nUin );
 
   m_nSize += n+1;
   InitBuffer();
@@ -1030,9 +1028,8 @@ CPU_ThroughServer::CPU_ThroughServer(uns
 	m_nMsgType = msgType;
 
   int msgLen = szMessage ? strlen(szMessage) : 0;
-  char uin[13];
-  uin[12] = '\0';
-  int nUinLen = snprintf(uin, 12, "%lu", nDestinationUin);
+  char uin[MAX_UIN_DIGITS];
+  int nUinLen = uin2ascii(uin,sizeof(uin),nDestinationUin); 
   unsigned short nFormat = 0;
   int nTypeLen = 0, nTLVType = 0;
   CBuffer tlvData;
@@ -1106,8 +1103,8 @@ CPU_AdvancedMessage::CPU_AdvancedMessage
 		unsigned char _nMsgFlags, bool _bAck)
   : CPU_CommonFamily(ICQ_SNACxFAM_MESSAGE, ICQ_SNACxMSG_SENDxSERVER)
 {
-	char szUin[13];
-	int nUinLen = snprintf(szUin, 12, "%ld", u->Uin());
+	char szUin[MAX_UIN_DIGITS];
+	int nUinLen = uin2ascii( szUin, sizeof(szUin), u->Uin() );
 
 	m_nSize += 123 + nUinLen;
 
@@ -1124,8 +1121,8 @@ void CPU_AdvancedMessage::InitBuffer()
 	ICQOwner *o = gUserManager.FetchOwner(LOCK_R);
 	unsigned short nStatus = o->Status();
 
-	char szUin[13];
-	int nUinLen = snprintf(szUin, 12, "%ld", m_pUser->Uin());
+	char szUin[MAX_UIN_DIGITS];
+	int nUinLen = uin2ascii( szUin,sizeof(szUin), m_pUser->Uin() );
 
 	unsigned short nSequence = m_pUser->Sequence(true);
 
@@ -1297,11 +1294,10 @@ CPU_AckThroughServer::CPU_AckThroughServ
 																					 unsigned short len)
   : CPU_CommonFamily(ICQ_SNACxFAM_MESSAGE, 0x0b)
 {
-  char szUin[13];
+  char szUin[MAX_UIN_DIGITS];
   int nUinLen;
 
-  snprintf(szUin, 13, "%lu", Uin);
-  nUinLen = strlen(szUin);
+  nUinLen = uin2ascii(szUin,sizeof(szUin),Uin);
 
   m_nSize += 75 + nUinLen + len;
 
@@ -1362,9 +1358,8 @@ CPU_SendSms::CPU_SendSms(unsigned long n
   gUserManager.DropUser(u);
   
   ICQOwner *o = gUserManager.FetchOwner(LOCK_R);
-  char szUin[13];
-  szUin[12] = '\0';
-  snprintf(szUin, 12, "%lu", o->Uin());
+  char szUin[MAX_UIN_DIGITS];
+  uin2ascii( szUin, sizeof(szUin), o->Uin() );
 
   snprintf(szXmlStr, 460, "<icq_sms_message><destination>%s</destination><text>%.160s</text><codepage>1252</codepage><encoding>utf8</encoding><senders_UIN>%s</senders_UIN><senders_name>%s</senders_name><delivery_receipt>Yes</delivery_receipt><time>%s</time></icq_sms_message>",
 	   szCellularNumber, szMessage, szUin, o->GetAlias(), szTime);
@@ -1400,9 +1395,8 @@ CPU_SendSms::CPU_SendSms(unsigned long n
 CPU_RequestAuth::CPU_RequestAuth(unsigned long _nUin, const char *_szMsg)
   : CPU_CommonFamily(ICQ_SNACxFAM_LIST, ICQ_SNACxLIST_AUTHxREQ)
 {
-  char szUin[13];
-  int nUinLen = snprintf(szUin, 12, "%lu", _nUin);
-  szUin[12] = '\0';
+  char szUin[MAX_UIN_DIGITS];
+  int nUinLen = uin2ascii( szUin, sizeof(szUin), _nUin );
   int nMsgLen = strlen(_szMsg);
 
   m_nSize += nUinLen + nMsgLen + 5;
@@ -1489,10 +1483,9 @@ CPU_ExportToServerList::CPU_ExportToServ
     ICQUser *pUser = gUserManager.FetchUser(*i, LOCK_R);
     if (pUser)
     {
-      char szTmp[13];
-      szTmp[12] = '\0';
+      char szTmp[MAX_UIN_DIGITS];
 
-      nSize += snprintf(szTmp, 12, "%lu", *i);
+      nSize += uin2ascii(szTmp,sizeof(szTmp),*i);
       nSize += 10;
 
       int nAliasLen = strlen(pUser->GetAlias());
@@ -1507,8 +1500,7 @@ CPU_ExportToServerList::CPU_ExportToServ
 
   for (i = uins.begin(); i != uins.end(); i++)
   {
-    char szUin[13];
-    szUin[12] = '\0';
+    char szUin[MAX_UIN_DIGITS];
     int nLen;
     int nAliasSize = 0;
 
@@ -1537,7 +1529,7 @@ CPU_ExportToServerList::CPU_ExportToServ
     nAliasSize = strlen(u->GetAlias());
     gUserManager.DropUser(u);
 
-    nLen = snprintf(szUin, 12, "%lu", *i);
+    nLen = uin2ascii(  szUin, sizeof(szUin),*i );
     buffer->PackUnsignedShortBE(nLen);
     buffer->Pack(szUin, nLen);
     buffer->PackUnsignedShortBE(m_nGSID);
Index: src/support.c
===================================================================
RCS file: /cvsroot/licq/licq/src/support.c,v
retrieving revision 1.4
diff -u -d -p -r1.4 support.c
--- src/support.c	15 May 2002 09:36:32 -0000	1.4
+++ src/support.c	10 Aug 2002 20:44:58 -0000
@@ -68,6 +68,16 @@ int strlen_safe(const char *sz)
   return sz == NULL ? 0 : strlen(sz);
 }
 
+int uin2ascii (  char *szBuf, size_t nSize, unsigned long nUin )
+{
+	int nRet;
+
+	nRet = snprintf( szBuf, nSize-1,  "%lu", nUin ); 
+	szBuf[nSize - 1 ] = '\0';
+
+	return (nRet == -1 ) ? nSize - 1 : nRet;
+}
+
 /*=====ALPHASORT==============================================================*/
 int my_alphasort(const void *a, const void *b)
 /*const struct dirent **a, const struct dirent **b)*/
Index: src/support.h
===================================================================
RCS file: /cvsroot/licq/licq/src/support.h,v
retrieving revision 1.3
diff -u -d -p -r1.3 support.h
--- src/support.h	15 May 2002 09:37:21 -0000	1.3
+++ src/support.h	10 Aug 2002 20:44:58 -0000
@@ -34,6 +34,15 @@ int strlen_safe(const char *);
 int scandir_alpha_r(const char *dir, struct dirent ***namelist,
               int (*select)(const struct dirent *));
 
+#define MAX_UIN_DIGITS 13
+
+/* uin2ascii 
+ *	Converts an uin number to an ascii `string'
+ * Returns
+ *	Number of digits for the uin 
+ */
+int uin2ascii ( char *szBuf, size_t nSize, unsigned long nUin );
+
 
 /* Cross-platform support functions */
 

Reply via email to