<commit_before>/* crypto/bio/bss_file_amiga.c */
/* This file introduces Amiga specific file handling which uses
 * buffered dos.library calls.
 */

#ifdef AMISSL
#include <proto/exec.h>
#include <proto/dos.h>
#include <dos/dos.h>
#include <dos/stdio.h>
#include <string.h>

#include <openssl/bio.h>
#include <openssl/err.h>

extern int __io2errno(int);

static int file_write(BIO *h, const char *buf, int num);
static int file_read(BIO *h, char *buf, int size);
static int file_puts(BIO *h, const char *str);
static int file_gets(BIO *h, char *str, int size);
static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int file_new(BIO *h);
static int file_free(BIO *data);

static BIO_METHOD methods_filep =
{
	BIO_TYPE_FILE,
	"FILE pointer (Amiga)",
	file_write,
	file_read,
	file_puts,
	file_gets,
	file_ctrl,
	file_new,
	file_free,
	NULL
};

BIO_METHOD *BIO_s_file(void);

#ifdef __amigaos4__
#define FSeek(file, pos, offset) Seek(file, pos, offset)
#else /* __amigaos4__ */
#define FFlush(file) Flush(file)

static BPTR FOpen(STRPTR name, LONG mode, LONG buffer_size)
{
	BPTR fh = Open(name, mode);

	if (fh && ((struct Library *)DOSBase)->lib_Version >= 39)
		SetVBuf(fh, NULL, IsInteractive(fh) ? BUF_LINE : BUF_FULL, buffer_size);

	return(fh);
}

static void FClose(BPTR file)
{
	Flush(file);
	Close(file);
}

static LONG FSeek(BPTR file, LONG pos, LONG mode)
{
	LONG ret;

	if (Flush(file))
		ret = Seek(file, pos, mode);
	else
		ret = -1;

	return(ret);
}
#endif /* __amigaos4__ */

static BPTR FOpenFromMode(char *name, char *mode)
{
	BOOL mode_is_valid = TRUE, seek_to_end = FALSE;
	BPTR file = (BPTR)NULL;
	LONG type;

	if (*mode == 'r')
		type = MODE_OLDFILE;
	else if (*mode == 'w')
		type = MODE_NEWFILE;
	else if (*mode == 'a')
	{
		type = MODE_READWRITE;
		seek_to_end = TRUE;
	}
	else
		mode_is_valid = FALSE;

	if (mode_is_valid)
	{
		file = FOpen(name, type, 16384);

		if (file && seek_to_end)
			Seek(file, 0, OFFSET_END);
	}

	return(file);
}

static BOOL FEOF(BPTR file)
{
	LONG curr_position, end_position;
	BOOL is_eof = FALSE;

	if ((curr_position = FSeek(file, 0, OFFSET_END)) >= 0)
		if ((end_position = FSeek(file, curr_position, OFFSET_BEGINNING)) >= 0)
			is_eof = curr_position == end_position;

	return(is_eof);
}

BIO *BIO_new_file(const char *filename, const char *mode)
{
	BIO *ret;
	BPTR file;

	if (file = FOpenFromMode((char *)filename, (char *)mode))
	{
		if (ret = BIO_new(BIO_s_file_internal()))
			BIO_set_fp_amiga(ret, file, BIO_CLOSE);
	}
	else
	{
		SYSerr(SYS_F_FOPEN, __io2errno(IoErr()));
		ERR_add_error_data(5, "FOpenFromMode('", filename, "','", mode, "')");

		if (IoErr() == ERROR_OBJECT_NOT_FOUND)
			BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
		else
			BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);

		ret = NULL;
	}

	return(ret);
}

BIO *BIO_new_fp_amiga(BPTR stream, int close_flag)
{
	BIO *ret;

	if (ret = BIO_new(BIO_s_file()))
		BIO_set_fp_amiga(ret, stream, close_flag);

	return(ret);
}

