summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <PromyLOPh@gmail.com>2008-08-08 19:37:31 +0200
committerLars-Dominik Braun <PromyLOPh@gmail.com>2008-08-08 19:37:31 +0200
commitb55cf2b0641d67f9832b14285af741ef0ed9d8bf (patch)
treecf3596f231dd0a27892fc138060062e70aeb1e79
parente1cb484fe66410e45363f29b167fedf19f236ded (diff)
downloadpianobar-b55cf2b0641d67f9832b14285af741ef0ed9d8bf.tar.gz
pianobar-b55cf2b0641d67f9832b14285af741ef0ed9d8bf.tar.bz2
pianobar-b55cf2b0641d67f9832b14285af741ef0ed9d8bf.zip
Several code optimizations
-rw-r--r--libpiano/src/crypt.c16
-rw-r--r--libpiano/src/http.c11
-rw-r--r--libpiano/src/xml.c9
3 files changed, 19 insertions, 17 deletions
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[] = {"&&amp;", "'&apos;", "\"&quot;", "<&lt;",
">&gt;", 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++;
}