/****************************************************************************
*																			*
*				Miscellaneous (Non-ASN.1) Routines Header File				*
*					  Copyright Peter Gutmann 1992-2025						*
*																			*
****************************************************************************/

#ifndef _PGPRW_DEFINED

#define _PGPRW_DEFINED

#include <time.h>
#if defined( INC_ALL )
  #include "misc_rw.h"
  #include "stream.h"
  #include "pgp.h"
#else
  #include "enc_dec/misc_rw.h"
  #include "io/stream.h"
  #include "misc/pgp.h"
#endif /* Compiler-specific includes */

/****************************************************************************
*																			*
*								Function Prototypes							*
*																			*
****************************************************************************/

/* Read/write PGP length values.  For the length encoding, 0...191 = 1 byte,
   192...8383 = 2 bytes, > 8383 = 0xFF marker + 4-byte encoding */

#define pgpSizeofLength( length ) \
		( ( ( length ) <= 191 ) ? 1 : \
		  ( ( length ) <= 8383 ) ? 2 : 5 )
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int pgpReadShortLength( INOUT_PTR STREAM *stream, 
						OUT_LENGTH_SHORT_Z int *length, 
						IN_BYTE const int ctb );
CHECK_RETVAL_SPECIAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int pgpReadPartialLength( INOUT_PTR STREAM *stream, 
						  OUT_LENGTH_Z int *length );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int pgpWriteLength( INOUT_PTR STREAM *stream, 
					IN_LENGTH const int length );

/* Read/write PGP packet headers.  The difference between 
   pgpReadPacketHeader() and pgpReadPacketHeaderI() is that the latter 
   allows indefinite-length encoding for partial lengths.  Once we've
   read an indefinite length, we have to use pgpReadPartialLengh() to
   read subsequence partial-length values */

CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int pgpReadPacketHeader( INOUT_PTR STREAM *stream, OUT_OPT_BYTE int *ctb, 
						 OUT_OPT_LENGTH_Z int *length, 
						 IN_LENGTH_SHORT const int minLength,
						 IN_LENGTH const int maxLength );
CHECK_RETVAL_SPECIAL STDC_NONNULL_ARG( ( 1 ) ) \
int pgpReadPacketHeaderI( INOUT_PTR STREAM *stream, OUT_OPT_BYTE int *ctb, 
						  OUT_OPT_LENGTH_Z int *length, 
						  IN_LENGTH_SHORT const int minLength );
CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int pgpWritePacketHeader( INOUT_PTR STREAM *stream, 
						  IN_ENUM( PGP_PACKET ) \
							const PGP_PACKET_TYPE packetType,
						  IN_LENGTH const int length );

#endif /* _PGPRW_DEFINED */
