summaryrefslogtreecommitdiffstats
path: root/kio/misc/kntlm/kswap.h
blob: e9db968ed81d3da65d2aac9cd73ca096feedba9c (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
   This file is part of the KDE libraries.
   Copyright (c) 2004 Szombathelyi György <gyurco@freemail.hu>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#ifndef KSWAP_H
#define KSWAP_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <qglobal.h>

/** 
 * \defgroup KSWAP Byte-swapping functions
 * kswap.h contains functions that will help converting
 * 16, 32 and 64 bit length data between little-endian and
 * big-endian representations.
 *
 * The KSWAP_16, KSWAP_32 and KSWAP_64 functions are always
 * swaps the byte order of the supplied argument (which should be
 * 16, 32 or 64 bit wide). These functions are inline, and tries to
 * use the most optimized function of the underlying system
 * (bswap_xx functions from byteswap.h in GLIBC, or ntohs and ntohl
 * on little-endian machines, and if neither are applicable, some fast
 * custom code).
 *
 * The KFromTo{Little|Big}Endian functions are for converting big-endian and 
 * little-endian data to and from the machine endianness.
 */

#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>

  inline Q_UINT16 KSWAP_16( Q_UINT16 b ) { return bswap_16( b ); }
  inline Q_INT16 KSWAP_16( Q_INT16 b ) { return bswap_16( (Q_UINT16)b ); }
  inline Q_UINT32 KSWAP_32( Q_UINT32 b ) { return bswap_32( b ); }
  inline Q_INT32 KSWAP_32( Q_INT32 b ) { return bswap_32( (Q_UINT32)b ); }
  inline Q_UINT64 KSWAP_64( Q_UINT64 b ) { return bswap_64( b ); }
  inline Q_INT64 KSWAP_64( Q_INT64 b ) { return bswap_64( (Q_UINT64)b ); }

#else /* HAVE_BYTESWAP_H */
#ifdef WORDS_BIGENDIAN
  inline Q_UINT16 KSWAP_16( Q_UINT16 b ) 
  { 
    return (((b) & 0x00ff) << 8 | ((b) & 0xff00) >> 8); 
  }

  inline Q_INT16 KSWAP_16( Q_INT16 b ) 
  { 
    return ((((Q_UINT16)b) & 0x00ff) << 8 | (((Q_UINT16)b) & 0xff00) >> 8); 
  }

  inline Q_UINT32 KSWAP_32( Q_UINT32 b ) 
  {
    return
      ((((b) & 0xff000000) >> 24) | (((b) & 0x00ff0000) >>  8) | \
       (((b) & 0x0000ff00) <<  8) | (((b) & 0x000000ff) << 24)); 
  }

  inline Q_INT32 KSWAP_32( Q_INT32 b ) 
  {
    return 
      (((((Q_UINT32)b) & 0xff000000) >> 24) | ((((Q_UINT32)b) & 0x00ff0000) >>  8) | \
       ((((Q_UINT32)b) & 0x0000ff00) <<  8) | ((((Q_UINT32)b) & 0x000000ff) << 24)); 
  }
#else /* WORDS_BIGENDIAN */
#include <sys/types.h>
#include <netinet/in.h>

  inline Q_UINT16 KSWAP_16( Q_UINT16 b ) { return htons(b); }
  inline Q_INT16 KSWAP_16( Q_INT16 b ) { return htons((Q_UINT16)b); }
  inline Q_UINT32 KSWAP_32( Q_UINT32 b ) { return htonl(b); }
  inline Q_INT32 KSWAP_32( Q_INT32 b ) { return htonl((Q_UINT32)b); }
#endif
  inline Q_UINT64 KSWAP_64( Q_UINT64 b ) 
  {
    union { 
        Q_UINT64 ll;
        Q_UINT32 l[2]; 
    } w, r;
    w.ll = b;
    r.l[0] = KSWAP_32( w.l[1] );
    r.l[1] = KSWAP_32( w.l[0] );
    return r.ll;
  }

  inline Q_INT64 KSWAP_64( Q_INT64 b ) 
  {
    union { 
        Q_UINT64 ll;
        Q_UINT32 l[2]; 
    } w, r;
    w.ll = (Q_UINT64) b;
    r.l[0] = KSWAP_32( w.l[1] );
    r.l[1] = KSWAP_32( w.l[0] );
    return r.ll;
  }
#endif	/* !HAVE_BYTESWAP_H */

/**
 * \ingroup KSWAP
 * Converts a 16 bit unsigned value from/to big-endian byte order to/from the machine order.
 */
inline Q_UINT16 KFromToBigEndian( Q_UINT16 b )
{
#ifdef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_16(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 16 bit unsigned array from/to big-endian byte order to/from the machine order.
 */
inline void KFromToBigEndian( Q_UINT16 *out, Q_UINT16 *in, uint len )
{
#ifdef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<1 ) ;
#else
  while ( len>0 ) { *out = KSWAP_16( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 32 bit unsigned value from/to big-endian byte order to/from the machine order.
 */
inline Q_UINT32 KFromToBigEndian( Q_UINT32 b )
{
#ifdef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_32(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 32 bit unsigned array from/to big-endian byte order to/from the machine order.
 */
inline void KFromToBigEndian( Q_UINT32 *out, Q_UINT32 *in, uint len )
{
#ifdef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<2 ) ;
#else
  while ( len>0 ) { *out = KSWAP_32( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 64 bit unsigned value from/to big-endian byte order to/from the machine order.
 */
inline Q_UINT64 KFromToBigEndian( Q_UINT64 b )
{
#ifdef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_64(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 64 bit unsigned array from/to big-endian byte order to/from the machine order.
 */
inline void KFromToBigEndian( Q_UINT64 *out, Q_UINT64 *in, uint len )
{
#ifdef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<3 ) ;
#else
  while ( len>0 ) { *out = KSWAP_64( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 16 bit signed value from/to big-endian byte order to/from the machine order.
 */
inline Q_INT16 KFromToBigEndian( Q_INT16 b )
{
#ifdef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_16(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 16 bit signed array from/to big-endian byte order to/from the machine order.
 */
inline void KFromToBigEndian( Q_INT16 *out, Q_INT16 *in, uint len )
{
#ifdef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<1 ) ;
#else
  while ( len>0 ) { *out = KSWAP_16( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 32 bit signed value from/to big-endian byte order to/from the machine order.
 */
inline Q_INT32 KFromToBigEndian( Q_INT32 b )
{
#ifdef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_32(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 32 bit signed array from/to big-endian byte order to/from the machine order.
 */
inline void KFromToBigEndian( Q_INT32 *out, Q_INT32 *in, uint len )
{
#ifdef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<2 ) ;
#else
  while ( len>0 ) { *out = KSWAP_32( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 64 bit signed value from/to big-endian byte order to/from the machine order.
 */
inline Q_INT64 KFromToBigEndian( Q_INT64 b )
{
#ifdef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_64(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 64 bit signed array from/to big-endian byte order to/from the machine order.
 */
inline void KFromToBigEndian( Q_INT64 *out, Q_INT64 *in, uint len )
{
#ifdef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<3 ) ;
#else
  while ( len>0 ) { *out = KSWAP_64( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 16 bit unsigned value from/to little-endian byte order to/from the machine order.
 */
inline Q_UINT16 KFromToLittleEndian( Q_UINT16 b )
{
#ifndef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_16(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 16 bit unsigned array from/to little-endian byte order to/from the machine order.
 */
inline void KFromToLittleEndian( Q_UINT16 *out, Q_UINT16 *in, uint len )
{
#ifndef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<1 ) ;
#else
  while ( len>0 ) { *out = KSWAP_16( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 32 bit unsigned value from/to little-endian byte order to/from the machine order.
 */
inline Q_UINT32 KFromToLittleEndian( Q_UINT32 b )
{
#ifndef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_32(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 32 bit unsigned array from/to little-endian byte order to/from the machine order.
 */
inline void KFromToLittleEndian( Q_UINT32 *out, Q_UINT32 *in, uint len )
{
#ifndef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<2 ) ;
#else
  while ( len>0 ) { *out = KSWAP_32( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 64 bit unsigned value from/to little-endian byte order to/from the machine order.
 */
inline Q_UINT64 KFromToLittleEndian( Q_UINT64 b )
{
#ifndef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_64(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 64 bit unsigned array from/to little-endian byte order to/from the machine order.
 */
inline void KFromToLittleEndian( Q_UINT64 *out, Q_UINT64 *in, uint len )
{
#ifndef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<3 ) ;
#else
  while ( len>0 ) { *out = KSWAP_64( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 16 bit signed value from/to little-endian byte order to/from the machine order.
 */
inline Q_INT16 KFromToLittleEndian( Q_INT16 b )
{
#ifndef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_16(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 16 bit signed array from/to little-endian byte order to/from the machine order.
 */
inline void KFromToLittleEndian( Q_INT16 *out, Q_INT16 *in, uint len )
{
#ifndef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<1 ) ;
#else
  while ( len>0 ) { *out = KSWAP_16( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 32 bit signed value from/to little-endian byte order to/from the machine order.
 */
inline Q_INT32 KFromToLittleEndian( Q_INT32 b )
{
#ifndef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_32(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 32 bit signed array from/to little-endian byte order to/from the machine order.
 */
inline void KFromToLittleEndian( Q_INT32 *out, Q_INT32 *in, uint len )
{
#ifndef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<2 ) ;
#else
  while ( len>0 ) { *out = KSWAP_32( *in ); out++; in++; len--; }
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 64 bit signed value from/to little-endian byte order to/from the machine order.
 */
inline Q_INT64 KFromToLittleEndian( Q_INT64 b )
{
#ifndef WORDS_BIGENDIAN
  return b;
#else
  return KSWAP_64(b);
#endif
}

/**
 * \ingroup KSWAP
 * Converts a 64 bit signed array from/to little-endian byte order to/from the machine order.
 */
inline void KFromToLittleEndian( Q_INT64 *out, Q_INT64 *in, uint len )
{
#ifndef WORDS_BIGENDIAN
  if ( out != in ) memcpy( out, in, len<<3 ) ;
#else
  while ( len>0 ) { *out = KSWAP_64( *in ); out++; in++; len--; }
#endif
}

#endif /* KSWAP_H */