apc initial implementation
authorjordan@hack_attack <jordanlavatai@gmail.com>
Fri, 5 Aug 2016 19:38:56 +0000 (12:38 -0700)
committerjordan@hack_attack <jordanlavatai@gmail.com>
Fri, 5 Aug 2016 19:38:56 +0000 (12:38 -0700)
14 files changed:
html/index.html
html/js/auth.js
html/js/the_march.config.js
src/apc/.gitignore [new file with mode: 0644]
src/apc/fileparser.y [new file with mode: 0644]
src/apc/fileparser_old.y [new file with mode: 0644]
src/apc/filescanner.rl [new file with mode: 0644]
src/apc/filescannerparser_old.rl [new file with mode: 0644]
src/apc/ragson [new file with mode: 0755]
src/apc/ragson.sh [new file with mode: 0755]
src/apc/sprite.c [new file with mode: 0644]
src/apc/sprite.h [new file with mode: 0644]
src/apc/symbol.h [new file with mode: 0644]
src/core/auth.c [new file with mode: 0644]

index 0834270..05d0d4a 100644 (file)
@@ -5,6 +5,9 @@
    <meta charset="UTF-8" />
   </head>
   <body>
+    <input type="text" class="form-control" id="user-id" placeholder="User-ID">
+    <input type="text" class="form-control" id="password" placeholder="Password">
+    <input type="submit" id="login" value="login" class="btn btn-default">
     Usage: the_march [-opts]<br>
     <div class="row">
       <div class="col-md-5 col-md-offset-5">
@@ -16,5 +19,6 @@
     <script type="text/javascript" src="js/the_march.config.js"></script>
     <script type="text/javascript" src="js/the_march.asm.js"></script>
     <script type="text/javascript" src="js/the_march.js"></script>
+    <script type="text/javascript" src="js/auth.js"></script>
   </body>
 </html>
index d399d6f..3eb052a 100644 (file)
@@ -1,7 +1,7 @@
 /*!@file
   \brief   Authorization Handling
-  \details Upon user submission of id/password, hashes password and saves
-           result in a file on VFS using hashing algorithm provided by libgcrypt.c
+  \details Upon user submission of id/password, hashes password and
+           saves result in a file on VFS using hashing algorithm prov           ided by wolfcrypt, a subsystem of wolfSSL
   \author  JEL
   \date    2016
   -----------------------------------------------------------------------------*/
@@ -9,6 +9,9 @@
 function userLogin()
 {
     var password = document.getElementById("password").value;
-    /* Call Module.execute on encrypt_password() */
+    /* Virtual Filesystem is of type IDBFS and has been setup in            preRun in Module via JVM runtime environment*/
+    Module.execute(_auth_encrypt, password);
+
 };
+
 document.getElementById("login").addEventListener("click", userLogin, false);
