From 2d4f1f5ec44e7056408dc1c080de2736914ea4a2 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Sun, 4 Jul 2021 23:44:58 -0600 Subject: [PATCH] added MIT license and created headers and code files for all the types --- LICENSE | 8 +++++ aux.c | 12 +++++++ aux.h | 9 ++++++ build.sh | 2 +- cart.c | 0 cart.h | 7 +++++ command.h | 12 +++++++ err.h | 2 ++ realm.c | 0 realm.h | 8 +++++ universe.c | 0 universe.h | 11 +++++++ user.c | 0 user.h | 8 +++++ util.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ util.h | 15 +++++++++ xrxs.c | 92 +++++++++++------------------------------------------- 17 files changed, 203 insertions(+), 75 deletions(-) create mode 100644 LICENSE create mode 100644 aux.c create mode 100644 aux.h create mode 100644 cart.c create mode 100644 cart.h create mode 100644 command.h create mode 100644 err.h create mode 100644 realm.c create mode 100644 realm.h create mode 100644 universe.c create mode 100644 universe.h create mode 100644 user.c create mode 100644 user.h create mode 100644 util.c create mode 100644 util.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..219325a --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +Copyright 2021 Derek Stevens + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/aux.c b/aux.c new file mode 100644 index 0000000..f28da3e --- /dev/null +++ b/aux.c @@ -0,0 +1,12 @@ +#include +#include +#include "aux.h" + +Aux* create_aux(FileType t) { + Aux* self = (Aux*)malloc(sizeof(Aux)); + self->type = t; + self->data = nil; + self->count = 0; + + return self; +} \ No newline at end of file diff --git a/aux.h b/aux.h new file mode 100644 index 0000000..d926712 --- /dev/null +++ b/aux.h @@ -0,0 +1,9 @@ +typedef enum { CTL = 1, USERS, CARTS, SLOT, DATA, REALMS, UNIVERSE } FileType; + +typedef struct Aux { + FileType type; + char* data; + int count; +} Aux; + +Aux* create_aux(FileType t); \ No newline at end of file diff --git a/build.sh b/build.sh index 84b6024..833b50e 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,6 @@ #!/bin/sh -clang-format -i ./*.c +clang-format -i ./*.c ./*.h mk clean mk o.xrxs diff --git a/cart.c b/cart.c new file mode 100644 index 0000000..e69de29 diff --git a/cart.h b/cart.h new file mode 100644 index 0000000..8f0253a --- /dev/null +++ b/cart.h @@ -0,0 +1,7 @@ +typedef struct Cart { + char name[32]; + char* cart_rom; + char* cart_txt_data; + char* cart_sprite_data; + char* cart_audio_data; +} Cart; \ No newline at end of file diff --git a/command.h b/command.h new file mode 100644 index 0000000..ed30220 --- /dev/null +++ b/command.h @@ -0,0 +1,12 @@ +typedef enum { + LOGIN = 208176873, + LOAD = 5626172, + CREATE = 7083959236, + PROTECT = 295480618573, + ENTER = 195024746, + LEAVE = 207662601, + LOGOUT = 7702552890, + SAVE = 5962355, + RESET = 218931595, + UNLOAD = 8325026851 +} Command; \ No newline at end of file diff --git a/err.h b/err.h new file mode 100644 index 0000000..1031da5 --- /dev/null +++ b/err.h @@ -0,0 +1,2 @@ +#define EUNAME "username is already taken" +#define EUFULL "users table is full" \ No newline at end of file diff --git a/realm.c b/realm.c new file mode 100644 index 0000000..e69de29 diff --git a/realm.h b/realm.h new file mode 100644 index 0000000..b88d2a5 --- /dev/null +++ b/realm.h @@ -0,0 +1,8 @@ +typedef struct Universe Universe; + +typedef struct Realm { + char name[32]; + ushort max; + uvlong password; + Universe* universe; +} Realm; diff --git a/universe.c b/universe.c new file mode 100644 index 0000000..e69de29 diff --git a/universe.h b/universe.h new file mode 100644 index 0000000..0e23ed7 --- /dev/null +++ b/universe.h @@ -0,0 +1,11 @@ +typedef unsigned int unit; + +typedef struct Atom { + char name[16]; + char value[64]; +} Atom; + +typedef struct Universe { + uint count; + Atom** data; +} Universe; diff --git a/user.c b/user.c new file mode 100644 index 0000000..e69de29 diff --git a/user.h b/user.h new file mode 100644 index 0000000..709e7c9 --- /dev/null +++ b/user.h @@ -0,0 +1,8 @@ +typedef struct Cart Cart; +typedef struct Realm Realm; +typedef struct UserInfo { + char name[32]; + uvlong password; + Cart* cart; + Realm* realm; +} UserInfo; diff --git a/util.c b/util.c new file mode 100644 index 0000000..5fa3e09 --- /dev/null +++ b/util.c @@ -0,0 +1,92 @@ +#include +#include "util.h" + +uvlong hash(char* str) { + uvlong h; + uchar* p; + + h = 0; + for (p = (unsigned char*)str; *p != '\0'; p++) + h = 37 * h + *p; + return h; // or, h % ARRAY_SIZE; +} + +char clca(char c) { + return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; +} /* char to lowercase */ + +char cuca(char c) { + return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; +} /* char to uppercase */ + +int slen(char* s) { + int i = 0; + while (s[i] && s[++i]) { + ; + } + return i; +} /* string length */ + +char* st__(char* s, char (*fn)(char)) { + int i = 0; + char c; + while ((c = s[i])) + s[i++] = fn(c); + return s; +} +char* stuc(char* s) { return st__(s, cuca); } /* string to uppercase */ + +char* stlc(char* s) { return st__(s, clca); } /* string to lowercase */ + +char* scpy(char* src, char* dst, int len) { + int i = 0; + while ((dst[i] = src[i]) && i < len - 2) + i++; + dst[i + 1] = '\0'; + return dst; +} /* string copy */ + +int scmp(char* a, char* b) { + int i = 0; + while (a[i] == b[i]) + if (!a[i++]) + return 1; + return 0; +} /* string compare */ + +char* scsw(char* s, char a, char b) { + int i = 0; + char c; + while ((c = s[i])) + s[i++] = c == a ? b : c; + return s; +} /* string char swap */ + +char* scat(char* dst, const char* src) { + char* ptr = dst + slen(dst); + while (*src) + *ptr++ = *src++; + *ptr = '\0'; + return dst; +} /* string cat */ + +int ssin(char* s, char* ss) { + int a = 0, b = 0; + while (s[a]) { + if (s[a] == ss[b]) { + if (!ss[b + 1]) + return a - b; + b++; + } else + b = 0; + a++; + } + return -1; +} /* string substring index */ + +char* ccat(char* dst, char c) { + int len = slen(dst); + dst[len] = c; + dst[len + 1] = '\0'; + return dst; +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..70f8c83 --- /dev/null +++ b/util.h @@ -0,0 +1,15 @@ +typedef unsigned long long uvlong; + +uvlong hash(char* str); +char clca(char c); +char cuca(char c); +int slen(char* str); +char* st__(char* str, char (*fn)(char c)); +char* stuc(char* str); +char* stlc(char* str); +char* scpy(char* src, char* dst, int len); +int scmp(char* a, char* b); +char* scsw(char* str, char a, char b); +char* scat(char* dst, const char* src); +int ssin(char* str, char* substr); +char* ccat(char* dst, char c); diff --git a/xrxs.c b/xrxs.c index ba72070..bb94c8e 100644 --- a/xrxs.c +++ b/xrxs.c @@ -6,84 +6,21 @@ #include #include #include -#include -#include +#include "err.h" +#include "command.h" +#include "util.h" +#include "aux.h" +#include "cart.h" +#include "universe.h" +#include "realm.h" +#include "user.h" -/* clang-format off */ - -char clca(char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; } /* char to lowercase */ -char cuca(char c) { return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; } /* char to uppercase */ -int slen(char *s) { int i = 0; while(s[i] && s[++i]) { ; } return i; } /* string length */ -char *st__(char *s, char (*fn)(char)) { int i = 0; char c; while((c = s[i])) s[i++] = fn(c); return s; } -char *stuc(char *s) { return st__(s, cuca); } /* string to uppercase */ -char *stlc(char *s) { return st__(s, clca); } /* string to lowercase */ -char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */ -int scmp(char *a, char *b) { int i = 0; while(a[i] == b[i]) if(!a[i++]) return 1; return 0; } /* string compare */ -char *scsw(char *s, char a, char b) { int i = 0; char c; while((c = s[i])) s[i++] = c == a ? b : c; return s; } /* string char swap */ -char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */ -int ssin(char *s, char *ss) { int a = 0, b = 0; while(s[a]) { if(s[a] == ss[b]) { if(!ss[b + 1]) return a - b; b++; } else b = 0; a++; } return -1; } /* string substring index */ -char *ccat(char *dst, char c) { int len = slen(dst); dst[len] = c; dst[len + 1] = '\0'; return dst; } - -/* clang-format on */ int chatty9p = 1; -static char Ebad[] = "something bad happened"; -static char Enomem[] = "no memory"; -static char Euname[] = "username is already taken"; - static Tree* tree; -typedef enum { - LOGIN = 208176873, - LOAD = 5626172, - CREATE = 7083959236, - PROTECT = 295480618573, - ENTER = 195024746, - LEAVE = 207662601, - LOGOUT = 7702552890, - SAVE = 5962355, - RESET = 218931595, - UNLOAD = 8325026851 -} Command; - -typedef enum { CTL = 1, USERS, CARTS, SLOT, DATA, REALMS, UNIVERSE } FileType; - -typedef struct Aux Aux; -struct Aux { - FileType type; - char* data; - int count; -}; - -Aux* create_aux(FileType t) { - Aux* self = (Aux*)malloc(sizeof(Aux)); - self->type = t; - self->data = nil; - self->count = 0; - - return self; -} - -typedef struct UserInfo UserInfo; -struct UserInfo { - char name[32]; - uvlong password; - char realm[32]; - char cart[32]; -}; - static UserInfo users_table[64]; -uvlong hash(char* str) { - uvlong h; - uchar* p; - - h = 0; - for (p = (unsigned char*)str; *p != '\0'; p++) - h = 37 * h + *p; - return h; // or, h % ARRAY_SIZE; -} - void xrxs_attach(Req* r) { /* As it is, once the user detaches, they will stay in the table * until the server restarts. We have to figure out some way @@ -92,6 +29,7 @@ void xrxs_attach(Req* r) { */ int i = 0; int l = 0; + int vacancy = 0; char* usr; char* username = r->ifcall.uname; usr = username; @@ -102,15 +40,20 @@ void xrxs_attach(Req* r) { for (i = 0; i < 64; i++) { usr = users_table[i].name; if (scmp(usr, username)) { - respond(r, Euname); + respond(r, EUNAME); return; } if (*usr == 0) { scpy(username, usr, l + 1); + vacancy = 1; break; } } - respond(r, nil); + if (vacancy) { + respond(r, nil); + } else { + respond(r, EUFULL); + } } void login(char* uname, char* password) { @@ -129,7 +72,8 @@ int logout(char* uname) { if (scmp(uname, users_table[i].name)) { *(users_table[i].name) = 0; users_table[i].password = 0; - *(users_table[i].realm) = 0; + /* free cart */ + /* free realm */ return 1; } }