diff src/xs_length.c @ 87:94497283affa

Various fixes and improvements
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 05 Oct 2003 10:38:03 +0000
parents 9533b78f1a7d
children 2607683bc9eb
line wrap: on
line diff
--- a/src/xs_length.c	Sat Oct 04 08:21:01 2003 +0000
+++ b/src/xs_length.c	Sun Oct 05 10:38:03 2003 +0000
@@ -198,7 +198,7 @@
 	iResult = -1;
 
  /* Ignore and skip the possible attributes */
- while (!isspace(pcStr[*piPos])) (*piPos)++;
+ while (pcStr[*piPos] && !isspace(pcStr[*piPos])) (*piPos)++;
  
  return iResult;
 }
@@ -264,6 +264,8 @@
 	(guint *) &(tmpNode->md5Hash[14]), (guint *) &(tmpNode->md5Hash[15]));
 
 	/* Get playtimes */
+	if (inLine[linePos] != 0)
+	{
 	if (inLine[linePos] != '=')
 		{
 		XSERR("'=' expected in SongLengthDB file '%s' line #%d, column #%d\n",
@@ -295,6 +297,7 @@
 			xs_db_node_free(tmpNode);
 		}
 	}
+	}
  } else
  if ((inLine[0] != ';') && (inLine[0] != '['))
  	{
@@ -452,6 +455,7 @@
  return fread(s, sizeof(gchar), l, f);
 }
 
+#define MAX_MEMBUF (80*1024)
 
 gint xs_get_sid_hash(gchar *pcFilename, t_xs_md5hash hash)
 {
@@ -459,8 +463,9 @@
  t_xs_md5state inState;
  t_xs_psidv1_header psidH;
  t_xs_psidv2_header psidH2;
- guint8 *songData, ib8[2], i8;
- gint iIndex, iRes, songDataLen;
+ guint8 songData[MAX_MEMBUF+1], ib8[2], i8;
+ gint iIndex, iRes;
+
 
  /* Try to open the file */
  if ((inFile = fopen(pcFilename, "rb")) == NULL)
@@ -495,39 +500,18 @@
  	psidH2.reserved		= rd_be16(inFile);
  	}
 
- /* Get data length and seek to data offset */
- fseek(inFile, 0L, SEEK_END);
- songDataLen = ftell(inFile) - psidH.dataOffset;
- fseek(inFile, psidH.dataOffset, SEEK_SET);
-
- /* Allocate memory */ 
- songData = (guint8 *) g_malloc(sizeof(guint8) * songDataLen);
- if (songData == NULL)
- 	{
- 	fclose(inFile);
- 	return -7;
- 	}
-
  /* Read data to buffer */
- iRes = fread(songData, sizeof(guint8), songDataLen, inFile);
+ iRes = fread(songData, sizeof(guint8), MAX_MEMBUF, inFile);
  fclose(inFile);
 
- if (iRes != songDataLen)
- 	{
- 	g_free(songData);
- 	return -9;
- 	}
- 
  /* Initialize and start MD5-hash calculation */
  xs_md5_init(&inState);
  if (psidH.loadAddress == 0)
  	/* COULD SOMEONE EXPLAIN WHY DO WE NEED THIS +2 STRIP???? */
-	xs_md5_append(&inState, songData+2, iRes-2);
+	xs_md5_append(&inState, &songData[2], iRes-2);
 	else
-	xs_md5_append(&inState, songData, iRes);
-	
- g_free(songData);
- 
+	xs_md5_append(&inState, &songData, iRes);
+
  /* Append header data to hash */
 #define XSADDHASH(QDATAB) { ib8[0] = (QDATAB & 0xff); ib8[1] = (QDATAB >> 8); xs_md5_append(&inState, (guint8 *) &ib8, sizeof(ib8)); }