⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jvm_file.c

📁 java 1.1 gemini 08_16
💻 C
📖 第 1 页 / 共 4 页
字号:
 *  encrypt_value
 * DESCRIPTION
 *  
 * PARAMETERS
 *  pos             [IN]        
 *  base_value      [IN]        
 *  value           [IN]        
 * RETURNS
 *  
 *****************************************************************************/
__inline kal_uint32 encrypt_value(kal_uint32 pos, kal_uint32 base_value, kal_uint32 value)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint32 c;
    kal_uint32 temp_c;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    c = encrypt_table[(base_value ^ pos) >> 24] ^ (base_value >> 8);
    c = encrypt_table[((c ^ pos) << 8) >> 24] ^ (c >> 8);
    c = encrypt_table[((c ^ pos) << 16) >> 24] ^ (c >> 8);
    c = encrypt_table[((c ^ pos) << 24) >> 24] ^ (c >> 8);
    temp_c = (((c >> 8) ^ value) << 24) >> 24;
    return ((c ^ decrypt_table[temp_c]) << 24) >> 24;
}


/*****************************************************************************
 * FUNCTION
 *  get_base_code
 * DESCRIPTION
 *  
 * PARAMETERS
 *  handle      [IN]        
 * RETURNS
 *  
 *****************************************************************************/
kal_uint32 get_base_code(int handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint32 c = encrypt_code;
    kal_uint8 temp_buffer[1];
    kal_uint32 real_count;
    kal_int32 file_size = FS_Seek(handle, 0, SEEK_END);
    kal_int32 scan_pos = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (file_size != 0)
    {
        while (scan_pos < file_size)
        {
            FS_Seek(handle, scan_pos, SEEK_SET);
            FS_Read(handle, (void*)temp_buffer, 1, &real_count);
            c = encrypt_table[((c ^ temp_buffer[0]) << 24) >> 24] ^ (c >> 8);
            scan_pos += 1024;
        }
        FS_Seek(handle, 0, SEEK_SET);
        return c;
    }
    else
    {
        return 0xFA;
    }
}


/*****************************************************************************
 * FUNCTION
 *  encrypt_file
 * DESCRIPTION
 *  
 * PARAMETERS
 *  handle      [IN]        
 * RETURNS
 *  
 *****************************************************************************/
kal_int32 encrypt_file(kal_int32 handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 file_size = FS_Seek(handle, 0, SEEK_END);
    kal_uint32 scan_pos = 0;
    kal_uint32 base_code = encrypt_code;
    kal_uint8 temp_buffer[1];
    kal_uint32 real_count;
    kal_int32 return_code = J2ME_NO_ERROR;
    kal_uint8 *encrypt_buffer = (kal_uint8*) jam_listmem_malloc(1024);
    kal_int32 i;
    kal_int32 scan_length = 1023;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (file_size < 0)
    {
        return_code = J2ME_ACTION_FAILE;
        goto encrypt_fail;
    }
    while (scan_pos < file_size)
    {
        FS_Seek(handle, scan_pos, SEEK_SET);
        FS_Read(handle, (void*)temp_buffer, 1, &real_count);
        temp_buffer[0] = encrypt_value(scan_pos, encrypt_code, temp_buffer[0]);
        base_code = encrypt_table[((temp_buffer[0] ^ base_code) << 24) >> 24] ^ (base_code >> 8);
        FS_Seek(handle, scan_pos, SEEK_SET);
        FS_Write(handle, (void*)temp_buffer, 1, &real_count);
        scan_pos += 1024;
    }
    if (clean_event() == J2ME_GET_NEXT_EVENT_ABORT)
    {
        return_code = J2ME_USER_CANCEL;
        goto encrypt_fail;
    }
    scan_pos = 1;
    do
    {
        if (scan_pos + scan_length > file_size)
        {
            scan_length = file_size - scan_pos;
        }
        if (FS_Seek(handle, scan_pos, SEEK_SET) < 0)
        {
            return_code = J2ME_ACTION_FAILE;
            goto encrypt_fail;
        }
        FS_Read(handle, encrypt_buffer, scan_length, &real_count);
        if (real_count != scan_length)
        {
            return_code = J2ME_ACTION_FAILE;
            goto encrypt_fail;
        }
        i = real_count - 1;
        do
        {
            encrypt_buffer[i] = encrypt_value(scan_pos + i, base_code, encrypt_buffer[i]);
            i--;
        } while (i >= 0);
        if (FS_Seek(handle, scan_pos, SEEK_SET) < 0)
        {
            return_code = J2ME_ACTION_FAILE;
            goto encrypt_fail;
        }
        FS_Write(handle, encrypt_buffer, scan_length, &real_count);
        if (real_count != scan_length)
        {
            return_code = J2ME_ACTION_FAILE;
            goto encrypt_fail;
        }

        if (clean_event() == J2ME_GET_NEXT_EVENT_ABORT)
        {
            return_code = J2ME_USER_CANCEL;
            goto encrypt_fail;
        }
        scan_pos += scan_length + 1;
    } while (scan_pos < file_size);

  encrypt_fail:
    jam_listmem_free(encrypt_buffer);
    return return_code;
}