index 7751827..4d4afd7 100644 (file)
@@ -2,39 +2,61 @@ var Module =
     {
         'noExitRuntime': true,
         'arguments': [],
+        'argc': 0,
+        'argv_ptr': 0,
 
-        print: function(text) { for(var i=0; i<text.length; i++) document.write(String.fromCharCode(text.charCodeAt(i))); document.write("<br />");},
-        printErr: function(text) { console.log("ERR: " + text); },
+        //print: function(text) { for(var i=0; i<text.length; i++) document.write(String.fromCharCode(text.charCodeAt(i))); },
+        print: function(text) { console.log(text); },
+        printErr: function(text) { console.log("stderr| " + text); },
 
         //Execute the default em_main instead of main
-        execute: function(text)
+        setupArgV: function(text)
         {
             const PTR_BYTES = 4;
 
             //Parse argv and argc
             var argv = text.replace(/[\s]+/g," ").split(" ");
-            var argc = argv.length;
+            argc = argv.length;
 
             //Allocate C stack memory for the arg pointers
-            var argv_ptr = Runtime.stackAlloc(argc * PTR_BYTES);
+            //argv_ptr = Runtime.stackAlloc(argc * PTR_BYTES);
+            argv_ptr = Module._malloc(argc * PTR_BYTES);
 
             var arg_ptrs = [];
             for(var i = 0; i < argc; i++)
             {
                 //stack allocate for each argument
-                arg_ptrs[i] = Runtime.stackAlloc(argv[i].length+1);
+                arg_ptrs[i] = Module._malloc(argv[i].length+1);
                 //Add to argv's list of pointers in c-memory, then copy data
                 setValue(argv_ptr + i * PTR_BYTES, arg_ptrs[i], 'i32');
                 writeStringToMemory(argv[i], arg_ptrs[i]);
             }
 
-            _em_main(argc, argv_ptr, 0);
+        },
+
+        execute: function(func_name, args)
+        {
+            this.setupArgV(args);
+            console.log(func_name);
+            func_name(argc,argv_ptr, 0);
+            /* Free args? */
+        },
+
+        preRun: function()
+        {
+            FS.mkdir('/auth');
+            FS.mount(IDBFS, {}, '/auth');
+            FS.syncfs(true, function (err) {
+                console.log("synching failed with err:" + err);
+            });
         }
+
+
     };
 
 function start_game(event)
 {
-    Module.execute("the_march");
+    Module.execute(_em_main, "the_march");
 };
 document.getElementById("launch_game").addEventListener("click", start_game, false);
 
