Article · October 008 citations reads 5,930 authors
Download 77.34 Kb. Pdf ko'rish
|
The book cipher algorithm (1) (1)
Listing Two
// bkcode #include #include #include #include #define N_CAPITAL_LETTERS 26 #define LTRTOIDX(x) (x-'A') int main(int argc, char *argv[]) { char plain[_MAX_PATH], codfile[_MAX_PATH]; char cipher[_MAX_PATH], positions[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; FILE *fp_plain, *fp_cod, *fp_cipher, *fp_pos; long pos[N_CAPITAL_LETTERS]; int ch_plain, ch_cod; // Argument processing if (argc != 3) goto error1; _splitpath(argv[1], drive, dir, fname, ext ); if (strlen(ext) == 0) strcpy(ext, "cod"); _makepath(codfile, drive, dir, fname, ext); _makepath(positions, drive, dir, fname, "pos"); _splitpath(argv[2], drive, dir, fname, ext ); if (strlen(ext) == 0) strcpy(ext, "txt"); _makepath(plain, drive, dir, fname, ext); _makepath(cipher, drive, dir, fname, "cry"); // File opening if ((fp_plain = fopen(plain, "r")) == NULL) goto error3; if ((fp_cod = fopen(codfile, "r")) == NULL) goto error3; if ((fp_cipher = fopen(cipher, "w")) == NULL) goto error3; fp_pos = fopen(positions, "rb+"); // Position array intialization if (fp_pos == NULL) memset(pos, 0, sizeof(pos)); else { fread(pos, sizeof(long), N_CAPITAL_LETTERS, fp_pos); fclose(fp_pos); } // Main loop do { ch_plain = toupper(getc(fp_plain)); if (isalpha(ch_plain)) { fseek(fp_cod, pos[LTRTOIDX(ch_plain)], SEEK_SET); do { ch_cod = getc(fp_cod); } while (!(ch_cod == ch_plain || ch_cod == EOF)); if (ch_cod == ch_plain) { pos[LTRTOIDX(ch_plain)] = ftell(fp_cod); fprintf (fp_cipher, "%1ld ", pos[LTRTOIDX(ch_plain)]); } else goto error2; } } while (ch_plain != EOF); // Termination fp_pos = fopen(positions, "wb"); fwrite (pos, sizeof(long), N_CAPITAL_LETTERS, fp_pos); fclose(fp_plain); fclose(fp_cod); fclose(fp_cipher); fclose(fp_pos); return 0; // Error handling error1: printf ("USAGE: bkcode codfile message\n"); return 1; error2: printf ("Run out of letters %c!\n", ch_plain); fclose(fp_plain); fclose(fp_cod); fclose(fp_cipher); remove(cipher); return 1; error3: printf ("Can not open files\n"); return 1; } D10deja_p5db 8/15/08 10:05 AM Page 49 |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling