1pass.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 23 Dec 2013 15:57:24 -0500
changeset 12 6a2d5b34d5ca
parent 11 b52e0f1798b8
child 15 e2973f4fb366
permissions -rw-r--r--
Whole bunch of GUI work.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#include <stdio.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include <stdlib.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <string.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <assert.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#include "lua.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#include "lauxlib.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
#include "lualib.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#include "pkcs5_pbkdf2.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
#include "aes.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include "base64.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include "md5.h"
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    12
#include <gtk/gtk.h>
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
static lua_State *luaState = NULL;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
static const uint8_t zero16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
static inline int retvalStringBytes(lua_State *L, const uint8_t *str, size_t len)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
    if (str != NULL)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
        lua_pushlstring(L, (const char *) str, len);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
} // retvalStringBytes
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
    29
static inline int retvalString(lua_State *L, const char *str)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
    30
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
    31
    return retvalStringBytes(L, (const uint8_t *) str, strlen(str));
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
    32
} // retvalString
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
    33
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    34
static inline int retvalPointer(lua_State *L, void *ptr)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    35
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    36
    if (ptr != NULL)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    37
        lua_pushlightuserdata(L, ptr);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    38
    else
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    39
        lua_pushnil(L);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    40
    return 1;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    41
} // retvalPointer
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    42
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
static inline void xorBlock(uint8_t *dst, const uint8_t *src)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    int i;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    for (i = 0; i < 16; i++, dst++, src++)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
        *dst ^= *src;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
} // xorBlock
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
static int decryptUsingKeyAndIvec(uint8_t *data, size_t *datalen, const uint8_t *key, const uint8_t *iv)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    const size_t blocks = *datalen / 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    uint8_t *block = data + ((blocks-1) * 16);   // start at final block, work backwards
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    const uint8_t *padding = &block[15];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    uint8_t expkey[aesExpandedKeySize];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    size_t i;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
    if (blocks == 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
        return 1;  // nothing to do.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
	aesExpandKey(key, expkey);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    for (i = 0; i < blocks-1; i++)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
        aesDecrypt(block, expkey, block);   // decrypt in place.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
        xorBlock(block, block-16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
        block -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    }
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    aesDecrypt(block, expkey, block);   // decrypt in place.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    xorBlock(block, iv);   // xor against initial vector for final block.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    if (*padding > 16)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
        return 0;  // bad data?
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    *datalen -= *padding;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
} // decryptBinaryUsingKeyAndIvec
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
static inline int isSalted(const uint8_t *data, const size_t datalen)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    return ( (datalen > sizeof (saltprefix)) &&
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
             (memcmp(data, saltprefix, sizeof (saltprefix)) == 0) );
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
} // isSalted
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
static int decryptUsingPBKDF2(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    const char *base64 = luaL_checkstring(L, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
    const char *password = luaL_checkstring(L, 2);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    const int iterations = luaL_checkinteger(L, 3);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    size_t datalen = strlen(base64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    uint8_t *data = dataptr;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    base64_decodestate base64state;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    base64_init_decodestate(&base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    const uint8_t *salt = zero16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    int saltlen = sizeof (zero16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    if (isSalted(data, datalen))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
        salt = data + 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        saltlen = 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
        data += 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
        datalen -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    uint8_t output[32];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    pkcs5_pbkdf2(password, strlen(password), salt, saltlen, output, sizeof (output), (unsigned int) iterations);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    const uint8_t *aeskey = &output[0];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
    const uint8_t *aesiv = &output[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
        retvalStringBytes(L, data, datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
    free(dataptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
} // decryptUsingPBKDF2
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
static int decryptBase64UsingKey(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
    size_t keylen = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
    const char *base64 = luaL_checkstring(L, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
    size_t datalen = strlen(base64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
    uint8_t *data = dataptr;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    base64_decodestate base64state;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
    base64_init_decodestate(&base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    uint8_t aeskey[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    uint8_t aesiv[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    MD5_CTX md5;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
    if (isSalted(data, datalen))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
        const uint8_t *salt = data + 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
        const size_t saltlen = 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
        data += 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        datalen -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
        assert(aesNr == 10);  // AES-256 needs more rounds.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
        assert(aesNk == 4);   // hashing size is hardcoded later.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
        uint8_t hashing[32];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
        MD5_append(&md5, salt, saltlen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
        MD5_finish(&md5, hashing);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        MD5_append(&md5, hashing, 16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
        MD5_append(&md5, salt, saltlen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
        MD5_finish(&md5, &hashing[16]);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        memcpy(aeskey, hashing, 4 * aesNk);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
        memcpy(aesiv, &hashing[4 * aesNk], 16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
        MD5_finish(&md5, aeskey);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
        memset(aesiv, '\0', sizeof (aesiv));
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    } // else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
        retvalStringBytes(L, data, datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    free(dataptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
} // decryptBase64UsingKey
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   186
static int runGuiPasswordPrompt(lua_State *L)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   187
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   188
    const char *hintstr = lua_tostring(L, 1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   189
    GtkWidget *dialog = gtk_dialog_new_with_buttons(
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   190
                            "Master Password", NULL, GTK_DIALOG_MODAL,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   191
                            GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   192
                            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   193
                            NULL);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   194
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   195
    GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   196
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   197
    if (hintstr != NULL)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   198
    {
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   199
        GtkWidget *label = gtk_label_new(hintstr);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   200
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   201
        gtk_container_add(GTK_CONTAINER(content_area), label);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   202
    } // if
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   203
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   204
    GtkWidget *entry = gtk_entry_new();
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   205
    gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   206
    gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   207
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   208
    gtk_container_add(GTK_CONTAINER(content_area), entry);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   209
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   210
    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   211
    gtk_widget_show_all(dialog);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   212
    const int ok = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   213
    retvalString(L, ok ? (const char *) gtk_entry_get_text(GTK_ENTRY(entry)) : NULL);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   214
    gtk_widget_destroy(dialog);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   215
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   216
    return 1;
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   217
} // runGuiPasswordPrompt
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   218
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   219
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   220
static int copyToClipboard(lua_State *L)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   221
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   222
    const char *str = luaL_checkstring(L, 1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   223
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), str, -1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   224
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), str, -1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   225
} // copyToClipboard
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   226
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   227
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   228
static int makeGuiMenu(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   229
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   230
    return retvalPointer(L, gtk_menu_new());
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   231
} // makeGuiMenu
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   232
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   233
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   234
static void clickedMenuItem(void *arg)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   235
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   236
    // This is the callback from GTK+; now call into our actual Lua callback!
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   237
    const int callback = (int) ((size_t)arg);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   238
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   239
    lua_call(luaState, 0, 0);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   240
} // clickedMenuItem
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   241
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   242
#if 0  // !!! FIXME: figure out how to fire this.
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   243
static void deletedMenuItem(void *arg)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   244
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   245
    // Clean up the Lua function we referenced in the Registry.
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   246
    const int callback = (int) ((size_t)arg);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   247
printf("unref callback %d\n", callback);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   248
    luaL_unref(luaState, LUA_REGISTRYINDEX, callback);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   249
} // deletedMenuItem
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   250
#endif
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   251
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   252
static int appendGuiMenuItem(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   253
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   254
    const int argc = lua_gettop(L);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   255
    GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   256
    const char *label = luaL_checkstring(L, 2);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   257
    GtkWidget *item = gtk_menu_item_new_with_label(label);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   258
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   259
    if ((argc >= 3) && (!lua_isnil(L, 3)))
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   260
    {
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   261
        assert(lua_isfunction(L, 3));
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   262
        lua_pushvalue(L, 3);  // copy the Lua callback (luaL_ref() pops it).
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   263
        const int callback = luaL_ref(L, LUA_REGISTRYINDEX);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   264
        gtk_signal_connect_object(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(clickedMenuItem), (gpointer) ((size_t)callback));
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   265
    } // if
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   266
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   267
    gtk_widget_show(item);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   268
    gtk_menu_append(menu, item);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   269
    return retvalPointer(L, item);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   270
} // appendGuiMenuItem
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   271
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   272
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   273
static int setGuiMenuItemSubmenu(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   274
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   275
    GtkMenuItem *item = (GtkMenuItem *) lua_touserdata(L, 1);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   276
    GtkWidget *submenu = (GtkWidget *) lua_touserdata(L, 2);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   277
    gtk_menu_item_set_submenu(item, submenu);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   278
    return 0;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   279
} // setGuiMenuItemSubmenu
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   280
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   281
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   282
static int popupGuiMenu(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   283
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   284
    GtkMenu *menu = (GtkMenu *) lua_touserdata(L, 1);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   285
    gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   286
    return 0;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   287
} // popupGuiMenu
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   288
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   289
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   290
static int giveControlToGui(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   291
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   292
    gtk_main();
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   293
    return 0;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   294
} // giveControlToGui
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   295
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   296
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    if (nsize == 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
        free(ptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
        return NULL;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    return realloc(ptr, nsize);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
} // luaAlloc
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
static inline void luaSetCFunc(lua_State *L, lua_CFunction f, const char *sym)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
    lua_pushcfunction(L, f);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    lua_setglobal(luaState, sym);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
} // luaSetCFunc
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
static int luaFatal(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    const char *errstr = lua_tostring(L, -1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
    fflush(stderr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    exit(1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
} // luaFatal
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   324
static void deinitLua(void)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   325
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   326
    if (luaState != NULL)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   327
    {
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   328
        lua_close(luaState);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   329
        luaState = NULL;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   330
    } // if
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   331
} // deinitLua
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   332
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   333
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   334
static int initLua(const int argc, char **argv)
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
{
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   336
    atexit(deinitLua);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   337
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
    assert(luaState == NULL);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
    luaState = lua_newstate(luaAlloc, NULL);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    lua_atpanic(luaState, luaFatal);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
    assert(lua_checkstack(luaState, 20));  // Just in case.
2
16a2d269fd41 Just use the built-in luaL_openlibs()
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
   343
    luaL_openlibs(luaState);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    // Set up initial C functions, etc we want to expose to Lua code...
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   348
    luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu");
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   349
    luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem");
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   350
    luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu");
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   351
    luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu");
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   352
    luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   353
    luaSetCFunc(luaState, runGuiPasswordPrompt, "runGuiPasswordPrompt");
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   354
    luaSetCFunc(luaState, copyToClipboard, "copyToClipboard");
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   356
    // Set up argv table...
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   357
    lua_newtable(luaState);
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   358
    int i;
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   359
    for (i = 0; i < argc; i++)
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   360
    {
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   361
        lua_pushinteger(luaState, i+1);
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   362
        lua_pushstring(luaState, argv[i]);
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   363
        lua_settable(luaState, -3);
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   364
    } // for
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   365
    lua_setglobal(luaState, "argv");
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   366
3
32af809e8c0b Corrected comment. It's basically just moving to Lua as the mainline.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   367
    // Transfer control to Lua...
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
    if (luaL_dofile(luaState, "1pass.lua") != 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
        const char *msg = lua_tostring(luaState, -1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
        fprintf(stderr, "1pass.lua didn't run: %s\n", msg);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
        lua_pop(luaState, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
        return 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
} // initLua
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
int main(int argc, char **argv)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
{
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   382
    gtk_init(&argc, &argv);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   384
    if (!initLua(argc, argv))  // this will move control to 1pass.lua
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
        return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    return 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
} // main
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
// end of 1pass.c ...
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391