Changeset 2667
- Timestamp:
- 11/06/08 22:03:36 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tendra/src/producers/ada/libgela/gela-hash-crc-b32.adb
r2639 r2667 1 with Ada.Unchecked_Conversion;2 3 1 package body Gela.Hash.CRC.b32 is 4 2 … … 48 46 3020668471, 3272380065, 1510334235, 755167117); 49 47 50 type Byte_Array is 51 array (Natural range <>) of Interfaces.Unsigned_8; 48 subtype Byte is CRC32 range 0 .. 255; 49 50 procedure Update_Hash 51 (This : in out Hasher; 52 Value : in Byte); 53 pragma Inline (Update_Hash); 54 55 ------------ 56 -- Update -- 57 ------------ 52 58 53 59 procedure Update 54 60 (This : in out Hasher; 55 Value : in Byte_Array); 56 57 ------------ 58 -- Update -- 59 ------------ 60 61 procedure Update 62 (This : in out Hasher; 63 Value : in String) 64 is 65 subtype C_Array is 66 Byte_Array (1 .. Value'Size / Interfaces.Unsigned_8'Size); 67 68 function To_Array is 69 new Ada.Unchecked_Conversion (String, C_Array); 70 begin 71 Update (This, To_Array (Value)); 61 Value : in String) is 62 begin 63 This.Length := This.Length + Value'Length; 64 65 if This.Length > Maximum_Length then 66 raise Maximum_Length_Error; 67 end if; 68 69 for J in Value'Range loop 70 Update_Hash (This, Character'Pos (Value (J))); 71 end loop; 72 72 end Update; 73 73 … … 78 78 procedure Wide_Update 79 79 (This : in out Hasher; 80 Value : in Wide_String) 81 is 82 subtype C_Array is 83 Byte_Array (1 .. Value'Size / Interfaces.Unsigned_8'Size); 84 85 function To_Array is 86 new Ada.Unchecked_Conversion (Wide_String, C_Array); 87 begin 88 Update (This, To_Array (Value)); 80 Value : in Wide_String) is 81 begin 82 This.Length := This.Length + 2 * Value'Length; 83 84 if This.Length > Maximum_Length then 85 raise Maximum_Length_Error; 86 end if; 87 88 for J in Value'Range loop 89 Update_Hash (This, Wide_Character'Pos (Value (J)) and 16#FF#); 90 Update_Hash (This, Shift_Right (Wide_Character'Pos (Value (J)), 8)); 91 end loop; 89 92 end Wide_Update; 90 93 … … 97 100 Value : in Wide_Wide_String) 98 101 is 99 subtype C_Array is 100 Byte_Array (1 .. Value'Size / Interfaces.Unsigned_8'Size); 101 102 function To_Array is 103 new Ada.Unchecked_Conversion (Wide_Wide_String, C_Array); 104 begin 105 Update (This, To_Array (Value)); 102 subtype W is Wide_Wide_Character; 103 begin 104 This.Length := This.Length + 4 * Value'Length; 105 106 if This.Length > Maximum_Length then 107 raise Maximum_Length_Error; 108 end if; 109 110 for J in Value'Range loop 111 Update_Hash (This, W'Pos (Value (J)) and 16#FF#); 112 Update_Hash (This, Shift_Right (W'Pos (Value (J)), 8) and 16#FF#); 113 Update_Hash (This, Shift_Right (W'Pos (Value (J)), 16) and 16#FF#); 114 Update_Hash (This, Shift_Right (W'Pos (Value (J)), 24)); 115 end loop; 106 116 end Wide_Wide_Update; 107 117 … … 112 122 procedure Update 113 123 (This : in out Hasher; 114 Value : in Ada.Streams.Stream_Element_Array) 115 is 116 subtype C_Array is 117 Byte_Array (1 .. Value'Size / Interfaces.Unsigned_8'Size); 118 119 function To_Array is 120 new Ada.Unchecked_Conversion 121 (Ada.Streams.Stream_Element_Array, C_Array); 122 begin 123 Update (This, To_Array (Value)); 124 Value : in Ada.Streams.Stream_Element_Array) is 125 begin 126 This.Length := This.Length + Value'Length; 127 128 if This.Length > Maximum_Length then 129 raise Maximum_Length_Error; 130 end if; 131 132 for J in Value'Range loop 133 Update_Hash (This, CRC32 (Value (J))); 134 end loop; 124 135 end Update; 125 136 126 -- Calculate--127 function Calculate128 (Value : in String)129 return CRC32 130 is137 --------------- 138 -- Calculate -- 139 --------------- 140 141 function Calculate (Value : in String) return CRC32 is 131 142 H : Hasher; 132 143 begin … … 135 146 end Calculate; 136 147 148 -------------------- 137 149 -- Wide_Calculate -- 138 function Wide_Calculate 139 (Value : in Wide_String) 140 return CRC32 141 is 150 -------------------- 151 152 function Wide_Calculate (Value : in Wide_String) return CRC32 is 142 153 H : Hasher; 143 154 begin … … 146 157 end Wide_Calculate; 147 158 148 -- Calculate--149 function Wide_Wide_Calculate150 (Value : in Wide_Wide_String)151 return CRC32 152 is159 --------------- 160 -- Calculate -- 161 --------------- 162 163 function Wide_Wide_Calculate (Value : in Wide_Wide_String) return CRC32 is 153 164 H : Hasher; 154 165 begin … … 157 168 end Wide_Wide_Calculate; 158 169 159 -- Calculate -- 170 --------------- 171 -- Calculate -- 172 --------------- 173 160 174 function Calculate 161 175 (Value : in Ada.Streams.Stream_Element_Array) … … 168 182 end Calculate; 169 183 184 ------------- 170 185 -- To_Hash -- 171 function To_Hash 172 (T : in CRC32) 173 return Hash_Type 174 is 186 ------------- 187 188 function To_Hash (T : in CRC32) return Hash_Type is 175 189 begin 176 190 return Hash_Type (T); 177 191 end To_Hash; 178 192 179 -- Calculate--180 function Calculate181 (Value : in String)182 return Hash_Type 183 is193 --------------- 194 -- Calculate -- 195 --------------- 196 197 function Calculate (Value : in String) return Hash_Type is 184 198 begin 185 199 return To_Hash (Calculate (Value)); 186 200 end Calculate; 187 201 202 -------------------- 188 203 -- Wide_Calculate -- 189 function Wide_Calculate 190 (Value : in Wide_String) 191 return Hash_Type 192 is 204 -------------------- 205 206 function Wide_Calculate (Value : in Wide_String) return Hash_Type is 193 207 begin 194 208 return To_Hash (Wide_Calculate (Value)); 195 209 end Wide_Calculate; 196 210 197 -- Calculate -- 211 --------------- 212 -- Calculate -- 213 --------------- 214 198 215 function Wide_Wide_Calculate 199 216 (Value : in Wide_Wide_String) … … 204 221 end Wide_Wide_Calculate; 205 222 206 -- Calculate -- 223 --------------- 224 -- Calculate -- 225 --------------- 226 207 227 function Calculate 208 228 (Value : in Ada.Streams.Stream_Element_Array) … … 217 237 ------------ 218 238 219 procedure Update 220 (This : in out Hasher; 221 Value : in Byte_Array) 222 is 223 use Interfaces; 224 225 Reg : CRC32 := This.Cm_Reg; 226 begin 227 This.Length := This.Length + Value'Length; 228 229 if This.Length > Maximum_Length then 230 raise Maximum_Length_Error; 231 end if; 232 233 for Index in Value'Range loop 234 Reg := Shift_Right (Reg, 8) xor 235 Keys (CRC32 (Value (Index)) xor (Reg and 16#0000_00FF#)); 236 end loop; 237 238 This.Cm_Reg := Reg; 239 end Update; 239 procedure Update_Hash 240 (This : in out Hasher; 241 Value : in Byte) 242 is 243 begin 244 This.Cm_Reg := Shift_Right (This.Cm_Reg, 8) xor 245 Keys (Value xor (This.Cm_Reg and 16#0000_00FF#)); 246 end Update_Hash; 240 247 241 248 ------------ … … 243 250 ------------ 244 251 245 function Result 246 (This : in Hasher) 247 return CRC32 248 is 252 function Result (This : in Hasher) return CRC32 is 249 253 begin 250 254 return This.Cm_Reg xor 16#FFFFFFFF#;