void database_delete(struct Connection *conn, int id) { struct Address addr = {.id=id, .set=0}; conn->db->rows[id] = addr; } void database_list(struct Connection *conn) { for(int i=0; i<MAX_ROWS; ++i) { struct Address *addr = &conn->db->rows[i]; if(addr->set) address_print(addr); } } int main(int argc, char *argv[]) { if(argc < 3) die("usage: a.out file action additional_params"); char *filename = argv[1]; char action = argv[2][0]; struct Connection *conn = database_open(filename, action); int id=0; if(argc > 3) id = atoi(argv[3]); if(id >= MAX_ROWS) die("record id too high"); switch (action) { case 'c': database_create(conn); database_write(conn); break; case 'g': if(argc!=4) die("need an id for database_get()"); database_get(conn,id); break; case 's': if(argc!=6) die("need id, name, email for database_set()"); database_set(conn,id,argv[4],argv[5]); database_write(conn); break; case 'd': if(argc!=4) die("need id to delete"); database_delete(conn,id); database_write(conn); break; case 'l': database_list(conn); break; default: die("invalid action: c=create, g=get, s=set, d=delete, l=list"); } database_close(conn); return 0; }
void database_push(struct request *r) { struct lnode *n = calloc(sizeof(struct lnode), 1); struct file_entry *fe = calloc(sizeof(struct file_entry), 1); hash(r->data, r->len, fe->hash); unsigned long long id = 0; if (exists(fe->hash, &id)){ free(fe); free(n); errno = EEXIST; r->id = id; return; } fe->data = r->data; fe->len = r->len; fe->id = next_id++; strcpy(fe->ext, r->ext); n->data = fe; pthread_mutex_lock(&lock); file_list = lnode_push(file_list, n); pthread_mutex_unlock(&lock); database_write(fe); r->id = fe->id; return; }
static int8_t do_query(char *query, char *address) { uint32_t ip; if(!inet_pton(AF_INET, address, &ip)) { return ERROR_INTERNAL; } char *name = strstr(query, "name="), *id = strstr(query, "id="), *c; uint32_t len; uint8_t name_length; if(name && id) { name += 5; id += 3; c = name; while(*c != '&'&& *c) { if(*c >= 'A' && *c <= 'Z') { *c = *c - 'A' + 'a'; c++; continue; } if(!((*c >= 'a' && *c <= 'z') || (*c >= '0' && *c <= '9'))) { return INVALID_CHARS_NAME; } c++; } len = c - name; if(len >= MAX_NAME_LENGTH) { len = MAX_NAME_LENGTH; } name_length = len; c = id; while(*c != '&'&& *c){c++;} len = c - id; if(len != TOX_ID_SIZE * 2) { return INVALID_LENGTH; } uint8_t _id[TOX_ID_SIZE]; if(!string_to_id(_id, (uint8_t*)id)) { return INVALID_CHARS; } if(!validate_id(_id)) { return INVALID_CHECKSUM; } return database_write(_id, (uint8_t*)name, name_length, ip); } else { return 1; } }
unsigned long long database_push(char *data, size_t len) { struct lnode *n = calloc(sizeof(struct lnode), 1); struct file_entry *fe = calloc(sizeof(struct file_entry), 1); pthread_mutex_lock(&lock); fe->len = len; fe->id = next_id++; n->data = fe; file_list = lnode_push(file_list, n); pthread_mutex_unlock(&lock); database_write(fe, data); cache_push(data, len, fe->id); return fe->id; }
void database_create(struct Connection *conn) { int i = 0; for(i = 0; i < MAX_ROWS; i++) { // make a prototype to initialize the database struct Address addr = {.id = i, .set = 0}; // then assign it to conn conn->db->rows[i] = addr; } printf("got to bottom of database create"); } void database_set(struct Connection *conn, int id, const char *name, const char *email) { struct Address *addr = &conn->db->rows[id]; if(addr->set) die("Already set, delete it first"); addr->set = 1; // WARNING FIX THIS char *res = strncpy(addr->name, name, MAX_DATA); // demonstrate that strncpy bug if(!res) die("Name copy failed"); res = strncpy(addr->email, email, MAX_DATA); if(!res) die("Email copy failed"); } void database_get(struct Connection *conn, int id) { struct Address *addr = &conn->db->rows[id]; if(addr->set) { address_print(addr); } else { die("ID is not set"); } } void database_delete(struct Connection *conn, int id) { struct Address addr = {.id = id, .set = 0}; conn->db->rows[id] = addr; } void database_list(struct Connection *conn) { int i = 0; struct Database *db = conn->db; for(i = 0; i < MAX_ROWS; i++) { struct Address *cur = &db->rows[i]; if(cur->set) { address_print(cur); } } } int main(int argc, char *argv[]) { if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]"); char *filename = argv[1]; char action = argv[2][0]; struct Connection *conn = database_open(filename, action); int id = 0; if(argc > 3) id = atoi(argv[3]); if(id >= MAX_ROWS) die("There's not that many records."); switch(action) { case 'c': database_create(conn); database_write(conn); break; case 'g': if(argc != 4) die("Need an id to get"); database_get(conn, id); break; case 's': if(argc != 6) die("Need id, name, email to set"); database_set(conn, id, argv[4], argv[5]); database_write(conn); break; case 'd': if(argc != 4) die("Need id to delete"); database_delete(conn, id); database_write(conn); break; case 'l': database_list(conn); break; default: die("Invalid action, only: c=create, g=get, s=set, d=del, l=list"); } database_close(conn); return 0; }
/* get_compiler_info */ int get_compiler_info(void) { Dwarf_Unsigned cu_header_length, abbrev_offset, next_cu_header; Dwarf_Half version, address_size; Dwarf_Signed attrcount, i, language; Dwarf_Die no_die = 0, cu_die; Dwarf_Attribute *attrs; Dwarf_Half attrcode; Dwarf_Debug dbg = 0; Dwarf_Error err; char *compiler = NULL; int fd = -1; OUTPUT_VERBOSE((5, "%s", _BLUE("Extracting DWARF info"))); /* Open the binary */ if (0 > (fd = open(globals.program_full, O_RDONLY))) { OUTPUT(("%s [%s]", _ERROR("opening program binary"), globals.program_full)); return PERFEXPERT_ERROR; } /* Initialize DWARF library */ if (DW_DLV_OK != dwarf_init(fd, DW_DLC_READ, 0, 0, &dbg, &err)) { OUTPUT(("%s", _ERROR("failed DWARF initialization"))); return PERFEXPERT_ERROR; } /* Find compilation unit header */ if (DW_DLV_ERROR == dwarf_next_cu_header(dbg, &cu_header_length, &version, &abbrev_offset, &address_size, &next_cu_header, &err)) { OUTPUT(("%s", _ERROR("reading DWARF CU header"))); return PERFEXPERT_ERROR; } /* Expect the CU to have a single sibling, a DIE */ if (DW_DLV_ERROR == dwarf_siblingof(dbg, no_die, &cu_die, &err)) { OUTPUT(("%s", _ERROR("getting sibling of CU"))); return PERFEXPERT_ERROR; } /* Find the DIEs attributes */ if (DW_DLV_OK != dwarf_attrlist(cu_die, &attrs, &attrcount, &err)) { OUTPUT(("%s", _ERROR("in dwarf_attlist"))); return PERFEXPERT_ERROR; } /* For each attribute... */ for (i = 0; i < attrcount; ++i) { if (DW_DLV_OK != dwarf_whatattr(attrs[i], &attrcode, &err)) { OUTPUT(("%s [%s]", _ERROR("in dwarf_whatattr"))); return PERFEXPERT_ERROR; } if (DW_AT_producer == attrcode) { if (DW_DLV_OK != dwarf_formstring(attrs[i], &compiler, &err)) { OUTPUT(("%s [%s]", _ERROR("in dwarf_formstring"))); return PERFEXPERT_ERROR; } else { OUTPUT_VERBOSE((5, " Compiler: %s", _CYAN(compiler))); } } if (DW_AT_language == attrcode) { if (DW_DLV_OK != dwarf_formsdata(attrs[i], &language, &err)) { OUTPUT(("%s [%s]", _ERROR("in dwarf_formsdata"))); return PERFEXPERT_ERROR; } else { OUTPUT_VERBOSE((5, " Language: %d", language)); } } } if (PERFEXPERT_SUCCESS != database_write(compiler, language)) { OUTPUT(("%s", _ERROR("writing to database"))); return PERFEXPERT_ERROR; } /* Finalize DWARF library */ if (DW_DLV_OK != dwarf_finish(dbg, &err)) { OUTPUT(("%s", _ERROR("failed DWARF finalization"))); return PERFEXPERT_ERROR; } close(fd); return PERFEXPERT_SUCCESS; }