Beispiel #1
0
JSValueRef function_import_script(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
		size_t argc, const JSValueRef args[], JSValueRef* exception) {
	if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) {
		JSStringRef path_str_ref = JSValueToStringCopy(ctx, args[0], NULL);
		assert(JSStringGetLength(path_str_ref) < 100);
		char tmp[100];
		tmp[0] = '\0';
		JSStringGetUTF8CString(path_str_ref, tmp, 100);
		JSStringRelease(path_str_ref);

		char *path = tmp;
		if (str_has_prefix(path, "goog/../") == 0) {
			path = path + 8;
		}

		char *source = NULL;
		if (out_path == NULL) {
			source = bundle_get_contents(path);
		} else {
			char *full_path = str_concat(out_path, path);
			source = get_contents(full_path, NULL);
			free(full_path);
		}

		if (source != NULL) {
			evaluate_script(ctx, source, path);
			free(source);
		}
	}

	return JSValueMakeUndefined(ctx);
}
Beispiel #2
0
dstring_t *dstring_new_from_url(const char *url)
{
    if(str_has_prefix(url, "http://"))
        return dstring_new_from_http(url);
    /* else if(str_has_prefix(url, "file://")) */
        /* return dstring_new_from_file(url + 7); */
    return NULL;
}
Beispiel #3
0
JSValueRef function_import_script(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
                                  size_t argc, const JSValueRef args[], JSValueRef *exception) {
    if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) {
        JSStringRef path_str_ref = JSValueToStringCopy(ctx, args[0], NULL);
        assert(JSStringGetLength(path_str_ref) < 100);
        char tmp[100];
        tmp[0] = '\0';
        JSStringGetUTF8CString(path_str_ref, tmp, 100);
        JSStringRelease(path_str_ref);

        bool can_skip_load = false;
        char *path = tmp;
        if (str_has_prefix(path, "goog/../") == 0) {
            path = path + 8;
        } else {
            unsigned long h = hash((unsigned char *) path);
            if (is_loaded(h)) {
                can_skip_load = true;
            } else {
                add_loaded_hash(h);
            }
        }

        if (!can_skip_load) {
            char *source = NULL;
            if (config.out_path == NULL) {
                source = bundle_get_contents(path);
            } else {
                char *full_path = str_concat(config.out_path, path);
                source = get_contents(full_path, NULL);
                free(full_path);
            }

            if (source != NULL) {
                evaluate_script(ctx, source, path);
                display_launch_timing(path);
                free(source);
            }
        }
    }

    return JSValueMakeUndefined(ctx);
}
Beispiel #4
0
void ev_dispatch_fire(ev_dispatch_t * dispatch,ev_event_t * event,InvokeTickDeclare){
    ev_dispatch_t * impl = (ev_dispatch_t *)dispatch, * parent = dispatch->parent;
    hint32 i,c;
    ev_dispatch_listener_t * listener;
    hserial_list_t lis =serial_list_alloc(sizeof(ev_dispatch_listener_t), 0);
    
    if(event->target == NULL){
        event->target = (ev_dispatch_t *)impl;
    }
    
    c = serial_list_count(impl->listeners);
    
    for(i=0;i<c;i++){
        listener = serial_list_item_at(impl->listeners, i);
        if(str_has_prefix(listener->name,event->name)){
            serial_list_item_add(lis, listener);
        }
    }
    
    c = serial_list_count(lis);
    
    for(i=0;i<c;i++){
        listener = serial_list_item_at(lis, i);
        if(! (* listener->callback)(dispatch,event,listener->context,InvokeTickArg)){
            break;
        }
    }
    
    serial_list_dealloc(lis);

    if(!event->cancelBubble && parent){
        ev_dispatch_fire(parent, event, InvokeTickArg);
    }
    
    
}
Beispiel #5
0
int main(int argc, char **argv) {
    const char *arg0 = argv[0];

    const char *libmalcheck_path = MALCHECK_LIBMALCHECKSO;
    char *child_exe_path = NULL;
    int fail_index = -1;

    char **child_argv = calloc(argc + 1, sizeof(char *));
    if (!child_argv) {
        fprintf(stderr, "failed to alloc mem for child argv\n");
        return EXIT_FAILURE;
    }

    for (int i = 1; i < argc; i += 1) {
        const char *arg = argv[i];
        if (arg[0] == '-' && arg[1] == '-') {
            i += 1;
            if (i >= argc) {
                return usage(arg0);
            } else {
                if (strcmp(arg, "--fail-index") == 0) {
                    fail_index = atoi(argv[i]);
                } else if (strcmp(arg, "--libmalcheck") == 0) {
                    libmalcheck_path = argv[i];
                }
            }
        } else if (!child_exe_path) {
            child_exe_path = argv[i];
            i += 1;
            for (int j = 1; i < argc; i += 1, j += 1)
                child_argv[j] = argv[i];
            break;
        }
    }

    if (!child_exe_path)
        return usage(arg0);

    int environ_item_count = 2;
    int libmalcheck_path_len = strlen(libmalcheck_path);
    int needed_environ_size = strlen("LD_PRELOAD=:") + libmalcheck_path_len +
        strlen("MALCHECK_FAIL_INDEX=99999999999999");
    for (char **ptr = environ; *ptr; ptr += 1) {
        needed_environ_size += strlen(*ptr) + 1;
        environ_item_count += 1;
    }
    int ptr_table_size = (environ_item_count + 1) * sizeof(char *);
    needed_environ_size += ptr_table_size;

    char *new_environ_bytes = malloc(needed_environ_size);
    if (!new_environ_bytes) {
        fprintf(stderr, "failed to alloc mem for environment\n");
        return EXIT_FAILURE;
    }

    char **new_environ_table = (char **)new_environ_bytes;
    char *new_environ_data = new_environ_bytes + ptr_table_size;
    bool found_item = false;
    int ld_preload_prefix_size = strlen("LD_PRELOAD=");
    for (char **ptr = environ; *ptr; ptr += 1) {
        char *old_item = *ptr;
        int old_item_len = strlen(old_item);

        *new_environ_table = new_environ_data;
        new_environ_table += 1;

        if (str_has_prefix(old_item, "LD_PRELOAD=", ld_preload_prefix_size)) {
            fprintf(stderr, "item: %s\n", old_item);
            found_item = true;

            new_environ_data += sprintf(new_environ_data, "LD_PRELOAD=%s:%s",
                    libmalcheck_path,
                    old_item + ld_preload_prefix_size) + 1;
        } else {
            memcpy(new_environ_data, old_item, old_item_len + 1);
            new_environ_data += old_item_len + 1;
        }
    }
    if (!found_item) {
        *new_environ_table = new_environ_data;
        new_environ_table += 1;

        new_environ_data += sprintf(new_environ_data, "LD_PRELOAD=%s", libmalcheck_path) + 1;
    }
    {
        *new_environ_table = new_environ_data;
        new_environ_table += 1;

        new_environ_data += sprintf(new_environ_data, "MALCHECK_FAIL_INDEX=%d", fail_index) + 1;
    }
    *new_environ_table = NULL;

    pid_t child_id = fork();

    if (child_id == -1) {
        fprintf(stderr, "fork failed: %s", strerror(errno));
        return EXIT_FAILURE;
    }
    if (child_id == 0) {
        child_argv[0] = child_exe_path;
        execvpe(child_exe_path, child_argv, (char**)new_environ_bytes);
        fprintf(stderr, "unable to spawn %s: %s\n", child_exe_path, strerror(errno));
        return EXIT_FAILURE;
    }
    free(new_environ_bytes);
    free(child_argv);

    int child_return_value;
    pid_t id = waitpid(child_id, &child_return_value, 0);
    if (id == -1) {
        fprintf(stderr, "waitpid failed: %s", strerror(errno));
        return EXIT_FAILURE;
    }

    if (child_return_value)
        return EXIT_FAILURE;

    return EXIT_SUCCESS;
}
Beispiel #6
0
static int decode_and_add_blocks(struct buffer *b, const unsigned char *buf, size_t size)
{
	const char *e = detect_encoding_from_bom(buf, size);
	struct file_decoder *dec;
	char *line;
	ssize_t len;

	if (b->encoding == NULL) {
		if (e) {
			// UTF-16BE/LE or UTF-32BE/LE
			b->encoding = xstrdup(e);
		}
	} else if (streq(b->encoding, "UTF-16")) {
		// BE or LE?
		if (e && str_has_prefix(e, b->encoding)) {
			free(b->encoding);
			b->encoding = xstrdup(e);
		} else {
			// "open -e UTF-16" but incompatible or no BOM.
			// Do what the user wants. Big-endian is default.
			free(b->encoding);
			b->encoding = xstrdup("UTF-16BE");
		}
	} else if (streq(b->encoding, "UTF-32")) {
		// BE or LE?
		if (e && str_has_prefix(e, b->encoding)) {
			free(b->encoding);
			b->encoding = xstrdup(e);
		} else {
			// "open -e UTF-32" but incompatible or no BOM.
			// Do what the user wants. Big-endian is default.
			free(b->encoding);
			b->encoding = xstrdup("UTF-32BE");
		}
	}

	// Skip BOM only if it matches the specified file encoding.
	if (b->encoding && e && streq(b->encoding, e)) {
		size_t bom_len = 2;
		if (str_has_prefix(e, "UTF-32"))
			bom_len = 4;
		buf += bom_len;
		size -= bom_len;
	}

	dec = new_file_decoder(b->encoding, buf, size);
	if (dec == NULL)
		return -1;

	if (file_decoder_read_line(dec, &line, &len)) {
		struct block *blk = NULL;

		if (len && line[len - 1] == '\r') {
			b->newline = NEWLINE_DOS;
			len--;
		}
		blk = add_utf8_line(b, blk, line, len);

		while (file_decoder_read_line(dec, &line, &len)) {
			if (b->newline == NEWLINE_DOS && len && line[len - 1] == '\r')
				len--;
			blk = add_utf8_line(b, blk, line, len);
		}
		if (blk)
			add_block(b, blk);
	}
	if (b->encoding == NULL) {
		e = dec->encoding;
		if (e == NULL)
			e = charset;
		b->encoding = xstrdup(e);
	}
	free_file_decoder(dec);
	return 0;
}