summaryrefslogtreecommitdiffstats
path: root/net/irda/irlap_comp.c
blob: 37d6a6fbfddfcd59a72924a0e1d157e8cb337ba8 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*********************************************************************
 *                
 * Filename:      irlap_comp.c
 * Version:       
 * Description:   
 * Status:        Experimental.
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Fri Oct  9 09:18:07 1998
 * Modified at:   Tue Oct  5 11:34:52 1999
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
 * Modified at:   Fri May 28  3:11 CST 1999
 * Modified by:   Horst von Brand <vonbrand@sleipnir.valparaiso.cl>
 * Sources:       ppp.c, isdn_ppp.c
 * 
 *     Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
 *      
 *     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 2 of 
 *     the License, or (at your option) any later version.
 *  
 *     Neither Dag Brattli nor University of Tromsø admit liability nor
 *     provide warranty for any of this software. This material is 
 *     provided "AS-IS" and at no charge.
 *     
 ********************************************************************/

#include <linux/string.h>

#include <net/irda/irda.h>
#include <net/irda/irqueue.h>
#include <net/irda/irlap.h>
#include <net/irda/irlap_comp.h>
#include "../../drivers/net/zlib.h"

hashbin_t *irlap_compressors = NULL;

/*
 * Function irda_register_compressor (cp)
 *
 *    Register new compressor with the IrLAP
 *
 */
int irda_register_compressor( struct compressor *cp)
{
	struct irda_compressor *new;

	IRDA_DEBUG( 4, __FUNCTION__ "()\n");

	/* Check if this compressor has been registred before */
	if ( hashbin_find ( irlap_compressors, cp->compress_proto, NULL)) {
		IRDA_DEBUG( 0, __FUNCTION__ "(), Compressor already registered\n");
                return 0;
        }
	
	/* Make new IrDA compressor */
        new = (struct irda_compressor *) 
		kmalloc( sizeof( struct irda_compressor), GFP_KERNEL);
        if (new == NULL)
                return 1;
		
	memset( new, 0, sizeof( struct irda_compressor));
        new->cp = cp;

	/* Insert IrDA compressor into hashbin */
	hashbin_insert( irlap_compressors, (irda_queue_t *) new, cp->compress_proto,
			NULL);
	
        return 0;
}

/*
 * Function irda_unregister_compressor (cp)
 *
 *    Unregister compressor
 *
 */
void irda_unregister_compressor ( struct compressor *cp)
{
	struct irda_compressor *node;

	IRDA_DEBUG( 4, __FUNCTION__ "()\n");

	node = hashbin_remove( irlap_compressors, cp->compress_proto, NULL);
	if ( !node) {
		IRDA_DEBUG( 0, __FUNCTION__ "(), compressor not found!\n");
		return;
	}	
	kfree( node);
}

/*
 * Function irda_set_compression (self, proto)
 *
 *    The compression protocol to be used by this session
 *
 */
int irda_set_compression( struct irlap_cb *self, int proto)
{
	struct compressor *cp;
	struct irda_compressor *comp;

	__u8 options[CILEN_DEFLATE];

	IRDA_DEBUG( 4, __FUNCTION__ "()\n");

	ASSERT( self != NULL, return -ENODEV;);
	ASSERT( self->magic == LAP_MAGIC, return -EBADR;);

	/* Initialize options */
	options[0] = CI_DEFLATE;
	options[1] = CILEN_DEFLATE;
	options[2] = DEFLATE_METHOD( DEFLATE_METHOD_VAL);
	options[3] = DEFLATE_CHK_SEQUENCE;

	comp = hashbin_find( irlap_compressors, proto, NULL);
	if ( !comp) {
		IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to find compressor\n");
		return -1;
	}

	cp = comp->cp;
	/* 
	 *  Compressor part
	 */
	if ( self->compressor.state != NULL)
		(*self->compressor.cp->comp_free)( self->compressor.state);
	self->compressor.state = NULL;
	
	self->compressor.cp = cp;
	self->compressor.state = cp->comp_alloc( options, sizeof( options));
	if ( self->compressor.state == NULL) {
		IRDA_DEBUG( 0, __FUNCTION__ "(), Failed!\n");
		return -ENOBUFS;
	}

	/* 
	 *  Decompress part
	 */
	
	if ( self->decompressor.state != NULL)
		irda_decomp_free( self->decompressor.state);
	self->decompressor.state = NULL;
	
	self->decompressor.cp = cp;
	self->decompressor.state = cp->decomp_alloc( options, sizeof( options));
	if ( self->decompressor.state == NULL) {
		IRDA_DEBUG( 0, __FUNCTION__ "(), Failed!\n");
		return -ENOBUFS;
	}
	return 0;
}

/*
 * Function irda_free_compression (self)
 *
 *    
 *
 */
void irda_free_compression( struct irlap_cb *self)
{
	IRDA_DEBUG( 4, __FUNCTION__ "()\n");

	if ( self->compressor.state) {
		irda_comp_free( self->compressor.state);
		self->compressor.state = NULL;
	}
	
	if ( self->decompressor.state) {
		irda_decomp_free( self->decompressor.state);
		self->decompressor.state = NULL;
	}
}

/*
 * Function irlap_compress_init (self)
 *
 *    
 *
 */
