Skip to content

Commit

Permalink
Merge branch 'zlib-error-handling' into 'development'
Browse files Browse the repository at this point in the history
error handling for zlib uncompress

See merge request damask/DAMASK!1011
  • Loading branch information
dmentock committed Dec 19, 2024
2 parents 687bdb7 + c3dd28a commit 83ca849
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/C_routines.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ void signalusr2_c(void (*handler)(int)){
}


void inflate_c(const uLong *s_deflated, const uLong *s_inflated, const Byte deflated[], Byte inflated[]){
void inflate_c(const uLong *s_deflated, const uLong *s_inflated, const Byte deflated[], Byte inflated[], int *stat){
/* make writable copy, uncompress will write to it */
uLong s_inflated_,i;
s_inflated_ = *s_inflated;

if(uncompress((Bytef *)inflated, &s_inflated_, (Bytef *)deflated, *s_deflated) == Z_OK)
*stat = 1;
// https://stackoverflow.com/questions/51334741
if(uncompress((Bytef *)inflated, &s_inflated_, (Bytef *)deflated, *s_deflated) == Z_OK){
if (*s_inflated == s_inflated_) *stat = 0;
return;
else{
for(i=0;i<*s_inflated;i++){
inflated[i] = 0;
}
}
else memset(inflated,0,(size_t)*s_inflated);
}


Expand Down
17 changes: 10 additions & 7 deletions src/grid/zlib.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,34 @@ module zlib

interface

subroutine inflate_C(s_deflated,s_inflated,deflated,inflated) bind(C)
use, intrinsic :: ISO_C_Binding, only: C_SIGNED_CHAR, C_INT64_T
subroutine inflate_C(s_deflated,s_inflated,deflated,inflated,stat) bind(C)
use, intrinsic :: ISO_C_Binding, only: C_SIGNED_CHAR, C_INT64_T, C_INT
implicit none(type,external)

integer(C_INT64_T), intent(in) :: s_deflated,s_inflated
integer(C_SIGNED_CHAR), dimension(s_deflated), intent(in) :: deflated
integer(C_SIGNED_CHAR), dimension(s_inflated), intent(out) :: inflated
integer(C_SIGNED_CHAR), dimension(s_deflated), intent(in) :: deflated ! ok for unsigned char
integer(C_SIGNED_CHAR), dimension(s_inflated), intent(out) :: inflated ! ok for unsigned char
integer(C_INT), intent(out) :: stat
end subroutine inflate_C

end interface

contains

!--------------------------------------------------------------------------------------------------
!> @brief Inflate byte-wise representation
!> @brief Inflate byte-wise representation.
!--------------------------------------------------------------------------------------------------
function zlib_inflate(deflated,size_inflated)

integer(C_SIGNED_CHAR), dimension(:), intent(in) :: deflated
integer(pI64), intent(in) :: size_inflated

integer(C_SIGNED_CHAR), dimension(size_inflated) :: zlib_inflate

integer(C_INT) :: stat


call inflate_C(size(deflated,kind=C_INT64_T),int(size_inflated,C_INT64_T),deflated,zlib_inflate)
call inflate_C(size(deflated,kind=C_INT64_T),int(size_inflated,C_INT64_T),deflated,zlib_inflate,stat)
if (stat /= 0) error stop 'inflate failed'

end function zlib_inflate

Expand Down

0 comments on commit 83ca849

Please sign in to comment.