/*****************************************************************************
 * FUNCTION
 *  decrypt_buffer
 * DESCRIPTION
 *  
 * PARAMETERS
 *  buf             [?] 
 *  base_code       [IN]        
 *  pos             [IN]        
 *  len             [IN]        
 * RETURNS
 *  
 *****************************************************************************/
kal_bool decrypt_buffer(kal_uint8 *buf, kal_uint32 base_code, kal_uint32 pos, kal_int32 len)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint32 scan_pos = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    while (scan_pos < len)
    {
        if (((scan_pos + pos << 22) >> 22) != 0)
        {
            buf[scan_pos] = (get_org_encrypt_value(scan_pos + pos, buf[scan_pos], base_code) << 24) >> 24;
        }
        else
        {
            buf[scan_pos] = (get_org_encrypt_value(scan_pos + pos, buf[scan_pos], encrypt_code) << 24) >> 24;
        }
        scan_pos++;
    }
    return KAL_TRUE;
}

#endif /* ENCRYPT_JAR_FILE */ 


/*****************************************************************************
 * FUNCTION
 *  jvm_file_initialize
 * DESCRIPTION
 *  
 * PARAMETERS
 *  type        [IN]        
 * RETURNS
 *  
 *****************************************************************************/
int jvm_file_initialize(game_type_enum type)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_int32 attribute;
    kal_char *dir_path;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* Check whether the directory exists? */
    dir_path = _jvm_get_dir_path(type);
    kal_wsprintf(WCHARFilename, "%s", dir_path);
    attribute = FS_GetAttributes(WCHARFilename);

    if (attribute > 0)
    {
        /* this directory maybe exists */
        if ((attribute & FS_ATTR_DIR) != FS_ATTR_DIR)
        {
            return -1;
        }
    }
    else
    {
        /* this directory doesn't exist. Create this directory */
        if (FS_CreateDir(WCHARFilename) != FS_NO_ERROR)
        {
            return -1;
        }
    }

#ifdef ENCRYPT_JAR_FILE
    if (!jvm_file_initailized)
    {
        jvm_file_initailized = KAL_TRUE;
        make_encrypt_table(type);
    }
#endif /* ENCRYPT_JAR_FILE */ 

    /* Successfully initialized */
    return 0;
}


/*****************************************************************************
 * FUNCTION
 *  jvm_file_finalize
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void jvm_file_finalize(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
}


/*****************************************************************************
 * FUNCTION
 *  jvm_file_delete
 * DESCRIPTION
 *  
 * PARAMETERS
 *  filename        [IN]        
 * RETURNS
 *  
 *****************************************************************************/
int jvm_file_delete(const char *filename)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (is_system_file((char*)filename))
    {
        kal_wsprintf(WCHARFilename, "%s\\%s", work_sys_dir_path, filename);
    }
    else
    {
        kal_wsprintf(WCHARFilename, "%s\\%s", work_dir_path, filename);
    }

    /* this is to prevent removing a read only file */
    FS_SetAttributes(WCHARFilename, 0);
    if (FS_Delete(WCHARFilename) == FS_NO_ERROR)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}


