Ejemplo n.º 1
0
/* Prune the db file the first time this directory is opened in a session.  */
void fsdb_clean_dir (a_inode *dir)
{
  TCHAR buf[1 + 4 + 257 + 257 + 81];
  TCHAR *n;
  FILE *f;
  off_t pos1 = 0, pos2;

	if (!dir->nname)
		return;
  n = build_nname (dir->nname, FSDB_FILE);
	f = _tfopen (n, _T("r+b"));
  if (f == 0) {
  	xfree (n);
    return;
  }
  for (;;) {
    pos2 = ftell (f);
    if (fread (buf, 1, sizeof buf, f) < sizeof buf)
      break;
    fsdb_fixup (f, buf, sizeof buf, dir);
    if (buf[0] == 0)
      continue;
    if (pos1 != pos2) {
      fseek (f, pos1, SEEK_SET);
      fwrite (buf, 1, sizeof buf, f);
      fseek (f, pos2 + sizeof buf, SEEK_SET);
    }
  	pos1 += sizeof buf;
  }
  fclose (f);
  my_truncate (n, pos1);
  xfree (n);
}
Ejemplo n.º 2
0
Archivo: fsdb.c Proyecto: engur/PUAE
/* Prune the db file the first time this directory is opened in a session.  */
void fsdb_clean_dir (a_inode *dir)
{
	uae_u8 buf[1 + 4 + 257 + 257 + 81];
	TCHAR *n;
	FILE *f;
	off_t pos1 = 0, pos2;

	if (!dir->nname)
		return;
	n = build_nname (dir->nname, FSDB_FILE);
	f = _tfopen (n, _T("r+b"));
	if (f == 0) {
		xfree (n);
		return;
	}
	for (;;) {
		pos2 = ftell (f);
		if (fread (buf, 1, sizeof buf, f) < sizeof buf)
			break;
		fsdb_fixup (f, buf, sizeof buf, dir);
		if (buf[0] == 0)
			continue;
		if (pos1 != pos2) {
			fseek (f, pos1, SEEK_SET);
			size_t isWritten = fwrite (buf, 1, sizeof buf, f);
			if (isWritten < sizeof(buf))
				write_log("%s:%d [%s] - Failed to write %ld bytes (%ld/%ld)",
							 __FILE__, __LINE__, __FUNCTION__,
							sizeof(buf) - isWritten, isWritten, sizeof(buf));
			fseek (f, pos2 + sizeof buf, SEEK_SET);
		}
		pos1 += sizeof buf;
	}
	fclose (f);
	if (pos1 == 0) {
		kill_fsdb (dir);
	} else {
		my_truncate (n, pos1);
	}
	xfree (n);
}
Ejemplo n.º 3
0
int
main(int argc, char **argv)
{
	char	   *in_filename,
			   *out_filename,
			   *out_filename2;
	char	   *database;
	Oid			lobjOid;
	PGconn	   *conn;
	PGresult   *res;

	if (argc != 5)
	{
		fprintf(stderr, "Usage: %s database_name in_filename out_filename out_filename2\n",
				argv[0]);
		exit(1);
	}

	database = argv[1];
	in_filename = argv[2];
	out_filename = argv[3];
	out_filename2 = argv[4];

	/*
	 * set up the connection
	 */
	conn = PQsetdb(NULL, NULL, NULL, NULL, database);

	/* check to see that the backend connection was successfully made */
	if (PQstatus(conn) != CONNECTION_OK)
	{
		fprintf(stderr, "Connection to database failed: %s",
				PQerrorMessage(conn));
		exit_nicely(conn);
	}

	res = PQexec(conn, "begin");
	PQclear(res);
	printf("importing file \"%s\" ...\n", in_filename);
/*	lobjOid = importFile(conn, in_filename); */
	lobjOid = lo_import(conn, in_filename);
	if (lobjOid == 0)
		fprintf(stderr, "%s\n", PQerrorMessage(conn));
	else
	{
		printf("\tas large object %u.\n", lobjOid);

		printf("picking out bytes 4294967000-4294968000 of the large object\n");
		pickout(conn, lobjOid, 4294967000U, 1000);

		printf("overwriting bytes 4294967000-4294968000 of the large object with X's\n");
		overwrite(conn, lobjOid, 4294967000U, 1000);

		printf("exporting large object to file \"%s\" ...\n", out_filename);
/*		exportFile(conn, lobjOid, out_filename); */
		if (lo_export(conn, lobjOid, out_filename) < 0)
			fprintf(stderr, "%s\n", PQerrorMessage(conn));

		printf("truncating to 3294968000 bytes\n");
		my_truncate(conn, lobjOid, 3294968000U);

		printf("exporting truncated large object to file \"%s\" ...\n", out_filename2);
		if (lo_export(conn, lobjOid, out_filename2) < 0)
			fprintf(stderr, "%s\n", PQerrorMessage(conn));
	}

	res = PQexec(conn, "end");
	PQclear(res);
	PQfinish(conn);
	return 0;
}
Ejemplo n.º 4
0
int main(int argc, char** argv)
{
	char TMPFILE[] = "tmp1234";
	FILE *in = NULL;
	FILE *out = NULL;

	// we need one argument
	if (argc != 2)
	{
		printf("%s <file to decrypt>\n", argv[0]);
		return (-99);
	}

	// open input file
	if ( (in = fopen(argv[1], "rb")) == NULL )
	{
		printf("error: canot open file [%s] for reading\n", argv[1]);
		return (-1);
	}
	printf("open '%s' for reading\n", argv[1]);

	// open output file
	if ( (out = fopen(TMPFILE, "wb")) == NULL )
	{
		printf("error: open file [%s] for writing\n", TMPFILE);
		return (-3);
	}
	printf("open tmpfile for writing\n");

// get header from input file
	Header header;
	byte header_md5[MY_MD5_DIGEST_LENGTH];
	char *filename = NULL;
	size_t size_read;

	fseek(in, 0, SEEK_SET);
	size_read = fread(&header, sizeof(header)-sizeof(char*), 1, in);
	assert( size_read != 0 );

	filename = (char*)malloc(header.FileNameLength);
	size_read = fread(filename, header.FileNameLength, 1, in);
	assert( size_read != 0 );

	header.FileName = filename;

	memcpy(header_md5, header.header_md5, MY_MD5_DIGEST_LENGTH);
//	PrintHashValue((byte*)&header, header.HeaderSize);
	byte calc_md5[MY_MD5_DIGEST_LENGTH];
//	memset(header.reserved2, 0, MY_MD5_DIGEST_LENGTH);
	memset(header.header_md5, 0, MY_MD5_DIGEST_LENGTH);
	VerifyHeaderMD5(header, calc_md5);
	if (memcmp(calc_md5, header_md5, MY_MD5_DIGEST_LENGTH) == 0)
	{
		printf("ok\n");
	}
	else
	{
		printf("header is invalid\n");
		exit (-1);
	}

	fseek(in, header.HeaderSize, SEEK_SET);
	fseek(out, 0, SEEK_SET);
	//size_t in_pos = ftell(in);
	//printf("start read content from: %d\n", in_pos);
	// decrypt & output
	EncryptFileByRC4(in, out);
	fflush(out);
// close files
	fclose(in);
	fclose(out);

	exit(-2);

#if 0
// truncate file
	my_truncate(TMPFILE, header.FileSize);
#endif

// final check the output file
	FILE* check = NULL;
	if ( (check = fopen(TMPFILE, "rb")) == NULL )
	{
		printf("open tmpfile for checking md5 error\n");
	}
	else
	{
		size_t total_read = 0, file_size = 0;
		byte md5_hash[MY_MD5HASH_LEN];

		fseek(check, 0, SEEK_SET);
		CalculateFileMD5(check, md5_hash, total_read);

		if ( memcmp(md5_hash, header.file_md5, MY_MD5HASH_LEN) != 0 )
		{
			printf("md5 checksum invalid\n");
			printf("calculated md5 = ");
			PrintHashValue(md5_hash, MY_MD5HASH_LEN);
			printf("recorded md5 = ");
			PrintHashValue(header.file_md5, MY_MD5HASH_LEN);
		}
		if ( total_read != header.FileSize )
		{
			printf("size not matched %lu vs %lu\n", total_read, file_size);
		}
	}

	// renmae tmp file to actual file
	rename(TMPFILE, header.FileName);

	free(filename);

	return 0;
}