Ejemplo n.º 1
0
// This function takes a small screenshot at the specified position, checks for
// the magic pattern, and then fills in the measurement struct with data
// decoded from the pixels of the pattern. Returns true if successful, false
// if the screenshot failed or the magic pattern was not present.
static bool read_data_from_screen(uint32_t x, uint32_t y,
  const uint8_t magic_pattern[], measurement_t *out) {
  assert(out);
  screenshot *screenshot = take_screenshot(x, y, pattern_pixels, 1);
  if (!screenshot) {
    return false;
  }
  if (screenshot->width != pattern_pixels) {
    free_screenshot(screenshot);
    return false;
  }
  // Check that the magic pattern is there, starting at the first pixel.
  size_t found_x, found_y;
  if (!find_pattern(magic_pattern, screenshot, &found_x, &found_y) ||
    found_x || found_y) {
    free_screenshot(screenshot);
    return false;
  }
  out->javascript_frames = screenshot->pixels[pattern_magic_pixels * 4 + 0];
  out->key_down_events = screenshot->pixels[pattern_magic_pixels * 4 + 1];
  out->test_mode = (test_mode_t) screenshot->pixels[pattern_magic_pixels * 4 + 2];
  out->scroll_position = screenshot->pixels[(pattern_magic_pixels + 1) * 4];
  out->css_frames = screenshot->pixels[(pattern_magic_pixels + 2) * 4];
  out->screenshot_time = screenshot->time_nanoseconds;
  free_screenshot(screenshot);
  debug_log("javascript frames: %d, javascript events: %d, scroll position: %d"
      ", css frames: %d, test mode: %d", out->javascript_frames,
      out->key_down_events, out->scroll_position, out->css_frames,
      out->test_mode);
  return true;
}
Ejemplo n.º 2
0
static int SI_GetZ80 (void) {
	int offset=0;

	offset = find_pattern(SI_Z80_Block, 0x10);
	if (offset) SI_CurrentVer = 0;
	else SI_CurrentVer = SI_INVALID_VER;

	return offset;
}
Ejemplo n.º 3
0
bool Cgiter_get_local_bp_dist(const Cgiter* cgiter, Tstamp* dist)
{
    rassert(cgiter != NULL);
    rassert(dist != NULL);
    rassert(Tstamp_cmp(dist, TSTAMP_AUTO) >= 0);

    if (Cgiter_has_finished(cgiter))
        return false;

    const Pattern* pattern = find_pattern(cgiter);
    if (pattern == NULL)
        return false;

    // Check pattern end
    const Tstamp* pat_length = Pattern_get_length(pattern);
    const Tstamp* dist_to_end =
        Tstamp_sub(TSTAMP_AUTO, pat_length, &cgiter->pos.pat_pos);

    if (Tstamp_cmp(dist_to_end, TSTAMP_AUTO) <= 0)
    {
        // We cannot move forwards in playback time
        Tstamp_set(dist, 0, 0);
        return true;
    }

    // Check next trigger row
    Column* column = Pattern_get_column(pattern, cgiter->col_index);
    rassert(column != NULL);
    Column_iter* citer = Column_iter_init(COLUMN_ITER_AUTO);
    Column_iter_change_col(citer, column);

    const Tstamp* epsilon = Tstamp_set(TSTAMP_AUTO, 0, 1);
    Tstamp* next_pos_min = Tstamp_add(TSTAMP_AUTO, &cgiter->pos.pat_pos, epsilon);
    const Trigger_list* row = Column_iter_get_row(citer, next_pos_min);

    if (row != NULL)
    {
        rassert(row->next != NULL);
        rassert(row->next->trigger != NULL);
        if (Tstamp_cmp(&row->next->trigger->pos, pat_length) <= 0)
        {
            // Trigger row found inside this pattern
            const Tstamp* dist_to_row = Tstamp_sub(
                    TSTAMP_AUTO,
                    &row->next->trigger->pos,
                    &cgiter->pos.pat_pos);
            Tstamp_mina(dist, dist_to_row);
            return true;
        }
    }

    // No trigger row found
    Tstamp_mina(dist, dist_to_end);
    return true;
}
Ejemplo n.º 4
0
/*
 * This function patch the padRead calls
 */
int patch_padRead(void)
{
	u32 *ptr = NULL;
	u32 memscope, inst, fncall;
	u32 pattern[1], mask[1];
	u32 start = 0x00100000;
	int pattern_found = 0;
	const pattern_t *pat;

	GS_BGCOLOUR = 0x800080; /* Purple while padRead pattern search */

	memscope = 0x01f00000 - start;

	/* First try to locate the orginal libpad's scePadRead function */
	pat = _padread_patterns;
	while (pat->seq) {
		ptr = find_pattern((u32*)start, memscope, pat);
		if (ptr) {
			scePadRead_style = pat->tag; /* tag tells the version */
			break;
		}
		pat++;
	}

	if (!ptr) {
		GS_BGCOLOUR = 0x808080; /* Gray, pattern not found */
		return 0;
	}

 	GS_BGCOLOUR = 0x008000; /* Green while padRead patches */

	/* Save original scePadRead ptr */
	if (scePadRead_style == 2)
		scePad2Read = (void *)ptr;
	else
		scePadRead = (void *)ptr;

	/* Retrieve scePadRead call Instruction code */
	inst = 0x0c000000;
	inst |= 0x03ffffff & ((u32)ptr >> 2);

	/* Make pattern with function call code saved above */
	pattern[0] = inst;
	mask[0] = 0xffffffff;

	/* Get Hook_scePadRead call Instruction code */
	if (scePadRead_style == 2) {
		inst = 0x0c000000;
		inst |= 0x03ffffff & ((u32)Hook_scePad2Read >> 2);
	}
Ejemplo n.º 5
0
/**
 * Finds all occurrences of a given string in the html box tree
 *
 * \param pattern   the string pattern to search for
 * \param p_len     pattern length
 * \param cur       pointer to the current box
 * \param case_sens whether to perform a case sensitive search
 * \return true on success, false on memory allocation failure
 */
static bool find_occurrences_html(const char *pattern, int p_len,
		struct box *cur, bool case_sens,
		struct search_context *context)
{
	struct box *a;

	/* ignore this box, if there's no visible text */
	if (!cur->object && cur->text) {
		const char *text = cur->text;
		unsigned length = cur->length;

		while (length > 0) {
			struct list_entry *entry;
			unsigned match_length;
			unsigned match_offset;
			const char *new_text;
			const char *pos = find_pattern(text, length,
					pattern, p_len, case_sens,
					&match_length);
			if (!pos)
				break;

			/* found string in box => add to list */
			match_offset = pos - cur->text;

			entry = add_entry(cur->byte_offset + match_offset,
						cur->byte_offset +
							match_offset +
							match_length, context);
			if (!entry)
				return false;

			entry->start_box = cur;
			entry->end_box = cur;

			new_text = pos + match_length;
			length -= (new_text - text);
			text = new_text;
		}
	}

	/* and recurse */
	for (a = cur->children; a; a = a->next) {
		if (!find_occurrences_html(pattern, p_len, a, case_sens,
				context))
			return false;
	}

	return true;
}
Ejemplo n.º 6
0
int main() {
  srand(time(NULL));
  printf("!");
  while (1) {
    int pattern_len = rand() % MAX_LEN;
    int text_len = rand() % MAX_LEN;
    char pattern[pattern_len];
    char text[text_len];

    fill(pattern_len, pattern);
    fill(text_len, text);

    int *matches = NULL;
    find_pattern(pattern_len, pattern, text_len, text, &matches);
    printf(".");
  }
}
Ejemplo n.º 7
0
Archivo: mkdev.c Proyecto: fingon/procd
static void find_devs(bool block)
{
	char *path = block ? "/sys/dev/block" : "/sys/dev/char";
	struct dirent *dp;
	DIR *dir;

	dir = opendir(path);
	if (!dir)
		return;

	path = buf2 + sprintf(buf2, "%s/", path);
	while ((dp = readdir(dir)) != NULL) {
		char *c;
		int major = 0, minor = 0;
		int len;

		if (dp->d_type != DT_LNK)
			continue;

		if (sscanf(dp->d_name, "%d:%d", &major, &minor) != 2)
			continue;

		strcpy(path, dp->d_name);
		len = readlink(buf2, buf, sizeof(buf));
		if (len <= 0)
			continue;

		buf[len] = 0;
		if (!find_pattern(buf))
			continue;

		c = strrchr(buf, '/');
		if (!c)
			continue;

		c++;
		make_dev(c, block, major, minor);
	}
	closedir(dir);
}
Ejemplo n.º 8
0
static bool find_occurrences_text(const char *pattern, int p_len,
		struct content *c, bool case_sens,
		struct search_context *context)
{
	int nlines = textplain_line_count(c);
	int line;

	for(line = 0; line < nlines; line++) {
		size_t offset, length;
		const char *text = textplain_get_line(c, line,
				&offset, &length);
		if (text) {
			while (length > 0) {
				struct list_entry *entry;
				unsigned match_length;
				size_t start_idx;
				const char *new_text;
				const char *pos = find_pattern(text, length,
						pattern, p_len, case_sens,
						&match_length);
				if (!pos)
					break;

				/* found string in line => add to list */
				start_idx = offset + (pos - text);
				entry = add_entry(start_idx, start_idx +
						match_length, context);
				if (!entry)
					return false;

				new_text = pos + match_length;
				offset += (new_text - text);
				length -= (new_text - text);
				text = new_text;
			}
		}
	}

	return true;
}
Ejemplo n.º 9
0
/*
 * Example data = $NAME:111$STAT:222 find_match_pattern(name, strlen(name),
 * data, "$NAME", ""); produces : ret = 1, name = 111 find_match_pattern(ip, 
 * strlen(ip), data, "$IP", "0.0.0.0"); produces : ret = 0, ip = 0.0.0.0 
 */
int
find_match_pattern(char *name, size_t mlen,
		   const char *data, const char *pattern, char *def)
{
	int ret = 0;
	unsigned int offset, len, length;

	ret =
	    find_pattern(data, strlen(data), pattern, strlen(pattern), '$',
			 &offset, &len);
	// printf("ret=[%d] offset=[%d] len=[%d]\n", ret, offset,len);

	if (ret == 1 && len > 0) {
		length = len > mlen ? mlen : len;
		strncpy(name, data + offset, length);
		name[length] = '\0';
	} else
		snprintf(name, mlen + 1, "%s", def);	// not found and set to
	// default value

	return ret;
}
Ejemplo n.º 10
0
int main()
{
    int test_count;
    scanf("%d", &test_count);

    for (int i = 0; i < test_count; i ++)
    {
        int source_row, source_column, search_row, search_column;
        scanf("%d %d", &source_row, &source_column);
        
        char source[source_row][source_column];
        for(int j = 0; j < source_row; j++)
        {
            scanf("%s", source[j]);
        }
        
        scanf("%d %d", &search_row, &search_column);

        char search[search_row][search_column];
        for (int m = 0; m < search_row; m++)
        {
            scanf("%s", search[m]);
        }

        bool found;
        found = find_pattern(source_row, source_column, source, search_row, search_column, search);

        if (found == true)
        {
            printf("YES\n");
        } else {
            printf("NO\n");
        }
    }
    return 0;
}
Ejemplo n.º 11
0
player_won Referee::five_in_a_row()
{
    int tab_buff[][2] = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}};
    unsigned int buff_x, buff_y, buff_val;
    bool is_breakable;
    size_t size_five, j;
    
    
    for (unsigned int x = 0; x < Map::Size; ++x)
        for (unsigned int y = 0; y < Map::Size; ++y)
        {
            //_map->set_ref_winning(x, y, case_ref_winning::NONE_WINNING);
            buff_val = _map->get_occ_case(x, y);
            if (buff_val != case_type::EMPTY)
            {
                int pattern_tab[][2] = {{-1, case_type::EMPTY}, {0, static_cast<int>(buff_val)}, {1, static_cast<int>(buff_val)}, {2, (buff_val == case_type::WHITE ? case_type::BLACK : case_type::WHITE)}};
                int pattern_tab_inv[][2] = {{-2, case_type::EMPTY}, {-1, static_cast<int>(buff_val)}, {0, static_cast<int>(buff_val)}, {1, (buff_val == case_type::WHITE ? case_type::BLACK : case_type::WHITE)}};
                for (size_t i = 0; i < 8; ++i)
                {
                    size_five = 0;
                    for (j = 0; j < 5; j++)
                    {
                        size_five++;
                        buff_x = x + (tab_buff[i][0] * j);
                        buff_y = y + (tab_buff[i][1] * j);
                        if (!(buff_x >= Map::Size || buff_y >= Map::Size))
                        {
                            if (buff_val != _map->get_occ_case(buff_x, buff_y))
                                break;
                        }
                        else
                            break;
                    }
                    if (j == 5)
                    {
                        is_breakable = false;
                        for (int k = 0; k < 5; k++)
                        {
                            buff_x = x + (tab_buff[i][0] * k);
                            buff_y = y + (tab_buff[i][1] * k);
                            if (find_pattern(i, pattern_tab, pattern_tab_inv, buff_x, buff_y))
                                is_breakable = true;
                        }
                        if (!is_breakable)
                        {
                            return ((buff_val == case_type::WHITE) ? player_won::WHITE_WON : player_won::BLACK_WON);
                        }
                    }
                    else if (j == 4)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            buff_x = x + (tab_buff[i][0] * k);
                            buff_y = y + (tab_buff[i][1] * k);
                            find_pattern(i, pattern_tab, pattern_tab_inv, buff_x, buff_y);
                        }
                        buff_x = x + (tab_buff[i][0] * -1);
                        buff_y = y + (tab_buff[i][1] * -1);
                        if (!(buff_x >= Map::Size || buff_y >= Map::Size))
                            _map->set_ref_winning(buff_x, buff_y, (buff_val == case_type::WHITE) ? case_ref_winning::WHITE_WINNING : case_ref_winning::BLACK_WINNING);
                        buff_x = x + (tab_buff[i][0] * 4);
                        buff_y = y + (tab_buff[i][1] * 4);
                        if (!(buff_x >= Map::Size || buff_y >= Map::Size))
                            _map->set_ref_winning(buff_x, buff_y, (buff_val == case_type::WHITE) ? case_ref_winning::WHITE_WINNING : case_ref_winning::BLACK_WINNING);
                        
                    }
                    else if (j == 3)
                    {
                        for (int k = 0; k < 3; k++)
                        {
                            buff_x = x + (tab_buff[i][0] * k);
                            buff_y = y + (tab_buff[i][1] * k);
                            find_pattern(i, pattern_tab, pattern_tab_inv, buff_x, buff_y);
                        }
                        buff_x = x + (tab_buff[i][0] * -1);
                        buff_y = y + (tab_buff[i][1] * -1);
                        if (!(buff_x >= Map::Size || buff_y >= Map::Size))
                            _map->set_ref_winning(buff_x, buff_y, (buff_val == case_type::WHITE) ? case_ref_winning::WHITE_POSSIBLE : case_ref_winning::BLACK_POSSIBLE);
                        buff_x = x + (tab_buff[i][0] * 3);
                        buff_y = y + (tab_buff[i][1] * 3);
                        if (!(buff_x >= Map::Size || buff_y >= Map::Size))
                            _map->set_ref_winning(buff_x, buff_y, (buff_val == case_type::WHITE) ? case_ref_winning::WHITE_POSSIBLE : case_ref_winning::BLACK_POSSIBLE);
                    }
                }
            }
        }
    return (player_won::NONE);
}
Ejemplo n.º 12
0
/* Find a free name for an object given a a prefix. */
char *
find_name(const char *prefix, int type, int namelen)
{
	struct gctl_req *req;
	char comment[1], buf[GV_CFG_LEN - 1], *name, *sname, *ptr;
	const char *errstr;
	int i, n, begin, len, conflict;
	char line[1024];

	comment[0] = '\0';

	/* Find a name. Fetch out configuration first. */
	req = gctl_get_handle();
	gctl_ro_param(req, "class", -1, "VINUM");
	gctl_ro_param(req, "verb", -1, "getconfig");
	gctl_ro_param(req, "comment", -1, comment);
	gctl_rw_param(req, "config", sizeof(buf), buf);
	errstr = gctl_issue(req);
	if (errstr != NULL) {
		warnx("can't get configuration: %s", errstr);
		return (NULL);
	}
	gctl_free(req);

	begin = 0;
	len = strlen(buf);
	i = 0;
	sname = malloc(namelen + 1);

	/* XXX: Max object setting? */
	for (n = 0; n < 10000; n++) {
		snprintf(sname, namelen, "%s%d", prefix, n);
		conflict = 0;
		begin = 0;
		/* Loop through the configuration line by line. */
		for (i = 0; i < len; i++) {
			if (buf[i] == '\n' || buf[i] == '\0') {
				ptr = buf + begin;
				strlcpy(line, ptr, (i - begin) + 1);
				begin = i + 1;
				switch (type) {
				case GV_TYPE_DRIVE:
					name = find_pattern(line, "drive");
					break;
				case GV_TYPE_VOL:
					name = find_pattern(line, "volume");
					break;
				case GV_TYPE_PLEX:
				case GV_TYPE_SD:
					name = find_pattern(line, "name");
					break;
				default:
					printf("Invalid type given\n");
					continue;
				}
				if (name == NULL)
					continue;
				if (!strcmp(sname, name)) {
					conflict = 1;
					/* XXX: Could quit the loop earlier. */
				}
			}
		}
		if (!conflict)
			return (sname);
	}
	free(sname);
	return (NULL);
}
Ejemplo n.º 13
0
/*
 * This function patch the _sceSifLoadModule calls
 */
