#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/param.h>
#include <time.h>
#include <limits.h>

#include "geminc.h"
#include "gemprm.h"
#include "bridge.h"
#include "dccmn.h"  /* contains prototypes for dc_gbul and dc_wclg */

#include "decod_ut.h"  /* contains prototypes for ut_prvs and ut_wbfr */

#include "mbedtls/aes.h"

#define MXBLSZ	5000000

#define BIG_ENDN	0
#define LTL_ENDN	1

/*
** On certain operating systems, the FORTRAN compiler appends an underscore
** to subprogram names in its object namespace.  Therefore, on such systems,
** a matching underscore must be appended to any C language references to the
** same subprogram names so that the linker can correctly resolve such
** references across the C <-> FORTRAN interface at link time.
*/
#ifdef UNDERSCORE
#define dc_ghdr dc_ghdr_
#define lt_pnbf lt_pnbf_
#define closbf closbf_
#define cmpmsg cmpmsg_
#define maxout maxout_
#define openmb openmb_
#define ufbint ufbint_
#endif

/*
** Declare prototypes for ANSI C compatibility.  This should be done for any
** subprogram that is either:
**   1) a C function, or
**   2) a FORTRAN subprogram called from C
*/
#ifdef F77_INTSIZE_8  /* borrowing this typedef block from NCEPLIBS-bufr bufrlib.h */
    typedef long f77int;
#else
    typedef int f77int;
#endif
typedef double f77r8; /* define the C object type that is equivalent to a REAL*8 in Fortran */
void closbf( f77int * );
void cmpmsg( char *, size_t );
void maxout( f77int * );
void openmb( f77int *, char *, f77int *, size_t );
void ufbint( f77int *, f77r8 *, f77int *, f77int *, f77int *, char *, size_t );
void dc_ghdr( char *, int *, char *, char *, char *, char *, char *, int *, int *,
	      size_t, size_t, size_t, size_t, size_t, size_t );
int lt_dcrp( char *, mbedtls_aes_context *, unsigned char *, int *,
	     unsigned short * );
void lt_gtim( int, unsigned char *, int *, int *,
	      f77r8 *, f77r8 *, f77r8 *, f77r8 *, f77r8 *, f77r8 * );
void lt_nprt( void );
void lt_pnbf( char *, int *, int *, int );
unsigned short lt_shrt( int, unsigned char *, int * );
float lt_sw2c( int, unsigned char *, int * );
void lt_trmm( void );
unsigned int lt_word( int, unsigned char *, int * );

/*
** This is the GAI reference date for the lightning data
*/
#define REF_DATE 320630400  /* 00:00 Feb 29 1980 in seconds since epoch */

/*
** We use this alot, just hardwire a constant for it
*/
#define TWO_24TH 0x01000000

/*
** Typedef to describe the input format
*/
typedef enum {
    NOAAPORT,
    TRMM
} input_type;

/*
** Fix bufrtable directory
*/
char bufrtable[100];

/*
** Number of keyfiles for decryption.
*/
#define NKF 2

/*
** Length (in bytes) of each keyfile.
*/
#define LENKF 16

/*
** Format of the input data
*/
static input_type input_format = NOAAPORT;