Revert 45697:
[reactos.git] / lib / 3rdparty / fullfat / ff_memory.c
1 /*****************************************************************************
2 * FullFAT - High Performance, Thread-Safe Embedded FAT File-System *
3 * Copyright (C) 2009 James Walmsley (james@worm.me.uk) *
4 * *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 * *
18 * IMPORTANT NOTICE: *
19 * ================= *
20 * Alternative Licensing is available directly from the Copyright holder, *
21 * (James Walmsley). For more information consult LICENSING.TXT to obtain *
22 * a Commercial license. *
23 * *
24 * See RESTRICTIONS.TXT for extra restrictions on the use of FullFAT. *
25 * *
26 * Removing the above notice is illegal and will invalidate this license. *
27 *****************************************************************************
28 * See http://worm.me.uk/fullfat for more information. *
29 * Or http://fullfat.googlecode.com/ for latest releases and the wiki. *
30 *****************************************************************************/
31
32 /**
33 * @file ff_memory.c
34 * @author James Walmsley
35 * @ingroup MEMORY
36 *
37 * @defgroup MEMORY FullFAT Memory Access Routines
38 * @brief Handles memory access in a portable way.
39 *
40 * Provides simple, fast, and portable access to memory routines.
41 * These are only used to read data from buffers. That are LITTLE ENDIAN
42 * due to the FAT specification.
43 *
44 * These routines may need to be modified to your platform.
45 *
46 **/
47
48 #include "ff_memory.h"
49 #include "ff_config.h"
50
51 #ifdef FF_LITTLE_ENDIAN
52
53 /**
54 * @public
55 * @brief 8 bit memory access routines.
56 **/
57 /*
58 These functions swap the byte-orders of shorts and longs. A getChar function is provided
59 incase there is a system that doesn't have byte-wise access to all memory.
60
61 These functions can be replaced with your own platform specific byte-order swapping routines
62 for more efficiency.
63
64 The provided functions should work on almost all platforms.
65 */
66 FF_T_UINT8 FF_getChar(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset) {
67 return (FF_T_UINT8) (pBuffer[offset]);
68 }
69
70 FF_T_UINT16 FF_getShort(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset) {
71 return (FF_T_UINT16) (pBuffer[offset] & 0x00FF) | ((FF_T_UINT16) (pBuffer[offset+1] << 8) & 0xFF00);
72 }
73
74 FF_T_UINT32 FF_getLong(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset) {
75 return (FF_T_UINT32) (pBuffer[offset] & 0x000000FF) | ((FF_T_UINT32) (pBuffer[offset+1] << 8) & 0x0000FF00) | ((FF_T_UINT32) (pBuffer[offset+2] << 16) & 0x00FF0000) | ((FF_T_UINT32) (pBuffer[offset+3] << 24) & 0xFF000000);
76 }
77
78 void FF_putChar(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset, FF_T_UINT8 Value) {
79 pBuffer[offset] = Value;
80 }
81
82 void FF_putShort(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset, FF_T_UINT16 Value) {
83 FF_T_UINT8 *Val = (FF_T_UINT8 *) &Value;
84 pBuffer[offset] = Val[0];
85 pBuffer[offset + 1] = Val[1];
86 }
87
88 void FF_putLong(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset, FF_T_UINT32 Value) {
89 FF_T_UINT8 *Val = (FF_T_UINT8 *) &Value;
90 pBuffer[offset] = Val[0];
91 pBuffer[offset + 1] = Val[1];
92 pBuffer[offset + 2] = Val[2];
93 pBuffer[offset + 3] = Val[3];
94 }
95
96 #endif
97
98 #ifdef FF_BIG_ENDIAN
99 /*
100 These haven't been tested or checked. They should work in theory :)
101 Please contact james@worm.me.uk if they don't work, and also any fix.
102 */
103 FF_T_UINT8 FF_getChar(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset) {
104 return (FF_T_UINT8) (pBuffer[offset]);
105 }
106
107 FF_T_UINT16 FF_getShort(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset) {
108 return (FF_T_UINT16) ((pBuffer[offset] & 0xFF00) << 8) | ((FF_T_UINT16) (pBuffer[offset+1]) & 0x00FF);
109 }
110
111 FF_T_UINT32 FF_getLong(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset) {
112 return (FF_T_UINT32) ((pBuffer[offset] << 24) & 0xFF0000) | ((FF_T_UINT32) (pBuffer[offset+1] << 16) & 0x00FF0000) | ((FF_T_UINT32) (pBuffer[offset+2] << 8) & 0x0000FF00) | ((FF_T_UINT32) (pBuffer[offset+3]) & 0x000000FF);
113 }
114
115 void FF_putChar(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset, FF_T_UINT8 Value) {
116 pBuffer[offset] = Value;
117 }
118
119 void FF_putShort(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset, FF_T_UINT16 Value) {
120 FF_T_UINT8 *Val = (FF_T_UINT8 *) &Value;
121 pBuffer[offset] = Val[1];
122 pBuffer[offset + 1] = Val[0];
123 }
124
125 void FF_putLong(FF_T_UINT8 *pBuffer, FF_T_UINT16 offset, FF_T_UINT32 Value) {
126 FF_T_UINT8 *Val = (FF_T_UINT8 *) &Value;
127 pBuffer[offset] = Val[3];
128 pBuffer[offset + 1] = Val[2];
129 pBuffer[offset + 2] = Val[1];
130 pBuffer[offset + 3] = Val[0];
131 }
132 #endif
133
134