diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68bd2e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin/ +*.out +*.mp3 diff --git a/AI_gen_Approach.c b/AI_gen_Approach.c new file mode 100644 index 0000000..3860259 --- /dev/null +++ b/AI_gen_Approach.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +// Simplified frame length calculation (MPEG1 Layer3 only) +uint32_t calc_frame_len(uint32_t header) { + const int bitrates[] = {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}; + const int samplerates[] = {44100, 48000, 32000}; + + int br_idx = (header >> 12) & 0xF; + int sr_idx = (header >> 10) & 0x3; + int padding = (header >> 9) & 0x1; + + int bitrate = bitrates[br_idx] * 1000; + int samplerate = samplerates[sr_idx]; + + return 144 * bitrate / samplerate + padding; +} + +int main(int argc, char** argv) { + if(argc != 2) return fprintf(stderr, "Usage: %s \n", argv[0]), 1; + + FILE* fin = fopen(argv[1], "rb"); + if(!fin) return perror("fopen"), 1; + + FILE* fout = NULL; + long file_count = 0; + uint32_t pos = 0; + uint32_t header; + uint8_t buf[4]; + + while(fread(buf, 1, 4, fin) == 4) { + header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; + + // Check for valid MP3 header: 0xFF 0xE? (sync + version) + if(buf[0] == 0xFF && (buf[1] & 0xE0) == 0xE0) { + uint32_t frame_len = calc_frame_len(header >> 8); + + if(frame_len > 0 && frame_len < (10*1024*1024)) { // Sanity check + if(!fout) { + char fname[128]; + snprintf(fname, sizeof(fname), "output_%ld.mp3", file_count++); + fout = fopen(fname, "wb"); + if(!fout) { perror("fopen output"); break; } + printf("Extracting %s\n", fname); + } + + // Save header + fwrite(buf, 1, 4, fout); + // Save frame data + for(uint32_t i = 4; i < frame_len; i++) { + int c = fgetc(fin); + if(c == EOF) break; + fputc(c, fout); + } + pos = ftell(fin); + continue; + } + } + + if(fout) { + fclose(fout); + fout = NULL; + } + + // Rewind 3 bytes to allow overlapping frames + fseek(fin, ++pos, SEEK_SET); + } + + if(fout) fclose(fout); + fclose(fin); + return 0; +} diff --git a/stripMP3.c b/stripMP3.c new file mode 100644 index 0000000..08382aa --- /dev/null +++ b/stripMP3.c @@ -0,0 +1,74 @@ +#include +#include + +unsigned long ltob(unsigned long n){ + unsigned long m = n ? ltob(n /2) : 0; + printf("%d", (int)(n % 2)); + return m; +} + +int main(int argc, char** argv){ + FILE *f_point; + unsigned char record; + unsigned int mp3header = 4294677700; + unsigned int mp3test = 0; + + f_point=fopen(argv[1],"rb"); + if(!f_point){ + printf("Unable to open file!"); + return 1; + } + + fseek(f_point,0,SEEK_END); + long size = ftell(f_point); + rewind(f_point); + + printf("Size of file %ldb\n", size); + unsigned char *b = malloc(size+1); + + if(1 !=fread(b,size, 1, f_point)){ + fclose(f_point); + free(b); + printf("Error reading file\n"); + return 1; + } + + long i = 0; + long last = 0; + int x = 0; + while(i < (size - 5)){ + if(b[0+i]==0xFF && b[1+i]==0xFB && b[3+i]==0xC4){ + last = i; + printf("\t\tMP3 Header (%ld) with bitrate %X\n",i, b[2+i]); + } + if(b[0+i]==0x4C && b[1+i]==0x41 && b[2+i]==0x4d && b[3+i]==0x45){ + //b[4+i]==0x33 && b[5+i]==0x2E && b[6+i]==0x39 && b[7+i]==0x38 + printf("LAME located at %ld distance (%ld)\n", i, i - last); + + if(i >= 141){ + if(b[i-141]==0xFF && b[i-140]==0xFB && b[i-138]==0xC4){ + printf("\tHigh Possiblity of the start of an mp3 is here %ld | %X%X%X%X\n", i-141, b[i-141],b[i-140],b[i-139],b[i-138]); + x=1; + } + } + + if(i >= 35){ + if(b[i-35]==0xFF && b[i-34]==0xFB && b[i-32]==0xC4){ + printf("\tHigh Possiblity of the start of an mp3 is here %ld | %X%X%X%X\n", i-35, b[i-35],b[i-34],b[i-33],b[i-32]); + x=1; + } + } + + if(!x){ + printf("\tMaybe the end of an mp3?\n"); + } + + x=0; + } + i++; + } + + free(b); + fclose(f_point); + return 0; +}