aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp335
1 files changed, 176 insertions, 159 deletions
diff --git a/src/util.cpp b/src/util.cpp
index 142fcbd..dc95441 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1,16 +1,17 @@
-#include <string>
-#include <string.h>
#include <cstddef>
-#include <stdio.h>
#include <stdarg.h>
-#include <time.h>
+#include <stdio.h>
+#include <string.h>
+#include <string>
#include <sys/timeb.h>
+#include <time.h>
#include "util.hpp"
-#define VALID_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+#define VALID_CHARACTERS \
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
-enum Command parse_command(char* line){
+enum Command parse_command(char *line) {
/* Find a valid command in the first word of the line (case sensitive)
args:
line (char*): string of characters from the input file
@@ -18,130 +19,154 @@ enum Command parse_command(char* line){
returns:
(command): Returns the found command. Returns else if first word is invalid.
*/
- if (line == NULL){
+ if (line == NULL) {
return INVALID;
}
- char* rest;
- char* tok = strtok_r(line, " ", &rest);
+ char *rest;
+ char *tok = strtok_r(line, " ", &rest);
- if (tok == NULL){
+ if (tok == NULL) {
return INVALID;
}
// REGISTER [username] [password]
- if (strcmp(tok, "REGISTER") == 0){
- char* username = strtok_r(rest, " ", &rest);
- if(!isValidUsername(username)) return INVALID;
+ if (strcmp(tok, "REGISTER") == 0) {
+ char *username = strtok_r(rest, " ", &rest);
+ if (!isValidUsername(username))
+ return INVALID;
- char* password = strtok_r(rest, " ", &rest);
- if(password == NULL) return INVALID;
- if(!isValidPassword(password)) return INVALID;
+ char *password = strtok_r(rest, " ", &rest);
+ if (password == NULL)
+ return INVALID;
+ if (!isValidPassword(password))
+ return INVALID;
- char* misc = strtok_r(rest, " ", &rest);
- if(misc != NULL) return INVALID;
+ char *misc = strtok_r(rest, " ", &rest);
+ if (misc != NULL)
+ return INVALID;
return REGISTER;
}
// LOGIN [username] [password]
- if (strcmp(tok, "LOGIN") == 0){
- char* username = strtok_r(rest, " ", &rest);
- if(!isValidUsername(username)) return INVALID;
+ if (strcmp(tok, "LOGIN") == 0) {
+ char *username = strtok_r(rest, " ", &rest);
+ if (!isValidUsername(username))
+ return INVALID;
- char* password = strtok_r(rest, " ", &rest);
- if(password == NULL) return INVALID;
- if(!isValidPassword(password)) return INVALID;
+ char *password = strtok_r(rest, " ", &rest);
+ if (password == NULL)
+ return INVALID;
+ if (!isValidPassword(password))
+ return INVALID;
- char* misc = strtok_r(rest, " ", &rest);
- if(misc != NULL) return INVALID;
+ char *misc = strtok_r(rest, " ", &rest);
+ if (misc != NULL)
+ return INVALID;
return LOGIN;
}
// LOGOUT
- if (strcmp(tok, "LOGOUT") == 0){
- char* misc = strtok_r(rest, " ", &rest);
- if(misc != NULL) return INVALID;
+ if (strcmp(tok, "LOGOUT") == 0) {
+ char *misc = strtok_r(rest, " ", &rest);
+ if (misc != NULL)
+ return INVALID;
return LOGOUT;
}
// SEND [msg]
- if (strcmp(tok, "SEND") == 0){
- if (int(strlen(rest) == 0)) return INVALID;
+ if (strcmp(tok, "SEND") == 0) {
+ if (int(strlen(rest) == 0))
+ return INVALID;
return SEND;
}
// SEND2 [username] [msg]
- if (strcmp(tok, "SEND2") == 0){
- char* username = strtok_r(rest, " ", &rest);
- if(!isValidUsername(username)) return INVALID;
+ if (strcmp(tok, "SEND2") == 0) {
+ char *username = strtok_r(rest, " ", &rest);
+ if (!isValidUsername(username))
+ return INVALID;
- if (int(strlen(rest) == 0)) return INVALID;
+ if (int(strlen(rest) == 0))
+ return INVALID;
return SEND2;
}
// SENDA [msg]
- if (strcmp(tok, "SENDA") == 0){
- if (int(strlen(rest) == 0)) return INVALID;
+ if (strcmp(tok, "SENDA") == 0) {
+ if (int(strlen(rest) == 0))
+ return INVALID;
return SENDA;
}
// SENDA2 [username] [msg]
- if (strcmp(tok, "SENDA2") == 0){
- char* username = strtok_r(rest, " ", &rest);
- if(!isValidUsername(username)) return INVALID;
+ if (strcmp(tok, "SENDA2") == 0) {
+ char *username = strtok_r(rest, " ", &rest);
+ if (!isValidUsername(username))
+ return INVALID;
- if (int(strlen(rest) == 0)) return INVALID;
+ if (int(strlen(rest) == 0))
+ return INVALID;
return SENDA2;
}
// SENDF [local file]
- if (strcmp(tok, "SENDF") == 0){
- char* file_name = strtok_r(rest, " ", &rest);
- if(file_name == NULL) return INVALID;
+ if (strcmp(tok, "SENDF") == 0) {
+ char *file_name = strtok_r(rest, " ", &rest);
+ if (file_name == NULL)
+ return INVALID;
- if (int(strlen(rest) != 0)) return INVALID;
+ if (int(strlen(rest) != 0))
+ return INVALID;
return SENDF;
}
// SENDF2 [username] [local file]
- if (strcmp(tok, "SENDF2") == 0){
- char* username = strtok_r(rest, " ", &rest);
- if(!isValidUsername(username)) return INVALID;
+ if (strcmp(tok, "SENDF2") == 0) {
+ char *username = strtok_r(rest, " ", &rest);
+ if (!isValidUsername(username))
+ return INVALID;
- char* file_name = strtok_r(rest, " ", &rest);
- if(file_name == NULL) return INVALID;
+ char *file_name = strtok_r(rest, " ", &rest);
+ if (file_name == NULL)
+ return INVALID;
- if (int(strlen(rest) != 0)) return INVALID;
+ if (int(strlen(rest) != 0))
+ return INVALID;
return SENDF2;
}
// LIST
- if (strcmp(tok, "LIST") == 0){
- char* misc = strtok_r(rest, " ", &rest);
- if(misc != NULL) return INVALID;
+ if (strcmp(tok, "LIST") == 0) {
+ char *misc = strtok_r(rest, " ", &rest);
+ if (misc != NULL)
+ return INVALID;
return LIST;
}
// DELAY
- if (strcmp(tok, "DELAY") == 0){
- char* delay_time = strtok_r(rest, " ", &rest);
- if(delay_time == NULL) return INVALID;
+ if (strcmp(tok, "DELAY") == 0) {
+ char *delay_time = strtok_r(rest, " ", &rest);
+ if (delay_time == NULL)
+ return INVALID;
std::string str(delay_time);
std::size_t found = str.find_first_not_of("0123456789");
- if(found != std::string::npos) return INVALID;
+ if (found != std::string::npos)
+ return INVALID;
- if (int(strlen(rest) != 0)) return INVALID;
+ if (int(strlen(rest) != 0))
+ return INVALID;
return DELAY;
}
@@ -150,8 +175,7 @@ enum Command parse_command(char* line){
return INVALID;
}
-
-bool isValidUsername(char* username){
+bool isValidUsername(char *username) {
/* Helper function to check if a username string is valid
args:
username (char*): Username to be tested.
@@ -159,52 +183,58 @@ bool isValidUsername(char* username){
returns:
bool: Username is valid ? true, false
*/
- if(username == NULL) return false;
- else if ((strlen(username) > MAX_USERNAME_LEN)||(strlen(username) < MIN_USERNAME_LEN)) return false;
- else{
+ if (username == NULL)
+ return false;
+ else if ((strlen(username) > MAX_USERNAME_LEN) ||
+ (strlen(username) < MIN_USERNAME_LEN))
+ return false;
+ else {
std::string str(username);
std::size_t found = str.find_first_not_of(VALID_CHARACTERS);
- if(found != std::string::npos) return false;
+ if (found != std::string::npos)
+ return false;
}
return true;
}
-
-bool isValidPassword(char* password){
- if(password == NULL) return false;
- else if ((strlen(password) > MAX_PASSWORD_LEN)||(strlen(password) < MIN_PASSWORD_LEN)) return false;
- else{
+bool isValidPassword(char *password) {
+ if (password == NULL)
+ return false;
+ else if ((strlen(password) > MAX_PASSWORD_LEN) ||
+ (strlen(password) < MIN_PASSWORD_LEN))
+ return false;
+ else {
std::string str(password);
std::size_t found = str.find_first_not_of(VALID_CHARACTERS);
- if(found != std::string::npos) return false;
+ if (found != std::string::npos)
+ return false;
}
return true;
}
-
-void Error(const char * format, ...) {
- char msg[4096];
- va_list argptr;
- va_start(argptr, format);
- vsprintf(msg, format, argptr);
- va_end(argptr);
- fprintf(stderr, "Error: %s\n", msg);
- exit(-1);
+void Error(const char *format, ...) {
+ char msg[4096];
+ va_list argptr;
+ va_start(argptr, format);
+ vsprintf(msg, format, argptr);
+ va_end(argptr);
+ fprintf(stderr, "Error: %s\n", msg);
+ exit(-1);
}
-void Log(const char * format, ...) {
- char msg[2048];
- va_list argptr;
- va_start(argptr, format);
- vsprintf(msg, format, argptr);
- va_end(argptr);
- fprintf(stderr, "%s\n", msg);
+void Log(const char *format, ...) {
+ char msg[2048];
+ va_list argptr;
+ va_start(argptr, format);
+ vsprintf(msg, format, argptr);
+ va_end(argptr);
+ fprintf(stderr, "%s\n", msg);
}
// convert buf[0:3] to int
-void buf2i(BYTE * buf, int &i){
+void buf2i(BYTE *buf, int &i) {
// No way to avoid Segmentation fault. Some bytes may be 0
i = 0;
i += buf[3];
@@ -214,7 +244,7 @@ void buf2i(BYTE * buf, int &i){
}
// convert int to buf[4] where buf[0] contains MSB's and buf[3] contains LSB's
-void i2buf(int &i, BYTE * buf){
+void i2buf(int &i, BYTE *buf) {
// No way to avoid Segmentation fault. Some bytes may be 0
buf[0] = (BYTE)((i & 0xFF000000) >> 24);
buf[1] = (BYTE)((i & 0x00FF0000) >> 16);
@@ -222,36 +252,24 @@ void i2buf(int &i, BYTE * buf){
buf[3] = (BYTE)(i & 0x000000FF);
}
-const char * com2str(enum Command command){
- const char * com2str[] = {"REGISTER",
- "LOGIN",
- "LOGOUT",
- "SEND",
- "SEND2",
- "SENDA",
- "SENDA2",
- "SENDF",
- "SENDF2",
- "LIST",
- "DELAY",
- "GETF",
- "PING",
- "CONNECT",
- "",
- "INVALID"
- };
- return com2str[command];
+const char *com2str(enum Command command) {
+ const char *com2str[] = {"REGISTER", "LOGIN", "LOGOUT", "SEND",
+ "SEND2", "SENDA", "SENDA2", "SENDF",
+ "SENDF2", "LIST", "DELAY", "GETF",
+ "PING", "CONNECT", "", "INVALID"};
+ return com2str[command];
}
// Computes the difference between times a and b in seconds
// a - b = result
-double timeDifference(struct timeb a, struct timeb b){
- return (a.time + a.millitm / (double) 1000.0f) - (b.time + b.millitm / (double) 1000.0f);
+double timeDifference(struct timeb a, struct timeb b) {
+ return (a.time + a.millitm / (double)1000.0f) -
+ (b.time + b.millitm / (double)1000.0f);
}
-int getNumEntries(const char* database){
- FILE* fp = fopen(database, "r");
- if (!fp){
+int getNumEntries(const char *database) {
+ FILE *fp = fopen(database, "r");
+ if (!fp) {
Log("Username check failed to open the file: %s", database);
return -1;
}
@@ -259,9 +277,9 @@ int getNumEntries(const char* database){
int numEntries = 0;
char line[LINE_SIZE] = {0};
- while(fgets(line, LINE_SIZE, fp) != NULL){
- char* new_line_pos = strchr(line, '\n');
- if(new_line_pos != NULL)
+ while (fgets(line, LINE_SIZE, fp) != NULL) {
+ char *new_line_pos = strchr(line, '\n');
+ if (new_line_pos != NULL)
*new_line_pos = 0;
// Including blank lines as entries. Don't use any blank lines.
numEntries++;
@@ -272,28 +290,30 @@ int getNumEntries(const char* database){
return numEntries;
}
-// User/password is in registry: true
-// User/password is not in registry: false
-bool loginQuery(const char * database, char* username, char* password){
- FILE* fp = fopen(database, "r");
- if (!fp){
- Log("Registry check failed to open the file: %s", database);
+bool loginQuery(const char *database, char *username, char *password) {
+ FILE *fp = fopen(database, "r");
+ if (!fp) {
+ Log("Could not open user file: %s", database);
return false;
}
char line[LINE_SIZE] = {0};
- while(fgets(line, LINE_SIZE, fp) != NULL){
- char* new_line_pos = strchr(line, '\n');
- if(new_line_pos != NULL)
+ while (fgets(line, LINE_SIZE, fp) != NULL) {
+ char *new_line_pos = strchr(line, '\n');
+ if (new_line_pos != NULL)
*new_line_pos = 0;
- char* rest;
- char* usr = strtok_r(line, " ", &rest);
- char* pswd = strtok_r(rest, " ", &rest);
+ char *rest;
+ char *usr = strtok_r(line, " ", &rest);
+ char *pswd = strtok_r(rest, " ", &rest);
- //Log("username: |%s| Password: |%s|", usr, pswd);
+ if (usr == NULL || pswd == NULL) {
+ Log("Registry check failed to parse the file: %s", database);
+ fclose(fp);
+ return false;
+ }
- if((strcmp(username, usr) == 0) && (strcmp(password, pswd) == 0)){
+ if ((strcmp(username, usr) == 0) && (strcmp(password, pswd) == 0)) {
fclose(fp);
return true;
}
@@ -307,25 +327,26 @@ bool loginQuery(const char * database, char* username, char* password){
// User is in registry: true
// User is not in registry: false
-bool usernameQuery(const char * database, char* username){
- FILE* fp = fopen(database, "r");
- if (!fp){
+bool usernameQuery(const char *database, char *username) {
+ FILE *fp = fopen(database, "r");
+ if (!fp) {
Log("Username check failed to open the file: %s", database);
return false;
}
char line[LINE_SIZE] = {0};
- while(fgets(line, LINE_SIZE, fp) != NULL){
- char* new_line_pos = strchr(line, '\n');
- if(new_line_pos != NULL)
+ while (fgets(line, LINE_SIZE, fp) != NULL) {
+ char *new_line_pos = strchr(line, '\n');
+ if (new_line_pos != NULL)
*new_line_pos = 0;
- char* rest;
- char* usr = strtok_r(line, " ", &rest);
+ char *rest;
+ char *usr = strtok_r(line, " ", &rest);
- //Log("username: |%s|", usr);
+ if (usr == NULL)
+ continue;
- if(strcmp(username, usr) == 0){
+ if (strcmp(username, usr) == 0) {
fclose(fp);
return true;
}
@@ -337,10 +358,10 @@ bool usernameQuery(const char * database, char* username){
return false;
}
-void recordEntry(const char * database, char* key, char* value){
- FILE* fp = fopen(database, "a");
+void recordEntry(const char *database, char *key, char *value) {
+ FILE *fp = fopen(database, "a");
- if (!fp){
+ if (!fp) {
Log("Registry append failed to open the file: %s", database);
return;
}
@@ -355,41 +376,37 @@ void recordEntry(const char * database, char* key, char* value){
fclose(fp);
}
-void clearDatabase(const char * database){
- FILE* fp = fopen(database, "w");
- fclose(fp);
-}
-
-
-int buf2file(BYTE* buf, int nbytes, char* filename){
- FILE* f = fopen(filename, "w");
- if(!f) return -1;
+int buf2file(BYTE *buf, int nbytes, char *filename) {
+ FILE *f = fopen(filename, "w");
+ if (!f)
+ return -1;
fwrite(buf, 1, nbytes, f);
fclose(f);
return 0;
}
-
-int file2buf(char* filename, BYTE* buf){
- FILE* f = fopen(filename, "r");
- if(!f){
+int file2buf(char *filename, BYTE *buf) {
+ FILE *f = fopen(filename, "r");
+ if (!f) {
Log("Error: Cannot open %s", filename);
return -1;
}
fseek(f, 0, SEEK_END); // jump to end of file
- long nbytes = ftell(f); // find current offset on file f
+ long nbytes = ftell(f); // find current offset on file f
fseek(f, 0, SEEK_SET); // jump to beginning of file
- if(nbytes > MAX_REQUEST_SIZE){
+ if (nbytes > MAX_REQUEST_SIZE) {
Log("Error: File %s is too big: %d", filename, nbytes);
+ fclose(f);
return -1;
}
int nread = fread(buf, 1, nbytes, f);
- if(nread != nbytes){
+ if (nread != nbytes) {
Log("Error: Byte count mismatch with fread: %d, %d", nbytes, nread);
+ fclose(f);
return -1;
}