// Decode pIntput into pOutput. Input length in lSize. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. DWORD CBlowFish::Decode(BYTE * pInput, BYTE * pOutput, DWORD lSize) { DWORD lCount, *dwOutput = (DWORD *)pOutput; _int64 CBCval = 0, prevCBCval = 0; if (pInput != pOutput) memcpy(pOutput, pInput, lSize); for (lCount = 0; lCount < lSize; lCount += 8) { if (mode == CBC) { prevCBCval = CBCval; CBCval = *(_int64 *)dwOutput; } change_order(dwOutput); change_order(dwOutput + 1); Blowfish_decipher(dwOutput, dwOutput + 1); change_order(dwOutput); change_order(dwOutput + 1); if (mode == CBC) *(_int64 *)dwOutput ^= prevCBCval; dwOutput += 2; } if (mode == CBC) { DWORD paddingLen = pOutput[lCount - 1]; if (paddingLen <= 8 && pOutput[lCount - paddingLen] == paddingLen) pOutput[lCount -= paddingLen] = 0; } return lCount; }
// Encrypt pIntput into pOutput. Input length in lSize. Returned value // is length of output which will be even MOD 8 bytes. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. DWORD CBlowFish::Encrypt(const BYTE *pInput, BYTE *pOutput, DWORD lSize, int mode, _int64 IV) { DWORD lCount, lOutSize, *dwOutput = (DWORD *)pOutput; if (pInput != pOutput) memcpy(pOutput, pInput, lSize); lOutSize = GetOutputLength(lSize, mode); memset(pOutput + lSize, (mode & BF_PKCS5) ? lOutSize - lSize : 0, lOutSize - lSize); for (lCount = 0; lCount < lOutSize; lCount += BF_BLKSIZE) { if (mode & BF_CBC) *(_int64 *)dwOutput ^= IV; change_order(dwOutput); change_order(dwOutput + 1); Blowfish_encipher(dwOutput, dwOutput + 1); change_order(dwOutput); change_order(dwOutput + 1); if (mode & BF_CBC) IV = *(_int64 *)dwOutput; dwOutput += 2; } return lCount; }
// Encode pIntput into pOutput. Input length in lSize. Returned value // is length of output which will be even MOD 8 bytes. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. DWORD CBlowFish::Encode (BYTE * pInput, BYTE * pOutput, DWORD lSize) { DWORD lCount, lOutSize, *dwOutput = (DWORD *)pOutput; _int64 CBCval = 0; if (pInput != pOutput) memcpy(pOutput, pInput, lSize); lOutSize = GetOutputLength(lSize); memset(pOutput + lSize, mode == CBC ? lOutSize - lSize : 0, lOutSize - lSize); for (lCount = 0; lCount < lOutSize; lCount += 8) { if (mode == CBC) *(_int64 *)dwOutput ^= CBCval; change_order(dwOutput); change_order(dwOutput + 1); Blowfish_encipher(dwOutput, dwOutput + 1); change_order(dwOutput); change_order(dwOutput + 1); if (mode == CBC) CBCval = *(_int64 *)dwOutput; dwOutput += 2; } return lOutSize; }
// Decrypt pIntput into pOutput. Input length in lSize. Input buffer and // output buffer can be the same, but be sure buffer length is even MOD 8. DWORD CBlowFish::Decrypt(const BYTE *pInput, BYTE *pOutput, DWORD lSize, int mode, BYTE *_IV) { DWORD lCount, *dwOutput = (DWORD *)pOutput; _int64 prevIV = 0; _int64 IV = 0; if (_IV) memcpy(&IV, _IV, sizeof(IV)); if (pInput != pOutput) memcpy(pOutput, pInput, lSize); for (lCount = 0; lCount < lSize; lCount += BF_BLKSIZE) { if (mode & BF_CBC) { prevIV = IV; IV = *(_int64 *)dwOutput; } change_order(dwOutput); change_order(dwOutput + 1); Blowfish_decipher(dwOutput, dwOutput + 1); change_order(dwOutput); change_order(dwOutput + 1); if (mode & BF_CBC) *(_int64 *)dwOutput ^= prevIV; dwOutput += 2; } if (mode & BF_PKCS5) { DWORD paddingLen = pOutput[lCount - 1]; if (paddingLen <= 0 || paddingLen > BF_BLKSIZE || pOutput[lCount - paddingLen] != paddingLen) return 0; // error pOutput[lCount -= paddingLen] = 0; } return lCount; }
/* * Process keyboard input during the main loop */ void do_key(char c) { int numinput, i; char rcfile[MAXNAMELEN]; FILE *fp; /* * First the commands which don't require a terminal mode switch. */ if (c == 'q') sig_end(0); else if (c == ' ') return; else if (c == 12) { clear_screen(); return; } else if (c == 'I') { Irixmode=(Irixmode) ? 0 : 1; return; } /* * Switch the terminal to normal mode. (Will the original * attributes always be normal? Does it matter? I suppose the * shell will be set up the way the user wants it.) */ if (!Batch) tcsetattr(0, TCSANOW, &Savetty); /* * Handle the rest of the commands. */ switch (c) { case '?': case 'h': PUTP(cl); PUTP(ho); putchar('\n'); PUTP(mr); printf("Proc-Top Revision 1.2"); PUTP(me); putchar('\n'); printf("Secure mode "); PUTP(md); fputs(Secure ? "on" : "off", stdout); PUTP(me); fputs("; cumulative mode ", stdout); PUTP(md); fputs(Cumulative ? "on" : "off", stdout); PUTP(me); fputs("; noidle mode ", stdout); PUTP(md); fputs(Noidle ? "on" : "off", stdout); PUTP(me); fputs("\n\n", stdout); printf("%s\n\nPress any key to continue", Secure ? SECURE_HELP_SCREEN : HELP_SCREEN); if (!Batch) tcsetattr(0, TCSANOW, &Rawtty); (void) getchar(); break; case 'i': Noidle = !Noidle; SHOWMESSAGE(("No-idle mode %s", Noidle ? "on" : "off")); break; case 'u': SHOWMESSAGE(("Which User (Blank for All): ")); strcpy(CurrUser,getstr()); break; case 'k': if (Secure) SHOWMESSAGE(("\aCan't kill in secure mode")); else { int pid, signal; PUTP(md); SHOWMESSAGE(("PID to kill: ")); pid = getint(); if (pid == BAD_INPUT) break; PUTP(top_clrtoeol); SHOWMESSAGE(("Kill PID %d with signal [15]: ", pid)); PUTP(me); signal = getsig(); if (signal == -1) signal = SIGTERM; if (kill(pid, signal)) SHOWMESSAGE(("\aKill of PID %d with %d failed: %s", pid, signal, strerror(errno))); } break; case 'l': SHOWMESSAGE(("Display load average %s", !show_loadav ? "on" : "off")); if (show_loadav) { show_loadav = 0; header_lines--; } else { show_loadav = 1; header_lines++; } Numfields = make_header(); break; case 'm': SHOWMESSAGE(("Display memory information %s", !show_memory ? "on" : "off")); if (show_memory) { show_memory = 0; header_lines -= 2; } else { show_memory = 1; header_lines += 2; } Numfields = make_header(); break; case 'M': SHOWMESSAGE(("Sort by memory usage")); sort_type = S_MEM; reset_sort_options(); register_sort_function(-1, (cmp_t)mem_sort); break; case 'n': case '#': printf("Processes to display (0 for unlimited): "); numinput = getint(); if (numinput != BAD_INPUT) { Display_procs = numinput; window_size(0); } break; case 'r': if (Secure) SHOWMESSAGE(("\aCan't renice in secure mode")); else { int pid, val; printf("PID to renice: "); pid = getint(); if (pid == BAD_INPUT) break; PUTP(tgoto(cm, 0, header_lines - 2)); PUTP(top_clrtoeol); printf("Renice PID %d to value: ", pid); val = getint(); if (val == BAD_INPUT) val = 10; if (setpriority(PRIO_PROCESS, pid, val)) SHOWMESSAGE(("\aRenice of PID %d to %d failed: %s", pid, val, strerror(errno))); } break; case 'P': SHOWMESSAGE(("Sort by CPU usage")); sort_type = S_PCPU; reset_sort_options(); register_sort_function(-1, (cmp_t)pcpu_sort); break; case 'A': SHOWMESSAGE(("Sort by age")); sort_type = S_AGE; reset_sort_options(); register_sort_function(-1, (cmp_t)age_sort); break; case 'N': SHOWMESSAGE(("Sort numerically by pid")); sort_type = S_NONE; reset_sort_options(); break; case 'c': show_cmd = !show_cmd; SHOWMESSAGE(("Show %s", show_cmd ? "command names" : "command line")); break; case 'S': Cumulative = !Cumulative; SHOWMESSAGE(("Cumulative mode %s", Cumulative ? "on" : "off")); if (Cumulative) headers[22][1] = 'C'; else headers[22][1] = ' '; Numfields = make_header(); break; case 's': if (Secure) SHOWMESSAGE(("\aCan't change delay in secure mode")); else { double tmp; printf("Delay between updates: "); tmp = getfloat(); if (!(tmp < 0)) Sleeptime = tmp; } break; case 't': SHOWMESSAGE(("Display summary information %s", !show_stats ? "on" : "off")); if (show_stats) { show_stats = 0; header_lines -= 2; } else { show_stats = 1; header_lines += 2; } Numfields = make_header(); break; case 'T': SHOWMESSAGE(("Sort by %stime", Cumulative ? "cumulative " : "")); sort_type = S_TIME; reset_sort_options(); register_sort_function( -1, (cmp_t)time_sort); break; case 'f': case 'F': change_fields(); break; case 'o': case 'O': change_order(); break; case 'W': if (getenv("HOME")) { strcpy(rcfile, getenv("HOME")); strcat(rcfile, "/"); strcat(rcfile, RCFILE); fp = fopen(rcfile, "w"); if (fp != NULL) { fprintf(fp, "%s\n", Fields); i = (int) Sleeptime; if (i < 2) i = 2; if (i > 9) i = 9; fprintf(fp, "%d", i); if (Secure) fprintf(fp, "%c", 's'); if (Cumulative) fprintf(fp, "%c", 'S'); if (!show_cmd) fprintf(fp, "%c", 'c'); if (Noidle) fprintf(fp, "%c", 'i'); if (!show_memory) fprintf(fp, "%c", 'm'); if (!show_loadav) fprintf(fp, "%c", 'l'); if (!show_stats) fprintf(fp, "%c", 't'); if (!Irixmode) fprintf(fp, "%c", 'I'); fprintf(fp, "\n"); fclose(fp); SHOWMESSAGE(("Wrote configuration to %s", rcfile)); } else { SHOWMESSAGE(("Couldn't open %s", rcfile)); } } else { SHOWMESSAGE(("Couldn't get $HOME -- not saving")); } break; default: SHOWMESSAGE(("\aUnknown command `%c' -- hit `h' for help", c)); } /* * Return to raw mode. */ if (!Batch) tcsetattr(0, TCSANOW, &Rawtty); return; }