summaryrefslogtreecommitdiffstats
path: root/stdin.c
blob: ab95204d46b365457ed1e5d5536158ee53902fcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*	Copyright (C) 2021 Gentoo-libre Install

	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"

void stdinMode(char *buffer, int wrap, bool decode, char argv[])
{
//wrap encoded lines after COLS character (default 76).
int size = DEFAULT_SIZE-1;

int i = 0;//position in string
moreInput: while (i < size)
	{
	//yes, reading one byte at a time is slow; but you shouldn't be dumping a file into stdin anyway.
	if (!(fread(buffer+i, 1, 1*sizeof(unsigned char), stdin))){goto process;}//Goes to process if EOF or if fread fails
	++i;//increment i so preiously read char isn't overwritten.
	}
//realloc if input could have exceeded buffer
size += DEFAULT_SIZE;
buffer = realloc(buffer,size*sizeof(unsigned char));
if (!buffer){fprintf(stderr,"Realloc Failed!\n"); exit(1);}
goto moreInput;

process:;
buffer[i] = '\0';//add null char since function expects it

int result;

if (!decode)
	{
	char *output = base64Encode(buffer, i);
	if (!output){fprintf(stderr, "Malloc Failed!\n"); exit(1);}
	if (wrap == 0){result = fwrite(output, 1, sizeof(unsigned char)*strlen(output), stdout); goto leave;}//if wrap is set to 0, do no wrapping.

	int charactersLeft = strlen(output);//the total number of characters to print is strlen(output_
	int x = 0;//position to print from in string so wrap printing works

	while (charactersLeft > wrap)//keep looping until all of the wrap length writes are done
		{
		result = fwrite(output+x, 1, sizeof(unsigned char)*wrap, stdout);//write wrap number of characters at a time
		if (!result){fprintf(stderr, "Fwrite Failed!\n"); exit(1);}//fwrite to stdout should not fail. It's best to just exit if it does.
		putchar('\n');//add a newline to finish off the wrapped line
		x += wrap;//increase the printing position to the next position
		charactersLeft -= wrap;//decrease charactersLeft by how many were printed
		}

	if (charactersLeft == 0){goto leave;}//if the printed number of characters was perfect, don't print an extra newline

	result = fwrite(output+x, 1, sizeof(unsigned char)*charactersLeft, stdout);//print any remaining characters that are smaller in number than wrap.
	if (!result){fprintf(stderr, "Fwrite Failed!\n"); exit(1);}//fwrite to stdout should not fail. It's best to just exit if it does.
	putchar('\n');//add a newline to finish off the wrapped line
	goto leave;
	}

if (decode)
	{
	int outputLength = base64Decode(buffer, i);
	if (outputLength == -1){fprintf(stderr,"%s: invalid input\n", argv); free(buffer); exit(1);}//base64 returns -1 if input string was invalid
	result = fwrite(buffer, 1, sizeof(unsigned char)*outputLength, stdout);
	goto leave;
	}

leave: free(buffer);
exit(0);
}