vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / ape_reader / MAClib / GlobalFunctions.cpp
blob3ee42537000eeca2d9775fe89006d72d106ee453
1 #include "All.h"
2 #include "GlobalFunctions.h"
3 #include "IO.h"
5 #include "CharacterHelper.h"
7 #if 0
8 BOOL GetMMXAvailable ( void ) // 適当
10 return FALSE;
12 #endif
15 #ifndef __GNUC_IA32__
17 extern "C" BOOL GetMMXAvailable(void)
19 #ifdef ENABLE_ASSEMBLY
21 unsigned long nRegisterEDX;
23 try
25 __asm mov eax, 1
26 __asm CPUID
27 __asm mov nRegisterEDX, edx
29 catch(...)
31 return FALSE;
34 if (nRegisterEDX & 0x800000)
35 RETURN_ON_EXCEPTION(__asm emms, FALSE)
36 else
37 return FALSE;
39 return TRUE;
41 #else
42 return FALSE;
43 #endif
46 #endif // #ifndef __GNUC_IA32__
49 int ReadSafe(CIO * pIO, void * pBuffer, int nBytes)
51 unsigned int nBytesRead = 0;
52 int nRetVal = pIO->Read(pBuffer, nBytes, &nBytesRead);
53 if (nRetVal == ERROR_SUCCESS)
55 if (nBytes != int(nBytesRead))
56 nRetVal = ERROR_IO_READ;
59 return nRetVal;
62 int WriteSafe(CIO * pIO, void * pBuffer, int nBytes)
64 unsigned int nBytesWritten = 0;
65 int nRetVal = pIO->Write(pBuffer, nBytes, &nBytesWritten);
66 if (nRetVal == ERROR_SUCCESS)
68 if (nBytes != int(nBytesWritten))
69 nRetVal = ERROR_IO_WRITE;
72 return nRetVal;
75 BOOL FileExists(wchar_t * pFilename)
77 if (0 == wcscmp(pFilename, "-") || 0 == wcscmp(pFilename, "/dev/stdin"))
78 return TRUE;
80 #ifdef _WIN32
82 BOOL bFound = FALSE;
84 WIN32_FIND_DATA WFD;
85 HANDLE hFind = FindFirstFile(pFilename, &WFD);
86 if (hFind != INVALID_HANDLE_VALUE)
88 bFound = TRUE;
89 CloseHandle(hFind);
92 return bFound;
94 #else
96 CSmartPtr<char> spANSI(GetANSIFromUTF16(pFilename), TRUE);
98 struct stat b;
100 if (stat(spANSI, &b) != 0)
101 return FALSE;
103 if (!S_ISREG(b.st_mode))
104 return FALSE;
106 return TRUE;
108 #endif