summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.clang-format45
-rw-r--r--.clang-tidy20
-rw-r--r--.gitignore5
-rw-r--r--bcrypt-test.c48
-rw-r--r--compile_flags.txt5
-rw-r--r--makefile19
6 files changed, 108 insertions, 34 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..b32bad6
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,45 @@
1---
2AccessModifierOffset: -4
3AlignConsecutiveAssignments: 'true'
4AlignConsecutiveDeclarations: 'true'
5AlignEscapedNewlines: Left
6AlignTrailingComments: 'true'
7AlwaysBreakAfterReturnType: TopLevelDefinitions
8BreakBeforeBraces: Stroustrup
9BreakConstructorInitializers: BeforeComma
10BreakInheritanceList: BeforeComma
11ColumnLimit: '0'
12CompactNamespaces: 'false'
13Cpp11BracedListStyle: 'false'
14FixNamespaceComments: 'true'
15IncludeBlocks: Regroup
16IncludeCategories:
17 - Regex: '^.*(precomp|pch|stdafx)'
18 Priority: -1
19 - Regex: '^<.*>'
20 Priority: 1
21 - Regex: '^".*"'
22 Priority: 2
23 - Regex: '.*'
24 Priority: 3
25IndentCaseLabels: 'false'
26IndentPPDirectives: AfterHash
27IndentWidth: '4'
28IndentWrappedFunctionNames: 'false'
29KeepEmptyLinesAtTheStartOfBlocks: 'false'
30PointerAlignment: Right
31SortIncludes: 'true'
32SpaceAfterCStyleCast: 'true'
33SpaceAfterTemplateKeyword: 'false'
34SpaceBeforeAssignmentOperators: 'true'
35SpaceBeforeParens: ControlStatements
36SpaceBeforeRangeBasedForLoopColon: 'false'
37SpaceInEmptyParentheses: 'false'
38SpacesInAngles: 'false'
39SpacesInCStyleCastParentheses: 'false'
40SpacesInConditionalStatement: 'true'
41SpacesInParentheses: 'false'
42Standard: Auto
43TabWidth: '4'
44UseTab: ForIndentation
45...
diff --git a/.clang-tidy b/.clang-tidy
new file mode 100644
index 0000000..fe17118
--- /dev/null
+++ b/.clang-tidy
@@ -0,0 +1,20 @@
1---
2Checks: "*,
3 -abseil-*,
4 -altera-*,
5 -android-*,
6 -fuchsia-*,
7 -google-*,
8 -llvm*,
9 -modernize-use-trailing-return-type,
10 -zircon-*,
11 -readability-else-after-return,
12 -readability-static-accessed-through-instance,
13 -readability-avoid-const-params-in-decls,
14 -cppcoreguidelines-non-private-member-variables-in-classes,
15 -misc-non-private-member-variables-in-classes,
16"
17WarningsAsErrors: ''
18HeaderFilterRegex: ''
19FormatStyle: none
20
diff --git a/.gitignore b/.gitignore
index b64b38c..4eca2bd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,4 @@
1lua-bcrypt
2
3*.o 1*.o
2*.a
4bcrypt-test 3bcrypt-test
5tags
6.tags
7user.db 4user.db
diff --git a/bcrypt-test.c b/bcrypt-test.c
index 1df296d..19bd85e 100644
--- a/bcrypt-test.c
+++ b/bcrypt-test.c
@@ -1,19 +1,20 @@
1#include <ctype.h>
2#include <stdbool.h>
1#include <stdio.h> 3#include <stdio.h>
2#include <stdlib.h> 4#include <stdlib.h>
3#include <stdbool.h>
4#include <ctype.h>
5#include <string.h> 5#include <string.h>
6 6
7#include "lua-bcrypt/compat/include/pwd.h" 7#include "bcrypt.h"
8 8
9#define USER_DATABASE "./user.db" 9#define USER_DATABASE "./user.db"
10#define SECURITY_LEVEL 12 10#define SECURITY_LEVEL 12
11#define SEP '|' 11#define SEP '|'
12#define MAX_LINE_LENGTH 256
12 13
13static bool 14static bool
14valid_char(int ch) 15valid_char(int chr)
15{ 16{
16 return isalnum(ch) || ch == '_' || ch == '-' || ch == '.'; 17 return isalnum(chr) || chr == '_' || chr == '-' || chr == '.';
17} 18}
18 19
19/** 20/**
@@ -25,7 +26,7 @@ valid_char(int ch)
25 * \return true on success (user was created) or false on error 26 * \return true on success (user was created) or false on error
26 */ 27 */
27bool 28bool
28create_user(const char *username, const char *password) 29create_user(const char *username, const char *password) // NOLINT
29{ 30{
30 // check for invalid characters in username 31 // check for invalid characters in username
31 for ( const char *ptr = username; *ptr; ++ptr ) { 32 for ( const char *ptr = username; *ptr; ++ptr ) {
@@ -34,8 +35,8 @@ create_user(const char *username, const char *password)
34 } 35 }
35 } 36 }
36 37
37 FILE *db = fopen(USER_DATABASE, "a"); 38 FILE *database = fopen(USER_DATABASE, "a");
38 if ( db == NULL ) { 39 if ( database == NULL ) {
39 return false; 40 return false;
40 } 41 }
41 42
@@ -45,8 +46,8 @@ create_user(const char *username, const char *password)
45 return false; 46 return false;
46 } 47 }
47 48
48 fprintf(db, "%s%c%s\n", username, SEP, hash); 49 (void) fprintf(database, "%s%c%s\n", username, SEP, hash);
49 fclose(db); 50 (void) fclose(database);
50 51
51 return true; 52 return true;
52} 53}
@@ -60,18 +61,18 @@ create_user(const char *username, const char *password)
60 * @return true, if the user was successfully authenticated, otherwise false 61 * @return true, if the user was successfully authenticated, otherwise false
61 */ 62 */
62bool 63bool
63check_user(const char *const username, const char *const password) 64check_user(const char *const username, const char *const password) // NOLINT
64{ 65{
65 FILE *db = fopen(USER_DATABASE, "r"); 66 FILE *database = fopen(USER_DATABASE, "r");
66 if ( db == NULL ) { 67 if ( database == NULL ) {
67 return false; 68 return false;
68 } 69 }
69 70
70 const size_t username_length = strlen(username); 71 const size_t username_length = strlen(username);
71 char line[256]; 72 char line[MAX_LINE_LENGTH];
72 73
73 bool found = false; 74 bool found = false;
74 while ( !found && fgets(line, sizeof line, db) ) { 75 while ( !found && fgets(line, sizeof line, database) ) {
75 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) 76 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists)
76 77
77 if ( strncmp(line, username, username_length) != 0 ) { 78 if ( strncmp(line, username, username_length) != 0 ) {
@@ -82,7 +83,7 @@ check_user(const char *const username, const char *const password)
82 continue; 83 continue;
83 } 84 }
84 85
85 if ( bcrypt_checkpass(password, &line[username_length+1]) != 0 ) { 86 if ( bcrypt_checkpass(password, &line[username_length + 1]) != 0 ) {
86 continue; 87 continue;
87 } 88 }
88 89
@@ -90,7 +91,7 @@ check_user(const char *const username, const char *const password)
90 found = true; 91 found = true;
91 } 92 }
92 93
93 fclose(db); 94 (void) fclose(database);
94 95
95 return found; 96 return found;
96} 97}
@@ -105,16 +106,16 @@ check_user(const char *const username, const char *const password)
105bool 106bool
106check_user_if_exists(const char *username) 107check_user_if_exists(const char *username)
107{ 108{
108 FILE *db = fopen(USER_DATABASE, "r"); 109 FILE *database = fopen(USER_DATABASE, "r");
109 if ( db == NULL ) { 110 if ( database == NULL ) {
110 return false; 111 return false;
111 } 112 }
112 113
113 const size_t username_length = strlen(username); 114 const size_t username_length = strlen(username);
114 char line[256]; 115 char line[MAX_LINE_LENGTH];
115 116
116 bool found = false; 117 bool found = false;
117 while ( !found && fgets(line, sizeof line, db) ) { 118 while ( !found && fgets(line, sizeof line, database) ) {
118 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) 119 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists)
119 120
120 if ( strncmp(line, username, username_length) != 0 ) { 121 if ( strncmp(line, username, username_length) != 0 ) {
@@ -129,7 +130,7 @@ check_user_if_exists(const char *username)
129 found = true; 130 found = true;
130 } 131 }
131 132
132 fclose(db); 133 (void) fclose(database);
133 134
134 return found; 135 return found;
135} 136}
@@ -143,6 +144,3 @@ main(void)
143 144
144 return EXIT_SUCCESS; 145 return EXIT_SUCCESS;
145} 146}
146
147
148
diff --git a/compile_flags.txt b/compile_flags.txt
new file mode 100644
index 0000000..93961f5
--- /dev/null
+++ b/compile_flags.txt
@@ -0,0 +1,5 @@
1-Wall
2-Werror
3-Wno-pointer-sign
4-std=c17
5-I../lua-bcrypt/src
diff --git a/makefile b/makefile
index f98f66c..e0a8644 100644
--- a/makefile
+++ b/makefile
@@ -1,13 +1,22 @@
1all: bcrypt-test 1all: bcrypt-test
2 2
3CFLAGS =-Wall -Werror -std=c99 -pedantic -g 3# git clone https://github.com/mikejsavage/lua-bcrypt.git
4LUA_BCRYPT_PATH=../lua-bcrypt/src
4 5
5bcrypt-test: bcrypt-test.o 6CFLAGS =-Wall -Werror -Wno-pointer-sign -std=c17 -O2 -I$(LUA_BCRYPT_PATH)
6 cc -o $@ bcrypt-test.o lua-bcrypt/bcrypt.a
7 7
8bcrypt-test.o: bcrypt-test.c 8bcrypt-test: bcrypt-test.o bcrypt-lib.a
9 cc -o $@ bcrypt-test.o bcrypt-lib.a
10
11bcrypt-lib.a: bcrypt.o blowfish.o ggentropy.o safebfuns.o
12 ar -crs $@ $^
13
14%.o: %.c
15 cc $(CFLAGS) -o $@ -c $<
16
17%.o: $(LUA_BCRYPT_PATH)/%.c
9 cc $(CFLAGS) -o $@ -c $< 18 cc $(CFLAGS) -o $@ -c $<
10 19
11.PHONY: clean 20.PHONY: clean
12clean: 21clean:
13 rm -f bcrypt-test bcrypt-test.o 22 rm -f bcrypt-test *.a *.o