BIO_METHOD *BIO_s_file(void)
{
	return(&methods_filep);<commit_msg>- added some wrappers for BIO_set_fp() and BIO_new_fp() so that in case FILE* support   is enabled in AmiSSL the shared library has proper wrappers to BPTR.<commit_after>/* crypto/bio/bss_file_amiga.c */
/* This file introduces Amiga specific file handling which uses
 * buffered dos.library calls.
 */

#ifdef AMISSL
#include <proto/exec.h>
#include <proto/dos.h>
#include <dos/dos.h>
#include <dos/stdio.h>
#include <string.h>

#include <openssl/bio.h>
#include <openssl/err.h>

extern int __io2errno(int);

static int file_write(BIO *h, const char *buf, int num);
static int file_read(BIO *h, char *buf, int size);
static int file_puts(BIO *h, const char *str);
static int file_gets(BIO *h, char *str, int size);
static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int file_new(BIO *h);
static int file_free(BIO *data);

static BIO_METHOD methods_filep =
{
	BIO_TYPE_FILE,
	"FILE pointer (Amiga)",
	file_write,
	file_read,
	file_puts,
	file_gets,
	file_ctrl,
	file_new,
	file_free,
	NULL
};

BIO_METHOD *BIO_s_file(void);

#ifdef __amigaos4__
#define FSeek(file, pos, offset) Seek(file, pos, offset)
#else /* __amigaos4__ */
#define FFlush(file) Flush(file)

static BPTR FOpen(STRPTR name, LONG mode, LONG buffer_size)
{
	BPTR fh = Open(name, mode);

	if (fh && ((struct Library *)DOSBase)->lib_Version >= 39)
		SetVBuf(fh, NULL, IsInteractive(fh) ? BUF_LINE : BUF_FULL, buffer_size);

	return(fh);
}

static void FClose(BPTR file)
{
	Flush(file);
	Close(file);
}

static LONG FSeek(BPTR file, LONG pos, LONG mode)
{
	LONG ret;

	if (Flush(file))
		ret = Seek(file, pos, mode);
	else
		ret = -1;

	return(ret);
}
#endif /* __amigaos4__ */

static BPTR FOpenFromMode(char *name, char *mode)
{
	BOOL mode_is_valid = TRUE, seek_to_end = FALSE;
	BPTR file = (BPTR)NULL;
	LONG type;

	if (*mode == 'r')
		type = MODE_OLDFILE;
	else if (*mode == 'w')
		type = MODE_NEWFILE;
	else if (*mode == 'a')
	{
		type = MODE_READWRITE;
		seek_to_end = TRUE;
	}
	else
		mode_is_valid = FALSE;

	if (mode_is_valid)
	{
		file = FOpen(name, type, 16384);

		if (file && seek_to_end)
			Seek(file, 0, OFFSET_END);
	}

	return(file);
}

static BOOL FEOF(BPTR file)
{
	LONG curr_position, end_position;
	BOOL is_eof = FALSE;

	if ((curr_position = FSeek(file, 0, OFFSET_END)) >= 0)
		if ((end_position = FSeek(file, curr_position, OFFSET_BEGINNING)) >= 0)
			is_eof = curr_position == end_position;

	return(is_eof);
}

BIO *BIO_new_file(const char *filename, const char *mode)
{
	BIO *ret;
	BPTR file;

	if (file = FOpenFromMode((char *)filename, (char *)mode))
	{
		if (ret = BIO_new(BIO_s_file_internal()))
			BIO_set_fp_amiga(ret, file, BIO_CLOSE);
	}
	else
	{
		SYSerr(SYS_F_FOPEN, __io2errno(IoErr()));
		ERR_add_error_data(5, "FOpenFromMode('", filename, "','", mode, "')");

		if (IoErr() == ERROR_OBJECT_NOT_FOUND)
			BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE);
		else
			BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB);

		ret = NULL;
	}

	return(ret);
}

BIO *BIO_new_fp_amiga(BPTR stream, int close_flag)
{
	BIO *ret;

	if (ret = BIO_new(BIO_s_file()))
		BIO_set_fp_amiga(ret, stream, close_flag);

	return(ret);
}

#if !defined(OPENSSL_NO_FP_API)
/* Only stdin, stdout and stderr are used in OpenSSL test applications
 * and openssl program, so the following function should cover all cases.
 */
static BPTR GetFileBPTR(const char *func_name, FILE *fp)
{
  BPTR ret;

  if (fp == stdout || (fp == stderr && !ErrorOutput()))
    ret = Output();
  else if (fp == stderr)
    ret = ErrorOutput();
  else if (fp == stdin)
    ret = Input();
  else
  {
    Printf("*** WARNING (%s): %lx is neither stdin (%lx), stdout (%lx) nor stderr (%lx) in GetFileBPTR in stubs, returning NULL!\n", func_name, fp, stdin, stdout, stderr);
    ret = (BPTR)NULL;
  }

  if (ret)
    FFlush(ret);

  return(ret);
}

long (BIO_set_fp)(BIO *b, FILE *fp, int closeflag)
{
  fflush(fp); // Flush the file if there are pending writes. After this point we cannot repair anything out of sync

  return(BIO_set_fp_amiga(b, GetFileBPTR("BIO_set_fp", fp),
                          (closeflag & ~BIO_CLOSE) | BIO_NOCLOSE)); // We cannot allow someone else to close the file
}

BIO *(BIO_new_fp)(FILE *stream, int closeflag)
{
  BIO *ret;

  fflush(stream); // Flush the file if there are pending writes. After this point we cannot repair anything out of sync

  if((ret = BIO_new(BIO_s_file())) != NULL)
    BIO_set_fp_amiga(ret, GetFileBPTR("BIO_new_fp", stream),
                     (closeflag & ~BIO_CLOSE) | BIO_NOCLOSE); // We cannot allow someone else to close the file

  return(ret);
}
#endif

BIO_METHOD *BIO_s_file(void)
{
	return(&methods_filep);
{'fraction_non_alphanumeric': 0.1396167383652718, 'fraction_numerical': 0.005214444009907443, 'mean_word_length': 4.391426563598032, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4642896', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3062, 'n_words': 803}