/*
 * Copyright (C) 2010 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if !USE(OPENTYPE_SANITIZER)

#include "WOFF2FileFormat.h"

#if ENABLE(WOFF2)
#include "woff2_common.h"
#include "woff2/decode.h"

#include "SharedBuffer.h"

#if OS(UNIX) || OS(VXWORKS)
#include <netinet/in.h>
#endif

#if OS(WINDOWS)
#if CPU(BIG_ENDIAN)
#define ntohs(x) ((uint16_t)(x))
#define htons(x) ((uint16_t)(x))
#define ntohl(x) ((uint32_t)(x))
#define htonl(x) ((uint32_t)(x))
#elif CPU(MIDDLE_ENDIAN)
#define ntohs(x) ((unit16_t)(x))
#define htons(x) ((uint16_t)(x))
#define ntohl(x) ((uint32_t)((((uint32_t)(x) & 0xffff0000) >> 16) | (((uint32_t)(x) & 0xffff) << 16))
#define htonl(x) ntohl(x)
#else
#define ntohs(x) ((uint16_t)((((uint16_t)(x) & 0xff00) >> 8) | (((uint16_t)(x) & 0x00ff) << 8)))
#define htons(x) ntohs(x)
#define ntohl(x) ((uint32_t)((((uint32_t)(x) & 0xff000000) >> 24) | (((uint32_t)(x) & 0x00ff0000) >>  8) | \
                 (((uint32_t)(x) & 0x0000ff00) <<  8) | (((uint32_t)(x) & 0x000000ff) << 24)))
#define htonl(x) ntohl(x)
#endif
#endif // OS(WINDOWS)

namespace WebCore {

static bool readUInt32(SharedBuffer* buffer, size_t& offset, uint32_t& value)
{
    ASSERT_ARG(offset, offset <= buffer->size());
    if (buffer->size() - offset < sizeof(value))
        return false;

    value = ntohl(*reinterpret_cast_ptr<const uint32_t*>(buffer->data() + offset));
    offset += sizeof(value);

    return true;
}

bool isWOFF2(SharedBuffer* buffer)
{
    size_t offset = 0;
    uint32_t signature;

    return readUInt32(buffer, offset, signature) && signature == woff2::kWoff2Signature;
}

bool convertWOFF2ToSfnt(SharedBuffer* woff, Vector<char>& sfnt)
{
    ASSERT_ARG(sfnt, sfnt.isEmpty());

    size_t decompressed_size = woff2::ComputeWOFF2FinalSize(reinterpret_cast<const uint8_t*>(woff->data()), woff->size());

    sfnt.resize(decompressed_size);

    return woff2::ConvertWOFF2ToTTF(reinterpret_cast<uint8_t*>(sfnt.data()), decompressed_size,
    		reinterpret_cast<const uint8_t*>(woff->data()), woff->size());
}
    
} // namespace WebCore

#else // ENABLE(WOFF2)

namespace WebCore {

bool isWOFF2(SharedBuffer*)
{
	return false;
}

bool convertWOFF2ToSfnt(SharedBuffer*, Vector<char>&)
{
	return false;
}

}

#endif // ENABLE(WOFF2)

#endif // !USE(OPENTYPE_SANITIZER)
