Esempio n. 1
0
static FILE* open_file(const arg_char* name) {
#ifdef _WIN32
    // Open the file allowing shared read, but not shared write
    int fd;
    _wsopen_s(
        &fd, name, _O_RDWR | _O_BINARY | _O_NOINHERIT, _SH_DENYWR,
        _S_IREAD | _S_IWRITE
    );
    if(fd == -1) {
        return NULL;
    }
    FILE* file = _fdopen(fd, "r+b");
    if(file == NULL) {
        _close(fd);
        return NULL;
    }
    setvbuf(file, NULL, _IOFBF, FileBufferLen);
    return file;
#else
    FILE* file = fopen(name, "r+b");
    if(file != NULL) {
        setvbuf(file, NULL, _IOFBF, FileBufferLen);
    }
    return file;
#endif // _WIN32
}
Esempio n. 2
0
int mesage_SaveFieldBinary(lua_State* L) {
	int err = 0;
	char *b = NULL;
	CMessage* msg = cmessage_arg(L, "mesage_AddFieldBinary");
	CString fldName = luaL_checkstring(L, 2);
	CString path = luaL_checkstring(L, 3);
	char *description;
	DWORD l;

	CDatum *d = msg->GetDatum(fldName);
	if (d->GetVarType() != (VT_ARRAY | VT_UI1) ) {
		err = -1;
		description = "Not Binary Data";
		goto err;
	}
	{
		l = d->GetDataSize();
		b = new char[l];


		SAFEARRAY* pArray = d->value().parray;
		ASSERT(pArray->cDims == 1); // check we have 1 dimension array
		ASSERT(l == pArray->rgsabound[0].cElements * pArray->cbElements); // get size of array
		memcpy(b, (BYTE*)pArray->pvData, l);

		int charsLen = ::MultiByteToWideChar(CP_UTF8, 0, path, lstrlen(path), NULL, 0);
		std::wstring characters(charsLen, '\0');
		::MultiByteToWideChar(CP_UTF8, 0, path, lstrlen(path), &characters[0], charsLen);

		int pf;

		err = _wsopen_s(&pf, characters.c_str(), _O_BINARY | _O_CREAT | _O_TRUNC | _O_WRONLY, _SH_DENYRW, _S_IWRITE);
		if (err) {
			description = "Open File Error";
			goto err;
		}
		if (l != _write(pf, (b), l)) {
			err = -2;
			description = "Write File Error";
			goto err;
		}

		err = _close(pf);
		if (err){
			description = "Close File Error";
			goto err;
		}
	}
err:
	if (b)
		delete[]b;
	lua_pushinteger(L, err);
	if (err)
		lua_pushstring(L, description);
	else
		lua_pushinteger(L, l);
	return 2;
}
Esempio n. 3
0
int mesage_AddFieldBinary(lua_State* L) {
	CMessage* msg = cmessage_arg(L, "mesage_AddFieldBinary");
	CString fldName = luaL_checkstring(L, 2);
	CString path = luaL_checkstring(L, 3); 
	int err;
	char *description;

	int charsLen = ::MultiByteToWideChar(CP_UTF8, 0, path, lstrlen(path), NULL, 0);
	std::wstring characters(charsLen, '\0');
	::MultiByteToWideChar(CP_UTF8, 0, path, lstrlen(path), &characters[0], charsLen);

	int pf;
	char *b = NULL;
	err = _wsopen_s(&pf, characters.c_str(), _O_BINARY | _O_RDONLY, _SH_DENYWR, _S_IREAD);
	if (err) {
		description = "Open File error";
		goto err;
	}

	DWORD l = _filelength(pf);

	b = new char[l];


	if (l != _read(pf, b, l)) {
		err = -1;
		description = "Read File error";
		goto err;
	}
	err = _close(pf);
	if (err) {
		description = "Close File error";
		goto err;
	}
	{
		COleSafeArray arr;
		arr.Create(VT_UI1, 1, &l);

		for (DWORD i = 0; i < l; i++) {

			arr.PutElement((long*)&i, &b[i]);
		}

		msg->AddDatum(fldName, arr);
	}

err:
	if (b)
		delete []b;
	lua_pushinteger(L, err);
	if (err)
		lua_pushstring(L, description);
	else
		lua_pushinteger(L, l);
	return 2;
}
Esempio n. 4
0
/* Function to wrap wide character equivalent of open()
 */
int libewf_common_wide_open( const wchar_t *filename, uint8_t flags )
{
#if defined( HAVE_WINDOWS_API )
	int file_descriptor  = 0;
#endif
	int open_flags       = 0;

	if( filename == NULL )
	{
		LIBEWF_WARNING_PRINT( "libewf_common_wide_open: invalid filename.\n" );

		return( -1 );
	}
	if( flags == LIBEWF_OPEN_READ )
	{
#if defined( HAVE_WINDOWS_API )
		open_flags = _O_RDONLY | _O_BINARY;
#else
		open_flags = O_RDONLY;
#endif
	}
	else if( flags == LIBEWF_OPEN_WRITE )
	{
#if defined( HAVE_WINDOWS_API )
		open_flags = _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY;
#else
		open_flags = O_WRONLY | O_CREAT | O_TRUNC;
#endif
	}
	else if( flags == LIBEWF_OPEN_READ_WRITE )
	{
#if defined( HAVE_WINDOWS_API )
		open_flags = _O_RDWR | _O_BINARY;
#else
		open_flags = O_RDWR ;
#endif
	}
	else
	{
		LIBEWF_WARNING_PRINT( "libewf_common_wide_open: flags not supported.\n" );

		return( -1 );
	}
#if defined( HAVE_WINDOWS_API )
	if( _wsopen_s( &file_descriptor, filename, open_flags, _SH_DENYRW, ( _S_IREAD | _S_IWRITE ) ) != 0 )
	{
		LIBEWF_WARNING_PRINT( "libewf_common_wide_open: error opening file.\n" );

		return( -1 );
	}
	return( file_descriptor );
#else
#error libewf_common_wide_open: missing wide character equivalent of open()
#endif
}
 errno_t
 __cdecl
 My_sopen_s(
     _Out_ int * _FileHandle,
     _In_z_ const char * _Filename,
     _In_ int _OpenFlag,
     _In_ int _ShareFlag,
     _In_ int _PermissionMode
 )
 {
     return _wsopen_s( _FileHandle, FromUTF8( _Filename ), _OpenFlag, _ShareFlag, _PermissionMode );
 }
