Commit b4ac43ba authored by jsbell@chromium.org's avatar jsbell@chromium.org
Browse files

Add htons/htonl definitions and implementations

https://bugs.webkit.org/show_bug.cgi?id=98054

Reviewed by Darin Adler.

Source/WebCore:

Update users of htons and friends to use new wtf/ByteOrder.h header.

No new tests - just refactoring.

* platform/graphics/WOFFFileFormat.cpp:
* platform/graphics/chromium/VDMXParser.cpp:

Source/WTF:

For parsing or serializing binary data, byte order matters. The canonical htons/htonl/
ntohs/ntohl functions are not present everywhere, so implementations are proliferating in
parsers. Expose a new WTF header (wtf/ByteOrder.h) that includes the standard
implementations on UNIX-like OSs and provides basic inlined implementations on Windows.

* GNUmakefile.list.am:
* WTF.gypi:
* WTF.pro:
* WTF.vcproj/WTF.vcproj:
* WTF.xcodeproj/project.pbxproj:
* wtf/ByteOrder.h: Added.
(WTF::wswap32): Inline functions so arguments are only evaluated once.
(WTF::bswap32):
(WTF::bswap16):
(ntohs): Inline functions on OS(WINDOWS) to match macros on OS(UNIX)
(htons):
(ntohl):
(htonl):
* wtf/CMakeLists.txt:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@130238 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent eec26cbc
2012-10-02 Joshua Bell <jsbell@chromium.org>
Add htons/htonl definitions and implementations
https://bugs.webkit.org/show_bug.cgi?id=98054
Reviewed by Darin Adler.
For parsing or serializing binary data, byte order matters. The canonical htons/htonl/
ntohs/ntohl functions are not present everywhere, so implementations are proliferating in
parsers. Expose a new WTF header (wtf/ByteOrder.h) that includes the standard
implementations on UNIX-like OSs and provides basic inlined implementations on Windows.
* GNUmakefile.list.am:
* WTF.gypi:
* WTF.pro:
* WTF.vcproj/WTF.vcproj:
* WTF.xcodeproj/project.pbxproj:
* wtf/ByteOrder.h: Added.
(WTF::wswap32): Inline functions so arguments are only evaluated once.
(WTF::bswap32):
(WTF::bswap16):
(ntohs): Inline functions on OS(WINDOWS) to match macros on OS(UNIX)
(htons):
(ntohl):
(htonl):
* wtf/CMakeLists.txt:
2012-10-02 Michael Saboff <msaboff@apple.com>
HTMLConstructionSite::insertTextNode isn't optimized for 8 bit strings
......
......@@ -20,6 +20,7 @@ wtf_sources += \
Source/WTF/wtf/BloomFilter.h \
Source/WTF/wtf/BoundsCheckedPointer.h \
Source/WTF/wtf/BumpPointerAllocator.h \
Source/WTF/wtf/ByteOrder.h \
Source/WTF/wtf/CheckedArithmetic.h \
Source/WTF/wtf/CheckedBoolean.h \
Source/WTF/wtf/Compiler.h \
......
......@@ -14,6 +14,7 @@
'wtf/BlockStack.h',
'wtf/BloomFilter.h',
'wtf/BumpPointerAllocator.h',
'wtf/ByteOrder.h',
'wtf/CheckedArithmetic.h',
'wtf/CheckedBoolean.h',
'wtf/Compiler.h',
......
......@@ -34,6 +34,7 @@ HEADERS += \
BloomFilter.h \
BoundsCheckedPointer.h \
BumpPointerAllocator.h \
ByteOrder.h \
CheckedArithmetic.h \
Compiler.h \
CryptographicallyRandomNumber.h \
......
......@@ -644,6 +644,10 @@
RelativePath="..\wtf\BumpPointerAllocator.h"
>
</File>
<File
RelativePath="..\wtf\ByteOrder.h"
>
</File>
<File
RelativePath="..\wtf\CheckedArithmetic.h"
>
......
......@@ -68,6 +68,7 @@
A8A4738F151A825B004123FF /* BloomFilter.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A47265151A825A004123FF /* BloomFilter.h */; };
A8A47390151A825B004123FF /* BoundsCheckedPointer.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A47266151A825A004123FF /* BoundsCheckedPointer.h */; };
A8A47391151A825B004123FF /* BumpPointerAllocator.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A47267151A825A004123FF /* BumpPointerAllocator.h */; };
EB95E1F0161A72410089A2F5 /* ByteOrder.h in Headers */ = {isa = PBXBuildFile; fileRef = EB95E1EF161A72410089A2F5 /* ByteOrder.h */; };
A8A47394151A825B004123FF /* CheckedArithmetic.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A4726A151A825A004123FF /* CheckedArithmetic.h */; };
A8A47395151A825B004123FF /* CheckedBoolean.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A4726B151A825A004123FF /* CheckedBoolean.h */; };
A8A47398151A825B004123FF /* Compiler.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A47270151A825A004123FF /* Compiler.h */; };
......@@ -348,6 +349,7 @@
A8A47265151A825A004123FF /* BloomFilter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BloomFilter.h; sourceTree = "<group>"; };
A8A47266151A825A004123FF /* BoundsCheckedPointer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BoundsCheckedPointer.h; sourceTree = "<group>"; };
A8A47267151A825A004123FF /* BumpPointerAllocator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BumpPointerAllocator.h; sourceTree = "<group>"; };
EB95E1EF161A72410089A2F5 /* ByteOrder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ByteOrder.h; sourceTree = "<group>"; };
A8A4726A151A825A004123FF /* CheckedArithmetic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CheckedArithmetic.h; sourceTree = "<group>"; };
A8A4726B151A825A004123FF /* CheckedBoolean.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CheckedBoolean.h; sourceTree = "<group>"; };
A8A47270151A825A004123FF /* Compiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Compiler.h; sourceTree = "<group>"; };
......@@ -640,6 +642,7 @@
A8A47265151A825A004123FF /* BloomFilter.h */,
A8A47266151A825A004123FF /* BoundsCheckedPointer.h */,
A8A47267151A825A004123FF /* BumpPointerAllocator.h */,
EB95E1EF161A72410089A2F5 /* ByteOrder.h */,
A8A4726A151A825A004123FF /* CheckedArithmetic.h */,
A8A4726B151A825A004123FF /* CheckedBoolean.h */,
A8A47270151A825A004123FF /* Compiler.h */,
......@@ -994,6 +997,7 @@
A8A4738F151A825B004123FF /* BloomFilter.h in Headers */,
A8A47390151A825B004123FF /* BoundsCheckedPointer.h in Headers */,
A8A47391151A825B004123FF /* BumpPointerAllocator.h in Headers */,
EB95E1F0161A72410089A2F5 /* ByteOrder.h in Headers */,
A8A473AD151A825B004123FF /* cached-powers.h in Headers */,
A8A4745E151A825B004123FF /* CharacterNames.h in Headers */,
A8A47394151A825B004123FF /* CheckedArithmetic.h in Headers */,
......
/*
* Copyright (C) 2012 Google 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:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 THE COPYRIGHT
* OWNER OR 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.
*/
#ifndef WTF_ByteOrder_h
#define WTF_ByteOrder_h
#include <wtf/Platform.h>
#if OS(UNIX)
#include <arpa/inet.h>
#endif
#if OS(WINDOWS)
namespace WTF {
inline uint32_t wswap32(uint32_t x) { return ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16); }
inline uint32_t bswap32(uint32_t x) { return ((x & 0xff000000) >> 24) | ((x & 0x00ff0000) >> 8) | ((x & 0x0000ff00) << 8) | ((x & 0x000000ff) << 24); }
inline uint16_t bswap16(uint16_t x) { return ((x & 0xff00) >> 8) | ((x & 0x00ff) << 8); }
} // namespace WTF
#if CPU(BIG_ENDIAN)
inline uint16_t ntohs(uint16_t x) { return x; }
inline uint16_t htons(uint16_t x) { return x; }
inline uint32_t ntohl(uint32_t x) { return x; }
inline uint32_t htonl(uint32_t x) { return x; }
#elif CPU(MIDDLE_ENDIAN)
inline uint16_t ntohs(unit16_t x) { return x; }
inline uint16_t htons(uint16_t x) { return x; }
inline uint32_t ntohl(uint32_t x) { return WTF::wswap32(x); }
inline uint32_t htonl(uint32_t x) { return WTF::wswap32(x); }
#else
inline uint16_t ntohs(uint16_t x) { return WTF::bswap16(x); }
inline uint16_t htons(uint16_t x) { return WTF::bswap16(x); }
inline uint32_t ntohl(uint32_t x) { return WTF::bswap32(x); }
inline uint32_t htonl(uint32_t x) { return WTF::bswap32(x); }
#endif
#endif // OS(WINDOWS)
#endif // WTF_ByteOrder_h
......@@ -10,6 +10,7 @@ SET(WTF_HEADERS
Bitmap.h
BoundsCheckedPointer.h
BumpPointerAllocator.h
ByteOrder.h
Compiler.h
Complex.h
CryptographicallyRandomNumber.h
......
2012-10-02 Joshua Bell <jsbell@chromium.org>
Add htons/htonl definitions and implementations
https://bugs.webkit.org/show_bug.cgi?id=98054
Reviewed by Darin Adler.
Update users of htons and friends to use new wtf/ByteOrder.h header.
No new tests - just refactoring.
* platform/graphics/WOFFFileFormat.cpp:
* platform/graphics/chromium/VDMXParser.cpp:
== Rolled over to ChangeLog-2012-10-02 ==
......@@ -30,30 +30,7 @@
#if !USE(OPENTYPE_SANITIZER)
#include "SharedBuffer.h"
#if OS(UNIX)
#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)
#include <wtf/ByteOrder.h>
namespace WebCore {
......
......@@ -32,8 +32,7 @@
#include <stdlib.h>
#include <string.h>
// For htons/ntohs
#include <arpa/inet.h>
#include <wtf/ByteOrder.h>
// Buffer helper class
//
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment