Mercurial > projects > dcrypt
comparison dcrypt/misc/ByteConverter.d @ 23:4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 14 Feb 2009 19:58:20 -0500 |
parents | |
children | 6428dfd7fede |
comparison
equal
deleted
inserted
replaced
22:74303a717032 | 23:4589f8c5eb3c |
---|---|
1 /** | |
2 * This file is part of the dcrypt project. | |
3 * | |
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. | |
5 * License: MIT | |
6 * Authors: Thomas Dixon | |
7 */ | |
8 | |
9 module dcrypt.misc.ByteConverter; | |
10 | |
11 /** Converts between integral types and unsigned byte arrays */ | |
12 struct ByteConverter { | |
13 private static const char[] hexits = "0123456789abcdef"; | |
14 | |
15 /** Conversions between little endian integrals and bytes */ | |
16 struct LittleEndian { | |
17 /** | |
18 * Converts the supplied array to integral type T | |
19 * | |
20 * Params: | |
21 * x_ = The supplied array of bytes (ubytes, bytes, chars, whatever) | |
22 * | |
23 * Returns: | |
24 * A integral of type T created with the supplied bytes placed | |
25 * in the specified byte order. | |
26 */ | |
27 static T to(T)(void[] x_) { | |
28 ubyte[] x = cast(ubyte[])x_; | |
29 | |
30 T result = ((x[0] & 0xff) | | |
31 ((x[1] & 0xff) << 8)); | |
32 | |
33 static if (T.sizeof >= int.sizeof) { | |
34 result |= ((x[2] & 0xff) << 16) | | |
35 ((x[3] & 0xff) << 24); | |
36 } | |
37 | |
38 static if (T.sizeof >= long.sizeof) { | |
39 result |= (cast(T)(x[4] & 0xff) << 32) | | |
40 (cast(T)(x[5] & 0xff) << 40) | | |
41 (cast(T)(x[6] & 0xff) << 48) | | |
42 (cast(T)(x[7] & 0xff) << 56); | |
43 } | |
44 | |
45 return result; | |
46 } | |
47 | |
48 /** | |
49 * Converts the supplied integral to an array of unsigned bytes. | |
50 * | |
51 * Params: | |
52 * input = Integral to convert to bytes | |
53 * | |
54 * Returns: | |
55 * Integral input of type T split into its respective bytes | |
56 * with the bytes placed in the specified byte order. | |
57 */ | |
58 static ubyte[] from(T)(T input) { | |
59 ubyte[] output = new ubyte[T.sizeof]; | |
60 | |
61 output[0] = cast(ubyte)(input); | |
62 output[1] = cast(ubyte)(input >> 8); | |
63 | |
64 static if (T.sizeof >= int.sizeof) { | |
65 output[2] = cast(ubyte)(input >> 16); | |
66 output[3] = cast(ubyte)(input >> 24); | |
67 } | |
68 | |
69 static if (T.sizeof >= long.sizeof) { | |
70 output[4] = cast(ubyte)(input >> 32); | |
71 output[5] = cast(ubyte)(input >> 40); | |
72 output[6] = cast(ubyte)(input >> 48); | |
73 output[7] = cast(ubyte)(input >> 56); | |
74 } | |
75 | |
76 return output; | |
77 } | |
78 } | |
79 | |
80 /** Conversions between big endian integrals and bytes */ | |
81 struct BigEndian { | |
82 | |
83 static T to(T)(void[] x_) { | |
84 ubyte[] x = cast(ubyte[])x_; | |
85 | |
86 static if (is(T == ushort) || is(T == short)) { | |
87 return cast(T) (((x[0] & 0xff) << 8) | | |
88 (x[1] & 0xff)); | |
89 } else static if (is(T == uint) || is(T == int)) { | |
90 return cast(T) (((x[0] & 0xff) << 24) | | |
91 ((x[1] & 0xff) << 16) | | |
92 ((x[2] & 0xff) << 8) | | |
93 (x[3] & 0xff)); | |
94 } else static if (is(T == ulong) || is(T == long)) { | |
95 return cast(T) ((cast(T)(x[0] & 0xff) << 56) | | |
96 (cast(T)(x[1] & 0xff) << 48) | | |
97 (cast(T)(x[2] & 0xff) << 40) | | |
98 (cast(T)(x[3] & 0xff) << 32) | | |
99 ((x[4] & 0xff) << 24) | | |
100 ((x[5] & 0xff) << 16) | | |
101 ((x[6] & 0xff) << 8) | | |
102 (x[7] & 0xff)); | |
103 } | |
104 } | |
105 | |
106 static ubyte[] from(T)(T input) { | |
107 ubyte[] output = new ubyte[T.sizeof]; | |
108 | |
109 static if (T.sizeof == long.sizeof) { | |
110 output[0] = cast(ubyte)(input >> 56); | |
111 output[1] = cast(ubyte)(input >> 48); | |
112 output[2] = cast(ubyte)(input >> 40); | |
113 output[3] = cast(ubyte)(input >> 32); | |
114 output[4] = cast(ubyte)(input >> 24); | |
115 output[5] = cast(ubyte)(input >> 16); | |
116 output[6] = cast(ubyte)(input >> 8); | |
117 output[7] = cast(ubyte)(input); | |
118 } else static if (T.sizeof == int.sizeof) { | |
119 output[0] = cast(ubyte)(input >> 24); | |
120 output[1] = cast(ubyte)(input >> 16); | |
121 output[2] = cast(ubyte)(input >> 8); | |
122 output[3] = cast(ubyte)(input); | |
123 } else static if (T.sizeof == short.sizeof) { | |
124 output[0] = cast(ubyte)(input >> 8); | |
125 output[1] = cast(ubyte)(input); | |
126 } | |
127 return output; | |
128 } | |
129 } | |
130 | |
131 static char[] hexEncode(void[] input_) { | |
132 ubyte[] input = cast(ubyte[])input_; | |
133 char[] output = new char[input.length<<1]; | |
134 | |
135 int i = 0; | |
136 foreach (ubyte j; input) { | |
137 output[i++] = hexits[j>>4]; | |
138 output[i++] = hexits[j&0xf]; | |
139 } | |
140 return output; | |
141 } | |
142 | |
143 static ubyte[] hexDecode(char[] input) { | |
144 ubyte[] output = new ubyte[input.length>>1]; | |
145 | |
146 static ubyte[char] hexitIndex; | |
147 for (int i = 0; i < hexits.length; i++) | |
148 hexitIndex[hexits[i]] = i; | |
149 | |
150 for (int i = 0, j = 0; i < output.length; i++) { | |
151 output[i] = hexitIndex[input[j++]] << 4; | |
152 output[i] |= hexitIndex[input[j++]]; | |
153 } | |
154 return output; | |
155 } | |
156 } |