/* ghtml_set_proxy */ int ghtml_set_proxy(GtkWidget * widget, SurferProxyType type, char const * http, unsigned int http_port) { #if WEBKIT_CHECK_VERSION(1, 1, 0) SoupSession * session; char buf[32]; struct hostent * he; struct in_addr addr; SoupURI * uri = NULL; session = webkit_get_default_session(); if(type == SPT_HTTP && http != NULL && strlen(http) > 0) { if((he = gethostbyname(http)) == NULL) return -error_set_code(1, "%s: %s", http, hstrerror( h_errno)); memcpy(&addr.s_addr, he->h_addr, sizeof(addr.s_addr)); snprintf(buf, sizeof(buf), "http://%s:%u/", inet_ntoa(addr), http_port); uri = soup_uri_new(buf); } g_object_set(session, "proxy-uri", uri, NULL); return 0; #else /* FIXME really implement */ return -error_set_code(1, "%s", strerror(ENOSYS)); #endif }
/* code_variable_add */ int code_variable_add(Code * code, char const * name) { CodeScope * scope; CodeVariable * p; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, name); #endif /* check if the name is valid */ if(name == NULL || name[0] == '\0') return error_set_code(1, "%s", "Invalid name for a variable"); /* resize the current scope */ scope = &code->scopes[code->scopes_cnt - 1]; if((p = realloc(scope->variables, sizeof(*p) * (scope->variables_cnt + 1))) == NULL) return error_set_code(1, "%s", strerror(errno)); scope->variables = p; /* assign the variable */ p = &scope->variables[scope->variables_cnt]; p->name = strdup(name); p->type = NULL; /* FIXME implement */ if(p->name == NULL) return error_set_code(1, "%s", strerror(errno)); scope->variables_cnt++; return 0; }
/* arch_seek_buffer */ static off_t _arch_seek_buffer(AsmArch * arch, off_t offset, int whence) { #ifdef DEBUG fprintf(stderr, "DEBUG: %s(arch, %ld, %d)\n", __func__, offset, whence); #endif if(whence == SEEK_SET) { if(offset < 0 || (size_t)offset >= arch->buffer_cnt) return -error_set_code(1, "%s", "Invalid seek"); arch->buffer_pos = offset; } else if(whence == SEEK_CUR) { if(offset < 0) { if((size_t)(-offset) > arch->buffer_pos) return -error_set_code(1, "%s", "Invalid seek"); } else if(offset > 0) { if((size_t)offset + arch->buffer_pos >= arch->buffer_cnt) return -error_set_code(1, "%s", "Invalid seek"); } arch->buffer_pos += offset; } else /* FIXME implement */ return -error_set_code(1, "%s", "Not implemented"); return arch->buffer_pos; }
static int _x509_from_request(char const * filename, Buffer * csr, Buffer * x509) { FILE * fp; int fd[2]; pid_t pid; if((fp = fopen(filename, "w")) == NULL) return error_set_code(1, "%s%s%s", filename, strerror(errno)); if(fwrite(buffer_get_data(csr), sizeof(char), buffer_get_size(csr), fp) != buffer_get_size(csr)) { fclose(fp); return error_set_code(1, "%s%s%s", filename, strerror(errno)); } if(fclose(fp) != 0) return error_set_code(1, "%s%s%s", filename, strerror(errno)); if(pipe(fd) != 0) return error_set_code(1, "%s%s", "pipe: ", strerror(errno)); if((pid = fork()) == -1) { close(fd[0]); close(fd[1]); return error_set_code(1, "%s%s", "fork: ", strerror(errno)); } if(pid == 0) { close(fd[0]); _request_child(filename, fd[1]); exit(0); } close(fd[1]); return _request_parent(x509, pid, fd[0]); }
/* code_context_set_storage */ int code_context_set_storage(Code * code, CodeStorage storage) { struct { CodeStorage storage; char const * name; } sn[CODE_STORAGE_COUNT] = { { CODE_STORAGE_NULL, "NULL" }, { CODE_STORAGE_TYPEDEF, "typedef" }, { CODE_STORAGE_EXTERN, "extern" }, { CODE_STORAGE_STATIC, "static" }, { CODE_STORAGE_AUTO, "auto" }, { CODE_STORAGE_REGISTER,"register" } }; size_t i; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(0x%x)\n", __func__, storage); #endif if(!(code->storage & storage)) { code->storage |= storage; return 0; } for(i = 0; i < CODE_STORAGE_COUNT; i++) if(sn[i].storage == storage) return error_set_code(1, "%s\"%s\"", "Duplicate ", sn[i].name); return error_set_code(1, "%s", "Duplicate storage class specifier"); }
/* section_name */ static int _section_name(State * state) /* WORD */ { int ret = 0; char const * string = NULL; size_t len; char * section = NULL; #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif if(state->token == NULL || (string = token_get_string(state->token)) == NULL || (len = strlen(token_get_string(state->token))) == 0) return error_set_code(1, "%s", "Sections with empty names are not allowed"); if((section = malloc(len + 2)) == NULL) return ret | error_set_code(1, "%s", strerror(errno)); snprintf(section, len + 2, ".%s", string); ret |= _parser_scan(state); #ifdef DEBUG fprintf(stderr, "%s\"%s\"\n", "DEBUG: section ", section); #endif if(asmcode_section(state->code, section) != 0) ret |= _parser_error(state, "%s", error_get()); free(section); return ret; }
/* code_type_add */ int code_type_add(Code * code, char const * name) /* FIXME consider using a Token instead of the name */ { size_t i; CodeType * p; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, name); #endif if(name == NULL || name[0] == '\0') return -error_set_code(1, "%s", "Invalid name for a type"); for(i = 0; i < code->types_cnt; i++) { p = &code->types[i]; if(strcmp(p->name, name) != 0) continue; return -error_set_code(1, "%s%s", name, " is already defined"); } if((p = realloc(code->types, sizeof(*p) * (i + 1))) == NULL) return -error_set_code(1, "%s", strerror(errno)); code->types = p; code->types[i].name = strdup(name); if(code->types[i].name == NULL) { free(code->types[i].name); return -error_set_code(1, "%s", strerror(errno)); } return code->types_cnt++; }
/* ssl_client_send */ static int _ssl_client_send(SSLTransport * ssl, AppTransportClient * client, AppMessage * message) { size_t i; SSLSocket * s; Buffer * buffer; if(ssl->mode != ATM_SERVER) return -error_set_code(1, "%s", "Not a server"); /* lookup the client */ for(i = 0; i < ssl->u.server.clients_cnt; i++) { s = ssl->u.server.clients[i]; if(s->client == client) break; } if(i == ssl->u.server.clients_cnt) return -error_set_code(1, "%s", "Unknown client"); /* send the message */ if((buffer = buffer_new(0, NULL)) == NULL) return -1; if(appmessage_serialize(message, buffer) != 0 || _ssl_socket_queue(s, buffer) != 0) { buffer_delete(buffer); return -1; } return 0; }
/* arch_decode_at */ int arch_decode_at(AsmArch * arch, AsmCode * code, off_t offset, size_t size, off_t base, AsmArchInstructionCall ** calls, size_t * calls_cnt) { int ret; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%ld, %lu, %ld)\n", __func__, offset, size, base); #endif /* FIXME this only works for files */ if(arch->fp == NULL) return -error_set_code(1, "%s", strerror(ENOSYS)); if(fseek(arch->fp, offset, SEEK_SET) != 0) return -error_set_code(1, "%s", strerror(errno)); if(size == 0) return 0; arch->code = code; arch->buffer_pos = offset; arch->buffer_cnt = offset + size; if((ret = arch_decode(arch, code, base, calls, calls_cnt)) == 0 && fseek(arch->fp, offset + size, SEEK_SET) != 0) { free(*calls); /* XXX the pointer was updated anyway... */ ret = -error_set_code(1, "%s", strerror(errno)); } return ret; }
/* ssl_callback_connect */ static int _ssl_callback_connect(int fd, SSLTransport * transport) { int res; socklen_t s = sizeof(res); #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%d)\n", __func__, fd); #endif /* check parameters */ if(transport->u.client.fd != fd) return -1; /* obtain the connection status */ if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &res, &s) != 0) { error_set_code(1, "%s: %s", "getsockopt", strerror(errno)); SSL_free(transport->u.client.ssl); transport->u.client.ssl = NULL; close(fd); transport->u.client.fd = -1; /* FIXME report error */ #ifdef DEBUG fprintf(stderr, "DEBUG: %s() %s\n", __func__, strerror(errno)); #endif return -1; } if(res != 0) { /* the connection failed */ error_set_code(1, "%s: %s", "connect", strerror(res)); SSL_free(transport->u.client.ssl); transport->u.client.ssl = NULL; close(fd); transport->u.client.fd = -1; /* FIXME report error */ #ifdef DEBUG fprintf(stderr, "DEBUG: %s() %s\n", __func__, strerror(res)); #endif return -1; } SSL_set_connect_state(transport->u.client.ssl); /* listen for any incoming message */ event_register_io_read(transport->helper->event, fd, (EventIOFunc)_ssl_socket_callback_read, &transport->u.client); /* write pending messages if any */ if(transport->u.client.bufout_cnt > 0) { event_register_io_write(transport->helper->event, fd, (EventIOFunc)_ssl_socket_callback_write, &transport->u.client); return 0; } return 1; }
/* format_helper_read */ static ssize_t _format_helper_read(AsmFormat * format, void * buf, size_t size) { if(fread(buf, size, 1, format->fp) == 1) return size; if(ferror(format->fp)) return -error_set_code(1, "%s: %s", format->filename, strerror(errno)); if(feof(format->fp)) return -error_set_code(1, "%s: %s", format->filename, "End of file reached"); return -error_set_code(1, "%s: %s", format->filename, "Read error"); }
/* arch_write */ static ssize_t _arch_write(AsmArch * arch, void const * buf, size_t size) { if(fwrite(buf, size, 1, arch->fp) == 1) return size; if(ferror(arch->fp)) return -error_set_code(1, "%s: %s", arch->filename, strerror(errno)); if(feof(arch->fp)) return -error_set_code(1, "%s: %s", arch->filename, "End of file reached"); return -error_set_code(1, "%s: %s", arch->filename, "Write error"); }
static SSLTransport * _ssl_init(AppTransportPluginHelper * helper, AppTransportMode mode, char const * name) { SSLTransport * ssl; int res; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%u, \"%s\")\n", __func__, mode, name); #endif if((ssl = object_new(sizeof(*ssl))) == NULL) return NULL; memset(ssl, 0, sizeof(*ssl)); ssl->helper = helper; if((ssl->ssl_ctx = SSL_CTX_new(TLSv1_method())) == NULL || SSL_CTX_set_cipher_list(ssl->ssl_ctx, SSL_DEFAULT_CIPHER_LIST) != 1 || SSL_CTX_load_verify_locations(ssl->ssl_ctx, NULL, "/etc/openssl/certs") != 1) /* FIXME report the underlying error */ res = -error_set_code(1, "Could not initialize SSL"); else switch((ssl->mode = mode)) { case ATM_CLIENT: res = _init_client(ssl, name); break; case ATM_SERVER: res = _init_server(ssl, name); break; default: res = -error_set_code(1, "Unknown transport mode"); break; } /* check for errors */ if(res != 0) { #ifdef DEBUG fprintf(stderr, "DEBUG: %s() => %d (%s)\n", __func__, res, error_get(NULL)); #endif _ssl_destroy(ssl); return NULL; } #if 0 /* XXX may be useful */ SSL_CTX_set_mode(ssl->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE); #endif return ssl; }
/* cpp_path_add */ int cpp_path_add(Cpp * cpp, char const * path) { char ** p; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(cpp, \"%s\")\n", __func__, path); #endif if((p = realloc(cpp->paths, sizeof(*p) * (cpp->paths_cnt + 1))) == NULL) return -error_set_code(1, "%s", strerror(errno)); cpp->paths = p; if((p[cpp->paths_cnt] = strdup(path)) == NULL) return -error_set_code(1, "%s", strerror(errno)); cpp->paths_cnt++; return 0; }
static int _document_load_file(GHtml * ghtml, char const * filename) { FILE * fp; char buf[BUFSIZ]; size_t size; if((fp = fopen(filename, "r")) == NULL) return -error_set_code(1, "%s: %s", filename, strerror(errno)); while((size = fread(buf, sizeof(*buf), sizeof(buf), fp)) > 0) if(_document_load_write(ghtml, buf, size) != 0) return -error_set_code(1, "%s: %s", filename, strerror(errno)); fclose(fp); return _document_load_write_close(ghtml); }
/* variable_new_copy */ Variable * variable_new_copy(Variable * variable) { switch(variable->type) { case VT_NULL: case VT_BOOL: case VT_INT8: case VT_UINT8: case VT_INT16: case VT_UINT16: case VT_INT32: case VT_UINT32: case VT_INT64: case VT_UINT64: case VT_FLOAT: case VT_DOUBLE: return variable_new(variable->type, &variable->u); case VT_STRING: return variable_new(variable->type, variable->u.string); case VT_BUFFER: return variable_new(variable->type, variable->u.buffer); } error_set_code(1, "%s", "Unable to copy this type of variable"); return NULL; }
/* format_match */ int format_match(AsmFormat * format) { int ret = 0; char const * s = format->definition->signature; ssize_t s_len = format->definition->signature_len; char * buf = NULL; #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif if(s_len > 0) if((buf = malloc(s_len)) == NULL) ret = -error_set_code(1, "%s", strerror(errno)); if(buf != NULL) { if(_format_helper_seek(format, 0, SEEK_SET) != 0) ret = -1; else if(_format_helper_read(format, buf, s_len) != s_len) ret = -1; else if(memcmp(buf, s, s_len) == 0) ret = 1; free(buf); } return ret; }
/* parser */ int parser(AsmPrefs * ap, AsmCode * code, char const * infile) { CppPrefs prefs; State state; memset(&prefs, 0, sizeof(prefs)); prefs.filename = infile; prefs.filters = CPP_FILTER_COMMENT; memset(&state, 0, sizeof(state)); state.code = code; if((state.cpp = cpp_new(&prefs)) == NULL) return -1; if(_parser_defines(&state, ap) != 0) { cpp_delete(state.cpp); return -1; } if(_parser_scan(&state) != 0) { cpp_delete(state.cpp); return _parser_error(&state, "%s", error_get()); } if(_program(&state) != 0) error_set_code(1, "%s%s%u%s%u%s", infile, ": Compilation failed with ", state.error_cnt, " error(s) and ", state.warning_cnt, " warning(s)"); if(state.token != NULL) token_delete(state.token); return state.error_cnt; }
/* elf_detect */ static char const * _elf_detect(AsmFormatPlugin * format) { AsmFormatPluginHelper * helper = format->helper; char const * ret; union { Elf32_Ehdr ehdr32; Elf64_Ehdr ehdr64; } ehdr; if(helper->seek(helper->format, 0, SEEK_SET) != 0) return NULL; if(helper->read(helper->format, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) return NULL; switch(ehdr.ehdr32.e_ident[EI_CLASS]) { case ELFCLASS32: if((ret = elf32_detect(format, &ehdr.ehdr32)) != NULL) format->decode = elf32_decode; break; case ELFCLASS64: if((ret = elf64_detect(format, &ehdr.ehdr64)) != NULL) format->decode = elf64_decode; break; default: ret = NULL; error_set_code(1, "%s: %s 0x%x\n", helper->get_filename(helper->format), "Unsupported ELF class", ehdr.ehdr32.e_ident[EI_CLASS]); break; } return ret; }
/* ghtml_set_user_agent */ int ghtml_set_user_agent(GtkWidget * ghtml, char const * user_agent) { if(user_agent == NULL) return 0; /* FIXME really implement */ return -error_set_code(1, "%s", strerror(ENOSYS));; }
/* _sqlite2_init */ static SQLite2 * _sqlite2_init(Config * config, char const * section) { SQLite2 * sqlite; char const * name; char * error = NULL; if((sqlite = object_new(sizeof(*sqlite))) == NULL) return NULL; sqlite->handle = NULL; if((name = config_get(config, section, "filename")) != NULL && (sqlite->handle = sqlite_open(name, 0, &error)) == NULL) { error_set_code(1, "%s: %s", name, (error != NULL) ? error : "Unknown error"); free(error); } /* check for errors */ if(sqlite->handle == NULL) { _sqlite2_destroy(sqlite); return NULL; } return sqlite; }
/* parser_string */ int parser_string(AsmPrefs * ap, AsmCode * code, char const * string) { CppPrefs prefs; State state; size_t i; memset(&prefs, 0, sizeof(prefs)); #if 0 prefs.filename = infile; #endif prefs.filters = CPP_FILTER_COMMENT; memset(&state, 0, sizeof(state)); state.code = code; if((state.cpp = cpp_new(&prefs)) == NULL) return -1; if(ap != NULL) for(i = 0; i < ap->defines_cnt; i++) /* FIXME check errors */ cpp_define_add(state.cpp, ap->defines[i], NULL); if(_parser_scan(&state) != 0) return _parser_error(&state, "%s", error_get()); if(_program(&state) != 0) error_set_code(1, "%s%u%s%u%s", "Compilation failed with ", state.error_cnt, " error(s) and ", state.warning_cnt, " warning(s)"); if(state.token != NULL) token_delete(state.token); return state.error_cnt; }
static AppMessage * _new_deserialize_id(AppMessage * message, char const * data, const size_t size, size_t * pos) { int ret = 0; size_t s = size; Variable * v; #ifdef DEBUG fprintf(stderr, "DEBUG: %s()\n", __func__); #endif if((v = variable_new_deserialize_type(VT_UINT32, &s, &data[*pos])) == NULL) { error_set_code(1, "%s", "Could not obtain the AppMessage ID"); appmessage_delete(message); return NULL; } ret = variable_get_as(v, VT_UINT32, &message->id); variable_delete(v); if(ret != 0) { appmessage_delete(message); return NULL; } *pos += s; return message; }
/* panel_window_append */ int panel_window_append(PanelWindow * panel, char const * applet) { PanelAppletHelper * helper = panel->helper; PanelApplet * pa; if((pa = realloc(panel->applets, sizeof(*pa) * (panel->applets_cnt + 1))) == NULL) return -error_set_code(1, "%s", strerror(errno)); panel->applets = pa; pa = &panel->applets[panel->applets_cnt]; if((pa->plugin = plugin_new(LIBDIR, PACKAGE, "applets", applet)) == NULL) return -1; pa->widget = NULL; if((pa->pad = plugin_lookup(pa->plugin, "applet")) == NULL || (pa->pa = pa->pad->init(helper, &pa->widget)) == NULL || pa->widget == NULL) { if(pa->pa != NULL) pa->pad->destroy(pa->pa); plugin_delete(pa->plugin); return -1; } gtk_box_pack_start(GTK_BOX(panel->box), pa->widget, pa->pad->expand, pa->pad->fill, 0); gtk_widget_show_all(pa->widget); panel->applets_cnt++; return 0; }
static int _decode64_strtab(AsmFormatPlugin * format, Elf64_Shdr * shdr, size_t shdr_cnt, uint16_t ndx, char ** strtab, size_t * strtab_cnt) { AsmFormatPluginHelper * helper = format->helper; ssize_t size; if(ndx >= shdr_cnt) return -error_set_code(1, "%s: %s", helper->get_filename(helper->format), "Unable to read the string table"); shdr = &shdr[ndx]; if(helper->seek(helper->format, shdr->sh_offset, SEEK_SET) < 0) return -1; size = sizeof(**strtab) * shdr->sh_size; if((*strtab = malloc(size)) == NULL) return -_elf_error(format); if(helper->read(helper->format, *strtab, size) != size) { free(*strtab); return -1; } *strtab_cnt = shdr->sh_size; return 0; }
static int _decode64_shdr(AsmFormatPlugin * format, Elf64_Ehdr * ehdr, Elf64_Shdr ** shdr) { AsmFormatPluginHelper * helper = format->helper; ssize_t size; size_t i; if(ehdr->e_shentsize == 0) { *shdr = NULL; return 0; } if(ehdr->e_shentsize != sizeof(**shdr)) return -error_set_code(1, "%s: %s", helper->get_filename(helper->format), "Invalid section header size"); if(helper->seek(helper->format, ehdr->e_shoff, SEEK_SET) < 0) return -1; size = sizeof(**shdr) * ehdr->e_shnum; if((*shdr = malloc(size)) == NULL) return -_elf_error(format); if(helper->read(helper->format, *shdr, size) != size) { free(*shdr); return -1; } if(ehdr->e_ident[EI_DATA] != elf_arch_native->endian) for(i = 0; i < ehdr->e_shnum; i++) _swap_64_shdr(*shdr + i); return 0; }
/* ssl_error */ static int _ssl_error(char const * message, int code) { #ifdef DEBUG fprintf(stderr, "DEBUG: %s(\"%s\", %d)\n", __func__, message, code); #endif return error_set_code(code, "%s%s%s", (message != NULL) ? message : "", (message != NULL) ? ": " : "", strerror(errno)); }
static int _code_context_queue_identifier(Code * code, char const * identifier) { CodeIdentifier * p; if((p = realloc(code->identifiers, sizeof(*p) * (code->identifiers_cnt + 1))) == NULL) return error_set_code(1, "%s", strerror(errno)); code->identifiers = p; /* initialize identifier */ p = &code->identifiers[code->identifiers_cnt]; p->context = code->context; p->storage = code->storage; if((p->name = strdup(identifier)) == NULL) return error_set_code(1, "%s", strerror(errno)); code->identifiers_cnt++; return 0; }
/* java_encode */ static int _java_encode(AsmArchPlugin * plugin, AsmArchInstruction const * instruction, AsmArchInstructionCall * call) { AsmArchPluginHelper * helper = plugin->helper; size_t i; AsmArchOperandDefinition definitions[3]; AsmArchOperand * ao; uint8_t u8; uint16_t u16; uint32_t u32; if((helper->write(helper->arch, &instruction->opcode, 1)) != 1) return -1; /* FIXME tableswitch may need some padding */ definitions[0] = instruction->op1; definitions[1] = instruction->op2; definitions[2] = instruction->op3; for(i = 0; i < call->operands_cnt; i++) { ao = &call->operands[i]; if(AO_GET_TYPE(ao->definition) != AOT_IMMEDIATE) return -error_set_code(1, "%s", "Not implemented"); if(AO_GET_SIZE(definitions[i]) == 8) { u8 = ao->value.immediate.value; if(helper->write(helper->arch, &u8, 1) != 1) return -1; } else if(AO_GET_SIZE(definitions[i]) == 16) { u16 = _htob16(ao->value.immediate.value); if(helper->write(helper->arch, &u16, 2) != 2) return -1; } else if(AO_GET_SIZE(definitions[i]) == 32) { u32 = _htob32(ao->value.immediate.value); if(helper->write(helper->arch, &u32, 4) != 4) return -1; } else return -error_set_code(1, "%s", "Size not implemented"); } return 0; }
/* ghtml_set_proxy */ int ghtml_set_proxy(GtkWidget * ghtml, SurferProxyType type, char const * http, uint16_t http_port) { if(type == SPT_NONE) return 0; /* FIXME really implement */ return -error_set_code(1, "%s", strerror(ENOSYS));; }