shkey_t *shecdsa_key_pub(shkey_t *priv_key) { #ifdef HAVE_LIBGMP ecdsa_parameters curve; mpz_t temp; mpz_t key; char pub_key[256]; char *comp_hex; /* setup parameters */ curve = ecdsa_parameters_init(); ecdsa_parameters_load_curve(curve, secp160r1); /* initialize public key */ ecdsa_point Q = ecdsa_point_init(); mpz_init(key); #if 0 priv_key_hex = shkey_hex(priv_key); str_hash = shsha1_hash(priv_key_hex, strlen(priv_key_hex)); mpz_set_str(key, str_hash, 16); #endif mpz_set_str(key, shkey_hex(priv_key), 16); #if 0 /* key modulo n */ mpz_init(temp); mpz_mod(temp, key, curve->n); mpz_set(key, temp); mpz_clear(temp); #endif /* generate public key */ memset(pub_key, 0, sizeof(pub_key)); ecdsa_signature_generate_key(Q, key, curve); comp_hex = ecdsa_point_compress(Q, 21); if (!comp_hex) return (NULL); strncpy(pub_key, comp_hex, sizeof(pub_key)-1); free(comp_hex); ecdsa_parameters_clear(curve); ecdsa_point_clear(Q); mpz_clear(key); return (shecdsa_key(pub_key)); #else return (NULL); #endif }
int sexe_event_remove(lua_State *L, int e_type, char *e_name) { sexe_event_t t_event; sexe_event_t *e; shkey_t *key; int err; memset(&t_event, 0, sizeof(t_event)); t_event.event_type = e_type; strncpy(t_event.mod_name, e_name, sizeof(t_event.mod_name) - 1); key = shkey_bin(&t_event, sizeof(sexe_event_t)); /* remove event's global callback */ lua_pushnil(L); lua_setglobal(L, shkey_hex(key)); e = (sexe_event_t *)shmap_get_ptr(event_map, key); if (e) { shmap_unset(event_map, key); free(e); } shkey_free(&key); return (0); }
int install_sexe_userdata(sexe_t *S, char *tag) { SHFL *fl; shjson_t *udata; shfs_t *fs; shbuf_t *buff; shkey_t *k; char path[PATH_MAX+1]; int is_new; k = shkey_str(tag); sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); memcpy(&S->pname, k, sizeof(S->pname)); shkey_free(&k); buff = shbuf_init(); fs = shfs_init(NULL); fl = shfs_file_find(fs, path); is_new = shfs_read(fl, buff); udata = shjson_init(shbuf_size(buff) ? (char *)shbuf_data(buff) : NULL); shbuf_free(&buff); if (is_new) shjson_num_add(udata, "birth", shtimef(shtime())); sexe_table_set(S, udata); lua_setglobal(S, "userdata"); shjson_free(&udata); shfs_free(&fs); return (0); }
/** * Store supplementary credential data in a file. */ int shfs_cred_store(shfs_ino_t *file, shkey_t *key, unsigned char *data, size_t data_len) { shfs_ino_t *cred; shbuf_t *buff; char key_buf[MAX_SHARE_HASH_LENGTH]; int err; memset(key_buf, 0, sizeof(key_buf)); sprintf(key_buf, "%s", shkey_hex(key)); cred = shfs_inode(file, key_buf, SHINODE_ACCESS); if (!cred) return (SHERR_IO); cred->blk.hdr.format = SHINODE_ACCESS; buff = shbuf_map(data, data_len); err = shfs_aux_write(cred, buff); free(buff); if (err) return (err); file->blk.hdr.attr |= SHATTR_CRED; file->blk.hdr.crc = shcrc(data, data_len); file->blk.hdr.size = data_len; err = shfs_inode_write_entity(file); if (err) return (err); return (0); }
int txop_ward_confirm(shpeer_t *peer, tx_ward_t *ward) { tx_context_t *ctx; tx_t *tx; int err; #if 0 /* verify identity exists */ ctx = (tx_context_t *)tx_load(TX_CONTEXT, &ward->ward_ctx); if (!ctx) return (SHERR_NOKEY); /* .. */ pstore_free(ctx); err = confirm_signature(&ward->ward_sig, shpeer_kpriv(&ward->ward_peer), shkey_hex(&ward->ward_key)); pstore_free(tx); if (err) return (err); err = inittx_context(ctx, get_tx_key(ward), ashkey_uniq()); if (err) return (err); #endif return (0); }
static int _shcert_sign_verify_ecdsa(shcert_t *cert, shcert_t *parent) { shkey_t *pub_key; char *hex_data; unsigned char data[256]; int data_len; int err; if (parent) { if (!shkey_cmp(shcert_sub_sig(parent), shcert_iss_sig(cert))) { return (SHERR_INVAL); } } hex_data = shkey_hex(&cert->cert_iss.ent_sig.sig_key); data_len = strlen(hex_data) / 2; if (!data_len) { return (SHERR_INVAL); } memset(data, 0, sizeof(data)); hex2bin(data, hex_data, data_len); pub_key = &cert->cert_sub.ent_sig.sig_key; err = shecdsa_verify(pub_key, cert->cert_sub.ent_sig.key.ecdsa.sig_r, cert->cert_sub.ent_sig.key.ecdsa.sig_s, data, data_len); if (err) return (err); return (0); }
void *tx_load(int tx_op, shkey_t *tx_key) { if (!is_tx_stored(tx_op)) return (NULL); return (pstore_load(tx_op, shkey_hex(tx_key))); }
/** * @returns An allocated parent transaction or NULL. */ tx_t *get_tx_parent(tx_t *tx) { tx_t *p_tx; if (0 == shkey_cmp(&tx->tx_pkey, ashkey_blank())) return (NULL); return ((tx_t *)pstore_load(tx->tx_ptype, shkey_hex(&tx->tx_pkey))); }
/** * @note does not return allocated memory */ shpeer_t *load_asset_peer(shkey_t *id_key) { static shpeer_t ret_peer; tx_id_t *id; id = (tx_id_t *)pstore_load(TX_IDENT, (char *)shkey_hex(id_key)); if (!id) return (NULL); memcpy(&ret_peer, &id->id_peer, sizeof(shpeer_t)); return (&ret_peer); }
int shecdsa_verify(shkey_t *pub_key, char *str_r, char *str_s, unsigned char *data, size_t data_len) { #ifdef HAVE_LIBGMP ecdsa_parameters curve; ecdsa_signature sig; ecdsa_point Q; mpz_t temp; mpz_t m; uint8_t *hash; int ok; /* setup parameters */ curve = ecdsa_parameters_init(); ecdsa_parameters_load_curve(curve, secp160r1); /* decompress public key */ Q = ecdsa_point_init(); ecdsa_point_decompress(Q, (char *)shkey_hex(pub_key), curve); /* process message into sha1 hash */ mpz_init(m); hash = shsha1_hash(data, data_len); mpz_set_str(m, hash, 16); /* msg modulo n - note standard is bit-length not mod */ mpz_init(temp); mpz_mod(temp, m, curve->n); mpz_set(m, temp); mpz_clear(temp); sig = ecdsa_signature_init(); ecdsa_signature_set_str(sig, str_r, str_s, 16); /* verify signature */ ok = ecdsa_signature_verify(m, sig, Q, curve); ecdsa_parameters_clear(curve); ecdsa_signature_clear(sig); ecdsa_point_clear(Q); mpz_clear(m); if (!ok) return (SHERR_ACCESS); return (0); #else return (SHERR_OPNOTSUPP); #endif }
shfs_ino_t *shfs_rev_get(shfs_ino_t *repo, shkey_t *rev_key) { shfs_ino_t *rev; if (shfs_type(repo) == SHINODE_FILE) repo = shfs_inode(repo, NULL, SHINODE_REPOSITORY); if (shfs_type(repo) != SHINODE_REPOSITORY) return (NULL); if (shkey_cmp(rev_key, ashkey_blank())) return (NULL); return (shfs_inode(repo, (char *)shkey_hex(rev_key), SHINODE_REVISION)); }
int pstore_save(void *data, size_t data_len) { tx_t *tx; shkey_t *s_key; tx = (tx_t *)data; s_key = get_tx_key(tx); if (!s_key) return (SHERR_INVAL); pstore_write(tx->tx_op, (char *)shkey_hex(s_key), (unsigned char *)data, data_len); return (0); }
int update_sexe_userdata(sexe_t *S) { SHFL *fl; shjson_t *udata; shfs_t *fs; shbuf_t *buff; shkey_t *k; char path[PATH_MAX+1]; char *str; int err; k = &S->pname; if (shkey_cmp(k, ashkey_blank())) { fprintf(stderr, "DEBUG: update_sexe_userdata: no app key\n"); return (0); /* blank */ } sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); lua_getglobal(S, "userdata"); udata = sexe_table_get(S); if (!udata) { fprintf(stderr, "DEBUG: update_sexe_userdata: no global 'userdata' variable.\n"); return (SHERR_INVAL); } str = shjson_print(udata); if (!str) { fprintf(stderr, "DEBUG: update_sexe_userdata: error encoding JSON.\n"); return (SHERR_INVAL); } shjson_free(&udata); buff = shbuf_init(); shbuf_catstr(buff, str); free(str); fs = shfs_init(NULL); fl = shfs_file_find(fs, path); err = shfs_write(fl, buff); shbuf_free(&buff); shfs_free(&fs); return (err); }
int pstore_delete_tx(tx_t *tx) { shkey_t *s_key; int err; if (!tx) return (SHERR_INVAL); s_key = get_tx_key(tx); if (!s_key) return (SHERR_INVAL); err = pstore_delete(tx->tx_op, (char *)shkey_hex(s_key)); if (err) return (err); return (0); }
shkey_t *shfs_index_key(shfs_idx_t *idx, uint64_t crc, shfs_dirent_t *ent_p) { shkey_t *key; int h_num = (crc % 256); if (!idx->hash[h_num]) { key = shkey_bin(&crc, sizeof(uint64_t)); idx->hash_ino[h_num] = shfs_inode(idx->file, shkey_hex(key), SHINODE_INDEX); shkey_free(&key); return (NULL); } for (i = 0; i < idx->hash_max[h_num]; i++) { if ((idx->key[h_num] + i) == crc) { return (idx->hash[h_num] + i); } } return (NULL); }
/** * Generate a public key. */ shkey_t *shecdsa_key_priv(char *hex_seed) { #ifdef HAVE_LIBGMP ecdsa_parameters curve; mpz_t d; shkey_t *ret_key; shkey_t *ukey; #if 0 /* setup parameters */ curve = ecdsa_parameters_init(); ecdsa_parameters_load_curve(curve, secp160r1); /* public key */ ecdsa_point Q = ecdsa_point_init(); ecdsa_point Q_check = ecdsa_point_init(); #endif if (hex_seed) { ukey = shkey_hexgen(hex_seed); } else { ukey = shkey_uniq(); /* generate random */ } if (!ukey) { fprintf(stderr, "DEBUG: shecdsa_key_priv: !ukey\n"); return (NULL); } /* truncate to "21 bytes" */ ukey->code[5] = (ukey->code[5] & 0xff); ukey->code[6] = 0; ukey->code[7] = 0; ukey->alg = SHKEY_ALG_ECDSA; ret_key = shecdsa_key((char *)shkey_hex(ukey)); shkey_free(&ukey); return (ret_key); #else return (NULL); #endif }
int shfs_cred_remove(shfs_ino_t *file, shkey_t *key) { shfs_ino_t *cred; char key_buf[MAX_SHARE_HASH_LENGTH]; int err; memset(key_buf, 0, sizeof(key_buf)); sprintf(key_buf, "%s", shkey_hex(key)); cred = shfs_inode(file, key_buf, SHINODE_ACCESS); if (!cred) return (SHERR_IO); if (shfs_format(cred) != SHINODE_ACCESS) return (0); /* done */ err = shfs_inode_clear(cred); if (err) return (err); return (0); }
int sexe_event_handle(lua_State *L, int e_type, shjson_t *json) { const shmap_t *h = event_map; shmap_index_t *hi; sexe_event_t *event; char *key; char *val; size_t len; int flag; for (hi = shmap_first(h); hi; hi = shmap_next(hi)) { shmap_self(hi,(void*) &key, (void*) &val, &len, &flag); if (!(flag & SHMAP_BINARY)) continue; event = (sexe_event_t *)val; if (event->event_type != e_type) continue; /* wrong event type */ sexe_event_call(L, shkey_hex(&event->reg_key), e_type, json); } }
/** * Load supplementary credential data from a file. */ int shfs_cred_load(shfs_ino_t *file, shkey_t *key, unsigned char *data, size_t max_len) { shfs_ino_t *cred; shbuf_t *buff; char key_buf[MAX_SHARE_HASH_LENGTH]; int err; if (!(file->blk.hdr.attr & SHATTR_CRED)) return (SHERR_NOENT); memset(key_buf, 0, sizeof(key_buf)); sprintf(key_buf, "%s", shkey_hex(key)); cred = shfs_inode(file, key_buf, SHINODE_ACCESS); if (!cred) return (SHERR_IO); if (cred->blk.hdr.format != SHINODE_ACCESS) return (SHERR_NOENT); buff = shbuf_init(); err = shfs_aux_read(cred, buff); if (err) { shbuf_free(&buff); return (err); } if (shbuf_size(buff) == 0) { shbuf_free(&buff); return (SHERR_IO); } /* copy buffer */ memcpy(data, shbuf_data(buff), MIN(shbuf_size(buff), max_len)); shbuf_free(&buff); return (0); }
/** * The "os.register(<string>,<function>)" function will register a callback function for the event name specified. */ int _lfunc_register_event(sexe_t *L) { int ef_nr = sexe_event_next_id(); const char *e_name = (int)luaL_checkstring(L, 1); shkey_t *key = sexe_event_key(e_name); char *ptr = shkey_hex(key); /* _EVENT table */ lua_getglobal(L, EVENT_ENV); if (lua_isnil(L, -1)) { lua_pop(L, 1); /* _EVENT */ lua_createtable(L, 0, 1); } /* _EVENT[<id>] */ lua_pushstring(L, ptr); lua_getfield(L, -2, ptr); if (lua_isnil(L, -1)) { lua_pop(L, 1); lua_newtable(L); } /* event callback function */ lua_pushnumber(L, ef_nr); lua_pushvalue(L, 2); lua_settable(L, -3); /* set _ENV['_EVENT'][<id>] */ lua_settable(L, -3); /* set _ENV['_EVENT'] */ lua_setglobal(L, EVENT_ENV); return (0); }
char *sexe_event_init(int e_type, const char *e_name) { static char key_str[256]; sexe_event_t *e; shkey_t *key; if (!event_map) { event_map = shmap_init(); } e = (sexe_event_t *)calloc(1, sizeof(sexe_event_t)); if (!e) return (SHERR_NOMEM); e->event_type = e_type; strncpy(e->mod_name, e_name, sizeof(e->mod_name) - 1); key = shkey_bin(e, sizeof(sexe_event_t)); memcpy(&e->reg_key, key, sizeof(shkey_t)); shmap_set_ptr(event_map, key, e); strncpy(key_str, shkey_hex(key), sizeof(key_str) - 1); shkey_free(&key); return (key_str); }
tx_bond_t *load_bond(shkey_t *bond_key) { return ((tx_bond_t *)pstore_load(TX_FILE, shkey_hex(bond_key))); }
int shecdsa_sign(shkey_t *priv_key, char *sig_r, char *sig_s, unsigned char *data, size_t data_len) { #ifdef HAVE_LIBGMP ecdsa_parameters curve; ecdsa_signature sig; uint8_t *hash; mpz_t temp; mpz_t key; mpz_t m; /* setup parameters */ curve = ecdsa_parameters_init(); ecdsa_parameters_load_curve(curve, secp160r1); /* generate private key from user-context */ mpz_init(key); #if 0 priv_key_hex = shkey_hex(priv_key); hash = shsha1_hash(priv_key_hex, strlen(priv_key_hex)); mpz_set_str(key, hash, 16); #endif mpz_set_str(key, shkey_hex(priv_key), 16); #if 0 /* key modulo n */ mpz_init(temp); mpz_mod(temp, key, curve->n); mpz_set(key, temp); #endif /* process message into sha1 hash */ mpz_init(m); hash = shsha1_hash(data, data_len); mpz_set_str(m, hash, 16); /* msg modulo n */ mpz_init(temp); mpz_mod(temp, m, curve->n); mpz_set(m, temp); mpz_clear(temp); /* generate signature */ sig = ecdsa_signature_init(); ecdsa_signature_sign(sig, m, key, curve); #if 0 { FILE *out = stdout; fprintf(out, "Signature:\n\tR:"); mpz_out_str(out, 16, sig->r); fprintf(out, "\n\tS:"); mpz_out_str(out, 16, sig->s); fprintf(out, "\n"); } #endif memset(sig_r, 0, sizeof(sig_r)); strcpy(sig_r, mpz_get_str(NULL, 16, sig->r)); memset(sig_s, 0, sizeof(sig_s)); strcpy(sig_s, mpz_get_str(NULL, 16, sig->s)); ecdsa_parameters_clear(curve); ecdsa_signature_clear(sig); mpz_clear(key); mpz_clear(m); return (0); #else return (SHERR_OPNOTSUPP); #endif }
void shcert_print(shcert_t *cert, shbuf_t *pr_buff) { char tbuf1[256]; char tbuf2[256]; char buf[4096]; if (!cert || !pr_buff) return; shbuf_catstr(pr_buff, "Certificate:\n"); shbuf_catstr(pr_buff, " Data:\n"); sprintf(buf, " Version: %d\n", cert->cert_ver); shbuf_catstr(pr_buff, buf); shbuf_catstr(pr_buff, " Serial Number: "); shcert_hex_print(pr_buff, shcert_sub_ser(cert), sizeof(shcert_sub_ser(cert)), ""); sprintf(buf, " Signature Algorithm: %s\n", shsig_alg_str(shcert_iss_alg(cert) | shcert_sub_alg(cert))); shbuf_catstr(pr_buff, buf); sprintf(buf, " Issuer: %s\n", cert->cert_iss.ent_name); shbuf_catstr(pr_buff, buf); strcpy(tbuf1, shctime(shcert_sub_stamp(cert))+4); strcpy(tbuf2, shctime(shcert_sub_expire(cert))+4); sprintf(buf, " Validity: %-20.20s - %-20.20s\n", tbuf1, tbuf2); shbuf_catstr(pr_buff, buf); sprintf(buf, " Subject: %s\n", cert->cert_sub.ent_name); shbuf_catstr(pr_buff, buf); sprintf(buf, " Public Key Algorithm: (%d bit) %s\n", shcert_sub_len(cert) * 8, shsig_alg_str(shcert_sub_alg(cert))); shbuf_catstr(pr_buff, buf); sprintf(buf, " Checksum: %llu\n", shkey_crc(shcert_sub_sig(cert))); sprintf(buf, " 192-Bit: %s\n", shkey_hex(shcert_sub_sig(cert))); shbuf_catstr(pr_buff, buf); if (shcert_sub_alg(cert) & SHKEY_ALG_RSA) { shbuf_catstr(pr_buff, " Modulus:\n"); shcert_hex_print_reverse(pr_buff, cert->cert_sub.ent_sig.key.rsa.mod, cert->cert_sub.ent_sig.key.rsa.mod_len, " "); } shbuf_catstr(pr_buff, " X509v3 extensions:\n"); sprintf(buf, " Basic Constraints: CA=%s\n", (cert->cert_flag & SHCERT_CERT_CHAIN) ? "false" : "true"); shbuf_catstr(pr_buff, buf); if (!shpeer_localhost(&cert->cert_sub.ent_peer)) { sprintf(buf, " Alternate Subject: %s\n", shpeer_print(&cert->cert_sub.ent_peer)); shbuf_catstr(pr_buff, buf); } sprintf(buf, " Extended Usage: %s\n", shcert_flag_str(cert->cert_flag)); shbuf_catstr(pr_buff, buf); sprintf(buf, " Private Signature: %s (%d bytes)\n", shsig_alg_str(shcert_iss_alg(cert)), shcert_iss_len(cert)); shbuf_catstr(pr_buff, buf); if (shcert_iss_alg(cert) & SHKEY_ALG_MD5) { shcert_hex_print(pr_buff, cert->cert_iss.ent_sig.key.md.md, cert->cert_iss.ent_sig.key.md.md_len, " "); } else if (shcert_iss_alg(cert) & SHKEY_ALG_SHA1) { shcert_hex_print(pr_buff, cert->cert_iss.ent_sig.key.sha.sha, cert->cert_iss.ent_sig.key.sha.sha_len, " "); } else if (shcert_iss_alg(cert) & SHKEY_ALG_SHA256) { shcert_hex_print(pr_buff, cert->cert_iss.ent_sig.key.sha.sha, cert->cert_iss.ent_sig.key.sha.sha_len, " "); } else { sprintf(buf, " Checksum: %llu\n", shkey_crc(shcert_iss_sig(cert))); shbuf_catstr(pr_buff, buf); sprintf(buf, " 192-Bit: %s\n", shkey_hex(shcert_iss_sig(cert))); shbuf_catstr(pr_buff, buf); } }
/** * @see shsig_shr_sign() */ int shcert_sign(shcert_t *cert, shcert_t *parent) { shkey_t *key; unsigned char *enc_data; size_t enc_len; int err; if (!parent) return (SHERR_INVAL); if (!(parent->cert_flag & SHCERT_CERT_SIGN)) { /* parent certificate lacks ability to sign. */ return (SHERR_INVAL); } /* assign issuer's 128-bit serial number (regardless of algorythm) */ memcpy(cert->cert_iss.ent_ser, parent->cert_sub.ent_ser, 16); if (cert->cert_sub.ent_sig.sig_key.alg == SHKEY_ALG_ECDSA) { shkey_t *pub_key = &cert->cert_sub.ent_sig.sig_key; shkey_t *priv_key; shkey_t *seed_key; shpeer_t *peer; char sig_r[256]; char sig_s[256]; char *hex_data; unsigned char data[256]; int data_len; /* fill in parent signature */ memcpy(&cert->cert_iss.ent_sig, &parent->cert_sub.ent_sig, sizeof(shsig_t)); peer = shpeer_init(NULL, NULL); seed_key = shpeer_kpriv(peer); priv_key = shecdsa_key_priv(shkey_hex(seed_key)); shpeer_free(&peer); pub_key = shecdsa_key_pub(priv_key); memcpy(&cert->cert_sub.ent_sig.sig_key, pub_key, sizeof(shkey_t)); if ((parent->cert_flag & SHCERT_CERT_NONREPUDIATION)) { /* must be derived from owner to preserve authenticy. */ if (!shkey_cmp(&cert->cert_sub.ent_sig.sig_key, &cert->cert_iss.ent_sig.sig_key)) { return (SHERR_ACCESS); } } hex_data = shkey_hex(&cert->cert_iss.ent_sig.sig_key); data_len = strlen(hex_data) / 2; memset(data, 0, sizeof(data)); hex2bin(data, hex_data, data_len); shecdsa_sign(priv_key, sig_r, sig_s, data, data_len); strcpy(cert->cert_sub.ent_sig.key.ecdsa.sig_r, sig_r); strcpy(cert->cert_sub.ent_sig.key.ecdsa.sig_s, sig_s); cert->cert_sub.ent_len = data_len; shkey_free(&pub_key); shkey_free(&priv_key); } else { err = shencode((char *)&parent->cert_sub.ent_sig.sig_key, sizeof(shkey_t), &enc_data, &enc_len, &parent->cert_iss.ent_sig.sig_key); if (err) return (err); key = shkey_bin(enc_data, enc_len); free(enc_data); memcpy(&cert->cert_sub.ent_sig.sig_key, key, sizeof(shkey_t)); cert->cert_sub.ent_len = enc_len; shkey_free(&key); } cert->cert_flag |= SHCERT_CERT_CHAIN; cert->cert_flag |= parent->cert_flag; /* inherit parent's attributes */ cert->cert_sub.ent_sig.sig_key.alg = parent->cert_sub.ent_sig.sig_key.alg; strcpy(cert->cert_iss.ent_name, parent->cert_sub.ent_name); cert->cert_iss.ent_sig.sig_stamp = parent->cert_sub.ent_sig.sig_stamp; cert->cert_iss.ent_sig.sig_expire = parent->cert_sub.ent_sig.sig_expire; cert->cert_iss.ent_len = parent->cert_sub.ent_len; return (0); }
/** * The "os.trigger(<string>[,<table>])" function will trigger all callbacks for the specifid event name to be called with an optional object argument. */ int lfunc_trigger_event(sexe_t *L) { const char *e_name; shjson_t *json; int t_reg = 0; int ret_bool; int err; /* first argument: event name */ e_name = (int)luaL_checkstring(L, 1); /* second optional arg; table of data. */ json = NULL; if (lua_istable(L, 2)) { lua_pushvalue(L, 2); json = sexe_table_get(L); } ret_bool = 1; err = sexe_event_handle(L, e_name, json); if (err) ret_bool = 0; #if 0 /* iterate through registered functions for event */ { shkey_t *key = sexe_event_key(e_name); char *e_hex = shkey_hex(key); lua_getglobal(L, EVENT_ENV); if (lua_isnil(L, -1)) { return (0); /* error */ } lua_getfield(L, -1, e_hex); if (lua_isnil(L, -1)) { return (0); /* error */ } lua_pushnil(L); while (lua_next(L, -2)) { int t = lua_type(L, -1); if (t == LUA_TFUNCTION) { /* copy function call onto stack lua_pushvalue(L, 1); */ /* 1. user args */ if (json) sexe_table_set(L, json); else lua_pushnil(L); /* 2. event name */ lua_pushstring(L, e_name); /* exec */ lua_pcall(L, 2, 0, 0); } else { lua_pop(L, 1); /* value */ } //lua_pop(L, 1); /* key */ } } #endif if (json) shjson_free(&json); /* return single boolean */ lua_pushboolean(L, ret_bool); return (1); }
tx_asset_t *load_asset(shkey_t *asset_key) { return ((tx_asset_t *)pstore_load(TX_ASSET, (char *)shkey_hex(asset_key))); }
int shfs_rev_commit(shfs_ino_t *file, shfs_ino_t **rev_p) { shstat st; shbuf_t *diff_buff; shbuf_t *work_buff; shbuf_t *head_buff; shfs_ino_t *base; shfs_ino_t *repo; /* SHINODE_REPOSITORY */ shfs_ino_t *new_rev; /* SHINODE_REVISION */ shfs_ino_t *delta; /* SHINODE_DELTA */ shkey_t *rev_key; shfs_t *fs; int err; if (rev_p) *rev_p = NULL; head_buff = work_buff = diff_buff = NULL; err = shfs_fstat(file, &st); if (err) return (err); work_buff = shbuf_init(); err = shfs_read(file, work_buff); if (err) goto done; base = shfs_rev_base(file); if (base) { /* obtain delta of current file data content against BASE revision's data content. */ head_buff = shbuf_init(); err = shfs_rev_ref_read(file, "tag", "BASE", head_buff); if (err) goto done; if (shbuf_size(work_buff) == shbuf_size(head_buff) && 0 == memcmp(shbuf_data(work_buff), shbuf_data(head_buff), shbuf_size(work_buff))) { /* no difference */ err = SHERR_AGAIN; goto done; } diff_buff = shbuf_init(); err = shdelta(work_buff, head_buff, diff_buff); shbuf_free(&head_buff); if (err) return (err); rev_key = shfs_token(base); } else { /* initial revision */ rev_key = ashkey_uniq(); } repo = shfs_inode(file, NULL, SHINODE_REPOSITORY); if (!repo) { err = SHERR_IO; goto done; } /* create a new revision using previous revision's inode name */ new_rev = shfs_inode(repo, (char *)shkey_hex(rev_key), SHINODE_REVISION); if (!new_rev) { err = SHERR_IO; goto done; } /* define revision's meta information. */ shfs_meta_set(new_rev, SHMETA_USER_NAME, (char *)get_libshare_account_name()); /* save delta to new revision */ err = shfs_rev_delta_write(new_rev, diff_buff); shbuf_free(&diff_buff); if (err) goto done; /* save new revision as BASE branch head */ err = shfs_rev_base_set(file, new_rev); if (err) goto done; /* save work-data to BASE tag. */ err = shfs_rev_ref_write(file, "tag", "BASE", work_buff); shbuf_free(&work_buff); if (err) goto done; if (base) { /* tag previous revision's key token onto revision inode. */ shfs_rev_tag(new_rev, "PREV", base); } if (rev_p) *rev_p = new_rev; done: shbuf_free(&work_buff); shbuf_free(&diff_buff); shbuf_free(&head_buff); return (err); }