Advertisement
Guest User

lb13

a guest
Dec 12th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. typedef enum { false, true } bool;      /// created a new datatype for boolean
  6.  
  7. struct Frame {
  8.     char frameID[4];                    ///first c4 bytes contains the name of the frame
  9.     unsigned char sizeBytes[4];         /// size of meta info
  10.     char flagsBytes[2];
  11. };
  12.  
  13. struct MainHeader {
  14.     char ID3[3];
  15.     char versionBytes[2];
  16.     char flagByte;
  17.     char sizeBytes[4];
  18. };
  19.  
  20. int getFrameSize(char*);
  21. void showFrames(FILE*);  /// to display current tag values
  22. void showFrame(FILE*, char*); /// shows specific frame
  23. void setVal(FILE*, FILE*, char*, char*);
  24. void setFrameSize(char*, int);
  25. bool isEqual(char*, char*);   /// to check of the strings are same or not
  26.  
  27. int main(int argc, char* argv[]) {
  28.     char com[3][20];
  29.     char arg[3][50];
  30.     int k = 0;
  31.     int m = 0;
  32.     int j = 0;
  33.  
  34.     for (int i = 1; i < argc; i++) {
  35.         k = 0;
  36.         for (j = 0; argv[i][j] != '=' && argv[i][j] != '\0'; j++) {
  37.             com[m][k++] = argv[i][j];
  38.         }
  39.         com[m][k] = '\0';
  40.  
  41.         j++;
  42.         k = 0;
  43.         while (argv[i][j] != '\0') {
  44.             arg[m][k++] = argv[i][j++];
  45.         }
  46.         arg[m][k] = '\0';
  47.         m++;
  48.     }
  49.  
  50.     FILE* mp3;
  51.     mp3 = fopen(arg[0], "r+b");
  52.     FILE* output;
  53.     output = fopen("output.mp3", "r+b");
  54.     for (int i = 1; i < argc - 1; i++) {
  55.         if (strcmp(com[i], "--show") == 0) {
  56.             showFrames(mp3);
  57.     }
  58.         else if (strcmp(com[i], "--get") == 0) {
  59.             showFrame(mp3, arg[i]);
  60.         }
  61.         else if (strcmp(com[i], "--set") == 0) {
  62.             setVal(mp3, output, arg[i], arg[i + 1]);
  63.         }
  64.     }
  65.  
  66.     return 0;
  67. }
  68.  
  69. int getFrameSize(char* byteArr) {
  70.     int size = 0;
  71.     unsigned char ch;
  72.     for (int i = 0; i < 4; i++) {
  73.         ch = byteArr[i];    /// so that what is in byte array put it in character
  74.         size += ch * pow(2, 7 * (3 - i));
  75.     }
  76.     return size;
  77. }
  78.  
  79. void showFrames(FILE* f) {
  80.     fseek(f, 0, SEEK_SET);
  81.     struct MainHeader header;
  82.     fread(&header, sizeof(header), 1, f);
  83.     long long tagSize = getFrameSize(header.sizeBytes);
  84.     struct Frame frame;
  85.     long long counter = 0;
  86.     while (counter <= tagSize) {
  87.         fread(&frame, sizeof(frame), 1, f);
  88.         long long frameSize = getFrameSize(frame.sizeBytes);
  89.         counter += 10 + frameSize;
  90.         if (frame.frameID[0] == 'T') {
  91.             printf("%s ", frame.frameID);
  92.             while(frameSize--) {
  93.                 char ch = fgetc(f);
  94.                 if (ch >= 32 && ch <= 126)
  95.                     printf("%c", ch);
  96.             }
  97.             printf("\n");
  98.         }
  99.         else if (isEqual(frame.frameID, "COMM") == true) {  /// comment has 4 byetes so we are going to subtract them
  100.             printf("%s ", frame.frameID);
  101.             fseek(f, 4, SEEK_CUR);
  102.             frameSize -= 4;
  103.             while(frameSize--) {
  104.                 char ch = fgetc(f);
  105.                 if (ch >= 32 && ch <= 126)
  106.                     printf("%c", ch);
  107.             }
  108.             printf("\n");
  109.         }
  110.     }
  111. }
  112.  
  113. void showFrame(FILE* f, char* frameID) {        /// we need to read the id3 header
  114.     fseek(f, 0, SEEK_SET);
  115.     struct MainHeader header;
  116.     fread(&header, sizeof(header), 1, f);
  117.     int fileSize = getFrameSize(header.sizeBytes);
  118.     struct Frame frame;
  119.     int counter = 0;
  120.     while (counter <= fileSize) {
  121.         fread(&frame, sizeof(frame), 1, f);
  122.         int frameSize = getFrameSize(frame.sizeBytes);
  123.         counter += 10 + frameSize;    /// because the header is 10 bytes
  124.         if (isEqual(frame.frameID, frameID) == true) {
  125.             printf("%s ", frame.frameID);
  126.             while(frameSize--) {
  127.                 char ch = fgetc(f);
  128.                 if (ch >= 32 && ch <= 126)
  129.                     printf("%c", ch);
  130.             }
  131.         }
  132.         else {
  133.             fseek(f, getFrameSize(frame.sizeBytes), SEEK_CUR);
  134.         }
  135.     }
  136. }
  137.  
  138. bool isEqual(char* str1, char* str2) {
  139.     for (int i = 0; i < sizeof(str1); i++) {
  140.         if (str1[i] != str2[i]) {
  141.             return false;
  142.         }
  143.     }
  144.     return true;
  145. }
  146.  
  147. void setVal(FILE* fin, FILE* fout, char* someFrame, char* val) {
  148.     fseek(fin, 0, SEEK_SET);
  149.     struct MainHeader header;
  150.     fread(&header, sizeof(header), 1, fin);
  151.     fwrite(&header, sizeof(header), 1, fout);
  152.     int valSize = strlen(val);
  153.     int fileSize = getFrameSize(header.sizeBytes);              ///filesize ==value sizze// total id3 tag size
  154.     struct Frame frame;
  155.     int counter = 0;
  156.     int frameSize = 0;
  157.     while (counter <= fileSize) {
  158.         fread(&frame, sizeof(frame), 1, fin);
  159.         frameSize = getFrameSize(frame.sizeBytes);
  160.         counter += 10 + frameSize;
  161.         if (isEqual(frame.frameID, someFrame) == false && frameSize >= 0 && frameSize <= 100000) {
  162.             fwrite(&frame, 1, sizeof(frame), fout);
  163.             char info[frameSize];
  164.             fread(&info, frameSize, 1, fin);
  165.             fwrite(info, frameSize, 1, fout);
  166.         }
  167.         else if (isEqual(frame.frameID, someFrame) == true && frameSize >= 0){
  168.             int oldFrameSize = frameSize;
  169.             setFrameSize(header.sizeBytes, getFrameSize(header.sizeBytes) - frameSize + valSize);
  170.             int curPos = ftell(fout);
  171.             fseek(fout, 6, SEEK_SET);
  172.             fwrite(header.sizeBytes, sizeof(header.sizeBytes), 1, fout);
  173.             fseek(fout, curPos, SEEK_SET);
  174.             if (isEqual(frame.frameID, "COMM") == true)
  175.                 setFrameSize(frame.sizeBytes, valSize + 4);
  176.             else if (isEqual(frame.frameID, "TXXX") == true)
  177.                 setFrameSize(frame.sizeBytes, valSize + 2);
  178.             else
  179.                 setFrameSize(frame.sizeBytes, valSize);
  180.             int newFrameSize = getFrameSize(frame.sizeBytes);
  181.             fwrite(&frame, sizeof(frame), 1, fout);
  182.             if (isEqual(frame.frameID, "COMM") == true) {
  183.                 for (int i = 0; i < 4; i++) {
  184.                     fputc(fgetc(fin), fout);
  185.                 }
  186.                 fseek(fin, oldFrameSize - 4, SEEK_CUR);
  187.             }
  188.             else if (isEqual(frame.frameID, "TXXX") == true) {
  189.                 for (int i = 0; i < 2; i++) {
  190.                     fputc(fgetc(fin), fout);
  191.                 }
  192.                 fseek(fin, oldFrameSize - 2, SEEK_CUR);
  193.             }
  194.             else
  195.                 fseek(fin, oldFrameSize, SEEK_CUR);
  196.             fwrite(val, valSize, 1, fout);
  197.         }
  198.     }
  199.     int ch;
  200.     while ((ch = fgetc(fin)) != EOF) {
  201.         fputc(ch, fout);
  202.     }
  203.     fseek(fout, 0, SEEK_SET);
  204.     fseek(fin , 0, SEEK_SET);
  205.     while ((ch = getc(fout)) != EOF) {
  206.         fputc(ch, fin);
  207.     }
  208. }
  209.  
  210. void setFrameSize(char* frameSize, int valSize) {
  211.     for (int i = 0; i < 4; i++) {
  212.         frameSize[i] = valSize / pow(2, 7 * (3 - i));
  213.         valSize -= frameSize[i] * pow(2, 7 * (3 - i));
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement