short and clear names for integer precision

This commit is contained in:
Martin Diehl 2020-09-12 09:14:33 +02:00
parent 7a6ada9ad7
commit a6ac875db8
3 changed files with 74 additions and 73 deletions

View File

@ -39,10 +39,10 @@ end subroutine base64_init
!--------------------------------------------------------------------------------------------------
pure function base64_nChar(nByte)
integer(pLongInt), intent(in) :: nByte
integer(pLongInt) :: base64_nChar
integer(pI64), intent(in) :: nByte
integer(pI64) :: base64_nChar
base64_nChar = 4_pLongInt * (nByte/3_pLongInt + merge(1_pLongInt,0_pLongInt,mod(nByte,3_pLongInt) /= 0_pLongInt))
base64_nChar = 4_pI64 * (nByte/3_pI64 + merge(1_pI64,0_pI64,mod(nByte,3_pI64) /= 0_pI64))
end function base64_nChar
@ -52,10 +52,10 @@ end function base64_nChar
!--------------------------------------------------------------------------------------------------
pure function base64_nByte(nBase64)
integer(pLongInt), intent(in) :: nBase64
integer(pLongInt) :: base64_nByte
integer(pI64), intent(in) :: nBase64
integer(pI64) :: base64_nByte
base64_nByte = 3_pLongInt * (nBase64/4_pLongInt)
base64_nByte = 3_pI64 * (nBase64/4_pI64)
end function base64_nByte
@ -66,36 +66,36 @@ end function base64_nByte
function base64_to_bytes(base64_str,s,e) result(bytes)
character(len=*), intent(in) :: base64_str !< Base64 string representation
integer(pLongInt), intent(in), optional :: &
integer(pI64), intent(in), optional :: &
s, & !< start (in bytes)
e !< end (in bytes)
integer(pLongInt) :: s_bytes, e_bytes, s_str, e_str
integer(pI64) :: s_bytes, e_bytes, s_str, e_str
integer(C_SIGNED_CHAR), dimension(:), allocatable :: bytes
if(.not. valid_base64(base64_str)) call IO_error(114,ext_msg='invalid character')
if(.not. validBase64(base64_str)) call IO_error(114,ext_msg='invalid character')
if(present(s)) then
if(s<1_pLongInt) call IO_error(114, ext_msg='s out of range')
s_str = ((s-1_pLongInt)/3_pLongInt)*4_pLongInt + 1_pLongInt
s_bytes = mod(s-1_pLongInt,3_pLongInt) + 1_pLongInt
if(s<1_pI64) call IO_error(114, ext_msg='s out of range')
s_str = ((s-1_pI64)/3_pI64)*4_pI64 + 1_pI64
s_bytes = mod(s-1_pI64,3_pI64) + 1_pI64
else
s_str = 1_pLongInt
s_bytes = 1_pLongInt
s_str = 1_pI64
s_bytes = 1_pI64
endif
if(present(e)) then
if(e>base64_nByte(len(base64_str,kind=pLongInt))) call IO_error(114, ext_msg='e out of range')
e_str = ((e-1_pLongInt)/3_pLongInt)*4_pLongInt + 4_pLongInt
if(e>base64_nByte(len(base64_str,kind=pI64))) call IO_error(114, ext_msg='e out of range')
e_str = ((e-1_pI64)/3_pI64)*4_pI64 + 4_pI64
e_bytes = e - base64_nByte(s_str)
else
e_str = len(base64_str,kind=pLongInt)
e_bytes = base64_nByte(len(base64_str,kind=pLongInt)) - base64_nByte(s_str)
if(base64_str(e_str-0_pLongInt:e_str-0_pLongInt) == '=') e_bytes = e_bytes - 1_pLongInt
if(base64_str(e_str-1_pLongInt:e_str-1_pLongInt) == '=') e_bytes = e_bytes - 1_pLongInt
e_str = len(base64_str,kind=pI64)
e_bytes = base64_nByte(len(base64_str,kind=pI64)) - base64_nByte(s_str)
if(base64_str(e_str-0_pI64:e_str-0_pI64) == '=') e_bytes = e_bytes - 1_pI64
if(base64_str(e_str-1_pI64:e_str-1_pI64) == '=') e_bytes = e_bytes - 1_pI64
endif
bytes = decode_base64(base64_str(s_str:e_str))
bytes = decodeBase64(base64_str(s_str:e_str))
bytes = bytes(s_bytes:e_bytes)
end function base64_to_bytes
@ -104,21 +104,21 @@ end function base64_to_bytes
!--------------------------------------------------------------------------------------------------
!> @brief Convert a Base64 ASCII string into its byte-wise binary representation.
!--------------------------------------------------------------------------------------------------
pure function decode_base64(base64_str) result(bytes)
pure function decodeBase64(base64_str) result(bytes)
character(len=*), intent(in) :: base64_str !< Base64 string representation
integer(C_SIGNED_CHAR), dimension(base64_nByte(len(base64_str,pLongInt))) :: bytes
integer(C_SIGNED_CHAR), dimension(base64_nByte(len(base64_str,pI64))) :: bytes
integer(C_SIGNED_CHAR), dimension(0:3) :: charPos
integer(pLongInt) :: c, b, p
integer(pI64) :: c, b, p
c = 1_pLongInt
b = 1_pLongInt
c = 1_pI64
b = 1_pI64
do while(c < len(base64_str,kind=pLongInt))
do p=0_pLongInt,3_pLongInt
if(c+p<=len(base64_str,kind=pLongInt)) then
do while(c < len(base64_str,kind=pI64))
do p=0_pI64,3_pI64
if(c+p<=len(base64_str,kind=pI64)) then
charPos(p) = int(index(base64_encoding,base64_str(c+p:c+p))-1,C_SIGNED_CHAR)
else
charPos(p) = 0_C_SIGNED_CHAR
@ -131,31 +131,31 @@ pure function decode_base64(base64_str) result(bytes)
call mvbits(charPos(2),2,4,bytes(b+1),0)
call mvbits(charPos(2),0,2,bytes(b+2),6)
call mvbits(charPos(3),0,6,bytes(b+2),0)
b = b+3_pLongInt
c = c+4_pLongInt
b = b+3_pI64
c = c+4_pI64
enddo
end function decode_base64
end function decodeBase64
!--------------------------------------------------------------------------------------------------
!> @brief Test for valid Base64 encoded string.
!> @details Input string must be properly padded.
!--------------------------------------------------------------------------------------------------
pure logical function valid_base64(base64_str)
pure logical function validBase64(base64_str)
character(len=*), intent(in) :: base64_str !< Base64 string representation
integer(pLongInt) :: l
integer(pI64) :: l
l = len(base64_str,pLongInt)
valid_base64 = .true.
l = len(base64_str,pI64)
validBase64 = .true.
if(mod(l,4_pLongInt)/=0_pLongInt .or. l < 4_pInt) valid_base64 = .false.
if(verify(base64_str(:l-2_pLongInt),base64_encoding, kind=pLongInt) /= 0_pLongInt) valid_base64 = .false.
if(verify(base64_str(l-1_pLongInt:),base64_encoding//'=',kind=pLongInt) /= 0_pLongInt) valid_base64 = .false.
if(mod(l,4_pI64)/=0_pI64 .or. l < 4_pInt) validBase64 = .false.
if(verify(base64_str(:l-2_pI64),base64_encoding, kind=pI64) /= 0_pI64) validBase64 = .false.
if(verify(base64_str(l-1_pI64:),base64_encoding//'=',kind=pI64) /= 0_pI64) validBase64 = .false.
end function valid_base64
end function validBase64
!--------------------------------------------------------------------------------------------------
@ -167,58 +167,58 @@ subroutine selfTest
character(len=*), parameter :: zero_to_three = 'AAECAw=='
! https://en.wikipedia.org/wiki/Base64#Output_padding
if(base64_nChar(20_pLongInt) /= 28_pLongInt) call IO_error(0,ext_msg='base64_nChar/20/28')
if(base64_nChar(19_pLongInt) /= 28_pLongInt) call IO_error(0,ext_msg='base64_nChar/19/28')
if(base64_nChar(18_pLongInt) /= 24_pLongInt) call IO_error(0,ext_msg='base64_nChar/18/24')
if(base64_nChar(17_pLongInt) /= 24_pLongInt) call IO_error(0,ext_msg='base64_nChar/17/24')
if(base64_nChar(16_pLongInt) /= 24_pLongInt) call IO_error(0,ext_msg='base64_nChar/16/24')
if(base64_nChar(20_pI64) /= 28_pI64) call IO_error(0,ext_msg='base64_nChar/20/28')
if(base64_nChar(19_pI64) /= 28_pI64) call IO_error(0,ext_msg='base64_nChar/19/28')
if(base64_nChar(18_pI64) /= 24_pI64) call IO_error(0,ext_msg='base64_nChar/18/24')
if(base64_nChar(17_pI64) /= 24_pI64) call IO_error(0,ext_msg='base64_nChar/17/24')
if(base64_nChar(16_pI64) /= 24_pI64) call IO_error(0,ext_msg='base64_nChar/16/24')
if(base64_nByte(4_pLongInt) /= 3_pLongInt) call IO_error(0,ext_msg='base64_nByte/4/3')
if(base64_nByte(8_pLongInt) /= 6_pLongInt) call IO_error(0,ext_msg='base64_nByte/8/6')
if(base64_nByte(4_pI64) /= 3_pI64) call IO_error(0,ext_msg='base64_nByte/4/3')
if(base64_nByte(8_pI64) /= 6_pI64) call IO_error(0,ext_msg='base64_nByte/8/6')
bytes = base64_to_bytes(zero_to_three)
if(any(bytes /= int([0,1,2,3],C_SIGNED_CHAR)) .or. size(bytes) /= 4) call IO_error(0,ext_msg='base64_to_bytes//')
bytes = base64_to_bytes(zero_to_three,e=1_pLongInt)
bytes = base64_to_bytes(zero_to_three,e=1_pI64)
if(any(bytes /= int([0],C_SIGNED_CHAR)) .or. size(bytes) /= 1) call IO_error(0,ext_msg='base64_to_bytes//1')
bytes = base64_to_bytes(zero_to_three,e=2_pLongInt)
bytes = base64_to_bytes(zero_to_three,e=2_pI64)
if(any(bytes /= int([0,1],C_SIGNED_CHAR)) .or. size(bytes) /= 2) call IO_error(0,ext_msg='base64_to_bytes//2')
bytes = base64_to_bytes(zero_to_three,e=3_pLongInt)
bytes = base64_to_bytes(zero_to_three,e=3_pI64)
if(any(bytes /= int([0,1,2],C_SIGNED_CHAR)) .or. size(bytes) /= 3) call IO_error(0,ext_msg='base64_to_bytes//3')
bytes = base64_to_bytes(zero_to_three,e=4_pLongInt)
bytes = base64_to_bytes(zero_to_three,e=4_pI64)
if(any(bytes /= int([0,1,2,3],C_SIGNED_CHAR)) .or. size(bytes) /= 4) call IO_error(0,ext_msg='base64_to_bytes//4')
bytes = base64_to_bytes(zero_to_three,s=1_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=1_pI64)
if(any(bytes /= int([0,1,2,3],C_SIGNED_CHAR)) .or. size(bytes) /= 4) call IO_error(0,ext_msg='base64_to_bytes/1/')
bytes = base64_to_bytes(zero_to_three,s=2_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=2_pI64)
if(any(bytes /= int([1,2,3],C_SIGNED_CHAR)) .or. size(bytes) /= 3) call IO_error(0,ext_msg='base64_to_bytes/2/')
bytes = base64_to_bytes(zero_to_three,s=3_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=3_pI64)
if(any(bytes /= int([2,3],C_SIGNED_CHAR)) .or. size(bytes) /= 2) call IO_error(0,ext_msg='base64_to_bytes/3/')
bytes = base64_to_bytes(zero_to_three,s=4_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=4_pI64)
if(any(bytes /= int([3],C_SIGNED_CHAR)) .or. size(bytes) /= 1) call IO_error(0,ext_msg='base64_to_bytes/4/')
bytes = base64_to_bytes(zero_to_three,s=1_pLongInt,e=1_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=1_pI64,e=1_pI64)
if(any(bytes /= int([0],C_SIGNED_CHAR)) .or. size(bytes) /= 1) call IO_error(0,ext_msg='base64_to_bytes/1/1')
bytes = base64_to_bytes(zero_to_three,s=2_pLongInt,e=2_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=2_pI64,e=2_pI64)
if(any(bytes /= int([1],C_SIGNED_CHAR)) .or. size(bytes) /= 1) call IO_error(0,ext_msg='base64_to_bytes/2/2')
bytes = base64_to_bytes(zero_to_three,s=3_pLongInt,e=3_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=3_pI64,e=3_pI64)
if(any(bytes /= int([2],C_SIGNED_CHAR)) .or. size(bytes) /= 1) call IO_error(0,ext_msg='base64_to_bytes/3/3')
bytes = base64_to_bytes(zero_to_three,s=4_pLongInt,e=4_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=4_pI64,e=4_pI64)
if(any(bytes /= int([3],C_SIGNED_CHAR)) .or. size(bytes) /= 1) call IO_error(0,ext_msg='base64_to_bytes/4/4')
bytes = base64_to_bytes(zero_to_three,s=1_pLongInt,e=2_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=1_pI64,e=2_pI64)
if(any(bytes /= int([0,1],C_SIGNED_CHAR)) .or. size(bytes) /= 2) call IO_error(0,ext_msg='base64_to_bytes/1/2')
bytes = base64_to_bytes(zero_to_three,s=2_pLongInt,e=3_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=2_pI64,e=3_pI64)
if(any(bytes /= int([1,2],C_SIGNED_CHAR)) .or. size(bytes) /= 2) call IO_error(0,ext_msg='base64_to_bytes/2/3')
bytes = base64_to_bytes(zero_to_three,s=3_pLongInt,e=4_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=3_pI64,e=4_pI64)
if(any(bytes /= int([2,3],C_SIGNED_CHAR)) .or. size(bytes) /= 2) call IO_error(0,ext_msg='base64_to_bytes/3/4')
bytes = base64_to_bytes(zero_to_three,s=1_pLongInt,e=3_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=1_pI64,e=3_pI64)
if(any(bytes /= int([0,1,2],C_SIGNED_CHAR)) .or. size(bytes) /= 3) call IO_error(0,ext_msg='base64_to_bytes/1/3')
bytes = base64_to_bytes(zero_to_three,s=2_pLongInt,e=4_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=2_pI64,e=4_pI64)
if(any(bytes /= int([1,2,3],C_SIGNED_CHAR)) .or. size(bytes) /= 3) call IO_error(0,ext_msg='base64_to_bytes/2/4')
bytes = base64_to_bytes(zero_to_three,s=1_pLongInt,e=4_pLongInt)
bytes = base64_to_bytes(zero_to_three,s=1_pI64,e=4_pI64)
if(any(bytes /= int([0,1,2,3],C_SIGNED_CHAR)) .or. size(bytes) /= 4) call IO_error(0,ext_msg='base64_to_bytes/1/4')
end subroutine selfTest

View File

@ -15,12 +15,13 @@ module prec
! https://software.intel.com/en-us/blogs/2017/03/27/doctor-fortran-in-it-takes-all-kinds
integer, parameter :: pReal = IEEE_selected_real_kind(15,307) !< number with 15 significant digits, up to 1e+-307 (typically 64 bit)
integer, parameter :: pI32 = selected_int_kind(9) !< number with at least up to +-1e9 (typically 32 bit)
integer, parameter :: pI64 = selected_int_kind(18) !< number with at least up to +-1e18 (typically 64 bit)
#if(INT==8)
integer, parameter :: pInt = selected_int_kind(18) !< number with at least up to +-1e18 (typically 64 bit)
integer, parameter :: pInt = pI64
#else
integer, parameter :: pInt = selected_int_kind(9) !< number with at least up to +-1e9 (typically 32 bit)
integer, parameter :: pInt = pI32
#endif
integer, parameter :: pLongInt = selected_int_kind(18) !< number with at least up to +-1e18 (typically 64 bit)
integer, parameter :: pStringLen = 256 !< default string length
integer, parameter :: pPathLen = 4096 !< maximum length of a path name on linux
@ -237,7 +238,7 @@ end function cNeq
pure function bytes_to_C_FLOAT(bytes)
integer(C_SIGNED_CHAR), dimension(:), intent(in) :: bytes !< byte-wise representation of a C_FLOAT array
real(C_FLOAT), dimension(size(bytes,kind=pLongInt)/(storage_size(0._C_FLOAT,pLongInt)/8_pLongInt)) :: &
real(C_FLOAT), dimension(size(bytes,kind=pI64)/(storage_size(0._C_FLOAT,pI64)/8_pI64)) :: &
bytes_to_C_FLOAT
bytes_to_C_FLOAT = transfer(bytes,bytes_to_C_FLOAT,size(bytes_to_C_FLOAT))
@ -251,7 +252,7 @@ end function bytes_to_C_FLOAT
pure function bytes_to_C_DOUBLE(bytes)
integer(C_SIGNED_CHAR), dimension(:), intent(in) :: bytes !< byte-wise representation of a C_DOUBLE array
real(C_DOUBLE), dimension(size(bytes,kind=pLongInt)/(storage_size(0._C_DOUBLE,pLongInt)/8_pLongInt)) :: &
real(C_DOUBLE), dimension(size(bytes,kind=pI64)/(storage_size(0._C_DOUBLE,pI64)/8_pI64)) :: &
bytes_to_C_DOUBLE
bytes_to_C_DOUBLE = transfer(bytes,bytes_to_C_DOUBLE,size(bytes_to_C_DOUBLE))
@ -265,7 +266,7 @@ end function bytes_to_C_DOUBLE
pure function bytes_to_C_INT32_T(bytes)
integer(C_SIGNED_CHAR), dimension(:), intent(in) :: bytes !< byte-wise representation of a C_INT32_T array
integer(C_INT32_T), dimension(size(bytes,kind=pLongInt)/(storage_size(0_C_INT32_T,pLongInt)/8_pLongInt)) :: &
integer(C_INT32_T), dimension(size(bytes,kind=pI64)/(storage_size(0_C_INT32_T,pI64)/8_pI64)) :: &
bytes_to_C_INT32_T
bytes_to_C_INT32_T = transfer(bytes,bytes_to_C_INT32_T,size(bytes_to_C_INT32_T))
@ -279,7 +280,7 @@ end function bytes_to_C_INT32_T
pure function bytes_to_C_INT64_T(bytes)
integer(C_SIGNED_CHAR), dimension(:), intent(in) :: bytes !< byte-wise representation of a C_INT64_T array
integer(C_INT64_T), dimension(size(bytes,kind=pLongInt)/(storage_size(0_C_INT64_T,pLongInt)/8_pLongInt)) :: &
integer(C_INT64_T), dimension(size(bytes,kind=pI64)/(storage_size(0_C_INT64_T,pI64)/8_pI64)) :: &
bytes_to_C_INT64_T
bytes_to_C_INT64_T = transfer(bytes,bytes_to_C_INT64_T,size(bytes_to_C_INT64_T))

View File

@ -33,7 +33,7 @@ contains
function zlib_inflate(deflated,size_inflated)
integer(C_SIGNED_CHAR), dimension(:), intent(in) :: deflated
integer(pLongInt), intent(in) :: size_inflated
integer(pI64), intent(in) :: size_inflated
integer(C_SIGNED_CHAR), dimension(size_inflated) :: zlib_inflate