/* rinia4.c - Don Yang (uguu.org) 01/10/04 */ #include #include #include int t_table[256], t_embed_offset[256]; int o_freq[256], o_patch_offset[9]; int p_partial[9]; int q_counter[9]; int s_filesize; int c_input, c_crc, c_mod_char, c_partial; int i; int j_offset_count, j_run_length; int k_last_char, k_max_freq, k_embed_offset; int x_patch_char, x_ripple, x_bit; int l_last_block; FILE *f_file; unsigned char *d_data; unsigned char b_rchar[9][128], b_buffer[9]; unsigned char *w; int Checksum(int crc, int start, int end) { for(; start < end; crc = ((crc >> 8) & 0xffffff) ^ t_table[(crc ^ d_data[start++]) & 255]); return crc; } int main(int argc, char **argv) { if( argc < 4 ) return printf("%s \n", *argv); d_data = 0; if( f_file = fopen(argv[1], "rb") ) { fseek(f_file, 0, SEEK_END); if( (s_filesize = ftell(f_file)) > 8 ) { if( d_data = (unsigned char*)malloc(s_filesize) ) { fseek(f_file, 0, SEEK_SET); fread(d_data, s_filesize, 1, f_file); } } fclose(f_file); } if( !d_data ) return printf("bad input %s\n", argv[1]); i = j_offset_count = 0; if( f_file = fopen(argv[2], "rb") ) { for(; i < 256; t_embed_offset[i++] = -1) o_freq[i] = 0; k_last_char = -1; for(i = j_run_length = 0; i < s_filesize && (c_input = fgetc(f_file)) > -1; i++) { o_freq[c_input]++; if( c_input == k_last_char ) { if( ++j_run_length > 7 ) { if( t_embed_offset[k_last_char] == -1 ) t_embed_offset[k_last_char] = i - 7; } } else { k_last_char = c_input; j_run_length = 1; } } x_patch_char = -1; k_max_freq = s_filesize + 1; for(i = 33; i < 127; i++) { if( t_embed_offset[i] + 1 && k_max_freq > o_freq[i] ) { k_max_freq = o_freq[i]; x_patch_char = i; } } fseek(f_file, i = j_offset_count = 0, SEEK_SET); if( x_patch_char + 1 ) { for(k_embed_offset = t_embed_offset[x_patch_char]; i < s_filesize && j_offset_count < 8 && (c_input = fgetc(f_file)) > -1; i++) { if( i < k_embed_offset || i > k_embed_offset + 7 ) { if( c_input == x_patch_char ) { if( !(*b_rchar[j_offset_count] = d_data[i]) ) *b_rchar[j_offset_count] = 46; o_patch_offset[j_offset_count++] = i; } } } } fclose(f_file); } if( j_offset_count < 1 ) return printf("bad layout %s\n", argv[2]); for(i = 0; i < 256; i++) { c_crc = i; for(x_bit = 8; x_bit > 0; x_bit--) { c_crc = ( c_crc & 1 ) ? ((unsigned)c_crc >> 1) ^ 0xedb88320 : (unsigned)c_crc >> 1; } t_table[i] = c_crc; } l_last_block = o_patch_offset[j_offset_count - 1]; o_patch_offset[j_offset_count] = ( l_last_block < k_embed_offset ) ? (l_last_block = k_embed_offset + 8) : ++l_last_block; c_partial = time(0); sprintf((char*)b_buffer, "%08x", Checksum(c_partial, l_last_block, s_filesize) ^ ~0); for(i = 0; i < 8; i++) d_data[k_embed_offset + i] = *(b_buffer + i); for(i = 0; i < j_offset_count; q_counter[i++] = 0) { w = b_rchar[i] + 1; for(c_mod_char = 33; c_mod_char < 127; c_mod_char++) { if( c_mod_char - 47 && c_mod_char - 92 && c_mod_char - (int)*b_rchar[i] ) *(w++) = (unsigned char)c_mod_char; } *w = 0; } for(*p_partial = Checksum(~0, i = 0, *o_patch_offset); i > -1;) { for(x_patch_char = i; x_patch_char < j_offset_count; x_patch_char++) { d_data[o_patch_offset[x_patch_char]] = b_rchar[x_patch_char][q_counter[x_patch_char]]; p_partial[x_patch_char + 1] = Checksum(p_partial[x_patch_char], o_patch_offset[x_patch_char], o_patch_offset[x_patch_char + 1]); } if( p_partial[j_offset_count] == c_partial ) break; for(i = j_offset_count - 1; i > -1; i--) { if( b_rchar[i][++q_counter[i]] ) break; for(x_ripple = i; x_ripple < j_offset_count; q_counter[x_ripple++] = 0); } if( i < j_offset_count - 4 ) { putchar(46); fflush(stdout); } } if( i < 0 ) { puts("\nfail"); } else { if( f_file = fopen(argv[3], "wb+") ) { fwrite(d_data, s_filesize, 1, f_file); fclose(f_file); puts("\nsuccess"); } else { printf("Error writing %s\n", argv[3]); } } free(d_data); return 0; }