예제 #1
0
static guchar *
load_image (const gchar *name, guint width, guint height)
{
  guchar *grayscale_buffer;
  guint16 *depth;
  gchar *contents;
  gsize len;
  GError *error = NULL;

  gsize count = width * height * sizeof (guint16);
  depth = read_file_to_buffer (name, count, error);

  if (error != NULL)
    {
      g_debug ("Error Opening: %s", error->message);
      return NULL;
    }

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

  grayscale_buffer = create_grayscale_buffer (depth, width, height);

  g_slice_free1 (width * height * sizeof (guint16), depth);
  return grayscale_buffer;
}
예제 #2
0
void update_dirfile(const char * dir, const char * name, const char * user){
  dirscan scan = {0};
  ensure_directory("shares/");
  {
    char * dir_filename = fmtstr("shares/%s.dir", name);
    size_t size0;
    void * buf = read_file_to_buffer(dir_filename, &size0);
    if(buf != NULL){
      logd("Size: %i\n", size0);
      scan = dirscan_from_buffer(buf);
      dealloc(buf);
    }
    logd("SCAN: %i\n", scan.cnt);
    udpc_dirscan_update(dir, &scan, false);
    logd("SCAN2: %i\n", scan.cnt);
    size_t size = 0;
    buf = dirscan_to_buffer(scan,&size);
    
    write_buffer_to_file(buf, size, dir_filename);
    dealloc(buf);
  }
  
  {
    void * buffer = NULL;
    size_t cnt = 0;
    udpc_pack_string(dir, &buffer, &cnt);
    udpc_pack_string(name, &buffer, &cnt);
    udpc_pack_string(user, &buffer, &cnt);
    char * shareinfo_filename = fmtstr("shareinfo/%s", name);
    FILE * f = fopen(shareinfo_filename, "w");
    fwrite(buffer, 1, cnt, f);
    fclose(f);
    dealloc(shareinfo_filename);
  }
  
  char * bin_filename = fmtstr("shares/%s.bin", name);
  size_t s = 0;
  void * b = dirscan_to_buffer(scan, &s);
  FILE * f = fopen(bin_filename, "w");
  fwrite(b, 1, s, f);
  fclose(f);
  dealloc(bin_filename);
  
  char * filename = fmtstr("shares/%s.json", name);
  FILE * jsonfile = fopen(filename, "w");
  fprintf(jsonfile, "{\"dir\": \"%s\", \"files\": [\n", dir);
  for(size_t i = 0; i < scan.cnt; i++){
    fprintf(jsonfile, "{ \"path\": \"%s\", \"md5\":\"", scan.files[i]);
    udpc_fprintf_md5(jsonfile, scan.md5s[i]);
    if(i != scan.cnt -1){
      fprintf(jsonfile, "\"},\n");
    }else{
      fprintf(jsonfile, "\"}\n");
    }
  }
  fprintf(jsonfile, "]}\n");
  fclose(jsonfile);
  dealloc(filename);
}
예제 #3
0
static void disasm(const char *filename)
{
    buffer_t *input_buf;
    pefile_t *pefile;
    disassembly_t *da;

    
    input_buf = read_file_to_buffer(filename);
    pefile = parse_pefile(input_buf);
    free_buffer(&input_buf);

	assert(pefile != NULL);

	da = disassemble_pefile(pefile, TRUE, TRUE, 0);

	debug_print_disassembly(da);
}
예제 #4
0
int main(int argc, char *argv[])
{
	int c;
	char *input_filename = NULL;
	char *output_filename = NULL;
	buffer_t *input_buffer;
	pefile_t *original_pefile;
    int disasm_mode = 0;
    int sequences_mode = 0;

    printf("%s %s\nCopyright (C) 2007-2008 Nick Harbour, All Rights Reserved\n\n", PROGRAM_NAME, PROGRAM_VERSION);

	while (1) {
		static struct option long_options[] =
		{
			{"help", no_argument, 0, 'h'},
			{"input", required_argument, 0, 'i'},
			{"output", required_argument, 0, 'o'},
            {"disasm", no_argument, 0, 'd'},
            {"sequences", no_argument, 0, 's'},
			{0,0,0,0}
		};

		int option_index = 0;

		c = getopt_long(argc, argv, "hi:o:d", long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
			case 'h':
				usage(argv[0], stdout);
				printf("\n"
					   " -i FILE, --input=FILE     Specify an input executable FILE\n"
					   " -o FILE, --output=FILE    Specify an output executable FILE\n"
					   " -h, --help                Display this help information\n");
				return 0;
			case 'i':
				input_filename = optarg;
				break;
			case 'o':
				output_filename = optarg;
				break;
            case 'd':
                disasm_mode++;
                break;
            case 's':
                sequences_mode++;
                break;
			default:
				usage(argv[0], stderr);
				exit(-1);
		}
	}

    if (disasm_mode && sequences_mode && input_filename)
    {
    }
    if (disasm_mode && input_filename)
    {
        disasm(input_filename);
        if (!output_filename && !sequences_mode)
            return 0;
    }

    if (sequences_mode && input_filename)
    {
    }
	if (optind < argc || !input_filename || !output_filename) {
		usage(argv[0], stderr);		
		exit(-1);
	}

	
	fprintf(stderr, "Loading and Parsing Input File...\r");

	input_buffer = read_file_to_buffer(input_filename);
	original_pefile = parse_pefile(input_buffer);
	free_buffer(&input_buffer);

	if (original_pefile == NULL) {
		die("%s does not appear to be a valid PE executable", input_filename);
	}

	fprintf(stderr, "Loading and Parsing Input File. (done)\n");

	armoring_swirly();
	armor_pefile(original_pefile);
	fprintf(stderr, "\rArmoring Code. (done)\n");

	fprintf(stderr, "Writing Output File. ");
	dump_pefile(original_pefile, output_filename);
	fprintf(stderr, "(done)\n");

	return 0;
}
예제 #5
0
int web_main(void * _ed, struct MHD_Connection * con, const char * url,
		      const char * method, const char * version,
		      const char *upload_data, size_t * upload_data_size,
		     void ** con_cls){
  UNUSED(url); UNUSED(version); UNUSED(upload_data); UNUSED(upload_data_size);
  UNUSED(method); UNUSED(con_cls); UNUSED(_ed);
  const char * file = "page.html";

  bool style_loc = strcmp((char *) url + 1, "style.css") == 0;
  if(style_loc)
    file = "style.css";
  else if(0 == strcmp((char *) url + 1, (char *) "favicon.png"))
    file = "favicon.png";
  
  char fnamebuffer[100];
  logd("'%s' %s %s %i\n", url, method, version, upload_data_size);
  logd("File: %s\n", file);
  if(url == strstr(url, "/sharesinfo")){
    // Send back json code describing all the available shares.

    file = "shareinfo_data";
    
    dirscan dir = scan_directories("shareinfo");
    
    FILE * outfile = fopen(file, "w");
    fprintf(outfile, "[");
    for(size_t i = 0; i < dir.cnt; i++){
      logd("looking in: %s\n", dir.files[i]);
      char fnamebuffer2[100];
      sprintf(fnamebuffer2, "shareinfo/%s", dir.files[i]);
      void * rdbuffer = read_file_to_string(fnamebuffer2);
      ASSERT(rdbuffer != NULL);
      void *ptr = rdbuffer;
      char * dirname = udpc_unpack_string(&ptr);
      char * name = udpc_unpack_string(&ptr);
      char * user = udpc_unpack_string(&ptr);
      fprintf(outfile, "{\"path\": \"%s\", \"name\":\"%s\", \"user\":\"%s\"}%s\n",dirname, name, user, (i == dir.cnt -1) ? "" : ",");
      dealloc(rdbuffer);
    }
    fprintf(outfile, "]");
    fclose(outfile);
    dirscan_clean(&dir);
  }else if(url == strstr(url,"/shares/")){
    // fetch the active shares item inside the shares folder
    
    char * shareid = (char *) url + strlen("/shares/");
    logd("Shareid: %s\n", shareid);
    char * shareinfo_filename = fmtstr("shareinfo/%s", shareid);
    void * buffer = read_file_to_string(shareinfo_filename);
    if(buffer == NULL)
      goto error;
    void * bufptr = buffer;
    char * dir = udpc_unpack_string(&bufptr);
    char * name = udpc_unpack_string(&bufptr);
    char * user = udpc_unpack_string(&bufptr);
    service_descriptor sd;
    if(!udpc_get_service_descriptor(user, &sd)){
      logd("Unable to parse service name: '%s'\n", user);
      dealloc(buffer);
      goto error;
    }

    logd("path: %s, name: %s, user: %s\n", dir, name, user);
    update_dirfile(dir, name, user);
    sprintf(fnamebuffer, "shares/%s.json", name);
    dealloc(buffer);
    logd("Sending: %s\n", fnamebuffer);
    file = fnamebuffer;
  }
  if(strcmp(url, "/add_share") == 0){ 
    const char * path = MHD_lookup_connection_value(con, MHD_GET_ARGUMENT_KIND, "path");
    const char * name = MHD_lookup_connection_value(con, MHD_GET_ARGUMENT_KIND, "name");
    const char * user = MHD_lookup_connection_value(con, MHD_GET_ARGUMENT_KIND, "user");
    logd("path: %s, name: %s, user: %s\n", path, name, user);
    if(path == NULL || name == NULL || user == NULL){
      goto error;
    }
    service_descriptor sd;
    if(!udpc_get_service_descriptor(user, &sd)){
      logd("Unable to parse service name: '%s'\n", user);
      goto error;
    }
    logd("Service descriptor seems ok \n");
    ensure_directory("shareinfo/");
    char * sharepath = fmtstr("shareinfo/%s", name);
    struct stat filest;
    stat(sharepath, &filest);
    dealloc(sharepath);
    
    if(S_ISREG(filest.st_mode)){
      logd("File exists!\n");
    }else{
      struct stat dirst;
      stat(path, &dirst);
      if(!S_ISDIR(dirst.st_mode)){
	logd("Dir does not exist.. creating a new one..\n");
	int path_len = strlen(path);
	if(path[path_len] !='/'){
	  char * npath = fmtstr("%s/", path);
	  ensure_directory(npath);
	  dealloc(npath);
	}else{
	  ensure_directory(path);
	}
      }
      logd("Updating dirfile!\n");
      update_dirfile(path, name, user);
      logd("Done..\n");
    }
    const char * r = "\"OK\"";
    struct MHD_Response * response = MHD_create_response_from_data(strlen(r),
								   (void *) r,
								   0,
								   MHD_NO);
    int ret = MHD_queue_response(con, MHD_HTTP_OK, response);
    MHD_destroy_response(response);
    return ret;
  }

  size_t filesize = 0;
  void * pg = read_file_to_buffer(file, &filesize);
  struct MHD_Response * response = MHD_create_response_from_data(filesize,
					   pg,
					   1,
					   MHD_NO);
  int ret = MHD_queue_response(con, MHD_HTTP_OK, response);
  MHD_destroy_response(response);
  return ret;

 error:;
  const char * error_str = "<html><body>400</body></html>";
  response = MHD_create_response_from_data(strlen(error_str) + 1,
					   (void *) error_str,
					   0,
					   MHD_NO);
  ret = MHD_queue_response(con, MHD_HTTP_BAD_REQUEST, response);
  MHD_destroy_response(response);
  return ret;
}