struct String *string_to_struct(char *str)
{
  int length;
  struct String *string;
  char *cp;

  /* Find the length of the string. */
  length = find_len(str);

  /* Allocate the memory that is the same size as the struct. */
  string = malloc(sizeof(struct String));

  /* Handle a failed memory allocation. */
  if (string == NULL)
    return (NULL);

  /* Make a copy of the original string. */
  cp = str_copy(str, length);

  /* Assign the given values to the string struct. */
  string->str = cp;
  string->length = length;

  return(string);
}
int is_palindrome(char *s)
{
  int len, begin, end;
  len = find_len(s);
  begin = 0;
  end = len-1;
  return compare(len,s,begin,end);
}
char *int_to_string(int n) {
  char *str;
  int len;
  str = malloc(sizeof(char) * 509);

  if (str == NULL) {
    return NULL;
  }

  /* find the length and pass it to function return string */
  len = find_len(n);
  str = int_to_str(n, len, str);

  if (n < 0) {
    str[len + 1] = '\0';
  }
  if (n > 0) {
    str[len] = '\0';
  }

  return (str);
}
Exemple #4
0
/*
 * @brief	Rotate the list by K places
 */
node *rotate_list(node *head, int k)
{
	node *f_ptr = head;
	node *s_ptr = head;
	/* Argument check */
	if (head == NULL)
		return NULL;
	else if (k > find_len(head))
		return NULL;
	for (int i = 0; i < k; i++)
		f_ptr = f_ptr->next;
	while (f_ptr && f_ptr->next)
	{
		s_ptr = s_ptr->next;
		f_ptr = f_ptr->next;
	}
	/* Finally, logic for rotating the list */
	f_ptr->next = head;
	head = s_ptr->next;
	s_ptr->next = NULL;

	return head;
}
struct Param *params_to_struct_array(int ac, char **av)
{
  struct Param *array;
  int i, len;
  char *param;
  array = malloc(sizeof(struct Param) * ac);
  if (array == NULL)
    return (NULL);

  /* For each argument, assign the values. */
  for(i = 0; i < ac; ++i) {
    len = find_len(av[i]);
    array[i].str = av[i];
    array[i].length = len;
    array[i].copy = array[i].str;
    param = malloc(sizeof(char) * (len + 1));
    if (param == NULL)
      return (NULL);
    param = av[i];
    array[i].tab = string_split(av[i], len);
  }
  array[i].str = NULL;
  return(array);
}
Exemple #6
0
int main(int argc, char *argv[])
{
	char ch, addr_loc[10], line[100], offset_ar[4];
	unsigned char data[100];
	int len, i, ctr, len_loc;
	long offset;

	char leave1[1], leave2[2], leave3[3];

	char leave[1];
	leave[0]=201;

	leave1[0]=83;//83
	leave2[0]=139;leave2[1]=06;//8b 06
	leave3[0]=131;leave3[1]=238;leave3[2]=04;//83 ee 04

	FILE *ptr_loc, *ptr_dump, *ptr_bin, *ptr_bin_r, *ptr_len_off, *ptr_data;

	ptr_loc=fopen("locations.txt","r");
	ptr_dump=fopen("objdump.txt","r");
	ptr_bin=fopen(argv[1],"rb+");
	ptr_bin_r=fopen(argv[1],"rb");
	ptr_len_off=fopen("len_off.txt","w+");
	ptr_data=fopen("data.txt","w+");

	if(!ptr_loc || !ptr_dump || !ptr_bin || !ptr_bin_r || !ptr_len_off || !ptr_data)
	{
		printf("unable to open files\n");
		exit(1);
	}
	
	fseek(ptr_loc,1,SEEK_SET);
	while(1)
	{
		// read address from file locations.txt
		fseek(ptr_loc,1,SEEK_CUR);
		len_loc=0;
		do
		{
			ch=fgetc(ptr_loc);
			len_loc++;
		}while(ch != ':');
		len_loc-=2;
		//printf("len_loc : %d\n",len_loc);

		fseek(ptr_loc,-(len_loc+2),SEEK_CUR);
		fread(addr_loc,len_loc,1,ptr_loc);
		addr_loc[len_loc]='\0';
		printf("addr_loc : %s\n",addr_loc);

		// find length of instruction to be replaced
		len=find_len(ptr_dump,addr_loc);
		rewind(ptr_dump);

		if(len!=0)
		{

		offset=hex2_dec(addr_loc);
		printf("offset=%ld, len=%d\n",offset,len);

		// read from binary and update files.
		fseek(ptr_bin_r,offset,SEEK_SET);

		if(fread(data,len,1,ptr_bin_r)!=0)
			printf("\nyo\n");

		for(i = 0; i<len; i++)
		    printf("%x ", data[i]);
		printf("\n");

		exit(0);		
		
		// write to data.txt
		for(i=0;i<len;i++)
			fprintf(ptr_data, " %x", data[i]);
		fprintf(ptr_data, "%c", '\n');

		// write to len_off.txt
		fprintf(ptr_len_off, "%d %ld\n", len, offset);

		rewind(ptr_bin_r);

//		exit(0);

		// replacement in binary file
		fseek(ptr_bin,offset,SEEK_SET);
		ctr=len;

		if(len==1)
			fwrite(leave1, sizeof(leave1), 1, ptr_bin);
		else if(len==2)
			fwrite(leave2, sizeof(leave2), 1, ptr_bin);
		else if(len==3)
			fwrite(leave3, sizeof(leave3), 1, ptr_bin);
		else
		{
			while(len--)
				fwrite(leave1, sizeof(leave1), 1, ptr_bin);
		}
		rewind(ptr_bin);

//		exit(0);

		}
		do
		{
			ch=fgetc(ptr_loc);
		}while(ch != '\n');
		if(fread(addr_loc,1,1,ptr_loc)!=1)
			break;
	}

	fclose(ptr_len_off);
	fclose(ptr_data);
	fclose(ptr_bin);
	fclose(ptr_bin_r);
	fclose(ptr_dump);
	fclose(ptr_loc);

	return 0;
}