Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
openfoam
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Development
openfoam
Commits
6581c579
Commit
6581c579
authored
1 year ago
by
mattijs
Committed by
Mark OLESEN
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
BUG: STL: cannot handle files > 2Gb. Fixes #3171
parent
0ef75899
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/fileFormats/stl/STLCore.C
+62
-36
62 additions, 36 deletions
src/fileFormats/stl/STLCore.C
with
62 additions
and
36 deletions
src/fileFormats/stl/STLCore.C
+
62
−
36
View file @
6581c579
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
\\/ M anipulation |
\\/ M anipulation |
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-202
3
OpenCFD Ltd.
Copyright (C) 2016-202
4
OpenCFD Ltd.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
License
License
This file is part of OpenFOAM.
This file is part of OpenFOAM.
...
@@ -61,6 +61,36 @@ static bool startsWithSolid(const char header[STLHeaderSize])
...
@@ -61,6 +61,36 @@ static bool startsWithSolid(const char header[STLHeaderSize])
}
}
// Check if file size appears to be reasonable for an STL binary file.
// Compare file size with that expected from number of tris
// If this is not sensible, it may be an ASCII file
//
// sizeof(STLtriangle) = 50 bytes [int16 + 4 * (3 float)]
inline
static
bool
checkBinaryFileSize
(
const
int64_t
nTris
,
const
Foam
::
fileName
&
file
)
{
// When checking the content size, account for the header size (80),
// but ignore the nTris information (int32_t) to give some rounding
const
int64_t
contentSize
=
(
int64_t
(
Foam
::
fileSize
(
file
))
-
int64_t
(
STLHeaderSize
)
);
return
(
(
contentSize
>=
0
)
&&
(
nTris
>=
contentSize
/
50
)
&&
(
nTris
<=
contentSize
/
25
)
);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool
Foam
::
fileFormats
::
STLCore
::
isBinaryName
bool
Foam
::
fileFormats
::
STLCore
::
isBinaryName
...
@@ -117,31 +147,31 @@ int Foam::fileFormats::STLCore::detectBinaryHeader
...
@@ -117,31 +147,31 @@ int Foam::fileFormats::STLCore::detectBinaryHeader
// Read the number of triangles in the STL file
// Read the number of triangles in the STL file
// (note: read as signed so we can check whether >2^31)
// (note: read as signed int so we can check whether >2^31).
//
// With nTris == 2^31, file size is 107.37 GB !
//
// However, the limit is more likely caused by the number of points
// that can be stored (label-size=32) when flattened for merging.
// So more like 715.8M triangles (~35.8 GB)
int32_t
nTris
;
int32_t
nTris
;
is
.
read
(
reinterpret_cast
<
char
*>
(
&
nTris
),
sizeof
(
int32_t
));
is
.
read
(
reinterpret_cast
<
char
*>
(
&
nTris
),
sizeof
(
int32_t
));
// Check that stream is OK and number of triangles is positive,
bool
ok
=
(
is
&&
nTris
>=
0
);
// if not this may be an ASCII file
bool
bad
=
(
!
is
||
nTris
<
0
);
if
(
!
bad
&&
unCompressed
)
if
(
ok
&&
unCompressed
)
{
{
// Compare file size with that expected from number of tris
ok
=
checkBinaryFileSize
(
nTris
,
filename
);
// If this is not sensible, it may be an ASCII file
const
off_t
dataFileSize
=
Foam
::
fileSize
(
filename
);
bad
=
(
nTris
<
int
(
dataFileSize
-
STLHeaderSize
)
/
50
||
nTris
>
int
(
dataFileSize
-
STLHeaderSize
)
/
25
);
}
}
//if (ok)
//{
// InfoErr<< "stlb : " << nTris << " triangles" << nl;
//}
// Return number of triangles if it appears to be BINARY and good.
// Return number of triangles if it appears to be BINARY and good.
return
(
bad
?
0
:
nTris
);
return
(
ok
?
nTris
:
0
);
}
}
...
@@ -189,31 +219,27 @@ Foam::fileFormats::STLCore::readBinaryHeader
...
@@ -189,31 +219,27 @@ Foam::fileFormats::STLCore::readBinaryHeader
<<
exit
(
FatalError
);
<<
exit
(
FatalError
);
}
}
// Read the number of triangles in the STl file
// (note: read as int so we can check whether >2^31)
// Read the number of triangles in the STL file
// (note: read as signed int so we can check whether >2^31).
//
// With nTris == 2^31, file size is 107.37 GB !
//
// However, the limit is more likely caused by the number of points
// that can be stored (label-size=32) when flattened for merging.
// So more like 715.8M triangles (~35.8 GB)
int32_t
nTris
;
int32_t
nTris
;
is
.
read
(
reinterpret_cast
<
char
*>
(
&
nTris
),
sizeof
(
int32_t
));
is
.
read
(
reinterpret_cast
<
char
*>
(
&
nTris
),
sizeof
(
int32_t
));
// Check that stream is OK and number of triangles is positive,
bool
ok
=
(
is
&&
nTris
>=
0
);
// if not this maybe an ASCII file
bool
bad
=
(
!
is
||
nTris
<
0
);
if
(
ok
&&
unCompressed
)
if
(
!
bad
&&
unCompressed
)
{
{
// Compare file size with that expected from number of tris
ok
=
checkBinaryFileSize
(
nTris
,
filename
);
// If this is not sensible, it may be an ASCII file
const
off_t
dataFileSize
=
Foam
::
fileSize
(
filename
);
bad
=
(
nTris
<
int
(
dataFileSize
-
STLHeaderSize
)
/
50
||
nTris
>
int
(
dataFileSize
-
STLHeaderSize
)
/
25
);
}
}
if
(
bad
)
if
(
!
ok
)
{
{
FatalErrorInFunction
FatalErrorInFunction
<<
"problem reading number of triangles, perhaps file is not binary"
<<
"problem reading number of triangles, perhaps file is not binary"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment