/* Original author: mlb2gm5x @ misticriver forums */ /* Modified: Jon Lund Steffensen, 2008 */ #include #include #include #include #include #define USAGE \ "Usage: %s -e|-d [-h] [-r] INPUT OUTPUT\n" #define HELP \ USAGE \ " Encode or decode iriver files.\n" \ " -e\tEncode INPUT to OUTPUT\n" \ " -d\tDecode INPUT to OUTPUT\n" \ " -h\tDisplay this help message\n" \ " -r\tRaw data mode; Don't update header\n" \ " -s\tSkip checksum check when decoding (DANGEROUS!)\n" static const uint8_t table1[256] = { 0x67, 0xB2, 0x9F, 0x5A, 0x9E, 0xAD, 0x46, 0x4B, 0x8F, 0x23, 0x16, 0x7B, 0x4B, 0x5B, 0x5C, 0x44, 0xC1, 0x79, 0xCA, 0x35, 0x2D, 0xAA, 0xF4, 0x7D, 0x53, 0xED, 0x60, 0x03, 0x7D, 0x36, 0x98, 0xFB, 0x02, 0xBB, 0x5A, 0x6D, 0xD4, 0x09, 0x9C, 0x1D, 0xB2, 0x61, 0xA7, 0xC9, 0x62, 0x46, 0xB3, 0x4E, 0xEC, 0x26, 0x5C, 0xB0, 0xE7, 0x44, 0x27, 0xBD, 0x39, 0x8F, 0xD1, 0x0E, 0xD2, 0xDA, 0x6C, 0xDC, 0x76, 0x90, 0x33, 0x0E, 0xBD, 0x60, 0x5D, 0x34, 0x19, 0x56, 0x05, 0xD7, 0x5E, 0x14, 0xA0, 0x04, 0x1F, 0x11, 0x97, 0x42, 0x7F, 0xF1, 0x76, 0xA8, 0x60, 0xFD, 0xA0, 0xD0, 0x1B, 0x41, 0xCA, 0xF5, 0x30, 0x52, 0xD1, 0x9B, 0xD7, 0x14, 0xDB, 0xF5, 0xC2, 0xD0, 0xD9, 0x2C, 0x13, 0x98, 0x70, 0x3F, 0x16, 0x73, 0xE9, 0x08, 0x2C, 0xDD, 0xFA, 0x05, 0x96, 0xD6, 0x7E, 0x81, 0xA1, 0xD3, 0xF1, 0x09, 0x2C, 0x6D, 0x97, 0x0A, 0x0C, 0x6D, 0x6A, 0x99, 0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A, 0x25, 0x6A, 0x31, 0xCE, 0x48, 0x84, 0xA5, 0x5B, 0x11, 0xAC, 0x3B, 0xF9, 0x14, 0xE0, 0x8C, 0x4E, 0x8F, 0x95, 0xF5, 0xAF, 0xE3, 0x35, 0x34, 0x9D, 0x81, 0xAE, 0x50, 0xD1, 0x78, 0x10, 0x88, 0x86, 0xCF, 0x94, 0x1A, 0xDB, 0xAD, 0x18, 0xC8, 0xBF, 0x08, 0xA8, 0xFD, 0xBE, 0x96, 0xE6, 0x09, 0x75, 0x93, 0x9F, 0x01, 0xCB, 0xB3, 0x6D, 0xC5, 0x6E, 0xD4, 0xA3, 0x62, 0x41, 0x07, 0xA7, 0xAA, 0xEB, 0xE1, 0x79, 0x1E, 0xF0, 0xC2, 0x0D, 0x0A, 0x5E, 0xFC, 0xC6, 0xF4, 0xDA, 0xEC, 0x3E, 0x17, 0x15, 0x03, 0xD7, 0x70, 0x66, 0x47, 0x8E, 0xB1, 0xC9 }; static int decode_256(const uint8_t *src, uint8_t *dst, int skip_checksums) { unsigned int i, j, k; unsigned int tmp1, tmp2; uint8_t temp[256]; for (i = 0, tmp1 = 0; i < 255; i++) { tmp1 = 0xFFFF & (tmp1 + src[i]); } if ((tmp1 & 0xFF) != src[255]) { if (!skip_checksums) return 0; } for (i = 0; i < 255; i++) { temp[i] = table3[src[i]]; } for (i = 0, k = 0; i < 5; i++) { tmp2 = 50*i; tmp1 = 0; for (j = 0; j < 50; j++) { tmp1 = (temp[k] + tmp1) & 0xFFFF; dst[j+tmp2] = temp[k]; k++; } if (temp[k] != (tmp1 & 0xFF)) { if (!skip_checksums) return 0; } k++; } for (i = 0; i < 250; i++) { tmp1 = ((dst[i]) ^ (table2[i])) << (8-(table1[i] & 7)); dst[i] = tmp1 | (tmp1 >> 8); } return 1; } static int decode_file(const char *srcfile, const char *dstfile, int skip_checksums) { int r; int size, newsize, err = 0; int i, j; uint8_t *pMemSource, *pMemDest; FILE *pDest, *pSource; /* Open source file */ pSource = fopen(srcfile, "rb"); if (pSource == NULL) { perror("fopen"); return -1; } r = fseek(pSource, 0, SEEK_END); if (r < 0) { perror("fseek"); return -1; } size = ftell(pSource); if (size < 0) { perror("ftell"); return -1; } if ((size & 0x7f) != 0) { fprintf(stderr, "ERROR: size & 0x7f != 0\n"); return -1; } newsize = (125*size)>>7; r = fseek(pSource, 0, SEEK_SET); if (r < 0) { perror("fseek"); return -1; } pMemSource = (uint8_t *)malloc(size); if (pMemSource == NULL) { fprintf(stderr, "ERROR: malloc(%u) failed.\n", size); return -1; } /* Read source file to memory */ r = fread(pMemSource, sizeof(uint8_t), size, pSource); if (r < size) { if (ferror(pSource)) perror("fread"); else fprintf(stderr, "ERROR: EOF reached.\n"); return -1; } fclose(pSource); pMemDest = (uint8_t *)malloc(newsize); if (pMemDest == NULL) { fprintf(stderr, "ERROR: malloc(%u) failed.\n", newsize); return -1; } /* Decode */ for (i = 0, j = 0; i < size; i += 256, j += 250) { if (decode_256(pMemSource+i, pMemDest+j, skip_checksums) == 0) { err = 1; break; } } if (err) { fprintf(stderr, "ERROR: decode failed.\n"); return -1; } /* Open destination file */ pDest = fopen(dstfile, "wb"); if (pDest == NULL) { perror("fopen"); return -1; } r = fseek(pDest, 0, SEEK_SET); if (r < 0) { perror("fseek"); return -1; } /* Write to destination file */ r = fwrite(pMemDest, sizeof(uint8_t), newsize, pDest); if (r < newsize) { perror("fwrite"); return -1; } fclose(pDest); free(pMemDest); free(pMemSource); return 0; } static void encode_250(uint8_t *src, uint8_t *dst) { unsigned int i, j, k; unsigned int tmp1, tmp2; uint8_t temp[256]; for (i = 0; i < 250; i++) { int tmp2 = 8 - (table1[i] & 7); for (j = 0; j < 256; j++) { tmp1 = j << tmp2; if (src[i] == (0xFF & (tmp1 | (tmp1 >> 8)))) { break; } } src[i] = j ^ (table2[i]); } for (i = 0, k = 0; i < 5; i++) { tmp2 = 50*i; tmp1 = 0; for (j = 0; j < 50; j++) { temp[k] = src[j+tmp2]; tmp1 = (temp[k] + tmp1) & 0xFFFF; k++; } temp[k] = tmp1 & 0xFF; k++; } for (i = 0; i < 255; i++) { for (j = 0; j < 256; j++) { if (temp[i] == table3[j]) { dst[i] = j; break; } } } for (i = 0, tmp1 = 0; i < 255; i++) { tmp1 = 0xFFFF & (tmp1 + dst[i]); } dst[255] = tmp1 & 0xFF; } static void update_header(uint8_t *pHeader, unsigned int size, unsigned int checksum) { pHeader[0x10] = 0xFF&(size>>24); pHeader[0x20] = 0xFF&(size>>16); pHeader[0x30] = 0xFF&(size>>8); pHeader[0x40] = 0xFF&(size); pHeader[0x90] = 0xFF&(checksum>>24); pHeader[0xA0] = 0xFF&(checksum>>16); pHeader[0xB0] = 0xFF&(checksum>>8); pHeader[0xC0] = 0xFF&(checksum); } static int encode_file(const char *srcfile, const char *dstfile, int raw) { int r; unsigned int checksum, size, newsize; unsigned int i, j; uint8_t *pMemSource, *pMemDest; FILE *pDest, *pSource; /* Open source file */ pSource = fopen(srcfile, "rb"); if (pSource == NULL) { perror("fopen"); return -1; } r = fseek(pSource, 0, SEEK_END); if (r < 0) { perror("fseek"); return -1; } size = ftell(pSource); if (size < 0) { perror("ftell"); return -1; } r = fseek(pSource, 0, SEEK_SET); if (r < 0) { perror("fseek"); return -1; } newsize = size; if (size % 250 != 0) { newsize = size + 250 - size % 250; } pMemSource = (uint8_t *)calloc(newsize, 1); if (pMemSource == NULL) { fprintf(stderr, "ERROR: malloc(%u) failed.\n", newsize); return -1; } /* Read source to memory */ r = fread(pMemSource, sizeof(uint8_t), size, pSource); if (r < size) { if (ferror(pSource)) perror("fread"); else fprintf(stderr, "ERROR: EOF reached.\n"); return -1; } fclose(pSource); size = newsize; newsize = (size << 7)/125; pMemDest = (uint8_t *)malloc(newsize); if (pMemDest == NULL) { fprintf(stderr, "ERROR: malloc(%u) failed.\n", newsize); return -1; } /* Encode */ for (i = 500, j = 512; i < size; i += 250, j += 256) { encode_250(pMemSource+i, pMemDest+j); } if (!raw) { for (i = 512, checksum = 0; i < newsize; i++) { checksum += pMemDest[i]; } update_header(pMemSource, newsize, checksum); } encode_250(pMemSource, pMemDest); encode_250(pMemSource+250, pMemDest+256); /* Open destination file */ pDest = fopen(dstfile, "wb"); if (pDest == NULL) { perror("fopen"); return -1; } r = fseek(pDest, 0, SEEK_SET); if (r < 0) { perror("fseek"); return -1; } /* Write to destination file */ r = fwrite(pMemDest, sizeof(uint8_t), newsize, pDest); if (r < newsize) { perror("write"); return -1; } fclose(pDest); free(pMemDest); free(pMemSource); return 0; } int main(int argc, char *argv[]) { int r; int decode = -1; int raw = 0; int skip_checksums = 0; int opt; while ((opt = getopt(argc, argv, "edrhs")) != -1) { switch (opt) { case 'e': decode = 0; break; case 'd': decode = 1; break; case 'r': raw = 1; break; case 's': skip_checksums = 1; break; case 'h': printf(HELP, argv[0]); exit(EXIT_SUCCESS); break; default: fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); break; } } /* decode or encode mode must be chosen */ if (decode == -1) { fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); } char *in_file = NULL; char *out_file = NULL; /* input file */ if (optind < argc) in_file = argv[optind++]; else { fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); } /* output file */ if (optind < argc) out_file = argv[optind++]; else { fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); } if (decode) { r = decode_file(in_file, out_file, skip_checksums); if (r < 0) exit(EXIT_FAILURE); } else { r = encode_file(in_file, out_file, raw); if (r < 0) exit(EXIT_FAILURE); } return EXIT_SUCCESS; }