void irlap_compressor_init( struct irlap_cb *self, int compress)
{
	int debug = TRUE;
	__u8 options[CILEN_DEFLATE];

	IRDA_DEBUG(4, __FUNCTION__ "()\n");

	ASSERT( self != NULL, return;);
	ASSERT( self->magic == LAP_MAGIC, return;);
	
	/* Initialize options */
	options[0] = CI_DEFLATE;
	options[1] = CILEN_DEFLATE;
	options[2] = DEFLATE_METHOD_VAL;
	options[3] = DEFLATE_CHK_SEQUENCE;
	
	/*
	 *  We're agreeing to send compressed packets.
	 */
	if ( self->compressor.state == NULL) {
		IRDA_DEBUG( 0, __FUNCTION__ "(), state == NULL\n");
		return;
	}
	
	if ((*self->compressor.cp->comp_init)( self->compressor.state, 
					       options, sizeof( options),
					       0, 0, debug)) 
	{
		IRDA_DEBUG( 0, __FUNCTION__ "(), Compressor running!\n");
		/* ppp->flags |= SC_COMP_RUN; */
	}
	
	/*
	 *  Initialize decompressor
	 */
	if ( self->decompressor.state == NULL) {
		IRDA_DEBUG( 0, __FUNCTION__ "(), state == NULL\n");
		return;
	}
	
	if (( self->decompressor.cp->decomp_init)( self->decompressor.state,
						   options, sizeof( options),
						   0, 0, 0, debug)) 
	{
		IRDA_DEBUG( 0, __FUNCTION__ "(), Decompressor running!\n");	
		
		/* ppp->flags |= SC_DECOMP_RUN; */
		/* ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR); */
	}
}

/*
 * Function irlap_compress_frame (self, skb)
 *
 *
 *
 */
struct sk_buff *irlap_compress_frame( struct irlap_cb *self, 
				      struct sk_buff *skb)
{
	struct sk_buff *new_skb;
	int count;
	
	ASSERT( skb != NULL, return NULL;);
	
	IRDA_DEBUG(4, __FUNCTION__ "() skb->len=%d, jiffies=%ld\n", (int) skb->len,
	      jiffies);

	ASSERT( self != NULL, return NULL;);
	ASSERT( self->magic == LAP_MAGIC, return NULL;);

	/* Check if compressor got initialized */
	if ( self->compressor.state == NULL) {
		/* Tell peer that this frame is not compressed */
		skb_push( skb, LAP_COMP_HEADER);
		skb->data[0] = IRDA_NORMAL;

		return skb;
	}

	/* FIXME: Find out what is the max overhead (not 10) */
	new_skb = dev_alloc_skb( skb->len+LAP_MAX_HEADER+10);
	if(!new_skb)
		return skb;

	skb_reserve( new_skb, LAP_MAX_HEADER);
	skb_put( new_skb, skb->len+10);
	
	count = (self->compressor.cp->compress)( self->compressor.state, 
						 skb->data, new_skb->data, 
						 skb->len, new_skb->len);
	if( count <= 0) {
		IRDA_DEBUG(4, __FUNCTION__ "(), Unable to compress frame!\n");
		dev_kfree_skb( new_skb);

		/* Tell peer that this frame is not compressed */
		skb_push( skb, 1);
		skb->data[0] = IRDA_NORMAL;

		return skb;
	}
	skb_trim( new_skb, count);

	/* Tell peer that this frame is compressed */
	skb_push( new_skb, 1);
	new_skb->data[0] = IRDA_COMPRESSED;

	dev_kfree_skb( skb);
	
	IRDA_DEBUG(4, __FUNCTION__ "() new_skb->len=%d\n, jiffies=%ld", 
	      (int) new_skb->len, jiffies);
	
	return new_skb;
}

/*
 * Function irlap_decompress_frame (self, skb)
 *
 *    
 *
 */
struct sk_buff *irlap_decompress_frame( struct irlap_cb *self, 
					struct sk_buff *skb)
{
	struct sk_buff *new_skb;
	int count;

	IRDA_DEBUG( 4, __FUNCTION__ "() skb->len=%d\n", (int) skb->len);

	ASSERT( self != NULL, return NULL;);
	ASSERT( self->magic == LAP_MAGIC, return NULL;);

	ASSERT( self->compressor.state != NULL, return NULL;);

	/* Check if frame is compressed */
	if ( skb->data[0] == IRDA_NORMAL) {

		/* Remove compression header */
		skb_pull( skb, LAP_COMP_HEADER);

		/*
		 * The frame is not compressed. Pass it to the
		 * decompression code so it can update its
		 * dictionary if necessary.
		 */
		irda_incomp( self->decompressor.state, skb->data, skb->len);

		return skb;
	}

	/* Remove compression header */
	skb_pull( skb, LAP_COMP_HEADER);

	new_skb = dev_alloc_skb( 2048); /* FIXME: find the right size */
	if(!new_skb)
		return skb;
	skb_put( new_skb, 2048);

	count = irda_decompress( self->decompressor.state, skb->data, 
				 skb->len, new_skb->data, new_skb->len);
	if ( count <= 0) {
		IRDA_DEBUG( 4, __FUNCTION__ "(), Unable to decompress frame!\n");
		
		dev_kfree_skb( new_skb);
		return skb;
	}

	skb_trim( new_skb, count);
	
	IRDA_DEBUG( 4, __FUNCTION__ "() new_skb->len=%d\n", (int) new_skb->len);

	return new_skb;
}