1*d5c9a868SElliott Hughes /* Copyright 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 * filter to support byte-swapped filesystems
18*d5c9a868SElliott Hughes */
19*d5c9a868SElliott Hughes
20*d5c9a868SElliott Hughes #include "sysincludes.h"
21*d5c9a868SElliott Hughes #include "msdos.h"
22*d5c9a868SElliott Hughes #include "mtools.h"
23*d5c9a868SElliott Hughes #include "swap.h"
24*d5c9a868SElliott Hughes
25*d5c9a868SElliott Hughes typedef struct Swap_t {
26*d5c9a868SElliott Hughes struct Stream_t head;
27*d5c9a868SElliott Hughes } Swap_t;
28*d5c9a868SElliott Hughes
swap_buffer(char * buf,size_t len)29*d5c9a868SElliott Hughes static void swap_buffer(char *buf, size_t len)
30*d5c9a868SElliott Hughes {
31*d5c9a868SElliott Hughes unsigned int i;
32*d5c9a868SElliott Hughes for (i=0; i<len; i+=2) {
33*d5c9a868SElliott Hughes char temp = buf[i];
34*d5c9a868SElliott Hughes buf[i] = buf[i+1];
35*d5c9a868SElliott Hughes buf[i+1] = temp;
36*d5c9a868SElliott Hughes }
37*d5c9a868SElliott Hughes }
38*d5c9a868SElliott Hughes
39*d5c9a868SElliott Hughes
swap_pread(Stream_t * Stream,char * buf,mt_off_t where,size_t len)40*d5c9a868SElliott Hughes static ssize_t swap_pread(Stream_t *Stream, char *buf,
41*d5c9a868SElliott Hughes mt_off_t where, size_t len)
42*d5c9a868SElliott Hughes {
43*d5c9a868SElliott Hughes DeclareThis(Swap_t);
44*d5c9a868SElliott Hughes
45*d5c9a868SElliott Hughes ssize_t result = PREADS(This->head.Next, buf, where, len);
46*d5c9a868SElliott Hughes if(result < 0)
47*d5c9a868SElliott Hughes return result;
48*d5c9a868SElliott Hughes swap_buffer( buf, (size_t) result);
49*d5c9a868SElliott Hughes return result;
50*d5c9a868SElliott Hughes }
51*d5c9a868SElliott Hughes
swap_pwrite(Stream_t * Stream,char * buf,mt_off_t where,size_t len)52*d5c9a868SElliott Hughes static ssize_t swap_pwrite(Stream_t *Stream, char *buf,
53*d5c9a868SElliott Hughes mt_off_t where, size_t len)
54*d5c9a868SElliott Hughes {
55*d5c9a868SElliott Hughes DeclareThis(Swap_t);
56*d5c9a868SElliott Hughes
57*d5c9a868SElliott Hughes ssize_t result;
58*d5c9a868SElliott Hughes char *swapping = malloc( len );
59*d5c9a868SElliott Hughes memcpy( swapping, buf, len );
60*d5c9a868SElliott Hughes swap_buffer( swapping, len );
61*d5c9a868SElliott Hughes
62*d5c9a868SElliott Hughes result = PWRITES(This->head.Next, swapping, where, len);
63*d5c9a868SElliott Hughes
64*d5c9a868SElliott Hughes free(swapping);
65*d5c9a868SElliott Hughes return result;
66*d5c9a868SElliott Hughes }
67*d5c9a868SElliott Hughes
68*d5c9a868SElliott Hughes
69*d5c9a868SElliott Hughes static Class_t SwapClass = {
70*d5c9a868SElliott Hughes 0,
71*d5c9a868SElliott Hughes 0,
72*d5c9a868SElliott Hughes swap_pread,
73*d5c9a868SElliott Hughes swap_pwrite,
74*d5c9a868SElliott Hughes 0, /* flush */
75*d5c9a868SElliott Hughes 0, /* free */
76*d5c9a868SElliott Hughes set_geom_pass_through, /* set_geom */
77*d5c9a868SElliott Hughes 0, /* get_data */
78*d5c9a868SElliott Hughes 0, /* pre-allocate */
79*d5c9a868SElliott Hughes get_dosConvert_pass_through, /* dos convert */
80*d5c9a868SElliott Hughes 0, /* discard */
81*d5c9a868SElliott Hughes };
82*d5c9a868SElliott Hughes
OpenSwap(Stream_t * Next)83*d5c9a868SElliott Hughes Stream_t *OpenSwap(Stream_t *Next) {
84*d5c9a868SElliott Hughes Swap_t *This;
85*d5c9a868SElliott Hughes
86*d5c9a868SElliott Hughes This = New(Swap_t);
87*d5c9a868SElliott Hughes if (!This){
88*d5c9a868SElliott Hughes printOom();
89*d5c9a868SElliott Hughes return 0;
90*d5c9a868SElliott Hughes }
91*d5c9a868SElliott Hughes memset((void*)This, 0, sizeof(Swap_t));
92*d5c9a868SElliott Hughes init_head(&This->head, &SwapClass, Next);
93*d5c9a868SElliott Hughes
94*d5c9a868SElliott Hughes return &This->head;
95*d5c9a868SElliott Hughes }
96