/*****************************************************************************
 * FUNCTION
 *  jvm_file_rename
 * DESCRIPTION
 *  
 * PARAMETERS
 *  oldfilename     [IN]        
 *  newfilename     [IN]        
 * RETURNS
 *  
 *****************************************************************************/
int jvm_file_rename(const char *oldfilename, const char *newfilename)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    WCHAR newFilename[MAX_WCHAR_FILENAME_LEN];
    int i, file_count = work_info_ptr->virtual_file_info.virtual_file_count;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* a real file should not overwrite a pre-install file */
    for (i = 0; i < file_count; i++)
    {
        if (strcmp(newfilename, work_info_ptr->virtual_file_info.virtual_file_name[i]) == 0)
        {
            return -1;
        }
    }

    if (is_system_file((char*)oldfilename))
    {
        kal_wsprintf(WCHARFilename, "%s\\%s", work_sys_dir_path, oldfilename);
    }
    else
    {
        kal_wsprintf(WCHARFilename, "%s\\%s", work_dir_path, oldfilename);
    }

    if (is_system_file((char*)newfilename))
    {
        kal_wsprintf(newFilename, "%s\\%s", work_sys_dir_path, newfilename);
    }
    else
    {
        kal_wsprintf(newFilename, "%s\\%s", work_dir_path, newfilename);
    }

    if (FS_Move(WCHARFilename, newFilename, FS_MOVE_KILL | FS_MOVE_OVERWRITE, NULL, NULL, 0) == FS_NO_ERROR)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}


/*****************************************************************************
 * FUNCTION
 *  jvm_file_open
 * DESCRIPTION
 *  
 * PARAMETERS
 *  filename        [IN]        
 *  flags           [IN]        
 * RETURNS
 *  
 *****************************************************************************/
int jvm_file_open(const char *filename, int flags)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int handle;
    int file_count = work_info_ptr->virtual_file_info.virtual_file_count;
    int i;
    pvfile file;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (is_system_file((char*)filename))
    {
        kal_wsprintf(WCHARFilename, "%s\\%s", work_sys_dir_path, filename);
    }
    else
    {
        kal_wsprintf(WCHARFilename, "%s\\%s", work_dir_path, filename);
    }

    handle = DRM_open_file(WCHARFilename, flags, DRM_PERMISSION_EXECUTE);

    if (handle < 0)
    {
        for (i = 0; i < file_count; i++)
        {
            if (strcmp(filename, work_info_ptr->virtual_file_info.virtual_file_name[i]) == 0)
            {
                file = (pvfile) get_ctrl_buffer(sizeof(vfile_struct));

                file->file_tag = VIRTUAL_FILE_TAG;
                file->h.position = 0;
                file->f.filesize = work_info_ptr->virtual_file_info.virtual_file_size[i];
                file->filedata = (kal_uint8*) work_info_ptr->virtual_file_info.virtual_file_data[i];
                return (int)file;
            }
        }

        /* the file is neigher in real file system nor virtual file system */
        return -1;
    }

    file = (pvfile) get_ctrl_buffer(sizeof(vfile_struct));
    file->file_tag = NORMAL_FILE_TAG;
    file->h.handler = handle;

#if defined(__DRM_SUPPORT__)
    if (file->file_tag == NORMAL_FILE_TAG && DRM_get_object_method(handle, NULL) != DRM_METHOD_NONE)
    {
        file->file_tag = DRM_FILE_TAG;
    }
#endif /* defined(__DRM_SUPPORT__) */ 

#if defined(ENCRYPT_JAR_FILE)
    if (file->file_tag == NORMAL_FILE_TAG && strcmp((filename + strlen(filename) - 4), ".jar") == 0)
    {
        file->f.encrypt_code = get_base_code(handle);
        file->file_tag = ENCRYPTED_JAR_FILE;
    }
#endif /* defined(ENCRYPT_JAR_FILE) */ 
    return (int)file;
}


/*****************************************************************************
 * FUNCTION
 *  jvm_file_close
 * DESCRIPTION
 *  
 * PARAMETERS
 *  handle      [IN]        
 * RETURNS
 *  
 *****************************************************************************/
int jvm_file_close(int handle)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    pvfile file = (pvfile) handle;
    int h;
    int result;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -