Browse Source

implement Caesar transform

Scitoshi Nakayobro 1 year ago
parent
commit
bf24ca469b
5 changed files with 2771 additions and 9 deletions
  1. 1
    1
      build
  2. 31
    0
      src/caesar.lisp
  3. 13
    8
      src/main.lisp
  4. 1363
    0
      test_data/in.txt
  5. 1363
    0
      test_data/out.txt

+ 1
- 1
build View File

@@ -1,2 +1,2 @@
1 1
 #!/bin/bash
2
-urn --emit-lua bin/cct src/main.lisp
2
+urn --include src --emit-lua bin/cct src/main.lisp

+ 31
- 0
src/caesar.lisp View File

@@ -0,0 +1,31 @@
1
+(import core/string (string->bytes bytes->string))
2
+
3
+(define ALPHABET_LETTERS 	:hidden 26)
4
+(define CAPITAL_A_ASCII 	:hidden 65)
5
+(define CAPITAL_Z_ASCII 	:hidden 90)
6
+(define LOWER_A_ASCII 		:hidden 97)
7
+(define LOWER_Z_ASCII 		:hidden 122)
8
+
9
+(defun do-shift (c shift _min _max)
10
+	:hidden
11
+	(with (n (+ c shift))
12
+		(cond
13
+			[(< n _min)
14
+				(+ n ALPHABET_LETTERS)]
15
+			[(> n _max)
16
+				(- n ALPHABET_LETTERS)]
17
+			[else
18
+				n])))
19
+
20
+(defun transform (shift text)
21
+	(let* [(original (string->bytes text))
22
+		   (transformed '())]
23
+		(for-each c original
24
+			(cond
25
+				[(and (>= c CAPITAL_A_ASCII) (<= c CAPITAL_Z_ASCII))
26
+					(push! transformed (do-shift c shift CAPITAL_A_ASCII CAPITAL_Z_ASCII))]
27
+				[(and (>= c LOWER_A_ASCII) (<= c LOWER_Z_ASCII))
28
+					(push! transformed (do-shift c shift LOWER_A_ASCII LOWER_Z_ASCII))]
29
+				[else
30
+					(push! transformed c)]))
31
+		(bytes->string transformed)))

+ 13
- 8
src/main.lisp View File

@@ -1,7 +1,10 @@
1 1
 (import core/base (get-idx))
2
+(import io (read-all!))
3
+
4
+(import caesar)
2 5
 
3 6
 (defun usage! ()
4
-	(print! "Usage: cct <encrypt|decrypt|analyze|crack> [shift] <file>")
7
+	(print! "Usage: cct <transform|analyze|crack> [shift] <file>")
5 8
 	(exit!))
6 9
 
7 10
 (defun nargs-or-usage! (n (fn /=))
@@ -9,15 +12,17 @@
9 12
 		(print! "Not enough arguments")
10 13
 		(usage!)))
11 14
 
12
-(nargs-or-usage! 1 <)
13
-(with (mode (get-idx *arguments* 1))
15
+(nargs-or-usage! 2 <)
16
+(let* [(mode (get-idx *arguments* 1))
17
+	   (filename (get-idx *arguments* (len# *arguments*)))
18
+	   (data (read-all! filename))]
19
+	(when (= data nil)
20
+		(print! ($ "Couldn't read file '${filename}'"))
21
+		(exit!))
14 22
 	(case mode
15
-		["encrypt"
16
-			(nargs-or-usage! 3)
17
-			]
18
-		["decrypt"
23
+		["transform"
19 24
 			(nargs-or-usage! 3)
20
-			]
25
+			(print! (caesar/transform (string->number (get-idx *arguments* 2)) data))]
21 26
 		["analyze"
22 27
 			(nargs-or-usage! 2)
23 28
 			]

+ 1363
- 0
test_data/in.txt
File diff suppressed because it is too large
View File


+ 1363
- 0
test_data/out.txt
File diff suppressed because it is too large
View File


Loading…
Cancel
Save