1 /*
2 OWFS -- One-Wire filesystem
3 OWHTTPD -- One-Wire Web Server
4 Written 2003 Paul H Alfille
5 email: paul.alfille@gmail.com
6 Released under the GPL
7 See the header file: ow.h for full attribution
8 1wire/iButton system from Dallas Semiconductor
9 */
10
11 #include <config.h>
12 #include "owfs_config.h"
13 #include "ow.h"
14
15 /* Set and get a bit from a binary buffer */
16 /* Robin Gilks discovered that this is not endian-safe for setting bits in integers
17 * so see UT_getbit_U and UT_getbit_U
18 * */
19
UT_getbit(const BYTE * buf,int loc)20 int UT_getbit(const BYTE * buf, int loc)
21 {
22 // devide location by 8 to get byte
23 return (((buf[loc >> 3]) >> (loc & 0x7)) & 0x01);
24 }
25
UT_setbit(BYTE * buf,int loc,int bit)26 void UT_setbit(BYTE * buf, int loc, int bit)
27 {
28 if (bit) {
29 buf[loc >> 3] |= 0x01 << (loc & 0x7);
30 } else {
31 buf[loc >> 3] &= ~(0x01 << (loc & 0x7));
32 }
33 }
34
UT_get2bit(const BYTE * buf,int loc)35 int UT_get2bit(const BYTE * buf, int loc)
36 {
37 return (((buf[loc >> 2]) >> ((loc & 0x3) << 1)) & 0x03);
38 }
39
UT_set2bit(BYTE * buf,int loc,int bits)40 void UT_set2bit(BYTE * buf, int loc, int bits)
41 {
42 BYTE *p = &buf[loc >> 2];
43 switch (loc & 3) {
44 case 0:
45 *p = (*p & 0xFC) | bits;
46 return;
47 case 1:
48 *p = (*p & 0xF3) | (bits << 2);
49 return;
50 case 2:
51 *p = (*p & 0xCF) | (bits << 4);
52 return;
53 case 3:
54 *p = (*p & 0x3F) | (bits << 6);
55 return;
56 }
57 }
58