diff --git a/Makefile b/Makefile index 40a1697..713ed3c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ # SlideScript makefile -# (C) Copyright 2014-2021 Chris Dorman, some rights reserved (CC-BY-SA 3.0) +# (C) Copyright 2014-2021 Chris Dorman, some rights reserved (GPLv2) +# Some changes and tweaks from Menchers VERSION = \"0.2.1\" EXTRA ?= dev @@ -13,7 +14,7 @@ CPPFLAGS += -DVERSION=$(VERSION) -DVERSION_EXTRA=$(VERSION_EXTRA) -D_FORTIFY LDFLAGS += -Wl,-O1,--sort-common,--hash-style=gnu,-z,relro BIN ?= slidescript -OBJECTS = src/main.o src/functions.o src/util.o src/vars.o src/enc.o +OBJECTS = src/main.o src/functions.o src/util.o src/vars.o src/enc.o src/md5.o all: main diff --git a/README.md b/README.md index 2668740..3de62d0 100644 --- a/README.md +++ b/README.md @@ -80,4 +80,4 @@ V0.1.1 Contributions: Robert (OldCoder) Kirary -> shebang support and string manipulations -(C) Copyright 2014-2021 Chris Dorman, some rights reserved (CC-BY-SA 3.0) +(C) Copyright 2014-2021 Chris Dorman, some rights reserved (GPLv2) diff --git a/src/deps.h b/src/deps.h index 35a9c30..6bda6a9 100644 --- a/src/deps.h +++ b/src/deps.h @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ @@ -9,6 +9,7 @@ #include #include #include +#include #define MAX_VAR_NAME_LEN 128 #define MAX_VAR_NAME_BUFSIZE (MAX_VAR_NAME_LEN + 1) diff --git a/src/enc.c b/src/enc.c index 62afa58..248143a 100644 --- a/src/enc.c +++ b/src/enc.c @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ diff --git a/src/enc.h b/src/enc.h index 392d8b0..e1c3c6c 100644 --- a/src/enc.h +++ b/src/enc.h @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ diff --git a/src/functions.c b/src/functions.c index 9175b8c..abcc94b 100644 --- a/src/functions.c +++ b/src/functions.c @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ @@ -10,6 +10,7 @@ #include "util.h" #include "vars.h" #include "enc.h" +#include "md5.h" int process_line(char *line) { @@ -100,6 +101,20 @@ int process_line(char *line) } } + /* builtin md5 function */ + else if(strncmp("md5",tok_srch,3) == 0) + { + char *file_line, *file_md5_val; + tok_srch = strtok(NULL, "\""); + if(strcmp(tok_srch, "\n") == 0 || + strcmp(tok_srch, " \n") == 0 || tok_srch == NULL) + syn_error("ss:error:exec syntax error, requires a file argument in quotes"); + + file_line = parse_vars(tok_srch); + file_md5_val = md5_file(file_line); + printf("ss:md5: %s, %s\n", file_md5_val, file_line); + } + /* system execute function */ else if(strncmp("exec",tok_srch,4) == 0) { diff --git a/src/functions.h b/src/functions.h index 556d887..e3520c7 100644 --- a/src/functions.h +++ b/src/functions.h @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ diff --git a/src/main.c b/src/main.c index eddb1b8..2e9f0f8 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ diff --git a/src/md5.c b/src/md5.c new file mode 100644 index 0000000..4e35011 --- /dev/null +++ b/src/md5.c @@ -0,0 +1,283 @@ +// BUSYBOX MD5 code, repurposed for the function in SlideScript +// Licensing and contributions to the BusyBox development team, changes +// and cleanup done by Bob (OldCoder) Kiraly, 2021 + +/* + SlideScript - minimalistic top-down scripting language. + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) + + View README file supplied with this software for more details +*/ + +#include "deps.h" +#include "md5.h" + +#define ZERO 0 +#define ONE 1 +#define TWO 2 + +#define FALSE 0 +#define TRUE 1 + +static inline uint32_md5 rotl32 (uint32_md5 x, unsigned n) +{ + return (x << n) | (x >> (32 - n)); +} + +static inline uint32_md5 rotr32 (uint32_md5 x, unsigned n) +{ + return (x >> n) | (x << (32 - n)); +} + +static inline uint64_md5 rotr64 (uint64_md5 x, unsigned n) +{ + return (x >> n) | (x << (64 - n)); +} + +static inline uint64_md5 rotl64(uint64_md5 x, unsigned n) +{ + return (x << n) | (x >> (64 - n)); +} + +static void common64_hash + (md5_ctx_t *ctx, const void *buffer, size_t len) +{ + unsigned bufpos = ctx->total64 & 63; + ctx->total64 += len; + + while (TRUE) + { + unsigned remaining = 64 - bufpos; + if (remaining > len) remaining = len; + memcpy (ctx->wbuffer + bufpos, buffer, remaining); + len -= remaining; + buffer = (const char *) buffer + remaining; + bufpos += remaining; + bufpos -= 64; + if (bufpos != ZERO) break; + ctx->process_block (ctx); + } +} + +static void common64_end (md5_ctx_t *ctx, int swap_needed) +{ + unsigned bufpos = ctx->total64 & 63; + ctx->wbuffer [bufpos++] = 0x80; + + while (1) + { + unsigned remaining = 64 - bufpos; + memset (ctx->wbuffer + bufpos, ZERO, remaining); + + if (remaining >= 8) + { + uint64_md5 t = ctx->total64 << 3; + if (swap_needed) t = __bswap_64 (t); + *(uint64_md5 *) (&ctx->wbuffer [64 - 8]) = t; + } + + ctx->process_block (ctx); + if (remaining >= 8) break; + bufpos = ZERO; + } +} + +static void md5_process_block64 (md5_ctx_t *ctx) +{ + uint32_md5 *words = (void*) ctx->wbuffer; + + uint32_md5 A = ctx->hash [0]; + uint32_md5 B = ctx->hash [1]; + uint32_md5 C = ctx->hash [2]; + uint32_md5 D = ctx->hash [3]; + + do { A += (D ^ (B & (C ^ D))) + (*words) + 0xd76aa478; words++; A = rotl32 (A, 7); A += B; } while (0); + do { D += (C ^ (A & (B ^ C))) + (*words) + 0xe8c7b756; words++; D = rotl32 (D, 12); D += A; } while (0); + do { C += (B ^ (D & (A ^ B))) + (*words) + 0x242070db; words++; C = rotl32 (C, 17); C += D; } while (0); + do { B += (A ^ (C & (D ^ A))) + (*words) + 0xc1bdceee; words++; B = rotl32 (B, 22); B += C; } while (0); + do { A += (D ^ (B & (C ^ D))) + (*words) + 0xf57c0faf; words++; A = rotl32 (A, 7); A += B; } while (0); + do { D += (C ^ (A & (B ^ C))) + (*words) + 0x4787c62a; words++; D = rotl32 (D, 12); D += A; } while (0); + do { C += (B ^ (D & (A ^ B))) + (*words) + 0xa8304613; words++; C = rotl32 (C, 17); C += D; } while (0); + do { B += (A ^ (C & (D ^ A))) + (*words) + 0xfd469501; words++; B = rotl32 (B, 22); B += C; } while (0); + do { A += (D ^ (B & (C ^ D))) + (*words) + 0x698098d8; words++; A = rotl32 (A, 7); A += B; } while (0); + do { D += (C ^ (A & (B ^ C))) + (*words) + 0x8b44f7af; words++; D = rotl32 (D, 12); D += A; } while (0); + do { C += (B ^ (D & (A ^ B))) + (*words) + 0xffff5bb1; words++; C = rotl32 (C, 17); C += D; } while (0); + do { B += (A ^ (C & (D ^ A))) + (*words) + 0x895cd7be; words++; B = rotl32 (B, 22); B += C; } while (0); + do { A += (D ^ (B & (C ^ D))) + (*words) + 0x6b901122; words++; A = rotl32 (A, 7); A += B; } while (0); + do { D += (C ^ (A & (B ^ C))) + (*words) + 0xfd987193; words++; D = rotl32 (D, 12); D += A; } while (0); + do { C += (B ^ (D & (A ^ B))) + (*words) + 0xa679438e; words++; C = rotl32 (C, 17); C += D; } while (0); + do { B += (A ^ (C & (D ^ A))) + (*words) + 0x49b40821; words++; B = rotl32 (B, 22); B += C; } while (0); + + words -= 16; + + do { A += (C ^ (D & (B ^ C))) + words [ 1] + 0xf61e2562; A = rotl32 (A, 5); A += B; } while (0); + do { D += (B ^ (C & (A ^ B))) + words [ 6] + 0xc040b340; D = rotl32 (D, 9); D += A; } while (0); + do { C += (A ^ (B & (D ^ A))) + words [11] + 0x265e5a51; C = rotl32 (C, 14); C += D; } while (0); + do { B += (D ^ (A & (C ^ D))) + words [ 0] + 0xe9b6c7aa; B = rotl32 (B, 20); B += C; } while (0); + do { A += (C ^ (D & (B ^ C))) + words [ 5] + 0xd62f105d; A = rotl32 (A, 5); A += B; } while (0); + do { D += (B ^ (C & (A ^ B))) + words [10] + 0x02441453; D = rotl32 (D, 9); D += A; } while (0); + do { C += (A ^ (B & (D ^ A))) + words [15] + 0xd8a1e681; C = rotl32 (C, 14); C += D; } while (0); + do { B += (D ^ (A & (C ^ D))) + words [ 4] + 0xe7d3fbc8; B = rotl32 (B, 20); B += C; } while (0); + do { A += (C ^ (D & (B ^ C))) + words [ 9] + 0x21e1cde6; A = rotl32 (A, 5); A += B; } while (0); + do { D += (B ^ (C & (A ^ B))) + words [14] + 0xc33707d6; D = rotl32 (D, 9); D += A; } while (0); + do { C += (A ^ (B & (D ^ A))) + words [ 3] + 0xf4d50d87; C = rotl32 (C, 14); C += D; } while (0); + do { B += (D ^ (A & (C ^ D))) + words [ 8] + 0x455a14ed; B = rotl32 (B, 20); B += C; } while (0); + do { A += (C ^ (D & (B ^ C))) + words [13] + 0xa9e3e905; A = rotl32 (A, 5); A += B; } while (0); + do { D += (B ^ (C & (A ^ B))) + words [ 2] + 0xfcefa3f8; D = rotl32 (D, 9); D += A; } while (0); + do { C += (A ^ (B & (D ^ A))) + words [ 7] + 0x676f02d9; C = rotl32 (C, 14); C += D; } while (0); + do { B += (D ^ (A & (C ^ D))) + words [12] + 0x8d2a4c8a; B = rotl32 (B, 20); B += C; } while (0); + + do { A += (B ^ C ^ D) + words [ 5] + 0xfffa3942; A = rotl32 (A, 4); A += B; } while (0); + do { D += (A ^ B ^ C) + words [ 8] + 0x8771f681; D = rotl32 (D, 11); D += A; } while (0); + do { C += (D ^ A ^ B) + words [11] + 0x6d9d6122; C = rotl32 (C, 16); C += D; } while (0); + do { B += (C ^ D ^ A) + words [14] + 0xfde5380c; B = rotl32 (B, 23); B += C; } while (0); + do { A += (B ^ C ^ D) + words [ 1] + 0xa4beea44; A = rotl32 (A, 4); A += B; } while (0); + do { D += (A ^ B ^ C) + words [ 4] + 0x4bdecfa9; D = rotl32 (D, 11); D += A; } while (0); + do { C += (D ^ A ^ B) + words [ 7] + 0xf6bb4b60; C = rotl32 (C, 16); C += D; } while (0); + do { B += (C ^ D ^ A) + words [10] + 0xbebfbc70; B = rotl32 (B, 23); B += C; } while (0); + do { A += (B ^ C ^ D) + words [13] + 0x289b7ec6; A = rotl32 (A, 4); A += B; } while (0); + do { D += (A ^ B ^ C) + words [ 0] + 0xeaa127fa; D = rotl32 (D, 11); D += A; } while (0); + do { C += (D ^ A ^ B) + words [ 3] + 0xd4ef3085; C = rotl32 (C, 16); C += D; } while (0); + do { B += (C ^ D ^ A) + words [ 6] + 0x04881d05; B = rotl32 (B, 23); B += C; } while (0); + do { A += (B ^ C ^ D) + words [ 9] + 0xd9d4d039; A = rotl32 (A, 4); A += B; } while (0); + do { D += (A ^ B ^ C) + words [12] + 0xe6db99e5; D = rotl32 (D, 11); D += A; } while (0); + do { C += (D ^ A ^ B) + words [15] + 0x1fa27cf8; C = rotl32 (C, 16); C += D; } while (0); + do { B += (C ^ D ^ A) + words [ 2] + 0xc4ac5665; B = rotl32 (B, 23); B += C; } while (0); + + do { A += (C ^ (B | ~D)) + words [ 0] + 0xf4292244; A = rotl32 (A, 6); A += B; } while (0); + do { D += (B ^ (A | ~C)) + words [ 7] + 0x432aff97; D = rotl32 (D, 10); D += A; } while (0); + do { C += (A ^ (D | ~B)) + words [14] + 0xab9423a7; C = rotl32 (C, 15); C += D; } while (0); + do { B += (D ^ (C | ~A)) + words [ 5] + 0xfc93a039; B = rotl32 (B, 21); B += C; } while (0); + do { A += (C ^ (B | ~D)) + words [12] + 0x655b59c3; A = rotl32 (A, 6); A += B; } while (0); + do { D += (B ^ (A | ~C)) + words [ 3] + 0x8f0ccc92; D = rotl32 (D, 10); D += A; } while (0); + do { C += (A ^ (D | ~B)) + words [10] + 0xffeff47d; C = rotl32 (C, 15); C += D; } while (0); + do { B += (D ^ (C | ~A)) + words [ 1] + 0x85845dd1; B = rotl32 (B, 21); B += C; } while (0); + do { A += (C ^ (B | ~D)) + words [ 8] + 0x6fa87e4f; A = rotl32 (A, 6); A += B; } while (0); + do { D += (B ^ (A | ~C)) + words [15] + 0xfe2ce6e0; D = rotl32 (D, 10); D += A; } while (0); + do { C += (A ^ (D | ~B)) + words [ 6] + 0xa3014314; C = rotl32 (C, 15); C += D; } while (0); + do { B += (D ^ (C | ~A)) + words [13] + 0x4e0811a1; B = rotl32 (B, 21); B += C; } while (0); + do { A += (C ^ (B | ~D)) + words [ 4] + 0xf7537e82; A = rotl32 (A, 6); A += B; } while (0); + do { D += (B ^ (A | ~C)) + words [11] + 0xbd3af235; D = rotl32 (D, 10); D += A; } while (0); + do { C += (A ^ (D | ~B)) + words [ 2] + 0x2ad7d2bb; C = rotl32 (C, 15); C += D; } while (0); + do { B += (D ^ (C | ~A)) + words [ 9] + 0xeb86d391; B = rotl32 (B, 21); B += C; } while (0); + + ctx->hash [0] += A; + ctx->hash [1] += B; + ctx->hash [2] += C; + ctx->hash [3] += D; +} + +void md5_begin (md5_ctx_t *ctx) +{ + if ((sizeof (uint8_md5 ) != 1) || + (sizeof (uint16_md5) != 2) || + (sizeof (uint32_md5) != 4) || + (sizeof (uint64_md5) != 8)) + { + fprintf (stderr, "%s\n", + "Internal error: Host arch not supported"); + exit (ONE); + } + + ctx->hash [0] = 0x67452301; + ctx->hash [1] = 0xefcdab89; + ctx->hash [2] = 0x98badcfe; + ctx->hash [3] = 0x10325476; + + ctx->total64 = ZERO; + ctx->process_block = md5_process_block64; +} + +void md5_hash (md5_ctx_t *ctx, const void *buffer, size_t len) +{ + common64_hash (ctx, buffer, len); +} + +unsigned int md5_end (md5_ctx_t *ctx, void *resbuf) +{ + common64_end (ctx, ZERO); + memcpy (resbuf, ctx->hash, sizeof (ctx->hash [ZERO]) * 4); + return sizeof (ctx->hash [ZERO]) * 4; +} + +char *md5_data (char *data, int len) +{ + md5_ctx_t ctx; // MD5 context + int ii; // Counter + + // MD5 digest - 16 raw bytes + unsigned char digest_raw [16]; + // MD5 digest - 32-char string plus a + // null terminator + static unsigned char digest_hex [33]; + + md5_begin (&ctx); + md5_hash (&ctx, data, len); + md5_end (&ctx, digest_raw); + + for (ii = ZERO; ii < 16; ii++) + { + sprintf ((char *)digest_hex + (ii * 2), "%02x", digest_raw [ii]); + } + + return (char *) digest_hex; +} + +#define MD5_FILE_BSIZE 8192 + +char *md5_file (char *ifname) +{ + md5_ctx_t ctx; // MD5 context + FILE *ifp; // Input-file pointer + int ii; // Counter + int nb; // Number of bytes read + + char data [MD5_FILE_BSIZE]; // Block buffer + // MD5 digest - 16 raw bytes + unsigned char digest_raw [16]; + // MD5 digest - 32-char string plus a + // null terminator + static unsigned char digest_hex [33]; + +//-------------------------------------------------------------------- + + md5_begin (&ctx); + + if ((ifp = fopen (ifname, "rb")) == NULL) return NULL; + + while ((nb = fread (data, ONE, MD5_FILE_BSIZE, ifp)) != ZERO) + { md5_hash (&ctx, data, nb); } + + fclose (ifp); + md5_end (&ctx, digest_raw); + + for (ii = ZERO; ii < 16; ii++) + { + sprintf ((char *) digest_hex + (ii * 2), "%02x", digest_raw [ii]); + } + + return (char *) digest_hex; +} + +char *md5_string (char *str) +{ + return md5_data (str, strlen (str)); +} + +#ifdef TESTPROG +void main (int argc, char **argv) +{ + char *ifname; // Input-file name + char *input; // Pointer to input data + int ii; // Counter + + if (argc != TWO) + { + puts ("Usage: busymd5 input-file-name"); + exit (ONE); + } + + ifname = argv [ONE]; + printf ("%s *%s\n", md5_file (ifname), ifname); +} +#endif // Endif TESTPROG diff --git a/src/md5.h b/src/md5.h new file mode 100644 index 0000000..1542b0c --- /dev/null +++ b/src/md5.h @@ -0,0 +1,32 @@ +// BUSYBOX MD5 code, repurposed for the function in SlideScript +// Licensing and contributions to the BusyBox development team, changes +// and cleanup done by Bob (OldCoder) Kiraly, 2021 + +/* + SlideScript - minimalistic top-down scripting language. + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) + + View README file supplied with this software for more details +*/ + +typedef unsigned char uint8_md5; +typedef unsigned short uint16_md5; +typedef unsigned int uint32_md5; +typedef unsigned long uint64_md5; + +typedef struct md5_ctx_md5 +{ + uint8_md5 wbuffer [64]; + void (*process_block) (struct md5_ctx_md5 *); + uint64_md5 total64; + uint32_md5 hash [8]; +} + md5_ctx_t; + +void md5_begin (md5_ctx_t *ctx); +void md5_hash (md5_ctx_t *ctx, const void *buffer, size_t len); +unsigned int md5_end (md5_ctx_t *ctx, void *resbuf); + +extern char *md5_data (char *buf, int len); +extern char *md5_file (char *ifname); +extern char *md5_string (char *string); diff --git a/src/util.c b/src/util.c index 32a4038..4a1ba9c 100644 --- a/src/util.c +++ b/src/util.c @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ diff --git a/src/util.h b/src/util.h index dd8be5a..43cc177 100644 --- a/src/util.h +++ b/src/util.h @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ diff --git a/src/vars.c b/src/vars.c index e59e9af..b227494 100644 --- a/src/vars.c +++ b/src/vars.c @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ diff --git a/src/vars.h b/src/vars.h index ede51a2..172f9d3 100644 --- a/src/vars.h +++ b/src/vars.h @@ -1,6 +1,6 @@ /* SlideScript - minimalistic top-down scripting language. - (C) Copyright 2014-2021 Chris Dorman - some rights reserved (CC-BY-SA 3.0) + (C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2) View README file supplied with this software for more details */ diff --git a/test.ss b/test.ss index debbc10..d5ae456 100755 --- a/test.ss +++ b/test.ss @@ -8,6 +8,7 @@ ss_filename=file.txt ss_exec_command=ls -al # Sets 'ss_decoded' to 'Encrypt This Data!' ss_decoded=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/][-_.,?! +ss_encoded2=defejihknknopqrqvutwzwz{|}DCHGFILILMNOPOTSRUXUXYZ[\[4`\0d//B$ # Sets 'ss_encoded' to 'Ghfs~su#Yilv#Gduf$' ss_encoded=Ghfs~su#Yilv#Gduf$ # Sets 'ss_file_content' to 'This will be written to file.txt!' @@ -24,8 +25,13 @@ sleep 1 # Writes the content of ss_file_content to ss_filename write "%ss_filename%" "%ss_file_content%" +print "Getting md5 of %ss_filename%" + +md5 "%ss_filename%" + print "Data written to %ss_filename%:" # Reads data and prints to screen from ss_filename + read "%ss_filename%" print "Deleting %ss_filename% after 1 second sleep..." @@ -52,3 +58,5 @@ enc "%ss_decoded%" print "Decoding '%ss_encoded%'" # Decodes 'ss_decode' data dec "%ss_encoded%" + +dec "%ss_encoded2%"