4 * Copyright 2003 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #define NONAMELESSUNION
21 #define NONAMELESSSTRUCT
27 /* Size constraints */
29 #define I1_MIN ((-I1_MAX)-1)
33 #define I2_MIN ((-I2_MAX)-1)
34 #define UI2_MAX 0xffff
36 #define I4_MAX 0x7fffffff
37 #define I4_MIN ((-I4_MAX)-1)
38 #define UI4_MAX 0xffffffff
40 #define I8_MAX (((LONGLONG)I4_MAX << 32) | UI4_MAX)
41 #define I8_MIN ((-I8_MAX)-1)
42 #define UI8_MAX (((ULONGLONG)UI4_MAX << 32) | UI4_MAX)
44 #define DATE_MAX 2958465
45 #define DATE_MIN -657434
46 #define R4_MAX 3.402823567797336e38
47 #define R4_MIN 1.40129846432481707e-45
48 #define R8_MAX 1.79769313486231470e+308
49 #define R8_MIN 4.94065645841246544e-324
51 /* Value of sign for a positive number */
54 /* Native headers don't change the union ordering for DECIMAL sign/scale (duh).
55 * This means that the signscale member is only useful for setting both members to 0.
56 * SIGNSCALE creates endian-correct values so that we can properly set both at once
57 * to values other than 0.
59 #ifdef WORDS_BIGENDIAN
60 #define SIGNSCALE(sign,scale) (((scale) << 8) | sign)
62 #define SIGNSCALE(sign,scale) (((sign) << 8) | scale)
65 /* Macros for getting at a DECIMAL's parts */
66 #define DEC_SIGN(d) ((d)->u.s.sign)
67 #define DEC_SCALE(d) ((d)->u.s.scale)
68 #define DEC_SIGNSCALE(d) ((d)->u.signscale)
69 #define DEC_HI32(d) ((d)->Hi32)
70 #define DEC_MID32(d) ((d)->u1.s1.Mid32)
71 #define DEC_LO32(d) ((d)->u1.s1.Lo32)
72 #define DEC_LO64(d) ((d)->u1.Lo64)
74 #define DEC_MAX_SCALE 28 /* Maximum scale for a decimal */
76 /* Inline return type */
77 #define RETTYP inline static HRESULT
79 /* Simple compiler cast from one type to another */
80 #define SIMPLE(dest, src, func) RETTYP _##func(src in, dest* out) { \
81 *out = in; return S_OK; }
83 /* Compiler cast where input cannot be negative */
84 #define NEGTST(dest, src, func) RETTYP _##func(src in, dest* out) { \
85 if (in < (src)0) return DISP_E_OVERFLOW; *out = in; return S_OK; }
87 /* Compiler cast where input cannot be > some number */
88 #define POSTST(dest, src, func, tst) RETTYP _##func(src in, dest* out) { \
89 if (in > (dest)tst) return DISP_E_OVERFLOW; *out = in; return S_OK; }
91 /* Compiler cast where input cannot be < some number or >= some other number */
92 #define BOTHTST(dest, src, func, lo, hi) RETTYP _##func(src in, dest* out) { \
93 if (in < (dest)lo || in > hi) return DISP_E_OVERFLOW; *out = in; return S_OK; }
95 /* Conversions from IDispatch use the same code */
96 HRESULT VARIANT_FromDisp(IDispatch*,LCID,void*,VARTYPE);
97 /* As do conversions from BSTR to numeric types */
98 HRESULT VARIANT_NumberFromBstr(OLECHAR*,LCID,ULONG,void*,VARTYPE);
100 #define CY_MULTIPLIER 10000 /* 4 dp of precision */
101 #define CY_MULTIPLIER_F 10000.0
102 #define CY_HALF (CY_MULTIPLIER/2) /* 0.5 */
103 #define CY_HALF_F (CY_MULTIPLIER_F/2.0)
106 POSTST(signed char, BYTE, VarI1FromUI1, I1_MAX);
107 BOTHTST(signed char, SHORT, VarI1FromI2, I1_MIN, I1_MAX);
108 BOTHTST(signed char, LONG, VarI1FromI4, I1_MIN, I1_MAX);
109 #define _VarI1FromR4(flt,out) VarI1FromR8((double)flt,out)
110 #define _VarI1FromR8 VarI1FromR8
111 #define _VarI1FromCy VarI1FromCy
112 #define _VarI1FromDate(dt,out) VarI1FromR8((double)dt,out)
113 #define _VarI1FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_I1)
114 #define _VarI1FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, (BYTE*)out, VT_I1)
115 SIMPLE(signed char, VARIANT_BOOL, VarI1FromBool);
116 POSTST(signed char, USHORT, VarI1FromUI2, I1_MAX);
117 POSTST(signed char, ULONG, VarI1FromUI4, I1_MAX);
118 #define _VarI1FromDec VarI1FromDec
119 BOTHTST(signed char, LONG64, VarI1FromI8, I1_MIN, I1_MAX);
120 POSTST(signed char, ULONG64, VarI1FromUI8, I1_MAX);
123 BOTHTST(BYTE, SHORT, VarUI1FromI2, UI1_MIN, UI1_MAX);
124 #define _VarUI1FromR4(flt,out) VarUI1FromR8((double)flt,out)
125 #define _VarUI1FromR8 VarUI1FromR8
126 #define _VarUI1FromCy VarUI1FromCy
127 #define _VarUI1FromDate(dt,out) VarUI1FromR8((double)dt,out)
128 #define _VarUI1FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_UI1)
129 #define _VarUI1FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_UI1)
130 SIMPLE(BYTE, VARIANT_BOOL, VarUI1FromBool);
131 NEGTST(BYTE, signed char, VarUI1FromI1);
132 POSTST(BYTE, USHORT, VarUI1FromUI2, UI1_MAX);
133 BOTHTST(BYTE, LONG, VarUI1FromI4, UI1_MIN, UI1_MAX);
134 POSTST(BYTE, ULONG, VarUI1FromUI4, UI1_MAX);
135 #define _VarUI1FromDec VarUI1FromDec
136 BOTHTST(BYTE, LONG64, VarUI1FromI8, UI1_MIN, UI1_MAX);
137 POSTST(BYTE, ULONG64, VarUI1FromUI8, UI1_MAX);
140 POSTST(SHORT, BYTE, VarI2FromUI1, I2_MAX);
141 BOTHTST(SHORT, LONG, VarI2FromI4, I2_MIN, I2_MAX);
142 #define _VarI2FromR4(flt,out) VarI2FromR8((double)flt,out)
143 #define _VarI2FromR8 VarI2FromR8
144 #define _VarI2FromCy VarI2FromCy
145 #define _VarI2FromDate(dt,out) VarI2FromR8((double)dt,out)
146 #define _VarI2FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_I2)
147 #define _VarI2FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, (BYTE*)out, VT_I2)
148 SIMPLE(SHORT, VARIANT_BOOL, VarI2FromBool);
149 SIMPLE(SHORT, signed char, VarI2FromI1);
150 POSTST(SHORT, USHORT, VarI2FromUI2, I2_MAX);
151 POSTST(SHORT, ULONG, VarI2FromUI4, I2_MAX);
152 #define _VarI2FromDec VarI2FromDec
153 BOTHTST(SHORT, LONG64, VarI2FromI8, I2_MIN, I2_MAX);
154 POSTST(SHORT, ULONG64, VarI2FromUI8, I2_MAX);
157 SIMPLE(USHORT, BYTE, VarUI2FromUI1);
158 NEGTST(USHORT, SHORT, VarUI2FromI2);
159 BOTHTST(USHORT, LONG, VarUI2FromI4, UI2_MIN, UI2_MAX);
160 #define _VarUI2FromR4(flt,out) VarUI2FromR8((double)flt,out)
161 #define _VarUI2FromR8 VarUI2FromR8
162 #define _VarUI2FromCy VarUI2FromCy
163 #define _VarUI2FromDate(dt,out) VarUI2FromR8((double)dt,out)
164 #define _VarUI2FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_UI2)
165 #define _VarUI2FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_UI2)
166 SIMPLE(USHORT, VARIANT_BOOL, VarUI2FromBool);
167 NEGTST(USHORT, signed char, VarUI2FromI1);
168 POSTST(USHORT, ULONG, VarUI2FromUI4, UI2_MAX);
169 #define _VarUI2FromDec VarUI2FromDec
170 BOTHTST(USHORT, LONG64, VarUI2FromI8, UI2_MIN, UI2_MAX);
171 POSTST(USHORT, ULONG64, VarUI2FromUI8, UI2_MAX);
174 SIMPLE(LONG, BYTE, VarI4FromUI1);
175 SIMPLE(LONG, SHORT, VarI4FromI2);
176 #define _VarI4FromR4(flt,out) VarI4FromR8((double)flt,out)
177 #define _VarI4FromR8 VarI4FromR8
178 #define _VarI4FromCy VarI4FromCy
179 #define _VarI4FromDate(dt,out) VarI4FromR8((double)dt,out)
180 #define _VarI4FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_I4)
181 #define _VarI4FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, (BYTE*)out, VT_I4)
182 SIMPLE(LONG, VARIANT_BOOL, VarI4FromBool);
183 SIMPLE(LONG, signed char, VarI4FromI1);
184 SIMPLE(LONG, USHORT, VarI4FromUI2);
185 POSTST(LONG, ULONG, VarI4FromUI4, I4_MAX);
186 #define _VarI4FromDec VarI4FromDec
187 BOTHTST(LONG, LONG64, VarI4FromI8, I4_MIN, I4_MAX);
188 POSTST(LONG, ULONG64, VarI4FromUI8, I4_MAX);
191 SIMPLE(ULONG, BYTE, VarUI4FromUI1);
192 NEGTST(ULONG, SHORT, VarUI4FromI2);
193 NEGTST(ULONG, LONG, VarUI4FromI4);
194 #define _VarUI4FromR4(flt,out) VarUI4FromR8((double)flt,out)
195 #define _VarUI4FromR8 VarUI4FromR8
196 #define _VarUI4FromCy VarUI4FromCy
197 #define _VarUI4FromDate(dt,out) VarUI4FromR8((double)dt,out)
198 #define _VarUI4FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_UI4)
199 #define _VarUI4FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_UI4)
200 SIMPLE(ULONG, VARIANT_BOOL, VarUI4FromBool);
201 NEGTST(ULONG, signed char, VarUI4FromI1);
202 SIMPLE(ULONG, USHORT, VarUI4FromUI2);
203 #define _VarUI4FromDec VarUI4FromDec
204 BOTHTST(ULONG, LONG64, VarUI4FromI8, UI4_MIN, UI4_MAX);
205 POSTST(ULONG, ULONG64, VarUI4FromUI8, UI4_MAX);
208 SIMPLE(LONG64, BYTE, VarI8FromUI1);
209 SIMPLE(LONG64, SHORT, VarI8FromI2);
210 #define _VarI8FromR4 VarI8FromR8
211 #define _VarI8FromR8 VarI8FromR8
212 #define _VarI8FromCy VarI8FromCy
213 #define _VarI8FromDate(dt,out) VarI8FromR8((double)dt,out)
214 #define _VarI8FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_I8)
215 #define _VarI8FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_I8)
216 #define _VarI8FromBool _VarI8FromI2
217 SIMPLE(LONG64, signed char, VarI8FromI1);
218 SIMPLE(LONG64, USHORT, VarI8FromUI2);
219 SIMPLE(LONG64, LONG, VarI8FromI4);
220 SIMPLE(LONG64, ULONG, VarI8FromUI4);
221 #define _VarI8FromDec VarI8FromDec
222 POSTST(LONG64, ULONG64, VarI8FromUI8, I8_MAX);
225 SIMPLE(ULONG64, BYTE, VarUI8FromUI1);
226 NEGTST(ULONG64, SHORT, VarUI8FromI2);
227 #define _VarUI8FromR4 VarUI8FromR8
228 #define _VarUI8FromR8 VarUI8FromR8
229 #define _VarUI8FromCy VarUI8FromCy
230 #define _VarUI8FromDate(dt,out) VarUI8FromR8((double)dt,out)
231 #define _VarUI8FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_UI8)
232 #define _VarUI8FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_UI8)
233 #define _VarUI8FromBool _VarI8FromI2
234 NEGTST(ULONG64, signed char, VarUI8FromI1);
235 SIMPLE(ULONG64, USHORT, VarUI8FromUI2);
236 NEGTST(ULONG64, LONG, VarUI8FromI4);
237 SIMPLE(ULONG64, ULONG, VarUI8FromUI4);
238 #define _VarUI8FromDec VarUI8FromDec
239 NEGTST(ULONG64, LONG64, VarUI8FromI8);
242 SIMPLE(float, BYTE, VarR4FromUI1);
243 SIMPLE(float, SHORT, VarR4FromI2);
244 RETTYP _VarR4FromR8(double i, float* o) {
245 double d = i < 0.0 ? -i : i;
246 if (d > R4_MAX) return DISP_E_OVERFLOW;
250 RETTYP _VarR4FromCy(CY i, float* o) { *o = (double)i.int64 / CY_MULTIPLIER_F; return S_OK; }
251 #define _VarR4FromDate(dt,out) _VarR4FromR8((double)dt,out)
252 #define _VarR4FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_R4)
253 #define _VarR4FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_R4)
254 #define _VarR4FromBool _VarR4FromI2
255 SIMPLE(float, signed char, VarR4FromI1);
256 SIMPLE(float, USHORT, VarR4FromUI2);
257 SIMPLE(float, LONG, VarR4FromI4);
258 SIMPLE(float, ULONG, VarR4FromUI4);
259 #define _VarR4FromDec VarR4FromDec
260 SIMPLE(float, LONG64, VarR4FromI8);
261 SIMPLE(float, ULONG64, VarR4FromUI8);
264 SIMPLE(double, BYTE, VarR8FromUI1);
265 SIMPLE(double, SHORT, VarR8FromI2);
266 SIMPLE(double, float, VarR8FromR4);
267 RETTYP _VarR8FromCy(CY i, double* o) { *o = (double)i.int64 / CY_MULTIPLIER_F; return S_OK; }
268 SIMPLE(double, DATE, VarR8FromDate);
269 #define _VarR8FromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_R8)
270 #define _VarR8FromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_R8)
271 #define _VarR8FromBool _VarR8FromI2
272 SIMPLE(double, signed char, VarR8FromI1);
273 SIMPLE(double, USHORT, VarR8FromUI2);
274 SIMPLE(double, LONG, VarR8FromI4);
275 SIMPLE(double, ULONG, VarR8FromUI4);
276 #define _VarR8FromDec VarR8FromDec
277 SIMPLE(double, LONG64, VarR8FromI8);
278 SIMPLE(double, ULONG64, VarR8FromUI8);
281 #define BOOLFUNC(src, func) RETTYP _##func(src in, VARIANT_BOOL* out) { \
282 *out = in ? VARIANT_TRUE : VARIANT_FALSE; return S_OK; }
284 BOOLFUNC(signed char,VarBoolFromI1);
285 BOOLFUNC(BYTE,VarBoolFromUI1);
286 BOOLFUNC(SHORT,VarBoolFromI2);
287 BOOLFUNC(USHORT,VarBoolFromUI2);
288 BOOLFUNC(LONG,VarBoolFromI4);
289 BOOLFUNC(ULONG,VarBoolFromUI4);
290 BOOLFUNC(LONG64,VarBoolFromI8);
291 BOOLFUNC(ULONG64,VarBoolFromUI8);
292 #define _VarBoolFromR4(flt,out) _VarBoolFromR8((double)flt,out)
293 BOOLFUNC(double,VarBoolFromR8);
294 #define _VarBoolFromCy(i,o) _VarBoolFromI8(i.int64,o)
295 #define _VarBoolFromDate(dt,out) _VarBoolFromR8((double)dt,out)
296 #define _VarBoolFromStr VarBoolFromStr
297 #define _VarBoolFromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, (BYTE*)out, VT_BOOL)
298 #define _VarBoolFromDec VarBoolFromDec
301 #define _VarDecFromUI1 VarDecFromUI4
302 #define _VarDecFromI2 VarDecFromI4
303 #define _VarDecFromR4 VarDecFromR8
304 #define _VarDecFromR8 VarDecFromR8
305 #define _VarDecFromCy VarDecFromCy
306 #define _VarDecFromDate(dt,out) VarDecFromR8((double)dt,out)
307 #define _VarDecFromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_DECIMAL)
308 #define _VarDecFromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_DECIMAL)
309 #define _VarDecFromBool VarDecFromBool
310 #define _VarDecFromI1 VarDecFromI4
311 #define _VarDecFromUI2 VarDecFromUI4
312 #define _VarDecFromI4 VarDecFromI4
313 #define _VarDecFromUI4 VarDecFromUI4
314 #define _VarDecFromI8 VarDecFromI8
315 #define _VarDecFromUI8 VarDecFromUI8
318 #define _VarCyFromUI1 VarCyFromR8
319 #define _VarCyFromI2 VarCyFromR8
320 #define _VarCyFromR4 VarCyFromR8
321 #define _VarCyFromR8 VarCyFromR8
322 #define _VarCyFromDate VarCyFromR8
323 #define _VarCyFromStr(str,lcid,flags,out) VARIANT_NumberFromBstr(str,lcid,flags,(BYTE*)out,VT_CY)
324 #define _VarCyFromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_CY)
325 #define _VarCyFromBool VarCyFromR8
326 #define _VarCyFromI1 VarCyFromR8
327 #define _VarCyFromUI2 VarCyFromR8
328 #define _VarCyFromI4 VarCyFromR8
329 #define _VarCyFromUI4 VarCyFromR8
330 #define _VarCyFromDec VarCyFromDec
331 RETTYP _VarCyFromI8(LONG64 i, CY* o) {
332 if (i <= (I8_MIN/CY_MULTIPLIER) || i >= (I8_MAX/CY_MULTIPLIER)) return DISP_E_OVERFLOW;
333 o->int64 = i * CY_MULTIPLIER;
336 #define _VarCyFromUI8 VarCyFromR8
339 #define _VarDateFromUI1 VarDateFromR8
340 #define _VarDateFromI2 VarDateFromR8
341 #define _VarDateFromR4 VarDateFromR8
342 #define _VarDateFromR8 VarDateFromR8
343 #define _VarDateFromCy VarDateFromCy
344 #define _VarDateFromStr VarDateFromStr
345 #define _VarDateFromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_DATE)
346 #define _VarDateFromBool VarDateFromR8
347 #define _VarDateFromI1 VarDateFromR8
348 #define _VarDateFromUI2 VarDateFromR8
349 #define _VarDateFromI4 VarDateFromR8
350 #define _VarDateFromUI4 VarDateFromR8
351 #define _VarDateFromDec VarDateFromDec
352 #define _VarDateFromI8 VarDateFromR8
353 #define _VarDateFromUI8 VarDateFromR8
356 #define _VarBstrFromUI1 VarBstrFromUI4
357 #define _VarBstrFromI2 VarBstrFromI4
358 #define _VarBstrFromR4 VarBstrFromR8
359 #define _VarBstrFromR8 VarBstrFromR8
360 #define _VarBstrFromCy VarBstrFromCy
361 #define _VarBstrFromDate VarBstrFromDate
362 #define _VarBstrFromDisp(disp,lcid,out) VARIANT_FromDisp(disp, lcid, out, VT_BSTR)
363 #define _VarBstrFromBool VarBstrFromBool
364 #define _VarBstrFromI1 VarBstrFromI4
365 #define _VarBstrFromUI2 VarBstrFromUI4
366 #define _VarBstrFromI4 VarBstrFromI4
367 #define _VarBstrFromUI4 VarBstrFromUI4
368 #define _VarBstrFromDec VarBstrFromDec
369 #define _VarBstrFromI8 VarBstrFromI8
370 #define _VarBstrFromUI8 VarBstrFromUI8
372 /* Macro to inline conversion from a float or double to any integer type,
373 * rounding according to the 'dutch' convention.
375 #define OLEAUT32_DutchRound(typ, value, res) do { \
376 double whole = floor((double)value), fract = (double)value - whole; \
377 if (fract > 0.5) res = (typ)whole + (typ)1; \
378 else if (fract == 0.5) { typ is_odd = (typ)whole & 1; res = whole + is_odd; } \
379 else if (fract >= 0.0) res = (typ)whole; \
380 else if (fract == -0.5) { typ is_odd = (typ)whole & 1; res = whole - is_odd; } \
381 else if (fract > -0.5) res = (typ)whole; \
382 else res = (typ)whole - (typ)1; \
385 /* Localised text for variant conversions */
386 typedef struct tagVARIANT_TEXT
397 #define NUM_LOCALISED_LANGS 13
399 extern const VARIANT_TEXT VARIANT_LocalisedTextList[NUM_LOCALISED_LANGS];
401 const VARIANT_TEXT *VARIANT_GetLocalisedText(LANGID);
403 /* The localised characters that make up a valid number */
404 typedef struct tagVARIANT_NUMBER_CHARS
406 WCHAR cNegativeSymbol;
407 WCHAR cPositiveSymbol;
409 WCHAR cDigitSeperator;
410 WCHAR cCurrencyLocal;
411 WCHAR cCurrencyLocal2;
412 WCHAR cCurrencyDecimalPoint;
413 WCHAR cCurrencyDigitSeperator;
414 } VARIANT_NUMBER_CHARS;
416 void VARIANT_GetLocalisedNumberChars(VARIANT_NUMBER_CHARS*,LCID,DWORD);