1 /*++ 2 3 Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR> 4 This program and the accompanying materials 5 are licensed and made available under the terms and conditions of the BSD License 6 which accompanies this distribution. The full text of the license may be found at 7 http://opensource.org/licenses/bsd-license.php 8 9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 11 12 13 Module Name: 14 15 Unaligned.c 16 17 Abstract: 18 19 --*/ 20 21 #include "BaseLibInternals.h" 22 23 /** 24 Reads a 16-bit value from memory that may be unaligned. 25 26 This function returns the 16-bit value pointed to by Buffer. The function 27 guarantees that the read operation does not produce an alignment fault. 28 29 If the Buffer is NULL, then ASSERT(). 30 31 @param Buffer Pointer to a 16-bit value that may be unaligned. 32 33 @return *Uint16 34 35 **/ 36 UINT16 37 EFIAPI 38 ReadUnaligned16 ( 39 IN CONST UINT16 *Buffer 40 ) 41 { 42 ASSERT (Buffer != NULL); 43 44 return (UINT16)(((UINT8*)Buffer)[0] | (((UINT8*)Buffer)[1] << 8)); 45 } 46 47 /** 48 Writes a 16-bit value to memory that may be unaligned. 49 50 This function writes the 16-bit value specified by Value to Buffer. Value is 51 returned. The function guarantees that the write operation does not produce 52 an alignment fault. 53 54 If the Buffer is NULL, then ASSERT(). 55 56 @param Buffer Pointer to a 16-bit value that may be unaligned. 57 @param Value 16-bit value to write to Buffer. 58 59 @return Value 60 61 **/ 62 UINT16 63 EFIAPI 64 WriteUnaligned16 ( 65 OUT UINT16 *Buffer, 66 IN UINT16 Value 67 ) 68 { 69 ASSERT (Buffer != NULL); 70 71 ((UINT8*)Buffer)[0] = (UINT8)Value; 72 ((UINT8*)Buffer)[1] = (UINT8)(Value >> 8); 73 74 return Value; 75 } 76 77 /** 78 Reads a 24-bit value from memory that may be unaligned. 79 80 This function returns the 24-bit value pointed to by Buffer. The function 81 guarantees that the read operation does not produce an alignment fault. 82 83 If the Buffer is NULL, then ASSERT(). 84 85 @param Buffer Pointer to a 24-bit value that may be unaligned. 86 87 @return The value read. 88 89 **/ 90 UINT32 91 EFIAPI 92 ReadUnaligned24 ( 93 IN CONST UINT32 *Buffer 94 ) 95 { 96 ASSERT (Buffer != NULL); 97 98 return (UINT32)( 99 ReadUnaligned16 ((UINT16*)Buffer) | 100 (((UINT8*)Buffer)[2] << 16) 101 ); 102 } 103 104 /** 105 Writes a 24-bit value to memory that may be unaligned. 106 107 This function writes the 24-bit value specified by Value to Buffer. Value is 108 returned. The function guarantees that the write operation does not produce 109 an alignment fault. 110 111 If the Buffer is NULL, then ASSERT(). 112 113 @param Buffer Pointer to a 24-bit value that may be unaligned. 114 @param Value 24-bit value to write to Buffer. 115 116 @return The value written. 117 118 **/ 119 UINT32 120 EFIAPI 121 WriteUnaligned24 ( 122 OUT UINT32 *Buffer, 123 IN UINT32 Value 124 ) 125 { 126 ASSERT (Buffer != NULL); 127 128 WriteUnaligned16 ((UINT16*)Buffer, (UINT16)Value); 129 *(UINT8*)((UINT16*)Buffer + 1) = (UINT8)(Value >> 16); 130 return Value; 131 } 132 133 /** 134 Reads a 32-bit value from memory that may be unaligned. 135 136 This function returns the 32-bit value pointed to by Buffer. The function 137 guarantees that the read operation does not produce an alignment fault. 138 139 If the Buffer is NULL, then ASSERT(). 140 141 @param Buffer Pointer to a 32-bit value that may be unaligned. 142 143 @return *Uint32 144 145 **/ 146 UINT32 147 EFIAPI 148 ReadUnaligned32 ( 149 IN CONST UINT32 *Buffer 150 ) 151 { 152 UINT16 LowerBytes; 153 UINT16 HigherBytes; 154 155 ASSERT (Buffer != NULL); 156 157 LowerBytes = ReadUnaligned16 ((UINT16*) Buffer); 158 HigherBytes = ReadUnaligned16 ((UINT16*) Buffer + 1); 159 160 return (UINT32) (LowerBytes | (HigherBytes << 16)); 161 } 162 163 /** 164 Writes a 32-bit value to memory that may be unaligned. 165 166 This function writes the 32-bit value specified by Value to Buffer. Value is 167 returned. The function guarantees that the write operation does not produce 168 an alignment fault. 169 170 If the Buffer is NULL, then ASSERT(). 171 172 @param Buffer Pointer to a 32-bit value that may be unaligned. 173 @param Value 32-bit value to write to Buffer. 174 175 @return Value 176 177 **/ 178 UINT32 179 EFIAPI 180 WriteUnaligned32 ( 181 OUT UINT32 *Buffer, 182 IN UINT32 Value 183 ) 184 { 185 ASSERT (Buffer != NULL); 186 187 WriteUnaligned16 ((UINT16*)Buffer, (UINT16)Value); 188 WriteUnaligned16 ((UINT16*)Buffer + 1, (UINT16)(Value >> 16)); 189 return Value; 190 } 191 192 /** 193 Reads a 64-bit value from memory that may be unaligned. 194 195 This function returns the 64-bit value pointed to by Buffer. The function 196 guarantees that the read operation does not produce an alignment fault. 197 198 If the Buffer is NULL, then ASSERT(). 199 200 @param Buffer Pointer to a 64-bit value that may be unaligned. 201 202 @return *Uint64 203 204 **/ 205 UINT64 206 EFIAPI 207 ReadUnaligned64 ( 208 IN CONST UINT64 *Buffer 209 ) 210 { 211 UINT32 LowerBytes; 212 UINT32 HigherBytes; 213 214 ASSERT (Buffer != NULL); 215 216 LowerBytes = ReadUnaligned32 ((UINT32*) Buffer); 217 HigherBytes = ReadUnaligned32 ((UINT32*) Buffer + 1); 218 219 return (UINT64) (LowerBytes | LShiftU64 (HigherBytes, 32)); 220 } 221 222 /** 223 Writes a 64-bit value to memory that may be unaligned. 224 225 This function writes the 64-bit value specified by Value to Buffer. Value is 226 returned. The function guarantees that the write operation does not produce 227 an alignment fault. 228 229 If the Buffer is NULL, then ASSERT(). 230 231 @param Buffer Pointer to a 64-bit value that may be unaligned. 232 @param Value 64-bit value to write to Buffer. 233 234 @return Value 235 236 **/ 237 UINT64 238 EFIAPI 239 WriteUnaligned64 ( 240 OUT UINT64 *Buffer, 241 IN UINT64 Value 242 ) 243 { 244 ASSERT (Buffer != NULL); 245 246 WriteUnaligned32 ((UINT32*)Buffer, (UINT32)Value); 247 WriteUnaligned32 ((UINT32*)Buffer + 1, (UINT32)RShiftU64 (Value, 32)); 248 return Value; 249 } 250