1 : /* ***** BEGIN LICENSE BLOCK *****
2 : *
3 : * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 : *
5 : * Redistribution and use in source and binary forms, with or without
6 : * modification, are permitted provided that the following conditions
7 : * are met:
8 : * 1. Redistributions of source code must retain the above copyright
9 : * notice, this list of conditions and the following disclaimer.
10 : * 2. Redistributions in binary form must reproduce the above copyright
11 : * notice, this list of conditions and the following disclaimer in the
12 : * documentation and/or other materials provided with the distribution.
13 : *
14 : * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 : * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 : * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 : * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 : *
26 : * Contributor(s):
27 : * Josh Aas <josh@mozilla.com>
28 : *
29 : * ***** END LICENSE BLOCK ***** */
30 :
31 : #include "nptest_utils.h"
32 :
33 : #include <stdlib.h>
34 : #include <string.h>
35 : #include <assert.h>
36 :
37 : NPUTF8*
38 0 : createCStringFromNPVariant(const NPVariant* variant)
39 : {
40 0 : size_t length = NPVARIANT_TO_STRING(*variant).UTF8Length;
41 0 : NPUTF8* result = (NPUTF8*)malloc(length + 1);
42 0 : memcpy(result, NPVARIANT_TO_STRING(*variant).UTF8Characters, length);
43 0 : result[length] = '\0';
44 0 : return result;
45 : }
46 :
47 : NPIdentifier
48 0 : variantToIdentifier(NPVariant variant)
49 : {
50 0 : if (NPVARIANT_IS_STRING(variant))
51 0 : return stringVariantToIdentifier(variant);
52 0 : else if (NPVARIANT_IS_INT32(variant))
53 0 : return int32VariantToIdentifier(variant);
54 0 : else if (NPVARIANT_IS_DOUBLE(variant))
55 0 : return doubleVariantToIdentifier(variant);
56 0 : return 0;
57 : }
58 :
59 : NPIdentifier
60 0 : stringVariantToIdentifier(NPVariant variant)
61 : {
62 0 : assert(NPVARIANT_IS_STRING(variant));
63 0 : NPUTF8* utf8String = createCStringFromNPVariant(&variant);
64 0 : NPIdentifier identifier = NPN_GetStringIdentifier(utf8String);
65 0 : free(utf8String);
66 0 : return identifier;
67 : }
68 :
69 : NPIdentifier
70 0 : int32VariantToIdentifier(NPVariant variant)
71 : {
72 0 : assert(NPVARIANT_IS_INT32(variant));
73 0 : int32_t integer = NPVARIANT_TO_INT32(variant);
74 0 : return NPN_GetIntIdentifier(integer);
75 : }
76 :
77 : NPIdentifier
78 0 : doubleVariantToIdentifier(NPVariant variant)
79 : {
80 0 : assert(NPVARIANT_IS_DOUBLE(variant));
81 0 : double value = NPVARIANT_TO_DOUBLE(variant);
82 : // sadly there is no "getdoubleidentifier"
83 0 : int32_t integer = static_cast<int32_t>(value);
84 0 : return NPN_GetIntIdentifier(integer);
85 : }
86 :
87 : /*
88 : * Parse a color in hex format, #AARRGGBB or AARRGGBB.
89 : */
90 : PRUint32
91 0 : parseHexColor(const char* color, int len)
92 : {
93 0 : PRUint8 bgra[4] = { 0, 0, 0, 0xFF };
94 0 : int i = 0;
95 :
96 0 : assert(len == 9 || len == 8);
97 :
98 : // start from the right and work to the left
99 0 : while (len >= 2) { // we have at least #AA or AA left.
100 : char byte[3];
101 : // parse two hex digits
102 0 : byte[0] = color[len - 2];
103 0 : byte[1] = color[len - 1];
104 0 : byte[2] = '\0';
105 :
106 0 : bgra[i] = (PRUint8)(strtoul(byte, NULL, 16) & 0xFF);
107 0 : i++;
108 0 : len -= 2;
109 : }
110 0 : return (bgra[3] << 24) | (bgra[2] << 16) | (bgra[1] << 8) | bgra[0];
111 : }
|