daemonio

cid3v2.c - Insert/Edit tags on mp3 files - daemoniolabs

Oct 13th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.71 KB | None | 0 0
  1. /*
  2.  * [cid3v2.c - v2]
  3.  * Inserts some ID3v2 tags in a music file.
  4.  * The tags are:
  5.  * TIT2 : name of the music
  6.  * TPE1 : artist/band
  7.  * TALB : album name
  8.  * APIC : cover art
  9.  * TRCK : track number
  10.  * TCON : genre
  11.  *
  12.  * [Author]
  13.  * Marcos Paulo Ferreira (Daemonio)
  14.  * undefinido gmail com
  15.  * http://daemoniolabs.wordpress.com
  16.  *
  17.  * [How to compile]
  18.  * $ gcc -o cid3v2 cid3v2.c
  19.  *
  20.  * [How to use (examples)]
  21.  * 0) The following command will insert tags into
  22.  * a mp3 file. If the audio file already has tags in
  23.  * it, they will be replaced by the new ones.
  24.  *
  25.  * $ ./cid3v2 -t 'So What' -a 'Miles Davis' -l 'Kind Of Blue'\
  26.  *    --apic miles.jpg Miles_Davis-So_What.mp3
  27.  *
  28.  * 1) Inserting (= appending or replacing) tags in a audio file.
  29.  *
  30.  * $ cid3v2 --insert --album 'Kind Of Blue' Miles_Davis-So_What.mp3
  31.  *
  32.  * If the algum tag is already in the file so it will be replaced.
  33.  * If not, it will be inserted without touching the other tags.
  34.  *
  35.  * OBS: The file must be ID3v2.3.x
  36.  *
  37.  * 2) Dumping tags of an audio file:
  38.  *
  39.  * $ cid3v2 --dump Miles_Davis-So_What.mp3
  40.  *
  41.  * FILE : Miles_Davis-So_What.mp3
  42.  * TALB : Kind of Blue
  43.  * TPE1 : Miles Davis
  44.  * TIT2 : So What
  45.  *
  46.  * OBS: The file must be ID3v2.3.x
  47.  *
  48.  * 3) Removing ID3v2 tags (any version) of an audio file.
  49.  *
  50.  * $ cid3v2 --remove Miles_Davis-So_What.mp3
  51.  *
  52.  * Note that now the input file hasn't even a ID3v2 header
  53.  * anymore.
  54.  *
  55.  * 4) Inserting APIC frame to multiple files
  56.  *
  57.  * $ cid3v2 --insert --apic cover_art.jpg *.mp3
  58.  *
  59.  * Insert a cover art imagem to all the mp3 files of
  60.  * the current directory.
  61.  *
  62.  * [Notes]
  63.  * - rhythmbox has some problems with the cover art, so
  64.  *   sometimes it won't display the picture.
  65.  * - embedded APIC tag works fine in:
  66.  *   picard
  67.  *   windows media player
  68.  *   xmms (via plugin)
  69.  *   audacious
  70.  * - This program uses ID3v2.3.0 and some flags
  71.  *   are null by default.
  72.  * - This code was made/tested/compiled under a
  73.  *   linux box. I can't guarantee it will work
  74.  *   in others systems.
  75.  * - Only one file will be tagged by each run
  76.  *   of the program. You can create a script
  77.  *   to insert tags in multiple files.
  78.  *
  79.  * First version:
  80.  * Dom Set 25 13:07:08 BRT 2011
  81.  * Seg Set 26 21:35:48 BRT 2011
  82.  *
  83.  * Second version:
  84.  * Sab Out  8 13:06:28 BRT 2011
  85.  * Dom Out  9 15:45:46 BRT 2011
  86.  * Sex Out 14 01:05:30 BRT 2011 - Rest In Peace, dmr.
  87.  * Sab Out 29 01:39:37 BRST 2011
  88.  * Dom Dez  4 10:40:57 BRST 2011
  89.  */
  90. #include <stdio.h>
  91. #include <string.h>
  92. #include <stdlib.h>
  93. /* stat */
  94. #include <sys/stat.h>
  95. #include <sys/types.h>
  96. #include <unistd.h>
  97.  
  98. /* Am I crazy? ;D */
  99. #include <getopt.h> /* for getopt_long */
  100.  
  101. /* only two tag types for now */
  102. #define TEXT_TAG 0
  103. #define APIC_TAG 1
  104. #define INSERT_TAG 2
  105.  
  106. /* boolean values */
  107. #define TRUE 1
  108. #define FALSE 0
  109. /* for -vv */
  110. #define DOUBLE_VERBOSE 2
  111.  
  112.  /* template filename used by mkstemp() */
  113. #define FILENAME_TEMPLATE "./cid3v2XXXXXX"
  114.  
  115. /* supported tags */
  116. const char *TAGS[] = {"TIT2", "TPE1", "TALB", "APIC", "TRCK", "TCON"} ;
  117.  
  118. /* struct of a frame */
  119. struct ID3v2frame {
  120.     /* the next 10 bytes are part
  121.      * of header frame */
  122.     char id[4] ;
  123.     char charsize[4] ;
  124.     char flags[2] ;
  125.  
  126.     /* frame size as an int */
  127.     unsigned long int size ;
  128.  
  129.     /* TEXT or APIC */
  130.     int type ;
  131.  
  132.     /* body of the frame */
  133.     char *body ;
  134.  
  135.     /* a pointer to the next frame */
  136.     struct ID3v2frame *next ;
  137. } ;
  138.  
  139. /* global var: head of the linked list */
  140. struct ID3v2frame *framehead = NULL;
  141.  
  142. /* global var: true to show verbose info.
  143.  * the value DOUBLE_VERBOSE means that
  144.  * the user passed the -vv option. */
  145. int flagverbose = FALSE ;
  146.  
  147. /* malloc()s and exits if error */
  148. void *my_malloc(unsigned long int size) ;
  149.  
  150. /* fopen()s and exits if error */
  151. FILE *my_fopen(char *filename, char *mode) ;
  152.  
  153. /* free the mallocs */
  154. void freetroydavis() ;
  155.  
  156. /* free tags of input file and
  157.  * leave in the memory the tags
  158.  * passed to the command line. */
  159. void free_tags_of_input_file() ;
  160.  
  161. /* u long int to synch safe int */
  162. unsigned long int synchsafe(unsigned long int in) ;
  163.  
  164. /* synch to unsigned long int */
  165. /* thanks libtaggy ;) */
  166. unsigned long int ss2uli(char *buf) ;
  167.  
  168. /* put an u long int in a string representation */
  169. void set_size(char *buf, unsigned long int n) ;
  170.  
  171. /* string representation to u long int */
  172. unsigned long int get_size(char *buf) ;
  173.  
  174. /* returns image/<image extension> */
  175. char *get_MIMEtype(char *file) ;
  176.  
  177. /* open picture file and return its size in bytes */
  178. FILE *open_image_apic(char *file, unsigned long int *imgsize) ;
  179.  
  180. /* insert a frame in the linked list */
  181. void insertframe(char *body, int type, int index) ;
  182.  
  183. /* write a frame to stream */
  184. void frame_dump(struct ID3v2frame *frame, FILE *stream) ;
  185.  
  186. /* write ID3v2 header to stream */
  187. void header_dump(struct ID3v2frame *framehead, FILE *stream) ;
  188.  
  189. /* called by --help */
  190. void show_help() ;
  191.  
  192. /* TRUE if the tag `id' already exists in
  193.  * the input file */
  194. char tag_already_in(char *id) ;
  195.  
  196. /* increase the linked list with the tags of
  197.  * the input file. This function is called
  198.  * by the --insert option */
  199. void get_frames_from_file(unsigned long int headersize, FILE *inputfile) ;
  200.  
  201. /* dumps to stdout tags of input file on screen. */
  202. void dump_tags(unsigned long int headersize, FILE *inputfile) ;
  203.  
  204. int main(int argc, char **argv) {
  205.     /* options flags */
  206.     int flaginsert = FALSE ;
  207.     int flagremove = FALSE ;
  208.     int flagdump   = FALSE ;
  209.     int flagnooption = TRUE ;
  210.     /* TRUE if we have to write in the file */
  211.     int flaghasoutput = TRUE ;
  212.     /* return value of getopt_long() */
  213.     int opt ;
  214.     /* each byte of the music data */
  215.     char byte ;
  216.     /* input and output stream */
  217.     FILE *inputfile, *outputfile ;
  218.     /* each frame */
  219.     struct ID3v2frame *f ;
  220.     /* ID3v2 header of the inputfile (if it exists) */
  221.     char header[10] ;
  222.     /* the size of the ID3v2 header */
  223.     unsigned long int headersize ;
  224.  
  225.     char *inputfilename ;
  226.     int outputfilefd ;
  227.     char outputfilename[] = FILENAME_TEMPLATE;
  228.  
  229.     /* for getopt_long */
  230.     const struct option stoption[] =
  231.     {
  232.         {"title"  , required_argument, 0           , 't'},
  233.         {"artist" , required_argument, 0           , 'a'},
  234.         {"album"  , required_argument, 0           , 'l'},
  235.         {"apic"   , required_argument, 0           , 'p'},
  236.         {"track"  , required_argument, 0           , 'r'},
  237.         {"genre"  , required_argument, 0           , 'g'},
  238.         {"help"   , no_argument      , 0           , 'h'},
  239.         {"insert" , no_argument      , 0           , 'i'},
  240.         {"remove" , no_argument      , 0           , 'm'},
  241.         {"dump"   , no_argument      , 0           , 'd'},
  242.         {"verbose", no_argument      , 0           , 'v'},
  243.         {0        , 0                , 0           ,   0},
  244.     } ;
  245.  
  246.     if ( argc < 2 ) {
  247.         fprintf(stderr, "Try --help for more information.\n") ;
  248.         return -1 ;
  249.     }
  250.  
  251.     while ( (opt=getopt_long(argc, argv, "hvt:a:l:p:r:g:", stoption, NULL)) > 0 ) {
  252.         flagnooption = FALSE ; /* we have an option */
  253.  
  254.         switch(opt) {
  255.             case 'h':
  256.                 show_help() ;
  257.                 break;
  258.             case 't':
  259.                 insertframe(optarg, TEXT_TAG, 0) ;
  260.                 break ;
  261.             case 'a':
  262.                 insertframe(optarg, TEXT_TAG, 1) ;
  263.                 break ;
  264.             case 'l':
  265.                 insertframe(optarg, TEXT_TAG, 2) ;
  266.                 break ;
  267.             case 'p':
  268.                 insertframe(optarg, APIC_TAG, 3) ;
  269.                 break ;
  270.             case 'r':
  271.                 insertframe(optarg, TEXT_TAG, 4) ;
  272.                 break ;
  273.             case 'g':
  274.                 insertframe(optarg, TEXT_TAG, 5) ;
  275.                 break ;
  276.             case 'i':
  277.                 flaginsert = TRUE ;
  278.                 break ;
  279.             case 'v':
  280.                 /* for -v */
  281.                 if ( flagverbose == FALSE) {
  282.                     flagverbose |= TRUE ;
  283.                 /* for -vv */
  284.                 } else {
  285.                     flagverbose |= DOUBLE_VERBOSE ;
  286.                 }
  287.                 break ;
  288.             case 'm':
  289.                 flagremove = TRUE ;
  290.                 break ;
  291.             case 'd':
  292.                 flagdump   = TRUE ;
  293.                 flaghasoutput = FALSE ;
  294.                 break ;
  295.             default:
  296.                 /* strange options .... */
  297.                 return -1 ;
  298.         }
  299.     }
  300.  
  301.     /* I think it's not make sense */
  302.     if ( flagremove == TRUE && flaginsert == TRUE ) {
  303.         fprintf(stderr, "Error: Don't use --remove and --insert together.\n") ;
  304.         return -1 ;
  305.     }
  306.  
  307.     /* check if at least an input file was given
  308.      * in the command line */
  309.     if ( argv[optind] == NULL ) {
  310.         fprintf(stderr, "Error: No file to parser.\n") ;
  311.         return -1;
  312.     }
  313.  
  314.     /* no option ? call --dump */
  315.     if ( flagnooption == TRUE ) {
  316.         flaghasoutput = FALSE ;
  317.         flagdump = TRUE ;
  318.     }
  319.  
  320.     /* clean up function */
  321.     atexit(freetroydavis) ;
  322.  
  323.     while ( argv[optind++] != NULL ) {
  324.         inputfilename = argv[optind-1] ;
  325.  
  326.         if ( flagverbose & DOUBLE_VERBOSE ) {
  327.             fprintf(stdout, "Opening: %s\n", inputfilename) ;
  328.         }
  329.  
  330.         /* open input file */
  331.         inputfile = my_fopen(inputfilename, "rb") ;
  332.  
  333.         /* get the first 10 bytes. We're looking
  334.          * for a header here */
  335.         memset(header, 0x0, sizeof header) ;
  336.         fread(header, 1, 10, inputfile) ;
  337.  
  338.         /* Is there a ID3v2.3.x header? */
  339.         if ( !strncmp(header, "ID3", 3) ) {
  340.             headersize = ss2uli(header+6) ;
  341.  
  342.             if ( header[3] == 0x3 ) {
  343.                 /* --dump */
  344.                 if ( flagdump == TRUE ) {
  345.                     fprintf(stdout, "FILE : %s\n", inputfilename) ;
  346.                     dump_tags(headersize, inputfile) ;
  347.                 } else if ( flaginsert == TRUE ) { /* --insert */
  348.                     /* called insert without a tag option */
  349.                     if ( framehead == NULL ) {
  350.                         fprintf(stderr,"Error: No tag to insert.\n") ;
  351.                         fclose(inputfile) ;
  352.                         return -1 ;
  353.                     }
  354.                     /* put the tags of the input file in memory */
  355.                     get_frames_from_file(headersize, inputfile) ;
  356.                 }
  357.             } else if ( flagdump == TRUE || flaginsert == TRUE ) {
  358.                 fprintf(stderr, "Error: File is not ID3v2.3.x format.\n") ;
  359.                 fclose(inputfile) ;
  360.                 return -1 ;
  361.             }
  362.  
  363.             /* skip the whole tags and put the
  364.              * file pointer to the initial audio data */
  365.             fseek(inputfile, 10 + headersize, SEEK_SET) ;
  366.         }
  367.         /* Do we have to write to a file? */
  368.         if ( flaghasoutput == TRUE ) {
  369.             /* mkstemp create a temporary file in the
  370.              * current user directory */
  371.             if ( (outputfilefd = mkstemp(outputfilename)) < 0 ) {
  372.                 fprintf(stderr,"Error: Cant create temporary file.\n") ;
  373.                 return -1 ;
  374.             }
  375.  
  376.             /* convert file descriptor to stream */
  377.             outputfile = fdopen(outputfilefd, "w") ;
  378.  
  379.             /* points to the first frame */
  380.             f = framehead ;
  381.  
  382.             /* write the ID3v2 header */
  383.             header_dump(f, outputfile) ;
  384.  
  385.             /* write each frame */
  386.             while ( f != NULL ) {
  387.                 if ( flagverbose & DOUBLE_VERBOSE ) {
  388.                     fprintf(stderr, "storing: %c%c%c%c\n", f->id[0], f->id[1], f->id[2], f->id[3]) ;
  389.                 }
  390.                 frame_dump(f, outputfile) ;
  391.                 f = f->next ;
  392.             }
  393.  
  394.             /* write the mp3 data */
  395.             while ( fread(&byte, 1, 1, inputfile) > 0 ) fwrite(&byte, 1, 1, outputfile) ;
  396.  
  397.             /* replace the input file with the temporary one */
  398.             if ( rename(outputfilename, inputfilename) < 0 ) {
  399.                 /* We dont have 'w' in the input file... */
  400.                 fprintf(stderr, "Error: Cant overwrite the inputfile.\n") ;
  401.                 unlink(outputfilename) ;
  402.                 return -1 ;
  403.             }
  404.  
  405.             if ( flagverbose & TRUE ) {
  406.                 fprintf(stdout, "File `%s' was altered successfully.\n", inputfilename) ;
  407.             }
  408.  
  409.             strcpy(outputfilename, FILENAME_TEMPLATE) ;
  410.             fclose(outputfile) ;
  411.         }
  412.  
  413.         /* free tags of input file */
  414.         free_tags_of_input_file() ;
  415.  
  416.         fclose(inputfile) ;
  417.     }
  418.  
  419.     return 0;
  420. }
  421.  
  422. /*
  423.  * Functions body
  424.  */
  425. void *my_malloc(unsigned long int size) {
  426.     void *buf ;
  427.  
  428.     if ( (buf=malloc(size)) == NULL ) {
  429.         perror("malloc") ;
  430.         exit(-1) ;
  431.     }
  432.  
  433.     return buf ;
  434. }
  435.  
  436. FILE *my_fopen(char *filename, char *mode) {
  437.     FILE *f ;
  438.  
  439.     if ( (f = fopen(filename, mode)) == NULL ) {
  440.         perror(filename) ;
  441.         exit(-1) ;
  442.     }
  443.  
  444.     return f ;
  445. }
  446.  
  447. void freetroydavis() {
  448.     struct ID3v2frame *f, *p ;
  449.    
  450.     f = framehead ;
  451.     while ( f != NULL ) {
  452.         free(f->body) ;
  453.         p = f->next ;
  454.         free(f) ;
  455.         f = p ;
  456.     }
  457.     framehead = NULL ;
  458. }
  459.  
  460. void free_tags_of_input_file() {
  461.     struct ID3v2frame *f, *p=NULL ;
  462.  
  463.     if ( framehead == NULL ) {
  464.         return ;
  465.     }
  466.  
  467.     f = framehead ;
  468.     /* p is the last element with non-INSERT_TAG type */
  469.     while ( f != NULL && f->type != INSERT_TAG ) {
  470.         p = f ;
  471.         f = f->next ;
  472.     }
  473.  
  474.     /* no INSERT_TAG type was found */
  475.     if ( f == NULL ) return ;
  476.  
  477.     if ( p == NULL ) {
  478.         framehead = NULL ;
  479.     } else {
  480.         p->next = NULL ;
  481.     }
  482.  
  483.     while ( f != NULL ) {
  484.         p = f->next ;
  485.         if ( f->type == INSERT_TAG ) {
  486.             free(f->body) ;
  487.             free(f) ;
  488.         }
  489.         f = p ;
  490.     }
  491. }
  492.  
  493. unsigned long int synchsafe(unsigned long int in) {
  494.     unsigned long int out, mask = 0x7F;
  495.  
  496.     while (mask ^ 0x7FFFFFFF) {
  497.         out = in & ~mask;
  498.         out <<= 1;
  499.         out |= in & mask;
  500.         mask = ((mask + 1) << 8) - 1;
  501.         in = out;
  502.     }
  503.  
  504.     return out;
  505. }
  506.  
  507. unsigned long int ss2uli(char *buf) {
  508.     unsigned long int r ;
  509.  
  510.     r =     (buf[0] & 0x7f) << 21   ;
  511.     r = r | ((buf[1] & 0x7f) << 14) ;
  512.     r = r | ((buf[2] & 0x7f) << 7)  ;
  513.     r = r | (buf[3] & 0x7f)         ;
  514.     return r ;
  515. }
  516.  
  517. void set_size(char *buf, unsigned long int n) {
  518.     sprintf(buf, "%c%c%c%c",
  519.             (int)(n & 0xFF000000) >> 24,
  520.             (int)(n & 0xFF000) >> 16,
  521.             (int)(n & 0xFF00) >> 8,
  522.             (int)(n & 0xFF)) ;
  523. }
  524.  
  525. unsigned long int get_size(char *buf) {
  526.     unsigned long int n ;
  527.  
  528.     n  = (buf[0] & 0xFF) << 24 ;
  529.     n |= (buf[1] & 0xFF) << 16 ;
  530.     n |= (buf[2] & 0xFF) << 8  ;
  531.     n |= buf[3] & 0xFF;
  532.  
  533.     return n ;
  534. }
  535.  
  536. char *get_MIMEtype(char *file) {
  537.     char *ch ;
  538.  
  539.     if( (ch=strrchr(file, '.')) == NULL ) {
  540.         fprintf(stderr, "Error: Cant get file extension.\n") ;
  541.         exit(-1) ;
  542.     }
  543.  
  544.     /* points to the file extension */
  545.     ch++ ;
  546.     if ( !strcmp(ch, "jpg") ) return "image/jpg" ;
  547.     else if ( !strcmp(ch, "jpeg") ) return "image/jpeg" ;
  548.     else if ( !strcmp(ch, "png") ) return "image/png" ;
  549.  
  550.     fprintf(stderr, "Error: Unsupported MIME type.\n");
  551.     exit(-1) ;
  552. }
  553.  
  554. FILE *open_image_apic(char *file, unsigned long int *imgsize) {
  555.     FILE *fileapic ;
  556.     struct stat statapic ;
  557.  
  558.     fileapic = my_fopen(file, "rb") ;
  559.  
  560.     if ( stat(file, &statapic) < 0 ) {
  561.         perror("stat") ;
  562.         exit(-1) ;
  563.     }
  564.  
  565.     /* size in bytes of the picture file */
  566.     *imgsize = statapic.st_size ;
  567.  
  568.     return fileapic ;
  569. }
  570.  
  571. void insertframe(char *body, int type, int index) {
  572.     struct ID3v2frame *new, *next ;
  573.     char *newbody ;
  574.     unsigned long int framesize ;
  575.     unsigned long int bodysize ;
  576.  
  577.     /* create a new frame */
  578.     new = (struct ID3v2frame *)my_malloc(sizeof(struct ID3v2frame)) ;
  579.  
  580.     /* is it the first frame? */
  581.     if ( framehead == NULL ) {
  582.         framehead = new;
  583.     } else {
  584.         /* go to the last frame */
  585.         next = framehead ;
  586.         while ( next->next != NULL ) {
  587.             next = next->next ;
  588.         }
  589.         next->next = new ;
  590.     }
  591.  
  592.     /* copy the id */
  593.     strcpy(new->id, TAGS[index]) ;
  594.     /* no flags */
  595.     new->flags[0]=new->flags[1]=0;
  596.     /* last element */
  597.     new->next = NULL ;
  598.     /* type (TEXT_TAG or APIC_TAG) */
  599.     new->type = type ;
  600.  
  601.     if ( type == TEXT_TAG ) {
  602.         bodysize = 1 + strlen(body) ;
  603.         newbody = (char *)my_malloc(bodysize) ;
  604.  
  605.         /* text encoding: 00 */
  606.         sprintf(newbody, "%c%s%n", 0, body, (int *)&framesize) ;
  607.     } else if ( type == APIC_TAG ) {
  608.         char *MIMEtype ;
  609.         unsigned long int apicsize ;
  610.         FILE *apic ;
  611.  
  612.         /* get the MIMe type */
  613.         MIMEtype = get_MIMEtype(body) ;
  614.  
  615.         /* Get a stream for the picture */
  616.         apic = open_image_apic(body, &apicsize) ;
  617.  
  618.         /* size of the apic tag body */
  619.         bodysize = 1 + strlen(MIMEtype) + 1 + 1 + 1 + apicsize ;
  620.  
  621.         /* get memory for apic body */
  622.         newbody = (char *)my_malloc(bodysize) ;
  623.  
  624.         /* fulfill apic body */
  625.         sprintf(newbody, "%c%s%c%c%c%n", 0, MIMEtype, 0, 3, 0, (int *)&framesize) ;
  626.  
  627.         /* put image into memory */
  628.         while ( fread(newbody+framesize, 1, 1, apic) > 0 ) framesize++ ;
  629.  
  630.         fclose(apic) ;
  631.     }
  632.    
  633.     /* body of the frame */
  634.     new->body = newbody ;
  635.     /* size of the frame (char) */
  636.     set_size(new->charsize, framesize) ;
  637.     /* size of the frame (u long int) */
  638.     new->size = framesize ;
  639. }
  640.  
  641. void frame_dump(struct ID3v2frame *frame, FILE *stream) {
  642.     fwrite(frame, 1, 10, stream) ;
  643.     fwrite(frame->body, 1, frame->size, stream) ;
  644. }
  645.  
  646. void header_dump(struct ID3v2frame *framehead, FILE *stream) {
  647.     struct ID3v2frame *f ;
  648.     unsigned long int headersize = 0;
  649.     char charheadersize[4] ;
  650.  
  651.     /* should exist at least one tag to
  652.      * put the header */
  653.     if ( framehead != NULL ) {
  654.         /* get total bytes of the frames */
  655.         f = framehead ;
  656.         while ( f != NULL ) {
  657.             headersize += f->size + 10;
  658.             f = f->next ;
  659.         }
  660.  
  661.         /* synch safe it */
  662.         headersize = synchsafe(headersize) ;
  663.         set_size(charheadersize, headersize) ;
  664.         /* write the header in stream */
  665.         fprintf(stream, "%s%c%c%c%c%c%c%c", "ID3", 3, 0, 0,
  666.                 charheadersize[0],
  667.                 charheadersize[1],
  668.                 charheadersize[2],
  669.                 charheadersize[3]) ;
  670.     }
  671. }
  672.  
  673. void show_help() {
  674.     fprintf(stderr,
  675.             "\t\tcid3v2 - by Daemonio\n\
  676.            Small program that handles useful\n\
  677.            tags of ID3v2 format.\n\n\
  678.            Use: cid3v2 [OPTION]... [FILE]...\n\n\
  679.            -h, --help           , show this help\n\
  680.            -t, --title  TITLE   , song title\n\
  681.            -a, --artist ARTIST  , artist/band\n\
  682.            -l, --album  ALBUM   , album\n\
  683.            -g, --genre  GENRE   , genre\n\
  684.            -r, --track  NUMBER  , track number\n\
  685.            -p, --apic   PICTURE , cover art\n\
  686.            --insert             , insert tags\n\
  687.            --remove             , remove all ID3v2 tags\n\
  688.            --dump               , dumps text tags on screen\n\
  689.            --verbose            , show debug info\n\
  690.            ps: PICTURE = full path of the image\n") ;
  691.     exit(-1);
  692. }
  693.  
  694. char tag_already_in(char *id) {
  695.     struct ID3v2frame *f = framehead ;
  696.  
  697.     while ( f != NULL ) {
  698.         if ( strncmp(f->id, id, 4) == 0 ) {
  699.             return TRUE ;
  700.         }
  701.         f = f->next ;
  702.     }
  703.  
  704.     return FALSE ;
  705. }
  706.  
  707. void get_frames_from_file(unsigned long int headersize, FILE *inputfile) {
  708.     unsigned long int count = 0, framesize ;
  709.     struct ID3v2frame *last, *new ;
  710.     /* header of each frame */
  711.     char buffer[10] ;
  712.     /* body of the frame */
  713.     char *body ;
  714.  
  715.     if ( framehead != NULL ) {
  716.         last = framehead ;
  717.         while ( last->next != NULL ) last = last->next ;
  718.     }
  719.  
  720.     while ( headersize > count ) {
  721.         /* read the frame header */
  722.         fread(buffer, 1, 10, inputfile) ;
  723.  
  724.         /* we reach the padding */
  725.         if ( buffer[0] == 0 ) break ;
  726.  
  727.         if ( flagverbose & DOUBLE_VERBOSE ) {
  728.             fprintf(stdout, "Reading %c%c%c%c, size: %x%x%x%x",
  729.                           buffer[0],
  730.                           buffer[1],
  731.                           buffer[2],
  732.                           buffer[3],
  733.                           buffer[4] & 0xff,
  734.                           buffer[5] & 0xff,
  735.                           buffer[6] & 0xff,
  736.                           buffer[7] & 0xff) ;
  737.         }
  738.  
  739.         /* frame size */
  740.         framesize = get_size(buffer + 4) ;
  741.  
  742.         /* skip if the frame already exists */
  743.         if ( tag_already_in(buffer) == FALSE ) {
  744.             if ( flagverbose & DOUBLE_VERBOSE ) fprintf(stdout, ", doesn't exist in file.\n") ;
  745.  
  746.             new = (struct ID3v2frame *)my_malloc(sizeof(struct ID3v2frame)) ;
  747.             memcpy(new, buffer, 10) ;
  748.  
  749.             /* get memory for the body */
  750.             body = (char *)my_malloc(framesize) ;
  751.             fread(body, 1, framesize, inputfile) ;
  752.             new->body = body ;
  753.  
  754.             new->type = INSERT_TAG ;
  755.             new->size = framesize ;
  756.             new->next = NULL ;
  757.  
  758.             if ( framehead == NULL ) { /* first element */
  759.                 framehead = new ;
  760.             } else {
  761.                 last->next = new ;
  762.             }
  763.  
  764.             last = new ;
  765.         } else {
  766.             if ( flagverbose & DOUBLE_VERBOSE ) fprintf(stdout, ", already exists in file.\n") ;
  767.             fseek(inputfile, framesize, SEEK_CUR) ;
  768.         }
  769.  
  770.         count += 10 + framesize ;
  771.     }
  772. }
  773.  
  774. void dump_tags(unsigned long int headersize, FILE *inputfile) {
  775.     struct ID3v2frame *f ;
  776.  
  777.     get_frames_from_file(headersize, inputfile) ;
  778.  
  779.     f = framehead ;
  780.     while ( f != NULL ) {
  781.         /* show tag ID */
  782.         fprintf(stdout,"%c%c%c%c : ",
  783.                 f->id[0],
  784.                 f->id[1],
  785.                 f->id[2],
  786.                 f->id[3]) ;
  787.  
  788.         /* if it is a text tag, show its body */
  789.         if ( f->id[0] == 'T' ) {
  790.             fwrite(f->body+1, 1, f->size-1, stdout) ;
  791.         } else {
  792.             /* for APIC tag we can show the MIME type
  793.              * and picture type (FRONT COVER, BACK CONVER, etc)*/
  794.             if ( ! strncmp(f->id, "APIC", 4) ) {
  795.                 fprintf(stdout, "MIME type %s, pic type %x,",
  796.                         f->body + 1, f->body[0] & 0x000000FF) ;
  797.             }
  798.  
  799.             fprintf(stdout, " size: %lu", get_size(f->charsize)) ;
  800.         }
  801.  
  802.         putchar('\n') ;
  803.         f = f->next ;
  804.     }
  805. }
  806. /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment