1/* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18// -*- c++ -*- 19// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 20 21// B Y T E O R D E R U T I L S 22 23// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25/*! \file byte_order_utils.inl This file defines functions providing byte ordering utility. 26*/ 27 28//! Convert little endian to host format. 29/*! 30 This function takes a buffer of data which is assumed to be in little endian order and 31 rearranges it to the native order of the machine running the code. If the machine is a 32 little endian machine, nothing is done. 33 \param data A pointer to the input/output buffer 34 \param size The number of bytes in the buffer. 35*/ 36 37#if !(OSCL_BYTE_ORDER_LITTLE_ENDIAN) && !(OSCL_BYTE_ORDER_BIG_ENDIAN) 38#error must define either OSCL_BYTE_ORDER_LITTLE_ENDIAN or OSCL_BYTE_ORDER_BIG_ENDIAN 39#elif (OSCL_BYTE_ORDER_LITTLE_ENDIAN) && (OSCL_BYTE_ORDER_BIG_ENDIAN) 40#error must only define one of OSCL_BYTE_ORDER_LITTLE_ENDIAN, OSCL_BYTE_ORDER_BIG_ENDIAN 41#endif 42 43//! Swap the order of bytes. 44/*! 45 This function takes a buffer of data and reverses the order of the bytes. 46 \param data A pointer to the input/output buffer 47 \param size The number of bytes in the buffer. 48*/ 49OSCL_INLINE void swap_byte_order(char *data, unsigned int size) 50{ 51 char tmp; 52 char *ptr1, *ptr2; 53 54 ptr1 = data; 55 ptr2 = data + size - 1; 56 57 while (ptr1 < ptr2) 58 { 59 tmp = *ptr1; 60 *ptr1++ = *ptr2; 61 *ptr2-- = tmp; 62 } 63 64} 65 66 67 68 69OSCL_INLINE void little_endian_to_host(char *data, uint32 size) 70{ 71#if (OSCL_BYTE_ORDER_BIG_ENDIAN) 72 swap_byte_order(data, size); 73#else 74 OSCL_UNUSED_ARG(data); // to remove warning 'unreferenced parameter 75 OSCL_UNUSED_ARG(size); // to remove warning 'unreferenced parameter 76 // data=data; size=size; 77#endif 78} 79 80 81 82//! Convert host to little endian format. 83/*! 84 This function takes a buffer of data which is assumed to be in the host's native order and 85 rearranges it to the little endian format. If the machine is a little endian machine, nothing is done. 86 \param data A pointer to the input/output buffer 87 \param size The number of bytes in the buffer. 88*/ 89OSCL_INLINE void host_to_little_endian(char *data, unsigned int size) 90{ 91#if (OSCL_BYTE_ORDER_BIG_ENDIAN) 92 swap_byte_order(data, size); 93#else 94 OSCL_UNUSED_ARG(data); // to remove warning 'unreferenced parameter 95 OSCL_UNUSED_ARG(size); // to remove warning 'unreferenced parameter 96#endif 97} 98 99//! Convert big endian to host format. 100/*! 101 This function takes a buffer of data which is assumed to be in big endian order and 102 rearranges it to the native order of the machine running the code. If the machine is a 103 big endian machine, nothing is done. 104 \param data A pointer to the input/output buffer 105 \param size The number of bytes in the buffer. 106*/ 107OSCL_INLINE void big_endian_to_host(char *data, unsigned int size) 108{ 109#if (OSCL_BYTE_ORDER_LITTLE_ENDIAN) 110 swap_byte_order(data, size); 111#else 112 OSCL_UNUSED_ARG(data); // to remove warning 'unreferenced parameter 113 OSCL_UNUSED_ARG(size); // to remove warning 'unreferenced parameter 114#endif 115 116} 117 118 119//! Convert host to big endian format. 120/*! 121 This function takes a buffer of data which is assumed to be in native host order and 122 rearranges it to big endian format. If the machine is a big endian machine, nothing is done. 123 \param data A pointer to the input/output buffer 124 \param size The number of bytes in the buffer. 125*/ 126OSCL_INLINE void host_to_big_endian(char *data, unsigned int size) 127{ 128#if (OSCL_BYTE_ORDER_LITTLE_ENDIAN) 129 swap_byte_order(data, size); 130#else 131 OSCL_UNUSED_ARG(data); // to remove warning 'unreferenced parameter 132 OSCL_UNUSED_ARG(size); // to remove warning 'unreferenced parameter 133#endif 134 135 136} 137 138 139