diff --git a/bml.cpp b/bml.cpp new file mode 100644 index 00000000..4361c4b4 --- /dev/null +++ b/bml.cpp @@ -0,0 +1,349 @@ +#include +#include +#include +#include + +#include "bml.h" + +static inline bml_node *bml_node_new(void) +{ + bml_node *node = new bml_node; + + node->data = NULL; + node->name = NULL; + node->depth = -1; +} + +static char *strndup_trim (char *str, int len) +{ + int start; + int end; + + for (start = 0; str[start] && start != len && isblank (str[start]); start++) {} + if (!str[start] || start >= len) + return strdup (""); + + for (end = len - 1; isblank (str[end]) || str[end] == '\n' || str[end] == '\r'; end--) {} + + return strndup (str + start, end - start + 1); +} + +static inline int bml_valid (char c) +{ + return (isalnum (c) || c == '-'); +} + +static inline int islf(char c) +{ + return (c == '\r' || c == '\n'); +} + +static inline unsigned int bml_read_depth (char *data) +{ + unsigned int depth; + for (depth = 0; isblank (data[depth]); depth++) {} + return depth; +} + +static unsigned int bml_parse_depth (bml_node *node, char **data) +{ + unsigned int depth = bml_read_depth (*data); + *data += depth; + node->depth = depth; +} + +static char *bml_parse_name (bml_node *node, char **data) +{ + int len; + + for (len = 0; bml_valid(*(*data + len)); len++) {}; + + node->name = strndup_trim (*data, len); + *data += len; +} + +static void bml_parse_data (bml_node *node, char **data) +{ + char *p = *data; + int len; + + if (p[0] == '=' && p[1] == '\"') + { + len = 2; + while (p[len] && !islf (p[len])) + len++; + if (p[len] != '\"') + return; + + node->data = strndup (p + 2, len - 2); + *data += len + 1; + } + else if (*p == '=') + { + len = 1; + while (p[len] && !islf (p[len]) && p[len] != '"' && p[len] != ' ') + len++; + if (p[len] == '\"') + return; + node->data = strndup_trim (p + 1, len - 1); + *data += len; + } + else if (*p == ':') + { + len = 1; + while (p[len] && !islf (p[len])) + len++; + node->data = strndup_trim (p + 1, len - 1); + *data += len; + } + + return; +} + +static void bml_skip_empty (char **data) +{ + char *p = *data; + + while (*p) + { + for (; *p && isblank (*p) ; p++) {} + + if (!islf(p[0]) && (p[0] != '/' && p[1] != '/')) + return; + + /* Skip comment data */ + while (*p && *p != '\r' && *p != '\n') + p++; + + /* If we found new-line, try to skip more */ + if (*p) + { + p++; + *data = p; + } + } +} + +static char *bml_read_line (char **data) +{ + char *line; + char *p; + + bml_skip_empty (data); + + line = *data; + + if (line == NULL || *line == '\0') + return NULL; + + p = strpbrk (line, "\r\n\0"); + + if (p == NULL) + return NULL; + + if (islf (*p)) + { + *p = '\0'; + p++; + } + + *data = p; + + return line; +} + +static void bml_parse_attr (bml_node *node, char **data) +{ + char *p = *data; + bml_node *n; + int len; + + while (*p && !islf (*p)) + { + if (*p != ' ') + return; + + while (isblank (*p)) + p++; + if (p[0] == '/' && p[1] == '/') + break; + + n = bml_node_new (); + len = 0; + while (bml_valid (p[len])) + len++; + if (len == 0) + return; + n->name = strndup_trim (p, len); + p += len; + bml_parse_data (n, &p); + node->attr.push_back (n); + } + + *data = p; +} + +static int contains_space (char *str) +{ + for (int i = 0; str[i]; i++) + { + if (isblank (str[i])) + return 1; + } + + return 0; +} + +static void bml_print_node (bml_node *node, int depth) +{ + if (!node) + return; + + for (int i = 0; i < depth * 2; i++) + { + printf (" "); + } + + if (node->name) + printf ("%s", node->name); + + if (node->data) + { + if (contains_space (node->data)) + printf (": \"%s\"", node->data); + else + printf (": %s", node->data); + } + + for (int i = 0; i < node->attr.size(); i++) + { + if (node->attr[i]->name) + { + printf (" %s", node->attr[i]->name); + if (node->attr[i]->data) + { + if (contains_space (node->attr[i]->data)) + printf ("=\"%s\"", node->attr[i]->data); + else + printf ("=%s", node->attr[i]->data); + } + } + } + + if (depth >= 0) + printf ("\n"); + + for (int i = 0; i < node->child.size(); i++) + { + bml_print_node (node->child[i], depth + 1); + } + + if (depth == 0) + printf ("\n"); +} + +void bml_print_node (bml_node *node) +{ + bml_print_node (node, -1); +} + +static bml_node *bml_parse_node (char **doc) +{ + char *line; + bml_node *node = NULL; + + if ((line = bml_read_line (doc))) + { + node = bml_node_new (); + + bml_parse_depth (node, &line); + bml_parse_name (node, &line); + bml_parse_data (node, &line); + bml_parse_attr (node, &line); + } + else + return NULL; + + bml_skip_empty (doc); + while (*doc && bml_read_depth (*doc) > node->depth) + { + bml_node *child = bml_parse_node (doc); + + if (child) + node->child.push_back (child); + + bml_skip_empty (doc); + } + + return node; +} + +void bml_free_node (bml_node *node) +{ + delete[] (node->name); + delete[] (node->data); + + for (int i = 0; i < node->child.size(); i++) + { + bml_free_node (node->child[i]); + delete node->child[i]; + } + + for (int i = 0; i < node->attr.size(); i++) + { + bml_free_node (node->attr[i]); + delete node->attr[i]; + } + + return; +} + +bml_node *bml_parse (char **doc) +{ + bml_node *root = NULL; + bml_node *node = NULL; + char *ptr = *doc; + + root = bml_node_new (); + + while ((node = bml_parse_node (&ptr))) + { + root->child.push_back (node); + } + + if (!root->child.size()) + { + delete root; + root = NULL; + } + + return root; +} + +bml_node *bml_parse_file (char *filename) +{ + FILE *file = NULL; + char *buffer = NULL; + int file_size = 0; + bml_node *node = NULL; + + file = fopen (filename, "r"); + + if (!file) + return NULL; + + fseek (file, 0, SEEK_END); + file_size = ftell (file); + fseek (file, 0, SEEK_SET); + + buffer = new char[file_size + 1]; + fread (buffer, file_size, 1, file); + buffer[file_size] = '\0'; + + fclose (file); + + node = bml_parse (&buffer); + delete[] buffer; + + return node; +} diff --git a/bml.h b/bml.h new file mode 100644 index 00000000..a38ce437 --- /dev/null +++ b/bml.h @@ -0,0 +1,28 @@ +#ifndef __BML_H +#define __BML_H +#include + +typedef struct bml_node +{ + char *name; + char *data; + + int depth; + + std::vector attr; + std::vector child; + +} bml_node; + +bml_node *bml_parse_file (char *filename); + +/* Parse character array into BML tree. Destructive to input. */ +bml_node *bml_parse (char **buffer); + +/* Recursively free bml_node and substructures */ +void bml_free_node (bml_node *); + +/* Print node structure to stdout */ +void bml_print_node (bml_node *); + +#endif diff --git a/gtk/configure.ac b/gtk/configure.ac index e88bc77a..336e35a8 100644 --- a/gtk/configure.ac +++ b/gtk/configure.ac @@ -9,7 +9,7 @@ if test -z "$CXXFLAGS"; then CXXFLAGS="$CFLAGS" fi -EXTRA_FLAGS="-Wall -W -pedantic -Wno-unused-parameter -Wno-unused-but-set-variable" +EXTRA_FLAGS="-Wall -W -pedantic -Wno-unused-parameter" CFLAGS="$CFLAGS $EXTRA_FLAGS" CXXFLAGS="$CXXFLAGS $EXTRA_FLAGS" diff --git a/sha256.c b/sha256.c new file mode 100644 index 00000000..d46010d6 --- /dev/null +++ b/sha256.c @@ -0,0 +1,178 @@ +/********************************************************************* +* Filename: sha256.c +* Author: Brad Conte (brad AT bradconte.com) +* Copyright: +* Disclaimer: This code is presented "as is" without any guarantees. +* Details: Implementation of the SHA-256 hashing algorithm. + SHA-256 is one of the three algorithms in the SHA2 + specification. The others, SHA-384 and SHA-512, are not + offered in this implementation. + Algorithm specification can be found here: + * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf + This implementation uses little endian byte order. +*********************************************************************/ + +/*************************** HEADER FILES ***************************/ +#include +#include + +/****************************** MACROS ******************************/ +#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) +#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) + +#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) +#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) +#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) +#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) +#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) +#define SHA256_BLOCK_SIZE 32 /* SHA256 outputs a 32 byte digest */ + +/**************************** DATA TYPES ****************************/ +typedef unsigned char BYTE; /* 8-bit byte */ +typedef unsigned int WORD; /* 32-bit word, change to "long" for 16-bit machines */ + +typedef struct { + BYTE data[64]; + WORD datalen; + unsigned long long bitlen; + WORD state[8]; +} SHA256_CTX; + +/**************************** VARIABLES *****************************/ +static const WORD k[64] = { + 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, + 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, + 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, + 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, + 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, + 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, + 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, + 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 +}; + +/*********************** FUNCTION DEFINITIONS ***********************/ +void sha256_transform(SHA256_CTX *ctx, const BYTE data[]) +{ + WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; + + for (i = 0, j = 0; i < 16; ++i, j += 4) + m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); + for ( ; i < 64; ++i) + m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; + + a = ctx->state[0]; + b = ctx->state[1]; + c = ctx->state[2]; + d = ctx->state[3]; + e = ctx->state[4]; + f = ctx->state[5]; + g = ctx->state[6]; + h = ctx->state[7]; + + for (i = 0; i < 64; ++i) { + t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i]; + t2 = EP0(a) + MAJ(a,b,c); + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + + ctx->state[0] += a; + ctx->state[1] += b; + ctx->state[2] += c; + ctx->state[3] += d; + ctx->state[4] += e; + ctx->state[5] += f; + ctx->state[6] += g; + ctx->state[7] += h; +} + +void sha256_init(SHA256_CTX *ctx) +{ + ctx->datalen = 0; + ctx->bitlen = 0; + ctx->state[0] = 0x6a09e667; + ctx->state[1] = 0xbb67ae85; + ctx->state[2] = 0x3c6ef372; + ctx->state[3] = 0xa54ff53a; + ctx->state[4] = 0x510e527f; + ctx->state[5] = 0x9b05688c; + ctx->state[6] = 0x1f83d9ab; + ctx->state[7] = 0x5be0cd19; +} + +void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len) +{ + WORD i; + + for (i = 0; i < len; ++i) { + ctx->data[ctx->datalen] = data[i]; + ctx->datalen++; + if (ctx->datalen == 64) { + sha256_transform(ctx, ctx->data); + ctx->bitlen += 512; + ctx->datalen = 0; + } + } +} + +void sha256_final(SHA256_CTX *ctx, BYTE hash[]) +{ + WORD i; + + i = ctx->datalen; + + // Pad whatever data is left in the buffer. + if (ctx->datalen < 56) { + ctx->data[i++] = 0x80; + while (i < 56) + ctx->data[i++] = 0x00; + } + else { + ctx->data[i++] = 0x80; + while (i < 64) + ctx->data[i++] = 0x00; + sha256_transform(ctx, ctx->data); + memset(ctx->data, 0, 56); + } + + /* Append to the padding the total message's length in bits and transform. */ + ctx->bitlen += ctx->datalen * 8; + ctx->data[63] = ctx->bitlen; + ctx->data[62] = ctx->bitlen >> 8; + ctx->data[61] = ctx->bitlen >> 16; + ctx->data[60] = ctx->bitlen >> 24; + ctx->data[59] = ctx->bitlen >> 32; + ctx->data[58] = ctx->bitlen >> 40; + ctx->data[57] = ctx->bitlen >> 48; + ctx->data[56] = ctx->bitlen >> 56; + sha256_transform(ctx, ctx->data); + + /* Since this implementation uses little endian byte ordering and SHA uses big endian, + reverse all the bytes when copying the final state to the output hash. */ + for (i = 0; i < 4; ++i) { + hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; + hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; + hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; + hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; + hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; + hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; + hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; + hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; + } +} + +void sha256sum(unsigned char *data, unsigned int length, unsigned char *hash) +{ + SHA256_CTX ctx; + + sha256_init(&ctx); + sha256_update(&ctx, data, length); + sha256_final(&ctx, hash); +} diff --git a/sha256.h b/sha256.h new file mode 100644 index 00000000..201364df --- /dev/null +++ b/sha256.h @@ -0,0 +1,6 @@ +#ifndef __SHA256_H +#define __SHA256_H + +void sha256sum (unsigned char *data, unsigned int length, unsigned char *hash); + +#endif