static FILE *
    hdb_open_file(TSK_TCHAR *file_path)
{
    FILE *file = NULL;

#ifdef TSK_WIN32
    int fd = 0;
    if (_wsopen_s(&fd, file_path, _O_RDONLY | _O_BINARY, _SH_DENYNO, 0) == 0) {
        file = _wfdopen(fd, L"rb");
    }
#else
    file = fopen(file_path, "rb");
#endif

    return file;
}
//
// Debug string to file or message box.
//
void		CMy412MA0002TestAppDlg::UTLOut(TCHAR *strout)
{
	int			fOut, nBytes;
	if (strout == NULL) {
		DeleteFile(m_Logfile);
		return;
	}
	// U8TEXT ... Really...REALLY!
	if (!(_wsopen_s(&fOut, m_Logfile, _O_CREAT | _O_U8TEXT | _O_APPEND | _O_WRONLY, _SH_DENYNO, _S_IREAD | _S_IWRITE))) {
		nBytes = wcslen(strout) * 2;
		if ((_write(fOut, strout, nBytes)) == -1){
			;//DSWOut( L"Write failed on output\n" );
		}
		_close(fOut);
	}
	else
		;//Failed to open
}
bool os_file_truncate(const std::string &fn, int64 fsize)
{
	int fh;
	if( _wsopen_s ( &fh, ConvertToWchar(fn).c_str(), _O_RDWR | _O_CREAT, _SH_DENYNO,
            _S_IREAD | _S_IWRITE ) == 0 )
	{
		if( _chsize_s( fh, fsize ) != 0 )
		{
			_close( fh );
			return false;
		}
		_close( fh );
		return true;
	}
	else
	{
		return false;
	}
}
Esempio n. 9
0
int
wmain(int argc, wchar_t** argv)
{
  if (argc != 3) {
    printf("Usage: redit <exe file> <icon file>\n");
    return 1;
  }

  mozilla::ScopedClose file;
  if (0 != _wsopen_s(&file.rwget(),
                     argv[2],
                     _O_BINARY | _O_RDONLY,
                     _SH_DENYWR,
                     _S_IREAD)
  || (-1 == file)) {
    fprintf(stderr, "Unable to open icon file.\n");
    return 1;
  }

  // Load all the data from the icon file
  long filesize = _filelength(file);
  nsAutoArrayPtr<BYTE> data(new BYTE[filesize]);
  if(!data) {
    fprintf(stderr, "Failed to allocate memory for icon file.\n");
    return 1;
  }
  _read(file, data, filesize);

  IconHeader* header = reinterpret_cast<IconHeader*>(data.get());

  // Open the target library for updating
  ScopedResourceUpdate updateRes(BeginUpdateResourceW(argv[1], FALSE));
  if (NULL == updateRes) {
    fprintf(stderr, "Unable to open library for modification.\n");
    return 1;
  }

  // Allocate the group resource entry
  long groupSize = sizeof(IconHeader)
                 + header->ImageCount * sizeof(IconResEntry);
  nsAutoArrayPtr<BYTE> group(new BYTE[groupSize]);
  if(!group) {
    fprintf(stderr, "Failed to allocate memory for new images.\n");
    return 1;
  }
  memcpy(group, data, sizeof(IconHeader));

  IconDirEntry* sourceIcon =
                    reinterpret_cast<IconDirEntry*>(data
                                                  + sizeof(IconHeader));
  IconResEntry* targetIcon =
                    reinterpret_cast<IconResEntry*>(group
                                                  + sizeof(IconHeader));

  for (int id = 1; id <= header->ImageCount; id++) {
    // Add the individual icon
    if (!UpdateResourceW(updateRes, RT_ICON, MAKEINTRESOURCE(id),
                         MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
                         data + sourceIcon->ImageOffset,
                         sourceIcon->ImageSize)) {
      fprintf(stderr, "Unable to update resource (RT_ICON).\n");
      return 1;
    }
    // Copy the data for this icon
    // (note that the structs have different sizes)
    memcpy(targetIcon, sourceIcon, sizeof(IconResEntry));
    targetIcon->ResourceID = id;
    sourceIcon++;
    targetIcon++;
  }

  if (!UpdateResourceW(updateRes, RT_GROUP_ICON, L"MAINICON",
                       MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
                       group, groupSize)) {
    fprintf(stderr, "Unable to update resource (RT_GROUP_ICON).\n");
    return 1;
  }

  // Save the modifications
  if(!EndUpdateResourceW(updateRes.forget(), FALSE)) {
    fprintf(stderr, "Unable to write changes to library.\n");
    return 1;
  }

  return 0;
}