File indexing completed on 2024-04-14 04:49:17

0001 /*
0002  * This file is part of WorkMan, the civilized CD player library
0003  * Copyright (C) 1991-1997 by Steven Grimm (original author)
0004  * Copyright (C) by Dirk Försterling <milliByte@DeathsDoor.com>
0005  * Copyright (C) 2004-2006 Alexander Kern <alex.kern@gmx.de>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Library General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Library General Public
0018  * License along with this library; if not, write to the Free
0019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0020  *
0021  *
0022  * establish connection to cddb server and get data
0023  * socket stuff gotten from gnu port of the finger command
0024  *
0025  * provided by Sven Oliver Moll
0026  *
0027  */
0028 
0029 #include <stdio.h>
0030 #include <stdlib.h>
0031 #include <strings.h>
0032 #include <string.h>
0033 #include <unistd.h>
0034 #include <sys/types.h>
0035 #include <sys/stat.h>
0036 #include <sys/signal.h>
0037 #include <ctype.h>
0038 #include <sys/time.h>
0039 #include <sys/socket.h>
0040 #include <netinet/in.h>
0041 #include <netdb.h>
0042 
0043 #include "include/wm_config.h"
0044 #include "include/wm_struct.h"
0045 #include "include/wm_helpers.h"
0046 #include "include/wm_cddb.h"
0047 #include "include/wm_cdrom.h"
0048 
0049 /*
0050  * Subroutine from cddb_discid
0051  */
0052 static int cddb_sum(int n)
0053 {
0054     char    buf[12],
0055         *p;
0056     int ret = 0;
0057 
0058     /* For backward compatibility this algorithm must not change */
0059     sprintf(buf, "%lu", (unsigned long)n);
0060     for (p = buf; *p != '\0'; p++)
0061       ret += (*p - '0');
0062 
0063     return (ret);
0064 } /* cddb_sum() */
0065 
0066 
0067 /*
0068  * Calculate the discid of a CD according to cddb
0069  */
0070 unsigned long cddb_discid(struct wm_drive *pdrive)
0071 {
0072     int i,
0073         t,
0074         n = 0, tracks;
0075 
0076     tracks = wm_cd_getcountoftracks(pdrive);
0077     if(!tracks)
0078         return (unsigned)-1;
0079 
0080     /* For backward compatibility this algorithm must not change */
0081     for (i = 0; i < tracks; i++) {
0082 
0083         n += cddb_sum(wm_cd_gettrackstart(pdrive, i + 1));
0084     /*
0085      * Just for demonstration (See below)
0086      *
0087      *  t += (wm_cd_getref()->trk[i+1].start / 75) -
0088      *       (wm_cd_getref()->trk[i  ].start / 75);
0089      */
0090     }
0091 
0092     /*
0093      * Mathematics can be fun. Example: How to reduce a full loop to
0094      * a simple statement. The discid algorhythm is so half-hearted
0095      * developed that it doesn't even use the full 32bit range.
0096      * But it seems to be always this way: The bad standards will be
0097      * accepted, the good ones turned down.
0098          * Boy, you pulled out the /75. This is not correct here, because
0099          * this calculation needs the integer division for both .start
0100          * fields.
0101          */
0102 
0103         t = (wm_cd_gettrackstart(pdrive, tracks + 1)) - (wm_cd_gettrackstart(pdrive, 1));
0104     return ((n % 0xff) << 24 | t << 8 | tracks);
0105 } /* cddb_discid() */
0106