aboutsummaryrefslogtreecommitdiff
path: root/swap.c
blob: 28aad1bfadd932e3b74a1873a6276e1fb441df2c (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*  Copyright 2021 Alain Knaff.
 *  This file is part of mtools.
 *
 *  Mtools 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.
 *
 *  Mtools 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 Mtools.  If not, see <http://www.gnu.org/licenses/>.
 *
 * filter to support byte-swapped filesystems
 */

#include "sysincludes.h"
#include "msdos.h"
#include "mtools.h"
#include "swap.h"

typedef struct Swap_t {
	struct Stream_t head;
} Swap_t;

static void swap_buffer(char *buf, size_t len)
{
	unsigned int i;
	for (i=0; i<len; i+=2) {
		char temp = buf[i];
		buf[i] = buf[i+1];
		buf[i+1] = temp;
	}
}


static ssize_t swap_pread(Stream_t *Stream, char *buf,
			  mt_off_t where, size_t len)
{
	DeclareThis(Swap_t);

	ssize_t result = PREADS(This->head.Next, buf, where, len);
	if(result < 0)
		return result;
	swap_buffer( buf, (size_t) result);
	return result;
}

static ssize_t swap_pwrite(Stream_t *Stream, char *buf,
			  mt_off_t where, size_t len)
{
	DeclareThis(Swap_t);

	ssize_t result;
	char *swapping = malloc( len );
	memcpy( swapping, buf, len );
	swap_buffer( swapping, len );

	result = PWRITES(This->head.Next, swapping, where, len);

	free(swapping);
	return result;
}


static Class_t SwapClass = {
	0,
	0,
	swap_pread,
	swap_pwrite,
	0, /* flush */
	0, /* free */
	set_geom_pass_through, /* set_geom */
	0, /* get_data */
	0, /* pre-allocate */
	get_dosConvert_pass_through, /* dos convert */
	0, /* discard */
};

Stream_t *OpenSwap(Stream_t *Next) {
	Swap_t *This;

	This = New(Swap_t);
	if (!This){
		printOom();
		return 0;
	}
	memset((void*)This, 0, sizeof(Swap_t));
	init_head(&This->head, &SwapClass, Next);

	return &This->head;
}