FreeTDS API

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tdsbytes.h
1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 * Copyright (C) 2005-2008 Frediano Ziglio
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 #ifndef _tdsbytes_h_
21 #define _tdsbytes_h_
22
23 /* $Id: tdsbytes.h,v 1.5 2010-07-17 20:05:52 freddy77 Exp $ */
24
25 #ifndef _tds_h_
26 #error tds.h must be included before tdsbytes.h
27 #endif
28
29 /*
30 * read a word of n bytes aligned, architecture dependent endian
31 * TDS_GET_An
32 * read a word of n bytes aligned, little endian
33 * TDS_GET_AnLE
34 * read a word of n bytes aligned, big endian
35 * TDS_GET_AnBE
36 * read a word of n bytes unaligned, architecture dependent endian
37 * TDS_GET_UAn
38 * read a word of n bytes unaligned, little endian
39 * TDS_GET_UAnLE
40 * read a word of n bytes unaligned, big endian
41 * TDS_GET_UAnBE
42 */
43
44 /* TODO optimize (use swap, unaligned platforms) */
45
46 /* one byte, easy... */
47 #define TDS_GET_A1LE(ptr) (((TDS_UCHAR*)(ptr))[0])
48 #define TDS_GET_A1BE(ptr) TDS_GET_A1LE(ptr)
49 #define TDS_GET_UA1LE(ptr) TDS_GET_A1LE(ptr)
50 #define TDS_GET_UA1BE(ptr) TDS_GET_A1LE(ptr)
51
52 #define TDS_PUT_A1LE(ptr,val) do { ((TDS_UCHAR*)(ptr))[0] = (val); } while(0)
53 #define TDS_PUT_A1BE(ptr,val) TDS_PUT_A1LE(ptr,val)
54 #define TDS_PUT_UA1LE(ptr,val) TDS_PUT_A1LE(ptr,val)
55 #define TDS_PUT_UA1BE(ptr,val) TDS_PUT_A1LE(ptr,val)
56
57 /* two bytes */
58 #define TDS_GET_UA2LE(ptr) (((TDS_UCHAR*)(ptr))[1] * 0x100u + ((TDS_UCHAR*)(ptr))[0])
59 #define TDS_GET_UA2BE(ptr) (((TDS_UCHAR*)(ptr))[0] * 0x100u + ((TDS_UCHAR*)(ptr))[1])
60 #define TDS_GET_A2LE(ptr) TDS_GET_UA2LE(ptr)
61 #define TDS_GET_A2BE(ptr) TDS_GET_UA2BE(ptr)
62
63 #define TDS_PUT_UA2LE(ptr,val) do {\
64 ((TDS_UCHAR*)(ptr))[1] = (TDS_UCHAR)((val)>>8); ((TDS_UCHAR*)(ptr))[0] = (TDS_UCHAR)(val); } while(0)
65 #define TDS_PUT_UA2BE(ptr,val) do {\
66 ((TDS_UCHAR*)(ptr))[0] = (TDS_UCHAR)((val)>>8); ((TDS_UCHAR*)(ptr))[1] = (TDS_UCHAR)(val); } while(0)
67 #define TDS_PUT_A2LE(ptr,val) TDS_PUT_UA2LE(ptr,val)
68 #define TDS_PUT_A2BE(ptr,val) TDS_PUT_UA2BE(ptr,val)
69
70 /* four bytes */
71 #define TDS_GET_UA4LE(ptr) \
72 (((TDS_UCHAR*)(ptr))[3] * 0x1000000u + ((TDS_UCHAR*)(ptr))[2] * 0x10000u +\
73 ((TDS_UCHAR*)(ptr))[1] * 0x100u + ((TDS_UCHAR*)(ptr))[0])
74 #define TDS_GET_UA4BE(ptr) \
75 (((TDS_UCHAR*)(ptr))[0] * 0x1000000u + ((TDS_UCHAR*)(ptr))[1] * 0x10000u +\
76 ((TDS_UCHAR*)(ptr))[2] * 0x100u + ((TDS_UCHAR*)(ptr))[3])
77 #define TDS_GET_A4LE(ptr) TDS_GET_UA4LE(ptr)
78 #define TDS_GET_A4BE(ptr) TDS_GET_UA4BE(ptr)
79
80 #define TDS_PUT_UA4LE(ptr,val) do {\
81 ((TDS_UCHAR*)(ptr))[3] = (TDS_UCHAR)((val)>>24); ((TDS_UCHAR*)(ptr))[2] = (TDS_UCHAR)((val)>>16);\
82 ((TDS_UCHAR*)(ptr))[1] = (TDS_UCHAR)((val)>>8); ((TDS_UCHAR*)(ptr))[0] = (TDS_UCHAR)(val); } while(0)
83 #define TDS_PUT_UA4BE(ptr,val) do {\
84 ((TDS_UCHAR*)(ptr))[0] = (TDS_UCHAR)((val)>>24); ((TDS_UCHAR*)(ptr))[1] = (TDS_UCHAR)((val)>>16);\
85 ((TDS_UCHAR*)(ptr))[2] = (TDS_UCHAR)((val)>>8); ((TDS_UCHAR*)(ptr))[3] = (TDS_UCHAR)(val); } while(0)
86 #define TDS_PUT_A4LE(ptr,val) TDS_PUT_UA4LE(ptr,val)
87 #define TDS_PUT_A4BE(ptr,val) TDS_PUT_UA4BE(ptr,val)
88
89 /* architecture dependent */
90 #ifdef WORDS_BIGENDIAN
91 # define TDS_GET_A1(ptr) TDS_GET_A1BE(ptr)
92 # define TDS_GET_UA1(ptr) TDS_GET_UA1BE(ptr)
93 # define TDS_GET_A2(ptr) TDS_GET_A2BE(ptr)
94 # define TDS_GET_UA2(ptr) TDS_GET_UA2BE(ptr)
95 # define TDS_GET_A4(ptr) TDS_GET_A4BE(ptr)
96 # define TDS_GET_UA4(ptr) TDS_GET_UA4BE(ptr)
97 # undef TDS_GET_A2BE
98 # undef TDS_GET_A4BE
99 # define TDS_GET_A2BE(ptr) (*((TDS_USMALLINT*)(ptr)))
100 # define TDS_GET_A4BE(ptr) (*((TDS_UINT*)(ptr)))
101
102 # define TDS_PUT_A1(ptr,val) TDS_PUT_A1BE(ptr,val)
103 # define TDS_PUT_UA1(ptr,val) TDS_PUT_UA1BE(ptr,val)
104 # define TDS_PUT_A2(ptr,val) TDS_PUT_A2BE(ptr,val)
105 # define TDS_PUT_UA2(ptr,val) TDS_PUT_UA2BE(ptr,val)
106 # define TDS_PUT_A4(ptr,val) TDS_PUT_A4BE(ptr,val)
107 # define TDS_PUT_UA4(ptr,val) TDS_PUT_UA4BE(ptr,val)
108 # undef TDS_PUT_A2BE
109 # undef TDS_PUT_A4BE
110 # define TDS_PUT_A2BE(ptr,val) (*((TDS_USMALLINT*)(ptr)) = (val))
111 # define TDS_PUT_A4BE(ptr,val) (*((TDS_UINT*)(ptr)) = (val))
112 # define TDS_HOST2LE(val) TDS_BYTE_SWAP16(val)
113 # define TDS_HOST4LE(val) TDS_BYTE_SWAP32(val)
114 # define TDS_HOST2BE(val) (val)
115 # define TDS_HOST4BE(val) (val)
116 #else
117 # define TDS_GET_A1(ptr) TDS_GET_A1LE(ptr)
118 # define TDS_GET_UA1(ptr) TDS_GET_UA1LE(ptr)
119 # define TDS_GET_A2(ptr) TDS_GET_A2LE(ptr)
120 # define TDS_GET_UA2(ptr) TDS_GET_UA2LE(ptr)
121 # define TDS_GET_A4(ptr) TDS_GET_A4LE(ptr)
122 # define TDS_GET_UA4(ptr) TDS_GET_UA4LE(ptr)
123 # undef TDS_GET_A2LE
124 # undef TDS_GET_A4LE
125 # define TDS_GET_A2LE(ptr) (*((TDS_USMALLINT*)(ptr)))
126 # define TDS_GET_A4LE(ptr) (*((TDS_UINT*)(ptr)))
127
128 # define TDS_PUT_A1(ptr,val) TDS_PUT_A1LE(ptr,val)
129 # define TDS_PUT_UA1(ptr,val) TDS_PUT_UA1LE(ptr,val)
130 # define TDS_PUT_A2(ptr,val) TDS_PUT_A2LE(ptr,val)
131 # define TDS_PUT_UA2(ptr,val) TDS_PUT_UA2LE(ptr,val)
132 # define TDS_PUT_A4(ptr,val) TDS_PUT_A4LE(ptr,val)
133 # define TDS_PUT_UA4(ptr,val) TDS_PUT_UA4LE(ptr,val)
134 # undef TDS_PUT_A2LE
135 # undef TDS_PUT_A4LE
136 # define TDS_PUT_A2LE(ptr,val) (*((TDS_USMALLINT*)(ptr)) = (val))
137 # define TDS_PUT_A4LE(ptr,val) (*((TDS_UINT*)(ptr)) = (val))
138 # define TDS_HOST2LE(val) (val)
139 # define TDS_HOST4LE(val) (val)
140 # define TDS_HOST2BE(val) TDS_BYTE_SWAP16(val)
141 # define TDS_HOST4BE(val) TDS_BYTE_SWAP32(val)
142 #endif
143
144 /* these platform support unaligned fetch/store */
145 #if defined(__i386__) || defined(__amd64__) || defined(__CRIS__) ||\
146 defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) ||\
147 defined(__s390__) || defined(__s390x__) || defined(__m68k__)
148 # ifdef WORDS_BIGENDIAN
149 # undef TDS_GET_UA2BE
150 # undef TDS_GET_UA4BE
151 # define TDS_GET_UA2BE(ptr) TDS_GET_A2BE(ptr)
152 # define TDS_GET_UA4BE(ptr) TDS_GET_A4BE(ptr)
153
154 # undef TDS_PUT_UA2BE
155 # undef TDS_PUT_UA4BE
156 # define TDS_PUT_UA2BE(ptr,val) TDS_PUT_A2BE(ptr,val)
157 # define TDS_PUT_UA4BE(ptr,val) TDS_PUT_A4BE(ptr,val)
158 # else
159 # undef TDS_GET_UA2LE
160 # undef TDS_GET_UA4LE
161 # define TDS_GET_UA2LE(ptr) TDS_GET_A2LE(ptr)
162 # define TDS_GET_UA4LE(ptr) TDS_GET_A4LE(ptr)
163
164 # undef TDS_PUT_UA2LE
165 # undef TDS_PUT_UA4LE
166 # define TDS_PUT_UA2LE(ptr,val) TDS_PUT_A2LE(ptr,val)
167 # define TDS_PUT_UA4LE(ptr,val) TDS_PUT_A4LE(ptr,val)
168 # endif
169 #endif
170
171 #if defined(__linux__) && defined(__GNUC__) && defined(__i386__)
172 # include <byteswap.h>
173 # undef TDS_GET_UA2BE
174 # undef TDS_GET_UA4BE
175 # define TDS_GET_UA2BE(ptr) ({ TDS_USMALLINT _tds_si = TDS_GET_UA2LE(ptr); bswap_16(_tds_si); })
176 # define TDS_GET_UA4BE(ptr) ({ TDS_UINT _tds_i = TDS_GET_UA4LE(ptr); bswap_32(_tds_i); })
177
178 # undef TDS_PUT_UA2BE
179 # undef TDS_PUT_UA4BE
180 # define TDS_PUT_UA2BE(ptr,val) do {\
181 TDS_USMALLINT _tds_si = bswap_16(val); TDS_PUT_UA2LE(ptr,_tds_si); } while(0)
182 # define TDS_PUT_UA4BE(ptr,val) do {\
183 TDS_UINT _tds_i = bswap_32(val); TDS_PUT_UA4LE(ptr,_tds_i); } while(0)
184 #endif
185
186 #endif