From: jordan@hack_attack Date: Fri, 5 Aug 2016 19:38:56 +0000 (-0700) Subject: apc initial implementation X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fwebcc.git;a=commitdiff_plain;h=b363cb566b9d21b6c607d18579232f9c79b15f4b apc initial implementation --- diff --git a/html/index.html b/html/index.html index 0834270..05d0d4a 100644 --- a/html/index.html +++ b/html/index.html @@ -5,6 +5,9 @@ + + + Usage: the_march [-opts]
@@ -16,5 +19,6 @@ + diff --git a/html/js/auth.js b/html/js/auth.js index d399d6f..3eb052a 100644 --- a/html/js/auth.js +++ b/html/js/auth.js @@ -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); diff --git a/html/js/the_march.config.js b/html/js/the_march.config.js index 7751827..4d4afd7 100644 --- a/html/js/the_march.config.js +++ b/html/js/the_march.config.js @@ -2,39 +2,61 @@ var Module = { 'noExitRuntime': true, 'arguments': [], + 'argc': 0, + 'argv_ptr': 0, - print: function(text) { for(var i=0; i");}, - printErr: function(text) { console.log("ERR: " + text); }, + //print: function(text) { for(var i=0; i + #include + #include + #include "sprite.h" + #include "symbol.h" + #include + + + 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 EXT */ +/* %token FW */ +/* %token SPR */ +/* %token CC */ + +/* /\* Rules *\/ */ +/* %% */ +/* SS: SPR '/' MD '.' EXT */ +/* MD: CC '_' FW */ +/* | FW */ +/* | CC */ + + +%token 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 index 0000000..f7ca050 --- /dev/null +++ b/src/apc/fileparser_old.y @@ -0,0 +1,223 @@ +/* Asset Package Compiler */ +%{ + #include + #include + #include + #include "sprite.h" + #include "symbol.h" + #include + + + 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 EXT */ +/* %token FW */ +/* %token SPR */ +/* %token CC */ + +/* /\* Rules *\/ */ +/* %% */ +/* SS: SPR '/' MD '.' EXT */ +/* MD: CC '_' FW */ +/* | FW */ +/* | CC */ + + +%token 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 index 0000000..c3a1baa --- /dev/null +++ b/src/apc/filescanner.rl @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +#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 index 0000000..ecb2e80 --- /dev/null +++ b/src/apc/filescannerparser_old.rl @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#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 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 index 0000000..3da486d --- /dev/null +++ b/src/apc/ragson.sh @@ -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 index 0000000..82e1911 --- /dev/null +++ b/src/apc/sprite.c @@ -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 index 0000000..2d55988 --- /dev/null +++ b/src/apc/sprite.h @@ -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 index 0000000..8550a83 --- /dev/null +++ b/src/apc/symbol.h @@ -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 index 0000000..c556abc --- /dev/null +++ b/src/core/auth.c @@ -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 +#include +#include +#include + +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); +}