libid3v1.c
author Mikhail Gusarov <dottedmag@dottedmag.net>
Thu Dec 06 22:55:16 2007 +0600 (2 years ago)
changeset 15 371ca73d3311
parent 7e1c3c3af8feb
permissions -rw-r--r--
Added tag 0.1 for changeset 729e5fe3d280
     1 /*
     2  * libid3v1 (http://libid3v1.dottedmag.net/)
     3  *
     4  * Copyright (c) 2006,2007 Mikhail Gusarov <dottedmag@dottedmag.net>
     5  *
     6  * Permission is hereby granted, free of charge, to any person
     7  * obtaining a copy of this software and associated documentation
     8  * files (the "Software"), to deal in the Software without
     9  * restriction, including without limitation the rights to use, copy,
    10  * modify, merge, publish, distribute, sublicense, and/or sell copies
    11  * of the Software, and to permit persons to whom the Software is
    12  * furnished to do so, subject to the following conditions:
    13  *
    14  * The above copyright notice and this permission notice shall be
    15  * included in all copies or substantial portions of the Software.
    16  *
    17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
    21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
    22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    24  * SOFTWARE.
    25  */
    26 
    27 #include <sys/types.h>
    28 #include <unistd.h>
    29 #include <errno.h>
    30 #include <string.h>
    31 
    32 #include "libid3v1.h"
    33 
    34 #define ID3V1_TAG_LENGTH 128
    35 #define ID3V1_HEADER_LENGTH 3
    36 #define ID3V1_HEADER "TAG"
    37 
    38 static int read_all(int fh, char* buf, size_t size)
    39 {
    40     while(size > 0) {
    41         ssize_t read_ = read(fh, buf, size);
    42         if (read_ == -1 && (errno == EAGAIN || errno == EINTR)) continue;
    43         if (read_ == -1) return 0;
    44         if (read_ == 0) break;
    45         buf += read_;
    46         size -= read_;
    47     }
    48     return size == 0;
    49 }
    50 
    51 int id3v1_parse(int fh, id3v1_t* tag)
    52 {
    53   char rawtag[ID3V1_TAG_LENGTH];
    54 
    55   if ((off_t)-1 == lseek(fh, (off_t)-128, SEEK_END)) return -1;
    56   if (!read_all(fh, rawtag, ID3V1_TAG_LENGTH)) return -1;
    57   if (memcmp(ID3V1_HEADER, rawtag, ID3V1_HEADER_LENGTH)) return 0;
    58 
    59   memset(tag, 0, sizeof(id3v1_t));
    60   memcpy(tag->songname, rawtag + 3, 30);
    61   memcpy(tag->artist, rawtag + 33, 30);
    62   memcpy(tag->album, rawtag + 63, 30);
    63   memcpy(tag->year, rawtag + 93, 4);
    64   memcpy(tag->comment, rawtag + 97, 30);
    65   tag->genre = rawtag[127];
    66 
    67   if (tag->comment[28] == 0) {
    68     tag->track = tag->comment[29];
    69     tag->comment[29] = 0;
    70   }
    71 
    72   return 1;
    73 }
    74 
    75 static char* genres[] = {
    76   "Unknown", "Blues", "Classic Rock", "Country", "Dance", "Disco",
    77   "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
    78   "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative",
    79   "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient",
    80   "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical",
    81   "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise",
    82   "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative",
    83   "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave",
    84   "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream",
    85   "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap",
    86   "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychedelic",
    87   "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
    88   "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock",
    89 
    90   /* The following genres are Winamp extenisons */
    91 
    92   "Folk", "Folk/Rock", "National Folk", "Swing", "Fast Fusion", "Bebob",
    93   "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
    94   "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock",
    95   "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
    96   "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
    97   "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
    98   "Folklore", "Ballad", "Power Ballad", "Rhytmic Soul", "Freestyle", "Duet",
    99   "Punk Rock", "Drum Solo", "A Capella", "Euro-House", "Dance Hall"
   100 };
   101 
   102 const char* id3v1_genre_name(short genre)
   103 {
   104   if (genre < 0 || genre >= sizeof(genres) / sizeof(genres[0])) return NULL;
   105   return genres[genre];
   106 }