diff --git a/src/apc/.gitignore b/src/apc/.gitignore
new file mode 100644 (file)
index 0000000..c209b86
--- /dev/null
@@ -0,0 +1,3 @@
+*.tab.c
+*.tab.h
+filescanner.c
diff --git a/src/apc/fileparser.y b/src/apc/fileparser.y
new file mode 100644 (file)
index 0000000..0cf74b4
--- /dev/null
@@ -0,0 +1,176 @@
+/* Asset Package Compiler */
+%{
+  #include <stdio.h>
+  #include <string.h>
+  #include <dirent.h>
+  #include "sprite.h"
+  #include "symbol.h"
+  #include <png.h>
+
+
+  extern void yyerror();
+  extern int linit();
+  extern int yylex();
+
+
+  int handle_fname(char *);
+  int eval_png_file(char*);
+  int read_png_file(char*);
+
+%}
+%define parse.error verbose
+%union{
+  char* str;
+ }
+
+/* %token <str> EXT */
+/* %token <str> FW */
+/* %token <str> SPR */
+/* %token CC */
+
+/*  /\* Rules *\/ */
+/* %% */
+/* SS: SPR '/' MD '.' EXT */
+/* MD: CC '_' FW */
+/* | FW */
+/* | CC */
+
+
+%token <str> FDAT
+
+/* Rules */
+%%
+FLIST : FLIST FNAME {printf("reducing to flist\n");} /* Now output to asspack.o */  | FNAME
+FNAME : FDAT {printf("handle_fname(%s)\n", $1);handle_fname(yylval.str);printf("handled fname\n");}
+
+%%
+int
+main(int argc, char** argv)
+{
+  /* Parse cmd line arguments */
+
+  linit();
+  yyparse(); /* runs handle_fname for each filename */
+
+
+  return 0;
+}
+void
+yyerror (char const *s)
+{
+  fprintf(stderr, "%s\n", s);
+}
+
+
+
+/* Analyze file_name for information on sprite sheet,
+   store sprite sheet and label into sprite then
+   push sprite onto sprites[]*/
+int
+handle_fname(char* file_name)
+{
+  /* Parse the file_name for data */
+
+
+  /* Get the specs of the PNG file  */
+  if(eval_png_file(file_name))
+  {
+    printf("Failed in eval_png_file\n");
+    return 1;
+  }
+
+  /* Insert sprite sheet from PNG into sprite_sheets */
+  push_sprite();
+
+
+
+
+  /* Store dir/filename in label as well as any other data that fits */
+}
+int eval_png_file(char* file_name)
+{
+  /* Evaluate file_name for metadata */
+
+  /* Extracts header info from png_file */
+  read_png_file(file_name);
+
+  /* Create the symbol and put it in symbol_table[]*/
+
+  return 0;
+}
+
+
+
+/* variables for libPNG */
+int png_width,png_height;
+png_byte bit_depth;
+png_byte color_type;
+png_structp png_ptr;
+png_infop info_ptr;
+
+int
+read_png_file( char* file_name)
+{
+  /* Setup PNG file */
+  char header[8];
+  int is_png = 0;
+  char* dir = "pngfiles";
+
+  /* TODO: Change this fopen to a real var! */
+  FILE *fp = fopen("pngfiles/env_street_road03_0.png","rb");
+
+  if(!fp)
+    { printf("Could not read file %s in handle_fname()", file_name);
+      return 1;
+    }
+
+  fread(header, 1, 8, fp);
+
+  is_png = !png_sig_cmp(header, 0, 8);
+  if(!is_png)
+    return 1;
+
+  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+
+  if (!png_ptr)
+    { printf("Failed to allocate png_ptr\n");
+      return 1;
+    }
+  info_ptr = png_create_info_struct(png_ptr);
+
+  if (!info_ptr)
+    { printf("Failed to create info_ptr\n");
+      png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
+      return 1;
+    }
+
+  png_infop end_info = png_create_info_struct(png_ptr);
+  if (!end_info)
+    { printf("Failed to create end_info \n");
+      png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
+      return 1;
+    }
+
+  if(setjmp(png_jmpbuf(png_ptr)))
+    { printf("Failed in init_io\n");
+      png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
+      fclose(fp);
+      return 1;
+    }
+    png_init_io(png_ptr, fp);
+    png_set_sig_bytes(png_ptr, 8);
+
+    png_read_info(png_ptr, info_ptr);
+
+    png_width = png_get_image_width(png_ptr, info_ptr);
+    png_height = png_get_image_height(png_ptr, info_ptr);
+    color_type = png_get_color_type(png_ptr, info_ptr);
+    bit_depth = png_get_bit_depth(png_ptr, info_ptr);
+
+    png_read_update_info(png_ptr, info_ptr);
+
+    printf("successfully read png file %s\n", file_name);
+
+    return 0;
+
+}
diff --git a/src/apc/fileparser_old.y b/src/apc/fileparser_old.y
new file mode 100644 (file)
index 0000000..f7ca050
--- /dev/null
@@ -0,0 +1,223 @@
+/* Asset Package Compiler */
+%{
+  #include <stdio.h>
+  #include <string.h>
+  #include <dirent.h>
+  #include "sprite.h"
+  #include "symbol.h"
+  #include <png.h>
+
+
+  extern void yyerror();
+  extern int rinit();
+  extern int yylex();
+  #define DIR_SIZE 128/* size when all files have been concatenated together */
+
+  char dir_files[DIR_SIZE];
+  FILE* yyin;
+
+  int populate_yyin(FILE*);
+  int handle_vdat(char *);
+  int eval_png_file(char*);
+  int read_png_file(char*);
+
+%}
+%define parse.error verbose
+%union{
+  char* str;
+ }
+
+/* %token <str> EXT */
+/* %token <str> FW */
+/* %token <str> SPR */
+/* %token CC */
+
+/*  /\* Rules *\/ */
+/* %% */
+/* SS: SPR '/' MD '.' EXT */
+/* MD: CC '_' FW */
+/* | FW */
+/* | CC */
+
+
+%token <str> VDAT
+%token ODAT
+%token ADAT
+
+/* Rules */
+%%
+FLIST : FNAME /* Now output to VDAT.o */
+FNAME : VDAT {printf("handle_vdat(%s)\n", $1);handle_vdat(yylval.str);printf("handled vdat\n");}
+      | ADAT
+      | ODAT
+
+%%
+int
+main(int argc, char** argv)
+{
+  /* Take arguments */
+
+  /* Concatenate all files into buf, they are null-terminated
+     by default */
+  FILE *fp;
+
+  fp = fopen("all_files", "w+");
+
+  if(!populate_yyin(fp))
+    printf("error processing the directory");
+
+  yyin = fp;
+  rinit();
+  yyparse(); /* runs handle_vdat after token is scanned */
+
+
+
+  return 0;
+}
+void
+yyerror (char const *s)
+{
+  fprintf(stderr, "%s\n", s);
+}
+
+/* TODO: READ INTO SINGLE BUFFER, NOT A FILE */
+int
+populate_yyin(FILE* fp)
+{
+  int files_pos = 0;
+  int file_len = 0;
+  struct dirent* ep;
+
+  DIR* dp = opendir("pngfiles");
+  /* Concatenate all file names, seperated by '\0'
+     into files[] */
+  if(dp != NULL)
+    {
+      while(ep = readdir(dp))
+        { file_len = strlen(ep->d_name);
+          fwrite(ep->d_name, 1, file_len, fp);
+          printf("writing %s to file\n", ep->d_name);
+        }
+      fseek(fp, SEEK_SET, 0);
+      return 0;
+    }
+  else
+    {return 1;
+    }
+  closedir(dp);
+}
+
+
+
+
+/* Analyze file_name for information on sprite sheet,
+   store sprite sheet and label into sprite then
+   push sprite onto sprites[]*/
+int
+handle_vdat(char* file_name)
+{
+  /* Parse the file_name for data */
+
+
+  /* Get the specs of the PNG file  */
+  if(!eval_png_file(file_name))
+  {
+    printf("Failed in read_png_file\n");
+    return 1;
+  }
+
+  /* Insert sprite sheet from PNG into sprite_sheets */
+  push_sprite();
+
+
+
+
+  /* Store dir/filename in label as well as any other data that fits */
+}
+int eval_png_file(char* file_name)
+{
+  /* Evaluate file_name for metadata */
+
+  /* Extracts header info from png_file */
+  read_png_file(file_name);
+
+  
+  /* Create the symbol and put it in symbol_table[]*/
+
+  return 0;
+}
+
+
+
+/* variables for libPNG */
+int png_width,png_height;
+png_byte bit_depth;
+png_byte color_type;
+png_structp png_ptr;
+png_infop info_ptr;
+
+int
+read_png_file( char* file_name)
+{
+  /* Setup PNG file */
+  char header[8];
+  int is_png = 0;
+  char* dir = "pngfiles";
+
+  /* TODO: Change this fopen to a real var! */
+  FILE *fp = fopen("pngfiles/env_street_road03_0.png","rb");
+
+  if(!fp)
+    { printf("Could not read file %s in handle_vdat()", file_name);
+      return 1;
+    }
+
+  fread(header, 1, 8, fp);
+
+  is_png = !png_sig_cmp(header, 0, 8);
+  if(!is_png)
+    return 1;
+
+  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+
+  if (!png_ptr)
+    { printf("Failed to allocate png_ptr\n");
+      return 1;
+    }
+  info_ptr = png_create_info_struct(png_ptr);
+
+  if (!info_ptr)
+    { printf("Failed to create info_ptr\n");
+      png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
+      return 1;
+    }
+
+  png_infop end_info = png_create_info_struct(png_ptr);
+  if (!end_info)
+    { printf("Failed to create end_info \n");
+      png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
+      return 1;
+    }
+
+  if(setjmp(png_jmpbuf(png_ptr)))
+    { printf("Failed in init_io\n");
+      png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL);
+      fclose(fp);
+      return 1;
+    }
+    png_init_io(png_ptr, fp);
+    png_set_sig_bytes(png_ptr, 8);
+
+    png_read_info(png_ptr, info_ptr);
+
+    png_width = png_get_image_width(png_ptr, info_ptr);
+    png_height = png_get_image_height(png_ptr, info_ptr);
+    color_type = png_get_color_type(png_ptr, info_ptr);
+    bit_depth = png_get_bit_depth(png_ptr, info_ptr);
+
+    png_read_update_info(png_ptr, info_ptr);
+
+
+    return 0;
+
+}
diff --git a/src/apc/filescanner.rl b/src/apc/filescanner.rl
new file mode 100644 (file)
index 0000000..c3a1baa
--- /dev/null
@@ -0,0 +1,148 @@
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+#include <errno.h>
+#include <unistd.h>
+#include "fileparser.tab.h"
+
+int linit(void);
+int tok_dir(DIR*, char *);
+
+#define MAX_TOK_LEN 256
+#define MAX_DIR_DEP 8
+#define MAX_PATH_LEN 64
+static DIR* dp_stack[MAX_DIR_DEP];
+static DIR** dsp;
+static char path[MAX_PATH_LEN];
+
+/* Setup stack and first directory to read from */
+int
+linit()
+{
+  DIR *dp;
+  char cwd[MAX_TOK_LEN];
+
+  dsp = dp_stack;
+
+  getcwd(cwd, MAX_TOK_LEN);
+
+  printf("|------cwd is %s------|\n", cwd);
+  if(!(dp = opendir(cwd)))
+    printf("opendir(cwd) failed in linit()\n");
+
+  *dsp = dp;
+  /* Dont use ADD_PATH because it concats a "/", which is already
+     present with cwd */
+  strcat(path, cwd);
+  printf("dp_stack is %x, dsp is %x size is %d\n",*dp_stack, *dsp, sizeof dp);
+
+  return 0;
+}
+
+int
+yylex()
+{
+  int tok_t = 0;
+  char buf[MAX_TOK_LEN];
+  char* file_name;
+
+  /* Each call to tok_dir will return a file_name to be
+     tokenized. tok_dir calls readdir(), which saves the
+     position in the directory after returning a file_name
+     for the next call. */
+  printf("|------in yylex(), calling tok_dir------|\n");
+  if((tok_t = tok_dir(*dsp, buf)) == -1)
+    printf("tok_dir returned -1, something is broken\n");
+  yylval.str = strdup(buf);
+
+  printf("|------in yylex(), returning tok_t = %d | err = %s------|\n", tok_t, strerror(errno));
+
+  return tok_t;
+}
+
+#define DSP_PUSH(_val) *++dsp = (_val)
+
+int
+tok_dir(DIR* dp, char buf[])
+{
+  struct dirent* de; /* directory entry */
+  static DIR *tmp_dp;
+  char *tmp_path;
+  int path_len;
+
+
+tok_start:
+  if((*dsp == NULL) || (de = readdir(*dsp)) == NULL)
+    {
+      if(errno)
+        {  printf("Error:%s in tok_dir\n", strerror(errno));
+          return errno;
+        }
+      if( (dsp - dp_stack) >= 0)
+        {
+          printf("Current directory is null, pop one off ");
+#define DSP_POP() *dsp--
+
+          dp = DSP_POP();
+
+          /* Remove directory that was popped from path */
+#define SUB_PATH() tmp_path = strrchr(path, '/'); \
+          path_len = strlen(tmp_path);            \
+          memset(tmp_path, 0, path_len)
+
+          SUB_PATH();
+
+          goto tok_start;
+        }
+      return 0; /* Done */
+    }
+
+  else if(de->d_type == DT_REG)
+    { printf("|------dir_ent is a file, "\
+             "setting yylval to %s------|\n", de->d_name);
+      memmove(buf, de->d_name, MAX_TOK_LEN);
+      return FDAT;
+    }
+  else if (de->d_type == DT_DIR )
+    { if ((dsp - dp_stack) >= MAX_DIR_DEP) /* We've opened to many directories */
+        {
+          printf("Too many directories!\n");
+          return 1;
+        }
+      if(strcmp(de->d_name,".") == 0 || strcmp(de->d_name,"..") == 0)
+        {
+          printf("directory is %s \n", de->d_name);
+          goto tok_start;
+        }
+
+      printf("|------ dir_ent is directory %s, "\
+             "cwd = %s, path = %s ------|\n", de->d_name, get_current_dir_name(), path);
+
+
+      /* Add directory name to path */
+#define ADD_PATH(_name) strcat(path, "/");      \
+                        strcat(path, _name)
+
+      ADD_PATH(de->d_name);
+
+      tmp_dp = opendir(path);
+      if(tmp_dp == 0)
+        { printf("opening the directory failed,"\
+                 "errno = %s\n", strerror(errno));
+          return -1;
+        }
+
+      DSP_PUSH(tmp_dp);
+
+      goto tok_start;
+
+    }
+  else
+    {
+      printf("A file that is not a diretory or a regular file is unable to be tokenized: %s\n", de->d_name);
+      return -1;
+    }
+
+
+
+}
diff --git a/src/apc/filescannerparser_old.rl b/src/apc/filescannerparser_old.rl
new file mode 100644 (file)
index 0000000..ecb2e80
--- /dev/null
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <math.h>
+#include <string.h>
+#include <dirent.h>
+#include "fileparser.tab.h"
+extern FILE* yyin;
+
+int rinit(void);
+
+#define MAX_TOK_LEN 32
+static char buf[MAX_TOK_LEN];
+static char *p, *pe; /* scanner points to p, pe points to end of buf */
+
+%%{
+  machine scanner;
+
+  main := |*
+
+    'env_street_road03_0.png' => {ret=VDAT;memmove(rbuf, ts, (te-ts)); yylval.str=strdup(rbuf); printf("Lexer matched with %s\n",yylval.str);fbreak; };
+    '.' => {printf("matched with .\n");fbreak;};
+    '..' => {printf("matched with ..\n");fbreak;};
+
+   *|;
+
+}%%
+
+%%write data;
+
+int
+rinit()
+{ p = buf;
+  pe = p + fread(buf, 1, MAX_TOK_LEN, yyin);
+  printf("buf in rinit is %s\n", buf);
+  return 0;
+}
+
+int
+yylex()
+{ int ret;
+  static char *eof;
+  char rbuf[MAX_TOK_LEN];
+  int cs, act;
+  char *ts, *te;
+
+  /*init cs, ts, te, act */
+  %%write init;
+  ret = 0;
+
+  %%write exec ;
+  if (te == 0)
+    {
+      if(eof == pe ){
+        return 0;
+      }
+      printf("This is a lexical error, tokens can not be larger than max token length\n buf is = %s | ts = %s | te = %s\n", buf,ts,te);
+      return 0;
+    }
+
+
+  /* We matched a token, remove it from buf and set
+     buf to have rest of unmatched buf (buf - matchedtoken).*/
+  if (!feof(yyin))
+    { int r = pe-te; /* remainder length */
+      memmove(buf, te, r); /* also the max more buf can hold */
+      p = buf;
+      pe = p + r + fread(buf+r,1,MAX_TOK_LEN -r,yyin);
+    }
+  if((p+1) == pe && feof(yyin))
+    eof = pe;
+
+  return ret;
+}
+
+/* 'sprites' => {};
+   /0x[xdigit]{8}/ => {};
+   '_' => {};
+   /[digit]{2}/ => {}; /* CC */
+
diff --git a/src/apc/ragson b/src/apc/ragson
new file mode 100755 (executable)
index 0000000..d47463c
Binary files /dev/null and b/src/apc/ragson differ
diff --git a/src/apc/ragson.sh b/src/apc/ragson.sh
new file mode 100755 (executable)
index 0000000..3da486d
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+bison -t -d fileparser.y
+ragel filescanner.rl
+gcc sprite.c fileparser.tab.c filescanner.c -o ragson -lm -lpng
diff --git a/src/apc/sprite.c b/src/apc/sprite.c
new file mode 100644 (file)
index 0000000..82e1911
--- /dev/null
@@ -0,0 +1,5 @@
+int
+push_sprite(void)
+{
+  return 0;
+}
diff --git a/src/apc/sprite.h b/src/apc/sprite.h
new file mode 100644 (file)
index 0000000..2d55988
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef _SPRITEH_
+#define _SPRITEH_
+
+/* Sprite Sheet Header */
+/* Sprite Sheets can have 1-8 frames for each direction they are   facing. */
+struct sprite_sheet
+{
+  int num_frames; /* Defaults to 0 */
+  int frame_width; /*Found frome file name */
+  int sprite_start; /* points to where first frame points */
+};
+
+
+extern struct sprite_sheet sprite_sheets[16]; /* stack of sprites */
+extern struct sprite_sheet* ssp; /* sprite sheets pointer */
+
+
+int push_sprite();
+
+#endif
diff --git a/src/apc/symbol.h b/src/apc/symbol.h
new file mode 100644 (file)
index 0000000..8550a83
--- /dev/null
@@ -0,0 +1,14 @@
+/* Header file for Symbols */
+#ifndef _SYMBOLH_
+#define _SYMBOLH_
+#include "sprite.h"
+
+struct symbol
+{
+  struct sprite_sheet ss;
+  int ss_id;
+  char label[255];
+};
+
+struct symbol symbol_table[16];
+#endif
diff --git a/src/core/auth.c b/src/core/auth.c
new file mode 100644 (file)
index 0000000..c556abc
--- /dev/null
@@ -0,0 +1,111 @@
+/*!@file
+  \brief  Password Encyption and Storing
+  \details hashes password using SHA256 and saves result in a file on VFS using
+           hashing algorithm provided by wolfcrypt(wc), a subsystem of wolfSSL
+  \author  JEL
+  \date    2016
+  -----------------------------------------------------------------------------*/
+#include <stddef.h>
+#include <stdio.h>
+#include <wolfssl/wolfcrypt/sha256.h>
+#include <emscripten.h>
+
+word32 str_len(const char*);
+int auth_encrypt(int argc, char** argv);
+void store_password(byte*);
+
+/* Should come with wolfcrypt type.h */
+/* typedef unsigned int word32; */
+
+word32
+str_len (const char* str)
+{
+  const char* strl;
+
+  for (strl = str; *strl != '\0'; ++strl)
+  {
+  /* nothing to be done */;
+  }
+  return strl - str;
+}
+
+
+/* Takes a string and hashes it, returning
+   the encrypted string as a result */
+
+int
+auth_encrypt(int argc, char** argv)
+{
+  /* Determine length of password */
+  word32 len = 0;
+  char* password;
+  byte hash[32]; /* 32bytes * 8 = 256, the # bits of hash */
+  const unsigned char c_password[32]; /* Passwords are between 8-32 chars */
+  Sha256 sha256[1]; /* Init a Sha256 type for wc */
+
+
+  password = argv[0];
+  len = str_len(password);
+
+  /* Converting password to unsigned const char* for wc_functions */
+  memcpy( (void*) c_password, (void*) password, len+1);
+
+  if (wc_InitSha256(sha256) != 0 )
+   {
+     printf("wc_InitSha256 failed\n");
+   }
+   else
+   {
+     wc_Sha256Update(sha256, c_password, len);
+     wc_Sha256Final(sha256, hash);
+   }
+
+  printf("password is: %s\n", c_password);
+  printf("password length is: %d\n", len);
+  for(len = 0; len < 32; len++){
+    printf("hash is %x\n",hash[len]);
+  }
+
+  store_password(hash);
+  free((void*) c_password);
+
+
+  return 0;
+}
+
+void
+store_password(byte* hash)
+{
+  byte b;
+  /* Save hash to a file on emscripten VFS */
+  FILE *fp;
+  int i;
+  byte buff[32] = {0};
+
+  printf("=====Storing Password in auth/password========\n");
+  /* for(i = 0; i < 32; i++){ */
+  /*   printf("hash is %x\n",hash[i]); */
+  /* } */
+
+  fp = fopen("/auth/password", "w+");
+  if(!fp)
+  {
+    printf("cannot open file\n");
+  }
+  else
+  {
+    fwrite(hash, 1, 32, fp);
+  }
+
+  printf("========Stored Password=========\n");
+
+  fseek(fp, 0, SEEK_SET);
+
+  for(i = 0; i < 32; i++)
+  {
+    fread(buff, 1, 32, fp) ;
+    printf("buff is %x\n",buff[i]);
+  }
+
+  fclose(fp);
+}