Beispiel #1
0
static int lua_addCert(lua_State *L)
{
	int argc = lua_gettop(L);
	#ifndef SKIP_ERROR_HANDLING
	if (argc != 1)  return luaL_error(L, "wrong number of arguments");
	#endif
	const char *text = luaL_checkstring(L, 1);
	fileStream fileHandle;
	if (strncmp("romfs:/",text,7) == 0){
		fileHandle.isRomfs = true;
		FILE* handle = fopen(text,"r");
		#ifndef SKIP_ERROR_HANDLING
		if (handle == NULL) return luaL_error(L, "file doesn't exist.");
		#endif
		fileHandle.handle = (u32)handle;
	}else{
		fileHandle.isRomfs = false;
		FS_Path filePath = fsMakePath(PATH_ASCII, text);
		FS_Archive script=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}};
		Result ret = FSUSER_OpenFileDirectly( &fileHandle.handle, script, filePath, FS_OPEN_READ, 0x00000000);
		#ifndef SKIP_ERROR_HANDLING
		if (ret) return luaL_error(L, "file doesn't exist.");
		#endif
	}
	u64 cert_size;
	u32 bytesRead;
	FS_GetSize(&fileHandle, &cert_size);
	u8* cert = (u8*)malloc(cert_size);
	FS_Read(&fileHandle, &bytesRead, 0, cert, cert_size);
	sslcAddTrustedRootCA(RootCertChain_contexthandle, cert, cert_size, NULL);
	free(cert);
	FS_Close(&fileHandle);
	return 0;
}
//*********************************************************************************
//  Function:		CETYPE	FS_Recover(FSFILE* filePtr)
//---------------------------------------------------------------------------------
//  Summary:		Recovers the length of a 0-length file identified by the filePtr
//					based upon the number of allocated clusters
//  Input:
//    				filePtr -  	Pointer to the File Object structure identifying the
//								file to be recovered
//  Return Values:
//    				CE_GOOD	-	File was recovered successfully
//								Other return codes may identify various error 
//								conditions as required.
//*********************************************************************************
CETYPE	FS_Recover(FSFILE* filePtr)
	{
 	//----------------------------------------------------------------
	// Validate conditions and parameters
 	//----------------------------------------------------------------
 	if (!SDFS_InitStatus)
 		return CE_NOT_INIT;		// FS is not initialized!
 	if (NULL == filePtr)
 		return CE_INVALID_ARGUMENT;		
 	//----------------------------------------------------------------
    // Check if this is a file and not a Directory
    if( FS_IsDirectory(filePtr) )
        return CE_DIRECTORY;
    // Check if the file is actually 0-length
    if( FS_GetSize(filePtr) > 0 )
        return CE_GOOD;
	//-----------------------------------------------------------------
	DWORD	ClsSize	= (DWORD)SDFS_VolumeInfo.SecPerClus 
						   * SDFS_VolumeInfo.sectorSize;
	DWORD	ClsNum	= SDFS_DirGetFirstCluster(&filePtr->dirFileEntry);
	//-----------------------------------------------------------------
	while (ClsNum < EOC_CLUSTER_FAT32)
		{
	    //-------------------------------------------------------
		// Process cluster chain
	    //-------------------------------------------------------
	    // The first 2 clusters in FAT32 are reserved...
	    if	( ClsNum < 2)  // Cluster Number can't be "0" and "1"
	    	return CE_RECOVERY_FAIL;
	    //-------------------------------------------------------
	    filePtr->dirFileEntry.DIR_FileSize += ClsSize;
	    //-------------------------------------------------------
	    // Load next cluster in the chain
	    //-------------------------------------------------------
	    ClsNum = SDFS_FATCacheRead(ClsNum);
		}
	//----------------------------------------------------------------- 
	// Flush updates to disk
	//----------------------------------------------------------------- 
	return FS_Flush(filePtr);
	}