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