Beispiel #1
0
int compare_files(state *s, char *fn1, char *fn2)
{
  FILE *h1, *h2;
  int status;

  if ((h1 = fopen(fn1,"rb")) == NULL)
    {
      perror(fn1);
      return TRUE;
    }

  if ((h2 = fopen(fn2,"rb")) == NULL)
    {
      perror(fn2);
      fclose(h1);
      return TRUE;
    }
  
  if (find_file_size(fn1,h1,&s->size1))
    return TRUE;
  if (find_file_size(fn2,h2,&s->size2))
    return TRUE;

  if (s->size1 != s->size2)
    print_error ("%s: files are not the same size", __progname);
  
  status = compare_blocks(s,h1,h2);

  fclose(h1);
  fclose(h2);

  return status;
}
Beispiel #2
0
file_types state::file_type(const tstring &fn,display *ocb,uint64_t *filesize,
				   timestamp_t *ctime,timestamp_t *mtime,timestamp_t *atime)
{
    struct __stat64 sb;
    memset(&sb,0,sizeof(sb));
    if (TLSTAT(fn.c_str(),&sb))  {
	if(ocb) ocb->error_filename(fn,"%s", strerror(errno));
	return stat_unknown;
    }
    if(ctime) *ctime = sb.st_ctime;
    if(mtime) *mtime = sb.st_mtime;
    if(atime) *atime = sb.st_atime;
    if(filesize){
	if(sb.st_size!=0){
	    *filesize = sb.st_size;
	} else {
	    /*
	     * The stat() function does not return file size for raw devices.
	     * If we have no file size, try to use the find_file_size function,
	     * which calls ioctl.
	     */
	    FILE *f = _tfopen(fn.c_str(),_TEXT("rb"));
	    if(f){
		*filesize = find_file_size(f,ocb);
		fclose(f); f = 0;
	    }
	}
    }
    return file_metadata_t::decode_file_type(sb);
}
int process_file(f_state *s)
{

	//printf("processing file\n");
	f_info	*i = (f_info *)malloc(sizeof(f_info));
	char	temp[PATH_MAX];

	if ((realpath(s->input_file, temp)) == NULL)
		{
		print_error(s, s->input_file, strerror(errno));
		return TRUE;
		}

	i->file_name = strdup(s->input_file);
	i->is_stdin = FALSE;
	audit_start(s, i);

	//  printf("opening file %s\n",i->file_name);
#if defined(__LINUX)
	#ifdef DEBUG
	printf("Using 64 bit fopen\n");
	#endif
	i->handle = fopen64(i->file_name, "rb");
#elif defined(__WIN32)

	/*I would like to be able to read from
	* physical devices in Windows, have played
	* with different options to fopen and the
	* dd src says you need write access on WinXP
	* but nothing seems to work*/
	i->handle = fopen(i->file_name, "rb");
#else
	i->handle = fopen(i->file_name, "rb");
#endif
	if (i->handle == NULL)
		{
		print_error(s, s->input_file, strerror(errno));
		audit_msg(s, "Error: %s", strerror(errno));
		return TRUE;
		}

	i->total_bytes = find_file_size(i->handle);
	search_stream(s, i);
	audit_finish(s, i);

	fclose(i->handle);
	free(i);
	return FALSE;
}
Beispiel #4
0
void send_huge_file(int cliente, size_t* bytes_sent, char* arq_name){

    FILE* arq = fopen(arq_name, "rb");
    size_t file_size = 0;
    ssize_t aux = 0;
    char send_buff[64 KB];
    char* send_buff_aux;
    *bytes_sent = 0;

    if(arq == NULL){
        fprintf(stderr, "Erro ao abrir o arquivo %s\n", arq_name);
        exit(EXIT_FAILURE);
    }

    file_size = find_file_size(arq);

    while(fread(send_buff, sizeof(char), 64 KB, arq) == 64 KB){
        aux = send(cliente, send_buff, 64 KB, 0);

        if(aux > 0){
            *bytes_sent += aux;
        }
    }

    /* Send the chunk that could not be sent in a 64KB chunk */
    if(file_size % (64 KB) != 0){
        send_buff_aux = malloc(sizeof(char) * file_size % (64 KB));

        fread(send_buff_aux, sizeof(char), file_size % (64 KB), arq);
        aux = send(cliente, send_buff_aux, file_size % (64 KB), 0);

        if(aux > 0){
            *bytes_sent += aux;
        }

        free(send_buff_aux);
    }

    fclose(arq);

}
Beispiel #5
0
char* read_file_to_memory(const char* file_name) {
    FILE* fp = fopen(file_name, "r");
    char* buffer = NULL;
    long file_size;

    if (fp == NULL) {
        goto clean_up;
    }
    
    file_size = find_file_size(fp);
    if (file_size < 0) {
        goto clean_up;
    }

    buffer = malloc(file_size +1);
    if (buffer == NULL) {
        goto clean_up;
    }

    if (fread(buffer, 1, file_size, fp) != file_size) {
        goto clean_up;
    }
    buffer[file_size] = '\0';
    fclose(fp);
    return buffer;

clean_up:
    if (fp != NULL) {
        fclose(fp);
    }

    if (buffer != NULL) {
        free(buffer);
    }
    return NULL;
}
Beispiel #6
0
static int make_bmp_from_file(state *s, char *input)
{
  char *o1, *o2, *output = (char *)malloc(sizeof(char) * PATH_MAX);

  if (s->verbose)
    print_status ("%s: Processing %s", __progname, input);

  if ((o1 = strdup(input)) == NULL)
    {
      perror(__progname);
      return TRUE;
    }

  o2 = basename(o1);
  snprintf(output, PATH_MAX, "%s.bmp", o2);
  // We don't free o2 as it's statically allocated 
  free(o1);

  if ((s->in_handle = fopen(input,"rb")) == NULL)
    {
      free(output);
      perror(input);
      return TRUE;
    }

  snprintf(s->input_fn,PATH_MAX,"%s", input);

  if ((s->out_handle = fopen(output,"wb")) == NULL)
    {
      fclose(s->in_handle);
      perror(output);
      free(output);
      return TRUE;
    }

  if (find_file_size(input, s->in_handle, &s->input_length))
    return TRUE;

  s->image_height = (s->input_length / s->image_width);
  if (s->input_length % s->image_width != 0)
    s->image_height++;

  if (write_file_header(s))
    return TRUE;

  if (write_file_info(s))
    return TRUE;

  if (s->orientation == ORIENTATION_VERTICAL) {
    if (write_vertical_file(s)) {
      perror(output);
    }
  } else {
    write_horizontal_file(s);
  }

  if (s->verbose)
    print_status ("%s: Done.", __progname);

  fclose(s->in_handle);
  fclose(s->out_handle);
  free(output);

  return FALSE;
}
Beispiel #7
0
int hash_file(state *s, TCHAR *fn)
{
  size_t fn_length;
  char *sum;
  TCHAR *my_filename, *msg;
  FILE *handle;
  
  handle = _tfopen(fn,_TEXT("rb"));
  if (NULL == handle)
  {
    print_error_unicode(s,fn,"%s", strerror(errno));
    return TRUE;
  }
 
  if ((sum = (char *)malloc(sizeof(char) * FUZZY_MAX_RESULT)) == NULL)
  {
    fclose(handle);
    print_error_unicode(s,fn,"%s", strerror(errno));
    return TRUE;
  }

  if ((msg = (TCHAR *)malloc(sizeof(TCHAR) * (MAX_STATUS_MSG + 2))) == NULL)
  {
    free(sum);
    fclose(handle);
    print_error_unicode(s,fn,"%s", strerror(errno));
    return TRUE;
  }

  if (MODE(mode_verbose))
  {
    fn_length = _tcslen(fn);
    if (fn_length > MAX_STATUS_MSG)
    {
      // We have to make a duplicate of the string to call basename on it
      // We need the original name for the output later on
      my_filename = _tcsdup(fn);
      my_basename(my_filename);
    }
    else
      my_filename = fn;

    _sntprintf(msg,
	       MAX_STATUS_MSG-1,
	       _TEXT("Hashing: %s%s"), 
	       my_filename, 
	       _TEXT(BLANK_LINE));
    _ftprintf(stderr,_TEXT("%s\r"), msg);

    if (fn_length > MAX_STATUS_MSG)
      free(my_filename);
  }

  fuzzy_hash_file(handle,sum);
  prepare_filename(s,fn);
  display_result(s,fn,sum);

  if (find_file_size(handle) > SSDEEP_MIN_FILE_SIZE)
    s->found_meaningful_file = true;
  s->processed_file = true;

  fclose(handle);
  free(sum);
  free(msg);
  return FALSE;
}