| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <memory.h> |
|---|
| 4 | |
|---|
| 5 | int nBlockSize = 4000; |
|---|
| 6 | |
|---|
| 7 | static unsigned int DHL_PsiAPI_CalculateCRC(const unsigned char *data, unsigned int len) |
|---|
| 8 | { |
|---|
| 9 | static unsigned int crcTable[256]; |
|---|
| 10 | static bool bFirst=true; |
|---|
| 11 | if (bFirst) |
|---|
| 12 | { |
|---|
| 13 | bFirst = false; |
|---|
| 14 | for (int i=0; i<256; i++) { |
|---|
| 15 | unsigned int crc = 0; |
|---|
| 16 | for (int j=7; j>=0; j--) { |
|---|
| 17 | if (((i >> j) ^ (crc >> 31)) & 1) crc=(crc<<1)^0x04C11DB7; |
|---|
| 18 | else crc<<=1; |
|---|
| 19 | } |
|---|
| 20 | crcTable[i] = crc; |
|---|
| 21 | } |
|---|
| 22 | } |
|---|
| 23 | unsigned int crc = 0xFFFFFFFF; |
|---|
| 24 | for (int i = 0; i < len; ++i) crc = (crc << 8) ^ crcTable[(crc >> 24) ^ (*data++)]; |
|---|
| 25 | return crc; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | // 184¹ÙÀÌÆ® ÀÔ·Â 188 ¹ÙÀÌÆ® ¾²±â |
|---|
| 29 | void write_packet(FILE *fp, unsigned char *buff, int size, bool bFirst, int pid) |
|---|
| 30 | { |
|---|
| 31 | //printf("write_packet size = %d\n", size); |
|---|
| 32 | unsigned char packet[188]; |
|---|
| 33 | memset(packet, 0xFF, 188); |
|---|
| 34 | packet[0] = 0x47; //Sync |
|---|
| 35 | packet[1] = (bFirst ? 0x40 : 0x00) + (pid / 256); // payload unit start indicator + 0x1FF5 |
|---|
| 36 | packet[2] = pid % 256; // pid 0x1FF5 |
|---|
| 37 | static unsigned char g_continuity_counter[0x1FFF]; |
|---|
| 38 | packet[3] = 0x10 + (g_continuity_counter[pid]%16); // Adaptation field control + continuity counter |
|---|
| 39 | g_continuity_counter[pid]++; |
|---|
| 40 | memcpy(&packet[4], buff, size); // µ¥ÀÌÅÍ º¹»ç |
|---|
| 41 | fwrite(packet, 1, 188, fp); // ÆÄÀÏ ÀúÀå |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void write_section(FILE *fp, unsigned char *buff, int size, int pid) |
|---|
| 45 | { |
|---|
| 46 | //printf("write_section size = %d\n", size); |
|---|
| 47 | for (int i = 0; i < size; i+=184) |
|---|
| 48 | { |
|---|
| 49 | write_packet(fp, &buff[i], (i+184 < size) ? 184 : size-i, i==0, pid); |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void write_table(FILE *fp, unsigned char *buff, int size, int pid, unsigned char table) |
|---|
| 54 | { |
|---|
| 55 | //printf("write_table size = %d\n", size); |
|---|
| 56 | unsigned char section[5000]; |
|---|
| 57 | memset(section, 0xFF, 5000); |
|---|
| 58 | int packet_length = size+4; |
|---|
| 59 | section[0] = 0x00; |
|---|
| 60 | section[1] = table; // table id |
|---|
| 61 | section[2] = 0x80 + (packet_length/256); // section syntax indicator |
|---|
| 62 | section[3] = packet_length % 256; // length |
|---|
| 63 | memcpy(§ion[4], buff, size); |
|---|
| 64 | // CRC »ðÀÔ |
|---|
| 65 | unsigned int crc = DHL_PsiAPI_CalculateCRC(§ion[1], size+3); |
|---|
| 66 | section[packet_length+0] = (crc >> 24) & 0xFF; |
|---|
| 67 | section[packet_length+1] = (crc >> 16) & 0xFF; |
|---|
| 68 | section[packet_length+2] = (crc >> 8) & 0xFF; |
|---|
| 69 | section[packet_length+3] = (crc) & 0xFF; |
|---|
| 70 | write_section(fp, section, packet_length + 4, pid); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | int get_block_count(int size) |
|---|
| 74 | { |
|---|
| 75 | return (size + nBlockSize-1) / nBlockSize; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void write_ddb(FILE *fp, unsigned char *buff, int size, int pid, int nBlockNumber) |
|---|
| 79 | { |
|---|
| 80 | if (nBlockNumber >= get_block_count(size)) nBlockNumber = nBlockNumber % get_block_count(size); |
|---|
| 81 | unsigned char *p = &buff[nBlockSize * nBlockNumber]; |
|---|
| 82 | int nBuffSize = nBlockSize; |
|---|
| 83 | if ((nBlockSize+1) * nBlockNumber > size) nBuffSize = size-(nBlockSize * nBlockNumber); |
|---|
| 84 | |
|---|
| 85 | unsigned char tmp[4096]; |
|---|
| 86 | memset(tmp, 0xFF, 4096); |
|---|
| 87 | tmp[7] = 0x10; |
|---|
| 88 | tmp[8] = 0x03; |
|---|
| 89 | tmp[14] = 0; |
|---|
| 90 | tmp[15] = (nBuffSize+6) /256; |
|---|
| 91 | tmp[16] = (nBuffSize+6) %256; |
|---|
| 92 | tmp[21] = (nBlockNumber) /256; |
|---|
| 93 | tmp[22] = (nBlockNumber) %256; |
|---|
| 94 | memcpy(&tmp[23], p, nBuffSize); |
|---|
| 95 | |
|---|
| 96 | write_table(fp, tmp, nBuffSize+23, pid, 0x3C); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | void write_dii(FILE *fp, int size, int pid) |
|---|
| 100 | { |
|---|
| 101 | unsigned char tmp[100]; |
|---|
| 102 | memset(tmp, 0x00, 100); |
|---|
| 103 | tmp[7] = 0x10; |
|---|
| 104 | tmp[8] = 0x02; |
|---|
| 105 | tmp[14] = 0; |
|---|
| 106 | tmp[21] = nBlockSize / 0x100; |
|---|
| 107 | tmp[22] = nBlockSize % 0x100; |
|---|
| 108 | tmp[39] = size / 0x1000000; |
|---|
| 109 | tmp[40] = (size % 0x1000000) / 0x10000; |
|---|
| 110 | tmp[41] = (size % 0x10000) / 0x100; |
|---|
| 111 | tmp[42] = size % 256; |
|---|
| 112 | write_table(fp, tmp, 100, pid, 0x3B); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void write_dummy(FILE *fp) |
|---|
| 116 | { |
|---|
| 117 | unsigned char packet[188]; |
|---|
| 118 | memset(packet, 0x00, 188); |
|---|
| 119 | packet[0] = 0x47; //Sync |
|---|
| 120 | packet[1] = 0x1F; |
|---|
| 121 | packet[2] = 0xFF; |
|---|
| 122 | packet[3] = 0x10; |
|---|
| 123 | fwrite(packet, 1, 188, fp); // ÆÄÀÏ ÀúÀå |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | void write_dummys(FILE *fp, int nCount) |
|---|
| 127 | { |
|---|
| 128 | for (int i= 0; i < nCount; i++) |
|---|
| 129 | { |
|---|
| 130 | write_dummy(fp); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | int main(int argc, char* argv[]) |
|---|
| 135 | { |
|---|
| 136 | // ÀÔ·Â ÀÎÀÚ °¹¼ö È®ÀÎ |
|---|
| 137 | printf("argc = %d\n", argc); |
|---|
| 138 | if (argc != 6) |
|---|
| 139 | { |
|---|
| 140 | printf("%s cvtfile cvtpid imagefile imagepid\n", argv[0]); |
|---|
| 141 | return -1; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | // cvt file È®ÀÎ |
|---|
| 145 | FILE *fp = fopen(argv[1],"rb"); |
|---|
| 146 | if (fp == 0) |
|---|
| 147 | { |
|---|
| 148 | printf("%s cvtfile cvtpid imagefile imagepid\n", argv[0]); |
|---|
| 149 | printf("Can not find cvt file %s\n", argv[1]); |
|---|
| 150 | return -2; |
|---|
| 151 | } |
|---|
| 152 | // ÀÔ·Â ÆÄÀÏÀÇ ±æÀÌ È®ÀÎ |
|---|
| 153 | fseek(fp, 0L, SEEK_END); |
|---|
| 154 | int nlen_cvt = ftell(fp); |
|---|
| 155 | if (nlen_cvt == 0) |
|---|
| 156 | { |
|---|
| 157 | printf("%s cvtfile cvtpid imagefile imagepid\n", argv[0]); |
|---|
| 158 | printf("Can not find file %s\n", argv[1]); |
|---|
| 159 | printf("File Size is Zero\n"); |
|---|
| 160 | return -3; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | // ÀÔ·Â ÆÄÀÏÀ» ÀÐ¾î µéÀÓ |
|---|
| 164 | fseek(fp, 0L, SEEK_SET); |
|---|
| 165 | unsigned char *cvt = (unsigned char*)malloc(nlen_cvt); |
|---|
| 166 | if (cvt == 0) |
|---|
| 167 | { |
|---|
| 168 | printf("%s cvtfile cvtpid imagefile imagepid\n", argv[0]); |
|---|
| 169 | printf("Can not find file %s\n", argv[1]); |
|---|
| 170 | printf("Out of Memory\n"); |
|---|
| 171 | fclose(fp); |
|---|
| 172 | return -4; |
|---|
| 173 | } |
|---|
| 174 | fread( cvt, 1, nlen_cvt, fp); |
|---|
| 175 | fclose(fp); |
|---|
| 176 | |
|---|
| 177 | // image file È®ÀÎ |
|---|
| 178 | fp = fopen(argv[3],"rb"); |
|---|
| 179 | if (fp == 0) |
|---|
| 180 | { |
|---|
| 181 | printf("%s cvtfile cvtpid imagefile imagepid\n", argv[0]); |
|---|
| 182 | printf("Can not find image file %s\n", argv[3]); |
|---|
| 183 | return -2; |
|---|
| 184 | } |
|---|
| 185 | // ÀÔ·Â ÆÄÀÏÀÇ ±æÀÌ È®ÀÎ |
|---|
| 186 | fseek(fp, 0L, SEEK_END); |
|---|
| 187 | int nlen_image = ftell(fp); |
|---|
| 188 | if (nlen_image == 0) |
|---|
| 189 | { |
|---|
| 190 | printf("%s cvtfile cvtpid imagefile imagepid\n", argv[0]); |
|---|
| 191 | printf("Can not find image file %s\n", argv[3]); |
|---|
| 192 | printf("File Size is Zero\n"); |
|---|
| 193 | return -3; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | // ÀÔ·Â ÆÄÀÏÀ» ÀÐ¾î µéÀÓ |
|---|
| 197 | fseek(fp, 0L, SEEK_SET); |
|---|
| 198 | unsigned char *image = (unsigned char*)malloc(nlen_image); |
|---|
| 199 | if (image == 0) |
|---|
| 200 | { |
|---|
| 201 | printf("%s cvtfile cvtpid imagefile imagepid\n", argv[0]); |
|---|
| 202 | printf("Can not find image file %s\n", argv[3]); |
|---|
| 203 | printf("Out of Memory\n"); |
|---|
| 204 | fclose(fp); |
|---|
| 205 | return -4; |
|---|
| 206 | } |
|---|
| 207 | fread( image, 1, nlen_image, fp); |
|---|
| 208 | fclose(fp); |
|---|
| 209 | |
|---|
| 210 | fp = fopen(argv[5], "wb"); |
|---|
| 211 | int nlenloop = get_block_count(nlen_image); |
|---|
| 212 | //printf("nlenloop = %d\n", nlenloop); |
|---|
| 213 | for (int i = 0; i < nlenloop; i++) |
|---|
| 214 | { |
|---|
| 215 | write_dii(fp, nlen_cvt, atoi(argv[2])); |
|---|
| 216 | write_dummys(fp, 10); |
|---|
| 217 | write_ddb(fp, cvt, nlen_cvt, atoi(argv[2]), i); |
|---|
| 218 | write_dummys(fp, 10); |
|---|
| 219 | write_dii(fp, nlen_image, atoi(argv[4])); |
|---|
| 220 | write_dummys(fp, 10); |
|---|
| 221 | write_ddb(fp, image, nlen_image, atoi(argv[4]), i); |
|---|
| 222 | write_dummys(fp, 10); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | fclose(fp); |
|---|
| 226 | return 0; |
|---|
| 227 | } |
|---|