示例#1
0
/*
 * SameFile - check if two files are the same
 */
bool SameFile( char *f1, char *f2 )
{
    char        full1[FILENAME_MAX];
    char        full2[FILENAME_MAX];

    if( !file_cmp( f1, f2 ) ) {
        return( TRUE );
    }
    _fullpath( full1, f1, FILENAME_MAX );
    _fullpath( full2, f2, FILENAME_MAX );
    if( !file_cmp( full1, full2 ) ) {
        if( EditFlags.SameFileCheck ) {
            return( TRUE );
        }
        EditFlags.DuplicateFile = TRUE;
    }
    return( FALSE );

} /* SameFile */
示例#2
0
OSD_FILE *osd_fopen( int type, const char *path, const char *mode )
{
  int i;
  OSD_FILE	*st;

  st = NULL;
  for( i=0; i<MAX_STREAM; i++ ){	/* 空きバッファを探す */
    if( osd_stream[i].fp == NULL ){		/* fp が NULL なら空き */
      st = &osd_stream[i];
      break;
    }
  }
  if( st == NULL ) return NULL;			/* 空きがなければ NG */
  st->path = NULL;



  switch( type ){

  case FTYPE_DISK:		/* "r+b" , "rb"	*/
  case FTYPE_TAPE_LOAD:		/* "rb" 	*/
  case FTYPE_TAPE_SAVE:		/* "ab"		*/
  case FTYPE_PRN:		/* "ab"		*/
  case FTYPE_COM_LOAD:		/* "rb"		*/
  case FTYPE_COM_SAVE:		/* "ab"		*/

    for( i=0; i<MAX_STREAM; i++ ){	/* ファイルがすでに開いてないか検索 */
      if( osd_stream[i].fp ){
						/* すでに開いているならば   */
	if( file_cmp( osd_stream[i].path, path ) ){

	  if( type == FTYPE_DISK                    &&	/* DISKの場合のみ、 */
	      osd_stream[i].type == type            &&	/* 同じモードならば */
	      strcmp( osd_stream[i].mode, mode )==0 ){	/* それを返す       */

	    return &osd_stream[i];

	  }else{					/* DISK以外、ないし */
	    return NULL;				/* 違うモードならNG */
	  }
	}
      }
    }
					/* ファイル名保持用のバッファを確保 */
    st->path = malloc( strlen(path) +1 );
    if( st->path == NULL ){
      return NULL;
    }
    /* FALLTHROUGH */


  default:
    st->fp = fopen( path, mode );	/* ファイルを開く */

    if( st->fp ){

      st->type = type;
      if( st->path )
	strcpy( st->path, path );
      strncpy( st->mode, mode, sizeof(st->mode) );
      return st;

    }else{

      if( st->path ){
	free( st->path );
	st->path = NULL;
      }
      return NULL;
    }
  }
}
static int verify_verity(const int meta_version, const int dm_verity_version,
                         const char * data_device, const size_t block_size) {
	int r = -1;
	long data_blocks;
	int fd = -1;
	char *table;
	int ret;
	printf("verify_verity\n");
	fflush(stdout);

	ret = verify_verity_header(data_device, meta_version, block_size, &table, &data_blocks);
	if (0 != ret) {
		fprintf(stderr, "Failed to verify verity header\n");
		return -1;
	}

	printf("table 1: %s\n", table);
    fflush(stdout);
    //
    /////////////now verify hash tree
    char * version_str = strtok(table, " ");
    char * data_dev_str = strtok(NULL, " ");
    char * hash_dev_str = strtok(NULL, " ");
    char * data_blk_size_str = strtok(NULL, " ");
    char * hash_blk_size_str = strtok(NULL, " ");
    char * data_blocks_size_str = strtok(NULL, " ");
    char * hash_start_size_str = strtok(NULL, " ");
    char * alg_str = strtok(NULL, " ");
    char * digest_str = strtok(NULL, " ");
    char * salt_str = strtok(NULL, " ");

    if (dm_verity_version != atoi(version_str)) {
        printf("wrong version in table\n");
        goto exit_malloc;
    }
    if (0 != strcmp(data_dev_str, data_device)) {
        printf("wrong data device in table");
        goto exit_malloc;
    }
    if ((ssize_t)block_size != atoi(data_blk_size_str)) {
        printf("wrong block_size in table\n");
        goto exit_malloc;
    }
    if ((ssize_t)block_size != atoi(hash_blk_size_str)) {
        printf("wrong block_size in table\n");
        goto exit_malloc;
    }
    if (data_blocks != atoi(data_blocks_size_str)) {
        printf("wrong data_blocks in table\n");
        goto exit_malloc;
    }
    if (strlen(digest_str) % 2) {
        printf("wrong digest size in table\n");
        goto exit_malloc;
    }
    if (strlen(salt_str) % 2) {
        printf("wrong salt size in table\n");
        goto exit_malloc;
    }
    int digest_size = strlen(digest_str) / 2;
    int salt_size = strlen(salt_str) / 2;
    char * table_digest = malloc(digest_size);
    char * salt = malloc(salt_size);
    const char * tmp_hash_file = TMP_HASH_FILE;
    char * digest = malloc(digest_size);
    unlink(tmp_hash_file);
    if (hex_to_bytes(salt_str, salt) < 0) {
        printf("wrong salt in table\n");
        goto exit_malloc2;
    }
    if (hex_to_bytes(digest_str, table_digest) < 0) {
        printf("wrong digest in table\n");
        goto exit_malloc2;
    }
    printf("%d %d %x %x\n", digest_size, salt_size, digest, salt);
    fflush(stdout);
    if (VERITY_create_hash(dm_verity_version, alg_str, tmp_hash_file, data_device,
                           block_size, block_size, data_blocks, 0, (unsigned char *)digest,
                           digest_size, (const unsigned char*)salt, salt_size)) {
        printf("error calculation hash\n");
        goto exit_malloc2;
    }
    if (0 != memcmp(digest, table_digest, digest_size)) {
        printf("root hash digest mismatch\n");
        goto exit_malloc2;
    }
#if 0
    const size_t hash_start = atoi(hash_start_size_str);
    const size_t hash_off = hash_start * block_size;
    if (file_cmp(hash_dev_str, tmp_hash_file, hash_off, 0, 1024 * 1024)) {
        goto exit_malloc2;
    }
#endif
    printf("verity verified\n");
    r = 0;
 exit_malloc2: if (table_digest)
        free(table_digest);
    if (salt)
        free(salt);
    if (digest)
        free(digest);
 exit_malloc: if (table)
        free(table);
 exit_open: if (fd >= 0)
        close(fd);
    unlink(tmp_hash_file);
    return r;
}