1*d5c9a868SElliott Hughes /* Copyright 1996,1997,1999,2001-2003,2008,2009,2021 Alain Knaff.
2*d5c9a868SElliott Hughes * This file is part of mtools.
3*d5c9a868SElliott Hughes *
4*d5c9a868SElliott Hughes * Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes * it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes * the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes * (at your option) any later version.
8*d5c9a868SElliott Hughes *
9*d5c9a868SElliott Hughes * Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*d5c9a868SElliott Hughes * GNU General Public License for more details.
13*d5c9a868SElliott Hughes *
14*d5c9a868SElliott Hughes * You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes */
17*d5c9a868SElliott Hughes
18*d5c9a868SElliott Hughes #include "sysincludes.h"
19*d5c9a868SElliott Hughes #include "msdos.h"
20*d5c9a868SElliott Hughes #include "mtools.h"
21*d5c9a868SElliott Hughes #include "codepage.h"
22*d5c9a868SElliott Hughes
23*d5c9a868SElliott Hughes #define U2D_BUFSIZE 4096
24*d5c9a868SElliott Hughes
25*d5c9a868SElliott Hughes typedef struct Filter_t {
26*d5c9a868SElliott Hughes struct Stream_t head;
27*d5c9a868SElliott Hughes
28*d5c9a868SElliott Hughes char buffer[U2D_BUFSIZE];
29*d5c9a868SElliott Hughes
30*d5c9a868SElliott Hughes size_t readBytes; /* how many bytes read into buffer */
31*d5c9a868SElliott Hughes size_t bufPos; /* position in buffer */
32*d5c9a868SElliott Hughes
33*d5c9a868SElliott Hughes bool pendingNl;
34*d5c9a868SElliott Hughes bool eof;
35*d5c9a868SElliott Hughes } Filter_t;
36*d5c9a868SElliott Hughes
37*d5c9a868SElliott Hughes /* Add CR before NL, and 0x1a at end of file */
read_filter(Stream_t * Stream,char * output,size_t len)38*d5c9a868SElliott Hughes static ssize_t read_filter(Stream_t *Stream, char *output, size_t len)
39*d5c9a868SElliott Hughes {
40*d5c9a868SElliott Hughes DeclareThis(Filter_t);
41*d5c9a868SElliott Hughes size_t i;
42*d5c9a868SElliott Hughes
43*d5c9a868SElliott Hughes if(This->eof)
44*d5c9a868SElliott Hughes return 0;
45*d5c9a868SElliott Hughes
46*d5c9a868SElliott Hughes for(i=0; i < len && !This->eof; i++) {
47*d5c9a868SElliott Hughes char c;
48*d5c9a868SElliott Hughes if(This->pendingNl) {
49*d5c9a868SElliott Hughes c='\n';
50*d5c9a868SElliott Hughes This->pendingNl=false;
51*d5c9a868SElliott Hughes } else {
52*d5c9a868SElliott Hughes if(This->bufPos == This->readBytes) {
53*d5c9a868SElliott Hughes ssize_t ret = READS(This->head.Next,
54*d5c9a868SElliott Hughes This->buffer,
55*d5c9a868SElliott Hughes U2D_BUFSIZE);
56*d5c9a868SElliott Hughes if(ret < 0) {
57*d5c9a868SElliott Hughes /* an error */
58*d5c9a868SElliott Hughes /* If we already have read some data,
59*d5c9a868SElliott Hughes * first return count of that data
60*d5c9a868SElliott Hughes * before returning error */
61*d5c9a868SElliott Hughes if(i == 0)
62*d5c9a868SElliott Hughes return -1;
63*d5c9a868SElliott Hughes else
64*d5c9a868SElliott Hughes break;
65*d5c9a868SElliott Hughes }
66*d5c9a868SElliott Hughes This->readBytes = (size_t) ret;
67*d5c9a868SElliott Hughes This->bufPos = 0;
68*d5c9a868SElliott Hughes }
69*d5c9a868SElliott Hughes
70*d5c9a868SElliott Hughes if(This->bufPos == This->readBytes) {
71*d5c9a868SElliott Hughes /* Still at end of buffer, must be end
72*d5c9a868SElliott Hughes of file */
73*d5c9a868SElliott Hughes c='\032';
74*d5c9a868SElliott Hughes This->eof=true;
75*d5c9a868SElliott Hughes } else {
76*d5c9a868SElliott Hughes c = This->buffer[This->bufPos++];
77*d5c9a868SElliott Hughes if(c == '\n') {
78*d5c9a868SElliott Hughes This->pendingNl=true;
79*d5c9a868SElliott Hughes c = '\r';
80*d5c9a868SElliott Hughes }
81*d5c9a868SElliott Hughes }
82*d5c9a868SElliott Hughes }
83*d5c9a868SElliott Hughes output[i]=c;
84*d5c9a868SElliott Hughes }
85*d5c9a868SElliott Hughes
86*d5c9a868SElliott Hughes return (ssize_t) i;
87*d5c9a868SElliott Hughes }
88*d5c9a868SElliott Hughes
89*d5c9a868SElliott Hughes static Class_t FilterClass = {
90*d5c9a868SElliott Hughes read_filter,
91*d5c9a868SElliott Hughes 0,
92*d5c9a868SElliott Hughes 0,
93*d5c9a868SElliott Hughes 0,
94*d5c9a868SElliott Hughes 0, /* flush */
95*d5c9a868SElliott Hughes 0,
96*d5c9a868SElliott Hughes 0, /* set geometry */
97*d5c9a868SElliott Hughes get_data_pass_through,
98*d5c9a868SElliott Hughes 0,
99*d5c9a868SElliott Hughes 0, /* get_dosconvert */
100*d5c9a868SElliott Hughes 0 /* discard */
101*d5c9a868SElliott Hughes };
102*d5c9a868SElliott Hughes
open_unix2dos(Stream_t * Next,int convertCharset UNUSEDP)103*d5c9a868SElliott Hughes Stream_t *open_unix2dos(Stream_t *Next, int convertCharset UNUSEDP)
104*d5c9a868SElliott Hughes {
105*d5c9a868SElliott Hughes Filter_t *This;
106*d5c9a868SElliott Hughes
107*d5c9a868SElliott Hughes This = New(Filter_t);
108*d5c9a868SElliott Hughes if (!This)
109*d5c9a868SElliott Hughes return NULL;
110*d5c9a868SElliott Hughes init_head(&This->head, &FilterClass, Next);
111*d5c9a868SElliott Hughes
112*d5c9a868SElliott Hughes This->readBytes = This->bufPos = 0;
113*d5c9a868SElliott Hughes This->pendingNl = false;
114*d5c9a868SElliott Hughes This->eof = false;
115*d5c9a868SElliott Hughes
116*d5c9a868SElliott Hughes return &This->head;
117*d5c9a868SElliott Hughes }
118