int patch_loadModule(void)
{
	u32 *ptr = NULL;
	u32 memscope, inst, fncall;
	u32 pattern[1], mask[1];
	u32 start = 0x00100000;
	int pattern_found = 0;
	const pattern_t *pat;

	GS_BGCOLOUR = 0x00a5ff; /* Orange while _sceSifLoadModule pattern search */

	memscope = 0x01f00000 - start;

	/* First try to locate the orginal _sceSifLoadModule function */
	pat = _loadmodule_patterns;
	while (pat->seq) {
		ptr = find_pattern((u32*)start, memscope, pat);
		if (ptr)
			break;
		pat++;
	}

	if (!ptr) {
		GS_BGCOLOUR = 0x808080; /* Gray, pattern not found */
		return 0;
	}

 	GS_BGCOLOUR = 0xcbc0ff; /* Pink while _sceSifLoadModule patches */

	/* Save original _sceSifLoadModule ptr */
	_sceSifLoadModule = (void *)ptr;

	/* Retrieve _sceSifLoadModule call Instruction code */
	inst = 0x0c000000;
	inst |= 0x03ffffff & ((u32)ptr >> 2);

	/* Make pattern with function call code saved above */
	pattern[0] = inst;
	mask[0] = 0xffffffff;

	/* Get Hook_SifLoadModule call Instruction code */
	inst = 0x0c000000;
	inst |= 0x03ffffff & ((u32)Hook_SifLoadModule >> 2);

	/* Search & patch for calls to _sceSifLoadModule */
	ptr = (u32*)start;
	while (ptr) {
		memscope = 0x01f00000 - (u32)ptr;

		ptr = find_pattern_with_mask(ptr, memscope, pattern, mask, sizeof(pattern));
		if (ptr) {
			pattern_found = 1;

			fncall = (u32)ptr;
			_sw(inst, fncall); /* overwrite the original _sceSifLoadModule function call with our function call */

			ptr += 2;
		}
	}

	if (!pattern_found)
		GS_BGCOLOUR = 0x808080; /* gray, pattern not found */
	else
		GS_BGCOLOUR = 0x000000; /* Black, done */

	return 1;
}
Ejemplo n.º 14
0
// Main test function. Locates the given magic pixel pattern on the screen, then
// runs one full latency test, sending input events and recording responses. On
// success, the results of the test are reported in the output parameters, and
// true is returned. If the test fails, the error parameter is filled in with
// an error message and false is returned.
bool measure_latency(
    const uint8_t magic_pattern[],
    double *out_key_down_latency_ms,
    double *out_scroll_latency_ms,
    double *out_max_js_pause_time_ms,
    double *out_max_css_pause_time_ms,
    double *out_max_scroll_pause_time_ms,
    char **error) {
  screenshot *screenshot = take_screenshot(0, 0, UINT32_MAX, UINT32_MAX);
  if (!screenshot) {
    *error = "Failed to take screenshot.";
    return false;
  }
  assert(screenshot->width > 0 && screenshot->height > 0);

  size_t x, y;
  bool found_pattern = find_pattern(magic_pattern, screenshot, &x, &y);
  free_screenshot(screenshot);
  if (!found_pattern) {
    *error = "Failed to find test pattern on screen.";
    return false;
  }
  uint8_t full_pattern[pattern_bytes];
  for (int i = 0; i < pattern_magic_bytes; i++) {
    full_pattern[i] = magic_pattern[i];
  }
  measurement_t measurement;
  measurement_t previous_measurement;
  memset(&measurement, 0, sizeof(measurement_t));
  memset(&previous_measurement, 0, sizeof(measurement_t));
  int screenshots = 0;
  bool first_screenshot_successful = read_data_from_screen((uint32_t)x,
      (uint32_t) y, magic_pattern, &measurement);
  if (!first_screenshot_successful) {
    *error = "Failed to read data from test pattern.";
    return false;
  }
  if (measurement.test_mode == TEST_MODE_NATIVE_REFERENCE) {
    uint8_t *test_pattern = (uint8_t *)malloc(pattern_bytes);
    memset(test_pattern, 0, pattern_bytes);
    for (int i = 0; i < pattern_magic_bytes; i++) {
      test_pattern[i] = rand();
    }
    if (!open_native_reference_window(test_pattern)) {
      *error = "Failed to open native reference window.";
      return false;
    }
    bool return_value = measure_latency(test_pattern, out_key_down_latency_ms, out_scroll_latency_ms, out_max_js_pause_time_ms, out_max_css_pause_time_ms, out_max_scroll_pause_time_ms, error);
    if (!close_native_reference_window()) {
      debug_log("Failed to close native reference window.");
    };
    return return_value;
  }
  int64_t start_time = measurement.screenshot_time;
  previous_measurement = measurement;
  statistic javascript_frames;
  statistic css_frames;
  statistic key_down_events;
  statistic scroll_stats;
  init_statistic("javascript_frames", &javascript_frames,
      measurement.javascript_frames, start_time);
  init_statistic("key_down_events", &key_down_events,
      measurement.key_down_events, start_time);
  init_statistic("css_frames", &css_frames, measurement.css_frames, start_time);
  init_statistic("scroll", &scroll_stats, measurement.scroll_position,
      start_time);
  int sent_events = 0;
  int scroll_x = x + 40;
  int scroll_y = y + 40;
  int64_t last_scroll_sent = start_time;
  if (measurement.test_mode == TEST_MODE_SCROLL_LATENCY) {
    send_scroll_down(scroll_x, scroll_y);
    scroll_stats.previous_change_time = get_nanoseconds();
  }
  while(true) {
    bool screenshot_successful = read_data_from_screen((uint32_t)x,
        (uint32_t) y, magic_pattern, &measurement);
    if (!screenshot_successful) {
      *error = "Test window moved during test. The test window must remain "
          "stationary and focused during the entire test.";
      return false;
    }
    if (measurement.test_mode == TEST_MODE_ABORT) {
      *error = "Test aborted.";
      return false;
    }
    screenshots++;
    int64_t screenshot_time = measurement.screenshot_time;
    int64_t previous_screenshot_time = previous_measurement.screenshot_time;
    debug_log("screenshot time %f",
        (screenshot_time - previous_screenshot_time) /
            (double)nanoseconds_per_millisecond);
    update_statistic(&javascript_frames, measurement.javascript_frames,
        screenshot_time, previous_screenshot_time);
    update_statistic(&key_down_events, measurement.key_down_events,
        screenshot_time, previous_screenshot_time);
    update_statistic(&css_frames, measurement.css_frames, screenshot_time,
        previous_screenshot_time);
    bool scroll_updated = update_statistic(&scroll_stats,
        measurement.scroll_position, screenshot_time, previous_screenshot_time);

    if (measurement.test_mode == TEST_MODE_JAVASCRIPT_LATENCY) {
      if (key_down_events.measurements >= latency_measurements_to_take) {
        break;
      }
      if (key_down_events.value_delta > sent_events) {
        *error = "More events received than sent! This is probably a bug in "
            "the test.";
        return false;
      }
      if (screenshot_time - key_down_events.previous_change_time >
          event_response_timeout_ms * nanoseconds_per_millisecond) {
        *error = "Browser did not respond to keyboard input. Make sure the "
            "test page remains focused for the entire test.";
        return false;
      }
      if (key_down_events.value_delta == sent_events) {
        // We want to avoid sending input events at a predictable time relative
        // to frames, so introduce a random delay of up to 1 frame (16.67 ms)
        // before sending the next event.
        usleep((rand() % 17) * 1000);
        if (!send_keystroke_z()) {
          *error = "Failed to send keystroke for \"Z\" key to test window.";
          return false;
        }
        key_down_events.previous_change_time = get_nanoseconds();
        sent_events++;
      }
    } else if (measurement.test_mode == TEST_MODE_SCROLL_LATENCY) {
        if (scroll_stats.measurements >= latency_measurements_to_take) {
          break;
        }
        if (screenshot_time - scroll_stats.previous_change_time >
            event_response_timeout_ms * nanoseconds_per_millisecond) {
          *error = "Browser did not respond to scroll events. Make sure the "
              "test page remains focused for the entire test.";
          return false;
        }
        if (scroll_updated) {
          // We saw the start of a scroll. Wait for the scroll animation to
          // finish before continuing. We assume the animation is finished if
          // it's been 100 milliseconds since we last saw the scroll position
          // change.
          int64_t scroll_update_time = screenshot_time;
          int64_t scroll_wait_start_time = screenshot_time;
          while (screenshot_time - scroll_update_time <
                 100 * nanoseconds_per_millisecond) {
            screenshot_successful = read_data_from_screen((uint32_t)x,
                (uint32_t) y, magic_pattern, &measurement);
            if (!screenshot_successful) {
              *error = "Test window moved during test. The test window must "
                  "remain stationary and focused during the entire test.";
              return false;
            }
            screenshot_time = measurement.screenshot_time;
            if (screenshot_time - scroll_wait_start_time >
                nanoseconds_per_second) {
              *error = "Browser kept scrolling for more than 1 second after a "
                  "single scrollwheel event.";
              return false;
            }
            if (measurement.scroll_position != scroll_stats.value) {
              scroll_stats.value = measurement.scroll_position;
              scroll_update_time = screenshot_time;
            }
          }
          // We want to avoid sending input events at a predictable time
          // relative to frames, so introduce a random delay of up to 1 frame
          // (16.67 ms) before sending the next event.
          usleep((rand() % 17) * 1000);
          send_scroll_down(scroll_x, scroll_y);
          scroll_stats.previous_change_time = get_nanoseconds();
        }
    } else if (measurement.test_mode == TEST_MODE_PAUSE_TIME) {
      // For the pause time test we want the browser to scroll continuously.
      // Send a scroll event every frame.
      if (screenshot_time - last_scroll_sent >
          17 * nanoseconds_per_millisecond) {
        send_scroll_down(scroll_x, scroll_y);
        last_scroll_sent = get_nanoseconds();
      }
    } else if (measurement.test_mode == TEST_MODE_PAUSE_TIME_TEST_FINISHED) {
      break;
    } else {
      *error = "Invalid test type. This is a bug in the test.";
      return false;
    }

    if (screenshot_time - start_time >
        test_timeout_ms * nanoseconds_per_millisecond) {
      *error = "Timeout.";
      return false;
    }
    previous_measurement = measurement;
    usleep(0);
  }
  // The latency we report is the midpoint of the interval given by the average
  // upper and lower bounds we've computed.
  *out_key_down_latency_ms =
      (upper_bound_ms(&key_down_events) + lower_bound_ms(&key_down_events)) / 2;
  *out_scroll_latency_ms =
      (upper_bound_ms(&scroll_stats) + lower_bound_ms(&scroll_stats) / 2);
  *out_max_js_pause_time_ms =
      javascript_frames.max_lower_bound / (double) nanoseconds_per_millisecond;
  *out_max_css_pause_time_ms =
      css_frames.max_lower_bound / (double) nanoseconds_per_millisecond;
  *out_max_scroll_pause_time_ms =
      scroll_stats.max_lower_bound / (double) nanoseconds_per_millisecond;
  debug_log("out_key_down_latency_ms: %f out_scroll_latency_ms: %f "
      "out_max_js_pause_time_ms: %f out_max_css_pause_time: %f\n "
      "out_max_scroll_pause_time_ms: %f",
      *out_key_down_latency_ms,
      *out_scroll_latency_ms,
      *out_max_js_pause_time_ms,
      *out_max_css_pause_time_ms,
      *out_max_scroll_pause_time_ms);
  return true;
}
Ejemplo n.º 15
0
int main(int argc, char* argv[])
{
    FILE* file;
    char* inputfile;
    char* outputfile;
    UINT8* buffer;
    INT32 filesize;
    INT32 read;
    UINT8* rest;
    INT32 rest_size;
    UINT8* module;
    UINT32 module_counter;
    UINT8 error_code;

    if(argc < 3)
    {
        printf("PMPatch v0.3.1\nThis program patches UEFI BIOS files\nto be compatible with MacOS X SpeedStep implementation\n\n"
            "Usage: PMPatch INFILE OUTFILE\n\n");
        return ERR_ARGS;
    }

    inputfile = argv[1]/*/"in.rom"*/;
    outputfile = argv[2]/*/"out.rom"*/;

     /* Opening input file */
    file = fopen(inputfile, "rb");
    if (!file)
    {
        perror("Can't open input file.\n");
        return ERR_INPUT_FILE;
    }

    /* Determining file size */
    fseek(file, 0, SEEK_END);
    filesize = ftell(file);
    fseek(file, 0, SEEK_SET);

    /* Allocating memory for buffer */
    buffer = (UINT8*)malloc(filesize);
    if (!buffer)
    {
        fprintf(stderr, "Can't allocate memory for buffer.\n");
        return ERR_MEMORY;
    }

    /* Reading whole file to buffer */
    read = fread((void*)buffer, sizeof(char), filesize, file);
    if (read != filesize)
    {
        perror("Can't read input file.\n");
        return ERR_INPUT_FILE;
    }

    /* Closing input file */
    fclose(file);

    /* Searching for PowerManagement modules and patching them if found */
    module_counter = 0;
    rest = buffer;
    rest_size = filesize;
    do
    {
        module = find_pattern(rest, rest_size, PWRMGMT_UUID, MODULE_UUID_LENGTH);
        if(module)
        {
            rest_size = filesize - (module - buffer);
            rest = module + 1;
            module_counter++;

            if(patch_powermanagement_module(module, &error_code))
                printf("PowerManagement module at %08X patched.\n", module - buffer);
            else
                printf("PowerManagement module at %08X not patched.\n%s\n", module - buffer, PATCH_PWRMGMT_ERROR_MESSAGES[error_code]);
        }
    }
    while(module);

    if(!module_counter)
    {
        printf("PowerManagement module not found.\n");
        
        /* Searching for CpuPei modules and patching them if found */
        module_counter = 0;
        rest = buffer;
        rest_size = filesize;
        do
        {
            module = find_pattern(rest, rest_size, CPUPEI_UUID, MODULE_UUID_LENGTH);
            if(module)
            {
                rest_size = filesize - (module - buffer);
                rest = module + 1;
                module_counter++;

                if(patch_cpupei_module(module, &error_code))
                    printf("CpuPei module at %08X patched.\n", module - buffer);
                else
                    printf("CpuPei module at %08X not patched.\n%s\n", module - buffer, PATCH_CPUPEI_ERROR_MESSAGES[error_code]);
            }
        }
        while(module);

        if(!module_counter)
        {
            printf("CpuPei module not found. Nothing to do.\n");
            return ERR_NO_MODULE;
        }
    }
    
    /* Creating output file*/
    file = fopen(outputfile, "wb");
    if (!file)
    {
        perror("Can't create output file.\n");
        return ERR_OUTPUT_FILE;
    }
    /* Writing modified BIOS file*/
    if(fwrite(buffer, sizeof(char), filesize, file) != filesize)
    {
        perror("Can't write input file.\n");
        return ERR_INPUT_FILE;
    }

    /* Closing output file */
    fclose(file);

    /* Freeing buffer */
    free(buffer);
    
    return ERR_OK;
}