diff options
Diffstat (limited to 'lib/77bit')
| -rw-r--r-- | lib/77bit/77bit.txt | 36 | ||||
| -rw-r--r-- | lib/77bit/encode77.f90 | 103 | ||||
| -rw-r--r-- | lib/77bit/packjt77.f90 | 4496 | ||||
| -rw-r--r-- | lib/77bit/packjt77_grammar.f90 | 1530 | ||||
| -rw-r--r-- | lib/77bit/packjt77_schema.f90 | 871 |
5 files changed, 4258 insertions, 2778 deletions
diff --git a/lib/77bit/77bit.txt b/lib/77bit/77bit.txt index ae8f3dd3..1ff7c117 100644 --- a/lib/77bit/77bit.txt +++ b/lib/77bit/77bit.txt @@ -11,26 +11,21 @@ i3.n3 Example message Bits Total Purpose ---------------------------------------------------------------------------------- 0.0 FREE TEXT MSG 71 71 Free text 0.1 K1ABC RR73; W9XYZ <KH1/KH7Z> -12 28 28 10 5 71 DXpedition Mode -0.2 ... tbd +0.2 (reserved) 0.3 WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 ARRL Field Day 0.4 WA9XYZ KA1ABC R 32A EMA 28 28 1 4 3 7 71 ARRL Field Day 0.5 123456789ABCDEF012 71 71 Telemetry (18 hex) -0.6 Temporarily hijacked in packjt77 for WSPR-LF. This is not necessary! -0.7 ... tbd +0.6 K1ABC FN42 37 WSPR-LF (?) +0.7 (reserved) 1 WA9XYZ/R KA1ABC/R R FN42 28 1 28 1 1 15 74 Standard msg 2 PA3XYZ/P GM4ABC/P R JO22 28 1 28 1 1 15 74 EU VHF contest 3 TU; W9XYZ K1ABC R 579 MA 1 28 28 1 3 13 74 ARRL RTTY Roundup 4 <WA9XYZ> PJ4/KA1ABC RR73 12 58 1 2 1 74 Nonstandard calls 5 <G4ABC/P> <PA9XYZ> R 570007 JO22DB 12 22 1 3 11 25 74 EU VHF Contest -6 ... tbd -7 ... tbd +6 (reserved) +7 (reserved) ---------------------------------------------------------------------------------- -NB: three 74-bit message types and two 71-bit message subtypes are still TBD. ----------------------------------------------------------------------------------- - -The 28-bit fields normally used for callsigns are configured rather -differently from before. Facts about the 28-bit integers used to encode standard callsigns: 2^28 = 268,435,456 Available values @@ -47,17 +42,11 @@ Further details on message types: i3.n3 ----------------------------------------------------------------------------- 0.0 Free text, up to 13 characters. - 0.1 DXpedition mode, as developed for KH1/KH7Z. - -0.2 Report, QSO serial number from 0001 to 4095, 6-character grid, and Roger - for EU VHF contests. - 0.3 ARRL Field Day exchange (1-16 transmitters). - 0.4 ARRL Field Day exchange (17-32 transmitters). - 0.5 Telemetry, 71 bits +0.6 WSPR-style 1. Two callsigns, Roger, and grid or report. Each callsign may have an appended "/R" to indicate Rover status for NA VHF contests. @@ -70,15 +59,12 @@ i3.n3 beginning to finish a previous QSO; then two standard callsigns, optional "R", a 3-bit report (529 to 599), and 13 bits to indicate US state, Canadian province/territory, or DX serial number from - 0001 to 7999. + 0001 to 7999. (*** Obsolete: RTTY Roundup now allows only standard + Baudot RTTY ***) 4. One hashed call or "CQ"; one compound or nonstandard call with up to 11 characters; and (if not "CQ") an optional RRR, RR73, or 73. ---------------------------------------------------------------------------- -The following are tentative and not included in first release. - -5. CQ WW RTTY - US/Can: RST CQZ state/prov R 579 5 NJ R1 r3 z6 u7 - DX: RST + CQzone R 559 15 R1 r3 z6 - -6. CQ WPX RTTY - RST + serial R 589 0013 R1 r3 n12 +5. Two hashed callsigns (one 12-bit and one 22-bit), optional "R", 3-bit + report (52 to 59), 4-digit serial number (0001 - 2047), 6-character + grid locator. For EU VHF contest. diff --git a/lib/77bit/encode77.f90 b/lib/77bit/encode77.f90 index 99af70cd..f3d64b93 100644 --- a/lib/77bit/encode77.f90 +++ b/lib/77bit/encode77.f90 @@ -1,31 +1,41 @@ program encode77 use packjt77 - + + implicit none + character*80 msg0 - character msg*37,cerr*1 + character msg*37 character*77 c77 character*80 infile character*13 w(19) + character*40 status_text + character*5 type_text integer nw(19) logical unpk77_success + integer nargs,iz,iline,i3,n3,nwords,j2,pack_status + integer nencoded,nrejected,nunpack_failed,ndifferent nargs=iargc() if(nargs.ne.1 .and.nargs.ne.2) then print*,'Usage: encode77 "message"' print*,' encode77 -f <infile>' - go to 999 + stop 2 endif call getarg(1,msg0) - call fmtmsg(msg0,iz) if(nargs.eq.2) then call getarg(2,infile) open(10,file=infile,status='old') write(*,1000) -1000 format('i3.n3 Err Message to be encoded Decoded message' & - /80('-')) +1000 format('Type Result Message to be encoded Decoded message or reason' & + /96('-')) endif - + + nencoded=0 + nrejected=0 + nunpack_failed=0 + ndifferent=0 + do iline=1,999 if(nargs.eq.2) read(10,1002,end=999) msg0 1002 format(a80) @@ -34,27 +44,80 @@ program encode77 if(msg0(2:2).eq.'.' .or. msg0(3:3).eq.'.') cycle if(msg0(1:3).eq.'---') cycle msg0=adjustl(msg0) - i3=-1 - n3=-1 - call pack77(msg0(1:37),i3,n3,c77) - call unpack77(c77,1,msg,unpk77_success) - cerr=' ' - if(msg.ne.msg0(1:37)) cerr='*' - if(i3.eq.0 .and.n3.ne.6) write(*,1004) i3,n3,cerr,msg0(1:37),msg -1004 format(i2,'.',i1,4x,a1,1x,a37,1x,a37) + call fmtmsg(msg0,iz) + call pack77(msg0(1:37),i3,n3,c77,status=pack_status) + if(pack_status.ne.PACK77_STATUS_ENCODED) then + nrejected=nrejected+1 + status_text=pack77_status_text(pack_status) + write(*,1004) '--','REJECTED',msg0(1:37),trim(status_text) + if(nargs.eq.1) stop 1 + cycle + endif + + nencoded=nencoded+1 + call unpack77(c77,0,msg,unpk77_success) + if(.not.unpk77_success) then + nunpack_failed=nunpack_failed+1 + write(*,1004) '--','UNPACK FAIL',msg0(1:37),'--' + if(nargs.eq.1) stop 1 + cycle + endif + + type_text=' ' if(i3.eq.0 .and.n3.eq.6) then call split77(msg,nwords,nw,w) j2=0 if(nwords.eq.2 .and. len(trim(w(2))).le.2) j2=1 if(nwords.eq.2 .and. len(trim(w(2))).eq.6) j2=2 - write(*,1005) i3,n3,j2,cerr,msg0(1:37),msg -1005 format(i2,'.',i1,'.',i1,2x,a1,1x,a37,1x,a37) + write(type_text,1005) i3,n3,j2 +1005 format(i1,'.',i1,'.',i1) + else if(i3.eq.0) then + write(type_text,1006) i3,n3 +1006 format(i1,'.',i1) + else + write(type_text,1007) i3 +1007 format(i1) endif - if(i3.ge.1) write(*,1006) i3,cerr,msg0(1:37),msg -1006 format(i2,'.',5x,a1,1x,a37,1x,a37) + + if(msg.eq.msg0(1:37)) then + write(*,1004) trim(type_text),'OK',msg0(1:37),msg + else + ndifferent=ndifferent+1 + write(*,1004) trim(type_text),'DIFF',msg0(1:37),msg + endif +1004 format(a5,1x,a11,1x,a37,1x,a) if(nargs.eq.1) exit enddo -999 end program encode77 +999 if(nargs.eq.2) then + write(*,1010) nencoded,nrejected,nunpack_failed,ndifferent +1010 format(/'Summary: ',i0,' encoded, ',i0,' rejected, ',i0, & + ' unpack failures, ',i0,' differences') + endif + + if(nrejected.gt.0 .or. nunpack_failed.gt.0 .or. ndifferent.gt.0) stop 1 + +contains + + character(len=40) function pack77_status_text(status) result(text) + integer, intent(in) :: status + + select case(status) + case(PACK77_STATUS_NOT_ENCODED) + text='not encoded' + case(PACK77_STATUS_FREE_TEXT_TOO_LONG) + text='free text too long' + case(PACK77_STATUS_FREE_TEXT_INVALID) + text='invalid free text' + case(PACK77_STATUS_PREFERRED_FAMILY_REJECTED) + text='preferred message family rejected' + case(PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED) + text='internal round-trip check failed' + case default + write(text,'(a,i0)') 'unknown status ',status + end select + end function pack77_status_text + +end program encode77 include '../chkcall.f90' diff --git a/lib/77bit/packjt77.f90 b/lib/77bit/packjt77.f90 index 35236b37..9fba9f92 100644 --- a/lib/77bit/packjt77.f90 +++ b/lib/77bit/packjt77.f90 @@ -1,48 +1,256 @@ module packjt77 - + + use packjt77_schema + use packjt77_grammar + ! These variables are accessible from outside via "use packjt77": parameter (MAXHASH=1000,MAXRECENT=10) - parameter (MAXHASHVAR=1000,MAXTXHASHVAR=5,MAXRECENTVAR=10) - ! variables from original packjt77 + ! Callsign hash resolution uses one session-persistent shared table. Encode, + ! decode, and configured/var decode stage calls at their boundaries, then + ! commit them through save_hash_call instead of maintaining separate TX/RX + ! hash stores. character (len=13), dimension(0:1023) :: calls10='' character (len=13), dimension(0:4095) :: calls12='' character (len=13), dimension(1:MAXHASH) :: calls22='' character (len=13), dimension(1:MAXRECENT) :: recent_calls='' character (len=13) :: mycall13='' character (len=13) :: dxcall13='' - character (len=6) :: dxbase='' integer, dimension(1:MAXHASH) :: ihash22=-1 integer :: nzhash=0 - integer n28a,n28b - ! variables from packjt77var - character (len=13), dimension(0:1023) :: calls10var='' - character (len=13), dimension(0:4095) :: calls12var='' - character (len=13), dimension(1:MAXHASHVAR) :: calls22var='' - character (len=13), dimension(0:1023) :: txcalls10var='' - character (len=13), dimension(0:4095) :: txcalls12var='' - character (len=13), dimension(1:MAXTXHASHVAR) :: txcalls22var='' - character (len=13), dimension(1:MAXRECENTVAR) :: recent_callsvar='' - character (len=13), dimension(1:840) :: last_callsvar='' - character (len=13) :: mycall13var='' - character (len=13) :: dxcall13var='' - character (len=13) :: mycall13_0var='' - character (len=13) :: dxcall13_0var='' - integer hashmy10var,hashmy12var,hashmy22var,hashdx10var - logical :: dxcall13_setvar=.false. - logical :: mycall13_setvar=.false. - integer, dimension(1:MAXHASHVAR) :: ihash22var=-1 - integer, dimension(1:5) :: itxhash22var=-1 - integer, dimension(1:24) :: nlast_callsvar=0 - integer, dimension(1:25) :: nthrindexvar=(/0,200,300,370,420,460,500,530,560,590,610,630,650,670,690,710, & + ! Configured/var decodes queue per-thread facts until fillhashvar folds them + ! into the shared hash and recent-call state. + character (len=13), dimension(1:840) :: queued_calls_by_thread='' + character (len=13), dimension(1:840) :: queued_recent_calls_by_thread='' + character (len=13) :: mycall13_configured='' + character (len=13) :: dxcall13_configured='' + character (len=13) :: mycall13_configured_prev='' + character (len=13) :: dxcall13_configured_prev='' + integer hashmy10_configured,hashmy12_configured,hashmy22_configured,hashdx10_configured + logical :: dxcall13_configured_set=.false. + logical :: mycall13_configured_set=.false. + integer, dimension(1:24) :: nqueued_calls_by_thread=0 + integer, dimension(1:24) :: nqueued_recent_calls_by_thread=0 + ! Per-thread slice boundaries of queued_*_by_thread; earlier (busier) + ! decode threads get larger slots. + integer, dimension(1:25) :: thread_call_index=(/0,200,300,370,420,460,500,530,560,590,610,630,650,670,690,710, & 730,750,770,790,800,810,820,830,840/) - integer :: nzhashvar=0 - integer :: nztxhashvar=0 - integer n28avar,n28bvar - logical(1) lcommonft8b -!$omp threadprivate(recent_callsvar,n28avar,n28bvar) + integer n28a_configured,n28b_configured +!$omp threadprivate(n28a_configured,n28b_configured) +! Everything is private unless exported here; imported grammar/schema +! symbols therefore never re-export through "use packjt77". + private + public :: pack77,pack77_legacy_truncating_fallback + public :: pack77_options,unpack77_options + public :: PACK77_STATUS_NOT_ENCODED,PACK77_STATUS_ENCODED + public :: PACK77_STATUS_FREE_TEXT_TOO_LONG + public :: PACK77_STATUS_FREE_TEXT_INVALID + public :: PACK77_STATUS_PREFERRED_FAMILY_REJECTED + public :: PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED + public :: unpack77,unpack77_configured,pack28,unpack28 + public :: split77,packtext77,unpacktext77 + public :: ihashcall,hash10,hash12,hash22,save_hash_call + public :: add_call_to_recent_calls,queue_hash_call_for_thread + public :: fold_queued_recent_calls,sync_configured_calls_for_decode_start + public :: MAXHASH,MAXRECENT + public :: calls10,calls12,calls22,recent_calls,ihash22,nzhash + public :: mycall13,dxcall13 + public :: queued_calls_by_thread,queued_recent_calls_by_thread + public :: nqueued_calls_by_thread,nqueued_recent_calls_by_thread + public :: mycall13_configured,dxcall13_configured + public :: mycall13_configured_prev,dxcall13_configured_prev + public :: mycall13_configured_set,dxcall13_configured_set + public :: hashmy10_configured,hashmy12_configured,hashmy22_configured + public :: hashdx10_configured + public :: thread_call_index + + integer, parameter :: PACK77_STATUS_NOT_ENCODED=0 + integer, parameter :: PACK77_STATUS_ENCODED=1 + integer, parameter :: PACK77_STATUS_FREE_TEXT_TOO_LONG=3 + integer, parameter :: PACK77_STATUS_FREE_TEXT_INVALID=4 + integer, parameter :: PACK77_STATUS_PREFERRED_FAMILY_REJECTED=5 + integer, parameter :: PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED=6 + + type pack77_result + logical :: encoded=.false. + integer :: status=PACK77_STATUS_NOT_ENCODED + integer :: i3=-1 + integer :: n3=-1 + character(len=77) :: c77=' ' + end type pack77_result + + type pack77_hash_facts + ! Calls that an accepted encode should make resolvable by later hash-only + ! decodes. pack77 commits these unless record_tx_hashes is disabled. + integer :: ncalls=0 + character(len=13) :: calls(32)='' + end type pack77_hash_facts + + type pack77_candidate + type(pack77_result) :: result + type(pack77_hash_facts) :: hash_facts + end type pack77_candidate + + type pack77_encode_core_result + type(pack77_result) :: result + type(pack77_hash_facts) :: hash_facts + end type pack77_encode_core_result + + + + type unpack77_effects + ! Decoders collect hash/recent-call side effects here and apply them only + ! after the message has decoded successfully. + integer :: nhash=0 + integer :: nrecent=0 + character(len=13) :: hash_calls(32)='' + character(len=13) :: recent_calls(32)='' + end type unpack77_effects + + type unpack77_context + integer :: nrx=0 + logical :: configured=.false. + logical :: strict_var_guards=.false. + logical :: mycall_set=.false. + logical :: dxcall_set=.false. + character(len=13) :: mycall='' + character(len=13) :: dxcall='' + integer :: hashmy10=-1 + integer :: hashmy12=-1 + integer :: hashmy22=-1 + integer :: hashdx10=-1 + end type unpack77_context + + type configured_decode_state + logical :: mycall_set=.false. + logical :: dxcall_set=.false. + character(len=13) :: mycall='' + character(len=13) :: dxcall='' + integer :: hashmy10=-1 + integer :: hashmy12=-1 + integer :: hashmy22=-1 + integer :: hashdx10=-1 + end type configured_decode_state + + type unpack77_core_result + logical :: success=.false. + integer :: i3=-1 + integer :: n3=-1 + character(len=37) :: msg=' ' + type(unpack77_effects) :: effects + end type unpack77_core_result + + type unpack77_effect_policy + logical :: record_hashes=.true. + logical :: record_recent_calls=.true. + logical :: queue_by_thread=.false. + ! Queued policies require valid 1-based thread indexes; disable recording + ! through record_hashes or record_recent_calls instead of sentinel indexes. + integer :: nthr_hash=1 + integer :: nthr_recent=1 + end type unpack77_effect_policy + + type pack77_options + logical :: prefer_wspr_50bit=.false. + logical :: record_tx_hashes=.true. + logical :: expand_dx_macro=.false. + character(len=6) :: dx_macro_base='' + end type pack77_options + + type unpack77_options + integer :: thread_index=1 + logical :: record_hashes=.true. + logical :: record_recent_calls=.true. + end type unpack77_options contains - + +type(pack77_result) function pack77_no_match() result(encoded) + encoded=pack77_result() +end function pack77_no_match + +type(pack77_result) function pack77_reject(status) result(encoded) + integer, intent(in) :: status + + encoded=pack77_result() + encoded%status=status +end function pack77_reject + +type(pack77_result) function pack77_accept(i3,n3,c77) result(encoded) + integer, intent(in) :: i3,n3 + character(len=77), intent(in) :: c77 + + encoded=pack77_result() + encoded%encoded=.true. + encoded%status=PACK77_STATUS_ENCODED + encoded%i3=i3 + encoded%n3=n3 + encoded%c77=c77 +end function pack77_accept + +subroutine pack77_accept_candidate_encoded(candidate,i3,n3,c77,ok) + type(pack77_candidate), intent(inout) :: candidate + integer, intent(in) :: i3,n3 + character(len=77), intent(in) :: c77 + logical, intent(in) :: ok + + candidate%result=pack77_no_match() + if(ok) candidate%result=pack77_accept(i3,n3,c77) +end subroutine pack77_accept_candidate_encoded + +logical function take_candidate(candidate,msg,encoded) result(won) + type(pack77_candidate), intent(in) :: candidate + character(len=*), intent(in) :: msg + type(pack77_encode_core_result), intent(inout) :: encoded + type(unpack77_core_result) :: decoded + logical :: messages_match,resolved_render_fits + +! A candidate must round-trip through the neutral decoder before it wins; a +! syntactically shaped but gated-out candidate only records its family so +! fallback callers can report what kind of exact encoding was rejected. + won=candidate%result%encoded + if(won) then + decoded=pack77_decode_neutral(candidate%result%c77) + call pack77_gate_message_checks(msg,decoded%msg,candidate%hash_facts%calls, & + candidate%hash_facts%ncalls,messages_match,resolved_render_fits) + if(.not.(decoded%success .and. decoded%i3.eq.candidate%result%i3 .and. & + decoded%n3.eq.candidate%result%n3 .and. messages_match .and. & + resolved_render_fits)) then + won=.false. + encoded%result=pack77_reject(PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED) + endif + endif + if(.not.won) return + encoded%result=candidate%result + encoded%hash_facts=candidate%hash_facts +end function take_candidate + +type(unpack77_core_result) function pack77_decode_neutral(c77) result(decoded) + character(len=77), intent(in) :: c77 + type(unpack77_context) :: context + + context=unpack77_context() + decoded=unpack77_core(c77,context) +end function pack77_decode_neutral + +integer function pack77_wspr_payload_type(c77) result(wspr_type) + character(len=77), intent(in) :: c77 + +! Bit columns 49/50 are the j49/j50 subtype selector fields fixed by the +! WSPR schemas in packjt77_schema; keep in sync with those field offsets. + wspr_type=0 + if(c77(50:50).eq.'1') then + wspr_type=2 + else if(c77(49:49).eq.'0') then + wspr_type=1 + else if(c77(49:49).eq.'1') then + ! Invalid selector 110 reaches Type 3; the schema decoder rejects it. + wspr_type=3 + endif +end function pack77_wspr_payload_type + + + + subroutine hash10(n10,c13) character*13 c13 @@ -60,7 +268,7 @@ end subroutine hash10 subroutine hash12(n12,c13) character*13 c13 - + c13='<...>' if(n12.lt.0 .or. n12.gt.4095) return if(len(trim(calls12(n12))).gt.0) then @@ -75,7 +283,7 @@ end subroutine hash12 subroutine hash22(n22,c13) character*13 c13 - + c13='<...>' do i=1,nzhash if(ihash22(i).eq.n22) then @@ -90,27 +298,66 @@ end subroutine hash22 integer function ihashcall(c0,m) + implicit none - integer*8 n8 - character*13 c0 - character*38 c - data c/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/'/ + character(len=13), intent(in) :: c0 + integer, intent(in) :: m + integer(kind=8) :: n8 + integer :: i,j - n8=0 + n8=0_8 do i=1,11 - j=index(c,c0(i:i)) - 1 - n8=38*n8 + j + j=index(PACK77_BASE38,c0(i:i)) - 1 + n8=38_8*n8 + j enddo - ihashcall=ishft(47055833459_8*n8,m-64) + + ihashcall=ihashcall_from_n8(n8,m) return end function ihashcall +integer function ihashcall_from_n8(n8,m) + implicit none + + integer(kind=8), intent(in) :: n8 + integer, intent(in) :: m + integer(kind=8), parameter :: hash_factor(0:3) = (/2419_8, 62655_8, 10_8, 0_8/) + integer(kind=8) :: b(0:3), p(0:3) + integer(kind=8) :: carry, term + integer(kind=8) :: x + integer :: i,j + + x=n8 + do i=0,3 + b(i)=mod(x,65536_8) + x=x/65536_8 + enddo + + ! Return the top m bits of the low 64 bits of 47055833459*n8. + carry=0_8 + do i=0,3 + term=carry + do j=0,i + term=term+hash_factor(j)*b(i-j) + enddo + p(i)=mod(term,65536_8) + carry=term/65536_8 + enddo + + if(m.le.16) then + ihashcall_from_n8=int(p(3)/(2_8**(16-m))) + else + ihashcall_from_n8=int(p(3)*(2_8**(m-16)) + p(2)/(2_8**(32-m))) + endif + + return +end function ihashcall_from_n8 + subroutine save_hash_call(c13,n10,n12,n22) character*13 c13,cw - cw=c13 + cw=c13 if(cw(1:1).eq.' ' .or. cw(1:5).eq.'<...>') return if(cw(1:1).eq.'<') cw=cw(2:) i=index(cw,'>') @@ -125,7 +372,7 @@ subroutine save_hash_call(c13,n10,n12,n22) if(n12.ge.0 .and. n12 .le. 4095 .and. cw.ne.mycall13) calls12(n12)=cw n22=ihashcall(cw,22) - if(any(ihash22.eq.n22)) then ! If entry exists, make sure callsign is the most recently received one + if(any(ihash22.eq.n22)) then ! If entry exists, make sure callsign is the most recently received one where(ihash22.eq.n22) calls22=cw go to 900 endif @@ -139,157 +386,444 @@ subroutine save_hash_call(c13,n10,n12,n22) calls22(1)=cw if(nzhash.lt.MAXHASH) nzhash=nzhash+1 900 continue - return + return end subroutine save_hash_call -subroutine pack77(msg0,i3,n3,c77) +subroutine normalize_hash_call(c13,cw,ok) - use packjt - character*37 msg,msg0 - character*18 c18 - character*13 w(19) - character*77 c77 - integer nw(19) - integer ntel(3) + character(len=*), intent(in) :: c13 + character(len=13), intent(out) :: cw + logical, intent(out) :: ok + integer :: i + + cw=c13 + ok=.false. + if(cw(1:1).eq.' ' .or. cw(1:5).eq.'<...>') return + if(cw(1:1).eq.'<') then + if(.not.pack77_valid_hash_call_token(cw)) return + cw=cw(2:) + i=index(cw,'>') + if(i.gt.0) cw(i:)=' ' + endif + if(len(trim(cw)) .lt. 3) return + ok=.true. + + return +end subroutine normalize_hash_call + +subroutine stage_hash_call(hash_facts,c13) + + type(pack77_hash_facts), intent(inout) :: hash_facts + character(len=*), intent(in) :: c13 + character(len=13) :: cw + logical :: ok + +! Do not update the global hash table while testing encode candidates. The +! accepted candidate's facts are committed once by pack77. + call normalize_hash_call(c13,cw,ok) + if(.not.ok) return + if(hash_facts%ncalls.ge.size(hash_facts%calls)) return + hash_facts%ncalls=hash_facts%ncalls+1 + hash_facts%calls(hash_facts%ncalls)=cw + + return +end subroutine stage_hash_call + +subroutine pack77_hash_index(hash_facts,token,nbits,n,ok) + type(pack77_hash_facts), intent(inout) :: hash_facts + character(len=*), intent(in) :: token + integer, intent(in) :: nbits + integer, intent(out) :: n + logical, intent(out) :: ok + character(len=13) :: hash_call + + call normalize_hash_call(token,hash_call,ok) + if(.not.ok) return + n=ihashcall(hash_call,nbits) + call stage_hash_call(hash_facts,hash_call) + + return +end subroutine pack77_hash_index + +subroutine commit_hash_facts(hash_facts) + + type(pack77_hash_facts), intent(in) :: hash_facts + integer :: i,n10,n12,n22 + +! All hash-producing encode paths feed the same shared tables here. + do i=1,hash_facts%ncalls + call save_hash_call(hash_facts%calls(i),n10,n12,n22) + enddo + + return +end subroutine commit_hash_facts + +character(len=37) function pack77_expand_dx_macro(msg0,dx_base) result(msg) + character(len=*), intent(in) :: msg0,dx_base + character(len=37) :: msg1 + integer :: i1 msg=msg0 - if(msg(1:3).eq.'$DX') then - i1=index(msg,' ') - msg=trim(dxbase)//' '//msg(i1+1:) + msg1=adjustl(msg0) + if(.not.pack77_first_word_is_dx_macro(msg1)) return + if(len_trim(dx_base).eq.0) return + + i1=index(msg1,' ') + if(i1.gt.0) then + msg=trim(dx_base)//' '//adjustl(msg1(i1+1:)) + else + msg=trim(dx_base) endif +end function pack77_expand_dx_macro - i3_hint=i3 - n3_hint=n3 - i3=-1 - n3=-1 - if(i3_hint.eq.0 .and. n3_hint.eq.5) go to 5 +logical function pack77_first_word_is_dx_macro(msg0) result(is_macro) + character(len=*), intent(in) :: msg0 + character(len=37) :: first_word,msg1 + integer :: i,i1 + + msg1=adjustl(msg0) + if(len_trim(msg1).eq.0) then + is_macro=.false. + return + endif + + i1=index(msg1,' ') + if(i1.gt.1) then + first_word=msg1(1:i1-1) + else + first_word=trim(msg1) + endif + + do i=1,len_trim(first_word) + if(first_word(i:i).ge.'a' .and. first_word(i:i).le.'z') & + first_word(i:i)=char(ichar(first_word(i:i))-32) + enddo + + is_macro=first_word.eq.'$DX' .or. first_word.eq.'$DXCALL' +end function pack77_first_word_is_dx_macro + +logical function pack77_failed_preferred_wspr_shape(msg0,options) result(reject) + + character(len=*), intent(in) :: msg0 + type(pack77_options), intent(in), optional :: options + character(len=13) :: source_tokens(2) + integer :: nwords + logical :: split_ok + +! FST4W callers use this option to request a WSPR/50-bit payload, not merely +! to try WSPR first. If the source has WSPR shape but is not valid WSPR, reject +! instead of falling through to another pack77 family or free text. + reject=.false. + if(.not.present(options)) return + if(.not.options%prefer_wspr_50bit) return + + call pack77_split_source_tokens(msg0,2,nwords,source_tokens,split_ok) + reject=split_ok .and. nwords.eq.2 .and. & + len_trim(source_tokens(1)).ge.5 .and. & + len_trim(source_tokens(1)).le.12 .and. & + len_trim(source_tokens(2)).le.6 +end function pack77_failed_preferred_wspr_shape + +type(pack77_encode_core_result) function pack77_encode_core(msg0,options) & + result(encoded) + + character*37 msg,msg0 + character(len=13) :: source_tokens(19) + type(pack77_options), intent(in), optional :: options + type(pack77_candidate) :: candidate + character(len=6) :: dx_macro_base + integer :: nwords + logical prefer_wspr_50bit + logical expand_dx_macro + logical split_ok + logical word_overflow + logical starts_with_special_token + + encoded%result=pack77_no_match() + prefer_wspr_50bit=.false. + expand_dx_macro=.false. + dx_macro_base='' + if(present(options)) then + prefer_wspr_50bit=options%prefer_wspr_50bit + expand_dx_macro=options%expand_dx_macro + dx_macro_base=options%dx_macro_base + endif + msg=msg0 + if(expand_dx_macro) msg=pack77_expand_dx_macro(msg,dx_macro_base) ! Convert msg to upper case; collapse multiple blanks; parse into words. - call split77(msg,nwords,nw,w) - if(msg(1:3).eq.'CQ ' .or. msg(1:3).eq.'DE ' .or. msg(1:4).eq.'QRZ ') go to 100 + call pack77_split_source_tokens(msg,19,nwords,source_tokens,split_ok) + word_overflow=.not.split_ok + starts_with_special_token=msg(1:3).eq.'CQ ' .or. msg(1:3).eq.'DE ' .or. & + msg(1:4).eq.'QRZ ' + if(word_overflow) then + if(.not.starts_with_special_token) then + encoded%result=pack77_05(msg) + if(encoded%result%encoded) return + endif + encoded%result=pack77_free_text_exact(msg) + return + endif + if(.not.starts_with_special_token) then ! Check 0.1 (DXpedition mode) - call pack77_01(nwords,w,i3,n3,c77) - if(i3.ge.0 .or. n3.ge.1) go to 900 + candidate=pack77_01_candidate(msg) + if(take_candidate(candidate,msg,encoded)) return ! Check 0.2 (EU VHF contest exchange) ! call pack77_02(nwords,w,i3,n3,c77) ! if(i3.ge.0) go to 900 ! Check 0.3 and 0.4 (ARRL Field Day exchange) - call pack77_03(nwords,w,i3,n3,c77) - if(i3.ge.0) go to 900 - if(nwords.ge.2) go to 100 + candidate=pack77_03_candidate(msg) + if(take_candidate(candidate,msg,encoded)) return + if(nwords.lt.2) then - ! Check 0.5 (telemetry) -5 i0=index(msg,' ') - c18=msg(1:i0-1) - c18=adjustr(c18) - ntel=-99 - read(c18,1005,err=6) ntel -1005 format(3z6) - if(ntel(1).ge.2**23) go to 800 -6 if(ntel(1).ge.0 .and. ntel(2).ge.0 .and. ntel(3).ge.0) then - i3=0 - n3=5 - write(c77,1006) ntel,n3,i3 -1006 format(b23.23,2b24.24,2b3.3) - go to 900 +! Check 0.5 (telemetry) + encoded%result=pack77_05(msg) + if(encoded%result%encoded) return + endif endif -100 call pack77_06(nwords,w,i3,n3,c77,i3_hint,n3_hint) - if(i3.ge.0) go to 900 + candidate=pack77_06_candidate(msg,prefer_wspr_50bit) + if(take_candidate(candidate,msg,encoded)) return ! Check Type 1 (Standard 77-bit message) or Type 2, with optional "/P" - call pack77_1(nwords,w,i3,n3,c77) - if(i3.ge.0) go to 900 + candidate=pack77_1_candidate(msg) + if(take_candidate(candidate,msg,encoded)) return ! Check Type 3 (ARRL RTTY contest exchange) - call pack77_3(nwords,w,i3,n3,c77) - if(i3.ge.0) go to 900 + candidate=pack77_3_candidate(msg) + if(take_candidate(candidate,msg,encoded)) return ! Check Type 4 (One nonstandard call and one hashed call) - call pack77_4(nwords,w,i3,n3,c77) - if(i3.ge.0) go to 900 + candidate=pack77_4_candidate(msg) + if(take_candidate(candidate,msg,encoded)) return ! Check Type 5 (EU VHF Contest with 2 hashed calls, report, serial, and grid6) - call pack77_5(nwords,w,i3,n3,c77) - if(i3.ge.0) go to 900 + candidate=pack77_5_candidate(msg) + if(take_candidate(candidate,msg,encoded)) return + +! It defaults to exactly representable free text. + encoded%result=pack77_free_text_exact(msg) +end function pack77_encode_core + +type(pack77_result) function pack77_encode_result(msg0,options) result(encoded) + + character*37 msg0 + type(pack77_options), intent(in), optional :: options + type(pack77_encode_core_result) :: core + logical :: record_tx_hashes + + record_tx_hashes=.true. + if(present(options)) record_tx_hashes=options%record_tx_hashes + + core=pack77_encode_core(msg0,options) + encoded=core%result + if(.not.encoded%encoded .and. & + encoded%status.ne.PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED .and. & + pack77_failed_preferred_wspr_shape(pack77_normalized_message(msg0), & + options)) encoded=pack77_reject(PACK77_STATUS_PREFERRED_FAMILY_REJECTED) + if(record_tx_hashes) call commit_hash_facts(core%hash_facts) +end function pack77_encode_result + +subroutine pack77(msg0,i3,n3,c77,options,status) + character(len=*), intent(in) :: msg0 + integer, intent(out) :: i3,n3 + character(len=77), intent(out) :: c77 + type(pack77_options), intent(in), optional :: options + integer, intent(out), optional :: status + type(pack77_result) :: packed + + packed=pack77_encode_result(msg0,options) + i3=packed%i3 + n3=packed%n3 + c77=packed%c77 + if(present(status)) status=packed%status +end subroutine pack77 -! It defaults to free text -800 i3=0 - n3=0 - msg(14:)=' ' - call packtext77(msg(1:13),c77(1:71)) - write(c77(72:77),'(2b3.3)') n3,i3 +subroutine pack77_legacy_truncating_fallback(msg0,i3,n3,c77,options,status) + character(len=*), intent(in) :: msg0 + integer, intent(out) :: i3,n3 + character(len=77), intent(out) :: c77 + type(pack77_options), intent(in), optional :: options + integer, intent(out), optional :: status + type(pack77_result) :: packed + + packed=pack77_legacy_truncating_result(msg0,options) + i3=packed%i3 + n3=packed%n3 + c77=packed%c77 + if(present(status)) status=packed%status +end subroutine pack77_legacy_truncating_fallback + +type(pack77_result) function pack77_legacy_truncating_result(msg0,options) & + result(encoded) + character(len=*), intent(in) :: msg0 + type(pack77_options), intent(in), optional :: options + type(pack77_result) :: fallback + character(len=37) :: msg + character(len=13) :: payload + + encoded=pack77_encode_result(msg0,options) + if(encoded%encoded) return + if(encoded%status.eq.PACK77_STATUS_PREFERRED_FAMILY_REJECTED) return + if(encoded%status.eq.PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED) return -900 return -end subroutine pack77 + msg=msg0 + if(present(options)) then + if(options%expand_dx_macro) msg=pack77_expand_dx_macro(msg, & + options%dx_macro_base) + endif + msg=pack77_normalized_message(msg) + if(len_trim(msg).lt.1) return + + payload=msg(1:13) + if(.not.pack77_free_text_alphabet_ok(payload)) return + + fallback=pack77_free_text_accept(payload,PACK77_STATUS_ENCODED) + if(fallback%encoded) encoded=fallback +end function pack77_legacy_truncating_result + +type(pack77_result) function pack77_05(msg) result(encoded) + character(len=*), intent(in) :: msg + character*77 c77 + character*37 text + character*18 c18 + character*1 ch + integer ntel(3) + logical ok + + encoded=pack77_no_match() + text=adjustl(msg) + n=len_trim(text) + if(n.lt.1 .or. n.gt.18) return + do i=1,n + ch=text(i:i) + if(ch.ge.'a' .and. ch.le.'z') ch=char(ichar(ch)-32) + if(index('0123456789ABCDEF',ch).eq.0) return + text(i:i)=ch + enddo + c18=text(1:n) + c18=adjustr(c18) + ntel=-99 + read(c18,1005,err=6) ntel +1005 format(3z6) + 6 if(ntel(1).ge.0 .and. ntel(2).ge.0 .and. ntel(3).ge.0) then + call encode_pack77_telemetry(ntel(1),ntel(2),ntel(3),c77,ok) + if(ok) then + encoded=pack77_accept(0,5,c77) + endif + endif +end function pack77_05 + +type(pack77_result) function pack77_free_text_exact(msg) result(encoded) + character(len=*), intent(in) :: msg + character*37 msg0 + + msg0=pack77_normalized_message(msg) + if(.not.pack77_exact_free_text_ok(msg0)) then + encoded=pack77_reject(pack77_free_text_reject_status(msg0)) + return + endif + encoded=pack77_free_text_accept(msg0,PACK77_STATUS_ENCODED) +end function pack77_free_text_exact + +type(pack77_result) function pack77_free_text_accept(msg,status) result(encoded) + character(len=*), intent(in) :: msg + integer, intent(in) :: status + character*37 msg0 + character*71 text_bits + character*77 c77 + logical ok + + msg0=msg + msg0(14:)=' ' + call packtext77(msg0(1:13),text_bits) + call encode_pack77_free_text(text_bits,c77,ok) + encoded=pack77_no_match() + if(ok) then + encoded=pack77_accept(0,0,c77) + encoded%status=status + endif +end function pack77_free_text_accept + +integer function pack77_free_text_reject_status(msg) result(status) + character(len=*), intent(in) :: msg + + if(.not.pack77_free_text_alphabet_ok(msg)) then + status=PACK77_STATUS_FREE_TEXT_INVALID + else if(len_trim(msg).gt.13) then + status=PACK77_STATUS_FREE_TEXT_TOO_LONG + else + status=PACK77_STATUS_FREE_TEXT_INVALID + endif +end function pack77_free_text_reject_status + +logical function pack77_exact_free_text_ok(msg) result(ok) + character(len=*), intent(in) :: msg + + ok=.false. + if(len_trim(msg).gt.13) return + ok=pack77_free_text_alphabet_ok(msg(1:len_trim(msg))) +end function pack77_exact_free_text_ok + +logical function pack77_free_text_alphabet_ok(msg) result(ok) + character(len=*), intent(in) :: msg + character(len=42), parameter :: alphabet=' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+-./?' + integer :: i + + ok=.false. + do i=1,len_trim(msg) + if(index(alphabet,msg(i:i)).eq.0) return + enddo + ok=.true. +end function pack77_free_text_alphabet_ok + +! Must mirror the character canonicalization of pack77_split_source_tokens +! (NUL->space, uppercase fold, blank collapse) so free text and structured +! parsing agree on the canonical form. +character(len=37) function pack77_normalized_message(msg) result(normalized) + character(len=*), intent(in) :: msg + character(len=1) :: c,c0 + integer :: i,j,iz + + normalized=' ' + c0=' ' + j=0 + iz=len_trim(msg) + do i=1,min(iz,len(normalized)) + c=msg(i:i) + if(ichar(c).eq.0) c=' ' + if(c.eq.' ' .and. c0.eq.' ') cycle + if(c.ge.'a' .and. c.le.'z') c=char(ichar(c)-32) + j=j+1 + normalized(j:j)=c + c0=c + enddo +end function pack77_normalized_message subroutine unpack77(c77,nrx,msg,unpk77_success) ! ! nrx=1 when unpacking a received message ! nrx=0 when unpacking a to-be-transmitted message -! the value of nrx is used to decide when mycall13 or dxcall13 should -! be used in place of a callsign from the hashtable ! - parameter (NSEC=86) !Number of ARRL Sections - parameter (NUSCAN=171) !Number of States and Provinces - parameter (MAXGRID4=32400) - integer*8 n58 - integer ntel(3) character*77 c77 character*37 msg - character*13 call_1,call_2,call_3,call_1a + integer, intent(in) :: nrx + logical unpk77_success character*13 mycall13_0,dxcall13_0 - character*11 c11 - character*3 crpt,cntx,cpfx - character*3 cmult(NUSCAN) - character*6 cexch,grid6 - character*4 grid4,cserial - character*3 csec(NSEC) - character*38 c - character*36 a2 integer hashmy10,hashmy12,hashmy22,hashdx10,hashdx12,hashdx22 - logical unpk28_success,unpk77_success,unpkg4_success logical dxcall13_set,mycall13_set + type(unpack77_context) :: context + type(unpack77_effect_policy) :: policy - data a2/'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/,nzzz/46656/ - data c/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/'/ - data csec/ & - "AB ","AK ","AL ","AR ","AZ ","BC ","CO ","CT ","DE ","EB ", & - "EMA","ENY","EPA","EWA","GA ","GH ","IA ","ID ","IL ","IN ", & - "KS ","KY ","LA ","LAX","NS ","MB ","MDC","ME ","MI ","MN ", & - "MO ","MS ","MT ","NC ","ND ","NE ","NFL","NH ","NL ","NLI", & - "NM ","NNJ","NNY","TER","NTX","NV ","OH ","OK ","ONE","ONN", & - "ONS","OR ","ORG","PAC","PR ","QC ","RI ","SB ","SC ","SCV", & - "SD ","SDG","SF ","SFL","SJV","SK ","SNJ","STX","SV ","TN ", & - "UT ","VA ","VI ","VT ","WCF","WI ","WMA","WNY","WPA","WTX", & - "WV ","WWA","WY ","DX ","PE ","NB "/ - data cmult/ & - "AL ","AK ","AZ ","AR ","CA ","CO ","CT ","DE ","FL ","GA ", & - "HI ","ID ","IL ","IN ","IA ","KS ","KY ","LA ","ME ","MD ", & - "MA ","MI ","MN ","MS ","MO ","MT ","NE ","NV ","NH ","NJ ", & - "NM ","NY ","NC ","ND ","OH ","OK ","OR ","PA ","RI ","SC ", & - "SD ","TN ","TX ","UT ","VT ","VA ","WA ","WV ","WI ","WY ", & - "NB ","NS ","QC ","ON ","MB ","SK ","AB ","BC ","NWT","NF ", & - "LB ","NU ","YT ","PEI","DC ","DR ","FR ","GD ","GR ","OV ", & - "ZH ","ZL ","X01","X02","X03","X04","X05","X06","X07","X08", & - "X09","X10","X11","X12","X13","X14","X15","X16","X17","X18", & - "X19","X20","X21","X22","X23","X24","X25","X26","X27","X28", & - "X29","X30","X31","X32","X33","X34","X35","X36","X37","X38", & - "X39","X40","X41","X42","X43","X44","X45","X46","X47","X48", & - "X49","X50","X51","X52","X53","X54","X55","X56","X57","X58", & - "X59","X60","X61","X62","X63","X64","X65","X66","X67","X68", & - "X69","X70","X71","X72","X73","X74","X75","X76","X77","X78", & - "X79","X80","X81","X82","X83","X84","X85","X86","X87","X88", & - "X89","X90","X91","X92","X93","X94","X95","X96","X97","X98", & - "X99"/ data dxcall13_set/.false./ data mycall13_set/.false./ data mycall13_0/''/ - data dxcall13_0/''/ + data dxcall13_0/' '/ save hashmy10,hashmy12,hashmy22,hashdx10,hashdx12,hashdx22 + save dxcall13_set,mycall13_set,mycall13_0,dxcall13_0 if(mycall13.ne.mycall13_0) then if(len(trim(mycall13)).gt.2) then @@ -308,365 +842,51 @@ subroutine unpack77(c77,nrx,msg,unpk77_success) hashdx10=ihashcall(dxcall13,10) hashdx12=ihashcall(dxcall13,12) hashdx22=ihashcall(dxcall13,22) + else + dxcall13_set=.false. endif endif - unpk77_success=.true. - -! Check for bad data - do i=1,77 - if(c77(i:i).ne.'0' .and. c77(i:i).ne.'1') then - msg='failed unpack' - unpk77_success=.false. - return - endif - enddo - - read(c77(72:77),'(2b3)') n3,i3 - msg=repeat(' ',37) - - if(i3.eq.0 .and. n3.eq.0) then -! 0.0 Free text - call unpacktext77(c77(1:71),msg(1:13)) - msg(14:)=' ' - msg=adjustl(msg) - if(msg(1:1).eq.' ') then - unpk77_success=.false. - return - endif - else if(i3.eq.0 .and. n3.eq.1) then -! 0.1 K1ABC RR73; W9XYZ <KH1/KH7Z> -11 28 28 10 5 71 DXpedition Mode - read(c77,1010) n28a,n28b,n10,n5 -1010 format(2b28,b10,b5) - irpt=2*n5 - 30 - write(crpt,1012) irpt -1012 format(i3.2) - if(irpt.ge.0) crpt(1:1)='+' - call unpack28(n28a,call_1,unpk28_success) - if(.not.unpk28_success .or. n28a.le.2) unpk77_success=.false. - call unpack28(n28b,call_2,unpk28_success) - if(.not.unpk28_success .or. n28b.le.2) unpk77_success=.false. - call hash10(n10,call_3) - if(call_3.eq.'<...>') then - call hash10var(n10,call_3,1) - endif - if(nrx.eq.1 .and. & - dxcall13_set .and. & - len(trim(dxcall13)).ge.3 .and. & - hashdx10.eq.n10) call_3='<'//trim(dxcall13)//'>' - if(nrx.eq.0 .and. & - mycall13_set .and. & - n10.eq.hashmy10) call_3='<'//trim(mycall13)//'>' - msg=trim(call_1)//' RR73; '//trim(call_2)//' '//trim(call_3)//' '//crpt - - else if(i3.eq.0 .and. n3.eq.2) then - unpk77_success=.false. - - else if(i3.eq.0 .and. (n3.eq.3 .or. n3.eq.4)) then -! 0.3 WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 ARRL Field Day -! 0.4 WA9XYZ KA1ABC R 32A EMA 28 28 1 4 3 7 71 ARRL Field Day - read(c77,1030) n28a,n28b,ir,intx,nclass,isec -1030 format(2b28,b1,b4,b3,b7) - if(isec.gt.NSEC .or. isec.lt.1) then - unpk77_success=.false. - isec=1 - endif - call unpack28(n28a,call_1,unpk28_success) - if(.not.unpk28_success .or. n28a.le.2) unpk77_success=.false. - call unpack28(n28b,call_2,unpk28_success) - if(.not.unpk28_success .or. n28b.le.2) unpk77_success=.false. - ntx=intx+1 - if(n3.eq.4) ntx=ntx+16 - write(cntx(1:2),1032) ntx -1032 format(i2) - cntx(3:3)=char(ichar('A')+nclass) - if(ir.eq.0 .and. ntx.lt.10) msg=trim(call_1)//' '//trim(call_2)// & - cntx//' '//csec(isec) - if(ir.eq.1 .and. ntx.lt.10) msg=trim(call_1)//' '//trim(call_2)// & - ' R'//cntx//' '//csec(isec) - if(ir.eq.0 .and. ntx.ge.10) msg=trim(call_1)//' '//trim(call_2)// & - ' '//cntx//' '//csec(isec) - if(ir.eq.1 .and. ntx.ge.10) msg=trim(call_1)//' '//trim(call_2)// & - ' R '//cntx//' '//csec(isec) - - else if(i3.eq.0 .and. n3.eq.5) then -! 0.5 0123456789abcdef01 71 71 Telemetry (18 hex) - read(c77,1006) ntel -1006 format(b23,2b24) - write(msg,1007) ntel -1007 format(3z6.6) - do i=1,18 - if(msg(i:i).ne.'0') exit - msg(i:i)=' ' - enddo - msg=adjustl(msg) - - else if(i3.eq.0 .and. n3.eq.6) then - read(c77(48:50),'(3b1)') j48,j49,j50 -! bits 48:50 -! itype=1: x00 -! itype=2: xx1 -! itype=3: 010 - if(j50.eq.1) then - itype=2 - else if(j49.eq.0) then - itype=1 - else if(j48.eq.0) then - itype=3 - else - itype=-1 - unpk77_success=.false. - endif - - if(itype.eq.1) then -! WSPR Type 1 - read(c77,2010) n28,igrid4,idbm -2010 format(b28.28,b15.15,b5.5) - idbm=nint(idbm*10.0/3.0) - if(idbm.lt.0 .or. idbm.gt.60) unpk77_success=.false. - call unpack28(n28,call_1,unpk28_success) - if(.not.unpk28_success) unpk77_success=.false. - call to_grid4(igrid4,grid4,unpkg4_success) - if(.not.unpkg4_success) unpk77_success=.false. - write(crpt,'(i3)') idbm - msg=trim(call_1)//' '//grid4//' '//trim(adjustl(crpt)) - if (unpk77_success) call save_hash_call(call_1,n10,n12,n22) !### Is this OK here? ### - - else if(itype.eq.2) then -! WSPR Type 2 - read(c77,2020) n28,npfx,idbm -2020 format(b28.28,b16.16,b5.5) - idbm=nint(idbm*10.0/3.0) - if(idbm.lt.0 .or. idbm.gt.60) unpk77_success=.false. - call unpack28(n28,call_1,unpk28_success) - if(.not.unpk28_success) unpk77_success=.false. - write(crpt,'(i3)') idbm - cpfx=' ' - if(npfx.lt.nzzz) then -! Prefix - do i=3,1,-1 - j=mod(npfx,36)+1 - cpfx(i:i)=a2(j:j) - npfx=npfx/36 - if(npfx.eq.0) exit - enddo - msg=trim(adjustl(cpfx))//'/'//trim(call_1)//' '//trim(adjustl(crpt)) - call_1a=trim(adjustl(cpfx))//'/'//trim(call_1) - call save_hash_call(call_1a,n10,n12,n22) !### Is this OK here? ### - else -! Suffix - npfx=npfx-nzzz - if(npfx.le.35) then - cpfx(1:1)=a2(npfx+1:npfx+1) - else if(npfx.gt.35 .and. npfx.le.1295) then - cpfx(1:1)=a2(npfx/36+1:npfx/36+1) - cpfx(2:2)=a2(mod(npfx,36)+1:mod(npfx,36)+1) - else if(npfx.gt.1295 .and. npfx.le.12959) then - cpfx(1:1)=a2(npfx/360+1:npfx/360+1) - cpfx(2:2)=a2(mod(npfx/10,36)+1:mod(npfx/10,36)+1) - cpfx(3:3)=a2(mod(npfx,10)+1:mod(npfx,10)+1) - else - unpk77_success=.false. - return - endif - msg=trim(call_1)//'/'//trim(adjustl(cpfx))//' '//trim(adjustl(crpt)) - call_1a=trim(call_1)//'/'//trim(adjustl(cpfx)) - call save_hash_call(call_1a,n10,n12,n22) !### Is this OK here? ### - endif - - else if(itype.eq.3) then -! WSPR Type 3 - read(c77,2030) n22,igrid6 -2030 format(b22.22,b25.25) - n28=n22+2063592 - call unpack28(n28,call_1,unpk28_success) - if(.not.unpk28_success) unpk77_success=.false. - call to_grid(igrid6,grid6,unpkg4_success) - if(.not.unpkg4_success) unpk77_success=.false. - msg=trim(call_1)//' '//grid6 - endif - - else if(i3.eq.0 .and. n3.gt.6) then - unpk77_success=.false. - - else if(i3.eq.1 .or. i3.eq.2) then -! Type 1 (standard message) or Type 2 ("/P" form for EU VHF contest) - read(c77,1000) n28a,ipa,n28b,ipb,ir,igrid4,i3 -1000 format(2(b28,b1),b1,b15,b3) - call unpack28(n28a,call_1,unpk28_success) - if(nrx.eq.1 .and. mycall13_set .and. hashmy22.eq.(n28a-2063592)) then - call_1='<'//trim(mycall13)//'>' - unpk28_success=.true. - endif - if(.not.unpk28_success) unpk77_success=.false. - call unpack28(n28b,call_2,unpk28_success) - if(.not.unpk28_success) unpk77_success=.false. - if(call_1(1:3).eq.'CQ_') call_1(3:3)=' ' - if(index(call_1,'<').le.0) then - i=index(call_1,' ') - if(i.ge.4 .and. ipa.eq.1 .and. i3.eq.1) call_1(i:i+1)='/R' - if(i.ge.4 .and. ipa.eq.1 .and. i3.eq.2) call_1(i:i+1)='/P' - if(i.ge.4) call add_call_to_recent_calls(call_1) - endif - if(index(call_2,'<').le.0) then - i=index(call_2,' ') - if(i.ge.4 .and. ipb.eq.1 .and. i3.eq.1) call_2(i:i+1)='/R' - if(i.ge.4 .and. ipb.eq.1 .and. i3.eq.2) call_2(i:i+1)='/P' - if(i.ge.4) then - call add_call_to_recent_calls(call_2) -! only hash the "from" call - call save_hash_call(call_2,ndum10,ndum12,ndum22) - endif - endif - if(igrid4.le.MAXGRID4) then - call to_grid4(igrid4,grid4,unpkg4_success) - if(.not.unpkg4_success) unpk77_success=.false. - if(ir.eq.0) msg=trim(call_1)//' '//trim(call_2)//' '//grid4 - if(ir.eq.1) msg=trim(call_1)//' '//trim(call_2)//' R '//grid4 - if(msg(1:3).eq.'CQ ' .and. ir.eq.1) unpk77_success=.false. - else - irpt=igrid4-MAXGRID4 - if(irpt.eq.1) msg=trim(call_1)//' '//trim(call_2) - if(irpt.eq.2) msg=trim(call_1)//' '//trim(call_2)//' RRR' - if(irpt.eq.3) msg=trim(call_1)//' '//trim(call_2)//' RR73' - if(irpt.eq.4) msg=trim(call_1)//' '//trim(call_2)//' 73' - if(irpt.ge.5) then - isnr=irpt-35 - if(isnr.gt.50) isnr=isnr-101 - write(crpt,'(i3.2)') isnr - if(crpt(1:1).eq.' ') crpt(1:1)='+' - if(ir.eq.0) msg=trim(call_1)//' '//trim(call_2)//' '//crpt - if(ir.eq.1) msg=trim(call_1)//' '//trim(call_2)//' R'//crpt - endif - if(msg(1:3).eq.'CQ ' .and. irpt.ge.2) unpk77_success=.false. - endif + context%nrx=nrx + context%configured=.false. + context%strict_var_guards=.false. + context%mycall_set=mycall13_set + context%dxcall_set=dxcall13_set + context%mycall=mycall13 + context%dxcall=dxcall13 + context%hashmy10=hashmy10 + context%hashmy12=hashmy12 + context%hashmy22=hashmy22 + context%hashdx10=hashdx10 + + policy=unpack77_effect_policy() + call run_unpack77(c77,context,policy,msg,unpk77_success) - else if(i3.eq.3) then -! Type 3: ARRL RTTY Contest - read(c77,1040) itu,n28a,n28b,ir,irpt,nexch,i3 -1040 format(b1,2b28.28,b1,b3.3,b13.13,b3.3) - write(crpt,1042) irpt+2 -1042 format('5',i1,'9') - nserial=nexch - imult=-1 - if(nexch.gt.8000) then - imult=nexch-8000 - nserial=-1 - endif - call unpack28(n28a,call_1,unpk28_success) - if(.not.unpk28_success) unpk77_success=.false. - call unpack28(n28b,call_2,unpk28_success) - if(.not.unpk28_success) unpk77_success=.false. - imult=0 - nserial=0 - if(nexch.gt.8000) imult=nexch-8000 - if(nexch.lt.8000) nserial=nexch - - if(imult.ge.1 .and.imult.le.NUSCAN) then - if(itu.eq.0 .and. ir.eq.0) msg=trim(call_1)//' '//trim(call_2)// & - ' '//crpt//' '//cmult(imult) - if(itu.eq.1 .and. ir.eq.0) msg='TU; '//trim(call_1)//' '//trim(call_2)// & - ' '//crpt//' '//cmult(imult) - if(itu.eq.0 .and. ir.eq.1) msg=trim(call_1)//' '//trim(call_2)// & - ' R '//crpt//' '//cmult(imult) - if(itu.eq.1 .and. ir.eq.1) msg='TU; '//trim(call_1)//' '//trim(call_2)// & - ' R '//crpt//' '//cmult(imult) - else if(nserial.ge.1 .and. nserial.le.7999) then - write(cserial,'(i4.4)') nserial - if(itu.eq.0 .and. ir.eq.0) msg=trim(call_1)//' '//trim(call_2)// & - ' '//crpt//' '//cserial - if(itu.eq.1 .and. ir.eq.0) msg='TU; '//trim(call_1)//' '//trim(call_2)// & - ' '//crpt//' '//cserial - if(itu.eq.0 .and. ir.eq.1) msg=trim(call_1)//' '//trim(call_2)// & - ' R '//crpt//' '//cserial - if(itu.eq.1 .and. ir.eq.1) msg='TU; '//trim(call_1)//' '//trim(call_2)// & - ' R '//crpt//' '//cserial - endif - else if(i3.eq.4) then -! Type 4 - read(c77,1050) n12,n58,iflip,nrpt,icq -1050 format(b12,b58,b1,b2,b1) - do i=11,1,-1 - j=mod(n58,38)+1 - c11(i:i)=c(j:j) - n58=n58/38 - enddo - call hash12(n12,call_3) - if(call_3.eq.'<...>') then - call hash12var(n12,call_3,1) - endif - if(iflip.eq.0) then ! 12 bit hash for TO call - call_1=call_3 - call_2=adjustl(c11)//' ' - call add_call_to_recent_calls(call_2) - call save_hash_call(call_2,ndum10,ndum12,ndum22) - if(nrx.eq.1 .and. & - dxcall13_set .and. mycall13_set .and. & - call_2.eq.dxcall13 .and. & - n12.eq.hashmy12 ) call_1='<'//trim(mycall13)//'>' - if(nrx.eq.1 .and. & - mycall13_set .and. & - index(call_1,'<...>').gt.0 .and. & - n12.eq.hashmy12 ) call_1='<'//trim(mycall13)//'>' - else ! 12 bit hash for DE call - call_1=adjustl(c11) - call_2=call_3 - call add_call_to_recent_calls(call_1) - if(nrx.eq.0 .and. & - mycall13_set .and. & - n12.eq.hashmy12) call_2='<'//trim(mycall13)//'>' - endif - if(icq.eq.0) then - if(nrpt.eq.0) msg=trim(call_1)//' '//trim(call_2) - if(nrpt.eq.1) msg=trim(call_1)//' '//trim(call_2)//' RRR' - if(nrpt.eq.2) msg=trim(call_1)//' '//trim(call_2)//' RR73' - if(nrpt.eq.3) msg=trim(call_1)//' '//trim(call_2)//' 73' - else - msg='CQ '//trim(call_2) - endif + return +end subroutine unpack77 - else if(i3.eq.5) then - -! Type 5 <PA3XYZ> <G4ABC/P> R 590003 IO91NP h12 h22 r1 s3 S11 g25 -! EU VHF contest - read(c77,1060) n12,n22,ir,irpt,iserial,igrid6 -1060 format(b12,b22,b1,b3,b11,b25) - if(igrid6.lt.0 .or. igrid6.gt.18662399) then - unpk77_success=.false. - return - endif - call hash12(n12,call_1) - if(call_1.eq.'<...>') then - call hash12var(n12,call_1,1) - endif - if(n12.eq.hashmy12) call_1='<'//trim(mycall13)//'>' - call hash22(n22,call_2) - if(call_2.eq.'<...>') then - call hash22var(n22,call_2,1) - endif - nrs=52+irpt - write(cexch,1022) nrs,iserial -1022 format(i2,i4.4) - call to_grid6(igrid6,grid6,unpk77_success) - if(ir.eq.0) msg=trim(call_1)//' '//trim(call_2)//' '//cexch//' '//grid6 - if(ir.eq.1) msg=trim(call_1)//' '//trim(call_2)//' R '//cexch//' '//grid6 +subroutine pack28(c13,n28) +! Callers must pass a well-formed callsign or special/hash token. Malformed +! tokens are encoded as 22-bit hashes, never coerced to standard calls, per the +! c28 shape predicate in packjt77_grammar. + character*13 c13 + integer, intent(out) :: n28 + type(pack77_hash_facts) :: hash_facts - else if(i3.ge.6) then ! i3 values 6 and 7 are not yet defined - unpk77_success=.false. - endif - if(msg(1:4).eq.'CQ <') unpk77_success=.false. + call pack28_core(c13,n28,hash_facts) + call commit_hash_facts(hash_facts) return -end subroutine unpack77 +end subroutine pack28 -subroutine pack28(c13,n28) +subroutine pack28_core(c13,n28,hash_facts) ! Pack a special token, a 22-bit hash code, or a valid base call into a 28-bit ! integer. - parameter (NTOKENS=2063592,MAX22=4194304) - logical is_digit,is_letter character*13 c13 + integer, intent(out) :: n28 + type(pack77_hash_facts), intent(inout) :: hash_facts character*6 callsign character*1 c character*4 c4 @@ -678,9 +898,6 @@ subroutine pack28(c13,n28) data a2/'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/ data a3/'0123456789'/ data a4/' ABCDEFGHIJKLMNOPQRSTUVWXYZ'/ - - is_digit(c)=c.ge.'0' .and. c.le.'9' - is_letter(c)=c.ge.'A' .and. c.le.'Z' n28=-1 ! Work-around for Swaziland prefix: @@ -694,7 +911,7 @@ subroutine pack28(c13,n28) n28=0 go to 900 endif - + if(c13(1:4).eq.'QRZ ') then n28=1 go to 900 @@ -707,74 +924,60 @@ subroutine pack28(c13,n28) if(c13(1:3).eq.'CQ_') then n=len(trim(c13)) - if(n.ge.4 .and. n.le.7) then - nlet=0 - nnum=0 - do i=4,n - c=c13(i:i) - if(c.ge.'A' .and. c.le.'Z') nlet=nlet+1 - if(c.ge.'0' .and. c.le.'9') nnum=nnum+1 - enddo - if(nnum.eq.3 .and. nlet.eq.0) then - read(c13(4:3+nnum),*) nqsy +! The modifier grammar (1-4 letters or exactly 3 digits) is owned by +! pack77_valid_cq_modifier; only the number-space arithmetic lives here. + if(n.ge.4 .and. pack77_valid_cq_modifier(c13(4:n))) then + if(pack77_all_digits(c13(4:n))) then + nqsy=100*(iachar(c13(4:4))-iachar('0')) + & + 10*(iachar(c13(5:5))-iachar('0')) + & + iachar(c13(6:6))-iachar('0') n28=3+nqsy go to 900 endif - if(nlet.ge.1 .and. nlet.le.4 .and. nnum.eq.0) then - c4=c13(4:n) - c4=adjustr(c4) - m=0 - do i=1,4 - j=0 - c=c4(i:i) - if(c.ge.'A' .and. c.le.'Z') j=ichar(c)-ichar('A')+1 - m=27*m + j - enddo - n28=3+1000+m - go to 900 - endif + c4=c13(4:n) + c4=adjustr(c4) + m=0 + do i=1,4 + j=0 + c=c4(i:i) + if(c.ge.'A' .and. c.le.'Z') j=ichar(c)-ichar('A')+1 + m=27*m + j + enddo + n28=3+1000+m + go to 900 endif endif ! Check for <...> callsign if(c13(1:1).eq.'<')then - call save_hash_call(c13,n10,n12,n22) !Save callsign in hash table + if(.not.pack77_valid_hash_call_token(c13)) then + n28=PACK77_NTOKENS + go to 900 + endif + call stage_hash_call(hash_facts,c13) i2=index(c13,'>') c13=c13(2:i2-1) n22=ihashcall(c13,22) - n28=NTOKENS + n22 + n28=PACK77_NTOKENS + n22 go to 900 endif ! Check for standard callsign - iarea=-1 - n=len(trim(c13)) - do i=n,2,-1 - if(is_digit(c13(i:i))) exit - enddo - iarea=i !Call-area digit - npdig=0 !Digits before call area - nplet=0 !Letters before call area - do i=1,iarea-1 - if(is_digit(c13(i:i))) npdig=npdig+1 - if(is_letter(c13(i:i))) nplet=nplet+1 - enddo - nslet=0 - do i=iarea+1,n - if(is_letter(c13(i:i))) nslet=nslet+1 - enddo - if(iarea.lt.2 .or. iarea.gt.3 .or. nplet.eq.0 .or. & - npdig.ge.iarea-1 .or. nslet.gt.3) then + if(.not.pack77_c28_standard_shape(c13)) then ! Treat this as a nonstandard callsign: compute its 22-bit hash - call save_hash_call(c13,n10,n12,n22) !Save callsign in hash table + call stage_hash_call(hash_facts,c13) n22=ihashcall(c13,22) - n28=NTOKENS + n22 + n28=PACK77_NTOKENS + n22 go to 900 endif - + n=len(trim(c13)) + do i=n,2,-1 + if(pack77_is_digit_char(c13(i:i))) exit + enddo + iarea=i ! This is a standard callsign - call save_hash_call(c13,n10,n12,n22) !Save callsign in hash table + call stage_hash_call(hash_facts,c13) if(iarea.eq.2) callsign=' '//c13(1:5) if(iarea.eq.3) callsign=c13(1:6) i1=index(a1,callsign(1:1))-1 @@ -783,19 +986,33 @@ subroutine pack28(c13,n28) i4=index(a4,callsign(4:4))-1 i5=index(a4,callsign(5:5))-1 i6=index(a4,callsign(6:6))-1 + if(i1.lt.0 .or. i2.lt.0 .or. i3.lt.0 .or. i4.lt.0 .or. & + i5.lt.0 .or. i6.lt.0) then + n22=ihashcall(c13,22) + n28=PACK77_NTOKENS + n22 + go to 900 + endif n28=36*10*27*27*27*i1 + 10*27*27*27*i2 + 27*27*27*i3 + 27*27*i4 + & 27*i5 + i6 - n28=n28 + NTOKENS + MAX22 + n28=n28 + PACK77_NTOKENS + PACK77_MAX22 900 n28=iand(n28,ishft(1,28)-1) return -end subroutine pack28 - +end subroutine pack28_core subroutine unpack28(n28_0,c13,success) + integer, intent(in) :: n28_0 + character*13 c13 + logical success + + call unpack28_core(n28_0,c13,success,.false.) - parameter (NTOKENS=2063592,MAX22=4194304) + return +end subroutine unpack28 + +subroutine unpack28_core(n28_0,c13,success,reject_two_digit_prefix) logical success,callok + logical, intent(in) :: reject_two_digit_prefix character*13 c13 character*37 c1 character*36 c2 @@ -808,7 +1025,7 @@ subroutine unpack28(n28_0,c13,success) success=.true. n28=n28_0 - if(n28.lt.NTOKENS) then + if(n28.lt.PACK77_NTOKENS) then ! Special tokens DE, QRZ, CQ, CQ_nnn, CQ_aaaa if(n28.eq.0) c13='DE ' if(n28.eq.1) c13='QRZ ' @@ -834,19 +1051,16 @@ subroutine unpack28(n28_0,c13,success) go to 900 endif endif - n28=n28-NTOKENS - if(n28.lt.MAX22) then + n28=n28-PACK77_NTOKENS + if(n28.lt.PACK77_MAX22) then ! This is a 22-bit hash of a callsign n22=n28 call hash22(n22,c13) !Retrieve callsign from hash table - if(c13.eq.'<...>') then - call hash22var(n22,c13,1) - endif go to 900 endif - + ! Standard callsign - n=n28 - MAX22 + n=n28 - PACK77_MAX22 i1=n/(36*10*27*27*27) n=n-36*10*27*27*27*i1 i2=n/(10*27*27*27) @@ -862,18 +1076,24 @@ subroutine unpack28(n28_0,c13,success) c13=adjustl(c13) if(.not.callok(trim(c13))) then + ! Keep the legacy placeholder text for callers that still inspect c13 + ! when success is false. c13='QU1RK' success=.false. endif 900 i0=index(c13,' ') - if(i0.ne.0 .and. i0.lt.len(trim(c13))) then + if((i0.ne.0 .and. i0.lt.len(trim(c13))) .or. & + (reject_two_digit_prefix .and. pack77_is_digit_char(c13(1:1)) .and. & + pack77_is_digit_char(c13(2:2)))) then + ! Keep the legacy placeholder text for callers that still inspect c13 + ! when success is false. c13='QU1RK' success=.false. endif return -end subroutine unpack28 +end subroutine unpack28_core subroutine split77(msg,nwords,nw,w) @@ -885,13 +1105,14 @@ subroutine split77(msg,nwords,nw,w) character*6 bcall_1 logical ok1 integer nw(19) - + iz=len(trim(msg)) j=0 k=0 n=0 c0=' ' w=' ' + nw=0 do i=1,iz if(ichar(msg(i:i)).eq.0) msg(i:i)=' ' c=msg(i:i) !Single character @@ -904,584 +1125,33 @@ subroutine split77(msg,nwords,nw,w) n=n+1 !Index in word if(c.ge.'a' .and. c.le.'z') c=char(ichar(c)-32) !Force upper case msg(j:j)=c - if(n.le.13) w(k)(n:n)=c !Copy character c into word + if(k.le.19) then + nw(k)=n + if(n.le.13) w(k)(n:n)=c !Copy character c into word + endif c0=c enddo iz=j !Message length nwords=k !Number of words in msg if(nwords.le.0) go to 900 - do i=1,nwords - nw(i)=len(trim(w(i))) + do i=1,min(nwords,19) + if(nw(i).le.13) nw(i)=len(trim(w(i))) enddo msg(iz+1:)=' ' if(nwords.lt.3) go to 900 call chkcall(w(3),bcall_1,ok1) - if(ok1 .and. w(1)(1:3).eq.'CQ ') then + if(ok1 .and. w(1)(1:3).eq.'CQ ' .and. & + pack77_valid_cq_modifier(w(2))) then w(1)='CQ_'//w(2)(1:10) !Make "CQ " into "CQ_" w(2:12)=w(3:13) !Move all remaining words down by one nwords=nwords-1 - endif - -900 return -end subroutine split77 - - -subroutine pack77_01(nwords,w,i3,n3,c77) - -! Pack a Type 0.1 message: DXpedition mode -! Example message: "K1ABC RR73; W9XYZ <KH1/KH7Z> -11" 28 28 10 5 - - character*13 w(19),c13 - character*77 c77 - character*6 bcall_1,bcall_2 - logical ok1,ok2 - - if(nwords.ne.5) go to 900 !Must have 5 words - if(trim(w(2)).ne.'RR73;') go to 900 !2nd word must be "RR73;" - if(w(4)(1:1).ne.'<') go to 900 !4th word must have <...> - if(index(w(4),'>').lt.1) go to 900 - n=-99 - read(w(5),*,err=1) n -1 if(n.eq.-99) go to 900 !5th word must be a valid report - n5=(n+30)/2 - if(n5.lt.0) n5=0 - if(n5.gt.31) n5=31 - call chkcall(w(1),bcall_1,ok1) - if(.not.ok1) go to 900 !1st word must be a valid basecall - call chkcall(w(3),bcall_2,ok2) - if(.not.ok2) go to 900 !3rd word must be a valid basecall - -! Type 0.1: K1ABC RR73; W9XYZ <KH1/KH7Z> -11 28 28 10 5 71 - i3=0 - n3=1 - call pack28(w(1),n28a) - call pack28(w(3),n28b) - call save_hash_call(w(4),n10,n12,n22) - i2=index(w(4),'>') - c13=w(4)(2:i2-1) - n10=ihashcall(c13,10) - write(c77,1010) n28a,n28b,n10,n5,n3,i3 -1010 format(2b28.28,b10.10,b5.5,2b3.3) - -900 return -end subroutine pack77_01 - - -subroutine pack77_03(nwords,w,i3,n3,c77) - -! Check 0.3 and 0.4 (ARRL Field Day exchange) -! Example message: WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 - - parameter (NSEC=86) !Number of ARRL Sections - character*13 w(19) - character*77 c77 - character*6 bcall_1,bcall_2 - character*3 csec(NSEC) - logical ok1,ok2 - data csec/ & - "AB ","AK ","AL ","AR ","AZ ","BC ","CO ","CT ","DE ","EB ", & - "EMA","ENY","EPA","EWA","GA ","GH ","IA ","ID ","IL ","IN ", & - "KS ","KY ","LA ","LAX","NS ","MB ","MDC","ME ","MI ","MN ", & - "MO ","MS ","MT ","NC ","ND ","NE ","NFL","NH ","NL ","NLI", & - "NM ","NNJ","NNY","TER","NTX","NV ","OH ","OK ","ONE","ONN", & - "ONS","OR ","ORG","PAC","PR ","QC ","RI ","SB ","SC ","SCV", & - "SD ","SDG","SF ","SFL","SJV","SK ","SNJ","STX","SV ","TN ", & - "UT ","VA ","VI ","VT ","WCF","WI ","WMA","WNY","WPA","WTX", & - "WV ","WWA","WY ","DX ","PE ","NB "/ - - if(nwords.lt.4 .or. nwords.gt.5) return - call chkcall(w(1),bcall_1,ok1) - call chkcall(w(2),bcall_2,ok2) - if(.not.ok1 .or. .not.ok2) return - isec=-1 - do i=1,NSEC - if(csec(i).eq.w(nwords)(1:3)) then - isec=i - exit - endif - enddo - if(isec.eq.-1) return - if(nwords.eq.5 .and. trim(w(3)).ne.'R') return - - ntx=-1 - j=len(trim(w(nwords-1)))-1 - read(w(nwords-1)(1:j),*,err=1,end=1) ntx !Number of transmitters -1 if(ntx.lt.1 .or. ntx.gt.32) return - nclass=ichar(w(nwords-1)(j+1:j+1))-ichar('A') - - m=len(trim(w(nwords))) !Length of section abbreviation - if(m.lt.2 .or. m.gt.3) return - -! 0.3 WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 ARRL Field Day -! 0.4 WA9XYZ KA1ABC R 32A EMA 28 28 1 4 3 7 71 ARRL Field Day - - i3=0 - n3=3 !Type 0.3 ARRL Field Day - intx=ntx-1 - if(intx.ge.16) then - n3=4 !Type 0.4 ARRL Field Day - intx=ntx-17 - endif - call pack28(w(1),n28a) - call pack28(w(2),n28b) - ir=0 - if(w(3)(1:2).eq.'R ') ir=1 - write(c77,1010) n28a,n28b,ir,intx,nclass,isec,n3,i3 -1010 format(2b28.28,b1,b4.4,b3.3,b7.7,2b3.3) - - return -end subroutine pack77_03 - - -subroutine pack77_06(nwords,w,i3,n3,c77,i3_hint,n3_hint) - - character*13 w(19) - character*77 c77 - character*6 bcall,grid6 - character*4 grid4 - character*1 c - character*36 a2 - data a2/'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/,nzzz/46656/ - - logical is_grid4,is_grid6,is_digit,ok - is_grid4(grid4)=len(trim(grid4)).eq.4 .and. & - grid4(1:1).ge.'A' .and. grid4(1:1).le.'R' .and. & - grid4(2:2).ge.'A' .and. grid4(2:2).le.'R' .and. & - grid4(3:3).ge.'0' .and. grid4(3:3).le.'9' .and. & - grid4(4:4).ge.'0' .and. grid4(4:4).le.'9' - - is_grid6(grid6)=(len(trim(grid6)).eq.6.or.len(trim(grid6)).eq.4).and. & - grid6(1:1).ge.'A' .and. grid6(1:1).le.'R' .and. & - grid6(2:2).ge.'A' .and. grid6(2:2).le.'R' .and. & - grid6(3:3).ge.'0' .and. grid6(3:3).le.'9' .and. & - grid6(4:4).ge.'0' .and. grid6(4:4).le.'9' .and. & - (len(trim(grid6)).eq.4.or. & - (grid6(5:5).ge.'A' .and. grid6(5:5).le.'X' .and. & - grid6(6:6).ge.'A' .and. grid6(6:6).le.'X')) - - is_digit(c)=c.ge.'0' .and. c.le.'9' - - m1=len(trim(w(1))) - m2=len(trim(w(2))) - m3=len(trim(w(3))) - if(nwords.eq.3 .and. m1.ge.3 .and. m1.le.6 .and. m2.eq.4 .and. m3.le.2) then -! WSPR Type 1 - if(.not.is_grid4(w(2)(1:4))) go to 900 - if(.not.is_digit(w(3)(1:1))) go to 900 - if(m3.eq.2) then - if(.not.is_digit(w(3)(2:2))) go to 900 - endif - i3=0 - n3=6 - call pack28(w(1),n28) - grid4=w(2)(1:4) - k1=(ichar(grid4(1:1))-ichar('A'))*18*10*10 - k2=(ichar(grid4(2:2))-ichar('A'))*10*10 - k3=(ichar(grid4(3:3))-ichar('0'))*10 - k4=(ichar(grid4(4:4))-ichar('0')) - igrid4=k1+k2+k3+k4 - read(w(3),*) idbm - if(idbm.lt.0) idbm=0 - if(idbm.gt.60) idbm=60 - idbm=nint(0.3*idbm) - write(c77,1010) n28,igrid4,idbm,0,0,0,n3,i3 -1010 format(b28.28,b15.15,b5.5,2i1,b21.21,2b3.3) - go to 900 - endif - if(nwords.eq.2 .and. m1.ge.5 .and. m1.le.10 .and. m2.le.2) then -! WSPR Type 2 - i1=index(w(1),'/') - if(i1.lt.2 .or. i1.eq.m1) go to 900 - if(.not.is_digit(w(2)(1:1))) go to 900 - if(i1.eq.(m1-3) .and. .not.is_digit(w(1)(m1:m1))) go to 900 - if(m2.eq.2) then - if(.not.is_digit(w(2)(2:2))) go to 900 - endif - call chkcall(w(1),bcall,ok) - if(.not.ok) go to 900 - if(i1.le.4) then -! We have a prefix - npfx=index(a2,w(1)(1:1))-1 - if(i1.ge.3) npfx=36*npfx + index(a2,w(1)(2:2))-1 - if(i1.eq.4) npfx=36*npfx + index(a2,w(1)(3:3))-1 - else -! We have a suffix - if((m1-i1).eq.1) npfx=index(a2,w(1)(i1+1:i1+1))-1 - if((m1-i1).eq.2) npfx=36*(index(a2,w(1)(i1+1:i1+1))-1) + & - index(a2,w(1)(i1+2:i1+2))-1 - if((m1-i1).eq.3) then -! Third character of a suffix must be a digit - if(.not.is_digit(w(1)(i1+3:i1+3))) go to 900 - npfx=36*10*(index(a2,w(1)(i1+1:i1+1))-1) + & - 10*(index(a2,w(1)(i1+2:i1+2))-1) + index(a2,w(1)(i1+3:i1+3))-1 - endif - npfx=npfx + nzzz - endif - i3=0 - n3=6 - call pack28(bcall//' ',n28) - read(w(2),*) idbm - if(idbm.lt.0) idbm=0 - if(idbm.gt.60) idbm=60 - idbm=nint(0.3*idbm) - write(c77,1020) n28,npfx,idbm,1,0,n3,i3 -1020 format(b28.28,b16.16,b5.5,i1,b21.21,2b3.3) - go to 900 - endif - - if(i3_hint.eq.0.and.n3_hint.eq.6.and.nwords.eq.2 .and. m1.ge.5 & - .and. m1.le.12 .and. m2.le.6) then -! WSPR Type 3 - - !n3_hint=6 and i3_hint=0 is a hint that the caller wanted a - !50-bit encoding rather than the possible alternative n3=4 77-bit - !encoding - if(index(w(1),'<').lt.1 .or. index(w(1),'>').lt.1) go to 900 - grid6=w(2)(1:6) - if(.not.is_grid6(grid6)) go to 900 - i3=0 - n3=6 - call pack28(w(1),n28) - n22=n28-2063592 - k1=(ichar(grid6(1:1))-ichar('A'))*18*10*10*25*25 - k2=(ichar(grid6(2:2))-ichar('A'))*10*10*25*25 - k3=(ichar(grid6(3:3))-ichar('0'))*10*25*25 - k4=(ichar(grid6(4:4))-ichar('0'))*25*25 - if (grid6(5:6).eq.' ') then - igrid6=k1+k2+k3+k4+24*25+24 - else - k5=(ichar(grid6(5:5))-ichar('A'))*25 - k6=(ichar(grid6(6:6))-ichar('A')) - igrid6=k1+k2+k3+k4+k5+k6 - endif - write(c77,1030) n22,igrid6,2,0,n3,i3 -1030 format(b22.22,b25.25,b3.3,b21.21,2b3.3) - endif - -900 return -end subroutine pack77_06 - - -subroutine pack77_1(nwords,w,i3,n3,c77) - -! Check Type 1 (Standard 77-bit message) and Type 2 (ditto, with a "/P" call) -! Example message: WA9XYZ/R KA1ABC/R R FN42 28 1 28 1 1 15 74 - - parameter (MAXGRID4=32400) - character*13 w(19),c13 - character*77 c77 - character*6 bcall_1,bcall_2 - character*4 grid4 - character c1*1,c2*2 - logical is_grid4 - logical ok1,ok2 - is_grid4(grid4)=len(trim(grid4)).eq.4 .and. & - grid4(1:1).ge.'A' .and. grid4(1:1).le.'R' .and. & - grid4(2:2).ge.'A' .and. grid4(2:2).le.'R' .and. & - grid4(3:3).ge.'0' .and. grid4(3:3).le.'9' .and. & - grid4(4:4).ge.'0' .and. grid4(4:4).le.'9' - - if(nwords.lt.2 .or. nwords.gt.4) return - call chkcall(w(1),bcall_1,ok1) - call chkcall(w(2),bcall_2,ok2) - if(w(1)(1:3).eq.'DE ' .or. w(1)(1:3).eq.'CQ_' .or. w(1)(1:3).eq.'CQ ' .or. & - w(1)(1:4).eq.'QRZ ') ok1=.true. - if(w(1)(1:1).eq.'<' .and. index(w(1),'>').ge.5) ok1=.true. - if(w(2)(1:1).eq.'<' .and. index(w(2),'>').ge.5) ok2=.true. - if(.not.ok1 .or. .not.ok2) return - if(w(1)(1:1).eq.'<' .and. index(w(2),'/').gt.0) return - if(w(2)(1:1).eq.'<' .and. index(w(1),'/').gt.0) return - if(nwords.eq.2 .and. (.not.ok2 .or. index(w(2),'/').ge.2)) return - if(nwords.eq.2) go to 10 - - c1=w(nwords)(1:1) - c2=w(nwords)(1:2) - if(.not.is_grid4(w(nwords)(1:4)) .and. c1.ne.'+' .and. c1.ne.'-' & - .and. c2.ne.'R+' .and. c2.ne.'R-' .and. trim(w(nwords)).ne.'RRR' .and. & - trim(w(nwords)).ne.'RR73' .and. trim(w(nwords)).ne.'73') return - if(c1.eq.'+' .or. c1.eq.'-') then - ir=0 - read(w(nwords),*,err=900) irpt - if(irpt.ge.-50 .and. irpt.le.-31) irpt=irpt+101 - irpt=irpt+35 - else if(c2.eq.'R+' .or. c2.eq.'R-') then - ir=1 - read(w(nwords)(2:),*,err=900) irpt - if(irpt.ge.-50 .and. irpt.le.-31) irpt=irpt+101 - irpt=irpt+35 - else if(trim(w(nwords)).eq.'RRR') then - ir=0 - irpt=2 - else if(trim(w(nwords)).eq.'RR73') then - ir=0 - irpt=3 - else if(trim(w(nwords)).eq.'73') then - ir=0 - irpt=4 - endif - -! 1 WA9XYZ/R KA1ABC/R R FN42 28 1 28 1 1 15 74 Standard msg -! 2 PA3XYZ/P GM4ABC/P R JO22 28 1 28 1 1 15 74 EU VHF contest - -10 i1psuffix=index(w(1)//' ' ,'/P ') - i2psuffix=index(w(2)//' ','/P ') - if(nwords.eq.2 .or. nwords.eq.3 .or. (nwords.eq.4 .and. & - w(3)(1:2).eq.'R ')) then - n3=0 - i3=1 !Type 1: Standard message, possibly with "/R" - if (i1psuffix.ge.4.or.i2psuffix.ge.4) i3=2 !Type 2, with "/P" - endif - c13=bcall_1 - if(c13(1:3).eq.'CQ_' .or. w(1)(1:1).eq.'<') c13=w(1) - call pack28(c13,n28a) - c13=bcall_2 - if(w(2)(1:1).eq.'<') c13=w(2) - call pack28(c13,n28b) - ipa=0 - ipb=0 - if(i1psuffix.ge.4.or.index(w(1)//' ','/R ').ge.4) ipa=1 - if(i2psuffix.ge.4.or.index(w(2)//' ','/R ').ge.4) ipb=1 - - grid4=w(nwords)(1:4) - if(is_grid4(grid4)) then - ir=0 - if(w(3).eq.'R ') ir=1 - j1=(ichar(grid4(1:1))-ichar('A'))*18*10*10 - j2=(ichar(grid4(2:2))-ichar('A'))*10*10 - j3=(ichar(grid4(3:3))-ichar('0'))*10 - j4=(ichar(grid4(4:4))-ichar('0')) - igrid4=j1+j2+j3+j4 - else - igrid4=MAXGRID4 + irpt - endif - if(nwords.eq.2) then - ir=0 - irpt=1 - igrid4=MAXGRID4+irpt - endif - write(c77,1000) n28a,ipa,n28b,ipb,ir,igrid4,i3 -1000 format(2(b28.28,b1),b1,b15.15,b3.3) - return - -900 return -end subroutine pack77_1 - - -subroutine pack77_3(nwords,w,i3,n3,c77) - -! Check Type 3 (ARRL RTTY contest exchange) -! ARRL RTTY - US/Can: rpt state/prov R 579 MA -! - DX: rpt serial R 559 0013 -! Example message: TU; W9XYZ K1ABC R 579 MA 1 28 28 1 3 13 74 - - parameter (NUSCAN=171) !Number of US states and Canadian provinces/territories - character*13 w(19) - character*77 c77 - character*6 bcall_1,bcall_2 - character*3 cmult(NUSCAN),mult - character crpt*3 - logical ok1,ok2 - data cmult/ & - "AL ","AK ","AZ ","AR ","CA ","CO ","CT ","DE ","FL ","GA ", & - "HI ","ID ","IL ","IN ","IA ","KS ","KY ","LA ","ME ","MD ", & - "MA ","MI ","MN ","MS ","MO ","MT ","NE ","NV ","NH ","NJ ", & - "NM ","NY ","NC ","ND ","OH ","OK ","OR ","PA ","RI ","SC ", & - "SD ","TN ","TX ","UT ","VT ","VA ","WA ","WV ","WI ","WY ", & - "NB ","NS ","QC ","ON ","MB ","SK ","AB ","BC ","NWT","NF ", & - "LB ","NU ","YT ","PEI","DC ","DR ","FR ","GD ","GR ","OV ", & - "ZH ","ZL ","X01","X02","X03","X04","X05","X06","X07","X08", & - "X09","X10","X11","X12","X13","X14","X15","X16","X17","X18", & - "X19","X20","X21","X22","X23","X24","X25","X26","X27","X28", & - "X29","X30","X31","X32","X33","X34","X35","X36","X37","X38", & - "X39","X40","X41","X42","X43","X44","X45","X46","X47","X48", & - "X49","X50","X51","X52","X53","X54","X55","X56","X57","X58", & - "X59","X60","X61","X62","X63","X64","X65","X66","X67","X68", & - "X69","X70","X71","X72","X73","X74","X75","X76","X77","X78", & - "X79","X80","X81","X82","X83","X84","X85","X86","X87","X88", & - "X89","X90","X91","X92","X93","X94","X95","X96","X97","X98", & - "X99"/ - - if(w(1)(1:1).eq.'<' .and. w(2)(1:1).eq.'<') go to 900 - if(nwords.eq.4 .or. nwords.eq.5 .or. nwords.eq.6) then - i1=1 - if(trim(w(1)).eq.'TU;') i1=2 - call chkcall(w(i1),bcall_1,ok1) - call chkcall(w(i1+1),bcall_2,ok2) - if(.not.ok1 .or. .not.ok2) go to 900 - crpt=w(nwords-1)(1:3) - if(index(crpt,'-').ge.1 .or. index(crpt,'+').ge.1) go to 900 - if(crpt(1:1).eq.'5' .and. crpt(2:2).ge.'2' .and. crpt(2:2).le.'9' .and. & - crpt(3:3).eq.'9') then - nserial=0 - read(w(nwords),*,err=1) nserial - endif -1 mult=' ' - imult=-1 - do i=1,NUSCAN - if(cmult(i).eq.w(nwords)) then - imult=i - mult=cmult(i) - exit - endif + do i=1,nwords + nw(i)=len(trim(w(i))) enddo - nexch=0 - if(nserial.gt.0) nexch=nserial - if(imult.gt.0) nexch=8000+imult - if(mult.ne.' ' .or. nserial.gt.0) then - i3=3 - n3=0 - itu=0 - if(trim(w(1)).eq.'TU;') itu=1 - call pack28(w(1+itu),n28a) - call pack28(w(2+itu),n28b) - ir=0 - if(w(3+itu)(1:2).eq.'R ') ir=1 - read(w(3+itu+ir),*,err=900) irpt - irpt=(irpt-509)/10 - 2 - if(irpt.lt.0) irpt=0 - if(irpt.gt.7) irpt=7 -! 3 TU; W9XYZ K1ABC R 579 MA 1 28 28 1 3 13 74 ARRL RTTY contest -! 3 TU; W9XYZ G8ABC R 559 0013 1 28 28 1 3 13 74 ARRL RTTY (DX) - write(c77,1010) itu,n28a,n28b,ir,irpt,nexch,i3 -1010 format(b1,2b28.28,b1,b3.3,b13.13,b3.3) - endif endif 900 return -end subroutine pack77_3 - - -subroutine pack77_4(nwords,w,i3,n3,c77) - -! Check Type 4 (One nonstandard call and one hashed call) -! Example message: <WA9XYZ> PJ4/KA1ABC RR73 12 58 1 2 1 74 - - integer*8 n58 - logical ok1,ok2 - character*13 w(19) - character*77 c77 - character*13 call_1,call_2 - character*11 c11 - character*6 bcall_1,bcall_2 - character*38 c - data c/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/'/ - - iflip=0 - i3=-1 - if(nwords.eq.2 .or. nwords.eq.3) then - call_1=w(1) - if(call_1(1:1).eq.'<') call_1=w(1)(2:len(trim(w(1)))-1) - call_2=w(2) - if(call_2(1:1).eq.'<') call_2=w(2)(2:len(trim(w(2)))-1) - call chkcall(call_1,bcall_1,ok1) - call chkcall(call_2,bcall_2,ok2) - if(call_1.eq.bcall_1 .and. call_2.eq.bcall_2 .and. ok1 .and. ok2) go to 900 - icq=0 - if(trim(w(1)).eq.'CQ' .or. (ok1.and.ok2)) then - if(trim(w(1)).eq.'CQ' .and. len(trim(w(2))).le.4) go to 900 - i3=4 - n3=0 - if(trim(w(1)).eq.'CQ') icq=1 - endif - - if(icq.eq.1) then - iflip=0 - n12=0 - c11=adjustr(call_2(1:11)) - call save_hash_call(w(2),n10,n12,n22) - else if(w(1)(1:1).eq.'<') then - iflip=0 - i3=4 - call save_hash_call(w(1),n10,n12,n22) - c11=adjustr(call_2(1:11)) - else if(w(2)(1:1).eq.'<') then - iflip=1 - i3=4 - call save_hash_call(w(2),n10,n12,n22) - c11=adjustr(call_1(1:11)) - endif - n58=0 - do i=1,11 - n58=n58*38 + index(c,c11(i:i)) - 1 - enddo - nrpt=0 - if(trim(w(3)).eq.'RRR') nrpt=1 - if(trim(w(3)).eq.'RR73') nrpt=2 - if(trim(w(3)).eq.'73') nrpt=3 - if(icq.eq.1) then - iflip=0 - nrpt=0 - endif - write(c77,1010) n12,n58,iflip,nrpt,icq,i3 -1010 format(b12.12,b58.58,b1,b2.2,b1,b3.3) - do i=1,77 - if(c77(i:i).eq.'*') c77(i:i)='0' !### Clean up any illegal chars ### - enddo - endif - -900 return -end subroutine pack77_4 - -subroutine pack77_5(nwords,w,i3,n3,c77) - -! Pack a Type 0.2 message: EU VHF Contest mode -! Example message: PA3XYZ/P R 590003 IO91NP 28 1 1 3 12 25 -! <PA3XYZ> <G4ABC/P> R 590003 IO91NP h10 h20 r1 s3 s12 g25 - - character*13 w(19),c13 - character*77 c77 - character*6 grid6 - logical is_grid6 - - is_grid6(grid6)=len(trim(grid6)).eq.6 .and. & - grid6(1:1).ge.'A' .and. grid6(1:1).le.'R' .and. & - grid6(2:2).ge.'A' .and. grid6(2:2).le.'R' .and. & - grid6(3:3).ge.'0' .and. grid6(3:3).le.'9' .and. & - grid6(4:4).ge.'0' .and. grid6(4:4).le.'9' .and. & - grid6(5:5).ge.'A' .and. grid6(5:5).le.'X' .and. & - grid6(6:6).ge.'A' .and. grid6(6:6).le.'X' - - if(nwords.lt.4 .or. nwords.gt.5) return !nwords must be 4 or 5 - if(w(1)(1:1).ne.'<' .or. w(2)(1:1).ne.'<') return !Both calls must be hashed - nx=-1 - read(w(nwords-1),*,err=2) nx -2 if(nx.lt.520001 .or. nx.gt.594095) return !Exchange between 520001 - 594095 - if(.not.is_grid6(w(nwords)(1:6))) return !Last word must be a valid grid6 - -! Type 0.2: <PA3XYZ> <G4ABC/P> R 590003 IO91NP h10 h20 r1 s3 s12 g25 - - i3=5 - n3=0 - - call save_hash_call(w(1),n10,n12,n22) - i2=index(w(1),'>') - c13=w(1)(2:i2-1) - n12=ihashcall(c13,12) - - call save_hash_call(w(2),n10a,n12a,n22) - i2=index(w(2),'>') - c13=w(2)(2:i2-1) - n22=ihashcall(c13,22) - - ir=0 - if(w(3)(1:2).eq.'R ') ir=1 - irpt=nx/10000 - 52 - iserial=mod(nx,10000) - if(iserial.gt.2047) iserial=2047 - grid6=w(nwords)(1:6) - j1=(ichar(grid6(1:1))-ichar('A'))*18*10*10*24*24 - j2=(ichar(grid6(2:2))-ichar('A'))*10*10*24*24 - j3=(ichar(grid6(3:3))-ichar('0'))*10*24*24 - j4=(ichar(grid6(4:4))-ichar('0'))*24*24 - j5=(ichar(grid6(5:5))-ichar('A'))*24 - j6=(ichar(grid6(6:6))-ichar('A')) - igrid6=j1+j2+j3+j4+j5+j6 - - write(c77,1010) n12,n22,ir,irpt,iserial,igrid6,i3 -1010 format(b12.12,b22.22,b1,b3.3,b11.11,b25.25,b3.3) - - return -end subroutine pack77_5 +end subroutine split77 subroutine packtext77(c13,c71) @@ -1531,14 +1201,13 @@ subroutine unpacktext77(c71,c13) return end subroutine unpacktext77 -subroutine mp_short_ops(w,u) +subroutine mp_short_init character*1 w(*),u(*) integer i,ireg,j,n,ir,iv,ii1,ii2 character*1 creg(4) save ii1,ii2 equivalence (ireg,creg) - entry mp_short_init ireg=256*ichar('2')+ichar('1') do j=1,4 if (creg(j).eq.'1') ii1=j @@ -1572,9 +1241,9 @@ subroutine mp_short_ops(w,u) ir=mod(i,iv) enddo return - + return -end subroutine mp_short_ops +end subroutine mp_short_init subroutine add_call_to_recent_calls(callsign) @@ -1594,1814 +1263,1175 @@ subroutine add_call_to_recent_calls(callsign) recent_calls(1)=callsign endif -! Make sure that callsign is hashed -! call save_hash_call(callsign,n10,n12,n22) ! commented out - do this in the calling routine: +! Recent-call ordering is separate from hash storage; decode paths stage both +! effects explicitly when a call should become recent and hash-resolvable. return end subroutine add_call_to_recent_calls -subroutine to_grid4(n,grid4,ok) - character*4 grid4 - logical ok - ok=.false. - j1=n/(18*10*10) - if (j1.lt.0.or.j1.gt.17) goto 900 - n=n-j1*18*10*10 - j2=n/(10*10) - if (j2.lt.0.or.j2.gt.17) goto 900 - n=n-j2*10*10 - j3=n/10 - if (j3.lt.0.or.j3.gt.9) goto 900 - j4=n-j3*10 - if (j4.lt.0.or.j4.gt.9) goto 900 - grid4(1:1)=char(j1+ichar('A')) - grid4(2:2)=char(j2+ichar('A')) - grid4(3:3)=char(j3+ichar('0')) - grid4(4:4)=char(j4+ichar('0')) - ok=.true. - -900 return -end subroutine to_grid4 - -subroutine to_grid6(n,grid6,ok) - character*6 grid6 - logical ok - - ok=.false. - j1=n/(18*10*10*24*24) - if (j1.lt.0.or.j1.gt.17) goto 900 - n=n-j1*18*10*10*24*24 - j2=n/(10*10*24*24) - if (j2.lt.0.or.j2.gt.17) goto 900 - n=n-j2*10*10*24*24 - j3=n/(10*24*24) - if (j3.lt.0.or.j3.gt.9) goto 900 - n=n-j3*10*24*24 - j4=n/(24*24) - if (j4.lt.0.or.j4.gt.9) goto 900 - n=n-j4*24*24 - j5=n/24 - if (j5.lt.0.or.j5.gt.23) goto 900 - j6=n-j5*24 - if (j6.lt.0.or.j6.gt.23) goto 900 - grid6(1:1)=char(j1+ichar('A')) - grid6(2:2)=char(j2+ichar('A')) - grid6(3:3)=char(j3+ichar('0')) - grid6(4:4)=char(j4+ichar('0')) - grid6(5:5)=char(j5+ichar('A')) - grid6(6:6)=char(j6+ichar('A')) - ok=.true. - -900 return -end subroutine to_grid6 - -subroutine to_grid(n,grid6,ok) - ! 4-, or 6-character grid - character*6 grid6 - logical ok - - ok=.false. - j1=n/(18*10*10*25*25) - if (j1.lt.0.or.j1.gt.17) goto 900 - n=n-j1*18*10*10*25*25 - j2=n/(10*10*25*25) - if (j2.lt.0.or.j2.gt.17) goto 900 - n=n-j2*10*10*25*25 - j3=n/(10*25*25) - if (j3.lt.0.or.j3.gt.9) goto 900 - n=n-j3*10*25*25 - j4=n/(25*25) - if (j4.lt.0.or.j4.gt.9) goto 900 - n=n-j4*25*25 - j5=n/25 - if (j5.lt.0.or.j5.gt.24) goto 900 - j6=n-j5*25 - if (j6.lt.0.or.j6.gt.24) goto 900 - grid6='' - grid6(1:1)=char(j1+ichar('A')) - grid6(2:2)=char(j2+ichar('A')) - grid6(3:3)=char(j3+ichar('0')) - grid6(4:4)=char(j4+ichar('0')) - if (j5.ne.24.or.j6.ne.24) then - grid6(5:5)=char(j5+ichar('A')) - grid6(6:6)=char(j6+ichar('A')) - endif - ok=.true. - -900 return -end subroutine to_grid - - -subroutine hash10var(n10,c13,nthr) +subroutine queue_hash_call_for_thread(c13,nthr) character*13 c13 integer, intent(in) :: nthr - c13='<...>' - if(n10.lt.0 .or. n10.gt.1023) return - if(nthr.gt.25) then ! TX message - if(len(trim(txcalls10var(n10))).gt.0) then - c13=txcalls10var(n10) - c13='<'//trim(c13)//'>' - endif - else - if(len(trim(calls10var(n10))).gt.0) then - c13=calls10var(n10) - c13='<'//trim(c13)//'>' - endif - endif - return - -end subroutine hash10var +! Configured/var decode workers queue hash facts; fillhashvar later folds them +! into the shared calls10/calls12/calls22 tables. + call queue_call_for_thread(c13,nthr,queued_calls_by_thread, & + nqueued_calls_by_thread) -subroutine hash12var(n12,c13,nthr) - - character*13 c13 - integer, intent(in) :: nthr - - c13='<...>' - if(n12.lt.0 .or. n12.gt.4095) return - if(nthr.gt.25) then ! TX message - if(len(trim(txcalls12var(n12))).gt.0) then - c13=txcalls12var(n12) - c13='<'//trim(c13)//'>' - endif - else - if(len(trim(calls12var(n12))).gt.0) then - c13=calls12var(n12) - c13='<'//trim(c13)//'>' - endif - endif return +end subroutine queue_hash_call_for_thread -end subroutine hash12var - -subroutine hash22var(n22,c13,nthr) - +subroutine queue_recent_call_for_thread(c13,nthr) character*13 c13 integer, intent(in) :: nthr - - c13='<...>' - if(nthr.gt.25) then ! TX message - do i=1,nztxhashvar - if(itxhash22var(i).eq.n22) then - c13=txcalls22var(i) - c13='<'//trim(c13)//'>' - go to 900 - endif - enddo - else - do i=1,nzhashvar - if(ihash22var(i).eq.n22) then - c13=calls22var(i) - c13='<'//trim(c13)//'>' - go to 900 - endif - enddo - endif - -900 return -end subroutine hash22var -integer function ihashcallvar(c0,m) - - integer*8 n8 - character*13 c0 - character*38 c - data c/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/'/ - - n8=0 - do i=1,11 - j=index(c,c0(i:i)) - 1 - n8=38*n8 + j - enddo - ihashcallvar=ishft(47055833459_8*n8,m-64) + call queue_call_for_thread(c13,nthr,queued_recent_calls_by_thread, & + nqueued_recent_calls_by_thread) return -end function ihashcallvar - -subroutine save_hash_mycallvar(c13,n10,n12,n22) +end subroutine queue_recent_call_for_thread - character*13 c13,cw +subroutine queue_call_for_thread(c13,nthr,queued_calls,nqueued_calls) + character(len=*), intent(in) :: c13 + integer, intent(in) :: nthr + character(len=13), intent(inout) :: queued_calls(:) + integer, intent(inout) :: nqueued_calls(:) + character*13 cw - cw=c13 + if(nthr.lt.1 .or. nthr.gt.24) return + cw=c13 if(cw(1:1).eq.' ' .or. cw(1:5).eq.'<...>') return if(cw(1:1).eq.'<') cw=cw(2:) i=index(cw,'>') if(i.gt.0) cw(i:)=' ' if(len(trim(cw)) .lt. 3) return - n12=ihashcallvar(cw,12) - if(n12.ge.0 .and. n12 .le. 4095 .and. cw.ne.mycall13var) calls12var(n12)=cw + nposition=thread_call_index(nthr)+nqueued_calls(nthr) + if(nposition.lt.thread_call_index(nthr+1)) then + nqueued_calls(nthr)=nqueued_calls(nthr)+1 + queued_calls(nposition+1)=cw + endif - n10=ihashcallvar(cw,10) - if(n10.ge.0 .and. n10 .le. 1023 .and. cw.ne.mycall13var) calls10var(n10)=cw + return +end subroutine queue_call_for_thread - n22=ihashcallvar(cw,22) - if(any(ihash22var.eq.n22)) then ! If entry exists, make sure callsign is the most recently received one - where(ihash22var.eq.n22) calls22var=cw - go to 1900 - endif +subroutine fold_queued_recent_calls(numthreads) -! New entry: move table down, making room for new one at the top - ihash22var(nzhashvar:2:-1)=ihash22var(nzhashvar-1:1:-1) + integer, intent(in) :: numthreads + character*13 cw -! Add the new entry - calls22var(nzhashvar:2:-1)=calls22var(nzhashvar-1:1:-1) - ihash22var(1)=n22 - calls22var(1)=cw - if(nzhashvar.lt.MAXHASHvar) nzhashvar=nzhashvar+1 + do i=1,numthreads + do m=1,nqueued_recent_calls_by_thread(i) + nposition=thread_call_index(i)+m + cw=queued_recent_calls_by_thread(nposition) + call add_call_to_recent_calls(cw) + enddo + enddo -1900 continue + return +end subroutine fold_queued_recent_calls - return -end subroutine save_hash_mycallvar +subroutine sync_configured_calls_for_decode_start() -subroutine save_hash_txcallvar(c13,n10,n12,n22,lhashit) +! Capture the operator-configured calls at decode-cycle boundaries. These +! hashes let configured decodes resolve hash-only messages without a separate +! var hash table. + if(mycall13.ne.mycall13_configured_prev) then + if(len(trim(mycall13)).gt.2) then + call set_configured_mycall_from_current() + else + call clear_configured_mycall() + endif + endif + call sync_configured_mycall_value_from_current() - character*13 c13,cw - logical, intent(in) :: lhashit + if(dxcall13.ne.dxcall13_configured_prev) then + if(len(trim(dxcall13)).gt.2) then + call set_configured_dxcall_from_current() + else + call clear_configured_dxcall() + endif + endif + call sync_configured_dxcall_value_from_current() - cw=c13 - if(cw(1:1).eq.' ' .or. cw(1:5).eq.'<...>') return - if(cw(1:1).eq.'<') cw=cw(2:) - i=index(cw,'>') - if(i.gt.0) cw(i:)=' ' - - if(len(trim(cw)) .lt. 3) return + return +end subroutine sync_configured_calls_for_decode_start - n12=ihashcallvar(cw,12) +subroutine set_configured_mycall_from_current() - if(lhashit) then - if(n12.ge.0 .and. n12 .le. 4095 .and. cw.ne.mycall13var) txcalls12var(n12)=cw + mycall13_configured_set=.true. + mycall13_configured=mycall13 + mycall13_configured_prev=mycall13 + hashmy10_configured=ihashcall(mycall13,10) + hashmy12_configured=ihashcall(mycall13,12) + hashmy22_configured=ihashcall(mycall13,22) + call save_hash_call(mycall13,ndum10,ndum12,ndum22) - n10=ihashcallvar(cw,10) - if(n10.ge.0 .and. n10 .le. 1023 .and. cw.ne.mycall13var) txcalls10var(n10)=cw + return +end subroutine set_configured_mycall_from_current - n22=ihashcallvar(cw,22) - if(any(itxhash22var.eq.n22)) then ! If entry exists, make sure callsign is the most recently received one - where(itxhash22var.eq.n22) txcalls22var=cw - go to 1900 - endif +subroutine clear_configured_mycall() -! New entry: move table down, making room for new one at the top - itxhash22var(nztxhashvar:2:-1)=itxhash22var(nztxhashvar-1:1:-1) + mycall13_configured_set=.false. + mycall13_configured=' ' + mycall13_configured_prev=' ' + hashmy10_configured=-1 + hashmy12_configured=-1 + hashmy22_configured=-1 -! Add the new entry - txcalls22var(nztxhashvar:2:-1)=txcalls22var(nztxhashvar-1:1:-1) - itxhash22var(1)=n22 - txcalls22var(1)=cw - if(nztxhashvar.lt.MAXTXHASHvar) nztxhashvar=nztxhashvar+1 + return +end subroutine clear_configured_mycall -1900 continue - endif +subroutine sync_configured_mycall_value_from_current() - return -end subroutine save_hash_txcallvar + if(mycall13_configured_set) mycall13_configured=mycall13 -subroutine save_hash_callvar(c13,nthr) + return +end subroutine sync_configured_mycall_value_from_current - character*13 c13,cw - integer, intent(in) :: nthr +subroutine set_configured_dxcall_from_current() - if(nthr.gt.24) return - cw=c13 - if(cw(1:1).eq.' ' .or. cw(1:5).eq.'<...>') return - if(cw(1:1).eq.'<') cw=cw(2:) - i=index(cw,'>') - if(i.gt.0) cw(i:)=' ' - if(len(trim(cw)) .lt. 3) return + dxcall13_configured_set=.true. + dxcall13_configured=dxcall13 + dxcall13_configured_prev=dxcall13 + hashdx10_configured=ihashcall(dxcall13,10) + ! Seed the manual DX Call before decode so first-cycle hash references to it + ! can resolve even if the call has not yet been decoded over the air. + call queue_hash_call_for_thread(dxcall13,1) - nposition=nthrindexvar(nthr)+nlast_callsvar(nthr) - if(nposition.lt.nthrindexvar(nthr+1)) then - nlast_callsvar(nthr)=nlast_callsvar(nthr)+1 - last_callsvar(nposition+1)=cw - endif + return +end subroutine set_configured_dxcall_from_current - return -end subroutine save_hash_callvar +subroutine clear_configured_dxcall() -subroutine pack77var(msg0,i3,n3,c77,ntxhash) + dxcall13_configured_set=.false. + dxcall13_configured=' ' + dxcall13_configured_prev=' ' + hashdx10_configured=-1 - use packjt - character*37 msg,msg0 - character*18 c18 - character*13 w(19) - character*77 c77 - integer nw(19) - integer ntel(3) - integer, intent(in) :: ntxhash + return +end subroutine clear_configured_dxcall - msg=msg0 - i3_hint=i3 - n3_hint=n3 - i3=-1 - n3=-1 - if(i3_hint.eq.0 .and. n3_hint.eq.5) go to 5 +subroutine sync_configured_dxcall_value_from_current() -! Convert msg to upper case; collapse multiple blanks; parse into words. - call split77var(msg,nwords,nw,w) - if(msg(1:3).eq.'CQ ' .or. msg(1:3).eq.'DE ' .or. msg(1:4).eq.'QRZ ') go to 100 + if(dxcall13_configured_set) dxcall13_configured=dxcall13 -! Check 0.1 (DXpedition mode) - call pack77_01var(nwords,w,i3,n3,c77,ntxhash) - if(i3.ge.0 .or. n3.ge.1) go to 900 -! Check 0.2 (EU VHF contest exchange) -! call pack77_02(nwords,w,i3,n3,c77) -! if(i3.ge.0) go to 900 + return +end subroutine sync_configured_dxcall_value_from_current -! Check 0.3 and 0.4 (ARRL Field Day exchange) - call pack77_03var(nwords,w,i3,n3,c77,ntxhash) - if(i3.ge.0) go to 900 - if(nwords.ge.2) go to 100 +type(configured_decode_state) function current_configured_decode_state() & + result(state) - ! Check 0.5 (telemetry) -5 i0=index(msg,' ') - c18=msg(1:i0-1) - c18=adjustr(c18) - ntel=-99 - read(c18,1005,err=6) ntel -1005 format(3z6) - if(ntel(1).ge.2**23) go to 800 -6 if(ntel(1).ge.0 .and. ntel(2).ge.0 .and. ntel(3).ge.0) then - i3=0 - n3=5 - write(c77,1006) ntel,n3,i3 -1006 format(b23.23,2b24.24,2b3.3) - go to 900 - endif + state%mycall_set=mycall13_configured_set + state%dxcall_set=dxcall13_configured_set + state%mycall=mycall13_configured + state%dxcall=dxcall13_configured + state%hashmy10=hashmy10_configured + state%hashmy12=hashmy12_configured + state%hashmy22=hashmy22_configured + state%hashdx10=hashdx10_configured + + return +end function current_configured_decode_state + +type(unpack77_context) function configured_unpack77_context(nrx) result(context) + integer, intent(in) :: nrx + type(configured_decode_state) :: state + + state=current_configured_decode_state() + context%nrx=nrx + context%configured=.true. + context%strict_var_guards=.true. + context%mycall_set=state%mycall_set + context%dxcall_set=state%dxcall_set + context%mycall=state%mycall + context%dxcall=state%dxcall + context%hashmy10=state%hashmy10 + context%hashmy12=state%hashmy12 + context%hashmy22=state%hashmy22 + context%hashdx10=state%hashdx10 +end function configured_unpack77_context + +type(unpack77_effect_policy) function configured_unpack77_effect_policy(options) & + result(policy) + type(unpack77_options), intent(in) :: options + + policy=unpack77_effect_policy(queue_by_thread=.true., & + nthr_hash=options%thread_index,nthr_recent=options%thread_index) + policy%record_hashes=options%record_hashes + policy%record_recent_calls=options%record_recent_calls +end function configured_unpack77_effect_policy + +subroutine unpack77_configured(c77,nrx,msg,unpk77_successvar,options) + character*77 c77 + character*37 msg + integer, intent(in) :: nrx + logical unpk77_successvar + type(unpack77_options), intent(in) :: options + type(unpack77_context) :: context + type(unpack77_effect_policy) :: policy -100 call pack77_06var(nwords,w,i3,n3,c77,i3_hint,n3_hint,ntxhash) - if(i3.ge.0) go to 900 + context=configured_unpack77_context(nrx) + policy=configured_unpack77_effect_policy(options) + call run_unpack77(c77,context,policy,msg,unpk77_successvar) -! Check Type 1 (Standard 77-bit message) or Type 2, with optional "/P" - call pack77_1var(nwords,w,i3,n3,c77,ntxhash) - if(i3.ge.0) go to 900 + return +end subroutine unpack77_configured -! Check Type 3 (ARRL RTTY contest exchange) - call pack77_3var(nwords,w,i3,n3,c77,ntxhash) - if(i3.ge.0) go to 900 +subroutine run_unpack77(c77,context,policy,msg,unpk77_success) + character*77 c77 + type(unpack77_context), intent(in) :: context + type(unpack77_effect_policy), intent(in) :: policy + character*37 msg + logical unpk77_success + type(unpack77_core_result) :: decoded -! Check Type 4 (One nonstandard call and one hashed call) - call pack77_4var(nwords,w,i3,n3,c77,ntxhash) - if(i3.ge.0) go to 900 + decoded=unpack77_core(c77,context) + msg=decoded%msg + unpk77_success=decoded%success + if(unpk77_success) call apply_unpack77_effects(decoded%effects,policy) -! Check Type 5 (EU VHF Contest with 2 hashed calls, report, serial, and grid6) - call pack77_5var(nwords,w,i3,n3,c77,ntxhash) - if(i3.ge.0) go to 900 + return +end subroutine run_unpack77 -! It defaults to free text -800 i3=0 - n3=0 - msg(14:)=' ' - call packtext77var(msg(1:13),c77(1:71)) - write(c77(72:77),'(2b3.3)') n3,i3 +type(unpack77_core_result) function unpack77_core(c77,context) result(decoded) -900 return -end subroutine pack77var + character*77 c77 + type(unpack77_context), intent(in) :: context + logical ok -subroutine unpack77var(c77,nrx,msg,unpk77_successvar,nthr) -! -! nrx=1 when unpacking a received message -! nrx=0 when unpacking a to-be-transmitted message -! the value of nrx is used to decide when mycall13var or dxcall13var should -! be used in place of a callsign from the hashtable -! - parameter (NSEC=86) !Number of ARRL Sections - parameter (NUSCAN=171) !Number of States and Provinces - parameter (MAXGRID4=32400) - integer*8 n58 - integer ntel(3) + decoded=unpack77_core_result() + decoded%msg=repeat(' ',37) + call decode_pack77_tag(c77,decoded%i3,decoded%n3,ok) + if(.not.ok) then + decoded%msg='failed unpack' + return + endif + decoded%success=.true. + + if(decoded%i3.eq.0) then + call unpack77_decode_i3_0(c77,context,decoded) + else if(decoded%i3.eq.1 .or. decoded%i3.eq.2) then + call unpack77_decode_type12(c77,context,decoded) + else if(decoded%i3.eq.3) then + call unpack77_decode_type3(c77,context,decoded) + else if(decoded%i3.eq.4) then + call unpack77_decode_type4(c77,context,decoded) + else if(decoded%i3.eq.5) then + call unpack77_decode_type5(c77,context,decoded) + else if(decoded%i3.ge.6) then + decoded%success=.false. + endif +! CQ addressed to a hashed/nonstandard call is not a legal message form in +! any type; preserved verbatim from the legacy decoder. + if(decoded%msg(1:4).eq.'CQ <') decoded%success=.false. + + return +end function unpack77_core + +subroutine unpack77_decode_i3_0(c77,context,decoded) character*77 c77 - character*37 msg + type(unpack77_context), intent(in) :: context + type(unpack77_core_result), intent(inout) :: decoded + integer ntel(3) character*13 call_1,call_2,call_3,call_1a - character*11 c11 character*3 crpt,cntx,cpfx - character*3 cmult(NUSCAN) - character*6 cexch,grid6 - character*4 grid4,cserial - character*3 csec(NSEC) - character*38 c - character*36 a2 - logical unpk28_success,unpk77_successvar,unpkg4_success - integer, intent(in) :: nthr - - data a2/'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/,nzzz/46656/ - data c/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/'/ - data csec/ & - "AB ","AK ","AL ","AR ","AZ ","BC ","CO ","CT ","DE ","EB ", & - "EMA","ENY","EPA","EWA","GA ","GH ","IA ","ID ","IL ","IN ", & - "KS ","KY ","LA ","LAX","NS ","MB ","MDC","ME ","MI ","MN ", & - "MO ","MS ","MT ","NC ","ND ","NE ","NFL","NH ","NL ","NLI", & - "NM ","NNJ","NNY","TER","NTX","NV ","OH ","OK ","ONE","ONN", & - "ONS","OR ","ORG","PAC","PR ","QC ","RI ","SB ","SC ","SCV", & - "SD ","SDG","SF ","SFL","SJV","SK ","SNJ","STX","SV ","TN ", & - "UT ","VA ","VI ","VT ","WCF","WI ","WMA","WNY","WPA","WTX", & - "WV ","WWA","WY ","DX ","PE ","NB "/ - data cmult/ & - "AL ","AK ","AZ ","AR ","CA ","CO ","CT ","DE ","FL ","GA ", & - "HI ","ID ","IL ","IN ","IA ","KS ","KY ","LA ","ME ","MD ", & - "MA ","MI ","MN ","MS ","MO ","MT ","NE ","NV ","NH ","NJ ", & - "NM ","NY ","NC ","ND ","OH ","OK ","OR ","PA ","RI ","SC ", & - "SD ","TN ","TX ","UT ","VT ","VA ","WA ","WV ","WI ","WY ", & - "NB ","NS ","QC ","ON ","MB ","SK ","AB ","BC ","NWT","NF ", & - "LB ","NU ","YT ","PEI","DC ","DR ","FR ","GD ","GR ","OV ", & - "ZH ","ZL ","X01","X02","X03","X04","X05","X06","X07","X08", & - "X09","X10","X11","X12","X13","X14","X15","X16","X17","X18", & - "X19","X20","X21","X22","X23","X24","X25","X26","X27","X28", & - "X29","X30","X31","X32","X33","X34","X35","X36","X37","X38", & - "X39","X40","X41","X42","X43","X44","X45","X46","X47","X48", & - "X49","X50","X51","X52","X53","X54","X55","X56","X57","X58", & - "X59","X60","X61","X62","X63","X64","X65","X66","X67","X68", & - "X69","X70","X71","X72","X73","X74","X75","X76","X77","X78", & - "X79","X80","X81","X82","X83","X84","X85","X86","X87","X88", & - "X89","X90","X91","X92","X93","X94","X95","X96","X97","X98", & - "X99"/ - - unpk77_successvar=.true. - -! Check for bad data - do i=1,77 - if(c77(i:i).ne.'0' .and. c77(i:i).ne.'1') then - msg='failed unpack' - unpk77_successvar=.false. + character*6 grid6 + character*4 grid4 + logical unpk28_success,unpkg4_success,ok,is_prefix + type(pack77_free_text_fields) :: free_text_fields + type(pack77_telemetry_fields) :: telemetry_fields + type(pack77_dxpedition_fields) :: dxpedition_fields + type(pack77_field_day_fields) :: field_day_fields + type(pack77_wspr_type1_fields) :: wspr1_fields + type(pack77_wspr_type2_fields) :: wspr2_fields + type(pack77_wspr_type3_fields) :: wspr3_fields + integer :: n28a,n28b + + if(decoded%n3.eq.0) then +! 0.0 Free text + call decode_pack77_free_text(c77,free_text_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. return endif - enddo + call unpacktext77(free_text_fields%text_bits,decoded%msg(1:13)) + decoded%msg(14:)=' ' + decoded%msg=adjustl(decoded%msg) + if(decoded%msg(1:1).eq.' ') decoded%success=.false. - read(c77(72:77),'(2b3)') n3,i3 - msg=repeat(' ',37) - if(i3.eq.0 .and. n3.eq.0) then -! 0.0 Free text - call unpacktext77var(c77(1:71),msg(1:13)) - msg(14:)=' ' - msg=adjustl(msg) - if(msg(1:1).eq.' ') then - unpk77_successvar=.false. + else if(decoded%n3.eq.1) then +! 0.1 K1ABC RR73; W9XYZ <KH1/KH7Z> -11 28 28 10 5 71 DXpedition Mode + call decode_pack77_dxpedition(c77,dxpedition_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. return endif - else if(i3.eq.0 .and. n3.eq.1) then -! 0.1 K1ABC RR73; W9XYZ <KH1/KH7Z> -11 28 28 10 5 71 DXpedition Mode - read(c77,1010) n28avar,n28bvar,n10,n5 -1010 format(2b28,b10,b5) + n28a=dxpedition_fields%n28a + n28b=dxpedition_fields%n28b + n10=dxpedition_fields%n10 + n5=dxpedition_fields%n5 + call record_configured_n28_pair(context,n28a,n28b) irpt=2*n5 - 30 - write(crpt,1012) irpt -1012 format(i3.2) - if(irpt.ge.0) crpt(1:1)='+' - call unpack28var(n28avar,call_1,unpk28_success,nthr) - if(.not.unpk28_success .or. n28avar.le.2) unpk77_successvar=.false. - call unpack28var(n28bvar,call_2,unpk28_success,nthr) - if(.not.unpk28_success .or. n28bvar.le.2) unpk77_successvar=.false. - call hash10var(n10,call_3,nthr) - if(call_3.eq.'<...>') then - call hash10(n10,call_3) - endif - - if(nrx.eq.1) then - if(dxcall13_setvar .and. len(trim(dxcall13var)).ge.3 .and. hashdx10var.eq.n10) then; call_3='<'//trim(dxcall13var)//'>' - elseif(.not.dxcall13_setvar .and. len(trim(dxcall13var)).ge.3 .and. .not.lcommonft8b .and. hashdx10var.eq.n10) then - call_3='<'//trim(dxcall13_0var)//'>' - elseif(.not.dxcall13_setvar .and. len(trim(dxcall13var)).ge.3 .and. .not.lcommonft8b .and. hashdx10var.ne.n10) then - call_3=trim(call_3) - elseif(.not.dxcall13_setvar .and. len(trim(dxcall13var)).ge.3 .and. lcommonft8b .and. hashdx10var.eq.n10) then - call_3='<'//trim(dxcall13_0var)//'>' - elseif(.not.dxcall13_setvar .and. len(trim(dxcall13var)).ge.3 .and. lcommonft8b .and. hashdx10var.ne.n10) then - call_3='<...>' - endif + crpt=pack77_format_snr_report(irpt) + call unpack28_for_context(context,n28a,call_1,unpk28_success) + if(.not.unpk28_success .or. n28a.le.2) decoded%success=.false. + call unpack28_for_context(context,n28b,call_2,unpk28_success) + if(.not.unpk28_success .or. n28b.le.2) decoded%success=.false. + call hash10(n10,call_3) + if(context%nrx.eq.1) then + if(context%dxcall_set .and. len(trim(context%dxcall)).ge.3 .and. & + context%hashdx10.eq.n10) then + call_3='<'//trim(context%dxcall)//'>' + endif endif - if(nrx.eq.0 .and. mycall13_setvar .and. n10.eq.hashmy10var) call_3='<'//trim(mycall13var)//'>' - msg=trim(call_1)//' RR73; '//trim(call_2)//' '//trim(call_3)//' '//crpt + if(context%nrx.eq.0 .and. context%mycall_set .and. & + n10.eq.context%hashmy10) call_3='<'//trim(context%mycall)//'>' + decoded%msg=trim(call_1)//' RR73; '//trim(call_2)//' '//trim(call_3)//' '//crpt + + else if(decoded%n3.eq.2) then + decoded%success=.false. - else if(i3.eq.0 .and. n3.eq.2) then - unpk77_successvar=.false. - - else if(i3.eq.0 .and. (n3.eq.3 .or. n3.eq.4)) then + else if(decoded%n3.eq.3 .or. decoded%n3.eq.4) then ! 0.3 WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 ARRL Field Day ! 0.4 WA9XYZ KA1ABC R 32A EMA 28 28 1 4 3 7 71 ARRL Field Day - read(c77,1030) n28avar,n28bvar,ir,intx,nclass,isec -1030 format(2b28,b1,b4,b3,b7) - if(isec.gt.NSEC .or. isec.lt.1) then - unpk77_successvar=.false. + if(decoded%n3.eq.3) then + call decode_pack77_field_day_low(c77,field_day_fields,ok,.true.) + else + call decode_pack77_field_day_high(c77,field_day_fields,ok,.true.) + endif + if(.not.ok) then + decoded%success=.false. + return + endif + n28a=field_day_fields%n28a + n28b=field_day_fields%n28b + ir=field_day_fields%ir + intx=field_day_fields%intx + nclass=field_day_fields%nclass + isec=field_day_fields%isec + call record_configured_n28_pair(context,n28a,n28b) + if(isec.gt.PACK77_NSEC .or. isec.lt.1) then + decoded%success=.false. isec=1 endif - call unpack28var(n28avar,call_1,unpk28_success,nthr) - if(.not.unpk28_success .or. n28avar.le.2) unpk77_successvar=.false. - call unpack28var(n28bvar,call_2,unpk28_success,nthr) - if(.not.unpk28_success .or. n28bvar.le.2) unpk77_successvar=.false. + call unpack28_for_context(context,n28a,call_1,unpk28_success) + if(.not.unpk28_success .or. n28a.le.2) decoded%success=.false. + call unpack28_for_context(context,n28b,call_2,unpk28_success) + if(.not.unpk28_success .or. n28b.le.2) decoded%success=.false. ntx=intx+1 - if(n3.eq.4) ntx=ntx+16 - write(cntx(1:2),1032) ntx -1032 format(i2) + if(decoded%n3.eq.4) ntx=ntx+16 + write(cntx(1:2),'(i2)') ntx cntx(3:3)=char(ichar('A')+nclass) - if(ir.eq.0 .and. ntx.lt.10) msg=trim(call_1)//' '//trim(call_2)// & - cntx//' '//csec(isec) - if(ir.eq.1 .and. ntx.lt.10) msg=trim(call_1)//' '//trim(call_2)// & - ' R'//cntx//' '//csec(isec) - if(ir.eq.0 .and. ntx.ge.10) msg=trim(call_1)//' '//trim(call_2)// & - ' '//cntx//' '//csec(isec) - if(ir.eq.1 .and. ntx.ge.10) msg=trim(call_1)//' '//trim(call_2)// & - ' R '//cntx//' '//csec(isec) - - else if(i3.eq.0 .and. n3.eq.5) then + decoded%msg=pack77_format_exchange_message(call_1,call_2,ir, & + trim(adjustl(cntx))//' '//pack77_arrl_section_name(isec),.true.) + + else if(decoded%n3.eq.5) then ! 0.5 0123456789abcdef01 71 71 Telemetry (18 hex) - read(c77,1006) ntel -1006 format(b23,2b24) - write(msg,1007) ntel + call decode_pack77_telemetry(c77,telemetry_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. + return + endif + ntel=(/telemetry_fields%ntel1,telemetry_fields%ntel2,telemetry_fields%ntel3/) + write(decoded%msg,1007) ntel 1007 format(3z6.6) - do i=1,18 - if(msg(i:i).ne.'0') exit - msg(i:i)=' ' - enddo - msg=adjustl(msg) - - else if(i3.eq.0 .and. n3.eq.6) then - read(c77(49:50),'(2b1)') j2a,j2b - itype=2 - if(j2b.eq.0 .and. j2a.eq.0) itype=1 - if(j2b.eq.0 .and. j2a.eq.1) itype=3 - if(itype.eq.1) then -! WSPR Type 1 - read(c77,2010) n28,igrid4,idbm -2010 format(b28.28,b15.15,b5.5) - idbm=nint(idbm*10.0/3.0) - call unpack28var(n28,call_1,unpk28_success,nthr) - if(.not.unpk28_success) unpk77_successvar=.false. - call to_grid4var(igrid4,grid4,unpkg4_success) - if(.not.unpkg4_success) unpk77_successvar=.false. - write(crpt,'(i3)') idbm - msg=trim(call_1)//' '//grid4//' '//trim(adjustl(crpt)) - if (unpk77_successvar) call save_hash_callvar(call_1,nthr) !### Is this OK here? ### - else if(itype.eq.2) then + else if(decoded%n3.eq.6) then + select case(pack77_wspr_payload_type(c77)) + case(2) + call decode_pack77_wspr_type2(c77,wspr2_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. + return + endif ! WSPR Type 2 - read(c77,2020) n28,npfx,idbm -2020 format(b28.28,b16.16,b5.5) - idbm=nint(idbm*10.0/3.0) - call unpack28var(n28,call_1,unpk28_success,nthr) - if(.not.unpk28_success) unpk77_successvar=.false. + n28=wspr2_fields%n28 + npfx=wspr2_fields%npfx + idbm=nint(wspr2_fields%idbm*10.0/3.0) + call unpack28_for_context(context,n28,call_1,unpk28_success) + if(.not.unpk28_success) decoded%success=.false. write(crpt,'(i3)') idbm - cpfx=' ' - if(npfx.lt.nzzz) then -! Prefix - do i=3,1,-1 - j=mod(npfx,36)+1 - cpfx(i:i)=a2(j:j) - npfx=npfx/36 - if(npfx.eq.0) exit - enddo - msg=trim(adjustl(cpfx))//'/'//trim(call_1)//' '//trim(adjustl(crpt)) + call pack77_wspr_affix_text(npfx,cpfx,is_prefix,ok) + if(.not.ok) then + decoded%success=.false. + return + endif + if(is_prefix) then call_1a=trim(adjustl(cpfx))//'/'//trim(call_1) - call save_hash_callvar(call_1a,nthr) !### Is this OK here? ### else -! Suffix - npfx=npfx-nzzz - if(npfx.le.35) then - cpfx(1:1)=a2(npfx+1:npfx+1) - else if(npfx.gt.35 .and. npfx.le.1295) then - cpfx(1:1)=a2(npfx/36+1:npfx/36+1) - cpfx(2:2)=a2(mod(npfx,36)+1:mod(npfx,36)+1) - else if(npfx.gt.1295 .and. npfx.le.12959) then - cpfx(1:1)=a2(npfx/360+1:npfx/360+1) - cpfx(2:2)=a2(mod(npfx/10,36)+1:mod(npfx/10,36)+1) - cpfx(3:3)=a2(mod(npfx,10)+1:mod(npfx,10)+1) - else - unpk77_successvar=.false. - return - endif - msg=trim(call_1)//'/'//trim(adjustl(cpfx))//' '//trim(adjustl(crpt)) call_1a=trim(call_1)//'/'//trim(adjustl(cpfx)) - call save_hash_callvar(call_1a,nthr) !### Is this OK here? ### endif - - else if(itype.eq.3) then -! WSPR Type 3 - read(c77,2030) n22,igrid6 -2030 format(b22.22,b25.25) - n28=n22+2063592 - call unpack28var(n28,call_1,unpk28_success,nthr) - if(.not.unpk28_success) unpk77_successvar=.false. - call to_gridvar(igrid6,grid6,unpkg4_success) - if(.not.unpkg4_success) unpk77_successvar=.false. - msg=trim(call_1)//' '//grid6 - endif - - else if(i3.eq.0 .and. n3.gt.6) then - unpk77_successvar=.false. - - else if(i3.eq.1 .or. i3.eq.2) then -! Type 1 (standard message) or Type 2 ("/P" form for EU VHF contest) - read(c77,1000) n28avar,ipa,n28bvar,ipb,ir,igrid4,i3 -1000 format(2(b28,b1),b1,b15,b3) - call unpack28var(n28avar,call_1,unpk28_success,nthr) - if(nrx.eq.1 .and. mycall13_setvar .and. hashmy22var.eq.(n28avar-2063592)) then - call_1='<'//trim(mycall13var)//'>' - unpk28_success=.true. - endif - if(.not.unpk28_success) unpk77_successvar=.false. - call unpack28var(n28bvar,call_2,unpk28_success,nthr) - if(.not.unpk28_success) unpk77_successvar=.false. - if(call_1(1:3).eq.'CQ_') call_1(3:3)=' ' - if(index(call_1,'<').le.0) then - i=index(call_1,' ') - if(i.ge.4 .and. ipa.eq.1 .and. i3.eq.1) call_1(i:i+1)='/R' - if(i.ge.4 .and. ipa.eq.1 .and. i3.eq.2) call_1(i:i+1)='/P' - if(i.ge.4) call add_call_to_recent_callsvar(call_1,nthr) - endif - if(index(call_2,'<').le.0) then - i=index(call_2,' ') - if(i.ge.4 .and. ipb.eq.1 .and. i3.eq.1) call_2(i:i+1)='/R' - if(i.ge.4 .and. ipb.eq.1 .and. i3.eq.2) call_2(i:i+1)='/P' - if(i.ge.4) then - call add_call_to_recent_callsvar(call_2,nthr) -! only hash the "from" call - call save_hash_callvar(call_2,nthr) - endif - endif - if(igrid4.le.MAXGRID4) then - call to_grid4var(igrid4,grid4,unpkg4_success) - if(.not.unpkg4_success) unpk77_successvar=.false. - if(ir.eq.0) msg=trim(call_1)//' '//trim(call_2)//' '//grid4 - if(ir.eq.1) msg=trim(call_1)//' '//trim(call_2)//' R '//grid4 - if(msg(1:3).eq.'CQ ' .and. ir.eq.1) unpk77_successvar=.false. -!out of current realisation of protocol: GRID is always dropped at WSJT-X/JTDX transmission of the messages below -! i3 n3 -! 1 0 -!110015 -23 0.5 1450 ~ <...> Y96IHQ/R GL12 -!112215 -23 0.0 1606 ~ <...> 3U1TBM/R CC65 -!000102 -23 0.5 801 ~ <...> NV3QHV/R JF62 - if(ir.eq.0 .and. call_1(1:1).eq.'<' .and. grid4.ne.'RR73') then ! hash value can also be associated with a valid callsign - nlencall2=len_trim(call_2) - if(nlencall2.gt.1) then - if(call_2(nlencall2-1:nlencall2).eq.'/R') unpk77_successvar=.false. - endif + decoded%msg=trim(call_1a)//' '//trim(adjustl(crpt)) + call stage_unpack_hash_call(decoded%effects,call_1a) + + case(1) + call decode_pack77_wspr_type1(c77,wspr1_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. + return endif - else - irpt=igrid4-MAXGRID4 - if(irpt.eq.1) msg=trim(call_1)//' '//trim(call_2) - if(irpt.eq.2) msg=trim(call_1)//' '//trim(call_2)//' RRR' - if(irpt.eq.3) msg=trim(call_1)//' '//trim(call_2)//' RR73' - if(irpt.eq.4) msg=trim(call_1)//' '//trim(call_2)//' 73' - if(irpt.ge.5) then - isnr=irpt-35 - if(isnr.gt.50) isnr=isnr-101 - write(crpt,'(i3.2)') isnr - if(crpt(1:1).eq.' ') crpt(1:1)='+' - if(ir.eq.0) msg=trim(call_1)//' '//trim(call_2)//' '//crpt - if(ir.eq.1) msg=trim(call_1)//' '//trim(call_2)//' R'//crpt +! WSPR Type 1 + n28=wspr1_fields%n28 + igrid4=wspr1_fields%igrid4 + idbm=nint(wspr1_fields%idbm*10.0/3.0) + call unpack28_for_context(context,n28,call_1,unpk28_success) + if(.not.unpk28_success) decoded%success=.false. + call to_grid4(igrid4,grid4,unpkg4_success) + if(.not.unpkg4_success) decoded%success=.false. + write(crpt,'(i3)') idbm + decoded%msg=trim(call_1)//' '//grid4//' '//trim(adjustl(crpt)) + call stage_unpack_hash_call(decoded%effects,call_1) + + case(3) + call decode_pack77_wspr_type3(c77,wspr3_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. + return endif - if(msg(1:3).eq.'CQ ' .and. irpt.ge.2) unpk77_successvar=.false. - endif +! WSPR Type 3 + n22=wspr3_fields%n22 + igrid6=wspr3_fields%igrid6 + n28=n22+PACK77_NTOKENS + call unpack28_for_context(context,n28,call_1,unpk28_success) + if(.not.unpk28_success) decoded%success=.false. + call to_grid(igrid6,grid6,unpkg4_success) + if(.not.unpkg4_success) decoded%success=.false. + decoded%msg=trim(call_1)//' '//grid6 - else if(i3.eq.3) then -! Type 3: ARRL RTTY Contest - read(c77,1040) itu,n28avar,n28bvar,ir,irpt,nexch,i3 -1040 format(b1,2b28.28,b1,b3.3,b13.13,b3.3) - write(crpt,1042) irpt+2 -1042 format('5',i1,'9') - nserial=nexch - imult=-1 - if(nexch.gt.8000) then - imult=nexch-8000 - nserial=-1 - endif - call unpack28var(n28avar,call_1,unpk28_success,nthr) - if(.not.unpk28_success) unpk77_successvar=.false. - call unpack28var(n28bvar,call_2,unpk28_success,nthr) - if(.not.unpk28_success) unpk77_successvar=.false. - imult=0 - nserial=0 - if(nexch.gt.8000) imult=nexch-8000 - if(nexch.lt.8000) nserial=nexch - - if(imult.ge.1 .and.imult.le.NUSCAN) then - if(itu.eq.0 .and. ir.eq.0) msg=trim(call_1)//' '//trim(call_2)// & - ' '//crpt//' '//cmult(imult) - if(itu.eq.1 .and. ir.eq.0) msg='TU; '//trim(call_1)//' '//trim(call_2)// & - ' '//crpt//' '//cmult(imult) - if(itu.eq.0 .and. ir.eq.1) msg=trim(call_1)//' '//trim(call_2)// & - ' R '//crpt//' '//cmult(imult) - if(itu.eq.1 .and. ir.eq.1) msg='TU; '//trim(call_1)//' '//trim(call_2)// & - ' R '//crpt//' '//cmult(imult) - else if(nserial.ge.1 .and. nserial.le.7999) then - write(cserial,'(i4.4)') nserial - if(itu.eq.0 .and. ir.eq.0) msg=trim(call_1)//' '//trim(call_2)// & - ' '//crpt//' '//cserial - if(itu.eq.1 .and. ir.eq.0) msg='TU; '//trim(call_1)//' '//trim(call_2)// & - ' '//crpt//' '//cserial - if(itu.eq.0 .and. ir.eq.1) msg=trim(call_1)//' '//trim(call_2)// & - ' R '//crpt//' '//cserial - if(itu.eq.1 .and. ir.eq.1) msg='TU; '//trim(call_1)//' '//trim(call_2)// & - ' R '//crpt//' '//cserial - endif + case default + decoded%success=.false. + return + end select - else if(i3.eq.4) then -! Type 4 - read(c77,1050) n12,n58,iflip,nrpt,icq -1050 format(b12,b58,b1,b2,b1) - do i=11,1,-1 - j=mod(n58,38)+1 - c11(i:i)=c(j:j) - n58=n58/38 - enddo - call hash12var(n12,call_3,nthr) - if(call_3.eq.'<...>') then - call hash12(n12,call_3) - endif - if(iflip.eq.0) then ! 12 bit hash for TO call - call_1=call_3 - call_2=adjustl(c11)//' ' - call add_call_to_recent_callsvar(call_2,nthr) - if(nrx.eq.1 .and. & - dxcall13_setvar .and. mycall13_setvar .and. & - call_2.eq.dxcall13var .and. & - n12.eq.hashmy12var ) call_1='<'//trim(mycall13var)//'>' - if(nrx.eq.1 .and. & - mycall13_setvar .and. & - index(call_1,'<...>').gt.0 .and. & - n12.eq.hashmy12var ) call_1='<'//trim(mycall13var)//'>' - else ! 12 bit hash for DE call - call_1=adjustl(c11) - call_2=call_3 - call add_call_to_recent_callsvar(call_1,nthr) - if(nrx.eq.0 .and. & - mycall13_setvar .and. & - n12.eq.hashmy12var) call_2='<'//trim(mycall13var)//'>' - endif - if(icq.eq.0) then - if(nrpt.eq.0) msg=trim(call_1)//' '//trim(call_2) - if(nrpt.eq.1) msg=trim(call_1)//' '//trim(call_2)//' RRR' - if(nrpt.eq.2) msg=trim(call_1)//' '//trim(call_2)//' RR73' - if(nrpt.eq.3) msg=trim(call_1)//' '//trim(call_2)//' 73' - else - msg='CQ '//trim(call_2) - endif - if(call_2(1:1).lt.':' .and. call_2(1:1).gt.'/' .and. call_2(2:2).lt.':' .and. call_2(2:2).gt.'/') unpk77_successvar=.false. - if(len_trim(call_2).eq.11) then - ispace=index(call_2,' '); if(ispace.gt.0 .and. ispace.lt.12) unpk77_successvar=.false. - islash=index(call_2,'/'); if(islash.eq.1 .or. islash.eq.2 .or. islash.eq.11 .or. (islash.eq.10 .and. & - call_2(11:11).gt.'@' .and. call_2(11:11).lt.'[' .and. call_2(11:11).ne.'P')) unpk77_successvar=.false. - if(islash.lt.6 .and. call_2(11:11).lt.':' .and. call_2(11:11).gt.'/') unpk77_successvar=.false. - endif - if(msg(1:3).ne.'CQ ') then - if(call_1(1:1).lt.':' .and. call_1(1:1).gt.'/' .and. call_1(2:2).lt.':' .and.call_1(2:2).gt.'/') unpk77_successvar=.false. - if(len_trim(call_1).eq.11) then - ispace=index(call_1,' '); if(ispace.gt.0 .and. ispace.lt.12) unpk77_successvar=.false. - islash=index(call_1,'/'); if(islash.eq.1 .or. islash.eq.2 .or. islash.eq.11 .or. (islash.eq.10 .and. & - call_1(11:11).gt.'@' .and. call_1(11:11).lt.'[' .and. call_1(11:11).ne.'P')) unpk77_successvar=.false. - if(islash.lt.6 .and. call_1(11:11).lt.':' .and. call_1(11:11).gt.'/') unpk77_successvar=.false. - endif - endif - nmsglen=len_trim(msg) -! protocol violations, also reported at wrong TX message packing -! prevent broken message transmission - if(unpk77_successvar .and. nmsglen.gt.0) then - indxp=index(msg,'/P ') - if((icq.eq.0 .and. nrpt.eq.0 .and. (indxp.lt.1 .or. indxp.gt.7)) .or. icq.eq.1) then -! -23 -3.3 N0S/W45ETOE <...> -!071445 -14 0.1 779 ~ <...> -!071445 -14 0.1 779 ~ W5JZ ! hash was associated -! CQ <...> -! with exception to broken by packing message 'JQ1AHT/P <JL1LOF/1> -15' wrongly being transmitted as type 4 message 'JQ1AHT/P <JL1LOF/1>' - if(msg(nmsglen:nmsglen).eq.'>') unpk77_successvar=.false. - endif + else if(decoded%n3.gt.6) then + decoded%success=.false. + endif + + return +end subroutine unpack77_decode_i3_0 + +subroutine unpack77_decode_type12(c77,context,decoded) + character*77 c77 + type(unpack77_context), intent(in) :: context + type(unpack77_core_result), intent(inout) :: decoded + character*13 call_1,call_2 + character*3 crpt + character*4 grid4 + logical unpk28_success,unpkg4_success,ok + type(pack77_type12_fields) :: type12_fields + integer :: n28a,n28b + ! Type 1 (standard message) or Type 2 ("/P" form for EU VHF contest) + if(decoded%i3.eq.1) then + call decode_pack77_type1(c77,type12_fields,ok,.true.) + else + call decode_pack77_type2(c77,type12_fields,ok,.true.) + endif + if(.not.ok) then + decoded%success=.false. + return + endif + n28a=type12_fields%n28a + n28b=type12_fields%n28b + ipa=type12_fields%ipa + ipb=type12_fields%ipb + ir=type12_fields%ir + igrid4=type12_fields%igrid4 + call record_configured_n28_pair(context,n28a,n28b) + call unpack28_for_context(context,n28a,call_1,unpk28_success) + if(context%nrx.eq.1 .and. context%mycall_set .and. & + context%hashmy22.eq.(n28a-PACK77_NTOKENS)) then + call_1='<'//trim(context%mycall)//'>' + unpk28_success=.true. + endif + if(.not.unpk28_success) decoded%success=.false. + call unpack28_for_context(context,n28b,call_2,unpk28_success) + if(.not.unpk28_success) decoded%success=.false. + if(call_1(1:3).eq.'CQ_') call_1(3:3)=' ' + if(index(call_1,'<').le.0) then + i=index(call_1,' ') + if(i.ge.4 .and. ipa.eq.1 .and. decoded%i3.eq.1) call_1(i:i+1)='/R' + if(i.ge.4 .and. ipa.eq.1 .and. decoded%i3.eq.2) call_1(i:i+1)='/P' + if(i.ge.4) then + call stage_unpack_recent_call(decoded%effects,call_1) + if(context%configured) call stage_unpack_hash_call(decoded%effects,call_1) endif -! <...> /BZZ/0ZZ/C -! <...> ZZZZ/0Z/Z/ -! <...> OZZZZ/0ZZZ/ -! <...> 0I/IZZZ/O K -! <...> J/IZZZ/O K - if(unpk77_successvar .and. iflip.eq.0 .and. icq.eq.0 .and. nrpt.eq.0) then - nlencall2=len_trim(call_2) - if(nlencall2.gt.9) then - if(call_2(1:1).eq.'/' .or. call_2(nlencall2:nlencall2).eq.'/') unpk77_successvar=.false. - nindxspace=index(call_2,' ') - if(nindxspace.gt.0 .and. nindxspace.lt.nlencall2) unpk77_successvar=.false. - endif + endif + if(index(call_2,'<').le.0) then + i=index(call_2,' ') + if(i.ge.4 .and. ipb.eq.1 .and. decoded%i3.eq.1) call_2(i:i+1)='/R' + if(i.ge.4 .and. ipb.eq.1 .and. decoded%i3.eq.2) call_2(i:i+1)='/P' + if(i.ge.4) then + call stage_unpack_recent_call(decoded%effects,call_2) + call stage_unpack_hash_call(decoded%effects,call_2) endif + endif + if(igrid4.le.PACK77_MAXGRID4) then + call to_grid4(igrid4,grid4,unpkg4_success) + if(.not.unpkg4_success) decoded%success=.false. + decoded%msg=pack77_format_exchange_message(call_1,call_2,ir,grid4,.true.) + if(decoded%msg(1:3).eq.'CQ ' .and. ir.eq.1) decoded%success=.false. + if(context%strict_var_guards .and. .not.unpack77_type12_var_guard_ok( & + call_1,call_2,grid4,ir)) decoded%success=.false. + else + irpt=igrid4-PACK77_MAXGRID4 + if(irpt.ge.1 .and. irpt.le.4) & + decoded%msg=pack77_format_qso_tail(call_1,call_2, & + pack77_tail_from_type12_irpt(irpt)) + if(irpt.ge.5) then + crpt=pack77_format_snr_report(pack77_snr_from_report_index(irpt)) + decoded%msg=pack77_format_exchange_message(call_1,call_2,ir,crpt,.false.) + endif + if(decoded%msg(1:3).eq.'CQ ' .and. irpt.ge.2) decoded%success=.false. + endif + + return +end subroutine unpack77_decode_type12 - else if(i3.eq.5) then - -! Type 5 <PA3XYZ> <G4ABC/P> R 590003 IO91NP h12 h22 r1 s3 S11 g25 -! EU VHF contest - read(c77,1060) n12,n22,ir,irpt,iserial,igrid6 -1060 format(b12,b22,b1,b3,b11,b25) - if(igrid6.lt.0 .or. igrid6.gt.18662399) then - unpk77_successvar=.false. +subroutine unpack77_decode_type3(c77,context,decoded) + character*77 c77 + type(unpack77_context), intent(in) :: context + type(unpack77_core_result), intent(inout) :: decoded + character*13 call_1,call_2 + character*4 exchange + character crpt*3 + logical unpk28_success,ok + type(pack77_type3_fields) :: type3_fields + integer :: n28a,n28b + ! Type 3: ARRL RTTY Contest + call decode_pack77_type3(c77,type3_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. + return + endif + itu=type3_fields%itu + n28a=type3_fields%n28a + n28b=type3_fields%n28b + ir=type3_fields%ir + irpt=type3_fields%irpt + nexch=type3_fields%nexch + call record_configured_n28_pair(context,n28a,n28b) + crpt=pack77_format_rtty_report(irpt) + call unpack28_for_context(context,n28a,call_1,unpk28_success) + if(.not.unpk28_success) decoded%success=.false. + call unpack28_for_context(context,n28b,call_2,unpk28_success) + if(.not.unpk28_success) decoded%success=.false. + if(nexch.gt.8000 .and. nexch-8000.le.PACK77_NUSCAN) then + exchange=pack77_rtty_multiplier_name(nexch-8000) + if(.not.pack77_type3_render_fits(itu,ir,call_1,call_2,exchange)) then + decoded%success=.false. return endif - call hash12var(n12,call_1,nthr) - if(call_1.eq.'<...>') then - call hash12(n12,call_1) - endif - if(n12.eq.hashmy12var) call_1='<'//trim(mycall13var)//'>' - call hash22var(n22,call_2,nthr) - if(call_2.eq.'<...>') then - call hash22(n22,call_2) + decoded%msg=pack77_format_type3_message(itu,ir,call_1,call_2,crpt, & + exchange) + else if(nexch.ge.1 .and. nexch.le.7999) then + write(exchange,'(i4.4)') nexch + if(.not.pack77_type3_render_fits(itu,ir,call_1,call_2,exchange)) then + decoded%success=.false. + return endif - nrs=52+irpt - write(cexch,1022) nrs,iserial -1022 format(i2,i4.4) - call to_grid6var(igrid6,grid6,unpk77_successvar) - if(ir.eq.0) msg=trim(call_1)//' '//trim(call_2)//' '//cexch//' '//grid6 - if(ir.eq.1) msg=trim(call_1)//' '//trim(call_2)//' R '//cexch//' '//grid6 + decoded%msg=pack77_format_type3_message(itu,ir,call_1,call_2,crpt,exchange) + else + decoded%success=.false. + endif - else if(i3.ge.6) then ! i3 values 6 and 7 are not yet defined - unpk77_successvar=.false. + return +end subroutine unpack77_decode_type3 + +subroutine unpack77_decode_type4(c77,context,decoded) + character*77 c77 + type(unpack77_context), intent(in) :: context + type(unpack77_core_result), intent(inout) :: decoded + character*13 call_1,call_2,call_3 + character*11 c11 + logical ok + type(pack77_type4_fields) :: type4_fields + ! Type 4 + call decode_pack77_type4(c77,type4_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. + return endif - if(msg(1:4).eq.'CQ <') unpk77_successvar=.false. + n12=type4_fields%n12 + c11=pack77_type4_call_text_from_n58(type4_fields%n58) + iflip=type4_fields%iflip + nrpt=type4_fields%nrpt + icq=type4_fields%icq + call hash12(n12,call_3) + if(iflip.eq.0) then ! 12 bit hash for TO call + call_1=call_3 + call_2=adjustl(c11)//' ' + call stage_unpack_recent_call(decoded%effects,call_2) + call stage_unpack_hash_call(decoded%effects,call_2) + if(context%nrx.eq.1 .and. context%dxcall_set .and. & + context%mycall_set .and. call_2.eq.context%dxcall .and. & + n12.eq.context%hashmy12) call_1='<'//trim(context%mycall)//'>' + if(context%nrx.eq.1 .and. context%mycall_set .and. & + index(call_1,'<...>').gt.0 .and. & + n12.eq.context%hashmy12) call_1='<'//trim(context%mycall)//'>' + else ! 12 bit hash for DE call + call_1=adjustl(c11) + call_2=call_3 + call stage_unpack_recent_call(decoded%effects,call_1) + if(context%configured) call stage_unpack_hash_call(decoded%effects,call_1) + if(context%nrx.eq.0 .and. context%mycall_set .and. & + n12.eq.context%hashmy12) call_2='<'//trim(context%mycall)//'>' + endif + if(icq.eq.0) then + decoded%msg=pack77_format_qso_tail(call_1,call_2, & + pack77_tail_from_type4_nrpt(nrpt)) + else + decoded%msg='CQ '//trim(call_2) + endif + if(context%strict_var_guards .and. .not.unpack77_type4_var_guard_ok( & + call_1,call_2,decoded%msg,iflip,icq,nrpt)) decoded%success=.false. + return -end subroutine unpack77var +end subroutine unpack77_decode_type4 -subroutine pack28var(c13,n28,ntxhash) +subroutine unpack77_decode_type5(c77,context,decoded) + character*77 c77 + type(unpack77_context), intent(in) :: context + type(unpack77_core_result), intent(inout) :: decoded + character*13 call_1,call_2 + character*6 cexch,grid6 + logical ok + type(pack77_type5_fields) :: type5_fields + ! Type 5 <PA3XYZ> <G4ABC/P> R 590003 IO91NP h12 h22 r1 s3 S11 g25 + ! EU VHF contest + call decode_pack77_type5(c77,type5_fields,ok,.true.) + if(.not.ok) then + decoded%success=.false. + return + endif + n12=type5_fields%n12 + n22=type5_fields%n22 + ir=type5_fields%ir + irpt=type5_fields%irpt + iserial=type5_fields%iserial + igrid6=type5_fields%igrid6 + call hash12(n12,call_1) + if(n12.eq.context%hashmy12) call_1='<'//trim(context%mycall)//'>' + call hash22(n22,call_2) + cexch=pack77_format_vhf_exchange(irpt,iserial) + call to_grid6(igrid6,grid6,decoded%success) + decoded%msg=pack77_format_exchange_message(call_1,call_2,ir,cexch//' '//grid6,.true.) -! Pack a special token, a 22-bit hash code, or a valid base call into a 28-bit -! integer. + return +end subroutine unpack77_decode_type5 - parameter (NTOKENS=2063592,MAX22=4194304) - logical is_digit,is_letter,lhashit - character*13 c13 - character*6 callsign - character*1 c - character*4 c4 - character*37 a1 - character*36 a2 - character*10 a3 - character*27 a4 - integer, intent(in) :: ntxhash - data a1/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/ - data a2/'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/ - data a3/'0123456789'/ - data a4/' ABCDEFGHIJKLMNOPQRSTUVWXYZ'/ - - is_digit(c)=c.ge.'0' .and. c.le.'9' - is_letter(c)=c.ge.'A' .and. c.le.'Z' +subroutine stage_unpack_hash_call(effects,c13) + type(unpack77_effects), intent(inout) :: effects + character(len=*), intent(in) :: c13 - n28=-1 -! Work-around for Swaziland prefix: - if(c13(1:4).eq.'3DA0') callsign='3D0'//c13(5:7) -! Work-around for Guinea prefixes: - if(c13(1:2).eq.'3X' .and. c13(3:3).ge.'A' .and. & - c13(3:3).le.'Z') callsign='Q'//c13(3:6) + call stage_unpack_call(effects%hash_calls,effects%nhash,c13) -! Check for special tokens first - if(c13(1:3).eq.'DE ') then - n28=0 - go to 900 - endif - - if(c13(1:4).eq.'QRZ ') then - n28=1 - go to 900 - endif + return +end subroutine stage_unpack_hash_call - if(c13(1:3).eq.'CQ ') then - n28=2 - go to 900 - endif +subroutine stage_unpack_recent_call(effects,c13) + type(unpack77_effects), intent(inout) :: effects + character(len=*), intent(in) :: c13 - if(c13(1:3).eq.'CQ_') then - n=len(trim(c13)) - if(n.ge.4 .and. n.le.7) then - nlet=0 - nnum=0 - do i=4,n - c=c13(i:i) - if(c.ge.'A' .and. c.le.'Z') nlet=nlet+1 - if(c.ge.'0' .and. c.le.'9') nnum=nnum+1 - enddo - if(nnum.eq.3 .and. nlet.eq.0) then - read(c13(4:3+nnum),*) nqsy - n28=3+nqsy - go to 900 + call stage_unpack_call(effects%recent_calls,effects%nrecent,c13) + + return +end subroutine stage_unpack_recent_call + +subroutine stage_unpack_call(calls,ncalls,c13) + character(len=13), intent(inout) :: calls(:) + integer, intent(inout) :: ncalls + character(len=*), intent(in) :: c13 + character(len=13) :: cw + logical :: ok + +! Keep unpack77_core side-effect free; callers decide whether staged calls are +! written immediately or queued for fillhashvar. + call normalize_hash_call(c13,cw,ok) + if(.not.ok) return + if(ncalls.ge.size(calls)) return + ncalls=ncalls+1 + calls(ncalls)=cw + + return +end subroutine stage_unpack_call + +subroutine apply_unpack77_effects(effects,policy) + type(unpack77_effects), intent(in) :: effects + type(unpack77_effect_policy), intent(in) :: policy + integer :: i,n10,n12,n22 + +! Non-configured decodes write directly to shared state. Configured/var decodes +! queue by RX thread and let fillhashvar merge the successful decode effects. + if(policy%record_hashes) then + do i=1,effects%nhash + if(policy%queue_by_thread) then + call queue_hash_call_for_thread(effects%hash_calls(i),policy%nthr_hash) + else + call save_hash_call(effects%hash_calls(i),n10,n12,n22) endif - if(nlet.ge.1 .and. nlet.le.4 .and. nnum.eq.0) then - c4=c13(4:n) - c4=adjustr(c4) - m=0 - do i=1,4 - j=0 - c=c4(i:i) - if(c.ge.'A' .and. c.le.'Z') j=ichar(c)-ichar('A')+1 - m=27*m + j - enddo - n28=3+1000+m - go to 900 + enddo + endif + if(policy%record_recent_calls) then + do i=1,effects%nrecent + if(policy%queue_by_thread) then + call queue_recent_call_for_thread(effects%recent_calls(i), & + policy%nthr_recent) + else + call add_call_to_recent_calls(effects%recent_calls(i)) endif - endif + enddo endif - lhashit=.false.; if(ntxhash.eq.1) lhashit=.true. -! Check for <...> callsign - if(c13(1:1).eq.'<')then - call save_hash_txcallvar(c13,n10,n12,n22,lhashit) !Save callsign in hash table - i2=index(c13,'>') - c13=c13(2:i2-1) - n22=ihashcall(c13,22) - n28=NTOKENS + n22 - go to 900 - endif + return +end subroutine apply_unpack77_effects -! Check for standard callsign - iarea=-1 - n=len(trim(c13)) - do i=n,2,-1 - if(is_digit(c13(i:i))) exit - enddo - iarea=i !Call-area digit - npdig=0 !Digits before call area - nplet=0 !Letters before call area - do i=1,iarea-1 - if(is_digit(c13(i:i))) npdig=npdig+1 - if(is_letter(c13(i:i))) nplet=nplet+1 - enddo - nslet=0 - do i=iarea+1,n - if(is_letter(c13(i:i))) nslet=nslet+1 - enddo - if(iarea.lt.2 .or. iarea.gt.3 .or. nplet.eq.0 .or. & - npdig.ge.iarea-1 .or. nslet.gt.3) then -! Treat this as a nonstandard callsign: compute its 22-bit hash - call save_hash_txcallvar(c13,n10,n12,n22,lhashit) !Save callsign in hash table - n22=ihashcall(c13,22) - n28=NTOKENS + n22 - go to 900 +subroutine unpack28_for_context(context,n28,c13,success) + type(unpack77_context), intent(in) :: context + integer, intent(in) :: n28 + character*13 c13 + logical success + + if(context%configured) then + call unpack28_configured(n28,c13,success) + else + call unpack28(n28,c13,success) endif - - n=len(trim(c13)) -! This is a standard callsign - call save_hash_txcallvar(c13,n10,n12,n22,lhashit) !Save callsign in hash table - if(iarea.eq.2) callsign=' '//c13(1:5) - if(iarea.eq.3) callsign=c13(1:6) - i1=index(a1,callsign(1:1))-1 - i2=index(a2,callsign(2:2))-1 - i3=index(a3,callsign(3:3))-1 - i4=index(a4,callsign(4:4))-1 - i5=index(a4,callsign(5:5))-1 - i6=index(a4,callsign(6:6))-1 - n28=36*10*27*27*27*i1 + 10*27*27*27*i2 + 27*27*27*i3 + 27*27*i4 + & - 27*i5 + i6 - n28=n28 + NTOKENS + MAX22 -900 n28=iand(n28,ishft(1,28)-1) return -end subroutine pack28var +end subroutine unpack28_for_context +subroutine record_configured_n28_pair(context,n28a,n28b) + type(unpack77_context), intent(in) :: context + integer, intent(in) :: n28a,n28b -subroutine unpack28var(n28_0,c13,success,nthr) + if(.not.context%configured) return + n28a_configured=n28a + n28b_configured=n28b - parameter (NTOKENS=2063592,MAX22=4194304) - logical success - character*13 c13 - character*37 c1 - character*36 c2 - character*10 c3 - character*27 c4 - integer, intent(in) :: nthr - data c1/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/ - data c2/'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/ - data c3/'0123456789'/ - data c4/' ABCDEFGHIJKLMNOPQRSTUVWXYZ'/ + return +end subroutine record_configured_n28_pair - success=.true. - n28=n28_0 - if(n28.lt.NTOKENS) then -! Special tokens DE, QRZ, CQ, CQ_nnn, CQ_aaaa - if(n28.eq.0) c13='DE ' - if(n28.eq.1) c13='QRZ ' - if(n28.eq.2) c13='CQ ' - if(n28.le.2) go to 900 - if(n28.le.1002) then - write(c13,1002) n28-3 -1002 format('CQ_',i3.3) - go to 900 - endif - if(n28.le.532443) then - n=n28-1003 - n0=n - i1=n/(27*27*27) - n=n-27*27*27*i1 - i2=n/(27*27) - n=n-27*27*i2 - i3=n/27 - i4=n-27*i3 - c13=c4(i1+1:i1+1)//c4(i2+1:i2+1)//c4(i3+1:i3+1)//c4(i4+1:i4+1) - c13=adjustl(c13) - c13='CQ_'//c13(1:10) - go to 900 +logical function unpack77_type12_var_guard_ok(call_1,call_2,grid4,ir) result(ok) +! Configured decode rejects a few hash-collision renders that parse as valid +! source messages but cannot round-trip through the configured var encoder. + character(len=*), intent(in) :: call_1,call_2,grid4 + integer, intent(in) :: ir + type(pack77_type12_call_source) :: source + logical :: source_ok + + ok=.true. + if(ir.eq.0 .and. call_1(1:1).eq.'<' .and. grid4.ne.'RR73') then + call pack77_parse_type12_call(call_2,.false.,source,source_ok) + if(source_ok .and. source%suffix.eq.PACK77_TYPE12_SUFFIX_R) ok=.false. + endif + + return +end function unpack77_type12_var_guard_ok + +logical function unpack77_type4_var_guard_ok(call_1,call_2,msg,iflip,icq,nrpt) result(ok) +! Type 4 can decode arbitrary 58-bit text as plausible calls; configured var +! mode keeps only renders that also satisfy the source-side Type 4 grammar. + character(len=*), intent(in) :: call_1,call_2,msg + integer, intent(in) :: iflip,icq,nrpt + integer :: nmsglen,indxp,nlencall2,nindxspace + type(pack77_type4_source) :: source + logical :: source_ok + + ok=.true. + call pack77_parse_type4_source(msg,source,source_ok) + if(.not.source_ok) ok=.false. + if(.not.unpack77_type4_call_guard_ok(call_2)) ok=.false. + if(msg(1:3).ne.'CQ ' .and. .not.unpack77_type4_call_guard_ok(call_1)) & + ok=.false. + nmsglen=len_trim(msg) + if(ok .and. nmsglen.gt.0) then + indxp=index(msg,'/P ') + if((icq.eq.0 .and. nrpt.eq.0 .and. (indxp.lt.1 .or. indxp.gt.7)) .or. & + icq.eq.1) then + if(msg(nmsglen:nmsglen).eq.'>') ok=.false. endif endif - n28=n28-NTOKENS - if(n28.lt.MAX22) then -! This is a 22-bit hash of a callsign - n22=n28 - call hash22var(n22,c13,nthr) !Retrieve callsign from hash table - if(c13.eq.'<...>') then - call hash22(n22,c13) + if(ok .and. iflip.eq.0 .and. icq.eq.0 .and. nrpt.eq.0) then + nlencall2=len_trim(call_2) + if(nlencall2.gt.9) then + if(call_2(1:1).eq.'/' .or. call_2(nlencall2:nlencall2).eq.'/') & + ok=.false. + nindxspace=index(call_2,' ') + if(nindxspace.gt.0 .and. nindxspace.lt.nlencall2) ok=.false. endif - go to 900 endif - -! Standard callsign - n=n28 - MAX22 - i1=n/(36*10*27*27*27) - n=n-36*10*27*27*27*i1 - i2=n/(10*27*27*27) - n=n-10*27*27*27*i2 - i3=n/(27*27*27) - n=n-27*27*27*i3 - i4=n/(27*27) - n=n-27*27*i4 - i5=n/27 - i6=n-27*i5 - c13=c1(i1+1:i1+1)//c2(i2+1:i2+1)//c3(i3+1:i3+1)//c4(i4+1:i4+1)// & - c4(i5+1:i5+1)//c4(i6+1:i6+1) - c13=adjustl(c13) -900 i0=index(c13,' ') + return +end function unpack77_type4_var_guard_ok + +logical function unpack77_type4_call_guard_ok(call_text) result(ok) + character(len=*), intent(in) :: call_text + integer :: ispace,islash + + ok=pack77_valid_hash_call_token(call_text) .or. & + pack77_valid_type4_c11(call_text) + if(.not.ok) return + if(pack77_valid_hash_call_token(call_text)) return + if(pack77_is_digit_char(call_text(1:1)) .and. & + pack77_is_digit_char(call_text(2:2))) ok=.false. + if(len_trim(call_text).eq.11) then + ispace=index(call_text,' ') + if(ispace.gt.0 .and. ispace.lt.12) ok=.false. + islash=index(call_text,'/') + if(islash.eq.1 .or. islash.eq.2 .or. islash.eq.11 .or. & + (islash.eq.10 .and. pack77_is_letter_char(call_text(11:11)) .and. & + call_text(11:11).ne.'P')) ok=.false. + if(islash.lt.6 .and. pack77_is_digit_char(call_text(11:11))) ok=.false. + endif - if((i0.ne.0 .and. i0.lt.len(trim(c13))) .or. (c13(1:1).lt.':' .and. c13(1:1).gt.'/' .and. & - c13(2:2).lt.':' .and. c13(2:2).gt.'/')) then - c13='QU1RK' - success=.false. + return +end function unpack77_type4_call_guard_ok + +subroutine lower_c28_slot(token,allow_auto_hash,n28,hash_facts,ok) + character(len=*), intent(in) :: token + logical, intent(in) :: allow_auto_hash + integer, intent(out) :: n28 + character*13 cw + type(pack77_hash_facts), intent(inout) :: hash_facts + type(pack77_hash_facts) :: trial_facts + logical, intent(out) :: ok + +! Some c28 fields may auto-hash nonstandard calls; stricter fields require an +! explicit <CALL> hash token so source text cannot choose a different family. + ok=.true. + n28=-1 + cw=token + if(cw(1:3).eq.'DE ' .or. cw(1:4).eq.'QRZ ' .or. & + cw(1:3).eq.'CQ ' .or. cw(1:3).eq.'CQ_') then + trial_facts=hash_facts + call pack28_core(cw,n28,trial_facts) + if(n28.lt.PACK77_NTOKENS) return + endif + + cw=token + if(pack77_valid_hash_call_token(cw) .or. pack77_c28_standard_shape(cw)) then + call pack28_core(cw,n28,hash_facts) + return + endif + + if(.not.allow_auto_hash) then + ok=.false. + return endif + call pack28_core(cw,n28,hash_facts) + return -end subroutine unpack28var +end subroutine lower_c28_slot -subroutine split77var(msg,nwords,nw,w) -! Convert msg to upper case; collapse multiple blanks; parse into words. +integer function pack77_c28_render_len(token) result(n) + character(len=*), intent(in) :: token + integer :: m - character*37 msg - character*13 w(19) - character*1 c,c0 - character*6 bcall_1 - logical ok1 - integer nw(19) - - iz=len(trim(msg)) - j=0 - k=0 - n=0 - c0=' ' - w=' ' - do i=1,iz - if(ichar(msg(i:i)).eq.0) msg(i:i)=' ' - c=msg(i:i) !Single character - if(c.eq.' ' .and. c0.eq.' ') cycle !Skip leading/repeated blanks - if(c.ne.' ' .and. c0.eq.' ') then - k=k+1 !New word - n=0 + m=len_trim(token) + n=m + if(pack77_valid_hash_call_token(token)) return + if(.not.pack77_c28_standard_shape(token)) then + if(m.gt.11) then + n=-1 + else + n=m+2 endif - j=j+1 !Index in msg - n=n+1 !Index in word - if(c.ge.'a' .and. c.le.'z') c=char(ichar(c)-32) !Force upper case - msg(j:j)=c - if(n.le.13) w(k)(n:n)=c !Copy character c into word - c0=c - enddo - iz=j !Message length - nwords=k !Number of words in msg - if(nwords.le.0) go to 900 - nw(k)=len(trim(w(k))) - msg(iz+1:)=' ' - if(nwords.lt.3) go to 900 - call chkcall(w(3),bcall_1,ok1) - if(ok1 .and. w(1)(1:3).eq.'CQ ') then - w(1)='CQ_'//w(2)(1:10) !Make "CQ " into "CQ_" - w(2:12)=w(3:13) !Move all remeining words down by one - nwords=nwords-1 endif - -900 return -end subroutine split77var +end function pack77_c28_render_len -subroutine pack77_01var(nwords,w,i3,n3,c77,ntxhash) +logical function pack77_dxpedition_render_fits(source) result(ok) + type(pack77_dxpedition_source), intent(in) :: source + integer :: n,ncall + + ok=.false. + n=pack77_c28_render_len(source%call_1) + if(n.lt.0) return + ncall=pack77_c28_render_len(source%call_2) + if(ncall.lt.0) return + n=n+1+5+1+ncall+1+len_trim(source%hash_token)+1+3 + ok=n.le.37 +end function pack77_dxpedition_render_fits + + +logical function pack77_field_day_render_fits(source) result(ok) + type(pack77_field_day_source), intent(in) :: source + character(len=3) :: section + integer :: n,ncall,nexch + + ok=.false. + section=pack77_arrl_section_name(source%isec) + nexch=2 + if(source%ntx.ge.10) nexch=3 + n=pack77_c28_render_len(source%call_1) + if(n.lt.0) return + ncall=pack77_c28_render_len(source%call_2) + if(ncall.lt.0) return + n=n+1+ncall+1+nexch+1+len_trim(section) + if(source%ir.eq.1) n=n+2 + ok=n.le.37 +end function pack77_field_day_render_fits + + +subroutine unpack28_configured(n28_0,c13,success) + integer, intent(in) :: n28_0 + character*13 c13 + logical success + + call unpack28_core(n28_0,c13,success,.true.) + + return +end subroutine unpack28_configured + +type(pack77_candidate) function pack77_01_candidate(msg) result(candidate) ! Pack a Type 0.1 message: DXpedition mode ! Example message: "K1ABC RR73; W9XYZ <KH1/KH7Z> -11" 28 28 10 5 - character*13 w(19),c13 + character(len=*), intent(in) :: msg character*77 c77 - character*6 bcall_1,bcall_2 - logical ok1,ok2,lhashit - integer, intent(in) :: ntxhash - - if(nwords.ne.5) go to 900 !Must have 5 words - if(trim(w(2)).ne.'RR73;') go to 900 !2nd word must be "RR73;" - if(w(4)(1:1).ne.'<') go to 900 !4th word must have <...> - if(index(w(4),'>').lt.1) go to 900 - n=-99 - read(w(5),*,err=1) n -1 if(n.eq.-99) go to 900 !5th word must be a valid report - n5=(n+30)/2 - if(n5.lt.0) n5=0 - if(n5.gt.31) n5=31 - call chkcall(w(1),bcall_1,ok1) - if(.not.ok1) go to 900 !1st word must be a valid basecall - call chkcall(w(3),bcall_2,ok2) - if(.not.ok2) go to 900 !3rd word must be a valid basecall + type(pack77_dxpedition_source) :: source + integer :: n28a,n28b + logical ok,matched_shape -! Type 0.1: K1ABC RR73; W9XYZ <KH1/KH7Z> -11 28 28 10 5 71 DXpedition special msg - i3=0 - n3=1 - call pack28var(w(1),n28avar,ntxhash) - call pack28var(w(3),n28bvar,ntxhash) - lhashit=.false.; if(ntxhash.eq.1) lhashit=.true. - call save_hash_txcallvar(w(4),n10,n12,n22,lhashit) - i2=index(w(4),'>') - c13=w(4)(2:i2-1) - n10=ihashcall(c13,10) - write(c77,1010) n28avar,n28bvar,n10,n5,n3,i3 -1010 format(2b28.28,b10.10,b5.5,2b3.3) - -900 return -end subroutine pack77_01var + candidate%result=pack77_no_match() + call pack77_parse_dxpedition_source(msg,source,ok,matched_shape) + if(.not.ok) return + if(.not.pack77_dxpedition_render_fits(source)) return -subroutine pack77_03var(nwords,w,i3,n3,c77,ntxhash) +! Type 0.1: K1ABC RR73; W9XYZ <KH1/KH7Z> -11 28 28 10 5 71 DXpedition special msg + call lower_c28_slot(source%call_1,.true.,n28a,candidate%hash_facts,ok) + if(.not.ok) return + call lower_c28_slot(source%call_2,.true.,n28b,candidate%hash_facts,ok) + if(.not.ok) return + call pack77_hash_index(candidate%hash_facts,source%hash_token,10,n10,ok) + if(.not.ok) return + call encode_pack77_dxpedition(n28a,n28b,n10,source%n5,c77,ok) + call pack77_accept_candidate_encoded(candidate,0,1,c77,ok) +end function pack77_01_candidate + +type(pack77_candidate) function pack77_03_candidate(msg) result(candidate) ! Check 0.3 and 0.4 (ARRL Field Day exchange) -! Example message: WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 +! Example message: WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 - parameter (NSEC=86) !Number of ARRL Sections - character*13 w(19) + character(len=*), intent(in) :: msg character*77 c77 - character*6 bcall_1,bcall_2 - character*3 csec(NSEC) - logical ok1,ok2 - integer, intent(in) :: ntxhash - data csec/ & - "AB ","AK ","AL ","AR ","AZ ","BC ","CO ","CT ","DE ","EB ", & - "EMA","ENY","EPA","EWA","GA ","GH ","IA ","ID ","IL ","IN ", & - "KS ","KY ","LA ","LAX","NS ","MB ","MDC","ME ","MI ","MN ", & - "MO ","MS ","MT ","NC ","ND ","NE ","NFL","NH ","NL ","NLI", & - "NM ","NNJ","NNY","TER","NTX","NV ","OH ","OK ","ONE","ONN", & - "ONS","OR ","ORG","PAC","PR ","QC ","RI ","SB ","SC ","SCV", & - "SD ","SDG","SF ","SFL","SJV","SK ","SNJ","STX","SV ","TN ", & - "UT ","VA ","VI ","VT ","WCF","WI ","WMA","WNY","WPA","WTX", & - "WV ","WWA","WY ","DX ","PE ","NB "/ - - if(nwords.lt.4 .or. nwords.gt.5) return - call chkcall(w(1),bcall_1,ok1) - call chkcall(w(2),bcall_2,ok2) - if(.not.ok1 .or. .not.ok2) return - isec=-1 - do i=1,NSEC - if(csec(i).eq.w(nwords)(1:3)) then - isec=i - exit - endif - enddo - if(isec.eq.-1) return - if(nwords.eq.5 .and. trim(w(3)).ne.'R') return - - ntx=-1 - j=len(trim(w(nwords-1)))-1 - read(w(nwords-1)(1:j),*,err=1,end=1) ntx !Number of transmitters -1 if(ntx.lt.1 .or. ntx.gt.32) return - nclass=ichar(w(nwords-1)(j+1:j+1))-ichar('A') - - m=len(trim(w(nwords))) !Length of section abbreviation - if(m.lt.2 .or. m.gt.3) return + type(pack77_field_day_source) :: source + integer :: n28a,n28b + logical ok,matched_shape + + candidate%result=pack77_no_match() + call pack77_parse_field_day_source(msg,source,ok,matched_shape) + if(.not.ok) return + if(.not.pack77_field_day_render_fits(source)) return ! 0.3 WA9XYZ KA1ABC R 16A EMA 28 28 1 4 3 7 71 ARRL Field Day ! 0.4 WA9XYZ KA1ABC R 32A EMA 28 28 1 4 3 7 71 ARRL Field Day - - i3=0 + n3=3 !Type 0.3 ARRL Field Day - intx=ntx-1 + intx=source%ntx-1 if(intx.ge.16) then n3=4 !Type 0.4 ARRL Field Day - intx=ntx-17 + intx=source%ntx-17 endif - call pack28var(w(1),n28avar,ntxhash) - call pack28var(w(2),n28bvar,ntxhash) - ir=0 - if(w(3)(1:2).eq.'R ') ir=1 - write(c77,1010) n28avar,n28bvar,ir,intx,nclass,isec,n3,i3 -1010 format(2b28.28,b1,b4.4,b3.3,b7.7,2b3.3) + call lower_c28_slot(source%call_1,.true.,n28a,candidate%hash_facts,ok) + if(.not.ok) return + call lower_c28_slot(source%call_2,.true.,n28b,candidate%hash_facts,ok) + if(.not.ok) return + if(n3.eq.3) then + call encode_pack77_field_day_low(n28a,n28b,source%ir,intx, & + source%nclass,source%isec,c77,ok) + else + call encode_pack77_field_day_high(n28a,n28b,source%ir,intx, & + source%nclass,source%isec,c77,ok) + endif + call pack77_accept_candidate_encoded(candidate,0,n3,c77,ok) return -end subroutine pack77_03var +end function pack77_03_candidate -subroutine pack77_06var(nwords,w,i3,n3,c77,i3_hint,n3_hint,ntxhash) +type(pack77_candidate) function pack77_06_candidate(msg,prefer_wspr_50bit) result(candidate) - character*13 w(19) + character(len=*), intent(in) :: msg character*77 c77 - character*6 bcall,grid6 - character*4 grid4 - character*1 c - character*36 a2 - integer, intent(in) :: ntxhash - data a2/'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'/,nzzz/46656/ - - logical is_grid4,is_grid6,is_digit,ok - is_grid4(grid4)=len(trim(grid4)).eq.4 .and. & - grid4(1:1).ge.'A' .and. grid4(1:1).le.'R' .and. & - grid4(2:2).ge.'A' .and. grid4(2:2).le.'R' .and. & - grid4(3:3).ge.'0' .and. grid4(3:3).le.'9' .and. & - grid4(4:4).ge.'0' .and. grid4(4:4).le.'9' - - is_grid6(grid6)=(len(trim(grid6)).eq.6.or.len(trim(grid6)).eq.4).and. & - grid6(1:1).ge.'A' .and. grid6(1:1).le.'R' .and. & - grid6(2:2).ge.'A' .and. grid6(2:2).le.'R' .and. & - grid6(3:3).ge.'0' .and. grid6(3:3).le.'9' .and. & - grid6(4:4).ge.'0' .and. grid6(4:4).le.'9' .and. & - (len(trim(grid6)).eq.4.or. & - (grid6(5:5).ge.'A' .and. grid6(5:5).le.'X' .and. & - grid6(6:6).ge.'A' .and. grid6(6:6).le.'X')) - - is_digit(c)=c.ge.'0' .and. c.le.'9' - - m1=len(trim(w(1))) - m2=len(trim(w(2))) - m3=len(trim(w(3))) - if(nwords.eq.3 .and. m1.ge.3 .and. m1.le.6 .and. m2.eq.4 .and. m3.le.2) then + type(pack77_wspr_source) :: source + logical, intent(in) :: prefer_wspr_50bit + logical ok,matched_shape + + candidate%result=pack77_no_match() + call pack77_parse_wspr_source(msg,source,ok,matched_shape) + if(.not.ok) return + + if(source%subtype.eq.1) then ! WSPR Type 1 - if(.not.is_grid4(w(2)(1:4))) go to 900 - if(.not.is_digit(w(3)(1:1))) go to 900 - if(m3.eq.2) then - if(.not.is_digit(w(3)(2:2))) go to 900 - endif - i3=0 - n3=6 - call pack28var(w(1),n28,ntxhash) - grid4=w(2)(1:4) - k1=(ichar(grid4(1:1))-ichar('A'))*18*10*10 - k2=(ichar(grid4(2:2))-ichar('A'))*10*10 - k3=(ichar(grid4(3:3))-ichar('0'))*10 - k4=(ichar(grid4(4:4))-ichar('0')) - igrid4=k1+k2+k3+k4 - read(w(3),*) idbm - if(idbm.lt.0) idbm=0 - if(idbm.gt.60) idbm=60 - idbm=nint(0.3*idbm) - write(c77,1010) n28,igrid4,idbm,0,0,0,n3,i3 -1010 format(b28.28,b15.15,b5.5,2i1,b21.21,2b3.3) - go to 900 + call lower_c28_slot(source%call_token,.false.,n28,candidate%hash_facts,ok) + if(.not.ok) return + igrid4=pack77_grid4_index(source%grid4) + call encode_pack77_wspr_type1(n28,igrid4,source%idbm,c77,ok) + call pack77_accept_candidate_encoded(candidate,0,6,c77,ok) + return endif - if(nwords.eq.2 .and. m1.ge.5 .and. m1.le.10 .and. m2.le.2) then + + if(source%subtype.eq.2) then ! WSPR Type 2 - i1=index(w(1),'/') - if(i1.lt.2 .or. i1.eq.m1) go to 900 - if(.not.is_digit(w(2)(1:1))) go to 900 - if(i1.eq.(m1-3) .and. .not.is_digit(w(1)(m1:m1))) go to 900 - if(m2.eq.2) then - if(.not.is_digit(w(2)(2:2))) go to 900 - endif - call chkcall(w(1),bcall,ok) - if(.not.ok) go to 900 - if(i1.le.4) then -! We have a prefix - npfx=index(a2,w(1)(1:1))-1 - if(i1.ge.3) npfx=36*npfx + index(a2,w(1)(2:2))-1 - if(i1.eq.4) npfx=36*npfx + index(a2,w(1)(3:3))-1 - else -! We have a suffix - if((m1-i1).eq.1) npfx=index(a2,w(1)(i1+1:i1+1))-1 - if((m1-i1).eq.2) npfx=36*(index(a2,w(1)(i1+1:i1+1))-1) + & - index(a2,w(1)(i1+2:i1+2))-1 - if((m1-i1).eq.3) then -! Third character of a suffix must be a digit - if(.not.is_digit(w(1)(i1+3:i1+3))) go to 900 - npfx=36*10*(index(a2,w(1)(i1+1:i1+1))-1) + & - 10*(index(a2,w(1)(i1+2:i1+2))-1) + index(a2,w(1)(i1+3:i1+3))-1 - endif - npfx=npfx + nzzz - endif - i3=0 - n3=6 - call pack28var(bcall//' ',n28,ntxhash) - read(w(2),*) idbm - if(idbm.lt.0) idbm=0 - if(idbm.gt.60) idbm=60 - idbm=nint(0.3*idbm) - write(c77,1020) n28,npfx,idbm,1,0,n3,i3 -1020 format(b28.28,b16.16,b5.5,i1,b21.21,2b3.3) - go to 900 + call lower_c28_slot(source%base_call//' ',.false.,n28, & + candidate%hash_facts,ok) + if(.not.ok) return + call encode_pack77_wspr_type2(n28,source%npfx,source%idbm,c77,ok) + call pack77_accept_candidate_encoded(candidate,0,6,c77,ok) + return endif - if(i3_hint.eq.0.and.n3_hint.eq.6.and.nwords.eq.2 .and. m1.ge.5 & - .and. m1.le.12 .and. m2.le.6) then + if(source%subtype.eq.3 .and. prefer_wspr_50bit) then ! WSPR Type 3 - !n3_hint=6 and i3_hint=0 is a hint that the caller wanted a - !50-bit encoding rather than the possible alternative n3=4 77-bit - !encoding - if(index(w(1),'<').lt.1 .or. index(w(1),'>').lt.1) go to 900 - grid6=w(2)(1:6) - if(.not.is_grid6(grid6)) go to 900 - i3=0 - n3=6 - call pack28var(w(1),n28,ntxhash) - n22=n28-2063592 - k1=(ichar(grid6(1:1))-ichar('A'))*18*10*10*25*25 - k2=(ichar(grid6(2:2))-ichar('A'))*10*10*25*25 - k3=(ichar(grid6(3:3))-ichar('0'))*10*25*25 - k4=(ichar(grid6(4:4))-ichar('0'))*25*25 - if (grid6(5:6).eq.' ') then - igrid6=k1+k2+k3+k4+24*25+24 - else - k5=(ichar(grid6(5:5))-ichar('A'))*25 - k6=(ichar(grid6(6:6))-ichar('A')) - igrid6=k1+k2+k3+k4+k5+k6 - endif - write(c77,1030) n22,igrid6,2,0,n3,i3 -1030 format(b22.22,b25.25,b3.3,b21.21,2b3.3) + ! n3=6 and i3=0 are an advisory preference for the caller's + ! 50-bit encoding over the possible alternative n3=4 77-bit + ! encoding. + call pack77_hash_index(candidate%hash_facts,source%hash_token,22,n22,ok) + if(.not.ok) return + igrid6=pack77_grid6_wspr_index(source%grid6) + call encode_pack77_wspr_type3(n22,igrid6,c77,ok) + call pack77_accept_candidate_encoded(candidate,0,6,c77,ok) endif +end function pack77_06_candidate -900 return -end subroutine pack77_06var +type(pack77_candidate) function pack77_1_candidate(msg) result(candidate) -subroutine pack77_1var(nwords,w,i3,n3,c77,ntxhash) +! Type 1/2 source grammar lowers only exact c28 call-slot forms. -! Check Type 1 (Standard 77-bit message) and Type 2 (ditto, with a "/P" call) -! Example message: WA9XYZ/R KA1ABC/R R FN42 28 1 28 1 1 15 74 - - parameter (MAXGRID4=32400) - character*13 w(19),c13 + character(len=*), intent(in) :: msg character*77 c77 - character*6 bcall_1,bcall_2 - character*4 grid4 - character c1*1,c2*2 - logical is_grid4 - logical ok1,ok2 - integer, intent(in) :: ntxhash - is_grid4(grid4)=len(trim(grid4)).eq.4 .and. & - grid4(1:1).ge.'A' .and. grid4(1:1).le.'R' .and. & - grid4(2:2).ge.'A' .and. grid4(2:2).le.'R' .and. & - grid4(3:3).ge.'0' .and. grid4(3:3).le.'9' .and. & - grid4(4:4).ge.'0' .and. grid4(4:4).le.'9' - - if(nwords.lt.2 .or. nwords.gt.4) return - call chkcall(w(1),bcall_1,ok1) - call chkcall(w(2),bcall_2,ok2) - if(w(1)(1:3).eq.'DE ' .or. w(1)(1:3).eq.'CQ_' .or. w(1)(1:3).eq.'CQ ' .or. & - w(1)(1:4).eq.'QRZ ') ok1=.true. - if(w(1)(1:1).eq.'<' .and. index(w(1),'>').ge.5) ok1=.true. - if(w(2)(1:1).eq.'<' .and. index(w(2),'>').ge.5) ok2=.true. - if(.not.ok1 .or. .not.ok2) return - if(w(1)(1:1).eq.'<' .and. index(w(2),'/').gt.0) return - if(w(2)(1:1).eq.'<' .and. index(w(1),'/').gt.0) return - if(nwords.eq.2 .and. (.not.ok2 .or. index(w(2),'/').ge.2)) return - if(nwords.eq.2) go to 10 - - c1=w(nwords)(1:1) - c2=w(nwords)(1:2) - if(.not.is_grid4(w(nwords)(1:4)) .and. c1.ne.'+' .and. c1.ne.'-' & - .and. c2.ne.'R+' .and. c2.ne.'R-' .and. trim(w(nwords)).ne.'RRR' .and. & - trim(w(nwords)).ne.'RR73' .and. trim(w(nwords)).ne.'73') return - if(c1.eq.'+' .or. c1.eq.'-') then - ir=0 - read(w(nwords),*,err=900) irpt - if(irpt.ge.-50 .and. irpt.le.-31) irpt=irpt+101 - irpt=irpt+35 - else if(c2.eq.'R+' .or. c2.eq.'R-') then - ir=1 - read(w(nwords)(2:),*,err=900) irpt - if(irpt.ge.-50 .and. irpt.le.-31) irpt=irpt+101 - irpt=irpt+35 - else if(trim(w(nwords)).eq.'RRR') then - ir=0 - irpt=2 - else if(trim(w(nwords)).eq.'RR73') then - ir=0 - irpt=3 - else if(trim(w(nwords)).eq.'73') then - ir=0 - irpt=4 - endif - -! 1 WA9XYZ/R KA1ABC/R R FN42 28 1 28 1 1 15 74 Standard msg -! 2 PA3XYZ/P GM4ABC/P R JO22 28 1 28 1 1 15 74 EU VHF contest - -10 i1psuffix=index(w(1)//' ' ,'/P ') - i2psuffix=index(w(2)//' ','/P ') - if(nwords.eq.2 .or. nwords.eq.3 .or. (nwords.eq.4 .and. & - w(3)(1:2).eq.'R ')) then - n3=0 - i3=1 !Type 1: Standard message, possibly with "/R" - if (i1psuffix.ge.4.or.i2psuffix.ge.4) i3=2 !Type 2, with "/P" - endif - c13=bcall_1 - if(c13(1:3).eq.'CQ_' .or. w(1)(1:1).eq.'<') c13=w(1) - call pack28var(c13,n28avar,ntxhash) - c13=bcall_2 - if(w(2)(1:1).eq.'<') c13=w(2) - call pack28var(c13,n28bvar,ntxhash) + type(pack77_type12_source) :: source + integer :: n28a,n28b + logical ok + + candidate%result=pack77_no_match() + call pack77_parse_type12_source(msg,source,ok) + if(.not.ok) return + + call lower_c28_slot(source%call_1%c28_token,.false.,n28a, & + candidate%hash_facts,ok) + if(.not.ok) return + call lower_c28_slot(source%call_2%c28_token,.false.,n28b, & + candidate%hash_facts,ok) + if(.not.ok) return + ipa=0 ipb=0 - if(i1psuffix.ge.4.or.index(w(1)//' ','/R ').ge.4) ipa=1 - if(i2psuffix.ge.4.or.index(w(2)//' ','/R ').ge.4) ipb=1 - - grid4=w(nwords)(1:4) - if(is_grid4(grid4)) then - ir=0 - if(w(3).eq.'R ') ir=1 - j1=(ichar(grid4(1:1))-ichar('A'))*18*10*10 - j2=(ichar(grid4(2:2))-ichar('A'))*10*10 - j3=(ichar(grid4(3:3))-ichar('0'))*10 - j4=(ichar(grid4(4:4))-ichar('0')) - igrid4=j1+j2+j3+j4 + if(pack77_type12_call_has_suffix(source%call_1)) ipa=1 + if(pack77_type12_call_has_suffix(source%call_2)) ipb=1 + + if(source%i3.eq.1) then + call encode_pack77_type1(n28a,ipa,n28b,ipb, & + source%ir,source%igrid4,c77,ok) else - igrid4=MAXGRID4 + irpt + call encode_pack77_type2(n28a,ipa,n28b,ipb, & + source%ir,source%igrid4,c77,ok) endif - if(nwords.eq.2) then - ir=0 - irpt=1 - igrid4=MAXGRID4+irpt - endif - write(c77,1000) n28avar,ipa,n28bvar,ipb,ir,igrid4,i3 -1000 format(2(b28.28,b1),b1,b15.15,b3.3) - return - -900 return -end subroutine pack77_1var + call pack77_accept_candidate_encoded(candidate,source%i3,0,c77,ok) +end function pack77_1_candidate -subroutine pack77_3var(nwords,w,i3,n3,c77,ntxhash) +type(pack77_candidate) function pack77_3_candidate(msg) result(candidate) ! Check Type 3 (ARRL RTTY contest exchange) !ARRL RTTY - US/Can: rpt state/prov R 579 MA ! - DX: rpt serial R 559 0013 ! Example message: TU; W9XYZ K1ABC R 579 MA 1 28 28 1 3 13 74 - parameter (NUSCAN=171) !Number of US states and Canadian provinces/territories - character*13 w(19) + character(len=*), intent(in) :: msg character*77 c77 - character*6 bcall_1,bcall_2 - character*3 cmult(NUSCAN),mult - character crpt*3 - integer, intent(in) :: ntxhash - logical ok1,ok2 - data cmult/ & - "AL ","AK ","AZ ","AR ","CA ","CO ","CT ","DE ","FL ","GA ", & - "HI ","ID ","IL ","IN ","IA ","KS ","KY ","LA ","ME ","MD ", & - "MA ","MI ","MN ","MS ","MO ","MT ","NE ","NV ","NH ","NJ ", & - "NM ","NY ","NC ","ND ","OH ","OK ","OR ","PA ","RI ","SC ", & - "SD ","TN ","TX ","UT ","VT ","VA ","WA ","WV ","WI ","WY ", & - "NB ","NS ","QC ","ON ","MB ","SK ","AB ","BC ","NWT","NF ", & - "LB ","NU ","YT ","PEI","DC ","DR ","FR ","GD ","GR ","OV ", & - "ZH ","ZL ","X01","X02","X03","X04","X05","X06","X07","X08", & - "X09","X10","X11","X12","X13","X14","X15","X16","X17","X18", & - "X19","X20","X21","X22","X23","X24","X25","X26","X27","X28", & - "X29","X30","X31","X32","X33","X34","X35","X36","X37","X38", & - "X39","X40","X41","X42","X43","X44","X45","X46","X47","X48", & - "X49","X50","X51","X52","X53","X54","X55","X56","X57","X58", & - "X59","X60","X61","X62","X63","X64","X65","X66","X67","X68", & - "X69","X70","X71","X72","X73","X74","X75","X76","X77","X78", & - "X79","X80","X81","X82","X83","X84","X85","X86","X87","X88", & - "X89","X90","X91","X92","X93","X94","X95","X96","X97","X98", & - "X99"/ - - if(w(1)(1:1).eq.'<' .and. w(2)(1:1).eq.'<') go to 900 - if(nwords.eq.4 .or. nwords.eq.5 .or. nwords.eq.6) then - i1=1 - if(trim(w(1)).eq.'TU;') i1=2 - call chkcall(w(i1),bcall_1,ok1) - call chkcall(w(i1+1),bcall_2,ok2) - if(.not.ok1 .or. .not.ok2) go to 900 - crpt=w(nwords-1)(1:3) - if(index(crpt,'-').ge.1 .or. index(crpt,'+').ge.1) go to 900 - if(crpt(1:1).eq.'5' .and. crpt(2:2).ge.'2' .and. crpt(2:2).le.'9' .and. & - crpt(3:3).eq.'9') then - nserial=0 - read(w(nwords),*,err=1) nserial - endif -1 mult=' ' - imult=-1 - do i=1,NUSCAN - if(cmult(i).eq.w(nwords)) then - imult=i - mult=cmult(i) - exit - endif - enddo - nexch=0 - if(nserial.gt.0) nexch=nserial - if(imult.gt.0) nexch=8000+imult - if(mult.ne.' ' .or. nserial.gt.0) then - i3=3 - n3=0 - itu=0 - if(trim(w(1)).eq.'TU;') itu=1 - call pack28var(w(1+itu),n28avar,ntxhash) - call pack28var(w(2+itu),n28bvar,ntxhash) - ir=0 - if(w(3+itu)(1:2).eq.'R ') ir=1 - read(w(3+itu+ir),*,err=900) irpt - irpt=(irpt-509)/10 - 2 - if(irpt.lt.0) irpt=0 - if(irpt.gt.7) irpt=7 + character*4 exchange + type(pack77_type3_source) :: source + integer :: n28a,n28b + logical ok,matched_shape + + candidate%result=pack77_no_match() + call pack77_parse_type3_source(msg,source,ok,matched_shape) + if(.not.ok) return + + call lower_c28_slot(source%call_1,.true.,n28a,candidate%hash_facts,ok) + if(.not.ok) return + call lower_c28_slot(source%call_2,.true.,n28b,candidate%hash_facts,ok) + if(.not.ok) return + exchange=' ' + if(source%nexch.gt.0 .and. source%nexch.lt.8000) & + write(exchange,'(i4.4)') source%nexch + if(source%nexch.gt.8000) & + exchange=pack77_rtty_multiplier_name(source%nexch-8000) + if(.not.pack77_type3_display_fits(source%itu,source%ir,n28a, & + n28b,source%call_1,source%call_2,exchange)) return ! 3 TU; W9XYZ K1ABC R 579 MA 1 28 28 1 3 13 74 ARRL RTTY contest ! 3 TU; W9XYZ G8ABC R 559 0013 1 28 28 1 3 13 74 ARRL RTTY (DX) - write(c77,1010) itu,n28avar,n28bvar,ir,irpt,nexch,i3 -1010 format(b1,2b28.28,b1,b3.3,b13.13,b3.3) - endif - endif - -900 return -end subroutine pack77_3var + call encode_pack77_type3(source%itu,n28a,n28b,source%ir,source%irpt, & + source%nexch,c77,ok) + call pack77_accept_candidate_encoded(candidate,3,0,c77,ok) +end function pack77_3_candidate -subroutine pack77_4var(nwords,w,i3,n3,c77,ntxhash) +type(pack77_candidate) function pack77_4_candidate(msg) result(candidate) ! Check Type 4 (One nonstandard call and one hashed call) ! Example message: <WA9XYZ> PJ4/KA1ABC RR73 12 58 1 2 1 74 + character(len=*), intent(in) :: msg integer*8 n58 - logical ok1,ok2,lhashit - character*13 w(19) character*77 c77 - character*13 call_1,call_2 character*11 c11 - character*6 bcall_1,bcall_2 - character*38 c - integer, intent(in) :: ntxhash - data c/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/'/ - - iflip=0 - i3=-1 - if(nwords.eq.2 .or. nwords.eq.3) then - call_1=w(1) - if(call_1(1:1).eq.'<') call_1=w(1)(2:len(trim(w(1)))-1) - call_2=w(2) - if(call_2(1:1).eq.'<') call_2=w(2)(2:len(trim(w(2)))-1) - call chkcall(call_1,bcall_1,ok1) - call chkcall(call_2,bcall_2,ok2) - if(call_1.eq.bcall_1 .and. call_2.eq.bcall_2 .and. ok1 .and. ok2) go to 900 - icq=0 - if(trim(w(1)).eq.'CQ' .or. (ok1.and.ok2)) then - if(trim(w(1)).eq.'CQ' .and. len(trim(w(2))).le.4) go to 900 - i3=4 - n3=0 - if(trim(w(1)).eq.'CQ') icq=1 - endif + character*13 hash_token + type(pack77_type4_source) :: source + logical ok - lhashit=.false.; if(ntxhash.eq.1) lhashit=.true. - if(icq.eq.1) then - iflip=0 - n12=0 - c11=adjustr(call_2(1:11)) - call save_hash_txcallvar(w(2),n10,n12,n22,lhashit) - else if(w(1)(1:1).eq.'<') then - iflip=0 - i3=4 - call save_hash_txcallvar(w(1),n10,n12,n22,lhashit) - c11=adjustr(call_2(1:11)) - else if(w(2)(1:1).eq.'<') then - iflip=1 - i3=4 - call save_hash_txcallvar(w(2),n10,n12,n22,lhashit) - c11=adjustr(call_1(1:11)) - endif - n58=0 - do i=1,11 - n58=n58*38 + index(c,c11(i:i)) - 1 - enddo - nrpt=0 - if(trim(w(3)).eq.'RRR') nrpt=1 - if(trim(w(3)).eq.'RR73') nrpt=2 - if(trim(w(3)).eq.'73') nrpt=3 - if(icq.eq.1) then - iflip=0 - nrpt=0 - endif - write(c77,1010) n12,n58,iflip,nrpt,icq,i3 -1010 format(b12.12,b58.58,b1,b2.2,b1,b3.3) - do i=1,77 - if(c77(i:i).eq.'*') c77(i:i)='0' !### Clean up any illegal chars ### - enddo - endif + candidate%result=pack77_no_match() + call pack77_parse_type4_source(msg,source,ok) + if(.not.ok) return -900 return -end subroutine pack77_4var + hash_token=source%hash_token + if(source%icq.eq.1 .and. len_trim(hash_token).eq.0) hash_token=source%c11 + call pack77_hash_index(candidate%hash_facts,hash_token,12,n12,ok) + if(.not.ok) return + c11=adjustr(source%c11) + n58=pack77_type4_n58_from_call_text(c11) + nrpt=pack77_type4_nrpt_from_tail(source%itail) + if(source%icq.eq.1) nrpt=0 + call encode_pack77_type4(n12,n58,source%iflip,nrpt,source%icq,c77,ok) + call pack77_accept_candidate_encoded(candidate,4,0,c77,ok) +end function pack77_4_candidate -subroutine pack77_5var(nwords,w,i3,n3,c77,ntxhash) +type(pack77_candidate) function pack77_5_candidate(msg) result(candidate) ! Pack a Type 0.2 message: EU VHF Contest mode ! Example message: PA3XYZ/P R 590003 IO91NP 28 1 1 3 12 25 ! <PA3XYZ> <G4ABC/P> R 590003 IO91NP h10 h20 r1 s3 s12 g25 - character*13 w(19),c13 + character(len=*), intent(in) :: msg character*77 c77 - character*6 grid6 - logical is_grid6,lhashit - integer, intent(in) :: ntxhash - - is_grid6(grid6)=len(trim(grid6)).eq.6 .and. & - grid6(1:1).ge.'A' .and. grid6(1:1).le.'R' .and. & - grid6(2:2).ge.'A' .and. grid6(2:2).le.'R' .and. & - grid6(3:3).ge.'0' .and. grid6(3:3).le.'9' .and. & - grid6(4:4).ge.'0' .and. grid6(4:4).le.'9' .and. & - grid6(5:5).ge.'A' .and. grid6(5:5).le.'X' .and. & - grid6(6:6).ge.'A' .and. grid6(6:6).le.'X' - - if(nwords.lt.4 .or. nwords.gt.5) return !nwords must be 4 or 5 - if(w(1)(1:1).ne.'<' .or. w(2)(1:1).ne.'<') return !Both calls must be hashed - nx=-1 - read(w(nwords-1),*,err=2) nx -2 if(nx.lt.520001 .or. nx.gt.594095) return !Exchange between 520001 - 594095 - if(.not.is_grid6(w(nwords)(1:6))) return !Last word must be a valid grid6 - -! Type 0.2: <PA3XYZ> <G4ABC/P> R 590003 IO91NP h10 h20 r1 s3 s12 g25 - - i3=5 - n3=0 - - lhashit=.false.; if(ntxhash.eq.1) lhashit=.true. - call save_hash_txcallvar(w(1),n10,n12,n22,lhashit) - i2=index(w(1),'>') - c13=w(1)(2:i2-1) - n12=ihashcall(c13,12) - - call save_hash_txcallvar(w(2),n10a,n12a,n22,lhashit) - i2=index(w(2),'>') - c13=w(2)(2:i2-1) - n22=ihashcall(c13,22) - - ir=0 - if(w(3)(1:2).eq.'R ') ir=1 - irpt=nx/10000 - 52 - iserial=mod(nx,10000) - if(iserial.gt.2047) iserial=2047 - grid6=w(nwords)(1:6) - j1=(ichar(grid6(1:1))-ichar('A'))*18*10*10*24*24 - j2=(ichar(grid6(2:2))-ichar('A'))*10*10*24*24 - j3=(ichar(grid6(3:3))-ichar('0'))*10*24*24 - j4=(ichar(grid6(4:4))-ichar('0'))*24*24 - j5=(ichar(grid6(5:5))-ichar('A'))*24 - j6=(ichar(grid6(6:6))-ichar('A')) - igrid6=j1+j2+j3+j4+j5+j6 - write(c77,1010) n12,n22,ir,irpt,iserial,igrid6,i3 -1010 format(b12.12,b22.22,b1,b3.3,b11.11,b25.25,b3.3) - - return -end subroutine pack77_5var - -subroutine packtext77var(c13,c71) - - character*13 c13,w - character*71 c71 - character*42 c - character*1 qa(10),qb(10) - data c/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+-./?'/ - - call mp_short_init - qa=char(0) - w=adjustr(c13) - do i=1,13 - j=index(c,w(i:i))-1 - if(j.lt.0) j=0 - call mp_short_mult(qb,qa(2:10),9,42) !qb(1:9)=42*qa(2:9) - call mp_short_add(qa,qb(2:10),9,j) !qa(1:9)=qb(2:9)+j - enddo - - write(c71,1010) qa(2:10) -1010 format(b7.7,8b8.8) - - return -end subroutine packtext77var - -subroutine unpacktext77var(c71,c13) - - integer*1 ia(10) - character*1 qa(10),qb(10) - character*13 c13 - character*71 c71 - character*42 c - equivalence (qa,ia),(qb,ib) - data c/' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+-./?'/ - - qa(1)=char(0) - read(c71,1010) qa(2:10) -1010 format(b7.7,8b8.8) - - do i=13,1,-1 - call mp_short_div(qb,qa(2:10),9,42,ir) - c13(i:i)=c(ir+1:ir+1) - qa(2:10)=qb(1:9) - enddo + type(pack77_type5_source) :: source + logical ok,matched_shape - return -end subroutine unpacktext77var + candidate%result=pack77_no_match() + call pack77_parse_type5_source(msg,source,ok,matched_shape) + if(.not.ok) return -subroutine add_call_to_recent_callsvar(callsign,nthr) +! Type 0.2: <PA3XYZ> <G4ABC/P> R 590003 IO91NP h10 h20 r1 s3 s12 g25 - character*13 callsign - logical ladd - integer, intent(in) :: nthr + call pack77_hash_index(candidate%hash_facts,source%hash_token_1,12,n12,ok) + if(.not.ok) return + call pack77_hash_index(candidate%hash_facts,source%hash_token_2,22,n22,ok) + if(.not.ok) return -! only add if the callsign is not already on the list - ladd=.true. - do i=1,MAXRECENT-1 ! if callsign is at the end of the list add it again - if(recent_callsvar(i).eq.callsign) ladd=.false. - enddo - - if(ladd) then - do i=MAXRECENT,2,-1 - recent_callsvar(i)=recent_callsvar(i-1) - enddo - recent_callsvar(1)=callsign - endif - -! Make sure that callsign is hashed - call save_hash_callvar(callsign,nthr) + igrid6=pack77_grid6_index(source%grid6) + call encode_pack77_type5(n12,n22,source%ir,source%irpt,source%iserial, & + igrid6,c77,ok) + call pack77_accept_candidate_encoded(candidate,5,0,c77,ok) return -end subroutine add_call_to_recent_callsvar - -subroutine to_grid4var(n,grid4,ok) - character*4 grid4 - logical ok - - ok=.false. - j1=n/(18*10*10) - if (j1.lt.0.or.j1.gt.17) goto 900 - n=n-j1*18*10*10 - j2=n/(10*10) - if (j2.lt.0.or.j2.gt.17) goto 900 - n=n-j2*10*10 - j3=n/10 - if (j3.lt.0.or.j3.gt.9) goto 900 - j4=n-j3*10 - if (j4.lt.0.or.j4.gt.9) goto 900 - grid4(1:1)=char(j1+ichar('A')) - grid4(2:2)=char(j2+ichar('A')) - grid4(3:3)=char(j3+ichar('0')) - grid4(4:4)=char(j4+ichar('0')) - ok=.true. - -900 return -end subroutine to_grid4var - -subroutine to_grid6var(n,grid6,ok) - character*6 grid6 - logical ok - - ok=.false. - j1=n/(18*10*10*24*24) - if (j1.lt.0.or.j1.gt.17) goto 900 - n=n-j1*18*10*10*24*24 - j2=n/(10*10*24*24) - if (j2.lt.0.or.j2.gt.17) goto 900 - n=n-j2*10*10*24*24 - j3=n/(10*24*24) - if (j3.lt.0.or.j3.gt.9) goto 900 - n=n-j3*10*24*24 - j4=n/(24*24) - if (j4.lt.0.or.j4.gt.9) goto 900 - n=n-j4*24*24 - j5=n/24 - if (j5.lt.0.or.j5.gt.23) goto 900 - j6=n-j5*24 - if (j6.lt.0.or.j6.gt.23) goto 900 - grid6(1:1)=char(j1+ichar('A')) - grid6(2:2)=char(j2+ichar('A')) - grid6(3:3)=char(j3+ichar('0')) - grid6(4:4)=char(j4+ichar('0')) - grid6(5:5)=char(j5+ichar('A')) - grid6(6:6)=char(j6+ichar('A')) - ok=.true. - -900 return -end subroutine to_grid6var - -subroutine to_gridvar(n,grid6,ok) - ! 4-, or 6-character grid - character*6 grid6 - logical ok - - ok=.false. - j1=n/(18*10*10*25*25) - if (j1.lt.0.or.j1.gt.17) goto 900 - n=n-j1*18*10*10*25*25 - j2=n/(10*10*25*25) - if (j2.lt.0.or.j2.gt.17) goto 900 - n=n-j2*10*10*25*25 - j3=n/(10*25*25) - if (j3.lt.0.or.j3.gt.9) goto 900 - n=n-j3*10*25*25 - j4=n/(25*25) - if (j4.lt.0.or.j4.gt.9) goto 900 - n=n-j4*25*25 - j5=n/25 - if (j5.lt.0.or.j5.gt.24) goto 900 - j6=n-j5*25 - if (j6.lt.0.or.j6.gt.24) goto 900 - grid6='' - grid6(1:1)=char(j1+ichar('A')) - grid6(2:2)=char(j2+ichar('A')) - grid6(3:3)=char(j3+ichar('0')) - grid6(4:4)=char(j4+ichar('0')) - if (j5.ne.24.or.j6.ne.24) then - grid6(5:5)=char(j5+ichar('A')) - grid6(6:6)=char(j6+ichar('A')) - endif - ok=.true. - -900 return -end subroutine to_gridvar +end function pack77_5_candidate end module packjt77 @@ -3410,7 +2440,6 @@ logical function callok(w) character*(*) w character*1 c1 character*2 pfx - character*3 sfx logical isdig,islet islet(c1)=(ichar(c1).ge.65 .and. ichar(c1).le.90) .or. & @@ -3421,7 +2450,7 @@ logical function callok(w) n=len(trim(w)) if(n.lt.3) return !Must be at lkeast three characters if(w(1:1).eq.'Q') return !Callsigns can't start with Q - + i0=0 do i=n,1,-1 if(isdig(w(i:i))) exit @@ -3431,7 +2460,6 @@ logical function callok(w) if(i0.ne.2 .and. i0.ne.3) return pfx=w(1:i0-1) !Prefix, without call area - sfx=w(i0+1:) !Suffix nlp=0 ndp=0 @@ -3440,16 +2468,18 @@ logical function callok(w) if(isdig(pfx(i:i))) ndp=ndp+1 if(islet(pfx(i:i))) nlp=nlp+1 enddo + if(nlp+ndp.ne.np) return if(nlp.eq.0) return !Prefix must have at least one letter nls=0 - ns=len(trim(sfx)) - do i=1,ns - if(islet(sfx(i:i))) nls=nls+1 + ns=n-i0 + if(ns.lt.1 .or. ns.gt.3) return + do i=i0+1,n + if(islet(w(i:i))) nls=nls+1 enddo if(nls.lt.ns) return !Suffix must be all letters - callok=.true. - + callok=.true. + return end function callok diff --git a/lib/77bit/packjt77_grammar.f90 b/lib/77bit/packjt77_grammar.f90 new file mode 100644 index 00000000..7a2616b0 --- /dev/null +++ b/lib/77bit/packjt77_grammar.f90 @@ -0,0 +1,1530 @@ +module packjt77_grammar + + implicit none + + private :: pack77_gate_staged_call_matches + + integer, parameter :: PACK77_NSEC=86 + integer, parameter :: PACK77_NUSCAN=171 + integer, parameter :: PACK77_WSPR_NZZZ=46656 +! c28 number-space partition: special tokens, then 22-bit hashes, then +! standard callsigns; grid4 indexes above PACK77_MAXGRID4 carry reports. + integer, parameter :: PACK77_NTOKENS=2063592 + integer, parameter :: PACK77_MAX22=4194304 + integer, parameter :: PACK77_MAXGRID4=32400 + character(len=36), parameter :: PACK77_BASE36='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' + character(len=38), parameter :: PACK77_BASE38=' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ/' + character(len=3), parameter :: PACK77_ARRL_SECTIONS(PACK77_NSEC)=(/ & + "AB ","AK ","AL ","AR ","AZ ","BC ","CO ","CT ","DE ","EB ", & + "EMA","ENY","EPA","EWA","GA ","GH ","IA ","ID ","IL ","IN ", & + "KS ","KY ","LA ","LAX","NS ","MB ","MDC","ME ","MI ","MN ", & + "MO ","MS ","MT ","NC ","ND ","NE ","NFL","NH ","NL ","NLI", & + "NM ","NNJ","NNY","TER","NTX","NV ","OH ","OK ","ONE","ONN", & + "ONS","OR ","ORG","PAC","PR ","QC ","RI ","SB ","SC ","SCV", & + "SD ","SDG","SF ","SFL","SJV","SK ","SNJ","STX","SV ","TN ", & + "UT ","VA ","VI ","VT ","WCF","WI ","WMA","WNY","WPA","WTX", & + "WV ","WWA","WY ","DX ","PE ","NB " /) + character(len=3), parameter :: PACK77_RTTY_MULTIPLIERS(PACK77_NUSCAN)=(/ & + "AL ","AK ","AZ ","AR ","CA ","CO ","CT ","DE ","FL ","GA ", & + "HI ","ID ","IL ","IN ","IA ","KS ","KY ","LA ","ME ","MD ", & + "MA ","MI ","MN ","MS ","MO ","MT ","NE ","NV ","NH ","NJ ", & + "NM ","NY ","NC ","ND ","OH ","OK ","OR ","PA ","RI ","SC ", & + "SD ","TN ","TX ","UT ","VT ","VA ","WA ","WV ","WI ","WY ", & + "NB ","NS ","QC ","ON ","MB ","SK ","AB ","BC ","NWT","NF ", & + "LB ","NU ","YT ","PEI","DC ","DR ","FR ","GD ","GR ","OV ", & + "ZH ","ZL ","X01","X02","X03","X04","X05","X06","X07","X08", & + "X09","X10","X11","X12","X13","X14","X15","X16","X17","X18", & + "X19","X20","X21","X22","X23","X24","X25","X26","X27","X28", & + "X29","X30","X31","X32","X33","X34","X35","X36","X37","X38", & + "X39","X40","X41","X42","X43","X44","X45","X46","X47","X48", & + "X49","X50","X51","X52","X53","X54","X55","X56","X57","X58", & + "X59","X60","X61","X62","X63","X64","X65","X66","X67","X68", & + "X69","X70","X71","X72","X73","X74","X75","X76","X77","X78", & + "X79","X80","X81","X82","X83","X84","X85","X86","X87","X88", & + "X89","X90","X91","X92","X93","X94","X95","X96","X97","X98", & + "X99" /) + integer, parameter :: PACK77_TYPE12_SUFFIX_NONE=0 + integer, parameter :: PACK77_TYPE12_SUFFIX_R=1 + integer, parameter :: PACK77_TYPE12_SUFFIX_P=2 + + + type pack77_type12_call_source + character(len=13) :: c28_token='' + integer :: suffix=PACK77_TYPE12_SUFFIX_NONE + logical :: is_hash=.false. + logical :: is_cq=.false. + end type pack77_type12_call_source + + type pack77_type12_source + type(pack77_type12_call_source) :: call_1 + type(pack77_type12_call_source) :: call_2 + integer :: ir=0 + integer :: igrid4=0 + integer :: i3=1 + end type pack77_type12_source + + type pack77_dxpedition_source + character(len=13) :: call_1='',call_2='',hash_token='' + integer :: n5=0 + end type pack77_dxpedition_source + + type pack77_field_day_source + character(len=13) :: call_1='',call_2='' + integer :: ir=0,ntx=0,nclass=0,isec=-1 + end type pack77_field_day_source + + type pack77_wspr_source + character(len=13) :: call_token='',hash_token='' + character(len=6) :: base_call='',grid6='' + character(len=4) :: grid4='' + integer :: subtype=0,idbm=0,npfx=0 + end type pack77_wspr_source + + type pack77_type3_source + character(len=13) :: call_1='',call_2='' + integer :: itu=0,ir=0,irpt=-1,nexch=0 + end type pack77_type3_source + + type pack77_type4_source + character(len=13) :: hash_token='' + character(len=11) :: c11='' + integer :: icq=0,iflip=0,itail=0 + end type pack77_type4_source + + type pack77_type5_source + character(len=13) :: hash_token_1='',hash_token_2='' + character(len=6) :: grid6='' + integer :: ir=0,irpt=0,iserial=0 + end type pack77_type5_source + contains + +integer function pack77_arrl_section_index(section) result(isec) + character(len=*), intent(in) :: section + integer :: i + + isec=-1 + if(len(section).lt.3) return + do i=1,PACK77_NSEC + if(PACK77_ARRL_SECTIONS(i).eq.section(1:3)) then + isec=i + return + endif + enddo + + return +end function pack77_arrl_section_index + +character(len=3) function pack77_arrl_section_name(isec) result(section) + integer, intent(in) :: isec + + section=' ' + if(isec.ge.1 .and. isec.le.PACK77_NSEC) section=PACK77_ARRL_SECTIONS(isec) + + return +end function pack77_arrl_section_name + +integer function pack77_rtty_multiplier_index(multiplier) result(imult) + character(len=*), intent(in) :: multiplier + integer :: i + + imult=-1 + if(len(multiplier).lt.3) return + do i=1,PACK77_NUSCAN + if(PACK77_RTTY_MULTIPLIERS(i).eq.multiplier(1:3)) then + imult=i + return + endif + enddo + + return +end function pack77_rtty_multiplier_index + +character(len=3) function pack77_rtty_multiplier_name(imult) result(multiplier) + integer, intent(in) :: imult + + multiplier=' ' + if(imult.ge.1 .and. imult.le.PACK77_NUSCAN) & + multiplier=PACK77_RTTY_MULTIPLIERS(imult) + + return +end function pack77_rtty_multiplier_name + +integer function pack77_report_index_from_snr(isnr) result(irpt) + integer, intent(in) :: isnr + + if(isnr.lt.-50 .or. isnr.gt.50) then + irpt=-1 + return + endif +! irpt 1..85 covers the legacy -30..+50 dB range; -50..-31 dB fold to +! irpt 86..105 above the old top so pre-extension decoders keep their mapping. + irpt=isnr + if(irpt.ge.-50 .and. irpt.le.-31) irpt=irpt+101 + irpt=irpt+35 + + return +end function pack77_report_index_from_snr + +integer function pack77_snr_from_report_index(irpt) result(isnr) + integer, intent(in) :: irpt + + isnr=irpt-35 + if(isnr.gt.50) isnr=isnr-101 + + return +end function pack77_snr_from_report_index + +character(len=3) function pack77_format_snr_report(isnr) result(crpt) + integer, intent(in) :: isnr + + write(crpt,'(i3.2)') isnr + if(crpt(1:1).eq.' ') crpt(1:1)='+' + + return +end function pack77_format_snr_report + +integer function pack77_rtty_report_index(report) result(irpt) + character(len=*), intent(in) :: report + + irpt=-1 + if(len_trim(report).ne.3) return + if(report(1:1).ne.'5') return + if(report(2:2).lt.'2' .or. report(2:2).gt.'9') return + if(report(3:3).ne.'9') return + irpt=iachar(report(2:2))-iachar('2') +end function pack77_rtty_report_index + +character(len=3) function pack77_format_rtty_report(irpt) result(crpt) + integer, intent(in) :: irpt + + write(crpt,1001) irpt+2 +1001 format('5',i1,'9') + + return +end function pack77_format_rtty_report + +character(len=6) function pack77_format_vhf_exchange(irpt,iserial) result(cexch) + integer, intent(in) :: irpt,iserial + + write(cexch,1002) 52+irpt,iserial +1002 format(i2,i4.4) + + return +end function pack77_format_vhf_exchange + +character(len=37) function pack77_format_type3_message(itu,ir,call_1,call_2,crpt,exchange) result(msg) + integer, intent(in) :: itu,ir + character(len=*), intent(in) :: call_1,call_2,crpt,exchange + character(len=37) :: body + + body=pack77_format_exchange_message(call_1,call_2,ir,crpt//' '//exchange,.true.) + msg=body + if(itu.eq.1) msg='TU; '//trim(body) + + return +end function pack77_format_type3_message + +integer function pack77_type3_message_len(itu,ir,call_1_len,call_2_len,exchange_len) result(nmsg) + integer, intent(in) :: itu,ir,call_1_len,call_2_len,exchange_len + + nmsg=call_1_len + 1 + call_2_len + 1 + 3 + 1 + exchange_len + if(ir.eq.1) nmsg=call_1_len + 1 + call_2_len + 3 + 3 + 1 + exchange_len + if(itu.eq.1) nmsg=nmsg+4 +end function pack77_type3_message_len + +integer function pack77_type3_display_call_len(n28,call_text) result(ncall) + integer, intent(in) :: n28 + character(len=*), intent(in) :: call_text + ncall=len_trim(call_text) + if(n28.ge.PACK77_NTOKENS .and. n28.lt.PACK77_NTOKENS+PACK77_MAX22) then + if(ncall.gt.11) then + ncall=-1 + else + ncall=ncall+2 + endif + endif +end function pack77_type3_display_call_len + +logical function pack77_type3_display_fits(itu,ir,n28a,n28b,call_1,call_2,exchange) result(ok) + integer, intent(in) :: itu,ir,n28a,n28b + character(len=*), intent(in) :: call_1,call_2,exchange + integer :: ncall_1,ncall_2,nmsg + + ok=.false. + ncall_1=pack77_type3_display_call_len(n28a,call_1) + if(ncall_1.lt.0) return + ncall_2=pack77_type3_display_call_len(n28b,call_2) + if(ncall_2.lt.0) return + nmsg=pack77_type3_message_len(itu,ir,ncall_1,ncall_2,len_trim(exchange)) + ok=nmsg.le.37 +end function pack77_type3_display_fits + +logical function pack77_type3_render_fits(itu,ir,call_1,call_2,exchange) result(ok) + integer, intent(in) :: itu,ir + character(len=*), intent(in) :: call_1,call_2,exchange + integer :: ncall_1,ncall_2,nmsg + + ok=.false. + ncall_1=len_trim(call_1) + ncall_2=len_trim(call_2) + if(call_1(1:1).eq.'<' .and. index(call_1,'>').lt.1) return + if(call_2(1:1).eq.'<' .and. index(call_2,'>').lt.1) return + nmsg=pack77_type3_message_len(itu,ir,ncall_1,ncall_2,len_trim(exchange)) + ok=nmsg.le.37 +end function pack77_type3_render_fits + +character(len=37) function pack77_format_qso_tail(call_1,call_2,itail) result(msg) + character(len=*), intent(in) :: call_1,call_2 + integer, intent(in) :: itail + + msg=trim(call_1)//' '//trim(call_2) + if(itail.eq.1) msg=trim(call_1)//' '//trim(call_2)//' RRR' + if(itail.eq.2) msg=trim(call_1)//' '//trim(call_2)//' RR73' + if(itail.eq.3) msg=trim(call_1)//' '//trim(call_2)//' 73' + + return +end function pack77_format_qso_tail + +integer function pack77_qso_tail_index(token) result(itail) + character(len=*), intent(in) :: token + + itail=0 + if(trim(token).eq.'RRR') itail=1 + if(trim(token).eq.'RR73') itail=2 + if(trim(token).eq.'73') itail=3 +end function pack77_qso_tail_index + +integer function pack77_type12_irpt_from_tail(itail) result(irpt) + integer, intent(in) :: itail + + irpt=0 + if(itail.ge.1 .and. itail.le.3) irpt=itail+1 +end function pack77_type12_irpt_from_tail + +integer function pack77_tail_from_type12_irpt(irpt) result(itail) + integer, intent(in) :: irpt + + itail=0 + if(irpt.ge.2 .and. irpt.le.4) itail=irpt-1 +end function pack77_tail_from_type12_irpt + +integer function pack77_type4_nrpt_from_tail(itail) result(nrpt) + integer, intent(in) :: itail + + nrpt=0 + if(itail.ge.1 .and. itail.le.3) nrpt=itail +end function pack77_type4_nrpt_from_tail + +integer function pack77_tail_from_type4_nrpt(nrpt) result(itail) + integer, intent(in) :: nrpt + + itail=0 + if(nrpt.ge.1 .and. nrpt.le.3) itail=nrpt +end function pack77_tail_from_type4_nrpt + +character(len=37) function pack77_format_exchange_message(call_1,call_2,ir,exchange,reply_has_separate_token) result(msg) + character(len=*), intent(in) :: call_1,call_2,exchange + integer, intent(in) :: ir + logical, intent(in) :: reply_has_separate_token + + msg=trim(call_1)//' '//trim(call_2)//' '//exchange + if(ir.eq.1) then + if(reply_has_separate_token) then + msg=trim(call_1)//' '//trim(call_2)//' R '//exchange + else + msg=trim(call_1)//' '//trim(call_2)//' R'//exchange + endif + endif + + return +end function pack77_format_exchange_message + +logical function pack77_is_digit_char(c) result(is_digit) + character(len=1), intent(in) :: c + + is_digit=c.ge.'0' .and. c.le.'9' + + return +end function pack77_is_digit_char + +logical function pack77_is_letter_char(c) result(is_letter) + character(len=1), intent(in) :: c + + is_letter=c.ge.'A' .and. c.le.'Z' + + return +end function pack77_is_letter_char + +logical function pack77_all_digits(token) result(ok) + character(len=*), intent(in) :: token + integer :: i,n + + ok=.false. + n=len_trim(token) + if(n.lt.1) return + do i=1,n + if(.not.pack77_is_digit_char(token(i:i))) return + enddo + ok=.true. +end function pack77_all_digits + +subroutine pack77_parse_uint(token,value,ok) + character(len=*), intent(in) :: token + integer, intent(out) :: value + logical, intent(out) :: ok + + value=0 + ok=pack77_all_digits(token) + if(.not.ok) return + read(token,*,err=10) value + return +10 ok=.false. +end subroutine pack77_parse_uint + +subroutine pack77_parse_signed_report(token,value,ok) + character(len=*), intent(in) :: token + integer, intent(out) :: value + logical, intent(out) :: ok + + value=0 + ok=.false. + if(len_trim(token).ne.3) return + if(token(1:1).ne.'+' .and. token(1:1).ne.'-') return + if(.not.pack77_is_digit_char(token(2:2))) return + if(.not.pack77_is_digit_char(token(3:3))) return + read(token,*,err=10) value + ok=.true. + return +10 ok=.false. +end subroutine pack77_parse_signed_report + +subroutine pack77_parse_dxpedition_report(token,n5,ok) + character(len=*), intent(in) :: token + integer, intent(out) :: n5 + integer :: isnr + logical, intent(out) :: ok + + n5=0 + call pack77_parse_signed_report(token,isnr,ok) + if(.not.ok) return + if(isnr.lt.-30 .or. isnr.gt.32) then + ok=.false. + return + endif + n5=(isnr+30)/2 + ok=(2*n5-30).eq.isnr +end subroutine pack77_parse_dxpedition_report + +subroutine pack77_parse_type3_serial(token,nserial,ok) + character(len=*), intent(in) :: token + integer, intent(out) :: nserial + logical, intent(out) :: ok + + nserial=0 + ok=.false. + if(len_trim(token).ne.4) return + call pack77_parse_uint(token,nserial,ok) + if(.not.ok) return + ok=nserial.ge.1 .and. nserial.le.7999 +end subroutine pack77_parse_type3_serial + +subroutine pack77_parse_wspr_dbm(token,idbm,ok) + character(len=*), intent(in) :: token + integer, intent(out) :: idbm + integer :: dbm,decoded_dbm + logical, intent(out) :: ok + + idbm=0 + call pack77_parse_uint(token,dbm,ok) + if(.not.ok) return + if(len_trim(token).gt.1 .and. token(1:1).eq.'0') then + ok=.false. + return + endif + if(dbm.lt.0 .or. dbm.gt.60) then + ok=.false. + return + endif + idbm=nint(0.3*dbm) + decoded_dbm=nint(idbm*10.0/3.0) + ok=decoded_dbm.eq.dbm +end subroutine pack77_parse_wspr_dbm + +subroutine pack77_parse_field_day_exchange(token,ntx,nclass,ok) + character(len=*), intent(in) :: token + integer, intent(out) :: ntx,nclass + integer :: n + logical, intent(out) :: ok + + ntx=-1 + nclass=-1 + ok=.false. + n=len_trim(token) + if(n.lt.2) return + if(n.gt.2 .and. token(1:1).eq.'0') return + call pack77_parse_uint(token(1:n-1),ntx,ok) + if(.not.ok) return + if(ntx.lt.1 .or. ntx.gt.32) then + ok=.false. + return + endif + nclass=ichar(token(n:n))-ichar('A') + ok=nclass.ge.0 .and. nclass.le.5 +end subroutine pack77_parse_field_day_exchange + +logical function pack77_valid_hash_call_token(token) result(ok) + character(len=*), intent(in) :: token + integer :: n + + ok=.false. + n=len_trim(token) + if(n.lt.5 .or. n.gt.13) return + if(token(1:1).ne.'<' .or. token(n:n).ne.'>') return + if(index(token(2:n-1),'<').gt.0) return + if(index(token(2:n-1),'>').gt.0) return + ok=.true. +end function pack77_valid_hash_call_token + +logical function pack77_valid_cq_modifier(token) result(ok) + character(len=*), intent(in) :: token + integer :: i,n,nletters,ndigits + + ok=.false. + n=len_trim(token) + if(n.lt.1 .or. n.gt.4) return + nletters=0 + ndigits=0 + do i=1,n + if(token(i:i).ge.'A' .and. token(i:i).le.'Z') then + nletters=nletters+1 + else if(pack77_is_digit_char(token(i:i))) then + ndigits=ndigits+1 + else + return + endif + enddo + ok=(ndigits.eq.3 .and. nletters.eq.0) .or. & + (nletters.ge.1 .and. nletters.le.4 .and. ndigits.eq.0) +end function pack77_valid_cq_modifier + +pure subroutine pack77_split_source_tokens(msg,max_words,nwords,w,ok) + character(len=*), intent(in) :: msg + integer, intent(in) :: max_words + integer, intent(out) :: nwords + character(len=*), intent(out) :: w(:) + logical, intent(out) :: ok + character(len=1) :: c,c0 + integer :: i,n,iz + + nwords=0 + n=0 + c0=' ' + w=' ' + ok=.true. + iz=len_trim(msg) + do i=1,iz + c=msg(i:i) + if(ichar(c).eq.0) c=' ' + if(c.ge.'a' .and. c.le.'z') c=char(ichar(c)-32) + if(c.eq.' ' .and. c0.eq.' ') cycle + if(c.ne.' ' .and. c0.eq.' ') then + nwords=nwords+1 + n=0 + if(nwords.gt.max_words .or. nwords.gt.size(w)) then + ok=.false. + return + endif + endif + if(c.ne.' ') then + n=n+1 + if(n.gt.len(w(1))) then + ok=.false. + return + endif + w(nwords)(n:n)=c + endif + c0=c + enddo +end subroutine pack77_split_source_tokens + +pure subroutine pack77_gate_message_checks(input_msg,decoded_msg,staged_calls, & + nstaged,messages_match,resolved_render_fits) + character(len=*), intent(in) :: input_msg,decoded_msg + character(len=13), intent(in) :: staged_calls(:) + integer, intent(in) :: nstaged + logical, intent(out) :: messages_match,resolved_render_fits + character(len=37) :: input_tokens(19),decoded_tokens(19),input_body + integer :: input_nwords,decoded_nwords,i,nrender,nbody + logical :: input_ok,decoded_ok,token_match + + messages_match=.false. + resolved_render_fits=.false. + call pack77_split_source_tokens(input_msg,19,input_nwords,input_tokens,input_ok) + call pack77_split_source_tokens(decoded_msg,19,decoded_nwords,decoded_tokens, & + decoded_ok) + if(.not.input_ok .or. .not.decoded_ok) return + if(input_nwords.ne.decoded_nwords) return + + messages_match=.true. + resolved_render_fits=.true. +! nrender is the display length after hash resolution: a decoded <...> token +! renders as the staged call body plus its two brackets, and separators are +! counted only between tokens. + nrender=0 + do i=1,input_nwords + if(i.gt.1) nrender=nrender+1 + token_match=.false. + if(trim(decoded_tokens(i)).eq.trim(input_tokens(i))) then + token_match=.true. + nrender=nrender+len_trim(decoded_tokens(i)) + else + input_body=' ' + input_body='<'//trim(input_tokens(i))//'>' + if(trim(decoded_tokens(i)).eq.trim(input_body)) then + token_match=.true. + nrender=nrender+len_trim(decoded_tokens(i)) + else if(trim(decoded_tokens(i)).eq.'<...>') then + input_body=input_tokens(i) + nbody=len_trim(input_body) + if(nbody.ge.2 .and. input_body(1:1).eq.'<' .and. & + input_body(nbody:nbody).eq.'>') then + input_body=' ' + if(nbody.gt.2) input_body=input_tokens(i)(2:nbody-1) + endif + token_match=pack77_gate_staged_call_matches(input_body,staged_calls, & + nstaged) + if(len_trim(input_body).gt.11) resolved_render_fits=.false. + nrender=nrender+len_trim(input_body)+2 + else + nrender=nrender+len_trim(decoded_tokens(i)) + endif + endif + if(.not.token_match) messages_match=.false. + if(trim(decoded_tokens(i)).eq.'<...>' .and. .not.token_match) & + resolved_render_fits=.false. + if(nrender.gt.37) resolved_render_fits=.false. + enddo +end subroutine pack77_gate_message_checks + +! Canonicalization accepted by the encode/decode gate is exhaustive: +! 1. The tokenizer folds case and collapses blanks before parsing. +! 2. A compound/nonstandard call in a c28 slot transmits as a hash and decodes bracketed. +! 3. A bracketed hash decodes as <...> when no hash-table call is available and the staged source call matches. +! 4. Type 4 CQ accepts only unbracketed c11 CQ targets; bracketed CQ targets are rejected and decoded CQ targets stay unbracketed. +! 5. DX-macro expansion is performed by callers through pack77_options before this gate. +! Any other source-vs-decode difference is rejected by this gate. +pure logical function pack77_gate_messages_match(input_msg,decoded_msg, & + staged_calls,nstaged) result(ok) + character(len=*), intent(in) :: input_msg,decoded_msg + character(len=13), intent(in) :: staged_calls(:) + integer, intent(in) :: nstaged + logical :: resolved_render_fits + + call pack77_gate_message_checks(input_msg,decoded_msg,staged_calls,nstaged, & + ok,resolved_render_fits) +end function pack77_gate_messages_match + +pure logical function pack77_gate_staged_call_matches(input_body,staged_calls, & + nstaged) result(ok) + character(len=*), intent(in) :: input_body + character(len=13), intent(in) :: staged_calls(:) + integer, intent(in) :: nstaged + integer :: i,n + + ok=.false. + n=min(max(nstaged,0),size(staged_calls)) + do i=1,n + if(trim(staged_calls(i)).eq.trim(input_body)) then + ok=.true. + return + endif + enddo +end function pack77_gate_staged_call_matches + +pure logical function pack77_gate_resolved_render_fits(input_msg,decoded_msg, & + staged_calls,nstaged) result(ok) + character(len=*), intent(in) :: input_msg,decoded_msg + character(len=13), intent(in) :: staged_calls(:) + integer, intent(in) :: nstaged + logical :: messages_match + + call pack77_gate_message_checks(input_msg,decoded_msg,staged_calls,nstaged, & + messages_match,ok) +end function pack77_gate_resolved_render_fits + +logical function pack77_c28_standard_shape(token) result(ok) + character(len=*), intent(in) :: token + integer :: i,n,iarea,npdig,nplet,nslet + character(len=1) :: c + + ok=.false. + n=len_trim(token) + if(n.lt.3 .or. n.gt.6) return + do i=1,n + c=token(i:i) + if(.not.((c.ge.'A' .and. c.le.'Z') .or. pack77_is_digit_char(c))) return + enddo + + iarea=0 + do i=n,2,-1 + if(pack77_is_digit_char(token(i:i))) then + iarea=i + exit + endif + enddo + if(iarea.ne.2 .and. iarea.ne.3) return + + npdig=0 + nplet=0 + do i=1,iarea-1 + c=token(i:i) + if(pack77_is_digit_char(c)) then + npdig=npdig+1 + else if(c.ge.'A' .and. c.le.'Z') then + nplet=nplet+1 + else + return + endif + enddo + if(nplet.eq.0 .or. npdig.ge.iarea-1) return + + nslet=0 + do i=iarea+1,n + c=token(i:i) + if(c.ge.'A' .and. c.le.'Z') then + nslet=nslet+1 + else + return + endif + enddo + ok=nslet.le.3 +end function pack77_c28_standard_shape + +logical function pack77_type12_standard_call(token) result(ok) + character(len=*), intent(in) :: token + + ok=.false. + if(.not.pack77_c28_standard_shape(token)) return + ! Q-prefix rejection is a source-grammar policy, not a c28 wire shape fact. + if(token(1:1).eq.'Q') return + ok=.true. +end function pack77_type12_standard_call + + +subroutine pack77_parse_type12_call(token,allow_special,call_source,ok) + character(len=*), intent(in) :: token + logical, intent(in) :: allow_special + type(pack77_type12_call_source), intent(out) :: call_source + logical, intent(out) :: ok + character(len=37) :: base,modifier + integer :: n,slash + + call_source=pack77_type12_call_source() + ok=.false. + n=len_trim(token) + if(n.lt.1 .or. n.gt.13) return + + if(allow_special) then + if(trim(token).eq.'CQ') then + call_source%c28_token='CQ' + call_source%is_cq=.true. + ok=.true. + return + endif + if(trim(token).eq.'DE' .or. trim(token).eq.'QRZ') then + call_source%c28_token=trim(token) + ok=.true. + return + endif + if(n.ge.4 .and. token(1:3).eq.'CQ_') then + modifier=' ' + modifier=token(4:n) + if(.not.pack77_valid_cq_modifier(modifier)) return + call_source%c28_token=trim(token) + call_source%is_cq=.true. + ok=.true. + return + endif + endif + + if(token(1:1).eq.'<' .or. index(token(1:n),'>').gt.0) then + if(.not.pack77_valid_hash_call_token(token)) return + call_source%c28_token=trim(token) + call_source%is_hash=.true. + ok=.true. + return + endif + + slash=index(token(1:n),'/') + base=' ' + if(slash.gt.0) then + if(slash.lt.2 .or. slash.ne.n-1) return + if(index(token(slash+1:n),'/').gt.0) return + if(token(slash:n).eq.'/R') then + call_source%suffix=PACK77_TYPE12_SUFFIX_R + else if(token(slash:n).eq.'/P') then + call_source%suffix=PACK77_TYPE12_SUFFIX_P + else + return + endif + base=token(1:slash-1) + else + base=token(1:n) + endif + + if(.not.pack77_type12_standard_call(base)) return + call_source%c28_token=trim(base) + ok=.true. +end subroutine pack77_parse_type12_call + +logical function pack77_type12_call_has_suffix(call_source) result(ok) + type(pack77_type12_call_source), intent(in) :: call_source + + ok=call_source%suffix.ne.PACK77_TYPE12_SUFFIX_NONE +end function pack77_type12_call_has_suffix + +subroutine pack77_parse_type12_source(msg,source,ok) + character(len=*), intent(in) :: msg + type(pack77_type12_source), intent(out) :: source + logical, intent(out) :: ok + character(len=37) :: w(5),call_1_token,tail + integer :: nwords,call_2_index,tail_start,ntail,itail,isnr,irpt + logical :: split_ok,call_ok,tail_is_grid + + source=pack77_type12_source() + ok=.false. + call pack77_split_source_tokens(msg,5,nwords,w,split_ok) + if(.not.split_ok) return + if(nwords.lt.2) return + + call_1_token=w(1) + call_2_index=2 + tail_start=3 + if(trim(w(1)).eq.'CQ' .and. nwords.ge.3 .and. & + pack77_valid_cq_modifier(w(2))) then + call_1_token=' ' + call_1_token='CQ_'//trim(w(2)) + call_2_index=3 + tail_start=4 + endif + + call pack77_parse_type12_call(call_1_token,.true.,source%call_1,call_ok) + if(.not.call_ok) return + call pack77_parse_type12_call(w(call_2_index),.false.,source%call_2,call_ok) + if(.not.call_ok) return + if(source%call_1%is_cq .and. source%call_2%is_hash) return + if(source%call_1%is_hash .and. & + (pack77_type12_call_has_suffix(source%call_2) .or. & + index(source%call_2%c28_token,'/').gt.0)) return + if(source%call_2%is_hash .and. & + (pack77_type12_call_has_suffix(source%call_1) .or. & + index(source%call_1%c28_token,'/').gt.0)) return + + if(source%call_1%suffix.eq.PACK77_TYPE12_SUFFIX_R .and. & + source%call_2%suffix.eq.PACK77_TYPE12_SUFFIX_P) return + if(source%call_1%suffix.eq.PACK77_TYPE12_SUFFIX_P .and. & + source%call_2%suffix.eq.PACK77_TYPE12_SUFFIX_R) return + + source%i3=1 + if(source%call_1%suffix.eq.PACK77_TYPE12_SUFFIX_P .or. & + source%call_2%suffix.eq.PACK77_TYPE12_SUFFIX_P) source%i3=2 + + ntail=nwords-tail_start+1 + if(ntail.lt.0 .or. ntail.gt.2) return + if(ntail.eq.0) then + source%ir=0 + source%igrid4=PACK77_MAXGRID4+1 + ok=.true. + return + endif + + if(ntail.eq.2) then + if(trim(w(tail_start)).ne.'R') return + if(source%call_1%is_cq) return + if(len_trim(w(tail_start+1)).ne.4) return + if(.not.pack77_is_grid4(w(tail_start+1))) return + source%ir=1 + source%igrid4=pack77_grid4_index(w(tail_start+1)(1:4)) + ok=.true. + return + endif + + tail=w(tail_start) + tail_is_grid=.false. + if(len_trim(tail).eq.4) tail_is_grid=pack77_is_grid4(tail) + if(tail_is_grid) then + source%ir=0 + source%igrid4=pack77_grid4_index(tail(1:4)) + ok=.true. + return + endif + if(source%call_1%is_cq) return + + if(tail(1:1).eq.'+' .or. tail(1:1).eq.'-') then + call pack77_parse_signed_report(tail,isnr,ok) + if(.not.ok) return + irpt=pack77_report_index_from_snr(isnr) + if(irpt.lt.1) then + ok=.false. + return + endif + source%ir=0 + source%igrid4=PACK77_MAXGRID4+irpt + ok=.true. + return + endif + if(tail(1:2).eq.'R+' .or. tail(1:2).eq.'R-') then + call pack77_parse_signed_report(tail(2:),isnr,ok) + if(.not.ok) return + irpt=pack77_report_index_from_snr(isnr) + if(irpt.lt.1) then + ok=.false. + return + endif + source%ir=1 + source%igrid4=PACK77_MAXGRID4+irpt + ok=.true. + return + endif + + itail=pack77_qso_tail_index(tail) + if(itail.lt.1) return + source%ir=0 + source%igrid4=PACK77_MAXGRID4+pack77_type12_irpt_from_tail(itail) + ok=.true. +end subroutine pack77_parse_type12_source +logical function pack77_valid_c28_call_slot(token) result(ok) + character(len=*), intent(in) :: token + integer :: i,n,slash + + ok=.false. + n=len_trim(token) + if(n.lt.3 .or. n.gt.13) return + if(pack77_valid_hash_call_token(token)) return + do i=1,n + if(.not.((token(i:i).ge.'A' .and. token(i:i).le.'Z') .or. & + pack77_is_digit_char(token(i:i)) .or. token(i:i).eq.'/')) return + enddo + if(pack77_type12_standard_call(token)) then + ok=.true. + return + endif + slash=index(token(1:n),'/') + if(slash.lt.2 .or. slash.ge.n) return + if(index(token(slash+1:n),'/').gt.0) return + ok=.true. +end function pack77_valid_c28_call_slot + +subroutine pack77_parse_dxpedition_source(msg,source,ok,matched_shape) +! DXpedition messages encode two c28 calls, a 10-bit hashed callsign, and a +! report in the fixed "CALL RR73; CALL <HASH> REPORT" token shape. + character(len=*), intent(in) :: msg + type(pack77_dxpedition_source), intent(out) :: source + logical, intent(out) :: ok + logical, intent(out) :: matched_shape + character(len=37) :: w(5) + integer :: nwords + logical :: split_ok + + source=pack77_dxpedition_source() + ok=.false. + matched_shape=.false. + call pack77_split_source_tokens(msg,5,nwords,w,split_ok) + if(.not.split_ok) return + if(nwords.ne.5) return + if(trim(w(2)).ne.'RR73;') return + matched_shape=.true. + if(.not.pack77_valid_c28_call_slot(w(1))) return + if(.not.pack77_valid_c28_call_slot(w(3))) return + if(.not.pack77_valid_hash_call_token(w(4))) return + call pack77_parse_dxpedition_report(w(5),source%n5,ok) + if(.not.ok) return + source%call_1=trim(w(1)) + source%call_2=trim(w(3)) + source%hash_token=trim(w(4)) +end subroutine pack77_parse_dxpedition_source + +subroutine pack77_parse_field_day_source(msg,source,ok,matched_shape) + character(len=*), intent(in) :: msg + type(pack77_field_day_source), intent(out) :: source + logical, intent(out) :: ok + logical, intent(out) :: matched_shape + character(len=37) :: w(5),section + integer :: nwords,exchange_index,section_index + logical :: split_ok + + source=pack77_field_day_source() + ok=.false. + matched_shape=.false. + call pack77_split_source_tokens(msg,5,nwords,w,split_ok) + if(.not.split_ok) return + if(nwords.lt.4 .or. nwords.gt.5) return + section_index=nwords + source%isec=pack77_arrl_section_index(w(section_index)) + if(source%isec.eq.-1) return + section=pack77_arrl_section_name(source%isec) + if(len_trim(w(section_index)).ne.len_trim(section)) return + matched_shape=.true. + if(.not.pack77_valid_c28_call_slot(w(1))) return + if(.not.pack77_valid_c28_call_slot(w(2))) return + if(nwords.eq.5) then + if(trim(w(3)).ne.'R') return + source%ir=1 + endif + exchange_index=nwords-1 + call pack77_parse_field_day_exchange(w(exchange_index), & + source%ntx,source%nclass,ok) + if(.not.ok) return + source%call_1=trim(w(1)) + source%call_2=trim(w(2)) + ok=.true. +end subroutine pack77_parse_field_day_source + +subroutine pack77_parse_wspr_source(msg,source,ok,matched_shape) + character(len=*), intent(in) :: msg + type(pack77_wspr_source), intent(out) :: source + logical, intent(out) :: ok + logical, intent(out) :: matched_shape + character(len=37) :: w(3),base + character(len=3) :: cpfx + integer :: nwords,m1,m2,slash + logical :: split_ok,is_prefix,grid4_ok + + source=pack77_wspr_source() + ok=.false. + matched_shape=.false. + call pack77_split_source_tokens(msg,3,nwords,w,split_ok) + if(.not.split_ok) return + if(nwords.lt.2 .or. nwords.gt.3) return + m1=len_trim(w(1)) + m2=len_trim(w(2)) + + grid4_ok=.false. + if(nwords.eq.3) then + grid4_ok=m2.eq.4 .and. pack77_is_grid4(w(2)) + matched_shape=grid4_ok .and. pack77_all_digits(w(3)) + else if(nwords.eq.2) then + matched_shape=index(w(1)(1:m1),'/').gt.0 .and. pack77_all_digits(w(2)) + endif + + if(nwords.eq.3) then + if(.not.pack77_type12_standard_call(w(1))) return + if(.not.grid4_ok) return + call pack77_parse_wspr_dbm(w(3),source%idbm,ok) + if(.not.ok) return + source%subtype=1 + source%call_token=trim(w(1)) + source%grid4=w(2)(1:4) + ok=.true. + return + endif + + if(m1.ge.5 .and. m1.le.10 .and. m2.le.2) then + slash=index(w(1)(1:m1),'/') + if(slash.ge.2 .and. slash.lt.m1 .and. & + index(w(1)(slash+1:m1),'/').eq.0) then + base=' ' + if(slash.le.4) then + base=w(1)(slash+1:m1) + else + base=w(1)(1:slash-1) + endif + if(pack77_type12_standard_call(base)) then + call pack77_parse_wspr_dbm(w(2),source%idbm,ok) + if(.not.ok) return + source%npfx=pack77_wspr_affix_index(w(1),slash,m1,ok) + if(.not.ok) return + call pack77_wspr_affix_text(source%npfx,cpfx,is_prefix,ok) + if(.not.ok) return + ok=.false. + cpfx=adjustl(cpfx) + if(slash.le.4) then + if(.not.is_prefix) return + if(trim(cpfx).ne.w(1)(1:slash-1)) return + else + if(is_prefix) return + if(trim(cpfx).ne.w(1)(slash+1:m1)) return + endif + source%subtype=2 + source%call_token=trim(w(1)) + source%base_call=trim(base) + ok=.true. + return + endif + endif + endif + + if(m1.ge.5 .and. m1.le.12 .and. m2.le.6) then + if(.not.pack77_valid_hash_call_token(w(1))) return + if(.not.pack77_is_grid6(w(2),.true.)) return + source%subtype=3 + source%hash_token=trim(w(1)) + source%grid6=w(2)(1:6) + ok=.true. + endif +end subroutine pack77_parse_wspr_source + +subroutine pack77_parse_type3_source(msg,source,ok,matched_shape) + character(len=*), intent(in) :: msg + type(pack77_type3_source), intent(out) :: source + logical, intent(out) :: ok + logical, intent(out) :: matched_shape + character(len=37) :: w(6),exchange + integer :: nwords,report_index,exchange_index,nserial,imult + logical :: split_ok,serial_ok + + source=pack77_type3_source() + ok=.false. + matched_shape=.false. + call pack77_split_source_tokens(msg,6,nwords,w,split_ok) + if(.not.split_ok) return + if(nwords.lt.4 .or. nwords.gt.6) return + + if(trim(w(1)).eq.'TU;') source%itu=1 + if(trim(w(3+source%itu)).eq.'R') source%ir=1 + report_index=3+source%itu+source%ir + if(report_index.le.nwords) & + matched_shape=pack77_rtty_report_index(w(report_index)).ge.0 + if(.not.pack77_valid_c28_call_slot(w(1+source%itu))) return + if(.not.pack77_valid_c28_call_slot(w(2+source%itu))) return + source%call_1=trim(w(1+source%itu)) + source%call_2=trim(w(2+source%itu)) + + exchange_index=report_index+1 + if(exchange_index.ne.nwords) return + + source%irpt=pack77_rtty_report_index(w(report_index)) + if(source%irpt.lt.0) return + + call pack77_parse_type3_serial(w(exchange_index),nserial,serial_ok) + if(serial_ok) then + source%nexch=nserial + ok=.true. + return + endif + + imult=pack77_rtty_multiplier_index(w(exchange_index)) + if(imult.le.0) return + exchange=pack77_rtty_multiplier_name(imult) + if(len_trim(w(exchange_index)).ne.len_trim(exchange)) return + source%nexch=8000+imult + ok=.true. +end subroutine pack77_parse_type3_source + +subroutine pack77_parse_type4_source(msg,source,ok) + character(len=*), intent(in) :: msg + type(pack77_type4_source), intent(out) :: source + logical, intent(out) :: ok + character(len=37) :: w(3),c11_token + integer :: nwords + logical :: split_ok,hash_1,hash_2 + + source=pack77_type4_source() + ok=.false. + call pack77_split_source_tokens(msg,3,nwords,w,split_ok) + if(.not.split_ok) return + if(nwords.lt.2 .or. nwords.gt.3) return + + hash_1=pack77_valid_hash_call_token(w(1)) + hash_2=pack77_valid_hash_call_token(w(2)) + + if(trim(w(1)).eq.'CQ') then + if(nwords.ne.2) return + if(hash_2) return + c11_token=w(2) + if(len_trim(c11_token).lt.5) return + if(.not.pack77_valid_type4_c11(c11_token)) return + if(pack77_type12_standard_call(c11_token)) return + source%c11=c11_token(1:11) + source%icq=1 + ok=.true. + return + endif + + if(nwords.eq.3) then + source%itail=pack77_qso_tail_index(w(3)) + if(source%itail.eq.0) return + endif + + if(hash_1 .and. .not.hash_2) then + if(.not.pack77_valid_type4_c11(w(2))) return + if(pack77_type12_standard_call(w(2))) return + source%hash_token=trim(w(1)) + source%c11=w(2)(1:11) + source%iflip=0 + ok=.true. + return + endif + + if(hash_2 .and. .not.hash_1) then + if(.not.pack77_valid_type4_c11(w(1))) return + if(pack77_type12_standard_call(w(1))) return + source%hash_token=trim(w(2)) + source%c11=w(1)(1:11) + source%iflip=1 + ok=.true. + return + endif +end subroutine pack77_parse_type4_source + +subroutine pack77_parse_type5_source(msg,source,ok,matched_shape) +! Type 5 EU VHF messages carry two explicit hash tokens, an optional R, +! report+serial as 52..59 plus 0001..2047, and a required six-character grid. + character(len=*), intent(in) :: msg + type(pack77_type5_source), intent(out) :: source + logical, intent(out) :: ok + logical, intent(out) :: matched_shape + character(len=37) :: w(5) + integer :: nwords,exchange_index,grid_index,nx + logical :: split_ok + + source=pack77_type5_source() + ok=.false. + matched_shape=.false. + call pack77_split_source_tokens(msg,5,nwords,w,split_ok) + if(.not.split_ok) return + if(nwords.lt.4 .or. nwords.gt.5) return + matched_shape=pack77_valid_hash_call_token(w(1)) .and. & + pack77_valid_hash_call_token(w(2)) + if(.not.pack77_valid_hash_call_token(w(1))) return + if(.not.pack77_valid_hash_call_token(w(2))) return + if(nwords.eq.5) then + if(trim(w(3)).ne.'R') return + source%ir=1 + endif + + exchange_index=nwords-1 + grid_index=nwords + call pack77_valid_type5_exchange(w(exchange_index),nx,ok) + if(.not.ok) return + if(nx.lt.520001 .or. nx.gt.592047) then + ok=.false. + return + endif + source%iserial=mod(nx,10000) + if(source%iserial.lt.1 .or. source%iserial.gt.2047) then + ok=.false. + return + endif + if(len_trim(w(grid_index)).ne.6) then + ok=.false. + return + endif + if(.not.pack77_is_grid6(w(grid_index),.false.)) then + ok=.false. + return + endif + + source%hash_token_1=trim(w(1)) + source%hash_token_2=trim(w(2)) + source%irpt=nx/10000 - 52 + source%grid6=w(grid_index)(1:6) + ok=.true. +end subroutine pack77_parse_type5_source + +logical function pack77_valid_type4_c11(token) result(ok) + character(len=*), intent(in) :: token + integer :: i,n + + ok=.false. + n=len_trim(token) + if(n.lt.1 .or. n.gt.11) return + do i=1,n + if(index(PACK77_BASE38,token(i:i)).lt.1) return + enddo + ok=.true. +end function pack77_valid_type4_c11 + +subroutine pack77_valid_type5_exchange(token,nx,ok) + character(len=*), intent(in) :: token + integer, intent(out) :: nx + logical, intent(out) :: ok + + nx=-1 + ok=.false. + if(len_trim(token).ne.6) return + call pack77_parse_uint(token,nx,ok) +end subroutine pack77_valid_type5_exchange + +integer function pack77_wspr_affix_index(token,slash_index,token_len,ok) result(npfx) + character(len=*), intent(in) :: token + integer, intent(in) :: slash_index,token_len + logical, intent(out) :: ok + integer :: i + + ok=.true. + npfx=0 + if(slash_index.le.4 .and. token_len-slash_index.lt.3) then + ok=.false. + return + endif + if(slash_index.gt.4 .and. token_len-slash_index.gt.3) then + ok=.false. + return + endif + do i=1,slash_index-1 + if(index(PACK77_BASE36,token(i:i)).lt.1) then + ok=.false. + return + endif + enddo + do i=slash_index+1,token_len + if(index(PACK77_BASE36,token(i:i)).lt.1) then + ok=.false. + return + endif + enddo + if(slash_index.le.4) then + npfx=index(PACK77_BASE36,token(1:1))-1 + if(slash_index.ge.3) npfx=36*npfx + index(PACK77_BASE36,token(2:2))-1 + if(slash_index.eq.4) npfx=36*npfx + index(PACK77_BASE36,token(3:3))-1 + else + if((token_len-slash_index).eq.1) & + npfx=index(PACK77_BASE36,token(slash_index+1:slash_index+1))-1 + if((token_len-slash_index).eq.2) & + npfx=36*(index(PACK77_BASE36,token(slash_index+1:slash_index+1))-1) + & + index(PACK77_BASE36,token(slash_index+2:slash_index+2))-1 + if((token_len-slash_index).eq.3) then + if(.not.pack77_is_digit_char(token(slash_index+3:slash_index+3))) then + ok=.false. + return + endif + npfx=36*10*(index(PACK77_BASE36,token(slash_index+1:slash_index+1))-1) + & + 10*(index(PACK77_BASE36,token(slash_index+2:slash_index+2))-1) + & + index(PACK77_BASE36,token(slash_index+3:slash_index+3))-1 + endif + npfx=npfx + PACK77_WSPR_NZZZ + endif + + return +end function pack77_wspr_affix_index + +subroutine pack77_wspr_affix_text(npfx,cpfx,is_prefix,ok) + integer, intent(in) :: npfx + character(len=3), intent(out) :: cpfx + logical, intent(out) :: is_prefix,ok + integer :: n,i,j + + n=npfx + cpfx=' ' + ok=.true. + is_prefix=n.lt.PACK77_WSPR_NZZZ + if(is_prefix) then + do i=3,1,-1 + j=mod(n,36)+1 + cpfx(i:i)=PACK77_BASE36(j:j) + n=n/36 + if(n.eq.0) exit + enddo + else + n=n-PACK77_WSPR_NZZZ + if(n.le.35) then + cpfx(1:1)=PACK77_BASE36(n+1:n+1) + else if(n.gt.35 .and. n.le.1295) then + cpfx(1:1)=PACK77_BASE36(n/36+1:n/36+1) + cpfx(2:2)=PACK77_BASE36(mod(n,36)+1:mod(n,36)+1) + else if(n.gt.1295 .and. n.le.12959) then + cpfx(1:1)=PACK77_BASE36(n/360+1:n/360+1) + cpfx(2:2)=PACK77_BASE36(mod(n/10,36)+1:mod(n/10,36)+1) + cpfx(3:3)=PACK77_BASE36(mod(n,10)+1:mod(n,10)+1) + else + ok=.false. + endif + endif + + return +end subroutine pack77_wspr_affix_text + +character(len=11) function pack77_type4_call_text_from_n58(n58) result(c11) + integer*8, intent(in) :: n58 + integer*8 :: n + integer :: i,j + + c11=' ' + n=n58 + do i=11,1,-1 + j=mod(n,38_8)+1 + c11(i:i)=PACK77_BASE38(j:j) + n=n/38 + enddo + + return +end function pack77_type4_call_text_from_n58 + +integer*8 function pack77_type4_n58_from_call_text(c11) result(n58) + character(len=*), intent(in) :: c11 + integer :: i + + n58=0_8 + do i=1,11 + n58=n58*38_8 + index(PACK77_BASE38,c11(i:i)) - 1 + enddo + + return +end function pack77_type4_n58_from_call_text + +subroutine to_grid4(n,grid4,ok) + character*4 grid4 + logical ok + integer :: n,j1,j2,j3,j4 + + ok=.false. + j1=n/(18*10*10) + if (j1.lt.0.or.j1.gt.17) goto 900 + n=n-j1*18*10*10 + j2=n/(10*10) + if (j2.lt.0.or.j2.gt.17) goto 900 + n=n-j2*10*10 + j3=n/10 + if (j3.lt.0.or.j3.gt.9) goto 900 + j4=n-j3*10 + if (j4.lt.0.or.j4.gt.9) goto 900 + grid4(1:1)=char(j1+ichar('A')) + grid4(2:2)=char(j2+ichar('A')) + grid4(3:3)=char(j3+ichar('0')) + grid4(4:4)=char(j4+ichar('0')) + ok=.true. + +900 return +end subroutine to_grid4 + +subroutine to_grid6(n,grid6,ok) + character*6 grid6 + logical ok + integer :: n,j1,j2,j3,j4,j5,j6 + + ok=.false. + j1=n/(18*10*10*24*24) + if (j1.lt.0.or.j1.gt.17) goto 900 + n=n-j1*18*10*10*24*24 + j2=n/(10*10*24*24) + if (j2.lt.0.or.j2.gt.17) goto 900 + n=n-j2*10*10*24*24 + j3=n/(10*24*24) + if (j3.lt.0.or.j3.gt.9) goto 900 + n=n-j3*10*24*24 + j4=n/(24*24) + if (j4.lt.0.or.j4.gt.9) goto 900 + n=n-j4*24*24 + j5=n/24 + if (j5.lt.0.or.j5.gt.23) goto 900 + j6=n-j5*24 + if (j6.lt.0.or.j6.gt.23) goto 900 + grid6(1:1)=char(j1+ichar('A')) + grid6(2:2)=char(j2+ichar('A')) + grid6(3:3)=char(j3+ichar('0')) + grid6(4:4)=char(j4+ichar('0')) + grid6(5:5)=char(j5+ichar('A')) + grid6(6:6)=char(j6+ichar('A')) + ok=.true. + +900 return +end subroutine to_grid6 + +subroutine to_grid(n,grid6,ok) + ! 4-, or 6-character grid + character*6 grid6 + logical ok + integer :: n,j1,j2,j3,j4,j5,j6 + + ok=.false. + j1=n/(18*10*10*25*25) + if (j1.lt.0.or.j1.gt.17) goto 900 + n=n-j1*18*10*10*25*25 + j2=n/(10*10*25*25) + if (j2.lt.0.or.j2.gt.17) goto 900 + n=n-j2*10*10*25*25 + j3=n/(10*25*25) + if (j3.lt.0.or.j3.gt.9) goto 900 + n=n-j3*10*25*25 + j4=n/(25*25) + if (j4.lt.0.or.j4.gt.9) goto 900 + n=n-j4*25*25 + j5=n/25 + if (j5.lt.0.or.j5.gt.24) goto 900 + j6=n-j5*25 + if (j6.lt.0.or.j6.gt.24) goto 900 + grid6='' + grid6(1:1)=char(j1+ichar('A')) + grid6(2:2)=char(j2+ichar('A')) + grid6(3:3)=char(j3+ichar('0')) + grid6(4:4)=char(j4+ichar('0')) + if (j5.ne.24.or.j6.ne.24) then + grid6(5:5)=char(j5+ichar('A')) + grid6(6:6)=char(j6+ichar('A')) + endif + ok=.true. + +900 return +end subroutine to_grid + +logical function pack77_is_grid4(grid4) result(ok) + character(len=*), intent(in) :: grid4 + + ok=.false. + if(len(grid4).lt.4) return + if(len(trim(grid4)).ne.4) return + ok=grid4(1:1).ge.'A' .and. grid4(1:1).le.'R' .and. & + grid4(2:2).ge.'A' .and. grid4(2:2).le.'R' .and. & + grid4(3:3).ge.'0' .and. grid4(3:3).le.'9' .and. & + grid4(4:4).ge.'0' .and. grid4(4:4).le.'9' + + return +end function pack77_is_grid4 + +logical function pack77_is_grid6(grid6,allow_grid4) result(ok) + character(len=*), intent(in) :: grid6 + logical, intent(in) :: allow_grid4 + integer :: ngrid + + ok=.false. + if(len(grid6).lt.4) return + ngrid=len(trim(grid6)) + if(ngrid.eq.4) then + ok=allow_grid4 .and. pack77_is_grid4(grid6(1:4)) + return + endif + if(len(grid6).lt.6 .or. ngrid.ne.6) return + ok=pack77_is_grid4(grid6(1:4)) .and. & + grid6(5:5).ge.'A' .and. grid6(5:5).le.'X' .and. & + grid6(6:6).ge.'A' .and. grid6(6:6).le.'X' + + return +end function pack77_is_grid6 + +integer function pack77_grid4_index(grid4) result(igrid4) + character(len=*), intent(in) :: grid4 + + igrid4=(ichar(grid4(1:1))-ichar('A'))*18*10*10 + & + (ichar(grid4(2:2))-ichar('A'))*10*10 + & + (ichar(grid4(3:3))-ichar('0'))*10 + & + (ichar(grid4(4:4))-ichar('0')) + + return +end function pack77_grid4_index + +integer function pack77_grid6_index(grid6) result(igrid6) + character(len=*), intent(in) :: grid6 + + igrid6=pack77_grid4_index(grid6(1:4))*24*24 + & + (ichar(grid6(5:5))-ichar('A'))*24 + & + (ichar(grid6(6:6))-ichar('A')) + + return +end function pack77_grid6_index + +integer function pack77_grid6_wspr_index(grid6) result(igrid6) + character(len=*), intent(in) :: grid6 + + igrid6=pack77_grid4_index(grid6(1:4))*25*25 + if(grid6(5:6).eq.' ') then + igrid6=igrid6 + 24*25 + 24 + else + igrid6=igrid6 + (ichar(grid6(5:5))-ichar('A'))*25 + & + (ichar(grid6(6:6))-ichar('A')) + endif + + return +end function pack77_grid6_wspr_index + +end module packjt77_grammar diff --git a/lib/77bit/packjt77_schema.f90 b/lib/77bit/packjt77_schema.f90 new file mode 100644 index 00000000..cfa43687 --- /dev/null +++ b/lib/77bit/packjt77_schema.f90 @@ -0,0 +1,871 @@ +module packjt77_schema + + use, intrinsic :: iso_fortran_env, only: int64 + implicit none + + integer, parameter :: PACK77_SCHEMA_INT_KIND=int64 + integer, parameter :: PACK77_MAX_SCHEMA_FIELDS=8 + integer, parameter :: PACK77_FIELD_NAME_LEN=16 + integer, parameter :: PACK77_SCHEMA_NAME_LEN=32 + + private :: NO_FIELD,put_int,get_int,begin_message,put_int_i4,put_int_i8, & + put_bits,get_int_i4,get_int_i8,get_bits,valid_field_bounds, & + encode_pack77_field_day,encode_pack77_type12,decode_pack77_field_day, & + decode_pack77_type12,require_int,pack77_binary_precondition, & + bits_are_binary + + ! Schema bit ranges use Fortran string coordinates in the 77-character c77 + ! payload: start_bit is 1-based and integer fields are encoded MSB-first. + type bit_field + character(len=PACK77_FIELD_NAME_LEN) :: name='' + integer :: start_bit=0 + integer :: width=0 + integer(kind=int64) :: min_value=0_int64 + integer(kind=int64) :: max_value=-1_int64 + logical :: bitstring=.false. + end type bit_field + + type message_schema + integer :: i3=-1 + integer :: n3=-1 + character(len=PACK77_SCHEMA_NAME_LEN) :: name='' + integer :: nfields=0 + type(bit_field) :: fields(PACK77_MAX_SCHEMA_FIELDS) + end type message_schema + + type(bit_field), parameter :: NO_FIELD=bit_field('',0,0,0_int64,-1_int64,.false.) + + interface put_int + module procedure put_int_i4 + module procedure put_int_i8 + end interface put_int + + interface get_int + module procedure get_int_i4 + module procedure get_int_i8 + end interface get_int + + type(message_schema), parameter :: PACK77_SCHEMA_FREE_TEXT=message_schema( & + 0,0,'free text',3,(/ & + bit_field('text_bits',1,71,0_int64,0_int64,.true.), & + bit_field('n3',72,3,0_int64,0_int64,.false.), & + bit_field('i3',75,3,0_int64,0_int64,.false.), & + NO_FIELD,NO_FIELD,NO_FIELD,NO_FIELD,NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_TELEMETRY=message_schema( & + 0,5,'telemetry',5,(/ & + bit_field('ntel1',1,23,0_int64,8388607_int64,.false.), & + bit_field('ntel2',24,24,0_int64,16777215_int64,.false.), & + bit_field('ntel3',48,24,0_int64,16777215_int64,.false.), & + bit_field('n3',72,3,5_int64,5_int64,.false.), & + bit_field('i3',75,3,0_int64,0_int64,.false.), & + NO_FIELD,NO_FIELD,NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_DXPEDITION=message_schema( & + 0,1,'dxpedition',6,(/ & + bit_field('n28a',1,28,0_int64,268435455_int64,.false.), & + bit_field('n28b',29,28,0_int64,268435455_int64,.false.), & + bit_field('n10',57,10,0_int64,1023_int64,.false.), & + bit_field('n5',67,5,0_int64,31_int64,.false.), & + bit_field('n3',72,3,1_int64,1_int64,.false.), & + bit_field('i3',75,3,0_int64,0_int64,.false.), & + NO_FIELD,NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_FIELD_DAY_LOW=message_schema( & + 0,3,'field day low',8,(/ & + bit_field('n28a',1,28,0_int64,268435455_int64,.false.), & + bit_field('n28b',29,28,0_int64,268435455_int64,.false.), & + bit_field('ir',57,1,0_int64,1_int64,.false.), & + bit_field('intx',58,4,0_int64,15_int64,.false.), & + bit_field('nclass',62,3,0_int64,7_int64,.false.), & + bit_field('isec',65,7,0_int64,127_int64,.false.), & + bit_field('n3',72,3,3_int64,3_int64,.false.), & + bit_field('i3',75,3,0_int64,0_int64,.false.) /)) + + type(message_schema), parameter :: PACK77_SCHEMA_FIELD_DAY_HIGH=message_schema( & + 0,4,'field day high',8,(/ & + bit_field('n28a',1,28,0_int64,268435455_int64,.false.), & + bit_field('n28b',29,28,0_int64,268435455_int64,.false.), & + bit_field('ir',57,1,0_int64,1_int64,.false.), & + bit_field('intx',58,4,0_int64,15_int64,.false.), & + bit_field('nclass',62,3,0_int64,7_int64,.false.), & + bit_field('isec',65,7,0_int64,127_int64,.false.), & + bit_field('n3',72,3,4_int64,4_int64,.false.), & + bit_field('i3',75,3,0_int64,0_int64,.false.) /)) + + type(message_schema), parameter :: PACK77_SCHEMA_WSPR_TYPE1=message_schema( & + 0,6,'wspr type 1',8,(/ & + bit_field('n28',1,28,0_int64,268435455_int64,.false.), & + bit_field('igrid4',29,15,0_int64,32767_int64,.false.), & + ! idbm max 18 = nint(0.3*dBm) for dBm 0..60; range enforced here + bit_field('idbm',44,5,0_int64,18_int64,.false.), & + bit_field('j49',49,1,0_int64,0_int64,.false.), & + bit_field('j50',50,1,0_int64,0_int64,.false.), & + bit_field('pad',51,21,0_int64,0_int64,.false.), & + bit_field('n3',72,3,6_int64,6_int64,.false.), & + bit_field('i3',75,3,0_int64,0_int64,.false.) /)) + + type(message_schema), parameter :: PACK77_SCHEMA_WSPR_TYPE2=message_schema( & + 0,6,'wspr type 2',7,(/ & + bit_field('n28',1,28,0_int64,268435455_int64,.false.), & + bit_field('npfx',29,16,0_int64,65535_int64,.false.), & + ! idbm max 18 = nint(0.3*dBm) for dBm 0..60; range enforced here + bit_field('idbm',45,5,0_int64,18_int64,.false.), & + bit_field('j50',50,1,1_int64,1_int64,.false.), & + bit_field('pad',51,21,0_int64,0_int64,.false.), & + bit_field('n3',72,3,6_int64,6_int64,.false.), & + bit_field('i3',75,3,0_int64,0_int64,.false.), & + NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_WSPR_TYPE3=message_schema( & + 0,6,'wspr type 3',6,(/ & + bit_field('n22',1,22,0_int64,4194303_int64,.false.), & + bit_field('igrid6',23,25,0_int64,33554431_int64,.false.), & + bit_field('selector',48,3,2_int64,2_int64,.false.), & + bit_field('pad',51,21,0_int64,0_int64,.false.), & + bit_field('n3',72,3,6_int64,6_int64,.false.), & + bit_field('i3',75,3,0_int64,0_int64,.false.), & + NO_FIELD,NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_TYPE1=message_schema( & + 1,0,'type 1 standard',7,(/ & + bit_field('n28a',1,28,0_int64,268435455_int64,.false.), & + bit_field('ipa',29,1,0_int64,1_int64,.false.), & + bit_field('n28b',30,28,0_int64,268435455_int64,.false.), & + bit_field('ipb',58,1,0_int64,1_int64,.false.), & + bit_field('ir',59,1,0_int64,1_int64,.false.), & + bit_field('igrid4',60,15,0_int64,32767_int64,.false.), & + bit_field('i3',75,3,1_int64,1_int64,.false.), & + NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_TYPE2=message_schema( & + 2,0,'type 2 portable',7,(/ & + bit_field('n28a',1,28,0_int64,268435455_int64,.false.), & + bit_field('ipa',29,1,0_int64,1_int64,.false.), & + bit_field('n28b',30,28,0_int64,268435455_int64,.false.), & + bit_field('ipb',58,1,0_int64,1_int64,.false.), & + bit_field('ir',59,1,0_int64,1_int64,.false.), & + bit_field('igrid4',60,15,0_int64,32767_int64,.false.), & + bit_field('i3',75,3,2_int64,2_int64,.false.), & + NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_TYPE3=message_schema( & + 3,0,'type 3 rtty',7,(/ & + bit_field('itu',1,1,0_int64,1_int64,.false.), & + bit_field('n28a',2,28,0_int64,268435455_int64,.false.), & + bit_field('n28b',30,28,0_int64,268435455_int64,.false.), & + bit_field('ir',58,1,0_int64,1_int64,.false.), & + bit_field('irpt',59,3,0_int64,7_int64,.false.), & + bit_field('nexch',62,13,0_int64,8191_int64,.false.), & + bit_field('i3',75,3,3_int64,3_int64,.false.), & + NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_TYPE4=message_schema( & + 4,0,'type 4 nonstandard',6,(/ & + bit_field('n12',1,12,0_int64,4095_int64,.false.), & + bit_field('n58',13,58,0_int64,288230376151711743_int64,.false.), & + bit_field('iflip',71,1,0_int64,1_int64,.false.), & + bit_field('nrpt',72,2,0_int64,3_int64,.false.), & + bit_field('icq',74,1,0_int64,1_int64,.false.), & + bit_field('i3',75,3,4_int64,4_int64,.false.), & + NO_FIELD,NO_FIELD /)) + + type(message_schema), parameter :: PACK77_SCHEMA_TYPE5=message_schema( & + 5,0,'type 5 eu vhf',7,(/ & + bit_field('n12',1,12,0_int64,4095_int64,.false.), & + bit_field('n22',13,22,0_int64,4194303_int64,.false.), & + bit_field('ir',35,1,0_int64,1_int64,.false.), & + bit_field('irpt',36,3,0_int64,7_int64,.false.), & + bit_field('iserial',39,11,1_int64,2047_int64,.false.), & + ! igrid6 max 18662399 = EU-VHF grid6 range, enforced here + ! (WSPR type3 igrid6 keeps the full 2^25-1 range) + bit_field('igrid6',50,25,0_int64,18662399_int64,.false.), & + bit_field('i3',75,3,5_int64,5_int64,.false.), & + NO_FIELD /)) + + type pack77_free_text_fields + character(len=71) :: text_bits='' + end type pack77_free_text_fields + + type pack77_telemetry_fields + integer :: ntel1=0 + integer :: ntel2=0 + integer :: ntel3=0 + end type pack77_telemetry_fields + + type pack77_dxpedition_fields + integer :: n28a=0 + integer :: n28b=0 + integer :: n10=0 + integer :: n5=0 + end type pack77_dxpedition_fields + + type pack77_field_day_fields + integer :: n28a=0 + integer :: n28b=0 + integer :: ir=0 + integer :: intx=0 + integer :: nclass=0 + integer :: isec=0 + end type pack77_field_day_fields + + type pack77_wspr_type1_fields + integer :: n28=0 + integer :: igrid4=0 + integer :: idbm=0 + end type pack77_wspr_type1_fields + + type pack77_wspr_type2_fields + integer :: n28=0 + integer :: npfx=0 + integer :: idbm=0 + end type pack77_wspr_type2_fields + + type pack77_wspr_type3_fields + integer :: n22=0 + integer :: igrid6=0 + end type pack77_wspr_type3_fields + + type pack77_type12_fields + integer :: n28a=0 + integer :: ipa=0 + integer :: n28b=0 + integer :: ipb=0 + integer :: ir=0 + integer :: igrid4=0 + end type pack77_type12_fields + + type pack77_type3_fields + integer :: itu=0 + integer :: n28a=0 + integer :: n28b=0 + integer :: ir=0 + integer :: irpt=0 + integer :: nexch=0 + end type pack77_type3_fields + + type pack77_type4_fields + integer :: n12=0 + integer(kind=int64) :: n58=0_int64 + integer :: iflip=0 + integer :: nrpt=0 + integer :: icq=0 + end type pack77_type4_fields + + type pack77_type5_fields + integer :: n12=0 + integer :: n22=0 + integer :: ir=0 + integer :: irpt=0 + integer :: iserial=0 + integer :: igrid6=0 + end type pack77_type5_fields +contains + + + subroutine begin_message(c77,ok) + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + c77=repeat('0',len(c77)) + ok=.true. + end subroutine begin_message + + subroutine put_int_i4(ok,c77,field,value) + logical, intent(inout) :: ok + character(len=77), intent(inout) :: c77 + type(bit_field), intent(in) :: field + integer, intent(in) :: value + + call put_int_i8(ok,c77,field,int(value,kind=int64)) + end subroutine put_int_i4 + + subroutine put_int_i8(ok,c77,field,value) + logical, intent(inout) :: ok + character(len=77), intent(inout) :: c77 + type(bit_field), intent(in) :: field + integer(kind=int64), intent(in) :: value + integer :: i + integer(kind=int64) :: place + + if(.not.ok) return + ok=.false. + if(.not.valid_field_bounds(field,len(c77))) return + if(field%bitstring) return + if(value.lt.field%min_value .or. value.gt.field%max_value) return + do i=field%width,1,-1 + place=2_int64**(field%width-i) + if(mod(value/place,2_int64).eq.1_int64) then + c77(field%start_bit+i-1:field%start_bit+i-1)='1' + else + c77(field%start_bit+i-1:field%start_bit+i-1)='0' + endif + enddo + ok=.true. + end subroutine put_int_i8 + + subroutine put_bits(ok,c77,field,bits) + logical, intent(inout) :: ok + character(len=77), intent(inout) :: c77 + type(bit_field), intent(in) :: field + character(len=*), intent(in) :: bits + integer :: i + + if(.not.ok) return + ok=.false. + if(.not.valid_field_bounds(field,len(c77))) return + if(.not.field%bitstring) return + if(len_trim(bits).ne.field%width) return + do i=1,field%width + if(bits(i:i).ne.'0' .and. bits(i:i).ne.'1') return + enddo + c77(field%start_bit:field%start_bit+field%width-1)=bits(1:field%width) + ok=.true. + end subroutine put_bits + + subroutine get_int_i4(ok,c77,field,value) + logical, intent(inout) :: ok + character(len=77), intent(in) :: c77 + type(bit_field), intent(in) :: field + integer, intent(out) :: value + integer(kind=int64) :: int64_value + + value=0 + call get_int_i8(ok,c77,field,int64_value) + if(ok) value=int(int64_value) + end subroutine get_int_i4 + + subroutine get_int_i8(ok,c77,field,value) + logical, intent(inout) :: ok + character(len=77), intent(in) :: c77 + type(bit_field), intent(in) :: field + integer(kind=int64), intent(out) :: value + integer :: i + + value=0_int64 + if(.not.ok) return + ok=.false. + if(.not.valid_field_bounds(field,len(c77))) return + if(field%bitstring) return + do i=0,field%width-1 + value=value*2_int64 + if(c77(field%start_bit+i:field%start_bit+i).eq.'1') then + value=value+1_int64 + else if(c77(field%start_bit+i:field%start_bit+i).ne.'0') then + return + endif + enddo + if(value.lt.field%min_value .or. value.gt.field%max_value) return + ok=.true. + end subroutine get_int_i8 + + subroutine get_bits(ok,c77,field,bits) + logical, intent(inout) :: ok + character(len=77), intent(in) :: c77 + type(bit_field), intent(in) :: field + character(len=*), intent(out) :: bits + + bits='' + if(.not.ok) return + ok=.false. + if(.not.valid_field_bounds(field,len(c77))) return + if(.not.field%bitstring) return + if(field%width.gt.len(bits)) return + if(.not.bits_are_binary(c77(field%start_bit:field%start_bit+field%width-1))) return + bits(1:field%width)=c77(field%start_bit:field%start_bit+field%width-1) + ok=.true. + end subroutine get_bits + + logical function valid_field_bounds(field,nbits) result(ok) + type(bit_field), intent(in) :: field + integer, intent(in) :: nbits + + ok=.false. + if(field%start_bit.lt.1 .or. field%width.lt.1) return + if(field%start_bit+field%width-1.gt.nbits) return + ok=.true. + end function valid_field_bounds + + subroutine encode_pack77_free_text(text_bits,c77,ok) + character(len=*), intent(in) :: text_bits + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + character(len=71) :: packed_text + + call begin_message(c77,ok) + if(len(text_bits).ne.71) then + ok=.false. + return + endif + packed_text=text_bits + call put_bits(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(1),packed_text) + call put_int(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(2),0) + call put_int(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(3),0) + end subroutine encode_pack77_free_text + + subroutine encode_pack77_telemetry(ntel1,ntel2,ntel3,c77,ok) + integer, intent(in) :: ntel1,ntel2,ntel3 + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(1),ntel1) + call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(2),ntel2) + call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(3),ntel3) + call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(4),5) + call put_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(5),0) + end subroutine encode_pack77_telemetry + + subroutine encode_pack77_dxpedition(n28a,n28b,n10,n5,c77,ok) + integer, intent(in) :: n28a,n28b,n10,n5 + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(1),n28a) + call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(2),n28b) + call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(3),n10) + call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(4),n5) + call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(5),1) + call put_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(6),0) + end subroutine encode_pack77_dxpedition + + subroutine encode_pack77_field_day(schema,n28a,n28b,ir,intx,nclass,isec,c77,ok) + type(message_schema), intent(in) :: schema + integer, intent(in) :: n28a,n28b,ir,intx,nclass,isec + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,schema%fields(1),n28a) + call put_int(ok,c77,schema%fields(2),n28b) + call put_int(ok,c77,schema%fields(3),ir) + call put_int(ok,c77,schema%fields(4),intx) + call put_int(ok,c77,schema%fields(5),nclass) + call put_int(ok,c77,schema%fields(6),isec) + call put_int(ok,c77,schema%fields(7),schema%n3) + call put_int(ok,c77,schema%fields(8),0) + end subroutine encode_pack77_field_day + + subroutine encode_pack77_field_day_low(n28a,n28b,ir,intx,nclass,isec,c77,ok) + integer, intent(in) :: n28a,n28b,ir,intx,nclass,isec + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call encode_pack77_field_day(PACK77_SCHEMA_FIELD_DAY_LOW, & + n28a,n28b,ir,intx,nclass,isec,c77,ok) + end subroutine encode_pack77_field_day_low + + subroutine encode_pack77_field_day_high(n28a,n28b,ir,intx,nclass,isec,c77,ok) + integer, intent(in) :: n28a,n28b,ir,intx,nclass,isec + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call encode_pack77_field_day(PACK77_SCHEMA_FIELD_DAY_HIGH, & + n28a,n28b,ir,intx,nclass,isec,c77,ok) + end subroutine encode_pack77_field_day_high + + subroutine encode_pack77_wspr_type1(n28,igrid4,idbm,c77,ok) + integer, intent(in) :: n28,igrid4,idbm + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(1),n28) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(2),igrid4) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(3),idbm) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(4),0) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(5),0) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(6),0) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(7),6) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(8),0) + end subroutine encode_pack77_wspr_type1 + + subroutine encode_pack77_wspr_type2(n28,npfx,idbm,c77,ok) + integer, intent(in) :: n28,npfx,idbm + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(1),n28) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(2),npfx) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(3),idbm) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(4),1) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(5),0) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(6),6) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(7),0) + end subroutine encode_pack77_wspr_type2 + + subroutine encode_pack77_wspr_type3(n22,igrid6,c77,ok) + integer, intent(in) :: n22,igrid6 + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(1),n22) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(2),igrid6) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(3),2) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(4),0) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(5),6) + call put_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(6),0) + end subroutine encode_pack77_wspr_type3 + + subroutine encode_pack77_type12(schema,n28a,ipa,n28b,ipb,ir,igrid4,c77,ok) + type(message_schema), intent(in) :: schema + integer, intent(in) :: n28a,ipa,n28b,ipb,ir,igrid4 + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,schema%fields(1),n28a) + call put_int(ok,c77,schema%fields(2),ipa) + call put_int(ok,c77,schema%fields(3),n28b) + call put_int(ok,c77,schema%fields(4),ipb) + call put_int(ok,c77,schema%fields(5),ir) + call put_int(ok,c77,schema%fields(6),igrid4) + call put_int(ok,c77,schema%fields(7),schema%i3) + end subroutine encode_pack77_type12 + + subroutine encode_pack77_type1(n28a,ipa,n28b,ipb,ir,igrid4,c77,ok) + integer, intent(in) :: n28a,ipa,n28b,ipb,ir,igrid4 + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call encode_pack77_type12(PACK77_SCHEMA_TYPE1, & + n28a,ipa,n28b,ipb,ir,igrid4,c77,ok) + end subroutine encode_pack77_type1 + + subroutine encode_pack77_type2(n28a,ipa,n28b,ipb,ir,igrid4,c77,ok) + integer, intent(in) :: n28a,ipa,n28b,ipb,ir,igrid4 + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call encode_pack77_type12(PACK77_SCHEMA_TYPE2, & + n28a,ipa,n28b,ipb,ir,igrid4,c77,ok) + end subroutine encode_pack77_type2 + + subroutine encode_pack77_type3(itu,n28a,n28b,ir,irpt,nexch,c77,ok) + integer, intent(in) :: itu,n28a,n28b,ir,irpt,nexch + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(1),itu) + call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(2),n28a) + call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(3),n28b) + call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(4),ir) + call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(5),irpt) + call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(6),nexch) + call put_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(7),3) + end subroutine encode_pack77_type3 + + subroutine encode_pack77_type4(n12,n58,iflip,nrpt,icq,c77,ok) + integer, intent(in) :: n12,iflip,nrpt,icq + integer(kind=int64), intent(in) :: n58 + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(1),n12) + call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(2),n58) + call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(3),iflip) + call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(4),nrpt) + call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(5),icq) + call put_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(6),4) + end subroutine encode_pack77_type4 + + subroutine encode_pack77_type5(n12,n22,ir,irpt,iserial,igrid6,c77,ok) + integer, intent(in) :: n12,n22,ir,irpt,iserial,igrid6 + character(len=77), intent(out) :: c77 + logical, intent(out) :: ok + + call begin_message(c77,ok) + call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(1),n12) + call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(2),n22) + call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(3),ir) + call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(4),irpt) + call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(5),iserial) + call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(6),igrid6) + call put_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(7),5) + end subroutine encode_pack77_type5 + + subroutine decode_pack77_tag(c77,i3,n3,ok,binary_checked) + character(len=77), intent(in) :: c77 + integer, intent(out) :: i3,n3 + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + integer(kind=int64) :: value + + i3=-1 + n3=-1 + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,bit_field('i3',75,3,0_int64,7_int64,.false.),value) + if(.not.ok) return + i3=int(value) + if(i3.eq.0) then + call get_int(ok,c77,bit_field('n3',72,3,0_int64,7_int64,.false.),value) + if(.not.ok) return + n3=int(value) + else + n3=0 + endif + end subroutine decode_pack77_tag + + subroutine decode_pack77_free_text(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_free_text_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_free_text_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_bits(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(1),fields%text_bits) + call require_int(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(2),0) + call require_int(ok,c77,PACK77_SCHEMA_FREE_TEXT%fields(3),0) + end subroutine decode_pack77_free_text + + subroutine decode_pack77_telemetry(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_telemetry_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_telemetry_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(1),fields%ntel1) + call get_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(2),fields%ntel2) + call get_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(3),fields%ntel3) + call require_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(4),5) + call require_int(ok,c77,PACK77_SCHEMA_TELEMETRY%fields(5),0) + end subroutine decode_pack77_telemetry + + subroutine decode_pack77_dxpedition(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_dxpedition_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_dxpedition_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(1),fields%n28a) + call get_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(2),fields%n28b) + call get_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(3),fields%n10) + call get_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(4),fields%n5) + call require_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(5),1) + call require_int(ok,c77,PACK77_SCHEMA_DXPEDITION%fields(6),0) + end subroutine decode_pack77_dxpedition + + subroutine decode_pack77_field_day_low(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_field_day_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + call decode_pack77_field_day(PACK77_SCHEMA_FIELD_DAY_LOW,c77,fields, & + ok,binary_checked) + end subroutine decode_pack77_field_day_low + + subroutine decode_pack77_field_day_high(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_field_day_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + call decode_pack77_field_day(PACK77_SCHEMA_FIELD_DAY_HIGH,c77,fields, & + ok,binary_checked) + end subroutine decode_pack77_field_day_high + + subroutine decode_pack77_wspr_type1(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_wspr_type1_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_wspr_type1_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(1),fields%n28) + call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(2),fields%igrid4) + call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(3),fields%idbm) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(4),0) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(5),0) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(6),0) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(7),6) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE1%fields(8),0) + end subroutine decode_pack77_wspr_type1 + + subroutine decode_pack77_wspr_type2(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_wspr_type2_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_wspr_type2_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(1),fields%n28) + call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(2),fields%npfx) + call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(3),fields%idbm) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(4),1) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(5),0) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(6),6) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE2%fields(7),0) + end subroutine decode_pack77_wspr_type2 + + subroutine decode_pack77_wspr_type3(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_wspr_type3_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_wspr_type3_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(1),fields%n22) + call get_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(2),fields%igrid6) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(3),2) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(4),0) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(5),6) + call require_int(ok,c77,PACK77_SCHEMA_WSPR_TYPE3%fields(6),0) + end subroutine decode_pack77_wspr_type3 + + subroutine decode_pack77_type1(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_type12_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + call decode_pack77_type12(PACK77_SCHEMA_TYPE1,c77,fields,ok,binary_checked) + end subroutine decode_pack77_type1 + + subroutine decode_pack77_type2(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_type12_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + call decode_pack77_type12(PACK77_SCHEMA_TYPE2,c77,fields,ok,binary_checked) + end subroutine decode_pack77_type2 + + subroutine decode_pack77_type3(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_type3_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_type3_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(1),fields%itu) + call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(2),fields%n28a) + call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(3),fields%n28b) + call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(4),fields%ir) + call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(5),fields%irpt) + call get_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(6),fields%nexch) + call require_int(ok,c77,PACK77_SCHEMA_TYPE3%fields(7),3) + end subroutine decode_pack77_type3 + + subroutine decode_pack77_type4(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_type4_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_type4_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(1),fields%n12) + call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(2),fields%n58) + call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(3),fields%iflip) + call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(4),fields%nrpt) + call get_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(5),fields%icq) + call require_int(ok,c77,PACK77_SCHEMA_TYPE4%fields(6),4) + end subroutine decode_pack77_type4 + + subroutine decode_pack77_type5(c77,fields,ok,binary_checked) + character(len=77), intent(in) :: c77 + type(pack77_type5_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_type5_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(1),fields%n12) + call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(2),fields%n22) + call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(3),fields%ir) + call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(4),fields%irpt) + call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(5),fields%iserial) + call get_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(6),fields%igrid6) + call require_int(ok,c77,PACK77_SCHEMA_TYPE5%fields(7),5) + end subroutine decode_pack77_type5 + + subroutine decode_pack77_field_day(schema,c77,fields,ok,binary_checked) + type(message_schema), intent(in) :: schema + character(len=77), intent(in) :: c77 + type(pack77_field_day_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_field_day_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,schema%fields(1),fields%n28a) + call get_int(ok,c77,schema%fields(2),fields%n28b) + call get_int(ok,c77,schema%fields(3),fields%ir) + call get_int(ok,c77,schema%fields(4),fields%intx) + call get_int(ok,c77,schema%fields(5),fields%nclass) + call get_int(ok,c77,schema%fields(6),fields%isec) + call require_int(ok,c77,schema%fields(7),schema%n3) + call require_int(ok,c77,schema%fields(8),0) + end subroutine decode_pack77_field_day + + subroutine decode_pack77_type12(schema,c77,fields,ok,binary_checked) + type(message_schema), intent(in) :: schema + character(len=77), intent(in) :: c77 + type(pack77_type12_fields), intent(out) :: fields + logical, intent(out) :: ok + logical, intent(in), optional :: binary_checked + + fields=pack77_type12_fields() + ok=pack77_binary_precondition(c77,binary_checked) + call get_int(ok,c77,schema%fields(1),fields%n28a) + call get_int(ok,c77,schema%fields(2),fields%ipa) + call get_int(ok,c77,schema%fields(3),fields%n28b) + call get_int(ok,c77,schema%fields(4),fields%ipb) + call get_int(ok,c77,schema%fields(5),fields%ir) + call get_int(ok,c77,schema%fields(6),fields%igrid4) + call require_int(ok,c77,schema%fields(7),schema%i3) + end subroutine decode_pack77_type12 + + subroutine require_int(ok,c77,field,expected) + logical, intent(inout) :: ok + character(len=77), intent(in) :: c77 + type(bit_field), intent(in) :: field + integer, intent(in) :: expected + integer(kind=int64) :: value + + call get_int(ok,c77,field,value) + if(ok) ok=value.eq.int(expected,kind=int64) + end subroutine require_int + + logical function pack77_binary_precondition(c77,binary_checked) result(ok) + character(len=*), intent(in) :: c77 + logical, intent(in), optional :: binary_checked + + ok=.false. + if(present(binary_checked)) then + if(binary_checked) then + ! Caller asserts decode_pack77_tag has accepted this same c77. + ok=.true. + return + endif + endif + ok=bits_are_binary(c77) + end function pack77_binary_precondition + + logical function bits_are_binary(c77) result(ok) + character(len=*), intent(in) :: c77 + integer :: i + + ok=.false. + do i=1,len(c77) + if(c77(i:i).ne.'0' .and. c77(i:i).ne.'1') return + enddo + ok=.true. + end function bits_are_binary + +end module packjt77_schema |
