Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. typedef struct Header
  7. {
  8.     unsigned char tag[3];
  9.     unsigned char v1;
  10.     unsigned char v2;
  11.     unsigned char flags;
  12.     unsigned char size[4];
  13. } mp3Header;
  14.  
  15. unsigned char *formatFrameData(int size, unsigned char *data)
  16. {
  17.     unsigned char *result;
  18.     result = (unsigned char*)malloc(sizeof(unsigned char) * ((size-3)/2 + 1));
  19.     int pos = 0;
  20.     //pass 3 bytes and every NUL symbol
  21.     for (int i = 3; i < size; i+=2)
  22.     {
  23.         *(result + pos) = *(data + i);
  24.         pos++;
  25.     }
  26.     *(result + pos) = '\0';
  27.     return result;
  28. }
  29.  
  30. void printID3v2FrameInfo(unsigned char fid[5], int size, unsigned char *data)
  31. {
  32.         printf("\n[%s]:\t%s\n", fid, formatFrameData(size, data));
  33. }
  34.  
  35. typedef struct FrameHeader
  36. {
  37.     unsigned char tag[4];
  38.     unsigned char size[4];
  39.     unsigned char flags[2];
  40. } id3v2Frame;
  41.  
  42. unsigned char frames[77][5] = {
  43.     "AENC",
  44.     "APIC",
  45.     "COMM",
  46.     "COMR",
  47.     "ENCR",
  48.     "EQUA",
  49.     "ETCO",
  50.     "GEOB",
  51.     "GRID",
  52.     "IPLS",
  53.     "LINK",
  54.     "MCDI",
  55.     "MLLT",
  56.     "OWNE",
  57.     "PRIV",
  58.     "PCNT",
  59.     "POPM",
  60.     "POSS",
  61.     "RBUF",
  62.     "RVAD",
  63.     "RVRB",
  64.     "SYLT",
  65.     "SYTC",
  66.     "TALB",
  67.     "TBPM",
  68.     "TCMP",
  69.     "TCOM",
  70.     "TCON",
  71.     "TCOP",
  72.     "TDAT",
  73.     "TDLY",
  74.     "TENC",
  75.     "TEXT",
  76.     "TFLT",
  77.     "TIME",
  78.     "TIT1",
  79.     "TIT2",
  80.     "TIT3",
  81.     "TKEY",
  82.     "TLAN",
  83.     "TLEN",
  84.     "TMED",
  85.     "TOAL",
  86.     "TOFN",
  87.     "TOLY",
  88.     "TOPE",
  89.     "TORY",
  90.     "TOWN",
  91.     "TPE1",
  92.     "TPE2",
  93.     "TPE3",
  94.     "TPE4",
  95.     "TPOS",
  96.     "TPUB",
  97.     "TRCK",
  98.     "TRDA",
  99.     "TRSN",
  100.     "TRSO",
  101.     "TSIZ",
  102.     "TSO2",
  103.     "TSOC",
  104.     "TSRC",
  105.     "TSSE",
  106.     "TYER",
  107.     "TXXX",
  108.     "UFID",
  109.     "USER",
  110.     "USLT",
  111.     "WCOM",
  112.     "WCOP",
  113.     "WOAF",
  114.     "WOAR",
  115.     "WOAS",
  116.     "WORS",
  117.     "WPAY",
  118.     "WPUB",
  119.     "WXXX"
  120. };
  121.  
  122.  
  123. int getHeaderSize(unsigned char size[4])
  124. {
  125.     int c = 0;
  126.     int res = 0;
  127.    
  128.     for (int i = 3; i >= 0; i--)
  129.     {
  130.         for (int j = 0; j < 7; j++)
  131.         {
  132.             res += (size[i] % 2) * pow(2, c);
  133.             c++;
  134.             size[i] /= 2;
  135.         }
  136.     }
  137.    
  138.     return res;
  139. }
  140.  
  141. int getFrameSize(unsigned char size[4])
  142. {
  143.     int res = 0;
  144.     int c = 0;
  145.    
  146.     for (int i = 3; i >= 0; i--)
  147.     {
  148.         while (size[i])
  149.         {
  150.             res += (size[i] % 2) * pow(2, c);
  151.             c++;
  152.             size[i] /= 2;
  153.         }
  154.     }
  155.     return res;
  156. }
  157.  
  158. int checkFrame(char *frame)
  159. {
  160.    
  161.     int found = 0;
  162.    
  163.     for (int i = 0; i < 77; i++)
  164.     {
  165.         if (strcmp(frame, frames[i]) == 0)
  166.         {
  167.             found = 1;
  168.             break;
  169.         }
  170.     }
  171.    
  172.     return found;
  173. }
  174.  
  175. void getFrames(FILE *f, unsigned char *frame)
  176. {
  177.     id3v2Frame newFrame;
  178.     unsigned char *frameData;
  179.     int success = 0;
  180.     fread(&newFrame, 1, 10, f);
  181.     int frameSize = getFrameSize(newFrame.size);
  182.     while (checkFrame(newFrame.tag))
  183.     {
  184.         frameData = (unsigned char*)malloc(sizeof(unsigned char) * frameSize);
  185.         fread(frameData, frameSize, 1, f);
  186.         if (*frame == '\0' || strcmp(frame, newFrame.tag) == 0)
  187.         {
  188.             printID3v2FrameInfo(newFrame.tag, frameSize, frameData);
  189.             success = 1;
  190.         }
  191.         fread(&newFrame, 1, 10, f);
  192.         frameSize = getFrameSize(newFrame.size);
  193.     }
  194.     if (success == 0)
  195.         printf("[ERROR] There's no such frame (%s).", frame);
  196. }
  197.  
  198. unsigned char *formHeaderSize(int size)
  199. {
  200.     unsigned char *cur;
  201.     cur = (unsigned char*)malloc(sizeof(unsigned char) * 4);
  202.     for (int i = 0; i < 4; i++)
  203.         cur[i] = 0;
  204.     unsigned char sz[32];
  205.     int c = 0;
  206.     for (int i = 0; i < 32; i++)
  207.         sz[i] = '0';
  208.     while (size)
  209.     {
  210.         sz[31 - c] = '0' + size % 2;
  211.         c++;
  212.         size /= 2;
  213.     }
  214.     for (int k = 3; k >= 0; k--)
  215.     {
  216.         c = 0;
  217.         for (int i = (k == 3 ? (k + 1) * 8 - 1 : (k + 1) * 8); i > k * 8; i--)
  218.         {
  219.             cur[k] += (sz[i] - '0') * pow(2, c);
  220.             c++;
  221.         }
  222.     }
  223.     return cur;
  224. }
  225.  
  226. unsigned char *updFrameData(unsigned char *in, int size)
  227. {
  228.     unsigned char *out;
  229.     out = (unsigned char*)malloc(sizeof(unsigned char) * (size * 2 + 3));
  230.     *(out + 0) = 1;
  231.     *(out + 1) = 255;
  232.     *(out + 2) = 254;
  233.     int pos = 3;
  234.     for (int i = 0; i < size; i++)
  235.     {
  236.         *(out + pos) = *(in + i);
  237.         pos++;
  238.         *(out + pos) = 0;
  239.         pos++;
  240.     }
  241.    
  242.     return out;
  243. }
  244.  
  245. unsigned char *formFrameSize(int size)
  246. {
  247.     unsigned char *out;
  248.     out = (unsigned char*)malloc(sizeof(unsigned char)*4);
  249.    
  250.     for (int i = 0; i < 4; i++)
  251.         *(out + i) = 0;
  252.     int c = 0;
  253.     int k = 3;
  254.     while(size)
  255.     {
  256.         *(out + k) += (size % 2) * pow(2, c);
  257.         c++;
  258.         size /= 2;
  259.         if (c == 8)
  260.         {
  261.             c = 0;
  262.             k--;
  263.         }
  264.     }
  265.     return out;
  266. }
  267.  
  268. void rewriteID3v2(FILE *f, unsigned char *frame, unsigned char *value, unsigned char *filename, long headersize, long filesize)
  269. {
  270.     unsigned char **framesIDs;
  271.     int *framesSizes;
  272.     unsigned char **framesData;
  273.    
  274.     int sizeofNewHeader = 0;
  275.    
  276.     id3v2Frame newFrame;
  277.     int c = 0;
  278.     int ex = 0;
  279.     fread(&newFrame, 1, 10, f);
  280.     int frameSize = getFrameSize(newFrame.size);
  281.    
  282.     framesData = (unsigned char**)malloc(sizeof(unsigned char*) * 77);
  283.     framesIDs = (unsigned char**)malloc(sizeof(unsigned char*) * 77);
  284.     framesSizes = (int*)malloc(sizeof(int) * 77);
  285.    
  286.     while (checkFrame(newFrame.tag))
  287.     {
  288.         sizeofNewHeader += 10;
  289.         if (strcmp(frame, newFrame.tag) == 0)
  290.         {
  291.             *(framesIDs + c) = (unsigned char*)malloc(sizeof(unsigned char) * 5);
  292.             strcpy(*(framesIDs + c), newFrame.tag);
  293.             *(framesSizes + c) = strlen(value) * 2 + 3;
  294.             sizeofNewHeader += strlen(value) * 2 + 3;
  295.             *(framesData + c) = (unsigned char*)malloc(sizeof(unsigned char) * (strlen(value)*2 + 3));
  296.             unsigned char *formattedValue = updFrameData(value, strlen(value) * 2 + 3);
  297.             for(int i = 0; i < strlen(value) * 2 + 3; i++)
  298.                 *(*(framesData + c) + i) = *(formattedValue + i);
  299.             fseek(f, frameSize, SEEK_CUR);
  300.             ex = 1;
  301.         }
  302.         else
  303.         {
  304.             *(framesIDs + c) = (unsigned char*)malloc(sizeof(unsigned char) * 5);
  305.             strcpy(*(framesIDs + c), newFrame.tag);
  306.             *(framesSizes + c) = frameSize;
  307.             sizeofNewHeader += frameSize;
  308.             *(framesData + c) = (unsigned char*)malloc(sizeof(unsigned char) * frameSize);
  309.             fread(*(framesData + c), frameSize, 1, f);
  310.         }
  311.        
  312.         fread(&newFrame, 1, 10, f);
  313.         //printf("%s\n", newFrame.tag);
  314.         frameSize = getFrameSize(newFrame.size);
  315.         c++;
  316.     }
  317.     if (ex == 0)
  318.     {
  319.         *(framesIDs + c) = (unsigned char*)malloc(sizeof(unsigned char) * 5);
  320.         strcpy(*(framesIDs + c), frame);
  321.         *(framesSizes + c) = strlen(value) * 2 + 3;
  322.         sizeofNewHeader += strlen(value) * 2 + 3 + 10;
  323.         *(framesData + c) = (unsigned char*)malloc(sizeof(unsigned char) * (strlen(value)*2 + 3));
  324.         unsigned char *formattedValue = updFrameData(value, strlen(value) * 2 + 3);
  325.         for(int i = 0; i < strlen(value) * 2 + 3; i++)
  326.             *(*(framesData + c) + i) = *(formattedValue + i);
  327.     }
  328.    
  329.     unsigned char *new_file;
  330.    
  331.     new_file = (unsigned char*)malloc(sizeof(unsigned char) * (strlen(filename) + 5));
  332.     char *fn = strtok(filename, ".");
  333.     sprintf (new_file, "%s_new.mp3", fn);
  334.    
  335.     FILE *o = fopen(new_file, "wb");
  336.    
  337.     fwrite("ID3", 1, 3, o);
  338.     unsigned char t[1] = {3};
  339.     fwrite(t, 1, 1, o);
  340.     t[0] = 0;
  341.     fwrite(t, 1, 1, o);
  342.     fwrite(t, 1, 1, o);
  343.     fwrite(formHeaderSize(sizeofNewHeader), 1, 4, o);
  344.    
  345.     for (int i = 0; i <= (ex == 0 ? c : c - 1) ; i++)
  346.     {
  347.         fwrite(*(framesIDs + i), 1, 4, o);
  348.         fwrite(formFrameSize(*(framesSizes + i)), 1, 4, o);
  349.         fwrite(t, 1, 1, o);
  350.         fwrite(t, 1, 1, o);
  351.         fwrite(*(framesData + i), 1, *(framesSizes + i), o);
  352.     }
  353.    
  354.     char *mp3;
  355.     mp3 = (unsigned char*)malloc(sizeof(unsigned char) * filesize);
  356.    
  357.     fseek(f, headersize, SEEK_SET);
  358.    
  359.     fread(mp3, 1, filesize, f);
  360.    
  361.     fwrite(mp3, 1, filesize, o);
  362.    
  363.     fclose(o);
  364. }
  365.  
  366. int main(int argc, unsigned char **argv)
  367. {
  368.     unsigned char *val;
  369.     unsigned char *filename;
  370.     unsigned char *getFrame;
  371.     unsigned char *setFrame;
  372.     unsigned char *setFrameVal;
  373.     int show = 0;
  374.     int get = 0;
  375.     int set = 0;
  376.     for (int i = 1; i < argc; i++)
  377.     {
  378.         val = strtok(*(argv + i), "=");
  379.         if (strcmp(val, "--filepath") == 0)
  380.         {
  381.             val = strtok(NULL, "=");
  382.             filename = val;
  383.         }
  384.         else if (strcmp(val, "--get") == 0)
  385.         {
  386.             val = strtok(NULL, "=");
  387.             getFrame = val;
  388.             get = 1;
  389.         }
  390.         else if (strcmp(val, "--set") == 0)
  391.         {
  392.             val = strtok(NULL, "=");
  393.             setFrame = val;
  394.             set = 1;
  395.         }
  396.         else if (strcmp(val, "--value") == 0)
  397.         {
  398.             val = strtok(NULL, "=");
  399.             setFrameVal = val;
  400.         }
  401.         else if (strcmp(val, "--show") == 0)
  402.         {
  403.             show = 1;
  404.         }
  405.     }
  406.    
  407.     FILE *f = fopen(filename, "rw");
  408.    
  409.     mp3Header header;
  410.    
  411.     fread(&header, 1, 10, f);
  412.    
  413.     if (show == 1)
  414.     {
  415.         getFrames(f, "");
  416.         fclose(f);
  417.         return 0;
  418.     }
  419.    
  420.     if (get == 1)
  421.     {
  422.         getFrames(f, getFrame);
  423.         fclose(f);
  424.         return 0;
  425.     }
  426.    
  427.     if (set == 1)
  428.     {
  429.         fseek(f, 0, SEEK_END);
  430.         long f_size = ftell(f);
  431.         fseek(f, 0, SEEK_SET);
  432.         fread(&header, 1, 10, f);
  433.         int hz = getHeaderSize(header.size);
  434.         rewriteID3v2(f, setFrame, setFrameVal, filename, hz + 10, f_size - hz - 10);
  435.         fclose(f);
  436.         return 0;
  437.     }
  438.    
  439.     return (0);
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement