From b55cf2b0641d67f9832b14285af741ef0ed9d8bf Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Fri, 8 Aug 2008 19:37:31 +0200 Subject: Several code optimizations --- libpiano/src/crypt.c | 16 ++++++++-------- libpiano/src/http.c | 11 +++++------ libpiano/src/xml.c | 9 ++++++--- 3 files changed, 19 insertions(+), 17 deletions(-) (limited to 'libpiano') diff --git a/libpiano/src/crypt.c b/libpiano/src/crypt.c index 7a17927..dc5bf7b 100644 --- a/libpiano/src/crypt.c +++ b/libpiano/src/crypt.c @@ -37,19 +37,19 @@ THE SOFTWARE. */ void PianoHexToInts (const char *strHex, unsigned int **retInts, size_t *retIntsN) { - size_t i; + size_t i, strHexN = strlen (strHex); char hexInt[9]; - unsigned int *arrInts = calloc (strlen (strHex) / 8, sizeof (*arrInts)); + unsigned int *arrInts = calloc (strHexN / 8, sizeof (*arrInts)); /* FIXME: error handling: string too short, e.g. */ /* unsigned int = 4 bytes, 8 chars in hex */ - for (i = 0; i < strlen (strHex); i += 8) { + for (i = 0; i < strHexN; i += 8) { memcpy (hexInt, strHex+i, sizeof (hexInt)-1); sscanf (hexInt, "%x", &arrInts[i/8]); } *retInts = arrInts; /* FIXME: copy & waste */ - *retIntsN = strlen (strHex) / 8, sizeof (*arrInts); + *retIntsN = strHexN / 8, sizeof (*arrInts); } /* decipher int array; reverse engineered from pandora source @@ -138,12 +138,12 @@ char *PianoDecryptString (const char *strInput) { */ void PianoBytesToInts (const char *strInput, unsigned int **retArrInts, size_t *retArrIntsN) { - size_t i, j, neededStrLen = strlen (strInput); + size_t i, j, neededStrLen, strInputN = strlen (strInput); unsigned int *arrInts; char shift; /* blowfish encrypts two 4 byte blocks */ - neededStrLen = strlen (strInput); + neededStrLen = strInputN; if (neededStrLen % 8 != 0) { /* substract overhead and add full 8 byte block */ neededStrLen = neededStrLen - (neededStrLen % 8) + 8; @@ -155,9 +155,9 @@ void PianoBytesToInts (const char *strInput, unsigned int **retArrInts, shift = 24; i = 0; j = 0; - while (i < strlen (strInput)) { + while (i < strInputN) { shift = 24; - while (shift >= 0 && i < strlen (strInput)) { + while (shift >= 0 && i < strInputN) { arrInts[i/4] |= strInput[i] << shift; shift -= 8; i++; diff --git a/libpiano/src/http.c b/libpiano/src/http.c index 91941d9..88d299c 100644 --- a/libpiano/src/http.c +++ b/libpiano/src/http.c @@ -39,12 +39,13 @@ THE SOFTWARE. size_t PianoCurlRetToVar (void *ptr, size_t size, size_t nmemb, void *stream) { char *charPtr = ptr; char *streamPtr = stream; + size_t streamPtrN = strlen (streamPtr); - if (strlen (streamPtr) + nmemb > PIANO_HTTP_BUFFER_SIZE) { + if (streamPtrN + nmemb > PIANO_HTTP_BUFFER_SIZE) { printf ("buffer overflow...\n"); return 0; } else { - memcpy (streamPtr+strlen(streamPtr), charPtr, size*nmemb); + memcpy (streamPtr+streamPtrN, charPtr, size*nmemb); return size*nmemb; } } @@ -78,8 +79,7 @@ PianoReturn_t PianoHttpPost (CURL *ch, const char *url, const char *postData, if (curl_easy_perform (ch) == CURLE_OK) { ret = PIANO_RET_OK; - *retData = calloc (strlen (curlRet) + 1, sizeof (char)); - strcpy (*retData, curlRet); + *retData = strdup (curlRet); } else { ret = PIANO_RET_NET_ERROR; *retData = NULL; @@ -111,8 +111,7 @@ PianoReturn_t PianoHttpGet (CURL *ch, const char *url, char **retData) { if (curl_easy_perform (ch) == CURLE_OK) { ret = PIANO_RET_OK; - *retData = calloc (strlen (curlRet) + 1, sizeof (char)); - strcpy (*retData, curlRet); + *retData = strdup (curlRet); } else { ret = PIANO_RET_NET_ERROR; *retData = NULL; diff --git a/libpiano/src/xml.c b/libpiano/src/xml.c index 30f4c2e..c0d388f 100644 --- a/libpiano/src/xml.c +++ b/libpiano/src/xml.c @@ -617,7 +617,8 @@ char *PianoXmlEncodeString (const char *s) { char *replacements[] = {"&&", "''", "\""", "<<", ">>", NULL}; char **r; - char *sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut)); + char *sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut)), + *sOutCurr = sOut; char found; while (*s != '\0') { @@ -626,13 +627,15 @@ char *PianoXmlEncodeString (const char *s) { while (*r != NULL) { if (*s == *r[0]) { found = 1; - strcat (sOut, (*r) + 1); + strcat (sOutCurr, (*r) + 1); + sOutCurr += strlen ((*r) + 1); break; } r++; } if (!found) { - strncat (sOut, s, 1); + *sOutCurr = *s; + sOutCurr++; } s++; } -- cgit v1.2.3