-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpass.c
625 lines (564 loc) · 22.7 KB
/
pass.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//
// File: pass.c
// pass.c plays the password guessing game
// @author Josh Bicking <josh1147582>
// // // // // // // // // // // // // // // // // // // // // // //
#define _BSD_SOURCE // for unistd.h
#ifdef _WIN32
# include <Windows.h>
# include <curses.h>
# define SLEEP(delay) Sleep(delay/1000)
#else
# include <ncurses.h>
# include <unistd.h>
# define SLEEP(delay) usleep(delay)
#endif
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include "pass.h"
#include "print.h"
#include "wordParse.h"
#include "intro.h"
#define OFFSET_LEFT 0
#define OFFSET_RIGHT 20
#define BIGSTRING_SIZE 408
static int currentCharContains(char arr[],char c){
int i;
for(i=0; i<12; i++)
if(arr[i]==c)
return 1;
return 0;
}
static int getCharLoc(int y, int x){
// Left side
if(x<19)
return 12*(y-5)+(x-7);
// Right side
else
return 12*(y-5)+(x-27+204);
}
void pass(){
// Note: Most of the strings in this function are NOT NUL terminated (they
// do not end with a \0, and will not work with many of the string.h
// functions). When I wrote the first revision of this program, I knew very
// little about C, and therefore managed the length of my strings manually.
// I've decided to keep it this way, as a majority of the work deals with
// fixed-length substrings of bigString. It's easier to pull characters out
// of that and campare them than it would be to try and make proper C
// strings out of every operation.
// Clear the screen
erase();
// Intro text
passPrint("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL",0);
passPrint("ENTER PASSWORD NOW", 1);
passPrint("4 ATTEMPT(S) LEFT: * * * *", 3);
// Generate the hex values on the left sides
int arbHex;
arbHex = (rand() % 200) + 63744;
// Generate the string to hold the bracket tricks and words
char bigString [BIGSTRING_SIZE];
char randValues[] = "!@#$%^*()_-=+\\|/[]{}?\"\':;,.<>";
int i;
for(i=0; i<BIGSTRING_SIZE; i++){
// Fill bigString with random values
bigString[i] = randValues[rand()%29];
}
char ** wordArr = getWordArr();
int WORD_POOL_SIZE = getNumWords();
int WORD_SIZE = getWordLength();
int WORDS_CHOSEN = getWordsToChoose();
// Place a word in the string total times, making sure it doesn
// Overwrite another word or get placed right next to it
int place; // Current place for checking and word insertion
int takenWords[WORDS_CHOSEN]; // Words already placed in bigString
for(int i=0; i<WORDS_CHOSEN; i++)
takenWords[i] = 0;
int valid; // 1 if selected word is not already used a
// does not conflict with other words, 0 otherwise */
int pickedWord = 0; // Indicate whether or not we've chosen the correct word
char correctWord[WORD_SIZE]; // the correct word
// Place all the words into bigString
for(int i=0; i<WORD_SIZE; i++) {
// Find a WORD_SIZE length spot in bigString that isn't already
// occupied by another word or part of another word.
do {
valid = 1;
// Choose a random place in bigString
place = rand()%(BIGSTRING_SIZE-WORD_SIZE);
// Check of any characters there or around it are A-Z
for(int j=place-1; j<place+WORD_SIZE+1; j++){
if(bigString[j] > 64 && bigString[j] < 91){
valid = 0;
break;
}
}
}while(!valid);
// Find a word that hasn't already been inserted
int wordLoc = 0;
do {
wordLoc = rand()%WORD_POOL_SIZE;
}while(takenWords[wordLoc]);
// Set it as taken
takenWords[wordLoc] = 1;
// Add the word to bigString
for(int j=place; j<place+WORD_SIZE; j++){
bigString[j] = *(*(wordArr+wordLoc)+(j-place));
// If this is the first word chosen, it is the correct word.
if(!pickedWord)
correctWord[j-place] = *(*(wordArr+wordLoc)+(j-place));
}
pickedWord = 1;
}
// Create and fill an array to keep track of which brackets were used
int usedBrackets[BIGSTRING_SIZE];
for(i=0; i<BIGSTRING_SIZE; i++){
usedBrackets[i] = 1;
}
// Print the hex and the filled bigString to the screen
char temp[12];
int current = 0;
for(i=5; i<22; i++){
// Print left side
for(int j=0; j<12; j++){
temp[j] = bigString[j+current];
}
printChoices(arbHex,temp,i, OFFSET_LEFT);
current = current + 12;
arbHex = arbHex + 12;
}
for(i=5; i<22; i++){
// Print right side
for(int j=0; j<12; j++){
temp[j] = bigString[j+current];
}
printChoices(arbHex,temp,i, OFFSET_RIGHT);
current = current + 12;
arbHex = arbHex + 12;
}
// Print the cursor and move the selection to the top left
mvprintw(21,40,"%c",'>');
move(5,7);
char currentChar[12]; // Max length currentChar could be (total possible length of a bracket trick)
currentChar[0] = (char)mvinch(5,7);
int y,x; // values that keep track of current yx locations
int origy, origx; // yx values from the previous cycle. Used for clearing highlights
int starty, startx; // yx values used for storing the start of a word
int wordLength; // How long a word is
int charStart; // where character counting starts for brackets
int keyPress; // key pressed by user
int charCounter; // counts currentChar - used for incrementing currentChar to print or change it
int bracketLength; // length of a bracket trick
char endBracket; // the end bracket that corresponds to currentChar[0];
int bracketTricks=0; // Total number of bracket tricks used
int needsClearing = 0; // Whether or not highlights need to be pur
int needsClearingMultiLine = 0; // Whether or not a multi line highlight needs to be purged
char output[13]; // Used for side terminal output
int allowances = 4; // Number of guesses remaining
// Get the key config
int GO_LEFT, GO_RIGHT, GO_DOWN, GO_UP;
switch(getKeyConfig()){
case ARROWS:
GO_LEFT = KEY_LEFT;
GO_RIGHT = KEY_RIGHT;
GO_UP = KEY_UP;
GO_DOWN = KEY_DOWN;
break;
case WASD:
GO_LEFT = 'a';
GO_RIGHT = 'd';
GO_UP = 'w';
GO_DOWN = 's';
break;
case HJKL:
GO_LEFT = 'h';
GO_RIGHT = 'l';
GO_UP = 'k';
GO_DOWN = 'j';
break;
}
// Get rid of all typed characters
int ch = getch();
while(ch != ERR)
ch = getch();
// Finally, set nodelay to false so we can wait for input
nodelay(stdscr, 0);
while(1){
getyx(stdscr,y,x);
// Get allowances left
mvprintw(1,0," ");
mvprintw(3,0," ");
switch(allowances){
case 1: mvprintw(3,0,"1 ATTEMPT(S) LEFT: *");
attron(A_BLINK);
mvprintw(1,0,"!!! WARNING: LOCKOUT IMNINENT !!!");
attroff(A_BLINK);
attron(A_BOLD);
break;
case 2: mvprintw(3,0,"2 ATTEMPT(S) LEFT: * *");
mvprintw(1,0,"ENTER PASSWORD NOW");
break;
case 3: mvprintw(3,0,"3 ATTEMPT(S) LEFT: * * *");
mvprintw(1,0,"ENTER PASSWORD NOW");
break;
case 4: mvprintw(3,0,"4 ATTEMPT(S) LEFT: * * * *");
mvprintw(1,0,"ENTER PASSWORD NOW");
break;
case 0: clear();
mvprintw(10,20,"TERMINAL LOCKED");
mvprintw(12,12,"PLEASE CONTACT AN ADMINISTRATOR");
refresh();
SLEEP(3000000);
endwin();
if(strlen(getCompleteProg())> 2)
system(getCompleteProg());
freeAll();
exit(EXIT_FAILURE);
}
refresh();
// Move the cursor back to where it was
move(y,x);
// Check if highlights need to be purged
if(needsClearing){
// Grab each character printed, and reprint it without a highlight
charCounter = 0;
while(charCounter!=bracketLength+1){
currentChar[charCounter] = (char)mvinch(origy,charStart+charCounter);
mvprintw(origy,charStart+charCounter,"%c",(int)currentChar[charCounter]);
charCounter++;
}
// Clear the > prompt, which previously contained the entire highlighted string
mvprintw(21,41," ",currentChar[0]);
needsClearing = 0;
move(y,origx);
}
if(needsClearingMultiLine){
charCounter = 0;
// Same as above, but jumps between lines if necessary
while(charCounter!=wordLength){
currentChar[charCounter] = (char)mvinch(starty,startx);
mvprintw(starty,startx,"%c",currentChar[charCounter]);
charCounter++;
startx++;
if(startx==19 || startx==39){
startx-=12;
starty++;
if(starty == 22) {
starty = 5;
startx+=20;
}
}
}
mvprintw(21,41," ",currentChar[0]);
needsClearingMultiLine = 0;
move(y,x);
}
// Clear the char array
for(i=0;i<12;i++)
currentChar[i]=' ';
currentChar[0] = (char) (char)mvinch(y,x);
// Set the new y and x to origy and origx
origy = y;
origx = x;
// Check for bracket tricks
if((currentChar[0]=='(' || currentChar[0]=='<' || currentChar[0]=='[' || currentChar[0]=='{') && usedBrackets[getCharLoc(y,x)] && bracketTricks<WORDS_CHOSEN){
charStart = x;
bracketLength=0;
// Check any chars to the right of the current char for a corresponding bracket
while(x!=18 && x!=38){
x++;
endBracket = (char)mvinch(y,x);
bracketLength++;
if((endBracket == ')' && currentChar[0]=='(') ||
(endBracket == '>' && currentChar[0]=='<') ||
(endBracket == ']' && currentChar[0]=='[') ||
(endBracket == '}' && currentChar[0]=='{')){
// Reprint the brackets, and anything in between them, with a highlight
attron(A_STANDOUT);
charCounter = 0;
while(1){
currentChar[charCounter] = (char)mvinch(y,charStart+charCounter);
mvprintw(y,charStart+charCounter,"%c",currentChar[charCounter]);
if(currentChar[charCounter] == endBracket)
break;
charCounter++;
}
attroff(A_STANDOUT);
// Print the bracket trick to output
attron(A_BOLD);
for(i=0;i<=charCounter;i++)
mvprintw(21,41+i,"%c",(int)currentChar[i]);
// Notify that highlighting will need to be cleared next move
needsClearing = 1;
}
}
// If this bracket isn't part of a pair, just print the bracket in the > prompt
if(!((endBracket == ')' && currentChar[0]=='(') ||
(endBracket == '>' && currentChar[0]=='<') ||
(endBracket == ']' && currentChar[0]=='[') ||
(endBracket == '}' && currentChar[0]=='{'))){
mvprintw(21,41,"%c",currentChar[0]);
}
}
// Check for letters
else if(currentChar[0]>64 && currentChar[0]<91){
// Check for letter behind the current location
int tempx = x;
int tempy = y;
while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
currentChar[0] = bigString[getCharLoc(tempy,tempx)];
tempx--;
if(tempx==6 || tempx==26){
tempx+=12;
tempy--;
if(tempy == 4){
tempy = 21;
tempx-=20;
}
}
}
startx = tempx;
starty = tempy; // We'll need the location of the first char for clean
// And start there
charCounter = 0;
while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
charCounter++;
tempx++;
if(tempx==19 || tempx==39){
tempx-=12;
tempy++;
if(tempy == 22) {
tempy = 5;
tempx+=20;
}
}
}
// Now currentChar is the String, and charCounter is the length
wordLength = charCounter;
// Reprint the word with highlight
tempx = startx;
tempy = starty;
attron(A_STANDOUT);
charCounter = 0;
while(charCounter!=wordLength){
currentChar[charCounter] = (char)mvinch(tempy,tempx);
mvprintw(tempy,tempx,"%c",currentChar[charCounter]);
charCounter++;
tempx++;
if(tempx==19 || tempx==39){
tempx-=12;
tempy++;
if(tempy == 22) {
tempy = 5;
tempx+=20;
}
}
}
attroff(A_STANDOUT);
// Print the word to output
attron(A_BOLD);
for(i=0;i<charCounter;i++)
mvprintw(21,41+i,"%c",(int)currentChar[i]);
// Notify that highlighting will need to be cleared next move
needsClearingMultiLine = 1;
}
// Nothing was found, print current char
else
mvprintw(21,41,"%c",currentChar[0]);
move(origy,origx);
refresh();
keyPress = getch();
getyx(stdscr,y,x);
if(keyPress==GO_UP){
if(y>5)
move(y-1,x);
}
if(keyPress==GO_DOWN){
if(y<21)
move(y+1,x);
}
if(keyPress==GO_LEFT){
if(x>7){
if(x==27)
move(y,18);
else
move(y,x-1);
}
}
if(keyPress==GO_RIGHT){
if(x<38){
if(x==18)
move(y,27);
else
move(y,x+1);
}
}
if(keyPress==3) // Ctrl-C
exit(0);
if(keyPress=='\n'){ // Enter
// Get past answers and shift them up along the right.
// This "log" handles 5 previous commands.
mvprintw(5,41," ");
mvprintw(6,41," ");
mvprintw(7,41," ");
char buf[15];
for(int i=8; i<19; i+=3) {
for(int j=0; j< 3; j++){
mvinnstr(i+j, 40, buf, 14);
mvprintw(i+j,40," ");
mvprintw(i+j-3, 40, "%s", buf);
}
}
// If the char is a left bracket
if(((currentChar[0]=='(') && currentCharContains(currentChar,')')) ||
(currentChar[0]=='<' && currentCharContains(currentChar,'>')) ||
(currentChar[0]=='[' && currentCharContains(currentChar,']')) ||
(currentChar[0]=='{' && currentCharContains(currentChar,'}'))){
// Set the selected bracket as used
usedBrackets[getCharLoc(y,x)] = 0;
// Increment total bracket tricks used
bracketTricks++;
if(rand()%5==0){
// 20% chance of allowance replenish
mvinnstr(21,40, buf, 14);
mvprintw(17,40, "%s", buf);
sprintf(output,"Allowance ");
mvprintw(18,40,">");
for(i=0;i<12;i++){
mvprintw(18,41+i,"%c",output[i]);
}
sprintf(output,"replenished.");
mvprintw(19,40,">");
for(i=0;i<12;i++){
mvprintw(19,41+i,"%c",output[i]);
}
allowances = 4;
}
else{
// 80% chance to remove a dud
int tempx,tempy; // Mark the beginning of the selected string, and read chars into currentChar
int allCorrect = 1; // Shows if all the chars in the string match the correct word
// Pick a random A-Z character in bigString
do{
do{
if(rand()%2==0)
tempx = (rand()%12)+7;
else
tempx = (rand()%12)+27;
tempy = (rand()%17)+5;
} while(!(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91));
// Move tempx to the beginning of the word selected
while(bigString[getCharLoc(tempy,tempx)-1]>64 && bigString[getCharLoc(tempy,tempx)-1]<91){
tempx--;
if(tempx==6 || tempx==26){
tempx+=12;
tempy--;
}
}
// Mark the start of the word
startx = tempx;
starty = tempy;
// Read the word into currentChar
charCounter = 0;
while(bigString[getCharLoc(tempy,tempx)+1]>64 && bigString[getCharLoc(tempy,tempx)+1]<91){
currentChar[charCounter] = bigString[getCharLoc(tempy,tempx)];
charCounter++;
tempx++;
if(tempx==19 || tempx==39){
tempx-=12;
tempy++;
}
}
// Check if currentChar = correctWord
allCorrect=1;
for(i=0;i<WORD_SIZE;i++){
if(currentChar[i]!=correctWord[i])
allCorrect = 0;
}
} while(allCorrect); // Pick again if the correct word was chosen
tempx = startx;
tempy = starty;
while(bigString[getCharLoc(tempy,tempx)]>64 && bigString[getCharLoc(tempy,tempx)]<91){
mvprintw(tempy,tempx,"%c",'.');
bigString[getCharLoc(tempy,tempx)] = '.';
tempx++;
if(tempx==19 || tempx==39){
tempx-=12;
tempy++;
}
}
mvinnstr(21,40, buf, 14);
mvprintw(17,40, "%s", buf);
mvprintw(18,40,">Dud");
mvprintw(19,40,">removed.");
}
}
// Else compare it to the correct word
else{
// Get the number of letters that match up with the correct word
int rightLetters = WORD_SIZE;
for(i=0;i<WORD_SIZE; i++){
if(currentChar[i]!=correctWord[i])
rightLetters--;
}
// If all letters matched, it's the correct word
if(rightLetters==WORD_SIZE){
mvprintw(15,40,">");
for(i=0;i<12;i++){
mvprintw(15,41+i,"%c",currentChar[i]);
}
sprintf(output,"Exact match!");
mvprintw(16,40,">");
for(i=0;i<12;i++){
mvprintw(16,41+i,"%c",output[i]);
}
sprintf(output,"Please wait ");
mvprintw(17,40,">");
for(i=0;i<12;i++){
mvprintw(17,41+i,"%c",output[i]);
}
sprintf(output,"while system");
mvprintw(18,40,">");
for(i=0;i<12;i++){
mvprintw(18,41+i,"%c",output[i]);
}
sprintf(output,"is accessed.");
mvprintw(19,40,">");
for(i=0;i<12;i++){
mvprintw(19,41+i,"%c",output[i]);
}
refresh();
SLEEP(3000000);
endwin();
if(strlen(getVictoryProg()) > 2)
system(getVictoryProg());
else if(strlen(getCompleteProg())> 2)
system(getCompleteProg());
freeAll();
exit(EXIT_SUCCESS);
}
// Otherwise, print the number right , decrement allowances, and prompt again
else{
mvprintw(17,40,">");
for(i=0;i<12;i++){
mvprintw(17,41+i,"%c",currentChar[i]);
}
sprintf(output,"Entry denied");
mvprintw(18,40,">");
for(i=0;i<12;i++){
mvprintw(18,41+i,"%c",output[i]);
}
sprintf(output,"%d/%d correct.",rightLetters,WORD_SIZE);
mvprintw(19,40,">");
for(i=0;i<12;i++){
mvprintw(19,41+i,"%c",output[i]);
}
allowances--;
}
}
move(y,x);
}
refresh();
}
}