| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <memory.h> |
|---|
| 4 | |
|---|
| 5 | int main(int argc, char* argv[]) |
|---|
| 6 | { |
|---|
| 7 | // ÀÔ·Â ÀÎÀÚ °¹¼ö È®ÀÎ |
|---|
| 8 | if (argc != 4) |
|---|
| 9 | { |
|---|
| 10 | printf("%s update_file ts_file output_file\n", argv[0]); |
|---|
| 11 | return -1; |
|---|
| 12 | } |
|---|
| 13 | // ÀÔ·Â ÆÄÀÏÀÌ Á¸ÀçÇÏ´Â Áö È®ÀÎ |
|---|
| 14 | FILE *fp = fopen(argv[1],"rb"); |
|---|
| 15 | if (fp == 0) |
|---|
| 16 | { |
|---|
| 17 | printf("Can not open file %s\n", argv[1]); |
|---|
| 18 | printf("%s update_file ts_file output_file\n", argv[0]); |
|---|
| 19 | return -2; |
|---|
| 20 | } |
|---|
| 21 | // ÀÔ·Â ÆÄÀÏÀÇ ±æÀÌ È®ÀÎ |
|---|
| 22 | fseek(fp, 0L, SEEK_END); |
|---|
| 23 | int nFileSize = ftell(fp); |
|---|
| 24 | if (nFileSize == 0) |
|---|
| 25 | { |
|---|
| 26 | printf("%s update_file ts_file output_file\n", argv[0]); |
|---|
| 27 | printf("File Size is Zero\n"); |
|---|
| 28 | return -5; |
|---|
| 29 | } |
|---|
| 30 | // ÀÔ·Â ÆÄÀÏÀ» ÀÐ¾î µéÀÓ µÎ¹ø ¹Ýº¹ÇÑ´Ù |
|---|
| 31 | fseek(fp, 0L, SEEK_SET); |
|---|
| 32 | unsigned char *data = (unsigned char*)malloc(nFileSize*2); |
|---|
| 33 | if (data == 0) |
|---|
| 34 | { |
|---|
| 35 | printf("%s update_file ts_file output_file\n", argv[0]); |
|---|
| 36 | printf("Out of Memory\n"); |
|---|
| 37 | fclose(fp); |
|---|
| 38 | return -6; |
|---|
| 39 | } |
|---|
| 40 | fread( data, 1, nFileSize, fp); |
|---|
| 41 | memcpy(&data[nFileSize], &data[0], nFileSize); // µÚ·Î º¹Á¦ |
|---|
| 42 | fclose(fp); |
|---|
| 43 | // ÀÔ·Â ÆÄÀÏÀÌ Á¸ÀçÇÏ´Â Áö È®ÀÎ |
|---|
| 44 | fp = fopen(argv[2],"rb"); |
|---|
| 45 | if (fp == 0) |
|---|
| 46 | { |
|---|
| 47 | printf("Can not open file %s\n", argv[2]); |
|---|
| 48 | printf("%s update_file ts_file output_file\n", argv[0]); |
|---|
| 49 | return -3; |
|---|
| 50 | } |
|---|
| 51 | remove(argv[3]); // Ãâ·Â ÆÄÀÏ »èÁ¦ |
|---|
| 52 | FILE *fp_output = fopen(argv[3],"wb"); |
|---|
| 53 | if (fp_output == 0) |
|---|
| 54 | { |
|---|
| 55 | printf("Can not open file %s\n", argv[3]); |
|---|
| 56 | printf("%s update_file ts_file output_file\n", argv[0]); |
|---|
| 57 | return -4; |
|---|
| 58 | } |
|---|
| 59 | for (int i = 0; i < nFileSize*2; i+=188) |
|---|
| 60 | { |
|---|
| 61 | char buff[188]; |
|---|
| 62 | if (fread( buff, 1, 188, fp) != 188) |
|---|
| 63 | { |
|---|
| 64 | printf("rewind\n"); |
|---|
| 65 | fseek(fp, 0L, SEEK_SET); |
|---|
| 66 | fread( buff, 1, 188, fp); |
|---|
| 67 | } |
|---|
| 68 | unsigned short pid = (buff[1]&0x1F)*256 + buff[2]; |
|---|
| 69 | if (pid < 256) |
|---|
| 70 | { |
|---|
| 71 | fwrite( buff, 1, 188, fp_output); |
|---|
| 72 | i-=188; |
|---|
| 73 | continue; |
|---|
| 74 | } |
|---|
| 75 | else |
|---|
| 76 | { |
|---|
| 77 | fwrite( &data[i], 1, 188, fp_output); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | fclose(fp); |
|---|
| 81 | fclose(fp_output); |
|---|
| 82 | return 0; |
|---|
| 83 | } |
|---|