Skip to content

Commit

Permalink
piano: Make sync response parser NUL-byte aware
Browse files Browse the repository at this point in the history
  • Loading branch information
PromyLOPh committed Jan 20, 2012
1 parent 9a380af commit d44f61b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
13 changes: 10 additions & 3 deletions src/libpiano/crypt.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2008-2010
Copyright (c) 2008-2011
Lars-Dominik Braun <lars@6xq.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -43,20 +43,23 @@ THE SOFTWARE.
/* decrypt hex-encoded, blowfish-crypted string: decode 2 hex-encoded blocks,
* decrypt, byteswap
* @param hex string
* @param decrypted string length (without trailing NUL)
* @return decrypted string or NULL
*/
#define INITIAL_SHIFT 28
#define SHIFT_DEC 4
char *PianoDecryptString (const char * const s) {
char *PianoDecryptString (const char * const s, size_t * const retSize) {
const unsigned char *strInput = (const unsigned char *) s;
/* hex-decode => strlen/2 + null-byte */
uint32_t *iDecrypt;
size_t decryptedSize;
char *strDecrypted;
unsigned char shift = INITIAL_SHIFT, intsDecoded = 0, j;
/* blowfish blocks, 32-bit */
uint32_t f, l, r, lrExchange;

if ((iDecrypt = calloc (strlen ((const char *) strInput)/2/sizeof (*iDecrypt)+1,
decryptedSize = strlen ((const char *) strInput)/2;
if ((iDecrypt = calloc (decryptedSize/sizeof (*iDecrypt)+1,
sizeof (*iDecrypt))) == NULL) {
return NULL;
}
Expand Down Expand Up @@ -112,6 +115,10 @@ char *PianoDecryptString (const char * const s) {
++strInput;
}

if (retSize != NULL) {
*retSize = decryptedSize;
}

return strDecrypted;
}
#undef INITIAL_SHIFT
Expand Down
4 changes: 2 additions & 2 deletions src/libpiano/crypt.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2008-2010
Copyright (c) 2008-2011
Lars-Dominik Braun <lars@6xq.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -24,7 +24,7 @@ THE SOFTWARE.
#ifndef _CRYPH_H
#define _CRYPT_H

char *PianoDecryptString (const char *strInput);
char *PianoDecryptString (const char * const, size_t * const);
char *PianoEncryptString (const char *strInput);

#endif /* _CRYPT_H */
27 changes: 13 additions & 14 deletions src/libpiano/piano.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,24 +900,23 @@ PianoReturn_t PianoResponse (PianoHandle_t *ph, PianoRequest_t *req) {

/* abusing parseNarrative; has same xml structure */
ret = PianoXmlParseNarrative (req->responseData, &cryptedTimestamp);
if (cryptedTimestamp != NULL) {
if (ret == PIANO_RET_OK && cryptedTimestamp != NULL) {
unsigned long timestamp = 0;
time_t realTimestamp = time (NULL);
char *decryptedTimestamp = NULL, *decryptedPos = NULL;
unsigned char i = 4;

if ((decryptedTimestamp = PianoDecryptString (cryptedTimestamp)) != NULL) {
decryptedPos = decryptedTimestamp;
/* skip four bytes garbage? at beginning */
while (i-- > 0 && *decryptedPos++ != '\0');
timestamp = strtoul (decryptedPos, NULL, 0);
const time_t realTimestamp = time (NULL);
char *decryptedTimestamp = NULL;
size_t decryptedSize;

ret = PIANO_RET_ERR;
if ((decryptedTimestamp = PianoDecryptString (cryptedTimestamp,
&decryptedSize)) != NULL && decryptedSize > 4) {
/* skip four bytes garbage(?) at beginning */
timestamp = strtoul (decryptedTimestamp+4, NULL, 0);
ph->timeOffset = realTimestamp - timestamp;

free (decryptedTimestamp);
ret = PIANO_RET_CONTINUE_REQUEST;
}
free (cryptedTimestamp);
free (decryptedTimestamp);
}
ret = PIANO_RET_CONTINUE_REQUEST;
free (cryptedTimestamp);
++reqData->step;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libpiano/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static void PianoXmlParsePlaylistCb (const char *key, const ezxml_t value,
/* don't try to decrypt if string is too short (=> invalid memory
* reads/writes) */
if (valueStrN > urlTailN &&
(urlTail = PianoDecryptString (urlTailCrypted)) != NULL) {
(urlTail = PianoDecryptString (urlTailCrypted, NULL)) != NULL) {
if ((song->audioUrl = calloc (valueStrN + 1,
sizeof (*song->audioUrl))) != NULL) {
memcpy (song->audioUrl, valueStr, valueStrN - urlTailN);
Expand Down

0 comments on commit d44f61b

Please sign in to comment.