diff --git a/CMakeLists.txt b/CMakeLists.txt index 35642ebcf..1129e2d78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -172,6 +172,8 @@ if(NOT (${GCRYPT_VERSION_STRING} VERSION_LESS "1.6.0")) set(GCRYPT_HAS_SALSA20 1) endif() +find_package(LibMicroHTTPD REQUIRED) + find_package(ZLIB REQUIRED) check_cxx_source_compiles(" diff --git a/README.md b/README.md new file mode 100644 index 000000000..d7483c6ca --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# KeePassX + keepasshttp + autotype + +This code extends the brilliant [KeePassX](https://www.keepassx.org/) program +which accesses [KeePass](http://keepass.info/) password databases. + +I have merged the latest version of the code with Francois Ferrand's +[keepassx-http](https://gitorious.org/keepassx/keepassx-http/) repository. +This adds support for the [keepasshttp](https://github.com/pfn/keepasshttp/) +protocol, enabling automatic form-filling in web browsers. This is accomplished +via a compatible browser plugin such as +[PassIFox](https://passifox.appspot.com/passifox.xpi) for Mozilla Firefox and +[chromeIPass](https://chrome.google.com/webstore/detail/chromeipass/ompiailgknfdndiefoaoiligalphfdae) +for Google Chrome. + +I have also added global autotype for OSX machines and added a few other minor +tweaks and bugfixes. + diff --git a/cmake/FindLibMicroHTTPD.cmake b/cmake/FindLibMicroHTTPD.cmake new file mode 100644 index 000000000..f31928043 --- /dev/null +++ b/cmake/FindLibMicroHTTPD.cmake @@ -0,0 +1,9 @@ + +find_path(MHD_INCLUDE_DIR microhttpd.h) + +find_library(MHD_LIBRARIES microhttpd) + +mark_as_advanced(MHD_LIBRARIES MHD_INCLUDE_DIR) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LibMicroHTTPD DEFAULT_MSG MHD_LIBRARIES MHD_INCLUDE_DIR) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d57153e5f..e9bbbc17e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -102,6 +102,14 @@ set(keepassx_SOURCES gui/group/EditGroupWidget.cpp gui/group/GroupModel.cpp gui/group/GroupView.cpp + http/AccessControlDialog.cpp + http/EntryConfig.cpp + http/HttpPasswordGeneratorWidget.cpp + http/HttpSettings.cpp + http/OptionDialog.cpp + http/Protocol.cpp + http/Server.cpp + http/Service.cpp keys/CompositeKey.cpp keys/CompositeKey_p.h keys/FileKey.cpp @@ -180,6 +188,13 @@ set(keepassx_MOC gui/group/EditGroupWidget.h gui/group/GroupModel.h gui/group/GroupView.h + http/AccessControlDialog.h + http/EntryConfig.h + http/HttpPasswordGeneratorWidget.h + http/OptionDialog.h + http/Protocol.h + http/Server.h + http/Service.h keys/CompositeKey_p.h streams/HashedBlockStream.h streams/LayeredStream.h @@ -207,6 +222,9 @@ set(keepassx_FORMS gui/entry/EditEntryWidgetHistory.ui gui/entry/EditEntryWidgetMain.ui gui/group/EditGroupWidgetMain.ui + http/AccessControlDialog.ui + http/HttpPasswordGeneratorWidget.ui + http/OptionDialog.ui ) if(MINGW) @@ -221,17 +239,27 @@ qt4_wrap_cpp(keepassx_SOURCES ${keepassx_MOC}) add_library(keepassx_core STATIC ${keepassx_SOURCES}) set_target_properties(keepassx_core PROPERTIES COMPILE_DEFINITIONS KEEPASSX_BUILDING_CORE) +add_subdirectory(gui/qocoa) +add_subdirectory(http/qjson) + add_executable(${PROGNAME} WIN32 MACOSX_BUNDLE ${keepassx_SOURCES_MAINEXE}) target_link_libraries(${PROGNAME} keepassx_core + Qocoa + qjson + ${MHD_LIBRARIES} ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} + ${QT_QTNETWORK_LIBRARY} ${GCRYPT_LIBRARIES} ${ZLIB_LIBRARIES}) if(UNIX AND NOT APPLE) target_link_libraries(${PROGNAME} ${QT_QTDBUS_LIBRARY}) endif() +if(APPLE) + set_target_properties(${PROGNAME} PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit") +endif() set_target_properties(${PROGNAME} PROPERTIES ENABLE_EXPORTS ON) diff --git a/src/autotype/AutoTypeAction.cpp b/src/autotype/AutoTypeAction.cpp index cc751abe0..c448ef74c 100644 --- a/src/autotype/AutoTypeAction.cpp +++ b/src/autotype/AutoTypeAction.cpp @@ -87,7 +87,7 @@ void AutoTypeExecutor::execDelay(AutoTypeDelay* action) Tools::wait(action->delayMs); } -void AutoTypeExecutor::execClearField(AutoTypeClearField* action) +void AutoTypeExecutor::execClearField(AutoTypeClearField*) { // TODO: implement } diff --git a/src/autotype/CMakeLists.txt b/src/autotype/CMakeLists.txt index 0bf5fc252..37cb2f917 100644 --- a/src/autotype/CMakeLists.txt +++ b/src/autotype/CMakeLists.txt @@ -9,6 +9,10 @@ if(Q_WS_X11) endif() endif() +if(Q_WS_MAC) + add_subdirectory(mac) +endif() + if(WITH_TESTS) add_subdirectory(test) endif() diff --git a/src/autotype/mac/AutoTypeMac.cpp b/src/autotype/mac/AutoTypeMac.cpp new file mode 100644 index 000000000..6840850eb --- /dev/null +++ b/src/autotype/mac/AutoTypeMac.cpp @@ -0,0 +1,553 @@ +/* + * Copyright (C) 2009-2010 Jeff Gibbons + * Copyright (C) 2005-2008 by Tarek Saidi + * Copyright (C) 2012 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "AutoTypeMac.h" +#include + +static pid_t keepassxPID2; + +#define UNICODE_BUFFER_SIZE 20 +static UniChar unicodeBuffer[UNICODE_BUFFER_SIZE]; +static UniCharCount unicodePtr = 0; +// reusable events +static CGEventRef unicodeEvent = CGEventCreateKeyboardEvent(NULL, 0, true); +CGEventRef AutoTypePlatformMac::keyEvent = + CGEventCreateKeyboardEvent(NULL, 0, true); + +bool AutoTypePlatformMac::inHotKeyEvent = false; + +static const KeycodeWithMods NoKeycodeWithMods = + (KeycodeWithMods){ NoKeycode, 0 }; + +static uint orderedModifiers[] = { + 0, + ( shiftKey ) >> 8, + (controlKey ) >> 8, + ( optionKey ) >> 8, + ( cmdKey ) >> 8, + ( shiftKey | controlKey ) >> 8, + ( shiftKey | optionKey ) >> 8, + ( shiftKey | cmdKey ) >> 8, + (controlKey | optionKey ) >> 8, + (controlKey | cmdKey ) >> 8, + ( optionKey | cmdKey ) >> 8, + ( shiftKey | controlKey | optionKey ) >> 8, + ( shiftKey | controlKey | cmdKey ) >> 8, + ( shiftKey | optionKey | cmdKey ) >> 8, + (controlKey | optionKey | cmdKey ) >> 8, + ( shiftKey | controlKey | optionKey | cmdKey) >> 8 +}; + +static std::map unicodeToKeycodeWithModsMap; + + +AutoTypePlatformMac::AutoTypePlatformMac() : first(true), keepassxPID(0) +{ + globalKey = 0; + globalMod = 0; + inGlobalAutoType = false; + + // initialize hot key handling + hotKeyRef = NULL; + hotKeyID.signature = 'kpsx'; + hotKeyID.id = 1; + EventTypeSpec eventType; + eventType.eventClass = kEventClassKeyboard; + eventType.eventKind = kEventHotKeyPressed; + InstallApplicationEventHandler(&hotKeyHandler, 1, &eventType, this, NULL); + AutoTypePlatformMac::initUnicodeToKeycodeWithModsMap(); +} + + +QStringList AutoTypePlatformMac::windowTitles() +{ + pid_t pid; + return getTargetWindowInfo(&pid, NULL); +} + + +WId AutoTypePlatformMac::activeWindow() +{ + WId mid; + pid_t pid; + getTargetWindowInfo(&pid, &mid); + AutoTypePlatformMac::processToFront(pid); + if (first) + first = false; + + return mid; +} + + +QString AutoTypePlatformMac::activeWindowTitle() +{ + QStringList sl = getTargetWindowInfo(NULL, NULL); + return sl[0]; +} + + +bool AutoTypePlatformMac::registerGlobalShortcut(Qt::Key key, + Qt::KeyboardModifiers modifiers) +{ + if (key == 0) + return false; + + //KeycodeWithMods kc = keyToKeycodeWithMods(key); + KeycodeWithMods kc = AutoTypePlatformMac::keysymToKeycodeWithMods2(static_cast(key)); + int code = kc.keycode; + uint mod = qtToNativeModifiers(modifiers); + + if (code==globalKey && mod==globalMod) + return true; + + // need to unregister old before registering new + unregisterGlobalShortcut(key, modifiers); + OSStatus status = RegisterEventHotKey(code, mod, hotKeyID, + GetApplicationEventTarget(), 0, &hotKeyRef); + + if (noErr == status) { + globalKey = code; + globalMod = mod; + return true; + } else { + qWarning("Error registering global shortcut: %d", status); + RegisterEventHotKey(globalKey, globalMod, hotKeyID, + GetApplicationEventTarget(), 0, &hotKeyRef); + return false; + } +} + + +void AutoTypePlatformMac::unregisterGlobalShortcut(Qt::Key, + Qt::KeyboardModifiers) +{ + globalKey = 0; + globalMod = 0; + UnregisterEventHotKey(hotKeyRef); +} + + +int AutoTypePlatformMac::platformEventFilter(void* event) +{ + return -1; +} + + +int AutoTypePlatformMac::initialTimeout() +{ + first = true; + return 500; +} + + +AutoTypeExecutor* AutoTypePlatformMac::createExecutor() +{ + return new AutoTypeExecturorMac(this); +} + + +void AutoTypePlatformMac::sendUnicode(KeySym keysym) +{ + if (onlySendKeycodes) { + KeycodeWithMods keycodeWithMods = + AutoTypePlatformMac::keysymToKeycodeWithMods2(keysym); + if (NoKeycode == keycodeWithMods.keycode) return; + sendKeycode(keycodeWithMods); + return; + } + unicodeBuffer[unicodePtr++] = keysym; + if (UNICODE_BUFFER_SIZE == unicodePtr) flushUnicode(); +} + + +void AutoTypePlatformMac::sendKeycode(KeycodeWithMods keycodeWithMods) +{ + flushUnicode(); + uint keycode = keycodeWithMods.keycode; + uint mods = keycodeWithMods.mods << 8; + uint flags = 0; + if (0 != ( shiftKey & mods)) flags |= kCGEventFlagMaskShift; + if (0 != (controlKey & mods)) flags |= kCGEventFlagMaskControl; + if (0 != ( optionKey & mods)) flags |= kCGEventFlagMaskAlternate; + if (0 != ( cmdKey & mods)) flags |= kCGEventFlagMaskCommand; + CGEventSetIntegerValueField(keyEvent, kCGKeyboardEventKeycode, keycode); + CGEventSetFlags(AutoTypePlatformMac::keyEvent, flags); + keyDownUp(AutoTypePlatformMac::keyEvent); + sleepKeyStrokeDelay(); +} + + +KeycodeWithMods AutoTypePlatformMac::keyToKeycodeWithMods(Qt::Key key) +{ + KeycodeWithMods kc; + kc.mods = 0; + + switch (key) { + case Qt::Key_Tab: + kc.keycode = kVK_Tab; + break; + case Qt::Key_Enter: + kc.keycode = kVK_Return; + break; + case Qt::Key_Up: + kc.keycode = kVK_UpArrow; + break; + case Qt::Key_Down: + kc.keycode = kVK_DownArrow; + break; + case Qt::Key_Left: + kc.keycode = kVK_LeftArrow; + break; + case Qt::Key_Right: + kc.keycode = kVK_RightArrow; + break; + case Qt::Key_Insert: + kc.keycode = kVK_Help; + break; + case Qt::Key_Delete: + kc.keycode = kVK_ForwardDelete; + break; + case Qt::Key_Home: + kc.keycode = kVK_Home; + break; + case Qt::Key_End: + kc.keycode = kVK_End; + break; + case Qt::Key_PageUp: + kc.keycode = kVK_PageUp; + break; + case Qt::Key_PageDown: + kc.keycode = kVK_PageDown; + break; + case Qt::Key_Backspace: + kc.keycode = kVK_Delete; + break; + //case Qt::Key_Pause: + // return XK_Break; + case Qt::Key_CapsLock: + kc.keycode = kVK_CapsLock; + break; + case Qt::Key_Escape: + kc.keycode = kVK_Escape; + break; + case Qt::Key_Help: + kc.keycode = kVK_Help; + break; + case Qt::Key_NumLock: + kc.keycode = kVK_ANSI_KeypadClear; + break; + case Qt::Key_Print: + kc.keycode = kVK_F13; + break; + //case Qt::Key_ScrollLock: + // return XK_Scroll_Lock; + default: + if (key >= Qt::Key_F1 && key <= Qt::Key_F16) + kc.keycode = kVK_F1 + key - Qt::Key_F1; + else + kc.keycode = NoSymbol; + } + + return kc; +} + +// Private + +QStringList AutoTypePlatformMac::getTargetWindowInfo(pid_t *pidPtr, + WId *windowNumberPtr) +{ + QStringList titles; + + const int maxWindowNameSize = 512; + char windowName[maxWindowNameSize]; + + onlySendKeycodes = false; + + CFArrayRef windowInfo = CGWindowListCopyWindowInfo( + kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, + 0); + CFIndex windowCount = CFArrayGetCount(windowInfo); + + for (CFIndex i = 0; i < windowCount; i++) { + CFDictionaryRef window = static_cast + (CFArrayGetValueAtIndex(windowInfo, i)); + + // only want windows in layer 0 + CFNumberRef windowLayerRef = static_cast + (CFDictionaryGetValue(window, kCGWindowLayer)); + + int windowLayer = -1; + CFNumberGetValue(windowLayerRef, kCFNumberIntType, &windowLayer); + if (0 != windowLayer) continue; + + // get the pid owning this window + CFNumberRef pidRef = static_cast + (CFDictionaryGetValue(window, kCGWindowOwnerPID)); + pid_t pid = -1; + CFNumberGetValue(pidRef, kCFNumberIntType, &pid); + + // skip KeePassX windows + if (getKeepassxPID() == pid) continue; + + // get window name; continue if no name + CFStringRef windowNameRef = static_cast + (CFDictionaryGetValue(window, kCGWindowName)); + if (!windowNameRef) continue; + + windowName[0] = 0; + if (!CFStringGetCString(windowNameRef, windowName, + maxWindowNameSize, kCFStringEncodingUTF8) || + (0 == windowName[0])) continue; + + if (NULL != pidPtr) + *pidPtr = pid; + + if (NULL != windowNumberPtr) { + CGWindowID wid; + CFNumberRef windowNumberRef = static_cast + (CFDictionaryGetValue(window, kCGWindowNumber)); + CFNumberGetValue(windowNumberRef, kCGWindowIDCFNumberType, &wid); + *windowNumberPtr = wid; + return titles; + } + titles.append(QString(windowName)); + } + + return titles; +} + + +uint AutoTypePlatformMac::qtToNativeModifiers(Qt::KeyboardModifiers modifiers) +{ + uint nativeModifiers = 0; + + if (modifiers & Qt::ShiftModifier) { + nativeModifiers |= shiftKey; + } + if (modifiers & Qt::ControlModifier) { + nativeModifiers |= controlKey; + } + if (modifiers & Qt::AltModifier) { + nativeModifiers |= optionKey; + } + if (modifiers & Qt::MetaModifier) { + nativeModifiers |= cmdKey; + } + + return nativeModifiers; +} + + +void AutoTypePlatformMac::flushUnicode() +{ + if (0 == unicodePtr) return; + CGEventKeyboardSetUnicodeString(unicodeEvent, unicodePtr, unicodeBuffer); + keyDownUp(unicodeEvent); + unicodePtr = 0; + sleepKeyStrokeDelay(); +} + + +void AutoTypePlatformMac::keyDownUp(CGEventRef theEvent) +{ + // posting Key Down/Up events also annoyingly sets mouse location so mus + // current mouse location and set it in the event + CGEventRef eventLocation = CGEventCreate(NULL); + CGEventSetLocation(theEvent, CGEventGetLocation(eventLocation)); + CFRelease(eventLocation); + + CGEventSetType(theEvent, kCGEventKeyDown); + CGEventPost(kCGHIDEventTap, theEvent); + CGEventSetType(theEvent, kCGEventKeyUp); + CGEventPost(kCGHIDEventTap, theEvent); +} + + +void AutoTypePlatformMac::sleepTime(int msec) +{ + if (msec == 0) return; + + timespec timeOut, remains; + timeOut.tv_sec = msec/1000; + timeOut.tv_nsec = (msec%1000)*1000000; + nanosleep(&timeOut, &remains); +} + + +OSStatus AutoTypePlatformMac::hotKeyHandler(EventHandlerCallRef, EventRef, + void *userData) +{ + // ignore nextHandler - should not be called + if ((inHotKeyEvent) || + AutoTypePlatformMac::isFrontProcess(AutoTypePlatformMac::getKeepassxPID2())) return noErr; + + AutoTypePlatformMac::inHotKeyEvent = true; + Q_EMIT static_cast(userData)->globalShortcutTriggered(); + AutoTypePlatformMac::inHotKeyEvent = false; + return noErr; +} + + +pid_t AutoTypePlatformMac::getKeepassxPID() { + + if (0 == keepassxPID) { + ProcessSerialNumber processSerialNumber; + GetCurrentProcess(&processSerialNumber); + GetProcessPID(&processSerialNumber, &keepassxPID); + } + + return keepassxPID; +} + + +Boolean AutoTypePlatformMac::isFrontProcess(pid_t pid) +{ + Boolean result; + ProcessSerialNumber pidPSN; + ProcessSerialNumber frontPSN; + OSStatus status = GetProcessForPID(pid, &pidPSN); + if (noErr != status) { + qWarning("AutoTypePlatformMac::isFrontProcess: GetProcessForPID " + "error for pid %d: %d", pid, status); + return false; + } + GetFrontProcess(&frontPSN); + SameProcess(&pidPSN, &frontPSN, &result); + return result; +} + + +void AutoTypePlatformMac::initUnicodeToKeycodeWithModsMap() +{ + unicodeToKeycodeWithModsMap.clear(); + TISInputSourceRef inputSourceRef = TISCopyCurrentKeyboardInputSource(); + + if (NULL == inputSourceRef) { + qWarning("AutoTypePlatformMac::initUnicodeToKeycodeWithModsMap: " + "inputSourceRef is NULL"); + return; + } + + CFDataRef unicodeKeyLayoutDataRef = static_cast + (TISGetInputSourceProperty(inputSourceRef, + kTISPropertyUnicodeKeyLayoutData)); + + if (NULL == unicodeKeyLayoutDataRef) { + qWarning("AutoTypePlatformMac::initUnicodeToKeycodeWithModsMap: " + "unicodeKeyLayoutDataRef is NULL"); + return; + } + const UCKeyboardLayout *unicodeKeyLayoutDataPtr = + reinterpret_cast + (CFDataGetBytePtr(unicodeKeyLayoutDataRef)); + + UInt32 deadKeyState; + UniChar unicodeString[8]; + UniCharCount len; + for (int m = 0; m < 16; m++) { + uint mods = orderedModifiers[m]; + for (uint keycode = 0; keycode < 0x80; keycode++) { + deadKeyState = 0; + len = 0; + OSStatus status = UCKeyTranslate(unicodeKeyLayoutDataPtr, keycode, + kUCKeyActionDown, mods, LMGetKbdType(), + kUCKeyTranslateNoDeadKeysMask, &deadKeyState, + sizeof(unicodeString), &len, unicodeString); + + if (noErr != status) { + qWarning("AutoTypePlatformMac::initUnicodeToKeycodeWithModsMap: " + "UCKeyTranslate error: %d keycode 0x%02X modifiers 0x%02X", + status, keycode, mods); + continue; + } + + // store if only one char and not already in store + if ((1 != len) || + (0 < unicodeToKeycodeWithModsMap.count(unicodeString[0]))) + continue; + + KeycodeWithMods kc; + kc.keycode = keycode; + kc.mods = mods; + unicodeToKeycodeWithModsMap[unicodeString[0]] = kc; + } + } +} + + +void AutoTypePlatformMac::processToFront(pid_t pid) +{ + OSStatus status; + ProcessSerialNumber processSerialNumber; + status = GetProcessForPID(pid, &processSerialNumber); + + if (noErr != status) { + qWarning("AutoTypePlatformMac::processToFront: GetProcessForPID " + "error for pid %d: %d", pid, status); + return; + } + + status = SetFrontProcessWithOptions(&processSerialNumber, + kSetFrontProcessFrontWindowOnly); + + if (noErr != status) { + qWarning("AutoTypePlatformMac::processToFront: " + "SetFrontProcessWithOptions for pid %d: %d", pid, status); + return; + } +} + + +KeycodeWithMods AutoTypePlatformMac::keysymToKeycodeWithMods2(KeySym keysym) +{ + return 0 == unicodeToKeycodeWithModsMap.count(keysym) + ? NoKeycodeWithMods : unicodeToKeycodeWithModsMap[keysym]; +} + + +pid_t AutoTypePlatformMac::getKeepassxPID2() +{ + if (0 == keepassxPID2) { + ProcessSerialNumber processSerialNumber; + GetCurrentProcess(&processSerialNumber); + GetProcessPID(&processSerialNumber, &keepassxPID2); + } + return keepassxPID2; +} + + +AutoTypeExecturorMac::AutoTypeExecturorMac(AutoTypePlatformMac* platform) + : m_platform(platform) +{ +} + + +void AutoTypeExecturorMac::execChar(AutoTypeChar* action) +{ + m_platform->sendUnicode(action->character.unicode()); +} + + +void AutoTypeExecturorMac::execKey(AutoTypeKey* action) +{ + m_platform->sendKeycode(m_platform->keyToKeycodeWithMods(action->key)); +} + + +Q_EXPORT_PLUGIN2(keepassx-autotype-mac, AutoTypePlatformMac) diff --git a/src/autotype/mac/AutoTypeMac.h b/src/autotype/mac/AutoTypeMac.h new file mode 100644 index 000000000..e43d33cf0 --- /dev/null +++ b/src/autotype/mac/AutoTypeMac.h @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2009-2010 Jeff Gibbons + * Copyright (C) 2005-2008 Tarek Saidi + * Copyright (C) 2012 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KEEPASSX_AUTOTYPEMAC_H +#define KEEPASSX_AUTOTYPEMAC_H + +#include +#include + +#include "autotype/AutoTypePlatformPlugin.h" +#include "autotype/AutoTypeAction.h" +#include "core/Global.h" + +typedef quint32 KeySym; +#define NoSymbol static_cast(0) +#define NoKeycode static_cast(-1) + +struct KeycodeWithMods { + uint16 keycode; + uint16 mods; +}; + +class AutoTypePlatformMac : public QObject, public AutoTypePlatformInterface +{ + Q_OBJECT + Q_INTERFACES(AutoTypePlatformInterface) + +public: + AutoTypePlatformMac(); + + QStringList windowTitles(); + WId activeWindow(); + QString activeWindowTitle(); + bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers); + void unregisterGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers); + int platformEventFilter(void* event); + int initialTimeout(); + AutoTypeExecutor* createExecutor(); + + void sendUnicode(KeySym keysym); + void sendKeycode(KeycodeWithMods keycodeWithMods); + KeycodeWithMods keyToKeycodeWithMods(Qt::Key key); + +Q_SIGNALS: + void globalShortcutTriggered(); + +private: + QStringList getTargetWindowInfo(pid_t *pidPtr, WId *windowNumberPtr); + uint qtToNativeModifiers(Qt::KeyboardModifiers modifiers); + void flushUnicode(); + void keyDownUp(CGEventRef theEvent); + void sleepTime(int msec); + inline void sleepKeyStrokeDelay(){ sleepTime(5); }; + static OSStatus hotKeyHandler(EventHandlerCallRef, + EventRef, void *userData); + pid_t getKeepassxPID(); + + bool first; + bool onlySendKeycodes; + bool inGlobalAutoType; + int globalKey; + uint globalMod; + pid_t keepassxPID; + EventHotKeyRef hotKeyRef; + EventHotKeyID hotKeyID; + + static bool inHotKeyEvent; + static CGEventRef keyEvent; + +private: + static void initUnicodeToKeycodeWithModsMap(); + static void processToFront(pid_t pid); + static KeycodeWithMods keysymToKeycodeWithMods2(KeySym keysym); + static Boolean isFrontProcess(pid_t pid); + static pid_t getKeepassxPID2(); +}; + + +class AutoTypeExecturorMac : public AutoTypeExecutor +{ +public: + explicit AutoTypeExecturorMac(AutoTypePlatformMac* platform); + + void execChar(AutoTypeChar* action); + void execKey(AutoTypeKey* action); + +private: + AutoTypePlatformMac* const m_platform; +}; + +#endif // KEEPASSX_AUTOTYPEMAC_H diff --git a/src/autotype/mac/CMakeLists.txt b/src/autotype/mac/CMakeLists.txt new file mode 100644 index 000000000..c85e40e17 --- /dev/null +++ b/src/autotype/mac/CMakeLists.txt @@ -0,0 +1,18 @@ +set(autotype_mac_SOURCES + AutoTypeMac.cpp +) + +set(autotype_mac_MOC + AutoTypeMac.h +) + +qt4_wrap_cpp(autotype_mac_SOURCES ${autotype_mac_MOC}) + +add_library(keepassx-autotype-mac MODULE ${autotype_mac_SOURCES}) +if(APPLE) + set_target_properties(keepassx-autotype-mac PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit -framework Carbon") +endif() +target_link_libraries(keepassx-autotype-mac testautotype ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY}) +install(TARGETS keepassx-autotype-mac + BUNDLE DESTINATION . COMPONENT Runtime + LIBRARY DESTINATION ${PLUGIN_INSTALL_DIR} COMPONENT Runtime) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index c48bc9553..418c5e760 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -95,6 +95,7 @@ void Config::init(const QString& fileName) m_defaults.insert("ShowToolbar", true); m_defaults.insert("MinimizeOnCopy", false); m_defaults.insert("UseGroupIconOnEntryCreation", false); + m_defaults.insert("ReloadBehavior", 0 /*always ask*/); m_defaults.insert("security/clearclipboard", true); m_defaults.insert("security/clearclipboardtimeout", 10); m_defaults.insert("security/lockdatabaseidle", false); diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 0394051fc..14adc6424 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -228,6 +228,11 @@ bool Database::verifyKey(const CompositeKey& key) const return (m_data.key.rawKey() == key.rawKey()); } +CompositeKey Database::key() const +{ + return m_data.key; +} + void Database::createRecycleBin() { Group* recycleBin = Group::createRecycleBin(); diff --git a/src/core/Database.h b/src/core/Database.h index 0ee9a9659..28df70f6c 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -99,6 +99,7 @@ public: void setKey(const CompositeKey& key); bool hasKey() const; bool verifyKey(const CompositeKey& key) const; + CompositeKey key() const; void recycleEntry(Entry* entry); void recycleGroup(Group* group); void setEmitModified(bool value); diff --git a/src/core/EntryAttributes.cpp b/src/core/EntryAttributes.cpp index a950390cd..08fcf42f5 100644 --- a/src/core/EntryAttributes.cpp +++ b/src/core/EntryAttributes.cpp @@ -52,6 +52,11 @@ QString EntryAttributes::value(const QString& key) const return m_attributes.value(key); } +bool EntryAttributes::contains(const QString &key) const +{ + return m_attributes.contains(key); +} + bool EntryAttributes::isProtected(const QString& key) const { return m_protectedAttributes.contains(key); diff --git a/src/core/EntryAttributes.h b/src/core/EntryAttributes.h index 0eba4332c..2decc8a32 100644 --- a/src/core/EntryAttributes.h +++ b/src/core/EntryAttributes.h @@ -34,6 +34,7 @@ public: QList keys() const; QList customKeys(); QString value(const QString& key) const; + bool contains(const QString& key) const; bool isProtected(const QString& key) const; void set(const QString& key, const QString& value, bool protect = false); void remove(const QString& key); diff --git a/src/core/PasswordGenerator.cpp b/src/core/PasswordGenerator.cpp index 5915ba898..52f124786 100644 --- a/src/core/PasswordGenerator.cpp +++ b/src/core/PasswordGenerator.cpp @@ -89,6 +89,22 @@ QString PasswordGenerator::generatePassword() const return password; } +int PasswordGenerator::getbits() const +{ + QVector groups = passwordGroups(); + + int bits = 0; + QVector passwordChars; + Q_FOREACH (const PasswordGroup& group, groups) { + bits += group.size(); + } + + bits *= m_length; + + return bits; +} + + bool PasswordGenerator::isValid() const { if (m_classes == 0) { diff --git a/src/core/PasswordGenerator.h b/src/core/PasswordGenerator.h index 6a9d21245..54982ac9c 100644 --- a/src/core/PasswordGenerator.h +++ b/src/core/PasswordGenerator.h @@ -55,6 +55,7 @@ public: bool isValid() const; QString generatePassword() const; + int getbits() const; private: QVector passwordGroups() const; diff --git a/src/core/Uuid.cpp b/src/core/Uuid.cpp index 700f59641..a7375845a 100644 --- a/src/core/Uuid.cpp +++ b/src/core/Uuid.cpp @@ -89,6 +89,12 @@ Uuid Uuid::fromBase64(const QString& str) return Uuid(data); } +Uuid Uuid::fromHex(const QString& str) +{ + QByteArray data = QByteArray::fromHex(str.toAscii()); + return Uuid(data); +} + uint qHash(const Uuid& key) { return qHash(key.toByteArray()); diff --git a/src/core/Uuid.h b/src/core/Uuid.h index 8c39a7cb6..4164399d6 100644 --- a/src/core/Uuid.h +++ b/src/core/Uuid.h @@ -37,6 +37,7 @@ public: bool operator!=(const Uuid& other) const; static const int Length; static Uuid fromBase64(const QString& str); + static Uuid fromHex(const QString& str); private: QByteArray m_data; diff --git a/src/crypto/SymmetricCipher.cpp b/src/crypto/SymmetricCipher.cpp index 5f9222653..becdb8221 100644 --- a/src/crypto/SymmetricCipher.cpp +++ b/src/crypto/SymmetricCipher.cpp @@ -63,4 +63,4 @@ void SymmetricCipher::reset() int SymmetricCipher::blockSize() const { return m_backend->blockSize(); -} +} \ No newline at end of file diff --git a/src/crypto/SymmetricCipher.h b/src/crypto/SymmetricCipher.h index d036e532e..6c68013cf 100644 --- a/src/crypto/SymmetricCipher.h +++ b/src/crypto/SymmetricCipher.h @@ -22,6 +22,7 @@ #include #include "core/Global.h" +#include "crypto/SymmetricCipher.h" #include "crypto/SymmetricCipherBackend.h" class SymmetricCipher @@ -47,31 +48,42 @@ public: Encrypt }; + SymmetricCipher(); SymmetricCipher(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode, - SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv); + SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv = QByteArray()); ~SymmetricCipher(); + void init(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode, SymmetricCipher::Direction direction, + const QByteArray &key, const QByteArray &iv = QByteArray()); + bool isValid() const { + return m_backend != 0; + } + inline QByteArray process(const QByteArray& data) { + Q_ASSERT(m_backend); return m_backend->process(data); } inline void processInPlace(QByteArray& data) { + Q_ASSERT(m_backend); m_backend->processInPlace(data); } inline void processInPlace(QByteArray& data, quint64 rounds) { Q_ASSERT(rounds > 0); + Q_ASSERT(m_backend); m_backend->processInPlace(data, rounds); } void reset(); + void setIv(const QByteArray& iv); int blockSize() const; private: static SymmetricCipherBackend* createBackend(SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode, SymmetricCipher::Direction direction); - const QScopedPointer m_backend; + QScopedPointer m_backend; Q_DISABLE_COPY(SymmetricCipher) }; diff --git a/src/gui/DatabaseOpenWidget.cpp b/src/gui/DatabaseOpenWidget.cpp index 973192523..8d9610d96 100644 --- a/src/gui/DatabaseOpenWidget.cpp +++ b/src/gui/DatabaseOpenWidget.cpp @@ -93,14 +93,26 @@ void DatabaseOpenWidget::enterKey(const QString& pw, const QString& keyFile) openDatabase(); } +void DatabaseOpenWidget::enterKey(const CompositeKey& masterKey) +{ + if (masterKey.isEmpty()) { + return; + } + openDatabase(masterKey); +} + void DatabaseOpenWidget::openDatabase() { - KeePass2Reader reader; CompositeKey masterKey = databaseKey(); if (masterKey.isEmpty()) { return; } + openDatabase(masterKey); +} +void DatabaseOpenWidget::openDatabase(const CompositeKey& masterKey) +{ + KeePass2Reader reader; QFile file(m_filename); if (!file.open(QIODevice::ReadOnly)) { // TODO: error message diff --git a/src/gui/DatabaseOpenWidget.h b/src/gui/DatabaseOpenWidget.h index ad40c5711..9c4b87472 100644 --- a/src/gui/DatabaseOpenWidget.h +++ b/src/gui/DatabaseOpenWidget.h @@ -39,6 +39,7 @@ public: ~DatabaseOpenWidget(); void load(const QString& filename); void enterKey(const QString& pw, const QString& keyFile); + void enterKey(const CompositeKey& masterKey); Database* database(); Q_SIGNALS: @@ -49,6 +50,7 @@ protected: protected Q_SLOTS: virtual void openDatabase(); + void openDatabase(const CompositeKey& masterKey); void reject(); private Q_SLOTS: diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 8c2ba06d9..4effe5f4d 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -19,6 +19,9 @@ #include #include +#include +#include +#include #include "autotype/AutoType.h" #include "core/Config.h" @@ -45,7 +48,8 @@ DatabaseManagerStruct::DatabaseManagerStruct() const int DatabaseTabWidget::LastDatabasesCount = 5; DatabaseTabWidget::DatabaseTabWidget(QWidget* parent) - : QTabWidget(parent) + : QTabWidget(parent), + m_fileWatcher(new QFileSystemWatcher(this)) { DragTabBar* tabBar = new DragTabBar(this); tabBar->setDrawBase(false); @@ -53,6 +57,7 @@ DatabaseTabWidget::DatabaseTabWidget(QWidget* parent) connect(this, SIGNAL(tabCloseRequested(int)), SLOT(closeDatabase(int))); connect(autoType(), SIGNAL(globalShortcutTriggered()), SLOT(performGlobalAutoType())); + connect(m_fileWatcher, SIGNAL(fileChanged(QString)), SLOT(fileChanged(QString))); } DatabaseTabWidget::~DatabaseTabWidget() @@ -66,16 +71,7 @@ DatabaseTabWidget::~DatabaseTabWidget() void DatabaseTabWidget::toggleTabbar() { - if (count() > 1) { - if (!tabBar()->isVisible()) { - tabBar()->show(); - } - } - else { - if (tabBar()->isVisible()) { - tabBar()->hide(); - } - } + tabBar()->setVisible(count() > 1); } void DatabaseTabWidget::newDatabase() @@ -101,7 +97,7 @@ void DatabaseTabWidget::openDatabase() } void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, - const QString& keyFile) + const QString& keyFile, const CompositeKey& key, int index) { QFileInfo fileInfo(fileName); QString canonicalFilePath = fileInfo.canonicalFilePath(); @@ -145,12 +141,17 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, dbStruct.filePath = fileInfo.absoluteFilePath(); dbStruct.canonicalFilePath = canonicalFilePath; dbStruct.fileName = fileInfo.fileName(); + dbStruct.lastModified = fileInfo.lastModified(); - insertDatabase(db, dbStruct); + insertDatabase(db, dbStruct, index); + m_fileWatcher->addPath(dbStruct.filePath); - updateLastDatabases(dbStruct.filePath); + updateRecentDatabases(dbStruct.filePath); - if (!pw.isNull() || !keyFile.isEmpty()) { + if (!key.isEmpty()) { + dbStruct.dbWidget->switchToOpenDatabase(dbStruct.filePath, key); + } + else if (!pw.isNull() || !keyFile.isEmpty()) { dbStruct.dbWidget->switchToOpenDatabase(dbStruct.filePath, pw, keyFile); } else { @@ -177,6 +178,121 @@ void DatabaseTabWidget::importKeePass1Database() dbStruct.dbWidget->switchToImportKeepass1(fileName); } +void DatabaseTabWidget::fileChanged(const QString &fileName) +{ + const bool wasEmpty = m_changedFiles.isEmpty(); + m_changedFiles.insert(fileName); + bool found = false; + Q_FOREACH (QString f, m_fileWatcher->files()) { + if (f == fileName) { + found = true; + break; + } + } + if (!found) m_fileWatcher->addPath(fileName); + if (wasEmpty && !m_changedFiles.isEmpty()) + QTimer::singleShot(200, this, SLOT(checkReloadDatabases())); +} + +void DatabaseTabWidget::expectFileChange(const DatabaseManagerStruct& dbStruct) +{ + if (dbStruct.filePath.isEmpty()) + return; + m_expectedFileChanges.insert(dbStruct.filePath); +} + +void DatabaseTabWidget::unexpectFileChange(DatabaseManagerStruct& dbStruct) +{ + if (dbStruct.filePath.isEmpty()) + return; + m_expectedFileChanges.remove(dbStruct.filePath); + dbStruct.lastModified = QFileInfo(dbStruct.filePath).lastModified(); +} + +void DatabaseTabWidget::checkReloadDatabases() +{ + QSet changedFiles; + + changedFiles = m_changedFiles.subtract(m_expectedFileChanges); + m_changedFiles.clear(); + + if (changedFiles.isEmpty()) + return; + + Q_FOREACH (DatabaseManagerStruct dbStruct, m_dbList) { + QString filePath = dbStruct.filePath; + Database * db = dbStruct.dbWidget->database(); + + if (!changedFiles.contains(filePath)) + continue; + + QFileInfo fi(filePath); + QDateTime lastModified = fi.lastModified(); + if (dbStruct.lastModified == lastModified) + continue; + + DatabaseWidget::Mode mode = dbStruct.dbWidget->currentMode(); + if (mode == DatabaseWidget::None || mode == DatabaseWidget::LockedMode || !db->hasKey()) + continue; + + ReloadBehavior reloadBehavior = ReloadBehavior(config()->get("ReloadBehavior").toInt()); + if ( (reloadBehavior == AlwaysAsk) + || (reloadBehavior == ReloadUnmodified && (mode == DatabaseWidget::EditMode + || mode == DatabaseWidget::OpenMode)) + || (reloadBehavior == ReloadUnmodified && dbStruct.modified)) { + int res = QMessageBox::warning(this, fi.exists() ? tr("Database file changed") : tr("Database file removed"), + tr("Do you want to discard your changes and reload?"), + QMessageBox::Yes|QMessageBox::No); + if (res == QMessageBox::No) + continue; + } + + if (fi.exists()) { + //Ignore/cancel all edits + dbStruct.dbWidget->switchToView(false); + dbStruct.modified = false; + + //Save current group/entry + Uuid currentGroup; + if (Group* group = dbStruct.dbWidget->currentGroup()) + currentGroup = group->uuid(); + Uuid currentEntry; + if (Entry* entry = dbStruct.dbWidget->entryView()->currentEntry()) + currentEntry = entry->uuid(); + QString searchText = dbStruct.dbWidget->searchText(); + bool caseSensitive = dbStruct.dbWidget->caseSensitiveSearch(); + bool allGroups = dbStruct.dbWidget->isAllGroupsSearch(); + + //Reload updated db + CompositeKey key = db->key(); + int tabPos = databaseIndex(db); + closeDatabase(db); + openDatabase(filePath, QString(), QString(), key, tabPos); + + //Restore current group/entry + dbStruct = indexDatabaseManagerStruct(count() - 1); + if (dbStruct.dbWidget && dbStruct.dbWidget->currentMode() == DatabaseWidget::ViewMode) { + Database * db = dbStruct.dbWidget->database(); + if (!currentGroup.isNull()) + if (Group* group = db->resolveGroup(currentGroup)) + dbStruct.dbWidget->groupView()->setCurrentGroup(group); + if (!searchText.isEmpty()) + dbStruct.dbWidget->search(searchText, caseSensitive, allGroups); + if (!currentEntry.isNull()) + if (Entry* entry = db->resolveEntry(currentEntry)) + dbStruct.dbWidget->entryView()->setCurrentEntry(entry); + } + } else { + //Ignore/cancel all edits + dbStruct.dbWidget->switchToView(false); + dbStruct.modified = false; + + //Close database + closeDatabase(dbStruct.dbWidget->database()); + } + } +} + bool DatabaseTabWidget::closeDatabase(Database* db) { Q_ASSERT(db); @@ -189,7 +305,8 @@ bool DatabaseTabWidget::closeDatabase(Database* db) if (dbName.right(1) == "*") { dbName.chop(1); } - if (dbStruct.dbWidget->currentMode() == DatabaseWidget::EditMode && db->hasKey()) { + if ((dbStruct.dbWidget->currentMode() == DatabaseWidget::EditMode || + dbStruct.dbWidget->currentMode() == DatabaseWidget::OpenMode) && db->hasKey()) { QMessageBox::StandardButton result = MessageBox::question( this, tr("Close?"), @@ -231,6 +348,7 @@ void DatabaseTabWidget::deleteDatabase(Database* db) int index = databaseIndex(db); + m_fileWatcher->removePath(dbStruct.filePath); removeTab(index); toggleTabbar(); m_dbList.remove(db); @@ -244,6 +362,13 @@ void DatabaseTabWidget::deleteDatabase(Database* db) bool DatabaseTabWidget::closeAllDatabases() { + QStringList reloadDatabases; + if (config()->get("AutoReopenLastDatabases", false).toBool()) { + for (int i = 0; i < count(); i ++) + reloadDatabases << indexDatabaseManagerStruct(i).filePath; + } + config()->set("LastOpenDatabases", reloadDatabases); + while (!m_dbList.isEmpty()) { if (!closeDatabase()) { return false; @@ -259,12 +384,16 @@ void DatabaseTabWidget::saveDatabase(Database* db) if (dbStruct.saveToFilename) { bool result = false; + expectFileChange(dbStruct); + QSaveFile saveFile(dbStruct.filePath); if (saveFile.open(QIODevice::WriteOnly)) { m_writer.writeDatabase(&saveFile, db); result = saveFile.commit(); } + unexpectFileChange(dbStruct); + if (result) { dbStruct.modified = false; updateTabName(db); @@ -282,12 +411,12 @@ void DatabaseTabWidget::saveDatabase(Database* db) void DatabaseTabWidget::saveDatabaseAs(Database* db) { DatabaseManagerStruct& dbStruct = m_dbList[db]; - QString oldFileName; + QString oldFilePath; if (dbStruct.saveToFilename) { - oldFileName = dbStruct.filePath; + oldFilePath = dbStruct.filePath; } QString fileName = fileDialog()->getSaveFileName(this, tr("Save database as"), - oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)")); + oldFilePath, tr("KeePass 2 Database").append(" (*.kdbx)")); if (!fileName.isEmpty()) { bool result = false; @@ -298,15 +427,18 @@ void DatabaseTabWidget::saveDatabaseAs(Database* db) } if (result) { + m_fileWatcher->removePath(oldFilePath); dbStruct.modified = false; dbStruct.saveToFilename = true; QFileInfo fileInfo(fileName); dbStruct.filePath = fileInfo.absoluteFilePath(); dbStruct.canonicalFilePath = fileInfo.canonicalFilePath(); dbStruct.fileName = fileInfo.fileName(); + dbStruct.lastModified = fileInfo.lastModified(); dbStruct.dbWidget->updateFilename(dbStruct.filePath); updateTabName(db); - updateLastDatabases(dbStruct.filePath); + updateRecentDatabases(dbStruct.filePath); + m_fileWatcher->addPath(dbStruct.filePath); } else { MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" @@ -477,14 +609,13 @@ Database* DatabaseTabWidget::databaseFromDatabaseWidget(DatabaseWidget* dbWidget return Q_NULLPTR; } -void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct) +void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct, int index) { m_dbList.insert(db, dbStruct); - addTab(dbStruct.dbWidget, ""); + index = insertTab(index, dbStruct.dbWidget, ""); toggleTabbar(); updateTabName(db); - int index = databaseIndex(db); setCurrentIndex(index); connectDatabase(db); connect(dbStruct.dbWidget, SIGNAL(closeRequest()), SLOT(closeDatabaseFromSender())); @@ -510,7 +641,9 @@ bool DatabaseTabWidget::hasLockableDatabases() i.next(); DatabaseWidget::Mode mode = i.value().dbWidget->currentMode(); - if ((mode == DatabaseWidget::ViewMode || mode == DatabaseWidget::EditMode) + if ((mode == DatabaseWidget::ViewMode || + mode == DatabaseWidget::EditMode || + mode == DatabaseWidget::OpenMode) && i.value().dbWidget->dbHasKey()) { return true; } @@ -526,7 +659,9 @@ void DatabaseTabWidget::lockDatabases() i.next(); DatabaseWidget::Mode mode = i.value().dbWidget->currentMode(); - if ((mode == DatabaseWidget::ViewMode || mode == DatabaseWidget::EditMode) + if ((mode == DatabaseWidget::ViewMode || + mode == DatabaseWidget::EditMode || + mode == DatabaseWidget::OpenMode) && i.value().dbWidget->dbHasKey()) { i.value().dbWidget->lock(); updateTabName(i.key()); @@ -552,7 +687,7 @@ void DatabaseTabWidget::modified() } } -void DatabaseTabWidget::updateLastDatabases(const QString& filename) +void DatabaseTabWidget::updateRecentDatabases(const QString& filename) { if (!config()->get("RememberLastDatabases").toBool()) { config()->set("LastDatabases", QVariant()); diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 9261a065c..8fda1094e 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -18,8 +18,10 @@ #ifndef KEEPASSX_DATABASETABWIDGET_H #define KEEPASSX_DATABASETABWIDGET_H +#include #include #include +#include #include "format/KeePass2Writer.h" #include "gui/DatabaseWidget.h" @@ -27,6 +29,7 @@ class DatabaseWidget; class DatabaseOpenWidget; class QFile; +class QFileSystemWatcher; struct DatabaseManagerStruct { @@ -39,6 +42,7 @@ struct DatabaseManagerStruct bool saveToFilename; bool modified; bool readOnly; + QDateTime lastModified; }; Q_DECLARE_TYPEINFO(DatabaseManagerStruct, Q_MOVABLE_TYPE); @@ -51,16 +55,25 @@ public: explicit DatabaseTabWidget(QWidget* parent = Q_NULLPTR); ~DatabaseTabWidget(); void openDatabase(const QString& fileName, const QString& pw = QString(), - const QString& keyFile = QString()); + const QString& keyFile = QString(), const CompositeKey& key = CompositeKey(), + int index = -1); DatabaseWidget* currentDatabaseWidget(); bool hasLockableDatabases(); static const int LastDatabasesCount; + enum ReloadBehavior { + AlwaysAsk, + ReloadUnmodified, + IgnoreAll + }; + public Q_SLOTS: void newDatabase(); void openDatabase(); void importKeePass1Database(); + void fileChanged(const QString& fileName); + void checkReloadDatabases(); void saveDatabase(int index = -1); void saveDatabaseAs(int index = -1); bool closeDatabase(int index = -1); @@ -93,12 +106,17 @@ private: Database* indexDatabase(int index); DatabaseManagerStruct indexDatabaseManagerStruct(int index); Database* databaseFromDatabaseWidget(DatabaseWidget* dbWidget); - void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct); - void updateLastDatabases(const QString& filename); + void insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct, int index = -1); + void updateRecentDatabases(const QString& filename); void connectDatabase(Database* newDb, Database* oldDb = Q_NULLPTR); + void expectFileChange(const DatabaseManagerStruct& dbStruct); + void unexpectFileChange(DatabaseManagerStruct& dbStruct); KeePass2Writer m_writer; QHash m_dbList; + QSet m_changedFiles; + QSet m_expectedFileChanges; + QFileSystemWatcher* m_fileWatcher; }; #endif // KEEPASSX_DATABASETABWIDGET_H diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 419028ab8..f2688ff51 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -52,6 +52,8 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) , m_newGroup(Q_NULLPTR) , m_newEntry(Q_NULLPTR) , m_newParent(Q_NULLPTR) + , m_searchAllGroups(false) + , m_searchSensitivity(Qt::CaseInsensitive) { m_searchUi->setupUi(m_searchWidget); @@ -91,8 +93,9 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) closeAction->setIcon(closeIcon); m_searchUi->closeSearchButton->setDefaultAction(closeAction); m_searchUi->closeSearchButton->setShortcut(Qt::Key_Escape); + int iconsize = style()->pixelMetric(QStyle::PM_SmallIconSize); + m_searchUi->closeSearchButton->setIconSize(QSize(iconsize,iconsize)); m_searchWidget->hide(); - m_searchUi->caseSensitiveCheckBox->setVisible(false); QVBoxLayout* vLayout = new QVBoxLayout(rightHandSideWidget); vLayout->setMargin(0); @@ -152,11 +155,7 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) connect(m_keepass1OpenWidget, SIGNAL(editFinished(bool)), SLOT(openDatabase(bool))); connect(m_unlockDatabaseWidget, SIGNAL(editFinished(bool)), SLOT(unlockDatabase(bool))); connect(this, SIGNAL(currentChanged(int)), this, SLOT(emitCurrentModeChanged())); - connect(m_searchUi->searchEdit, SIGNAL(textChanged(QString)), this, SLOT(startSearchTimer())); - connect(m_searchUi->caseSensitiveCheckBox, SIGNAL(toggled(bool)), this, SLOT(startSearch())); - connect(m_searchUi->searchCurrentRadioButton, SIGNAL(toggled(bool)), this, SLOT(startSearch())); - connect(m_searchUi->searchRootRadioButton, SIGNAL(toggled(bool)), this, SLOT(startSearch())); - connect(m_searchUi->searchEdit, SIGNAL(returnPressed()), m_entryView, SLOT(setFocus())); + connect(m_searchUi->searchResults, SIGNAL(linkActivated(QString)), this, SLOT(onLinkActivated(QString))); connect(m_searchTimer, SIGNAL(timeout()), this, SLOT(search())); connect(closeAction, SIGNAL(triggered()), this, SLOT(closeSearch())); @@ -178,6 +177,9 @@ DatabaseWidget::Mode DatabaseWidget::currentMode() else if (currentWidget() == m_unlockDatabaseWidget) { return DatabaseWidget::LockedMode; } + else if (currentWidget() == m_databaseOpenWidget) { + return DatabaseWidget::OpenMode; + } else { return DatabaseWidget::EditMode; } @@ -658,6 +660,13 @@ void DatabaseWidget::switchToOpenDatabase(const QString& fileName, const QString m_databaseOpenWidget->enterKey(password, keyFile); } +void DatabaseWidget::switchToOpenDatabase(const QString &fileName, const CompositeKey& masterKey) +{ + updateFilename(fileName); + switchToOpenDatabase(fileName); + m_databaseOpenWidget->enterKey(masterKey); +} + void DatabaseWidget::switchToImportKeepass1(const QString& fileName) { updateFilename(fileName); @@ -665,84 +674,117 @@ void DatabaseWidget::switchToImportKeepass1(const QString& fileName) setCurrentWidget(m_keepass1OpenWidget); } -void DatabaseWidget::toggleSearch() +void DatabaseWidget::search(const QString& searchString, bool caseSensitive, bool allGroups) { - if (m_entryView->inEntryListMode()) { - closeSearch(); + m_searchSensitivity = caseSensitive ? Qt::CaseSensitive + : Qt::CaseInsensitive; + m_searchAllGroups = allGroups; + search(searchString); +} + +void DatabaseWidget::search(const QString& text) +{ + if (text.isEmpty()) { + if (m_entryView->inEntryListMode()) + closeSearch(); + } + else if (m_entryView->inEntryListMode()) { + m_searchText = text; + startSearchTimer(); } else { - showSearch(); + showSearch(text); + } +} + +bool DatabaseWidget::caseSensitiveSearch() const +{ + return m_searchSensitivity == Qt::CaseSensitive; +} + +void DatabaseWidget::setCaseSensitiveSearch(bool caseSensitive) +{ + if (caseSensitive != caseSensitiveSearch()) { + m_searchSensitivity = caseSensitive ? Qt::CaseSensitive + : Qt::CaseInsensitive; + if (m_entryView->inEntryListMode()) + startSearchTimer(); + } +} + +bool DatabaseWidget::isAllGroupsSearch() const +{ + return m_searchAllGroups; +} + +bool DatabaseWidget::canChooseSearchScope() const +{ + return currentGroup() != m_db->rootGroup(); +} + +Group*DatabaseWidget::currentGroup() const +{ + return m_entryView->inEntryListMode() ? m_lastGroup + : m_groupView->currentGroup(); +} + +void DatabaseWidget::setAllGroupsSearch(bool allGroups) +{ + if (allGroups != isAllGroupsSearch()) { + m_searchAllGroups = allGroups; + if (m_entryView->inEntryListMode()) + startSearchTimer(); } } void DatabaseWidget::closeSearch() { Q_ASSERT(m_lastGroup); + m_searchTimer->stop(); m_groupView->setCurrentGroup(m_lastGroup); } -void DatabaseWidget::showSearch() +void DatabaseWidget::showSearch(const QString & searchString) { - m_searchUi->searchEdit->blockSignals(true); - m_searchUi->searchEdit->clear(); - m_searchUi->searchEdit->blockSignals(false); - - m_searchUi->searchCurrentRadioButton->blockSignals(true); - m_searchUi->searchRootRadioButton->blockSignals(true); - m_searchUi->searchRootRadioButton->setChecked(true); - m_searchUi->searchCurrentRadioButton->blockSignals(false); - m_searchUi->searchRootRadioButton->blockSignals(false); - + m_searchText = searchString; m_lastGroup = m_groupView->currentGroup(); Q_ASSERT(m_lastGroup); - - if (m_lastGroup == m_db->rootGroup()) { - m_searchUi->optionsWidget->hide(); - m_searchUi->searchCurrentRadioButton->hide(); - m_searchUi->searchRootRadioButton->hide(); - } - else { - m_searchUi->optionsWidget->show(); - m_searchUi->searchCurrentRadioButton->show(); - m_searchUi->searchRootRadioButton->show(); - m_searchUi->searchCurrentRadioButton->setText(tr("Current group") - .append(" (") - .append(m_lastGroup->name()) - .append(")")); - } m_groupView->setCurrentIndex(QModelIndex()); m_searchWidget->show(); search(); - m_searchUi->searchEdit->setFocus(); +} + +void DatabaseWidget::onLinkActivated(const QString& link) +{ + if (link == "searchAll") + setAllGroupsSearch(true); + else if (link == "searchCurrent") + setAllGroupsSearch(false); } void DatabaseWidget::search() { Q_ASSERT(m_lastGroup); - Group* searchGroup; - if (m_searchUi->searchCurrentRadioButton->isChecked()) { - searchGroup = m_lastGroup; - } - else if (m_searchUi->searchRootRadioButton->isChecked()) { - searchGroup = m_db->rootGroup(); - } - else { - Q_ASSERT(false); - return; - } + Group* searchGroup = m_searchAllGroups ? m_db->rootGroup() + : m_lastGroup; + QList searchResult = searchGroup->search(m_searchText, m_searchSensitivity); - Qt::CaseSensitivity sensitivity; - if (m_searchUi->caseSensitiveCheckBox->isChecked()) { - sensitivity = Qt::CaseSensitive; + QString message; + switch(searchResult.count()) { + case 0: message = tr("No result found"); break; + case 1: message = tr("1 result found"); break; + default: message = tr("%1 results found").arg(searchResult.count()); break; } - else { - sensitivity = Qt::CaseInsensitive; - } - QList searchResult = searchGroup->search(m_searchUi->searchEdit->text(), sensitivity); - + if (searchGroup != m_db->rootGroup()) + message += tr(" in \"%1\". Search all groups...").arg(searchGroup->name()); + else if (m_lastGroup != m_db->rootGroup()) + message += tr(". Search in \"%1\"...").arg(m_lastGroup->name()); + else + message += tr("."); + m_searchUi->searchResults->setText(message); m_entryView->setEntryList(searchResult); } @@ -757,6 +799,9 @@ void DatabaseWidget::startSearchTimer() void DatabaseWidget::startSearch() { + if (!isInSearchMode()) + return; + if (!m_searchTimer->isActive()) { m_searchTimer->stop(); } @@ -785,11 +830,16 @@ bool DatabaseWidget::canDeleteCurrentGoup() return !isRootGroup && !isRecycleBin; } -bool DatabaseWidget::isInSearchMode() +bool DatabaseWidget::isInSearchMode() const { return m_entryView->inEntryListMode(); } +QString DatabaseWidget::searchText() const +{ + return m_entryView->inEntryListMode() ? m_searchText : QString(); +} + void DatabaseWidget::clearLastGroup(Group* group) { if (group) { diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index b535a9b8f..c0f104529 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -39,6 +39,7 @@ class KeePass1OpenWidget; class QFile; class QMenu; class UnlockDatabaseWidget; +class CompositeKey; namespace Ui { class SearchWidget; @@ -54,6 +55,7 @@ public: None, ViewMode, EditMode, + OpenMode, LockedMode }; @@ -64,13 +66,19 @@ public: Database* database(); bool dbHasKey(); bool canDeleteCurrentGoup(); - bool isInSearchMode(); + bool isInSearchMode() const; + QString searchText() const; + bool caseSensitiveSearch() const; + bool isAllGroupsSearch() const; + bool canChooseSearchScope() const; + Group* currentGroup() const; int addWidget(QWidget* w); void setCurrentIndex(int index); void setCurrentWidget(QWidget* widget); DatabaseWidget::Mode currentMode(); void lock(); void updateFilename(const QString& filename); + void search(const QString & searchString, bool caseSensitive, bool allGroups); Q_SIGNALS: void closeRequest(); @@ -103,15 +111,20 @@ public Q_SLOTS: void switchToDatabaseSettings(); void switchToOpenDatabase(const QString& fileName); void switchToOpenDatabase(const QString& fileName, const QString& password, const QString& keyFile); + void switchToOpenDatabase(const QString &fileName, const CompositeKey &masterKey); void switchToImportKeepass1(const QString& fileName); - void toggleSearch(); + void switchToView(bool accepted); + void search(const QString & searchString); + void setCaseSensitiveSearch(bool caseSensitive); + void setAllGroupsSearch(bool allGroups); void emitGroupContextMenuRequested(const QPoint& pos); void emitEntryContextMenuRequested(const QPoint& pos); private Q_SLOTS: void entryActivationSignalReceived(Entry* entry, EntryModel::ModelColumn column); + void onLinkActivated(const QString& link); + void showSearch(const QString & searchString = QString()); void switchBackToEntryEdit(); - void switchToView(bool accepted); void switchToHistoryView(Entry* entry); void switchToEntryEdit(Entry* entry); void switchToEntryEdit(Entry* entry, bool create); @@ -124,7 +137,6 @@ private Q_SLOTS: void search(); void startSearch(); void startSearchTimer(); - void showSearch(); void closeSearch(); private: @@ -152,6 +164,9 @@ private: QTimer* m_searchTimer; QWidget* widgetBeforeLock; QString m_filename; + QString m_searchText; + bool m_searchAllGroups; + Qt::CaseSensitivity m_searchSensitivity; }; #endif // KEEPASSX_DATABASEWIDGET_H diff --git a/src/gui/EditWidgetIcons.cpp b/src/gui/EditWidgetIcons.cpp index 26314d360..348ee5766 100644 --- a/src/gui/EditWidgetIcons.cpp +++ b/src/gui/EditWidgetIcons.cpp @@ -38,6 +38,7 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent) , m_database(Q_NULLPTR) , m_defaultIconModel(new DefaultIconModel(this)) , m_customIconModel(new CustomIconModel(this)) + , m_networkAccessMngr(new QNetworkAccessManager(this)) { m_ui->setupUi(this); @@ -54,6 +55,11 @@ EditWidgetIcons::EditWidgetIcons(QWidget* parent) this, SLOT(updateWidgetsCustomIcons(bool))); connect(m_ui->addButton, SIGNAL(clicked()), SLOT(addCustomIcon())); connect(m_ui->deleteButton, SIGNAL(clicked()), SLOT(removeCustomIcon())); + connect(m_ui->faviconButton, SIGNAL(clicked()), SLOT(downloadFavicon())); + connect(m_networkAccessMngr, SIGNAL(finished(QNetworkReply*)), + this, SLOT(onRequestFinished(QNetworkReply*)) ); + + m_ui->faviconButton->setVisible(false); } EditWidgetIcons::~EditWidgetIcons() @@ -90,13 +96,14 @@ IconStruct EditWidgetIcons::save() return iconStruct; } -void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct iconStruct) +void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct iconStruct, const QString &url) { Q_ASSERT(database); Q_ASSERT(!currentUuid.isNull()); m_database = database; m_currentUuid = currentUuid; + setUrl(url); m_customIconModel->setIcons(database->metadata()->customIcons(), database->metadata()->customIconsOrder()); @@ -120,6 +127,76 @@ void EditWidgetIcons::load(Uuid currentUuid, Database* database, IconStruct icon } } +void EditWidgetIcons::setUrl(const QString &url) +{ + m_url = url; + m_ui->faviconButton->setVisible(!url.isEmpty()); + abortFaviconDownload(); +} + +static QStringList getHost(QUrl url, const QString& path) +{ + QStringList hosts; + + QString host = url.host(); + for(;;) { + QString s = host; + if (url.port() >= 0) + s += ":" + QString::number(url.port()); + hosts << s + path; + + const int first_dot = host.indexOf( '.' ); + const int last_dot = host.lastIndexOf( '.' ); + if( ( first_dot != -1 ) && ( last_dot != -1 ) && ( first_dot != last_dot ) ) + host.remove( 0, first_dot + 1 ); + else + break; + } + return hosts; +} + +void EditWidgetIcons::downloadFavicon() +{ + const QStringList pathes = getHost(QUrl(m_url), "/favicon."); + const QStringList prefixes = QStringList() << "http://" << "https://"; + const QStringList suffixes = QStringList() << "ico" << "png" << "gif" << "jpg"; + + Q_FOREACH (QString path, pathes) + Q_FOREACH (QString prefix, prefixes) + Q_FOREACH (QString suffix, suffixes) + m_networkOperations << m_networkAccessMngr->get(QNetworkRequest(prefix + path + suffix)); + //TODO: progress indication +} + +void EditWidgetIcons::abortFaviconDownload() +{ + Q_FOREACH (QNetworkReply *r, m_networkOperations) + r->abort(); +} + +void EditWidgetIcons::onRequestFinished(QNetworkReply *reply) +{ + if (m_networkOperations.contains(reply)) { + m_networkOperations.remove(reply); + + QImage image; + if (!reply->error() && image.loadFromData(reply->readAll()) && !image.isNull()) { + //Abort all other requests + abortFaviconDownload(); + + //Set the image + Uuid uuid = Uuid::random(); + m_database->metadata()->addCustomIcon(uuid, image.scaled(16, 16)); + m_customIconModel->setIcons(m_database->metadata()->customIcons(), + m_database->metadata()->customIconsOrder()); + QModelIndex index = m_customIconModel->indexFromUuid(uuid); + m_ui->customIconsView->setCurrentIndex(index); + m_ui->customIconsRadio->setChecked(true); + } + } + reply->deleteLater(); +} + void EditWidgetIcons::addCustomIcon() { if (m_database) { diff --git a/src/gui/EditWidgetIcons.h b/src/gui/EditWidgetIcons.h index e18f4440f..1ab9625e2 100644 --- a/src/gui/EditWidgetIcons.h +++ b/src/gui/EditWidgetIcons.h @@ -19,6 +19,11 @@ #define KEEPASSX_EDITWIDGETICONS_H #include +#include + + +#include +#include #include "core/Global.h" #include "core/Uuid.h" @@ -48,9 +53,15 @@ public: ~EditWidgetIcons(); IconStruct save(); - void load(Uuid currentUuid, Database* database, IconStruct iconStruct); + void load(Uuid currentUuid, Database* database, IconStruct iconStruct, const QString &url = QString()); + +public Q_SLOTS: + void setUrl(const QString &url); private Q_SLOTS: + void downloadFavicon(); + void abortFaviconDownload(); + void onRequestFinished(QNetworkReply *reply); void addCustomIcon(); void removeCustomIcon(); void updateWidgetsDefaultIcons(bool checked); @@ -62,8 +73,11 @@ private: const QScopedPointer m_ui; Database* m_database; Uuid m_currentUuid; + QString m_url; DefaultIconModel* const m_defaultIconModel; CustomIconModel* const m_customIconModel; + QNetworkAccessManager* const m_networkAccessMngr; + QSet m_networkOperations; Q_DISABLE_COPY(EditWidgetIcons) }; diff --git a/src/gui/EditWidgetIcons.ui b/src/gui/EditWidgetIcons.ui index bbdcebca5..047e106b8 100644 --- a/src/gui/EditWidgetIcons.ui +++ b/src/gui/EditWidgetIcons.ui @@ -91,6 +91,13 @@ + + + + Download favicon + + + diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 36fb656f2..2caade099 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -20,6 +20,7 @@ #include #include +#include #include "autotype/AutoType.h" #include "core/Config.h" @@ -33,6 +34,43 @@ #include "gui/entry/EntryView.h" #include "gui/group/GroupView.h" +#include "http/Service.h" +#include "http/HttpSettings.h" +#include "http/OptionDialog.h" +#include "gui/SettingsWidget.h" +#include "gui/qocoa/qsearchfield.h" + +class HttpPlugin: public ISettingsPage { +public: + HttpPlugin(DatabaseTabWidget * tabWidget) { + m_service = new Service(tabWidget); + } + virtual ~HttpPlugin() { + //delete m_service; + } + virtual QString name() { + return QObject::tr("Http"); + } + virtual QWidget * createWidget() { + OptionDialog * dlg = new OptionDialog(); + QObject::connect(dlg, SIGNAL(removeSharedEncryptionKeys()), m_service, SLOT(removeSharedEncryptionKeys())); + QObject::connect(dlg, SIGNAL(removeStoredPermissions()), m_service, SLOT(removeStoredPermissions())); + return dlg; + } + virtual void loadSettings(QWidget * widget) { + qobject_cast(widget)->loadSettings(); + } + virtual void saveSettings(QWidget * widget) { + qobject_cast(widget)->saveSettings(); + if (HttpSettings::isEnabled()) + m_service->start(); + else + m_service->stop(); + } +private: + Service *m_service; +}; + const QString MainWindow::BaseWindowTitle = "KeePassX"; MainWindow::MainWindow() @@ -41,14 +79,20 @@ MainWindow::MainWindow() m_ui->setupUi(this); restoreGeometry(config()->get("window/Geometry").toByteArray()); + m_ui->settingsWidget->addSettingsPage(new HttpPlugin(m_ui->tabWidget)); setWindowIcon(filePath()->applicationIcon()); QAction* toggleViewAction = m_ui->toolBar->toggleViewAction(); toggleViewAction->setText(tr("Show toolbar")); m_ui->menuView->addAction(toggleViewAction); + int toolbarIconSize = config()->get("ToolbarIconSize", 20).toInt(); + setToolbarIconSize(toolbarIconSize); bool showToolbar = config()->get("ShowToolbar").toBool(); m_ui->toolBar->setVisible(showToolbar); connect(m_ui->toolBar, SIGNAL(visibilityChanged(bool)), this, SLOT(saveToolbarState(bool))); + connect(m_ui->actionToolbarIconSize16, SIGNAL(triggered()), this, SLOT(setToolbarIconSize16())); + connect(m_ui->actionToolbarIconSize22, SIGNAL(triggered()), this, SLOT(setToolbarIconSize22())); + connect(m_ui->actionToolbarIconSize28, SIGNAL(triggered()), this, SLOT(setToolbarIconSize28())); m_clearHistoryAction = new QAction("Clear history", m_ui->menuFile); m_lastDatabasesActions = new QActionGroup(m_ui->menuRecentDatabases); @@ -80,13 +124,19 @@ MainWindow::MainWindow() setShortcut(m_ui->actionDatabaseClose, QKeySequence::Close, Qt::CTRL + Qt::Key_W); m_ui->actionLockDatabases->setShortcut(Qt::CTRL + Qt::Key_L); setShortcut(m_ui->actionQuit, QKeySequence::Quit, Qt::CTRL + Qt::Key_Q); - setShortcut(m_ui->actionSearch, QKeySequence::Find, Qt::CTRL + Qt::Key_F); + //TODO: do not register shortcut on Q_OS_MAC, if this is done automatically?? + const QKeySequence seq = !QKeySequence::keyBindings(QKeySequence::Find).isEmpty() + ? QKeySequence::Find + : QKeySequence(Qt::CTRL + Qt::Key_F); + connect(new QShortcut(seq, this), SIGNAL(activated()), m_ui->searchField, SLOT(setFocus())); + m_ui->searchField->setContentsMargins(5,0,5,0); m_ui->actionEntryNew->setShortcut(Qt::CTRL + Qt::Key_N); m_ui->actionEntryEdit->setShortcut(Qt::CTRL + Qt::Key_E); m_ui->actionEntryDelete->setShortcut(Qt::CTRL + Qt::Key_D); m_ui->actionEntryClone->setShortcut(Qt::CTRL + Qt::Key_K); m_ui->actionEntryCopyUsername->setShortcut(Qt::CTRL + Qt::Key_B); m_ui->actionEntryCopyPassword->setShortcut(Qt::CTRL + Qt::Key_C); + m_ui->actionEntryCopyURL->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_U); setShortcut(m_ui->actionEntryAutoType, QKeySequence::Paste, Qt::CTRL + Qt::Key_V); m_ui->actionEntryOpenUrl->setShortcut(Qt::CTRL + Qt::Key_U); @@ -120,8 +170,6 @@ MainWindow::MainWindow() m_ui->actionAbout->setIcon(filePath()->icon("actions", "help-about")); - m_ui->actionSearch->setIcon(filePath()->icon("actions", "system-search")); - m_actionMultiplexer.connect(SIGNAL(currentModeChanged(DatabaseWidget::Mode)), this, SLOT(setMenuActionState(DatabaseWidget::Mode))); m_actionMultiplexer.connect(SIGNAL(groupChanged()), @@ -201,8 +249,25 @@ MainWindow::MainWindow() connect(m_ui->actionAbout, SIGNAL(triggered()), SLOT(showAboutDialog())); - m_actionMultiplexer.connect(m_ui->actionSearch, SIGNAL(triggered()), - SLOT(toggleSearch())); + m_ui->searchField->setPlaceholderText(tr("Type to search")); + m_ui->searchField->setEnabled(false); + m_ui->toolBar->addWidget(m_ui->mySpacer); + m_ui->toolBar->addWidget(m_ui->searchField); + m_actionMultiplexer.connect(m_ui->searchField, SIGNAL(textChanged(QString)), + SLOT(search(QString))); + QMenu* searchMenu = new QMenu(this); + searchMenu->addAction(m_ui->actionFindCaseSensitive); + searchMenu->addSeparator(); + searchMenu->addAction(m_ui->actionFindCurrentGroup); + searchMenu->addAction(m_ui->actionFindRootGroup); + m_ui->searchField->setMenu(searchMenu); + QActionGroup* group = new QActionGroup(this); + group->addAction(m_ui->actionFindCurrentGroup); + group->addAction(m_ui->actionFindRootGroup); + m_actionMultiplexer.connect(m_ui->actionFindCaseSensitive, SIGNAL(toggled(bool)), + SLOT(setCaseSensitiveSearch(bool))); + m_actionMultiplexer.connect(m_ui->actionFindRootGroup, SIGNAL(toggled(bool)), + SLOT(setAllGroupsSearch(bool))); } MainWindow::~MainWindow() @@ -255,11 +320,40 @@ void MainWindow::clearLastDatabases() config()->set("LastDatabases", QVariant()); } +void MainWindow::changeEvent(QEvent *e) +{ + QMainWindow::changeEvent(e); + if (e->type() == QEvent::ActivationChange) { + if (isActiveWindow()) + m_ui->tabWidget->checkReloadDatabases(); + } +} + void MainWindow::openDatabase(const QString& fileName, const QString& pw, const QString& keyFile) { m_ui->tabWidget->openDatabase(fileName, pw, keyFile); } +void MainWindow::updateSearchField(DatabaseWidget* dbWidget) +{ + bool enabled = dbWidget != NULL; + + m_ui->actionFindCaseSensitive->setChecked(enabled && dbWidget->caseSensitiveSearch()); + + m_ui->actionFindCurrentGroup->setEnabled(enabled && dbWidget->canChooseSearchScope()); + m_ui->actionFindRootGroup->setEnabled(enabled && dbWidget->canChooseSearchScope()); + if (enabled && dbWidget->isAllGroupsSearch()) + m_ui->actionFindRootGroup->setChecked(true); + else + m_ui->actionFindCurrentGroup->setChecked(true); + + m_ui->searchField->setEnabled(enabled); + if (enabled && dbWidget->isInSearchMode()) + m_ui->searchField->setText(dbWidget->searchText()); + else + m_ui->searchField->clear(); +} + void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) { bool inDatabaseTabWidget = (m_ui->stackedWidget->currentIndex() == 0); @@ -295,9 +389,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) m_ui->actionGroupNew->setEnabled(groupSelected); m_ui->actionGroupEdit->setEnabled(groupSelected); m_ui->actionGroupDelete->setEnabled(groupSelected && dbWidget->canDeleteCurrentGoup()); - m_ui->actionSearch->setEnabled(true); - // TODO: get checked state from db widget - m_ui->actionSearch->setChecked(inSearch); + updateSearchField(dbWidget); m_ui->actionChangeMasterKey->setEnabled(true); m_ui->actionChangeDatabaseSettings->setEnabled(true); m_ui->actionDatabaseSave->setEnabled(true); @@ -306,6 +398,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) } case DatabaseWidget::EditMode: case DatabaseWidget::LockedMode: + case DatabaseWidget::OpenMode: Q_FOREACH (QAction* action, m_ui->menuEntries->actions()) { action->setEnabled(false); } @@ -315,8 +408,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) } m_ui->menuEntryCopyAttribute->setEnabled(false); - m_ui->actionSearch->setEnabled(false); - m_ui->actionSearch->setChecked(false); + updateSearchField(); m_ui->actionChangeMasterKey->setEnabled(false); m_ui->actionChangeDatabaseSettings->setEnabled(false); m_ui->actionDatabaseSave->setEnabled(false); @@ -337,8 +429,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode) } m_ui->menuEntryCopyAttribute->setEnabled(false); - m_ui->actionSearch->setEnabled(false); - m_ui->actionSearch->setChecked(false); + updateSearchField(); m_ui->actionChangeMasterKey->setEnabled(false); m_ui->actionChangeDatabaseSettings->setEnabled(false); m_ui->actionDatabaseSave->setEnabled(false); @@ -475,6 +566,30 @@ void MainWindow::saveToolbarState(bool value) config()->set("ShowToolbar", value); } +void MainWindow::setToolbarIconSize(int size) +{ + config()->set("ToolbarIconSize", size); + m_ui->toolBar->setIconSize(QSize(size, size)); + m_ui->actionToolbarIconSize16->setChecked(size == 16); + m_ui->actionToolbarIconSize22->setChecked(size == 22); + m_ui->actionToolbarIconSize28->setChecked(size == 28); +} + +void MainWindow::setToolbarIconSize16() +{ + setToolbarIconSize(16); +} + +void MainWindow::setToolbarIconSize22() +{ + setToolbarIconSize(22); +} + +void MainWindow::setToolbarIconSize28() +{ + setToolbarIconSize(28); +} + void MainWindow::setShortcut(QAction* action, QKeySequence::StandardKey standard, int fallback) { if (!QKeySequence::keyBindings(standard).isEmpty()) { diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 706fd2d50..23b8cc481 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -43,7 +43,8 @@ public Q_SLOTS: const QString& keyFile = QString()); protected: - void closeEvent(QCloseEvent* event) Q_DECL_OVERRIDE; + void changeEvent(QEvent *e); + void closeEvent(QCloseEvent* event) Q_DECL_OVERRIDE; private Q_SLOTS: void setMenuActionState(DatabaseWidget::Mode mode = DatabaseWidget::None); @@ -61,8 +62,13 @@ private Q_SLOTS: void saveToolbarState(bool value); void rememberOpenDatabases(const QString& filePath); void applySettingsChanges(); + void setToolbarIconSize(int size); + void setToolbarIconSize16(); + void setToolbarIconSize22(); + void setToolbarIconSize28(); private: + void updateSearchField(DatabaseWidget* dbWidget = NULL); static void setShortcut(QAction* action, QKeySequence::StandardKey standard, int fallback = 0); static const QString BaseWindowTitle; diff --git a/src/gui/MainWindow.ui b/src/gui/MainWindow.ui index 13c5d6796..7585a7549 100644 --- a/src/gui/MainWindow.ui +++ b/src/gui/MainWindow.ui @@ -70,7 +70,7 @@ 0 0 800 - 20 + 24 @@ -148,6 +148,15 @@ View + + + Toolbar &Icon Size + + + + + + @@ -177,7 +186,17 @@ - + + + + 0 + 0 + + + + + + @@ -303,17 +322,6 @@ Clone entry - - - true - - - false - - - Find - - false @@ -389,6 +397,54 @@ Notes + + + true + + + &16x16 + + + + + true + + + &22x22 + + + + + true + + + 2&8x28 + + + + + true + + + Case Sensitive + + + + + true + + + Current Group + + + + + true + + + Root Group + + @@ -409,6 +465,12 @@
gui/WelcomeWidget.h
1
+ + QSearchField + QWidget +
gui/qocoa/qsearchfield.h
+ 1 +
diff --git a/src/gui/SearchWidget.ui b/src/gui/SearchWidget.ui index c3d59b8d1..9f3348074 100644 --- a/src/gui/SearchWidget.ui +++ b/src/gui/SearchWidget.ui @@ -7,17 +7,14 @@ 0 0 630 - 87 + 16 0 - - - - + @@ -27,78 +24,14 @@ - - - Find: - - + - - - - - 0 - - - - - Case sensitive - - - - - - - Current group - - - false - - - - - - - Root group - - - true - - - - - - - Qt::Horizontal - - - - 255 - 1 - - - - - - - - - - LineEdit - QLineEdit -
gui/LineEdit.h
-
-
closeSearchButton - searchEdit - caseSensitiveCheckBox - searchCurrentRadioButton - searchRootRadioButton diff --git a/src/gui/SettingsWidget.cpp b/src/gui/SettingsWidget.cpp index d09805939..d3cc0477e 100644 --- a/src/gui/SettingsWidget.cpp +++ b/src/gui/SettingsWidget.cpp @@ -22,6 +22,27 @@ #include "autotype/AutoType.h" #include "core/Config.h" +class SettingsWidget::ExtraPage +{ +public: + ExtraPage(ISettingsPage* page, QWidget* widget): settingsPage(page), widget(widget) + {} + + void loadSettings() const + { + settingsPage->loadSettings(widget); + } + + void saveSettings() const + { + settingsPage->saveSettings(widget); + } + +private: + QSharedPointer settingsPage; + QWidget* widget; +}; + SettingsWidget::SettingsWidget(QWidget* parent) : EditWidget(parent) , m_secWidget(new QWidget()) @@ -57,6 +78,14 @@ SettingsWidget::~SettingsWidget() { } +void SettingsWidget::addSettingsPage(ISettingsPage *page) +{ + QWidget * widget = page->createWidget(); + widget->setParent(this); + m_extraPages.append(ExtraPage(page, widget)); + add(page->name(), widget); +} + void SettingsWidget::loadSettings() { m_generalUi->rememberLastDatabasesCheckBox->setChecked(config()->get("RememberLastDatabases").toBool()); @@ -67,6 +96,7 @@ void SettingsWidget::loadSettings() m_generalUi->autoSaveOnExitCheckBox->setChecked(config()->get("AutoSaveOnExit").toBool()); m_generalUi->minimizeOnCopyCheckBox->setChecked(config()->get("MinimizeOnCopy").toBool()); m_generalUi->useGroupIconOnEntryCreationCheckBox->setChecked(config()->get("UseGroupIconOnEntryCreation").toBool()); + m_generalUi->reloadBehavior->setCurrentIndex(config()->get("ReloadBehavior").toInt()); if (autoType()->isAvailable()) { m_globalAutoTypeKey = static_cast(config()->get("GlobalAutoTypeKey").toInt()); @@ -85,7 +115,8 @@ void SettingsWidget::loadSettings() m_secUi->passwordCleartextCheckBox->setChecked(config()->get("security/passwordscleartext").toBool()); m_secUi->autoTypeAskCheckBox->setChecked(config()->get("security/autotypeask").toBool()); - + Q_FOREACH (const ExtraPage& page, m_extraPages) + page.loadSettings(); setCurrentRow(0); } @@ -102,6 +133,7 @@ void SettingsWidget::saveSettings() config()->set("MinimizeOnCopy", m_generalUi->minimizeOnCopyCheckBox->isChecked()); config()->set("UseGroupIconOnEntryCreation", m_generalUi->useGroupIconOnEntryCreationCheckBox->isChecked()); + config()->set("ReloadBehavior", m_generalUi->reloadBehavior->currentIndex()); if (autoType()->isAvailable()) { config()->set("GlobalAutoTypeKey", m_generalUi->autoTypeShortcutWidget->key()); config()->set("GlobalAutoTypeModifiers", @@ -116,6 +148,8 @@ void SettingsWidget::saveSettings() config()->set("security/passwordscleartext", m_secUi->passwordCleartextCheckBox->isChecked()); config()->set("security/autotypeask", m_secUi->autoTypeAskCheckBox->isChecked()); + Q_FOREACH (const ExtraPage& page, m_extraPages) + page.saveSettings(); Q_EMIT editFinished(true); } diff --git a/src/gui/SettingsWidget.h b/src/gui/SettingsWidget.h index cefbf6d49..a53fd30b7 100644 --- a/src/gui/SettingsWidget.h +++ b/src/gui/SettingsWidget.h @@ -25,6 +25,15 @@ namespace Ui { class SettingsWidgetSecurity; } +class ISettingsPage { +public: + virtual ~ISettingsPage() {} + virtual QString name() = 0; + virtual QWidget * createWidget() = 0; + virtual void loadSettings(QWidget * widget) = 0; + virtual void saveSettings(QWidget * widget) = 0; +}; + class SettingsWidget : public EditWidget { Q_OBJECT @@ -32,6 +41,7 @@ class SettingsWidget : public EditWidget public: explicit SettingsWidget(QWidget* parent = Q_NULLPTR); ~SettingsWidget(); + void addSettingsPage(ISettingsPage * page); void loadSettings(); Q_SIGNALS: @@ -49,6 +59,8 @@ private: const QScopedPointer m_generalUi; Qt::Key m_globalAutoTypeKey; Qt::KeyboardModifiers m_globalAutoTypeModifiers; + class ExtraPage; + QList m_extraPages; }; #endif // KEEPASSX_SETTINGSWIDGET_H diff --git a/src/gui/SettingsWidgetGeneral.ui b/src/gui/SettingsWidgetGeneral.ui index f9aa8b680..a86ee8b4c 100644 --- a/src/gui/SettingsWidgetGeneral.ui +++ b/src/gui/SettingsWidgetGeneral.ui @@ -6,7 +6,7 @@ 0 0 - 456 + 481 185 @@ -14,7 +14,7 @@ - Remember last databases + Remember recent databases true @@ -62,6 +62,32 @@ + + + + When database files are externally modified + + + + + + + + Always ask + + + + + Reload unmodified databases + + + + + Ignore modifications + + + + diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 4a6743324..8d711db30 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -326,7 +326,8 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore) IconStruct iconStruct; iconStruct.uuid = entry->iconUuid(); iconStruct.number = entry->iconNumber(); - m_iconsWidget->load(entry->uuid(), m_database, iconStruct); + m_iconsWidget->load(entry->uuid(), m_database, iconStruct, entry->url()); + connect(m_mainUi->urlEdit, SIGNAL(textChanged(QString)), m_iconsWidget, SLOT(setUrl(QString))); m_autoTypeUi->enableButton->setChecked(entry->autoTypeEnabled()); if (entry->defaultAutoTypeSequence().isEmpty()) { diff --git a/src/gui/entry/EditEntryWidgetAdvanced.ui b/src/gui/entry/EditEntryWidgetAdvanced.ui index ff7ccbb73..7cf0eb4d3 100644 --- a/src/gui/entry/EditEntryWidgetAdvanced.ui +++ b/src/gui/entry/EditEntryWidgetAdvanced.ui @@ -18,13 +18,31 @@ - - - - - - false - + + + + 0 + + + + + + 0 + 0 + + + + Qt::Horizontal + + + + + false + + + + + diff --git a/src/gui/entry/EntryModel.cpp b/src/gui/entry/EntryModel.cpp index 369a4abb0..734ba7633 100644 --- a/src/gui/entry/EntryModel.cpp +++ b/src/gui/entry/EntryModel.cpp @@ -41,7 +41,7 @@ Entry* EntryModel::entryFromIndex(const QModelIndex& index) const QModelIndex EntryModel::indexFromEntry(Entry* entry) const { int row = m_entries.indexOf(entry); - Q_ASSERT(row != -1); + //Q_ASSERT(row != -1); return index(row, 1); } diff --git a/src/gui/qocoa/.gitignore b/src/gui/qocoa/.gitignore new file mode 100644 index 000000000..4963d2730 --- /dev/null +++ b/src/gui/qocoa/.gitignore @@ -0,0 +1,2 @@ +cmake/ +qmake/ \ No newline at end of file diff --git a/src/gui/qocoa/CMakeLists.txt b/src/gui/qocoa/CMakeLists.txt new file mode 100644 index 000000000..c1dcdce35 --- /dev/null +++ b/src/gui/qocoa/CMakeLists.txt @@ -0,0 +1,50 @@ +project(Qocoa) +cmake_minimum_required(VERSION 2.8) + +#find_package(Qt4 COMPONENTS QtMain QtCore QtGui REQUIRED) +#include(UseQt4) + +set(SOURCES + #main.cpp + #gallery.cpp +) + +set(HEADERS + #gallery.h + qsearchfield.h + qbutton.h + qprogressindicatorspinning.h +) + +qt4_wrap_cpp(MOC_SOURCES ${HEADERS}) + +if(APPLE) + list(APPEND SOURCES + qsearchfield_mac.mm + qbutton_mac.mm + qprogressindicatorspinning_mac.mm + ) +else() + list(APPEND SOURCES + qsearchfield_nonmac.cpp + qbutton_nonmac.cpp + qprogressindicatorspinning_nonmac.cpp + ) + set(RESOURCES + qsearchfield_nonmac.qrc + qprogressindicatorspinning_nonmac.qrc + ) + qt4_add_resources(RESOURCES_SOURCES ${RESOURCES}) +endif() + +#add_executable(Qocoa +# WIN32 MACOSX_BUNDLE +# ${SOURCES} ${MOC_SOURCES} ${RESOURCES_SOURCES} +#) +#target_link_libraries(Qocoa ${QT_LIBRARIES}) + +add_library (Qocoa STATIC ${SOURCES} ${MOC_SOURCES} ${HEADERS} ${RESOURCES_SOURCES}) + +#if(APPLE) +# set_target_properties(Qocoa PROPERTIES LINK_FLAGS "-framework Foundation -framework AppKit") +#endif() diff --git a/src/gui/qocoa/LICENSE.txt b/src/gui/qocoa/LICENSE.txt new file mode 100644 index 000000000..910eb6d20 --- /dev/null +++ b/src/gui/qocoa/LICENSE.txt @@ -0,0 +1,19 @@ +Copyright (C) 2011 by Mike McQuaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/src/gui/qocoa/Qocoa.pro b/src/gui/qocoa/Qocoa.pro new file mode 100644 index 000000000..8b325d192 --- /dev/null +++ b/src/gui/qocoa/Qocoa.pro @@ -0,0 +1,17 @@ +SOURCES += main.cpp\ + gallery.cpp \ + +HEADERS += gallery.h \ + qocoa_mac.h \ + qsearchfield.h \ + qbutton.h \ + qprogressindicatorspinning.h \ + +mac { + OBJECTIVE_SOURCES += qsearchfield_mac.mm qbutton_mac.mm qprogressindicatorspinning_mac.mm + LIBS += -framework Foundation -framework Appkit + QMAKE_CFLAGS += -mmacosx-version-min=10.6 +} else { + SOURCES += qsearchfield_nonmac.cpp qbutton_nonmac.cpp qprogressindicatorspinning_nonmac.cpp + RESOURCES += qsearchfield_nonmac.qrc qprogressindicatorspinning_nonmac.qrc +} diff --git a/src/gui/qocoa/README.md b/src/gui/qocoa/README.md new file mode 100644 index 000000000..3d37b6a27 --- /dev/null +++ b/src/gui/qocoa/README.md @@ -0,0 +1,36 @@ +# Qocoa +Qocoa is a collection of Qt wrappers for OSX's Cocoa widgets. + +## Features +- basic fallback to sensible Qt types on non-OSX platforms +- shared class headers which expose no implementation details +- typical Qt signal/slot-based API +- trivial to import into projects (class header/implementation, [single shared global header](https://github.com/mikemcquaid/Qocoa/blob/master/qocoa_mac.h)) + +## Building +``` +git clone git://github.com/mikemcquaid/Qocoa.git +cd Qocoa +qmake # or cmake . +make +``` + +## Status +Qocoa classes are currently provided for NSButton, a spinning NSProgressIndicator and NSSearchField. There is a [TODO list](https://github.com/mikemcquaid/Qocoa/blob/master/TODO.md) for classes I hope to implement. + +## Usage +For each class you want to use copy the [`qocoa_mac.h`](https://github.com/mikemcquaid/Qocoa/blob/master/qocoa_mac.h), `$CLASS.h`, `$CLASS_mac.*` and `$CLASS_nonmac.*` files into your source tree and add them to your buildsystem. Examples are provided for [CMake](https://github.com/mikemcquaid/Qocoa/blob/master/CMakeLists.txt) and [QMake](https://github.com/mikemcquaid/Qocoa/blob/master/Qocoa.pro). + +## Contact +[Mike McQuaid](mailto:mike@mikemcquaid.com) + +## License +Qocoa is licensed under the [MIT License](http://en.wikipedia.org/wiki/MIT_License). +The full license text is available in [LICENSE.txt](https://github.com/mikemcquaid/Qocoa/blob/master/LICENSE.txt). + +Magnifier and EditClear icons taken from [QtCreator](http://qt-project.org/) and are licensed under the [LGPL](http://www.gnu.org/copyleft/lesser.html). + +Other icons are taken from the [Oxygen Project](http://www.oxygen-icons.org/) and are licensed under the [Creative Commons Attribution-ShareAlike 3.0 License](http://creativecommons.org/licenses/by-sa/3.0/). + +## Gallery +![Qocoa Gallery](https://github.com/mikemcquaid/Qocoa/raw/master/gallery.png) diff --git a/src/gui/qocoa/TODO.md b/src/gui/qocoa/TODO.md new file mode 100644 index 000000000..45972bafa --- /dev/null +++ b/src/gui/qocoa/TODO.md @@ -0,0 +1,13 @@ +Widgets I hope to implement (or at least investigate): + +- NSTokenField +- NSSegmentedControl +- NSLevelIndicator +- NSPathControl +- NSSlider (Circular) +- NSSplitView +- NSTextFinder +- NSOutlineView in an NSScrollView (Source List) +- NSDrawer +- PDFView +- WebView diff --git a/src/gui/qocoa/gallery.cpp b/src/gui/qocoa/gallery.cpp new file mode 100644 index 000000000..210821ebd --- /dev/null +++ b/src/gui/qocoa/gallery.cpp @@ -0,0 +1,75 @@ +#include "gallery.h" + +#include + +#include "qsearchfield.h" +#include "qbutton.h" +#include "qprogressindicatorspinning.h" + +Gallery::Gallery(QWidget *parent) : QWidget(parent) +{ + setWindowTitle("Qocoa Gallery"); + QVBoxLayout *layout = new QVBoxLayout(this); + + QSearchField *searchField = new QSearchField(this); + layout->addWidget(searchField); + + QSearchField *searchFieldPlaceholder = new QSearchField(this); + searchFieldPlaceholder->setPlaceholderText("Placeholder text"); + layout->addWidget(searchFieldPlaceholder); + + QButton *roundedButton = new QButton(this, QButton::Rounded); + roundedButton->setText("Button"); + layout->addWidget(roundedButton); + + QButton *regularSquareButton = new QButton(this, QButton::RegularSquare); + regularSquareButton->setText("Button"); + layout->addWidget(regularSquareButton); + + QButton *disclosureButton = new QButton(this, QButton::Disclosure); + layout->addWidget(disclosureButton); + + QButton *shadowlessSquareButton = new QButton(this, QButton::ShadowlessSquare); + shadowlessSquareButton->setText("Button"); + layout->addWidget(shadowlessSquareButton); + + QButton *circularButton = new QButton(this, QButton::Circular); + layout->addWidget(circularButton); + + QButton *textureSquareButton = new QButton(this, QButton::TexturedSquare); + textureSquareButton->setText("Textured Button"); + layout->addWidget(textureSquareButton); + + QButton *helpButton = new QButton(this, QButton::HelpButton); + layout->addWidget(helpButton); + + QButton *smallSquareButton = new QButton(this, QButton::SmallSquare); + smallSquareButton->setText("Gradient Button"); + layout->addWidget(smallSquareButton); + + QButton *texturedRoundedButton = new QButton(this, QButton::TexturedRounded); + texturedRoundedButton->setText("Round Textured"); + layout->addWidget(texturedRoundedButton); + + QButton *roundedRectangleButton = new QButton(this, QButton::RoundRect); + roundedRectangleButton->setText("Rounded Rect Button"); + layout->addWidget(roundedRectangleButton); + + QButton *recessedButton = new QButton(this, QButton::Recessed); + recessedButton->setText("Recessed Button"); + layout->addWidget(recessedButton); + + QButton *roundedDisclosureButton = new QButton(this, QButton::RoundedDisclosure); + layout->addWidget(roundedDisclosureButton); + +#ifdef __MAC_10_7 + QButton *inlineButton = new QButton(this, QButton::Inline); + inlineButton->setText("Inline Button"); + layout->addWidget(inlineButton); +#endif + + + QProgressIndicatorSpinning *progressIndicatorSpinning = new QProgressIndicatorSpinning(this); + progressIndicatorSpinning->animate(); + layout->addWidget(progressIndicatorSpinning); +} diff --git a/src/gui/qocoa/gallery.h b/src/gui/qocoa/gallery.h new file mode 100644 index 000000000..1e83bad94 --- /dev/null +++ b/src/gui/qocoa/gallery.h @@ -0,0 +1,14 @@ +#ifndef GALLERY_H +#define GALLERY_H + +#include + +class Gallery : public QWidget +{ + Q_OBJECT + +public: + explicit Gallery(QWidget *parent = 0); +}; + +#endif // WIDGET_H diff --git a/src/gui/qocoa/gallery.png b/src/gui/qocoa/gallery.png new file mode 100644 index 000000000..7a2736ff5 Binary files /dev/null and b/src/gui/qocoa/gallery.png differ diff --git a/src/gui/qocoa/main.cpp b/src/gui/qocoa/main.cpp new file mode 100644 index 000000000..835e5eb5d --- /dev/null +++ b/src/gui/qocoa/main.cpp @@ -0,0 +1,13 @@ +#include + +#include "gallery.h" + +int main(int argc, char *argv[]) +{ + QApplication application(argc, argv); + + Gallery gallery; + gallery.show(); + + return application.exec(); +} diff --git a/src/gui/qocoa/qbutton.h b/src/gui/qocoa/qbutton.h new file mode 100644 index 000000000..c84ad73fd --- /dev/null +++ b/src/gui/qocoa/qbutton.h @@ -0,0 +1,49 @@ +#ifndef QBUTTON_H +#define QBUTTON_H + +#include +#include + +class QButtonPrivate; +class QButton : public QWidget +{ + Q_OBJECT +public: + // Matches NSBezelStyle + enum BezelStyle { + Rounded = 1, + RegularSquare = 2, + Disclosure = 5, + ShadowlessSquare = 6, + Circular = 7, + TexturedSquare = 8, + HelpButton = 9, + SmallSquare = 10, + TexturedRounded = 11, + RoundRect = 12, + Recessed = 13, + RoundedDisclosure = 14, +#ifdef __MAC_10_7 + Inline = 15 +#endif + }; + + explicit QButton(QWidget *parent, BezelStyle bezelStyle = Rounded); + +public Q_SLOTS: + void setText(const QString &text); + void setImage(const QPixmap &image); + void setChecked(bool checked); + +public: + void setCheckable(bool checkable); + bool isChecked(); + +Q_SIGNALS: + void clicked(bool checked = false); + +private: + friend class QButtonPrivate; + QPointer pimpl; +}; +#endif // QBUTTON_H diff --git a/src/gui/qocoa/qbutton_mac.mm b/src/gui/qocoa/qbutton_mac.mm new file mode 100644 index 000000000..dccc771ac --- /dev/null +++ b/src/gui/qocoa/qbutton_mac.mm @@ -0,0 +1,229 @@ +/* +Copyright (C) 2011 by Mike McQuaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "qbutton.h" + +#include "qocoa_mac.h" + +#import "Foundation/NSAutoreleasePool.h" +#import "AppKit/NSButton.h" +#import "AppKit/NSFont.h" + +class QButtonPrivate : public QObject +{ +public: + QButtonPrivate(QButton *qButton, NSButton *nsButton, QButton::BezelStyle bezelStyle) + : QObject(qButton), qButton(qButton), nsButton(nsButton) + { + switch(bezelStyle) { + case QButton::Disclosure: + case QButton::Circular: +#ifdef __MAC_10_7 + case QButton::Inline: +#endif + case QButton::RoundedDisclosure: + case QButton::HelpButton: + [nsButton setTitle:@""]; + default: + break; + } + + NSFont* font = 0; + switch(bezelStyle) { + case QButton::RoundRect: + font = [NSFont fontWithName:@"Lucida Grande" size:12]; + break; + + case QButton::Recessed: + font = [NSFont fontWithName:@"Lucida Grande Bold" size:12]; + break; + +#ifdef __MAC_10_7 + case QButton::Inline: + font = [NSFont boldSystemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]; + break; +#endif + + default: + font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]; + break; + } + [nsButton setFont:font]; + + switch(bezelStyle) { + case QButton::Rounded: + qButton->setMinimumWidth(40); + qButton->setFixedHeight(24); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); + break; + case QButton::RegularSquare: + case QButton::TexturedSquare: + qButton->setMinimumSize(14, 23); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + break; + case QButton::ShadowlessSquare: + qButton->setMinimumSize(5, 25); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + break; + case QButton::SmallSquare: + qButton->setMinimumSize(4, 21); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + break; + case QButton::TexturedRounded: + qButton->setMinimumSize(10, 22); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + break; + case QButton::RoundRect: + case QButton::Recessed: + qButton->setMinimumWidth(16); + qButton->setFixedHeight(18); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); + break; + case QButton::Disclosure: + qButton->setMinimumWidth(13); + qButton->setFixedHeight(13); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); + break; + case QButton::Circular: + qButton->setMinimumSize(16, 16); + qButton->setMaximumHeight(40); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + break; + case QButton::HelpButton: + case QButton::RoundedDisclosure: + qButton->setMinimumWidth(22); + qButton->setFixedHeight(22); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); + break; +#ifdef __MAC_10_7 + case QButton::Inline: + qButton->setMinimumWidth(10); + qButton->setFixedHeight(16); + qButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); + break; +#endif + } + + switch(bezelStyle) { + case QButton::Recessed: + [nsButton setButtonType:NSPushOnPushOffButton]; + case QButton::Disclosure: + [nsButton setButtonType:NSOnOffButton]; + default: + [nsButton setButtonType:NSMomentaryPushInButton]; + } + + [nsButton setBezelStyle:bezelStyle]; + } + + void clicked() + { + Q_EMIT qButton->clicked(qButton->isChecked()); + } + + ~QButtonPrivate() { + [[nsButton target] release]; + [nsButton setTarget:nil]; + } + + QButton *qButton; + NSButton *nsButton; +}; + +@interface QButtonTarget : NSObject +{ +@public + QPointer pimpl; +} +-(void)clicked; +@end + +@implementation QButtonTarget +-(void)clicked { + Q_ASSERT(pimpl); + if (pimpl) + pimpl->clicked(); +} +@end + +QButton::QButton(QWidget *parent, BezelStyle bezelStyle) : QWidget(parent) +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSButton *button = [[NSButton alloc] init]; + pimpl = new QButtonPrivate(this, button, bezelStyle); + + QButtonTarget *target = [[QButtonTarget alloc] init]; + target->pimpl = pimpl; + [button setTarget:target]; + + [button setAction:@selector(clicked)]; + + setupLayout(button, this); + + [button release]; + + [pool drain]; +} + +void QButton::setText(const QString &text) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [pimpl->nsButton setTitle:fromQString(text)]; + [pool drain]; +} + +void QButton::setImage(const QPixmap &image) +{ + Q_ASSERT(pimpl); + if (pimpl) + [pimpl->nsButton setImage:fromQPixmap(image)]; +} + +void QButton::setChecked(bool checked) +{ + Q_ASSERT(pimpl); + if (pimpl) + [pimpl->nsButton setState:checked]; +} + +void QButton::setCheckable(bool checkable) +{ + const NSInteger cellMask = checkable ? NSChangeBackgroundCellMask : NSNoCellMask; + + Q_ASSERT(pimpl); + if (pimpl) + [[pimpl->nsButton cell] setShowsStateBy:cellMask]; +} + +bool QButton::isChecked() +{ + Q_ASSERT(pimpl); + if (!pimpl) + return false; + + return [pimpl->nsButton state]; +} diff --git a/src/gui/qocoa/qbutton_nonmac.cpp b/src/gui/qocoa/qbutton_nonmac.cpp new file mode 100644 index 000000000..0a79e2baf --- /dev/null +++ b/src/gui/qocoa/qbutton_nonmac.cpp @@ -0,0 +1,89 @@ +/* +Copyright (C) 2011 by Mike McQuaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "qbutton.h" + +#include +#include +#include +#include + +class QButtonPrivate : public QObject +{ +public: + QButtonPrivate(QButton *button, QAbstractButton *abstractButton) + : QObject(button), abstractButton(abstractButton) {} + QPointer abstractButton; +}; + +QButton::QButton(QWidget *parent, BezelStyle) : QWidget(parent) +{ + QAbstractButton *button = 0; + if (qobject_cast(parent)) + button = new QToolButton(this); + else + button = new QPushButton(this); + connect(button, SIGNAL(clicked()), + this, SIGNAL(clicked())); + pimpl = new QButtonPrivate(this, button); + + QVBoxLayout *layout = new QVBoxLayout(this); + layout->setMargin(0); + layout->addWidget(button); +} + +void QButton::setText(const QString &text) +{ + Q_ASSERT(pimpl); + if (pimpl) + pimpl->abstractButton->setText(text); +} + +void QButton::setImage(const QPixmap &image) +{ + Q_ASSERT(pimpl); + if (pimpl) + pimpl->abstractButton->setIcon(image); +} + +void QButton::setChecked(bool checked) +{ + Q_ASSERT(pimpl); + if (pimpl) + pimpl->abstractButton->setChecked(checked); +} + +void QButton::setCheckable(bool checkable) +{ + Q_ASSERT(pimpl); + if (pimpl) + pimpl->abstractButton->setCheckable(checkable); +} + +bool QButton::isChecked() +{ + Q_ASSERT(pimpl); + if (!pimpl) + return false; + + return pimpl->abstractButton->isChecked(); +} diff --git a/src/gui/qocoa/qocoa_mac.h b/src/gui/qocoa/qocoa_mac.h new file mode 100644 index 000000000..f8307e912 --- /dev/null +++ b/src/gui/qocoa/qocoa_mac.h @@ -0,0 +1,63 @@ +/* +Copyright (C) 2011 by Mike McQuaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include +#include +#include +#include +#include + +#if QT_VERSION >= QT_VERSION_CHECK(5,0,0) +#include +#endif + +static inline NSString* fromQString(const QString &string) +{ + const QByteArray utf8 = string.toUtf8(); + const char* cString = utf8.constData(); + return [[NSString alloc] initWithUTF8String:cString]; +} + +static inline QString toQString(NSString *string) +{ + if (!string) + return QString(); + return QString::fromUtf8([string UTF8String]); +} + +static inline NSImage* fromQPixmap(const QPixmap &pixmap) +{ +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) + CGImageRef cgImage = pixmap.toMacCGImageRef(); +#else + CGImageRef cgImage = QtMac::toCGImageRef(pixmap); +#endif + return [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize]; +} + +static inline void setupLayout(NSView *cocoaView, QWidget *parent) +{ + parent->setAttribute(Qt::WA_NativeWindow); + QVBoxLayout *layout = new QVBoxLayout(parent); + layout->setMargin(0); + layout->addWidget(new QMacCocoaViewContainer(cocoaView, parent)); +} diff --git a/src/gui/qocoa/qprogressindicatorspinning.h b/src/gui/qocoa/qprogressindicatorspinning.h new file mode 100644 index 000000000..17b20e44d --- /dev/null +++ b/src/gui/qocoa/qprogressindicatorspinning.h @@ -0,0 +1,29 @@ +#ifndef QPROGRESSINDICATORSPINNING_H +#define QPROGRESSINDICATORSPINNING_H + +#include +#include + +class QProgressIndicatorSpinningPrivate; +class QProgressIndicatorSpinning : public QWidget +{ + Q_OBJECT +public: + // Matches NSProgressIndicatorThickness + enum Thickness { + Default = 14, + Small = 10, + Large = 18, + Aqua = 12 + }; + + explicit QProgressIndicatorSpinning(QWidget *parent, + Thickness thickness = Default); +public Q_SLOTS: + void animate(bool animate = true); +private: + friend class QProgressIndicatorSpinningPrivate; + QPointer pimpl; +}; + +#endif // QPROGRESSINDICATORSPINNING_H diff --git a/src/gui/qocoa/qprogressindicatorspinning_mac.mm b/src/gui/qocoa/qprogressindicatorspinning_mac.mm new file mode 100644 index 000000000..c67c7c567 --- /dev/null +++ b/src/gui/qocoa/qprogressindicatorspinning_mac.mm @@ -0,0 +1,70 @@ +/* +Copyright (C) 2011 by Mike McQuaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "qprogressindicatorspinning.h" + +#include "qocoa_mac.h" + +#import "Foundation/NSAutoreleasePool.h" +#import "AppKit/NSProgressIndicator.h" + +class QProgressIndicatorSpinningPrivate : public QObject +{ +public: + QProgressIndicatorSpinningPrivate(QProgressIndicatorSpinning *qProgressIndicatorSpinning, + NSProgressIndicator *nsProgressIndicator) + : QObject(qProgressIndicatorSpinning), nsProgressIndicator(nsProgressIndicator) {} + + NSProgressIndicator *nsProgressIndicator; +}; + +QProgressIndicatorSpinning::QProgressIndicatorSpinning(QWidget *parent, + Thickness thickness) + : QWidget(parent) +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSProgressIndicator *progress = [[NSProgressIndicator alloc] init]; + [progress setStyle:NSProgressIndicatorSpinningStyle]; + + pimpl = new QProgressIndicatorSpinningPrivate(this, progress); + + setupLayout(progress, this); + + setFixedSize(thickness, thickness); + + [progress release]; + + [pool drain]; +} + +void QProgressIndicatorSpinning::animate(bool animate) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + if (animate) + [pimpl->nsProgressIndicator startAnimation:nil]; + else + [pimpl->nsProgressIndicator stopAnimation:nil]; +} diff --git a/src/gui/qocoa/qprogressindicatorspinning_nonmac.cpp b/src/gui/qocoa/qprogressindicatorspinning_nonmac.cpp new file mode 100644 index 000000000..6cbded6c1 --- /dev/null +++ b/src/gui/qocoa/qprogressindicatorspinning_nonmac.cpp @@ -0,0 +1,72 @@ +/* +Copyright (C) 2011 by Mike McQuaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "qprogressindicatorspinning.h" + +#include +#include +#include + +class QProgressIndicatorSpinningPrivate : public QObject +{ +public: + QProgressIndicatorSpinningPrivate(QProgressIndicatorSpinning *qProgressIndicatorSpinning, + QMovie *movie) + : QObject(qProgressIndicatorSpinning), movie(movie) {} + + QPointer movie; +}; + +QProgressIndicatorSpinning::QProgressIndicatorSpinning(QWidget *parent, + Thickness thickness) + : QWidget(parent) +{ + QVBoxLayout *layout = new QVBoxLayout(this); + layout->setMargin(0); + + QSize size(thickness, thickness); + QMovie *movie = new QMovie(this); + movie->setFileName(":/Qocoa/qprogressindicatorspinning_nonmac.gif"); + movie->setScaledSize(size); + // Roughly match OSX speed. + movie->setSpeed(200); + pimpl = new QProgressIndicatorSpinningPrivate(this, movie); + + QLabel *label = new QLabel(this); + label->setMovie(movie); + + layout->addWidget(label); + setFixedSize(size); +} + + +void QProgressIndicatorSpinning::animate(bool animate) +{ + Q_ASSERT(pimpl && pimpl->movie); + if (!(pimpl && pimpl->movie)) + return; + + if (animate) + pimpl->movie->start(); + else + pimpl->movie->stop(); +} diff --git a/src/gui/qocoa/qprogressindicatorspinning_nonmac.gif b/src/gui/qocoa/qprogressindicatorspinning_nonmac.gif new file mode 100644 index 000000000..3288d1035 Binary files /dev/null and b/src/gui/qocoa/qprogressindicatorspinning_nonmac.gif differ diff --git a/src/gui/qocoa/qprogressindicatorspinning_nonmac.qrc b/src/gui/qocoa/qprogressindicatorspinning_nonmac.qrc new file mode 100644 index 000000000..108c78ec1 --- /dev/null +++ b/src/gui/qocoa/qprogressindicatorspinning_nonmac.qrc @@ -0,0 +1,5 @@ + + + qprogressindicatorspinning_nonmac.gif + + diff --git a/src/gui/qocoa/qsearchfield.h b/src/gui/qocoa/qsearchfield.h new file mode 100644 index 000000000..4d012cd06 --- /dev/null +++ b/src/gui/qocoa/qsearchfield.h @@ -0,0 +1,48 @@ +#ifndef QSEARCHFIELD_H +#define QSEARCHFIELD_H + +#include +#include +#include + +class QSearchFieldPrivate; +class QSearchField : public QWidget +{ + Q_OBJECT + + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged USER true); + Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText); + +public: + explicit QSearchField(QWidget *parent); + + QString text() const; + QString placeholderText() const; + void setFocus(Qt::FocusReason); + void setMenu(QMenu *menu); + +public Q_SLOTS: + void setText(const QString &text); + void setPlaceholderText(const QString &text); + void clear(); + void selectAll(); + void setFocus(); + +Q_SIGNALS: + void textChanged(const QString &text); + void editingFinished(); + void returnPressed(); + +private Q_SLOTS: + void popupMenu(); + +protected: + void changeEvent(QEvent*); + void resizeEvent(QResizeEvent*); + +private: + friend class QSearchFieldPrivate; + QPointer pimpl; +}; + +#endif // QSEARCHFIELD_H diff --git a/src/gui/qocoa/qsearchfield_mac.mm b/src/gui/qocoa/qsearchfield_mac.mm new file mode 100644 index 000000000..c4d320347 --- /dev/null +++ b/src/gui/qocoa/qsearchfield_mac.mm @@ -0,0 +1,262 @@ +/* +Copyright (C) 2011 by Mike McQuaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "qsearchfield.h" + +#include "qocoa_mac.h" + +#import "Foundation/NSAutoreleasePool.h" +#import "Foundation/NSNotification.h" +#import "AppKit/NSSearchField.h" + +#include +#include + +#define KEYCODE_A 0 +#define KEYCODE_X 7 +#define KEYCODE_C 8 +#define KEYCODE_V 9 + +class QSearchFieldPrivate : public QObject +{ +public: + QSearchFieldPrivate(QSearchField *qSearchField, NSSearchField *nsSearchField) + : QObject(qSearchField), qSearchField(qSearchField), nsSearchField(nsSearchField) {} + + void textDidChange(const QString &text) + { + if (qSearchField) + Q_EMIT qSearchField->textChanged(text); + } + + void textDidEndEditing() + { + if (qSearchField) + Q_EMIT qSearchField->editingFinished(); + } + + void returnPressed() + { + if (qSearchField) + Q_EMIT qSearchField->returnPressed(); + } + + QPointer qSearchField; + NSSearchField *nsSearchField; +}; + +@interface QSearchFieldDelegate : NSObject +{ +@public + QPointer pimpl; +} +-(void)controlTextDidChange:(NSNotification*)notification; +-(void)controlTextDidEndEditing:(NSNotification*)notification; +@end + +@implementation QSearchFieldDelegate +-(void)controlTextDidChange:(NSNotification*)notification { + Q_ASSERT(pimpl); + if (pimpl) + pimpl->textDidChange(toQString([[notification object] stringValue])); +} + +-(void)controlTextDidEndEditing:(NSNotification*)notification { + Q_UNUSED(notification); + // No Q_ASSERT here as it is called on destruction. + if (pimpl) + pimpl->textDidEndEditing(); + + if ([[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement) + pimpl->returnPressed(); +} +@end + +@interface QocoaSearchField : NSSearchField +-(BOOL)performKeyEquivalent:(NSEvent*)event; +@end + +@implementation QocoaSearchField +-(BOOL)performKeyEquivalent:(NSEvent*)event { + if ([event type] == NSKeyDown && [event modifierFlags] & NSCommandKeyMask) + { + const unsigned short keyCode = [event keyCode]; + if (keyCode == KEYCODE_A) + { + [self performSelector:@selector(selectText:)]; + return NO; + } + else if (keyCode == KEYCODE_C) + { + QClipboard* clipboard = QApplication::clipboard(); + clipboard->setText(toQString([self stringValue])); + return NO; + } + else if (keyCode == KEYCODE_V) + { + QClipboard* clipboard = QApplication::clipboard(); + [self setStringValue:fromQString(clipboard->text())]; + return NO; + } + else if (keyCode == KEYCODE_X) + { + QClipboard* clipboard = QApplication::clipboard(); + clipboard->setText(toQString([self stringValue])); + [self setStringValue:@""]; + return NO; + } + } + + return NO; +} +@end + +QSearchField::QSearchField(QWidget *parent) : QWidget(parent) +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSSearchField *search = [[QocoaSearchField alloc] init]; + + QSearchFieldDelegate *delegate = [[QSearchFieldDelegate alloc] init]; + pimpl = delegate->pimpl = new QSearchFieldPrivate(this, search); + [search setDelegate:delegate]; + + setupLayout(search, this); + + setFixedHeight(24); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + + [search release]; + + [pool drain]; +} + +void QSearchField::setMenu(QMenu *menu) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) + NSMenu *nsMenu = menu->macMenu(); +#else + NSMenu *nsMenu = menu->toNSMenu(); +#endif + + [[pimpl->nsSearchField cell] setSearchMenuTemplate:nsMenu]; +} + +void QSearchField::popupMenu() +{ +} + +void QSearchField::setText(const QString &text) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [pimpl->nsSearchField setStringValue:fromQString(text)]; + [pool drain]; +} + +void QSearchField::setPlaceholderText(const QString &text) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [[pimpl->nsSearchField cell] setPlaceholderString:fromQString(text)]; + [pool drain]; +} + +void QSearchField::clear() +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + [pimpl->nsSearchField setStringValue:@""]; + Q_EMIT textChanged(QString()); +} + +void QSearchField::selectAll() +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + [pimpl->nsSearchField performSelector:@selector(selectText:)]; +} + +QString QSearchField::text() const +{ + Q_ASSERT(pimpl); + if (!pimpl) + return QString(); + + return toQString([pimpl->nsSearchField stringValue]); +} + +QString QSearchField::placeholderText() const +{ + Q_ASSERT(pimpl); + if (!pimpl) + return QString(); + + return toQString([[pimpl->nsSearchField cell] placeholderString]); +} + +void QSearchField::setFocus(Qt::FocusReason) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + if ([pimpl->nsSearchField acceptsFirstResponder]) + [[pimpl->nsSearchField window] makeFirstResponder: pimpl->nsSearchField]; +} + +void QSearchField::setFocus() +{ + setFocus(Qt::OtherFocusReason); +} + +void QSearchField::changeEvent(QEvent* event) +{ + if (event->type() == QEvent::EnabledChange) { + Q_ASSERT(pimpl); + if (!pimpl) + return; + + const bool enabled = isEnabled(); + [pimpl->nsSearchField setEnabled: enabled]; + } + QWidget::changeEvent(event); +} + +void QSearchField::resizeEvent(QResizeEvent *resizeEvent) +{ + QWidget::resizeEvent(resizeEvent); +} diff --git a/src/gui/qocoa/qsearchfield_nonmac.cpp b/src/gui/qocoa/qsearchfield_nonmac.cpp new file mode 100644 index 000000000..d3315b853 --- /dev/null +++ b/src/gui/qocoa/qsearchfield_nonmac.cpp @@ -0,0 +1,256 @@ +/* +Copyright (C) 2011 by Mike McQuaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "qsearchfield.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +class QSearchFieldPrivate : public QObject +{ +public: + QSearchFieldPrivate(QSearchField *searchField, QLineEdit *lineEdit, QToolButton *clearButton, QToolButton *searchButton) + : QObject(searchField), lineEdit(lineEdit), clearButton(clearButton), searchButton(searchButton) {} + + int lineEditFrameWidth() const { + return lineEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + } + + int clearButtonPaddedWidth() const { + return clearButton->width() + lineEditFrameWidth() * 2; + } + + int clearButtonPaddedHeight() const { + return clearButton->height() + lineEditFrameWidth() * 2; + } + + int searchButtonPaddedWidth() const { + return searchButton->width() + lineEditFrameWidth() * 2; + } + + int searchButtonPaddedHeight() const { + return searchButton->height() + lineEditFrameWidth() * 2; + } + + QPointer lineEdit; + QPointer clearButton; + QPointer searchButton; + QPointer searchMenu; +}; + +QSearchField::QSearchField(QWidget *parent) : QWidget(parent) +{ + QLineEdit *lineEdit = new QLineEdit(this); + connect(lineEdit, SIGNAL(textChanged(QString)), + this, SIGNAL(textChanged(QString))); + connect(lineEdit, SIGNAL(editingFinished()), + this, SIGNAL(editingFinished())); + connect(lineEdit, SIGNAL(returnPressed()), + this, SIGNAL(returnPressed())); + connect(lineEdit, SIGNAL(textChanged(QString)), + this, SLOT(setText(QString))); + + int iconsize = style()->pixelMetric(QStyle::PM_SmallIconSize); + QToolButton *clearButton = new QToolButton(this); + QIcon clearIcon = QIcon::fromTheme(QLatin1String("edit-clear"), + QIcon(QLatin1String(":/Qocoa/qsearchfield_nonmac_clear.png"))); + clearButton->setIcon(clearIcon); + clearButton->setIconSize(QSize(iconsize, iconsize)); + clearButton->setFixedSize(QSize(iconsize, iconsize)); + clearButton->setStyleSheet("border: none;"); + clearButton->hide(); + connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); + + QToolButton *searchButton = new QToolButton(this); + QIcon searchIcon = QIcon(QLatin1String(":/Qocoa/qsearchfield_nonmac_magnifier.png")); + searchButton->setIcon(searchIcon); + searchButton->setIconSize(QSize(iconsize, iconsize)); + searchButton->setFixedSize(QSize(iconsize, iconsize)); + searchButton->setStyleSheet("border: none;"); + searchButton->setPopupMode(QToolButton::InstantPopup); + searchButton->setEnabled(false); + connect(searchButton, SIGNAL(clicked()), this, SLOT(popupMenu())); + + pimpl = new QSearchFieldPrivate(this, lineEdit, clearButton, searchButton); + + lineEdit->setStyleSheet(QString("QLineEdit { padding-left: %1px; padding-right: %2px; } ") + .arg(pimpl->searchButtonPaddedWidth()) + .arg(pimpl->clearButtonPaddedWidth())); + const int width = qMax(lineEdit->minimumSizeHint().width(), pimpl->clearButtonPaddedWidth() + pimpl->searchButtonPaddedWidth()); + const int height = qMax(lineEdit->minimumSizeHint().height(), + qMax(pimpl->clearButtonPaddedHeight(), + pimpl->searchButtonPaddedHeight())); + lineEdit->setMinimumSize(width, height); + + QVBoxLayout *layout = new QVBoxLayout(this); + layout->setMargin(0); + layout->addWidget(lineEdit); +} + +void QSearchField::setMenu(QMenu *menu) +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + pimpl->searchMenu = menu; + + QIcon searchIcon = menu ? QIcon(QLatin1String(":/Qocoa/qsearchfield_nonmac_magnifier_menu.png")) + : QIcon(QLatin1String(":/Qocoa/qsearchfield_nonmac_magnifier.png")); + pimpl->searchButton->setIcon(searchIcon); + pimpl->searchButton->setEnabled(isEnabled() && menu); +} + +void QSearchField::popupMenu() +{ + Q_ASSERT(pimpl); + if (!pimpl) + return; + + if (pimpl->searchMenu) { + const QRect screenRect = qApp->desktop()->availableGeometry(pimpl->searchButton); + const QSize sizeHint = pimpl->searchMenu->sizeHint(); + const QRect rect = pimpl->searchButton->rect(); + const int x = pimpl->searchButton->isRightToLeft() + ? rect.right() - sizeHint.width() + : rect.left(); + const int y = pimpl->searchButton->mapToGlobal(QPoint(0, rect.bottom())).y() + sizeHint.height() <= screenRect.height() + ? rect.bottom() + : rect.top() - sizeHint.height(); + QPoint point = pimpl->searchButton->mapToGlobal(QPoint(x, y)); + point.rx() = qMax(screenRect.left(), qMin(point.x(), screenRect.right() - sizeHint.width())); + point.ry() += 1; + + pimpl->searchMenu->popup(point); + } +} + +void QSearchField::changeEvent(QEvent* event) +{ + if (event->type() == QEvent::EnabledChange) { + Q_ASSERT(pimpl); + if (!pimpl) + return; + + const bool enabled = isEnabled(); + pimpl->searchButton->setEnabled(enabled && pimpl->searchMenu); + pimpl->lineEdit->setEnabled(enabled); + pimpl->clearButton->setEnabled(enabled); + } + QWidget::changeEvent(event); +} + +void QSearchField::setText(const QString &text) +{ + Q_ASSERT(pimpl && pimpl->clearButton && pimpl->lineEdit); + if (!(pimpl && pimpl->clearButton && pimpl->lineEdit)) + return; + + pimpl->clearButton->setVisible(!text.isEmpty()); + + if (text != this->text()) + pimpl->lineEdit->setText(text); +} + +void QSearchField::setPlaceholderText(const QString &text) +{ + Q_ASSERT(pimpl && pimpl->lineEdit); + if (!(pimpl && pimpl->lineEdit)) + return; + +#if QT_VERSION >= 0x040700 + pimpl->lineEdit->setPlaceholderText(text); +#endif +} + +void QSearchField::clear() +{ + Q_ASSERT(pimpl && pimpl->lineEdit); + if (!(pimpl && pimpl->lineEdit)) + return; + + pimpl->lineEdit->clear(); +} + +void QSearchField::selectAll() +{ + Q_ASSERT(pimpl && pimpl->lineEdit); + if (!(pimpl && pimpl->lineEdit)) + return; + + pimpl->lineEdit->selectAll(); +} + +QString QSearchField::text() const +{ + Q_ASSERT(pimpl && pimpl->lineEdit); + if (!(pimpl && pimpl->lineEdit)) + return QString(); + + return pimpl->lineEdit->text(); +} + +QString QSearchField::placeholderText() const { + Q_ASSERT(pimpl && pimpl->lineEdit); + if (!(pimpl && pimpl->lineEdit)) + return QString(); + +#if QT_VERSION >= 0x040700 + return pimpl->lineEdit->placeholderText(); +#else + return QString(); +#endif +} + +void QSearchField::setFocus(Qt::FocusReason reason) +{ + Q_ASSERT(pimpl && pimpl->lineEdit); + if (pimpl && pimpl->lineEdit) + pimpl->lineEdit->setFocus(reason); +} + +void QSearchField::setFocus() +{ + setFocus(Qt::OtherFocusReason); +} + +void QSearchField::resizeEvent(QResizeEvent *resizeEvent) +{ + Q_ASSERT(pimpl && pimpl->clearButton && pimpl->lineEdit); + if (!(pimpl && pimpl->clearButton && pimpl->lineEdit)) + return; + + QWidget::resizeEvent(resizeEvent); + const int x = width() - pimpl->clearButtonPaddedWidth(); + const int y = (height() - pimpl->clearButton->height())/2; + pimpl->clearButton->move(x, y); + + pimpl->searchButton->move(pimpl->lineEditFrameWidth() * 2, + (height() - pimpl->searchButton->height())/2); +} diff --git a/src/gui/qocoa/qsearchfield_nonmac.qrc b/src/gui/qocoa/qsearchfield_nonmac.qrc new file mode 100644 index 000000000..68b570d5b --- /dev/null +++ b/src/gui/qocoa/qsearchfield_nonmac.qrc @@ -0,0 +1,7 @@ + + + qsearchfield_nonmac_clear.png + qsearchfield_nonmac_magnifier_menu.png + qsearchfield_nonmac_magnifier.png + + diff --git a/src/gui/qocoa/qsearchfield_nonmac_clear.png b/src/gui/qocoa/qsearchfield_nonmac_clear.png new file mode 100644 index 000000000..ec52c41bc Binary files /dev/null and b/src/gui/qocoa/qsearchfield_nonmac_clear.png differ diff --git a/src/gui/qocoa/qsearchfield_nonmac_magnifier.png b/src/gui/qocoa/qsearchfield_nonmac_magnifier.png new file mode 100644 index 000000000..ad7929d2b Binary files /dev/null and b/src/gui/qocoa/qsearchfield_nonmac_magnifier.png differ diff --git a/src/gui/qocoa/qsearchfield_nonmac_magnifier_menu.png b/src/gui/qocoa/qsearchfield_nonmac_magnifier_menu.png new file mode 100644 index 000000000..0e652c945 Binary files /dev/null and b/src/gui/qocoa/qsearchfield_nonmac_magnifier_menu.png differ diff --git a/src/http/AccessControlDialog.cpp b/src/http/AccessControlDialog.cpp new file mode 100644 index 000000000..de6f0e6ba --- /dev/null +++ b/src/http/AccessControlDialog.cpp @@ -0,0 +1,52 @@ +/** + *************************************************************************** + * @file AccessControlDialog.cpp + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#include "AccessControlDialog.h" +#include "ui_AccessControlDialog.h" +#include "core/Entry.h" + +AccessControlDialog::AccessControlDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::AccessControlDialog) +{ + ui->setupUi(this); + connect(ui->allowButton, SIGNAL(clicked()), this, SLOT(accept())); + connect(ui->denyButton, SIGNAL(clicked()), this, SLOT(reject())); +} + +AccessControlDialog::~AccessControlDialog() +{ + delete ui; +} + +void AccessControlDialog::setUrl(const QString &url) +{ + ui->label->setText(QString(tr("%1 has requested access to passwords for the following item(s).\n" + "Please select whether you want to allow access.")).arg(QUrl(url).host())); +} + +void AccessControlDialog::setItems(const QList &items) +{ + Q_FOREACH (Entry * entry, items) + ui->itemsList->addItem(entry->title() + " - " + entry->username()); +} + +bool AccessControlDialog::remember() const +{ + return ui->rememberDecisionCheckBox->isChecked(); +} + +void AccessControlDialog::setRemember(bool r) +{ + ui->rememberDecisionCheckBox->setChecked(r); +} diff --git a/src/http/AccessControlDialog.h b/src/http/AccessControlDialog.h new file mode 100644 index 000000000..67a6a8492 --- /dev/null +++ b/src/http/AccessControlDialog.h @@ -0,0 +1,42 @@ +/** + *************************************************************************** + * @file AccessControlDialog.h + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#ifndef ACCESSCONTROLDIALOG_H +#define ACCESSCONTROLDIALOG_H + +#include + +class Entry; + +namespace Ui { +class AccessControlDialog; +} + +class AccessControlDialog : public QDialog +{ + Q_OBJECT + +public: + explicit AccessControlDialog(QWidget *parent = 0); + ~AccessControlDialog(); + + void setUrl(const QString & url); + void setItems(const QList & items); + bool remember() const; + void setRemember(bool r); + +private: + Ui::AccessControlDialog *ui; +}; + +#endif // ACCESSCONTROLDIALOG_H diff --git a/src/http/AccessControlDialog.ui b/src/http/AccessControlDialog.ui new file mode 100644 index 000000000..4faed6de1 --- /dev/null +++ b/src/http/AccessControlDialog.ui @@ -0,0 +1,69 @@ + + + AccessControlDialog + + + + 0 + 0 + 400 + 221 + + + + KeyPassX/Http: Confirm Access + + + + + + + + + + + + + + + + Remember this decision + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Allow + + + + + + + Deny + + + + + + + + + + diff --git a/src/http/EntryConfig.cpp b/src/http/EntryConfig.cpp new file mode 100644 index 000000000..b737cf3e9 --- /dev/null +++ b/src/http/EntryConfig.cpp @@ -0,0 +1,100 @@ +/** + *************************************************************************** + * @file EntryConfig.cpp + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#include "EntryConfig.h" +#include "core/Entry.h" +#include "core/EntryAttributes.h" +#include "qjson/src/parser.h" +#include "qjson/src/qobjecthelper.h" +#include "qjson/src/serializer.h" + +static const char KEEPASSHTTP_NAME[] = "KeePassHttp Settings"; //TODO: duplicated string (also in Service.cpp) + +EntryConfig::EntryConfig(QObject *parent) : + QObject(parent) +{ +} + +QStringList EntryConfig::allowedHosts() const +{ + return m_allowedHosts.toList(); +} + +void EntryConfig::setAllowedHosts(const QStringList &allowedHosts) +{ + m_allowedHosts = allowedHosts.toSet(); +} + +QStringList EntryConfig::deniedHosts() const +{ + return m_deniedHosts.toList(); +} + +void EntryConfig::setDeniedHosts(const QStringList &deniedHosts) +{ + m_deniedHosts = deniedHosts.toSet(); +} + +bool EntryConfig::isAllowed(const QString &host) +{ + return m_allowedHosts.contains(host); +} + +void EntryConfig::allow(const QString &host) +{ + m_allowedHosts.insert(host); + m_deniedHosts.remove(host); +} + +bool EntryConfig::isDenied(const QString &host) +{ + return m_deniedHosts.contains(host); +} + +void EntryConfig::deny(const QString &host) +{ + m_deniedHosts.insert(host); + m_allowedHosts.remove(host); +} + +QString EntryConfig::realm() const +{ + return m_realm; +} + +void EntryConfig::setRealm(const QString &realm) +{ + m_realm = realm; +} + +bool EntryConfig::load(const Entry *entry) +{ + QString s = entry->attributes()->value(KEEPASSHTTP_NAME); + if (s.isEmpty()) + return false; + + bool isOk = false; + QVariant v = QJson::Parser().parse(s.toUtf8(), &isOk); + if (!isOk || v.type() != QVariant::Map) + return false; + + QJson::QObjectHelper::qvariant2qobject(v.toMap(), this); + return true; +} + +void EntryConfig::save(Entry *entry) +{ + QVariant v = QJson::QObjectHelper::qobject2qvariant(this, QJson::QObjectHelper::Flag_None); + QByteArray json = QJson::Serializer().serialize(v); + entry->attributes()->set(KEEPASSHTTP_NAME, json); +} diff --git a/src/http/EntryConfig.h b/src/http/EntryConfig.h new file mode 100644 index 000000000..40633162f --- /dev/null +++ b/src/http/EntryConfig.h @@ -0,0 +1,54 @@ +/** + *************************************************************************** + * @file EntryConfig.h + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#ifndef ENTRYCONFIG_H +#define ENTRYCONFIG_H + +#include +#include +#include +#include + +class Entry; + +class EntryConfig : public QObject +{ + Q_OBJECT + Q_PROPERTY(QStringList Allow READ allowedHosts WRITE setAllowedHosts) + Q_PROPERTY(QStringList Deny READ deniedHosts WRITE setDeniedHosts ) + Q_PROPERTY(QString Realm READ realm WRITE setRealm ) + +public: + EntryConfig(QObject * object = 0); + + bool load(const Entry * entry); + void save(Entry * entry); + bool isAllowed(const QString & host); + void allow(const QString & host); + bool isDenied(const QString & host); + void deny(const QString & host); + QString realm() const; + void setRealm(const QString &realm); + +private: + QStringList allowedHosts() const; + void setAllowedHosts(const QStringList &allowedHosts); + QStringList deniedHosts() const; + void setDeniedHosts(const QStringList &deniedHosts); + + QSet m_allowedHosts; + QSet m_deniedHosts; + QString m_realm; +}; + +#endif // ENTRYCONFIG_H diff --git a/src/http/HttpPasswordGeneratorWidget.cpp b/src/http/HttpPasswordGeneratorWidget.cpp new file mode 100644 index 000000000..30e4f71e7 --- /dev/null +++ b/src/http/HttpPasswordGeneratorWidget.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2013 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "HttpPasswordGeneratorWidget.h" +#include "ui_HttpPasswordGeneratorWidget.h" + +#include + +#include "core/Config.h" +#include "core/PasswordGenerator.h" +#include "core/FilePath.h" + +HttpPasswordGeneratorWidget::HttpPasswordGeneratorWidget(QWidget* parent) + : QWidget(parent) + , m_updatingSpinBox(false) + , m_generator(new PasswordGenerator()) + , m_ui(new Ui::HttpPasswordGeneratorWidget()) +{ + m_ui->setupUi(this); + + connect(m_ui->buttonApply, SIGNAL(clicked()), SLOT(saveSettings())); + + connect(m_ui->sliderLength, SIGNAL(valueChanged(int)), SLOT(sliderMoved())); + connect(m_ui->spinBoxLength, SIGNAL(valueChanged(int)), SLOT(spinBoxChanged())); + + connect(m_ui->optionButtons, SIGNAL(buttonClicked(int)), SLOT(updateGenerator())); + m_ui->buttonApply->setEnabled(true); + + loadSettings(); + reset(); +} + +HttpPasswordGeneratorWidget::~HttpPasswordGeneratorWidget() +{ +} + +void HttpPasswordGeneratorWidget::loadSettings() +{ + m_ui->checkBoxLower->setChecked(config()->get("Http/generator/LowerCase", true).toBool()); + m_ui->checkBoxUpper->setChecked(config()->get("Http/generator/UpperCase", true).toBool()); + m_ui->checkBoxNumbers->setChecked(config()->get("Http/generator/Numbers", true).toBool()); + m_ui->checkBoxSpecialChars->setChecked(config()->get("Http/generator/SpecialChars", false).toBool()); + + m_ui->checkBoxExcludeAlike->setChecked(config()->get("Http/generator/ExcludeAlike", true).toBool()); + m_ui->checkBoxEnsureEvery->setChecked(config()->get("Http/generator/EnsureEvery", true).toBool()); + + m_ui->spinBoxLength->setValue(config()->get("Http/generator/Length", 16).toInt()); +} + +void HttpPasswordGeneratorWidget::saveSettings() +{ + config()->set("Http/generator/LowerCase", m_ui->checkBoxLower->isChecked()); + config()->set("Http/generator/UpperCase", m_ui->checkBoxUpper->isChecked()); + config()->set("Http/generator/Numbers", m_ui->checkBoxNumbers->isChecked()); + config()->set("Http/generator/SpecialChars", m_ui->checkBoxSpecialChars->isChecked()); + + config()->set("Http/generator/ExcludeAlike", m_ui->checkBoxExcludeAlike->isChecked()); + config()->set("Http/generator/EnsureEvery", m_ui->checkBoxEnsureEvery->isChecked()); + + config()->set("Http/generator/Length", m_ui->spinBoxLength->value()); +} + +void HttpPasswordGeneratorWidget::reset() +{ + updateGenerator(); +} + +void HttpPasswordGeneratorWidget::sliderMoved() +{ + if (m_updatingSpinBox) { + return; + } + + m_ui->spinBoxLength->setValue(m_ui->sliderLength->value()); + + updateGenerator(); +} + +void HttpPasswordGeneratorWidget::spinBoxChanged() +{ + // Interlock so that we don't update twice - this causes issues as the spinbox can go higher than slider + m_updatingSpinBox = true; + + m_ui->sliderLength->setValue(m_ui->spinBoxLength->value()); + + m_updatingSpinBox = false; + + updateGenerator(); +} + +PasswordGenerator::CharClasses HttpPasswordGeneratorWidget::charClasses() +{ + PasswordGenerator::CharClasses classes; + + if (m_ui->checkBoxLower->isChecked()) { + classes |= PasswordGenerator::LowerLetters; + } + + if (m_ui->checkBoxUpper->isChecked()) { + classes |= PasswordGenerator::UpperLetters; + } + + if (m_ui->checkBoxNumbers->isChecked()) { + classes |= PasswordGenerator::Numbers; + } + + if (m_ui->checkBoxSpecialChars->isChecked()) { + classes |= PasswordGenerator::SpecialCharacters; + } + + return classes; +} + +PasswordGenerator::GeneratorFlags HttpPasswordGeneratorWidget::generatorFlags() +{ + PasswordGenerator::GeneratorFlags flags; + + if (m_ui->checkBoxExcludeAlike->isChecked()) { + flags |= PasswordGenerator::ExcludeLookAlike; + } + + if (m_ui->checkBoxEnsureEvery->isChecked()) { + flags |= PasswordGenerator::CharFromEveryGroup; + } + + return flags; +} + +void HttpPasswordGeneratorWidget::updateGenerator() +{ + m_generator->setLength(m_ui->spinBoxLength->value()); + m_generator->setCharClasses(charClasses()); + m_generator->setFlags(generatorFlags()); +} diff --git a/src/http/HttpPasswordGeneratorWidget.h b/src/http/HttpPasswordGeneratorWidget.h new file mode 100644 index 000000000..5766a6e7f --- /dev/null +++ b/src/http/HttpPasswordGeneratorWidget.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2013 Felix Geyer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KEEPASSX_PASSWORDGENERATORWIDGET_H +#define KEEPASSX_PASSWORDGENERATORWIDGET_H + +#include +#include + +#include "core/Global.h" +#include "core/PasswordGenerator.h" + +namespace Ui { + class HttpPasswordGeneratorWidget; +} + +class PasswordGenerator; + +class HttpPasswordGeneratorWidget : public QWidget +{ + Q_OBJECT + +public: + explicit HttpPasswordGeneratorWidget(QWidget* parent = Q_NULLPTR); + ~HttpPasswordGeneratorWidget(); + void loadSettings(); + void reset(); + +private Q_SLOTS: + void saveSettings(); + void sliderMoved(); + void spinBoxChanged(); + + void updateGenerator(); + +private: + bool m_updatingSpinBox; + + PasswordGenerator::CharClasses charClasses(); + PasswordGenerator::GeneratorFlags generatorFlags(); + + const QScopedPointer m_generator; + const QScopedPointer m_ui; +}; + +#endif // KEEPASSX_PASSWORDGENERATORWIDGET_H diff --git a/src/http/HttpPasswordGeneratorWidget.ui b/src/http/HttpPasswordGeneratorWidget.ui new file mode 100644 index 000000000..29399bcd6 --- /dev/null +++ b/src/http/HttpPasswordGeneratorWidget.ui @@ -0,0 +1,227 @@ + + + HttpPasswordGeneratorWidget + + + + 0 + 0 + 434 + 250 + + + + + + + + + + + + Length: + + + + + + + + + 1 + + + 64 + + + Qt::Horizontal + + + QSlider::TicksBelow + + + 8 + + + + + + + 1 + + + 999 + + + + + + + + + + + Character Types + + + + + + + + Upper Case Letters + + + A-Z + + + true + + + optionButtons + + + + + + + Lower Case Letters + + + a-z + + + true + + + optionButtons + + + + + + + Numbers + + + 0-9 + + + true + + + optionButtons + + + + + + + Special Characters + + + /*_& ... + + + true + + + optionButtons + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Exclude look-alike characters + + + optionButtons + + + + + + + Ensure that the password contains characters from every group + + + optionButtons + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + false + + + Accept + + + + + + + + + + PasswordComboBox + QComboBox +
gui/PasswordComboBox.h
+
+
+ + sliderLength + spinBoxLength + checkBoxUpper + checkBoxLower + checkBoxNumbers + checkBoxSpecialChars + checkBoxExcludeAlike + checkBoxEnsureEvery + buttonApply + + + + + + + false + + + +
diff --git a/src/http/HttpSettings.cpp b/src/http/HttpSettings.cpp new file mode 100644 index 000000000..4692023d1 --- /dev/null +++ b/src/http/HttpSettings.cpp @@ -0,0 +1,236 @@ +/** + *************************************************************************** + * @file HttpSettings.cpp + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#include "HttpSettings.h" +#include "core/Config.h" + +PasswordGenerator HttpSettings::m_generator; + +bool HttpSettings::isEnabled() +{ + return config()->get("Http/Enabled", true).toBool(); +} + +void HttpSettings::setEnabled(bool enabled) +{ + config()->set("Http/Enabled", enabled); +} + +bool HttpSettings::showNotification() +{ + return config()->get("Http/ShowNotification", true).toBool(); +} + +void HttpSettings::setShowNotification(bool showNotification) +{ + config()->set("Http/ShowNotification", showNotification); +} + +bool HttpSettings::bestMatchOnly() +{ + return config()->get("Http/BestMatchOnly", false).toBool(); +} + +void HttpSettings::setBestMatchOnly(bool bestMatchOnly) +{ + config()->set("Http/BestMatchOnly", bestMatchOnly); +} + +bool HttpSettings::unlockDatabase() +{ + return config()->get("Http/UnlockDatabase", true).toBool(); +} + +void HttpSettings::setUnlockDatabase(bool unlockDatabase) +{ + config()->set("Http/UnlockDatabase", unlockDatabase); +} + +bool HttpSettings::matchUrlScheme() +{ + return config()->get("Http/MatchUrlScheme", true).toBool(); +} + +void HttpSettings::setMatchUrlScheme(bool matchUrlScheme) +{ + config()->set("Http/MatchUrlScheme", matchUrlScheme); +} + +bool HttpSettings::sortByUsername() +{ + return config()->get("Http/SortByUsername", false).toBool(); +} + +void HttpSettings::setSortByUsername(bool sortByUsername) +{ + config()->set("Http/SortByUsername", sortByUsername); +} + +bool HttpSettings::sortByTitle() +{ + return !sortByUsername(); +} + +void HttpSettings::setSortByTitle(bool sortByUsertitle) +{ + setSortByUsername(!sortByUsertitle); +} + +bool HttpSettings::alwaysAllowAccess() +{ + return config()->get("Http/AlwaysAllowAccess", false).toBool(); +} + +void HttpSettings::setAlwaysAllowAccess(bool alwaysAllowAccess) +{ + config()->set("Http/AlwaysAllowAccess", alwaysAllowAccess); +} + +bool HttpSettings::alwaysAllowUpdate() +{ + return config()->get("Http/AlwaysAllowUpdate", false).toBool(); +} + +void HttpSettings::setAlwaysAllowUpdate(bool alwaysAllowUpdate) +{ + config()->set("Http/AlwaysAllowAccess", alwaysAllowUpdate); +} + +bool HttpSettings::searchInAllDatabases() +{ + return config()->get("Http/SearchInAllDatabases", false).toBool(); +} + +void HttpSettings::setSearchInAllDatabases(bool searchInAllDatabases) +{ + config()->set("Http/SearchInAllDatabases", searchInAllDatabases); +} + +bool HttpSettings::supportKphFields() +{ + return config()->get("Http/SupportKphFields", true).toBool(); +} + +void HttpSettings::setSupportKphFields(bool supportKphFields) +{ + config()->set("Http/SupportKphFields", supportKphFields); +} + +bool HttpSettings::passwordUseNumbers() +{ + return config()->get("Http/generator/Numbers", true).toBool(); +} + +void HttpSettings::setPasswordUseNumbers(bool useNumbers) +{ + config()->set("Http/generator/Numbers", useNumbers); +} + +bool HttpSettings::passwordUseLowercase() +{ + return config()->get("Http/generator/LowerCase", true).toBool(); +} + +void HttpSettings::setPasswordUseLowercase(bool useLowercase) +{ + config()->set("Http/generator/LowerCase", useLowercase); +} + +bool HttpSettings::passwordUseUppercase() +{ + return config()->get("Http/generator/UpperCase", true).toBool(); +} + +void HttpSettings::setPasswordUseUppercase(bool useUppercase) +{ + config()->set("Http/generator/UpperCase", useUppercase); +} + +bool HttpSettings::passwordUseSpecial() +{ + return config()->get("Http/generator/SpecialChars", false).toBool(); +} + +void HttpSettings::setPasswordUseSpecial(bool useSpecial) +{ + config()->set("Http/generator/SpecialChars", useSpecial); +} + +bool HttpSettings::passwordEveryGroup() +{ + return config()->get("Http/generator/EnsureEvery", true).toBool(); +} + +void HttpSettings::setPasswordEveryGroup(bool everyGroup) +{ + config()->get("Http/generator/EnsureEvery", everyGroup); +} + +bool HttpSettings::passwordExcludeAlike() +{ + return config()->get("Http/generator/ExcludeAlike", true).toBool(); +} + +void HttpSettings::setPasswordExcludeAlike(bool excludeAlike) +{ + config()->set("Http/generator/ExcludeAlike", excludeAlike); +} + +int HttpSettings::passwordLength() +{ + return config()->get("Http/generator/Length", 20).toInt(); +} + +void HttpSettings::setPasswordLength(int length) +{ + config()->set("Http/generator/Length", length); + m_generator.setLength(length); +} + +PasswordGenerator::CharClasses HttpSettings::passwordCharClasses() +{ + PasswordGenerator::CharClasses classes; + if (passwordUseLowercase()) + classes |= PasswordGenerator::LowerLetters; + if (passwordUseUppercase()) + classes |= PasswordGenerator::UpperLetters; + if (passwordUseNumbers()) + classes |= PasswordGenerator::Numbers; + if (passwordUseSpecial()) + classes |= PasswordGenerator::SpecialCharacters; + return classes; +} + +PasswordGenerator::GeneratorFlags HttpSettings::passwordGeneratorFlags() +{ + PasswordGenerator::GeneratorFlags flags; + if (passwordExcludeAlike()) + flags |= PasswordGenerator::ExcludeLookAlike; + if (passwordEveryGroup()) + flags |= PasswordGenerator::CharFromEveryGroup; + return flags; +} + +QString HttpSettings::generatePassword() +{ + m_generator.setLength(passwordLength()); + m_generator.setCharClasses(passwordCharClasses()); + m_generator.setFlags(passwordGeneratorFlags()); + + return m_generator.generatePassword(); +} + +int HttpSettings::getbits() +{ + return m_generator.getbits(); +} diff --git a/src/http/HttpSettings.h b/src/http/HttpSettings.h new file mode 100644 index 000000000..ba430c672 --- /dev/null +++ b/src/http/HttpSettings.h @@ -0,0 +1,69 @@ +/** + *************************************************************************** + * @file HttpSettings.h + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#ifndef HTTPSETTINGS_H +#define HTTPSETTINGS_H + +#include "core/PasswordGenerator.h" + +class HttpSettings +{ +public: + static bool isEnabled(); + static void setEnabled(bool enabled); + + static bool showNotification(); //TODO!! + static void setShowNotification(bool showNotification); + static bool bestMatchOnly(); //TODO!! + static void setBestMatchOnly(bool bestMatchOnly); + static bool unlockDatabase(); //TODO!! + static void setUnlockDatabase(bool unlockDatabase); + static bool matchUrlScheme(); + static void setMatchUrlScheme(bool matchUrlScheme); + static bool sortByUsername(); + static void setSortByUsername(bool sortByUsername = true); + static bool sortByTitle(); + static void setSortByTitle(bool sortByUsertitle = true); + static bool alwaysAllowAccess(); + static void setAlwaysAllowAccess(bool alwaysAllowAccess); + static bool alwaysAllowUpdate(); + static void setAlwaysAllowUpdate(bool alwaysAllowUpdate); + static bool searchInAllDatabases();//TODO!! + static void setSearchInAllDatabases(bool searchInAllDatabases); + static bool supportKphFields(); + static void setSupportKphFields(bool supportKphFields); + + static bool passwordUseNumbers(); + static void setPasswordUseNumbers(bool useNumbers); + static bool passwordUseLowercase(); + static void setPasswordUseLowercase(bool useLowercase); + static bool passwordUseUppercase(); + static void setPasswordUseUppercase(bool useUppercase); + static bool passwordUseSpecial(); + static void setPasswordUseSpecial(bool useSpecial); + static bool passwordEveryGroup(); + static void setPasswordEveryGroup(bool everyGroup); + static bool passwordExcludeAlike(); + static void setPasswordExcludeAlike(bool excludeAlike); + static int passwordLength(); + static void setPasswordLength(int length); + static PasswordGenerator::CharClasses passwordCharClasses(); + static PasswordGenerator::GeneratorFlags passwordGeneratorFlags(); + static QString generatePassword(); + static int getbits(); + +private: + static PasswordGenerator m_generator; +}; + +#endif // HTTPSETTINGS_H diff --git a/src/http/OptionDialog.cpp b/src/http/OptionDialog.cpp new file mode 100644 index 000000000..63222947a --- /dev/null +++ b/src/http/OptionDialog.cpp @@ -0,0 +1,85 @@ +/** + *************************************************************************** + * @file OptionDialog.cpp + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#include "OptionDialog.h" +#include "ui_OptionDialog.h" +#include "HttpSettings.h" + +OptionDialog::OptionDialog(QWidget *parent) : + QWidget(parent), + ui(new Ui::OptionDialog) +{ + ui->setupUi(this); + connect(ui->removeSharedEncryptionKeys, SIGNAL(clicked()), this, SIGNAL(removeSharedEncryptionKeys())); + connect(ui->removeStoredPermissions, SIGNAL(clicked()), this, SIGNAL(removeStoredPermissions())); +} + +OptionDialog::~OptionDialog() +{ + delete ui; +} + +void OptionDialog::loadSettings() +{ + HttpSettings settings; + ui->enableHttpServer->setChecked(settings.isEnabled()); + + ui->showNotification->setChecked(settings.showNotification()); + ui->bestMatchOnly->setChecked(settings.bestMatchOnly()); + ui->unlockDatabase->setChecked(settings.unlockDatabase()); + ui->matchUrlScheme->setChecked(settings.matchUrlScheme()); + if (settings.sortByUsername()) + ui->sortByUsername->setChecked(true); + else + ui->sortByTitle->setChecked(true); + +/* + ui->checkBoxLower->setChecked(settings.passwordUseLowercase()); + ui->checkBoxNumbers->setChecked(settings.passwordUseNumbers()); + ui->checkBoxUpper->setChecked(settings.passwordUseUppercase()); + ui->checkBoxSpecialChars->setChecked(settings.passwordUseSpecial()); + ui->checkBoxEnsureEvery->setChecked(settings.passwordEveryGroup()); + ui->checkBoxExcludeAlike->setChecked(settings.passwordExcludeAlike()); + ui->spinBoxLength->setValue(settings.passwordLength()); +*/ + + ui->alwaysAllowAccess->setChecked(settings.alwaysAllowAccess()); + ui->alwaysAllowUpdate->setChecked(settings.alwaysAllowUpdate()); + ui->searchInAllDatabases->setChecked(settings.searchInAllDatabases()); +} + +void OptionDialog::saveSettings() +{ + HttpSettings settings; + settings.setEnabled(ui->enableHttpServer->isChecked()); + + settings.setShowNotification(ui->showNotification->isChecked()); + settings.setBestMatchOnly(ui->bestMatchOnly->isChecked()); + settings.setUnlockDatabase(ui->unlockDatabase->isChecked()); + settings.setMatchUrlScheme(ui->matchUrlScheme->isChecked()); + settings.setSortByUsername(ui->sortByUsername->isChecked()); + +/* + settings.setPasswordUseLowercase(ui->checkBoxLower->isChecked()); + settings.setPasswordUseNumbers(ui->checkBoxNumbers->isChecked()); + settings.setPasswordUseUppercase(ui->checkBoxUpper->isChecked()); + settings.setPasswordUseSpecial(ui->checkBoxSpecialChars->isChecked()); + settings.setPasswordEveryGroup(ui->checkBoxEnsureEvery->isChecked()); + settings.setPasswordExcludeAlike(ui->checkBoxExcludeAlike->isChecked()); + settings.setPasswordLength(ui->spinBoxLength->value()); +*/ + + settings.setAlwaysAllowAccess(ui->alwaysAllowAccess->isChecked()); + settings.setAlwaysAllowUpdate(ui->alwaysAllowUpdate->isChecked()); + settings.setSearchInAllDatabases(ui->searchInAllDatabases->isChecked()); +} diff --git a/src/http/OptionDialog.h b/src/http/OptionDialog.h new file mode 100644 index 000000000..e7508d233 --- /dev/null +++ b/src/http/OptionDialog.h @@ -0,0 +1,43 @@ +/** + *************************************************************************** + * @file OptionDialog.h + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#ifndef OPTIONDIALOG_H +#define OPTIONDIALOG_H + +#include + +namespace Ui { +class OptionDialog; +} + +class OptionDialog : public QWidget +{ + Q_OBJECT + +public: + explicit OptionDialog(QWidget *parent = 0); + ~OptionDialog(); + +public Q_SLOTS: + void loadSettings(); + void saveSettings(); + +Q_SIGNALS: + void removeSharedEncryptionKeys(); + void removeStoredPermissions(); + +private: + Ui::OptionDialog *ui; +}; + +#endif // OPTIONDIALOG_H diff --git a/src/http/OptionDialog.ui b/src/http/OptionDialog.ui new file mode 100644 index 000000000..9563fd04a --- /dev/null +++ b/src/http/OptionDialog.ui @@ -0,0 +1,237 @@ + + + OptionDialog + + + + 0 + 0 + 463 + 354 + + + + Dialog + + + + + + Support KeypassHttp protocol +This is required for accessing keypass database from ChromeIPass or PassIfox + + + + + + + QTabWidget::Rounded + + + 0 + + + + General + + + + + + Sh&ow a notification when credentials are requested + + + + + + + &Return only best matching entries for an URL instead +of all entries for the whole domain + + + + + + + Re&quest for unlocking the database if it is locked + + + + + + + &Match URL schemes +Only entries with the same scheme (http://, https://, ftp://, ...) are returned + + + + + + + Sort matching entries by &username + + + + + + + Sort matching entries by &title + + + + + + + R&emove all shared encryption-keys from active database + + + + + + + Re&move all stored permissions from entries in active database + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + Password generator + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + Advanced + + + + + + + 75 + true + + + + color: rgb(255, 0, 0); + + + Activate the following only, if you know what you are doing! + + + + + + + Always allow &access to entries + + + + + + + Always allow &updating entries + + + + + + + Searc&h in all opened databases for matching entries + + + + + + + Only the selected database has to be connected with a client! + + + 30 + + + + + + + &Return also advanced string fields which start with "KPH: " + + + + + + + Automatic creates or updates are not supported for string fields! + + + 30 + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + HttpPasswordGeneratorWidget + QWidget +
http/HttpPasswordGeneratorWidget.h
+ 1 +
+ + PasswordEdit + QLineEdit +
gui/PasswordEdit.h
+
+
+ + +
diff --git a/src/http/Protocol.cpp b/src/http/Protocol.cpp new file mode 100644 index 000000000..36c2fd7ea --- /dev/null +++ b/src/http/Protocol.cpp @@ -0,0 +1,525 @@ +/** + *************************************************************************** + * @file Response.cpp + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#include "Protocol.h" + +#include +#include +#include + +#include "qjson/src/parser.h" +#include "qjson/src/qobjecthelper.h" +#include "qjson/src/serializer.h" + +#include "crypto/Random.h" +#include "crypto/SymmetricCipher.h" +#include "crypto/SymmetricCipherGcrypt.h" + +namespace KeepassHttpProtocol +{ +static const char * const STR_GET_LOGINS = "get-logins"; +static const char * const STR_GET_LOGINS_COUNT = "get-logins-count"; +static const char * const STR_GET_ALL_LOGINS = "get-all-logins"; +static const char * const STR_SET_LOGIN = "set-login"; +static const char * const STR_ASSOCIATE = "associate"; +static const char * const STR_TEST_ASSOCIATE = "test-associate"; +static const char * const STR_GENERATE_PASSWORD = "generate-password"; +static const char * const STR_VERSION = "1.8.3.0"; + +}/*namespace KeepassHttpProtocol*/ + +using namespace KeepassHttpProtocol; + +static QHash createStringHash() +{ + QHash hash; + hash.insert(STR_GET_LOGINS, GET_LOGINS); + hash.insert(STR_GET_LOGINS_COUNT, GET_LOGINS_COUNT); + hash.insert(STR_GET_ALL_LOGINS, GET_ALL_LOGINS); + hash.insert(STR_SET_LOGIN, SET_LOGIN); + hash.insert(STR_ASSOCIATE, ASSOCIATE); + hash.insert(STR_TEST_ASSOCIATE, TEST_ASSOCIATE); + hash.insert(STR_GENERATE_PASSWORD,GENERATE_PASSWORD); + return hash; +} + +static RequestType parseRequest(const QString &str) +{ + static const QHash REQUEST_STRINGS = createStringHash(); + return REQUEST_STRINGS.value(str, INVALID); +} + +static QByteArray decode64(QString s) +{ + return QByteArray::fromBase64(s.toAscii()); +} + +static QString encode64(QByteArray b) +{ + return QString::fromAscii(b.toBase64()); +} + +static QByteArray decrypt2(const QByteArray & data, SymmetricCipherGcrypt & cipher) +{ + //Ensure we get full blocks only + if (data.length() <= 0 || data.length() % cipher.blockSize()) + return QByteArray(); + + //Decrypt + cipher.reset(); + QByteArray buffer = cipher.process(data); + + //Remove PKCS#7 padding + buffer.chop(buffer.at(buffer.length()-1)); + return buffer; +} + +static QString decrypt(const QString &data, SymmetricCipherGcrypt & cipher) +{ + return QString::fromUtf8(decrypt2(decode64(data), cipher)); +} + +static QByteArray encrypt2(const QByteArray & data, SymmetricCipherGcrypt & cipher) +{ + //Add PKCS#7 padding + const int blockSize = cipher.blockSize(); + const int paddingSize = blockSize - data.size() % blockSize; + + //Encrypt + QByteArray buffer = data + QByteArray(paddingSize, paddingSize); + cipher.reset(); + cipher.processInPlace(buffer); + return buffer; +} + +static QString encrypt(const QString & data, SymmetricCipherGcrypt & cipher) +{ + return encode64(encrypt2(data.toUtf8(), cipher)); +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Request +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Request::Request(): + m_requestType(INVALID), + m_cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Decrypt) +{ +} + +QString Request::nonce() const +{ + return m_nonce; +} + +void Request::setNonce(const QString &nonce) +{ + m_nonce = nonce; +} + +QString Request::verifier() const +{ + return m_verifier; +} + +void Request::setVerifier(const QString &verifier) +{ + m_verifier = verifier; +} + +QString Request::id() const +{ + return m_id; +} + +void Request::setId(const QString &id) +{ + m_id = id; +} + +QString Request::key() const +{ + return m_key; +} + +void Request::setKey(const QString &key) +{ + m_key = key; +} + +QString Request::submitUrl() const +{ + Q_ASSERT(m_cipher.isValid()); + return decrypt(m_submitUrl, m_cipher); +} + +void Request::setSubmitUrl(const QString &submitUrl) +{ + m_submitUrl = submitUrl; +} + +QString Request::url() const +{ + Q_ASSERT(m_cipher.isValid()); + return decrypt(m_url, m_cipher); +} + +void Request::setUrl(const QString &url) +{ + m_url = url; +} + +QString Request::realm() const +{ + Q_ASSERT(m_cipher.isValid()); + return decrypt(m_realm, m_cipher); +} + +void Request::setRealm(const QString &realm) +{ + m_realm = realm; +} + +QString Request::login() const +{ + Q_ASSERT(m_cipher.isValid()); + return decrypt(m_login, m_cipher); +} + +void Request::setLogin(const QString &login) +{ + m_login = login; +} + +QString Request::uuid() const +{ + Q_ASSERT(m_cipher.isValid()); + return decrypt(m_uuid, m_cipher); +} + +void Request::setUuid(const QString &uuid) +{ + m_uuid = uuid; +} + +QString Request::password() const +{ + Q_ASSERT(m_cipher.isValid()); + return decrypt(m_password, m_cipher); +} + +void Request::setPassword(const QString &password) +{ + m_password = password; +} + +bool Request::sortSelection() const +{ + return m_sortSelection; +} + +void Request::setSortSelection(bool sortSelection) +{ + m_sortSelection = sortSelection; +} + +KeepassHttpProtocol::RequestType Request::requestType() const +{ + return parseRequest(m_requestType); +} + +QString Request::requestTypeStr() const +{ + return m_requestType; +} + +void Request::setRequestType(const QString &requestType) +{ + m_requestType = requestType; +} + +bool Request::CheckVerifier(const QString &key) const +{ + Q_ASSERT(!m_cipher.isValid()); + + m_cipher.setKey(decode64(key)); + m_cipher.setIv(decode64(m_nonce)); + + return decrypt(m_verifier, m_cipher) == m_nonce; +} + +bool Request::fromJson(QString text) +{ + bool isok = false; + QVariant v = QJson::Parser().parse(text.toUtf8(), &isok); + if (!isok) + return false; + + m_requestType.clear(); + QJson::QObjectHelper::qvariant2qobject(v.toMap(), this); + + return requestType() != INVALID; +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Response +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Response::Response(const Request &request, QString hash): + m_requestType(request.requestTypeStr()), + m_success(false), + m_count(-1), + m_version(STR_VERSION), + m_hash(hash), + m_cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt) +{ +} + +void Response::setVerifier(QString key) +{ + Q_ASSERT(!m_cipher.isValid()); + + m_cipher.setKey(decode64(key)); + + //Generate new IV + const QByteArray iv = randomGen()->randomArray(m_cipher.blockSize()); + m_cipher.setIv(iv); + m_nonce = encode64(iv); + + //Encrypt + m_verifier = encrypt(m_nonce, m_cipher); +} + +QString Response::toJson() +{ + QVariant result = QJson::QObjectHelper::qobject2qvariant(this, QJson::QObjectHelper::Flag_None); + + QJson::Serializer s; + s.setIndentMode(QJson::IndentCompact); + return s.serialize(result); +} + +KeepassHttpProtocol::RequestType Response::requestType() const +{ + return parseRequest(m_requestType); +} + +QString Response::requestTypeStr() const +{ + return m_requestType; +} + +QString Response::verifier() const +{ + return m_verifier; +} + +QString Response::nonce() const +{ + return m_nonce; +} + +QVariant Response::count() const +{ + return m_count < 0 ? QVariant() : QVariant(m_count); +} + +void Response::setCount(int count) +{ + m_count = count; +} + +QVariant Response::getEntries() const +{ + if (m_count < 0 || m_entries.isEmpty()) + return QVariant(); + + QList res; + res.reserve(m_entries.size()); + Q_FOREACH (const Entry &entry, m_entries) + res.append(QJson::QObjectHelper::qobject2qvariant(&entry, QJson::QObjectHelper::Flag_None)); + return res; +} + +void Response::setEntries(const QList &entries) +{ + Q_ASSERT(m_cipher.isValid()); + + m_count = entries.count(); + + QList encryptedEntries; + encryptedEntries.reserve(m_count); + Q_FOREACH (const Entry &entry, entries) { + Entry encryptedEntry(encrypt(entry.name(), m_cipher), + encrypt(entry.login(), m_cipher), + entry.password().isNull() ? QString() : encrypt(entry.password(), m_cipher), + encrypt(entry.uuid(), m_cipher)); + Q_FOREACH (const StringField & field, entry.stringFields()) + encryptedEntry.addStringField(encrypt(field.key(), m_cipher), + encrypt(field.value(), m_cipher)); + encryptedEntries << encryptedEntry; + } + m_entries = encryptedEntries; +} + +QString Response::hash() const +{ + return m_hash; +} + +QString Response::version() const +{ + return m_version; +} + +QString Response::id() const +{ + return m_id; +} + +void Response::setId(const QString &id) +{ + m_id = id; +} + +bool Response::success() const +{ + return m_success; +} + +void Response::setSuccess() +{ + m_success = true; +} + +QString Response::error() const +{ + return m_error; +} + +void Response::setError(const QString &error) +{ + m_success = false; + m_error = error; +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Entry +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Entry::Entry() +{} + +Entry::Entry(QString name, QString login, QString password, QString uuid): + m_login(login), + m_password(password), + m_uuid(uuid), + m_name(name) +{} + +Entry::Entry(const Entry & other): + QObject(), + m_login(other.m_login), + m_password(other.m_password), + m_uuid(other.m_uuid), + m_name(other.m_name), + m_stringFields(other.m_stringFields) +{} + +Entry & Entry::operator=(const Entry & other) +{ + m_login = other.m_login; + m_password = other.m_password; + m_uuid = other.m_uuid; + m_name = other.m_name; + m_stringFields = other.m_stringFields; + return *this; +} + +QString Entry::login() const +{ + return m_login; +} + +QString Entry::name() const +{ + return m_name; +} + +QString Entry::uuid() const +{ + return m_uuid; +} + +QString Entry::password() const +{ + return m_password; +} + +QList Entry::stringFields() const +{ + return m_stringFields; +} + +void Entry::addStringField(const QString &key, const QString &value) +{ + m_stringFields.append(StringField(key, value)); +} + +QVariant Entry::getStringFields() const +{ + if (m_stringFields.isEmpty()) + return QVariant(); + + QList res; + res.reserve(m_stringFields.size()); + Q_FOREACH (const StringField &stringfield, m_stringFields) + res.append(QJson::QObjectHelper::qobject2qvariant(&stringfield, QJson::QObjectHelper::Flag_None)); + return res; +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// StringField +//////////////////////////////////////////////////////////////////////////////////////////////////// + +StringField::StringField() +{} + +StringField::StringField(const QString &key, const QString &value): + m_key(key), m_value(value) +{} + +StringField::StringField(const StringField &other): + QObject(NULL), m_key(other.m_key), m_value(other.m_value) +{} + +StringField &StringField::operator =(const StringField &other) +{ + m_key = other.m_key; + m_value = other.m_value; + return *this; +} + +QString StringField::key() const +{ + return m_key; +} + +QString StringField::value() const +{ + return m_value; +} diff --git a/src/http/Protocol.h b/src/http/Protocol.h new file mode 100644 index 000000000..ee4fdeb61 --- /dev/null +++ b/src/http/Protocol.h @@ -0,0 +1,208 @@ +/** + *************************************************************************** + * @file Response.h + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#ifndef RESPONSE_H +#define RESPONSE_H + +#include +#include +#include +#include +#include "crypto/SymmetricCipherGcrypt.h" + +namespace KeepassHttpProtocol { + +enum RequestType { + INVALID = -1, + GET_LOGINS, + GET_LOGINS_COUNT, + GET_ALL_LOGINS, + SET_LOGIN, + ASSOCIATE, + TEST_ASSOCIATE, + GENERATE_PASSWORD +}; + +//TODO: use QByteArray whenever possible? + +class Request : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString RequestType READ requestTypeStr WRITE setRequestType ) + Q_PROPERTY(bool SortSelection READ sortSelection WRITE setSortSelection) + Q_PROPERTY(QString Login READ login WRITE setLogin ) + Q_PROPERTY(QString Password READ password WRITE setPassword ) + Q_PROPERTY(QString Uuid READ uuid WRITE setUuid ) + Q_PROPERTY(QString Url READ url WRITE setUrl ) + Q_PROPERTY(QString SubmitUrl READ submitUrl WRITE setSubmitUrl ) + Q_PROPERTY(QString Key READ key WRITE setKey ) + Q_PROPERTY(QString Id READ id WRITE setId ) + Q_PROPERTY(QString Verifier READ verifier WRITE setVerifier ) + Q_PROPERTY(QString Nonce READ nonce WRITE setNonce ) + Q_PROPERTY(QString Realm READ realm WRITE setRealm ) + +public: + Request(); + bool fromJson(QString text); + + KeepassHttpProtocol::RequestType requestType() const; + QString requestTypeStr() const; + bool sortSelection() const; + QString login() const; + QString password() const; + QString uuid() const; + QString url() const; + QString submitUrl() const; + QString key() const; + QString id() const; + QString verifier() const; + QString nonce() const; + QString realm() const; + bool CheckVerifier(const QString & key) const; + +private: + void setRequestType(const QString &requestType); + void setSortSelection(bool sortSelection); + void setLogin(const QString &login); + void setPassword(const QString &password); + void setUuid(const QString &uuid); + void setUrl(const QString &url); + void setSubmitUrl(const QString &submitUrl); + void setKey(const QString &key); + void setId(const QString &id); + void setVerifier(const QString &verifier); + void setNonce(const QString &nonce); + void setRealm(const QString &realm); + + QString m_requestType; + bool m_sortSelection; + QString m_login; + QString m_password; + QString m_uuid; + QString m_url; + QString m_submitUrl; + QString m_key; + QString m_id; + QString m_verifier; + QString m_nonce; + QString m_realm; + mutable SymmetricCipherGcrypt m_cipher; +}; + +class StringField : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString Key READ key ) + Q_PROPERTY(QString Value READ value) + +public: + StringField(); + StringField(const QString& key, const QString& value); + StringField(const StringField & other); + StringField &operator =(const StringField &other); + + QString key() const; + QString value() const; + +private: + QString m_key; + QString m_value; +}; + +class Entry : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString Login READ login ) + Q_PROPERTY(QString Password READ password ) + Q_PROPERTY(QString Uuid READ uuid ) + Q_PROPERTY(QString Name READ name ) + Q_PROPERTY(QVariant StringFields READ getStringFields) + +public: + Entry(); + Entry(QString name, QString login, QString password, QString uuid); + Entry(const Entry & other); + Entry &operator =(const Entry &other); + + QString login() const; + QString password() const; + QString uuid() const; + QString name() const; + QList stringFields() const; + void addStringField(const QString& key, const QString& value); + +private: + QVariant getStringFields() const; + + QString m_login; + QString m_password; + QString m_uuid; + QString m_name; + QList m_stringFields; +}; + +class Response : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString RequestType READ requestTypeStr) + Q_PROPERTY(QString Error READ error ) + Q_PROPERTY(bool Success READ success ) + Q_PROPERTY(QString Id READ id ) + Q_PROPERTY(QString Version READ version ) + Q_PROPERTY(QString Hash READ hash ) + Q_PROPERTY(QVariant Count READ count ) + Q_PROPERTY(QVariant Entries READ getEntries ) + Q_PROPERTY(QString Nonce READ nonce ) + Q_PROPERTY(QString Verifier READ verifier ) + +public: + Response(const Request &request, QString hash); + + KeepassHttpProtocol::RequestType requestType() const; + QString error() const; + void setError(const QString &error = QString()); + bool success() const; + void setSuccess(); + QString id() const; + void setId(const QString &id); + QString version() const; + QString hash() const; + QVariant count() const; + void setCount(int count); + QVariant getEntries() const; + void setEntries(const QList &entries); + QString nonce() const; + QString verifier() const; + void setVerifier(QString key); + + QString toJson(); + +private: + QString requestTypeStr() const; + + QString m_requestType; + QString m_error; + bool m_success; + QString m_id; + int m_count; + QString m_version; + QString m_hash; + QList m_entries; + QString m_nonce; + QString m_verifier; + mutable SymmetricCipherGcrypt m_cipher; +}; + +}/*namespace KeepassHttpProtocol*/ + +#endif // RESPONSE_H diff --git a/src/http/Server.cpp b/src/http/Server.cpp new file mode 100644 index 000000000..1afdd5051 --- /dev/null +++ b/src/http/Server.cpp @@ -0,0 +1,327 @@ +/** + *************************************************************************** + * @file Server.cpp + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#include "Server.h" +#include +#include "Protocol.h" +#include "HttpSettings.h" +#include "crypto/Crypto.h" +#include +#include +#include +#include + +using namespace KeepassHttpProtocol; + +using namespace KeepassHttpProtocol; + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Request +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Server::Server(QObject *parent) : + QObject(parent), + m_started(false) +{ + connect(this, SIGNAL(emitRequest(const QByteArray, QByteArray*)), + this, SLOT(handleRequest(const QByteArray, QByteArray*))); + connect(this, SIGNAL(emitOpenDatabase(bool*)), + this, SLOT(handleOpenDatabase(bool*))); +} + + +void Server::testAssociate(const Request& r, Response * protocolResp) +{ + if (r.id().isEmpty()) + return; //ping + + QString key = getKey(r.id()); + if (key.isEmpty() || !r.CheckVerifier(key)) + return; + + protocolResp->setSuccess(); + protocolResp->setId(r.id()); + protocolResp->setVerifier(key); +} + +void Server::associate(const Request& r, Response * protocolResp) +{ + if (!r.CheckVerifier(r.key())) + return; + + QString id = storeKey(r.key()); + if (id.isEmpty()) + return; + + protocolResp->setSuccess(); + protocolResp->setId(id); + protocolResp->setVerifier(r.key()); +} + +void Server::getLogins(const Request &r, Response *protocolResp) +{ + QString key = getKey(r.id()); + if (!r.CheckVerifier(key)) + return; + + protocolResp->setSuccess(); + protocolResp->setId(r.id()); + protocolResp->setVerifier(key); + QList entries = findMatchingEntries(r.id(), r.url(), r.submitUrl(), r.realm()); //TODO: filtering, request confirmation [in db adaptation layer?] + if (r.sortSelection()) { + //TODO: sorting (in db adaptation layer? here?) + } + protocolResp->setEntries(entries); +} + +void Server::getLoginsCount(const Request &r, Response *protocolResp) +{ + QString key = getKey(r.id()); + if (!r.CheckVerifier(key)) + return; + + protocolResp->setSuccess(); + protocolResp->setId(r.id()); + protocolResp->setVerifier(key); + protocolResp->setCount(countMatchingEntries(r.id(), r.url(), r.submitUrl(), r.realm())); +} + +void Server::getAllLogins(const Request &r, Response *protocolResp) +{ + QString key = getKey(r.id()); + if (!r.CheckVerifier(key)) + return; + + protocolResp->setSuccess(); + protocolResp->setId(r.id()); + protocolResp->setVerifier(key); + protocolResp->setEntries(searchAllEntries(r.id())); //TODO: ensure there is no password --> change API? +} + +void Server::setLogin(const Request &r, Response *protocolResp) +{ + QString key = getKey(r.id()); + if (!r.CheckVerifier(key)) + return; + + QString uuid = r.uuid(); + if (uuid.isEmpty()) + addEntry(r.id(), r.login(), r.password(), r.url(), r.submitUrl(), r.realm()); + else + updateEntry(r.id(), r.uuid(), r.login(), r.password(), r.url()); + + protocolResp->setSuccess(); + protocolResp->setId(r.id()); + protocolResp->setVerifier(key); +} + + +int Server::send_response(struct MHD_Connection *connection, const char *page) +{ + int ret; + struct MHD_Response *response; + + response = MHD_create_response_from_buffer( + strlen(page), static_cast(const_cast(page)), + MHD_RESPMEM_PERSISTENT); + if (!response) return MHD_NO; + + MHD_add_response_header (response, "Content-Type", "application/json"); + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); + + return ret; +} + + +int Server::send_unavailable(struct MHD_Connection *connection) +{ + int ret; + struct MHD_Response *response; + + response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT); + if (!response) return MHD_NO; + + ret = MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, response); + MHD_destroy_response (response); + + return ret; +} + +void Server::generatePassword(const Request &r, Response *protocolResp) +{ + QString key = getKey(r.id()); + if (!r.CheckVerifier(key)) + return; + + QString password = generatePassword(); + QString bits = QString::number(HttpSettings::getbits()); + + protocolResp->setSuccess(); + protocolResp->setId(r.id()); + protocolResp->setVerifier(key); + protocolResp->setEntries(QList() << Entry("generate-password", bits, password, "generate-password")); + + memset(password.data(), 0, password.length()); +} + + +int Server::request_handler_wrapper(void *me, struct MHD_Connection *connection, + const char *url, const char *method, const char *version, + const char *upload_data, size_t *upload_data_size, void **con_cls) +{ + Server *myself = static_cast(me); + + if (myself) + return myself->request_handler(connection, url, method, version, + upload_data, upload_data_size, con_cls); + else + return MHD_NO; +} + + +void Server::handleRequest(const QByteArray in, QByteArray *out) +{ + *out = QByteArray(); + + Request r; + if (!r.fromJson(in)) + return; + + QByteArray hash = QCryptographicHash::hash( + (getDatabaseRootUuid() + getDatabaseRecycleBinUuid()).toUtf8(), + QCryptographicHash::Sha1).toHex(); + + Response protocolResp(r, QString::fromAscii(hash)); + switch(r.requestType()) { + case INVALID: break; + case TEST_ASSOCIATE: testAssociate(r, &protocolResp); break; + case ASSOCIATE: associate(r, &protocolResp); break; + case GENERATE_PASSWORD: generatePassword(r, &protocolResp); break; + case GET_LOGINS: getLogins(r, &protocolResp); break; + case GET_LOGINS_COUNT: getLoginsCount(r, &protocolResp); break; + case GET_ALL_LOGINS: getAllLogins(r, &protocolResp); break; + case SET_LOGIN: setLogin(r, &protocolResp); break; + } + + *out = protocolResp.toJson().toUtf8(); + Q_EMIT donewrk(); +} + + +void Server::handleOpenDatabase(bool *success) +{ + *success = openDatabase(); + Q_EMIT donewrk(); +} + + +int Server::request_handler(struct MHD_Connection *connection, + const char *, const char *method, const char *, + const char *upload_data, size_t *upload_data_size, void **con_cls) +{ + struct Server::connection_info_struct *con_info = + static_cast(*con_cls); + + if (!isDatabaseOpened()) { + bool success; + QEventLoop loop1; + loop1.connect(this, SIGNAL(donewrk()), SLOT(quit())); + Q_EMIT emitOpenDatabase(&success); + loop1.exec(); + + if (!success) + return send_unavailable(connection); + } + + if (con_info == NULL) { + *con_cls = calloc(1, sizeof(*con_info)); + return MHD_YES; + } + + if (strcmp (method, MHD_HTTP_METHOD_POST) != 0) + return MHD_NO; + + if (*upload_data_size == 0) { + if (con_info && con_info->response) + return send_response(connection, con_info->response); + else + return MHD_NO; + } + + QString type = MHD_lookup_connection_value(connection, + MHD_HEADER_KIND, "Content-Type"); + if (!type.contains("application/json", Qt::CaseInsensitive)) + return MHD_NO; + + // Now process the POST request + + QByteArray post = QByteArray(upload_data, *upload_data_size); + + QByteArray s; + QEventLoop loop; + loop.connect(this, SIGNAL(donewrk()), SLOT(quit())); + Q_EMIT emitRequest(post, &s); + loop.exec(); + + if (s.size() == 0) + return MHD_NO; + + con_info->response = static_cast(calloc(1, s.size()+1)); + memcpy(con_info->response, s.data(), s.size()); + + *upload_data_size = 0; + + return MHD_YES; +} + + +void Server::request_completed(void *, struct MHD_Connection *, + void **con_cls, enum MHD_RequestTerminationCode) +{ + struct Server::connection_info_struct *con_info = + static_cast(*con_cls); + + if (con_info == NULL) + return; + + if (con_info->response) free(con_info->response); + free(con_info); + *con_cls = NULL; +} + + +void Server::start(void) +{ + if (m_started) return; + + static const int PORT = 19455; + + daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, + &this->request_handler_wrapper, this, + MHD_OPTION_NOTIFY_COMPLETED, + this->request_completed, NULL, + MHD_OPTION_END); + m_started = true; +} + + +void Server::stop(void) +{ + if (!m_started) + return; + + MHD_stop_daemon(daemon); + m_started = false; +} diff --git a/src/http/Server.h b/src/http/Server.h new file mode 100644 index 000000000..c093556a5 --- /dev/null +++ b/src/http/Server.h @@ -0,0 +1,93 @@ +/** + *************************************************************************** + * @file Server.h + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#ifndef SERVER_H +#define SERVER_H + +#include +#include +#include + + +namespace KeepassHttpProtocol { + +class Request; +class Response; +class Entry; + +class Server : public QObject +{ + Q_OBJECT +public: + explicit Server(QObject *parent = 0); + + //TODO: use QByteArray? + virtual bool isDatabaseOpened() const = 0; + virtual bool openDatabase() = 0; + virtual QString getDatabaseRootUuid() = 0; + virtual QString getDatabaseRecycleBinUuid() = 0; + virtual QString getKey(const QString &id) = 0; + virtual QString storeKey(const QString &key) = 0; + virtual QList findMatchingEntries(const QString &id, const QString &url, const QString & submitUrl, const QString & realm) = 0; + virtual int countMatchingEntries(const QString &id, const QString &url, const QString & submitUrl, const QString & realm) = 0; + virtual QList searchAllEntries(const QString &id) = 0; + virtual void addEntry(const QString &id, const QString &login, const QString &password, const QString &url, const QString &submitUrl, const QString &realm) = 0; + virtual void updateEntry(const QString &id, const QString &uuid, const QString &login, const QString &password, const QString &url) = 0; + virtual QString generatePassword() = 0; + +public Q_SLOTS: + void start(); + void stop(); + +private Q_SLOTS: + void handleRequest(const QByteArray in, QByteArray *out); + void handleOpenDatabase(bool *success); + +Q_SIGNALS: + void emitRequest(const QByteArray in, QByteArray *out); + void emitOpenDatabase(bool *success); + void donewrk(); + +private: + void testAssociate(const KeepassHttpProtocol::Request &r, KeepassHttpProtocol::Response *protocolResp); + void associate(const KeepassHttpProtocol::Request &r, KeepassHttpProtocol::Response *protocolResp); + void getLogins(const KeepassHttpProtocol::Request &r, KeepassHttpProtocol::Response *protocolResp); + void getLoginsCount(const KeepassHttpProtocol::Request &r, KeepassHttpProtocol::Response *protocolResp); + void getAllLogins(const KeepassHttpProtocol::Request &r, KeepassHttpProtocol::Response *protocolResp); + void setLogin(const KeepassHttpProtocol::Request &r, KeepassHttpProtocol::Response *protocolResp); + void generatePassword(const KeepassHttpProtocol::Request &r, KeepassHttpProtocol::Response *protocolResp); + + static int request_handler_wrapper(void *me, + struct MHD_Connection *connection, + const char *url, const char *method, const char *version, + const char *upload_data, size_t *upload_data_size, void **con_cls); + static void request_completed(void *, struct MHD_Connection *, + void **con_cls, enum MHD_RequestTerminationCode); + + int request_handler(struct MHD_Connection *connection, + const char *, const char *method, const char *, + const char *upload_data, size_t *upload_data_size, void **con_cls); + int send_response(struct MHD_Connection *connection, const char *page); + int send_unavailable(struct MHD_Connection *connection); + + bool m_started; + struct MHD_Daemon *daemon; + + struct connection_info_struct { + char *response; + }; +}; + +} /*namespace KeepassHttpProtocol*/ + +#endif // SERVER_H diff --git a/src/http/Service.cpp b/src/http/Service.cpp new file mode 100644 index 000000000..525b28778 --- /dev/null +++ b/src/http/Service.cpp @@ -0,0 +1,573 @@ +/** + *************************************************************************** + * @file Service.cpp + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#include +#include +#include +#include + +#include "Service.h" +#include "Protocol.h" +#include "EntryConfig.h" +#include "AccessControlDialog.h" +#include "HttpSettings.h" + +#include "core/Database.h" +#include "core/Entry.h" +#include "core/Group.h" +#include "core/Metadata.h" +#include "core/Uuid.h" +#include "core/PasswordGenerator.h" + +static const unsigned char KEEPASSHTTP_UUID_DATA[] = { + 0x34, 0x69, 0x7a, 0x40, 0x8a, 0x5b, 0x41, 0xc0, + 0x9f, 0x36, 0x89, 0x7d, 0x62, 0x3e, 0xcb, 0x31 +}; +static const Uuid KEEPASSHTTP_UUID = Uuid(QByteArray::fromRawData(reinterpret_cast(KEEPASSHTTP_UUID_DATA), sizeof(KEEPASSHTTP_UUID_DATA))); +static const char KEEPASSHTTP_NAME[] = "KeePassHttp Settings"; +static const char ASSOCIATE_KEY_PREFIX[] = "AES Key: "; +static const char KEEPASSHTTP_GROUP_NAME[] = "KeePassHttp Passwords"; //Group where new KeePassHttp password are stored +static int KEEPASSHTTP_DEFAULT_ICON = 1; +//private const int DEFAULT_NOTIFICATION_TIME = 5000; + +Service::Service(DatabaseTabWidget* parent) : + KeepassHttpProtocol::Server(parent), + m_dbTabWidget(parent) +{ + if (HttpSettings::isEnabled()) + start(); +} + +Entry* Service::getConfigEntry(bool create) +{ + if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget()) + if (Database * db = dbWidget->database()) { + Entry* entry = db->resolveEntry(KEEPASSHTTP_UUID); + if (!entry && create) { + entry = new Entry(); + entry->setTitle(QLatin1String(KEEPASSHTTP_NAME)); + entry->setUuid(KEEPASSHTTP_UUID); + entry->setAutoTypeEnabled(false); + entry->setGroup(db->rootGroup()); + } else if (entry && entry->group() == db->metadata()->recycleBin()) { + if (create) + entry->setGroup(db->rootGroup()); + else + entry = NULL; + } + return entry; + } + return NULL; +} + +bool Service::isDatabaseOpened() const +{ + if (DatabaseWidget* dbWidget = m_dbTabWidget->currentDatabaseWidget()) + switch(dbWidget->currentMode()) { + case DatabaseWidget::None: + case DatabaseWidget::LockedMode: + case DatabaseWidget::OpenMode: + break; + + case DatabaseWidget::ViewMode: + case DatabaseWidget::EditMode: + return true; + } + return false; +} + +bool Service::openDatabase() +{ + if (!HttpSettings::unlockDatabase()) + return false; + if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget()) { + switch(dbWidget->currentMode()) { + case DatabaseWidget::None: + case DatabaseWidget::LockedMode: + case DatabaseWidget::OpenMode: + break; + + case DatabaseWidget::ViewMode: + case DatabaseWidget::EditMode: + return true; + } + } + //if (HttpSettings::showNotification() + // && !ShowNotification(QString("%0: %1 is requesting access, click to allow or deny") + // .arg(id).arg(submitHost.isEmpty() ? host : submithost)); + // return false; + m_dbTabWidget->activateWindow(); + //Wait a bit for DB to be open... (w/ asynchronous reply?) + return false; +} + +QString Service::getDatabaseRootUuid() +{ + if (DatabaseWidget* dbWidget = m_dbTabWidget->currentDatabaseWidget()) + if (Database* db = dbWidget->database()) + if (Group* rootGroup = db->rootGroup()) + return rootGroup->uuid().toHex(); + return QString(); +} + +QString Service::getDatabaseRecycleBinUuid() +{ + if (DatabaseWidget* dbWidget = m_dbTabWidget->currentDatabaseWidget()) + if (Database* db = dbWidget->database()) + if (Group* recycleBin = db->metadata()->recycleBin()) + return recycleBin->uuid().toHex(); + return QString(); +} + +QString Service::getKey(const QString &id) +{ + if (Entry* config = getConfigEntry()) + return config->attributes()->value(QLatin1String(ASSOCIATE_KEY_PREFIX) + id); + return QString(); +} + +QString Service::storeKey(const QString &key) +{ + QString id; + if (Entry* config = getConfigEntry(true)) { + + //ShowNotification("New key association requested") + + do { + bool ok; + //Indicate who wants to associate, and request user to enter the 'name' of association key + id = QInputDialog::getText(0, + tr("KeyPassX/Http: New key association request"), + tr("You have received an association " + "request for the above key.\n" + "If you would like to allow it access " + "to your KeePassX database\n" + "give it a unique name to identify and accept it."), + QLineEdit::Normal, QString(), &ok); + if (!ok || id.isEmpty()) + return QString(); + + //Warn if association key already exists + } while(config->attributes()->contains(QLatin1String(ASSOCIATE_KEY_PREFIX) + id) && + QMessageBox::warning(0, tr("KeyPassX/Http: Overwrite existing key?"), + tr("A shared encryption-key with the name \"%1\" already exists.\nDo you want to overwrite it?").arg(id), + QMessageBox::Yes | QMessageBox::No) == QMessageBox::No); + + config->attributes()->set(QLatin1String(ASSOCIATE_KEY_PREFIX) + id, key, true); + } + return id; +} + +bool Service::matchUrlScheme(const QString & url) +{ + QString str = url.left(8).toLower(); + return str.startsWith("http://") || + str.startsWith("https://") || + str.startsWith("ftp://") || + str.startsWith("ftps://"); +} + +bool Service::removeFirstDomain(QString & hostname) +{ + int pos = hostname.indexOf("."); + if (pos < 0) + return false; + hostname = hostname.mid(pos + 1); + return !hostname.isEmpty(); +} + +QList Service::searchEntries(Database* db, const QString& hostname) +{ + QList entries; + if (Group* rootGroup = db->rootGroup()) + Q_FOREACH (Entry* entry, rootGroup->search(hostname, Qt::CaseInsensitive)) { + QString title = entry->title(); + QString url = entry->url(); + + //Filter to match hostname in Title and Url fields + if ( (!title.isEmpty() && hostname.contains(title)) + || (!url.isEmpty() && hostname.contains(url)) + || (matchUrlScheme(title) && hostname.endsWith(QUrl(title).host())) + || (matchUrlScheme(url) && hostname.endsWith(QUrl(url).host())) ) + entries.append(entry); + } + return entries; +} + +QList Service::searchEntries(const QString& text) +{ + //Get the list of databases to search + QList databases; + if (HttpSettings::searchInAllDatabases()) { + for (int i = 0; i < m_dbTabWidget->count(); i++) + if (DatabaseWidget* dbWidget = qobject_cast(m_dbTabWidget->widget(i))) + if (Database* db = dbWidget->database()) + databases << db; + } + else if (DatabaseWidget* dbWidget = m_dbTabWidget->currentDatabaseWidget()) { + if (Database* db = dbWidget->database()) + databases << db; + } + + //Search entries matching the hostname + QString hostname = QUrl(text).host(); + QList entries; + do { + Q_FOREACH (Database* db, databases) + entries << searchEntries(db, hostname); + } while(entries.isEmpty() && removeFirstDomain(hostname)); + + return entries; +} + +Service::Access Service::checkAccess(const Entry *entry, const QString & host, const QString & submitHost, const QString & realm) +{ + EntryConfig config; + if (!config.load(entry)) + return Unknown; //not configured + if ((config.isAllowed(host)) && (submitHost.isEmpty() || config.isAllowed(submitHost))) + return Allowed; //allowed + if ((config.isDenied(host)) || (!submitHost.isEmpty() && config.isDenied(submitHost))) + return Denied; //denied + if (!realm.isEmpty() && config.realm() != realm) + return Denied; + return Unknown; //not configured for this host +} + +KeepassHttpProtocol::Entry Service::prepareEntry(const Entry* entry) +{ + KeepassHttpProtocol::Entry res(entry->title(), entry->username(), entry->password(), entry->uuid().toHex()); + if (HttpSettings::supportKphFields()) { + const EntryAttributes * attr = entry->attributes(); + Q_FOREACH (const QString& key, attr->keys()) + if (key.startsWith(QLatin1String("KPH: "))) + res.addStringField(key, attr->value(key)); + } + return res; +} + +int Service::sortPriority(const Entry* entry, const QString& host, const QString& submitUrl, const QString& baseSubmitUrl) const +{ + QUrl url(entry->url()); + if (url.scheme().isEmpty()) + url.setScheme("http"); + const QString entryURL = url.toString(QUrl::StripTrailingSlash); + const QString baseEntryURL = url.toString(QUrl::StripTrailingSlash | QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment); + + if (submitUrl == entryURL) + return 100; + if (submitUrl.startsWith(entryURL) && entryURL != host && baseSubmitUrl != entryURL) + return 90; + if (submitUrl.startsWith(baseEntryURL) && entryURL != host && baseSubmitUrl != baseEntryURL) + return 80; + if (entryURL == host) + return 70; + if (entryURL == baseSubmitUrl) + return 60; + if (entryURL.startsWith(submitUrl)) + return 50; + if (entryURL.startsWith(baseSubmitUrl) && baseSubmitUrl != host) + return 40; + if (submitUrl.startsWith(entryURL)) + return 30; + if (submitUrl.startsWith(baseEntryURL)) + return 20; + if (entryURL.startsWith(host)) + return 10; + if (host.startsWith(entryURL)) + return 5; + return 0; +} + +class Service::SortEntries +{ +public: + SortEntries(const QHash& priorities, const QString & field): + m_priorities(priorities), m_field(field) + {} + + bool operator()(const Entry* left, const Entry* right) const + { + int res = m_priorities.value(left) - m_priorities.value(right); + if (res == 0) + return QString::localeAwareCompare(left->attributes()->value(m_field), right->attributes()->value(m_field)) < 0; + return res < 0; + } + +private: + const QHash& m_priorities; + const QString m_field; +}; + +QList Service::findMatchingEntries(const QString& /*id*/, const QString& url, const QString& submitUrl, const QString& realm) +{ + const bool alwaysAllowAccess = HttpSettings::alwaysAllowAccess(); + const QString host = QUrl(url).host(); + const QString submitHost = QUrl(submitUrl).host(); + + //Check entries for authorization + QList pwEntriesToConfirm; + QList pwEntries; + Q_FOREACH (Entry * entry, searchEntries(url)) { + switch(checkAccess(entry, host, submitHost, realm)) { + case Denied: + continue; + + case Unknown: + if (alwaysAllowAccess) + pwEntries.append(entry); + else + pwEntriesToConfirm.append(entry); + break; + + case Allowed: + pwEntries.append(entry); + break; + } + } + + //If unsure, ask user for confirmation + //if (!pwEntriesToConfirm.isEmpty() + // && HttpSettings::showNotification() + // && !ShowNotification(QString("%0: %1 is requesting access, click to allow or deny") + // .arg(id).arg(submitHost.isEmpty() ? host : submithost)); + // pwEntriesToConfirm.clear(); //timeout --> do not request confirmation + + if (!pwEntriesToConfirm.isEmpty()) { + + AccessControlDialog dlg; + dlg.setUrl(url); + dlg.setItems(pwEntriesToConfirm); + //dlg.setRemember(); //TODO: setting! + + int res = dlg.exec(); + if (dlg.remember()) { + Q_FOREACH (Entry * entry, pwEntriesToConfirm) { + EntryConfig config; + config.load(entry); + if (res == QDialog::Accepted) { + config.allow(host); + if (!submitHost.isEmpty() && host != submitHost) + config.allow(submitHost); + } else if (res == QDialog::Rejected) { + config.deny(host); + if (!submitHost.isEmpty() && host != submitHost) + config.deny(submitHost); + } + if (!realm.isEmpty()) + config.setRealm(realm); + config.save(entry); + } + } + if (res == QDialog::Accepted) + pwEntries.append(pwEntriesToConfirm); + } + + //Sort results + const bool sortSelection = true; + if (sortSelection) { + QUrl url(submitUrl); + if (url.scheme().isEmpty()) + url.setScheme("http"); + const QString submitUrl = url.toString(QUrl::StripTrailingSlash); + const QString baseSubmitURL = url.toString(QUrl::StripTrailingSlash | QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment); + + //Cache priorities + QHash priorities; + priorities.reserve(pwEntries.size()); + Q_FOREACH (const Entry * entry, pwEntries) + priorities.insert(entry, sortPriority(entry, host, submitUrl, baseSubmitURL)); + + //Sort by priorities + qSort(pwEntries.begin(), pwEntries.end(), SortEntries(priorities, HttpSettings::sortByTitle() ? "Title" : "UserName")); + } + + //if (pwEntries.count() > 0) + //{ + // var names = (from e in resp.Entries select e.Name).Distinct(); + // var n = String.Join("\n ", names.ToArray()); + // if (HttpSettings::receiveCredentialNotification()) + // ShowNotification(QString("%0: %1 is receiving credentials for:\n%2").arg(Id).arg(host).arg(n))); + //} + + //Fill the list + QList result; + result.reserve(pwEntries.count()); + Q_FOREACH (Entry * entry, pwEntries) + result << prepareEntry(entry); + return result; +} + +int Service::countMatchingEntries(const QString &, const QString &url, const QString &, const QString &) +{ + return searchEntries(url).count(); +} + +QList Service::searchAllEntries(const QString &) +{ + QList result; + if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget()) + if (Database * db = dbWidget->database()) + if (Group * rootGroup = db->rootGroup()) + Q_FOREACH (Entry * entry, rootGroup->entriesRecursive()) + if (!entry->url().isEmpty() || QUrl(entry->title()).isValid()) + result << KeepassHttpProtocol::Entry(entry->title(), entry->username(), QString(), entry->uuid().toHex()); + return result; +} + +Group * Service::findCreateAddEntryGroup() +{ + if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget()) + if (Database * db = dbWidget->database()) + if (Group * rootGroup = db->rootGroup()) { + const QString groupName = QLatin1String(KEEPASSHTTP_GROUP_NAME);//TODO: setting to decide where new keys are created + + Q_FOREACH (const Group * g, rootGroup->groupsRecursive(true)) + if (g->name() == groupName) + return db->resolveGroup(g->uuid()); + + Group * group; + group = new Group(); + group->setUuid(Uuid::random()); + group->setName(groupName); + group->setIcon(KEEPASSHTTP_DEFAULT_ICON); + group->setParent(rootGroup); + return group; + } + return NULL; +} + +void Service::addEntry(const QString &, const QString &login, const QString &password, const QString &url, const QString &submitUrl, const QString &realm) +{ + if (Group * group = findCreateAddEntryGroup()) { + Entry * entry = new Entry(); + entry->setUuid(Uuid::random()); + entry->setTitle(QUrl(url).host()); + entry->setUrl(url); + entry->setIcon(KEEPASSHTTP_DEFAULT_ICON); + entry->setUsername(login); + entry->setPassword(password); + entry->setGroup(group); + + const QString host = QUrl(url).host(); + const QString submitHost = QUrl(submitUrl).host(); + EntryConfig config; + config.allow(host); + if (!submitHost.isEmpty()) + config.allow(submitHost); + if (!realm.isEmpty()) + config.setRealm(realm); + config.save(entry); + } +} + +void Service::updateEntry(const QString &, const QString &uuid, const QString &login, const QString &password, const QString &url) +{ + if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget()) + if (Database * db = dbWidget->database()) + if (Entry * entry = db->resolveEntry(Uuid::fromHex(uuid))) { + QString u = entry->username(); + if (u != login || entry->password() != password) { + //ShowNotification(QString("%0: You have an entry change prompt waiting, click to activate").arg(requestId)); + if ( HttpSettings::alwaysAllowUpdate() + || QMessageBox::warning(0, tr("KeyPassX/Http: Update Entry"), + tr("Do you want to update the information in %1 - %2?").arg(QUrl(url).host()).arg(u), + QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes ) { + entry->beginUpdate(); + entry->setUsername(login); + entry->setPassword(password); + entry->endUpdate(); + } + } + } +} + +QString Service::generatePassword() +{ + return HttpSettings::generatePassword(); +} + +void Service::removeSharedEncryptionKeys() +{ + if (!isDatabaseOpened()) { + QMessageBox::critical(0, tr("KeyPassX/Http: Database locked!"), + tr("The active database is locked!\n" + "Please unlock the selected database or choose another one which is unlocked."), + QMessageBox::Ok); + } else if (Entry* entry = getConfigEntry()) { + QStringList keysToRemove; + Q_FOREACH (const QString& key, entry->attributes()->keys()) + if (key.startsWith(ASSOCIATE_KEY_PREFIX)) + keysToRemove << key; + + if(keysToRemove.count()) { + entry->beginUpdate(); + Q_FOREACH (const QString& key, keysToRemove) + entry->attributes()->remove(key); + entry->endUpdate(); + + const int count = keysToRemove.count(); + QMessageBox::information(0, tr("KeyPassX/Http: Removed keys from database"), + tr("Successfully removed %1 encryption-%2 from KeePassX/Http Settings.").arg(count).arg(count ? "keys" : "key"), + QMessageBox::Ok); + } else { + QMessageBox::information(0, tr("KeyPassX/Http: No keys found"), + tr("No shared encryption-keys found in KeePassHttp Settings."), + QMessageBox::Ok); + } + } else { + QMessageBox::information(0, tr("KeyPassX/Http: Settings not available!"), + tr("The active database does not contain an entry of KeePassHttp Settings."), + QMessageBox::Ok); + } +} + +void Service::removeStoredPermissions() +{ + if (!isDatabaseOpened()) { + QMessageBox::critical(0, tr("KeyPassX/Http: Database locked!"), + tr("The active database is locked!\n" + "Please unlock the selected database or choose another one which is unlocked."), + QMessageBox::Ok); + } else { + Database * db = m_dbTabWidget->currentDatabaseWidget()->database(); + QList entries = db->rootGroup()->entriesRecursive(); + + QProgressDialog progress(tr("Removing stored permissions..."), tr("Abort"), 0, entries.count()); + progress.setWindowModality(Qt::WindowModal); + + uint counter = 0; + Q_FOREACH (Entry* entry, entries) { + if (progress.wasCanceled()) + return; + if (entry->attributes()->contains(KEEPASSHTTP_NAME)) { + entry->beginUpdate(); + entry->attributes()->remove(KEEPASSHTTP_NAME); + entry->endUpdate(); + counter ++; + } + progress.setValue(progress.value() + 1); + } + progress.reset(); + + if (counter > 0) { + QMessageBox::information(0, tr("KeyPassX/Http: Removed permissions"), + tr("Successfully removed permissions from %1 %2.").arg(counter).arg(counter ? "entries" : "entry"), + QMessageBox::Ok); + } else { + QMessageBox::information(0, tr("KeyPassX/Http: No entry with permissions found!"), + tr("The active database does not contain an entry with permissions."), + QMessageBox::Ok); + } + } +} diff --git a/src/http/Service.h b/src/http/Service.h new file mode 100644 index 000000000..04d1188fe --- /dev/null +++ b/src/http/Service.h @@ -0,0 +1,61 @@ +/** + *************************************************************************** + * @file Service.h + * + * @brief + * + * Copyright (C) 2013 + * + * @author Francois Ferrand + * @date 4/2013 + *************************************************************************** + */ + +#ifndef SERVICE_H +#define SERVICE_H + +#include +#include "gui/DatabaseTabWidget.h" +#include "Server.h" + +class Service : public KeepassHttpProtocol::Server +{ + Q_OBJECT + +public: + explicit Service(DatabaseTabWidget* parent = 0); + + virtual bool isDatabaseOpened() const; + virtual bool openDatabase(); + virtual QString getDatabaseRootUuid(); + virtual QString getDatabaseRecycleBinUuid(); + virtual QString getKey(const QString& id); + virtual QString storeKey(const QString& key); + virtual QList findMatchingEntries(const QString& id, const QString& url, const QString& submitUrl, const QString& realm); + virtual int countMatchingEntries(const QString& id, const QString& url, const QString& submitUrl, const QString& realm); + virtual QList searchAllEntries(const QString& id); + virtual void addEntry(const QString& id, const QString& login, const QString& password, const QString& url, const QString& submitUrl, const QString& realm); + virtual void updateEntry(const QString& id, const QString& uuid, const QString& login, const QString& password, const QString& url); + virtual QString generatePassword(); + +public Q_SLOTS: + void removeSharedEncryptionKeys(); + void removeStoredPermissions(); + +private: + enum Access { Denied, Unknown, Allowed}; + Entry* getConfigEntry(bool create = false); + bool matchUrlScheme(const QString& url); + Access checkAccess(const Entry* entry, const QString& host, const QString& submitHost, const QString& realm); + bool removeFirstDomain(QString& hostname); + Group *findCreateAddEntryGroup(); + class SortEntries; + int sortPriority(const Entry *entry, const QString &host, const QString &submitUrl, const QString &baseSubmitUrl) const; + KeepassHttpProtocol::Entry prepareEntry(const Entry* entry); + QList searchEntries(Database* db, const QString& hostname); + QList searchEntries(const QString& text); + + DatabaseTabWidget * const m_dbTabWidget; +}; + +#endif // SERVICE_H diff --git a/src/http/qhttpserver/CMakeLists.txt b/src/http/qhttpserver/CMakeLists.txt new file mode 100644 index 000000000..932e4b265 --- /dev/null +++ b/src/http/qhttpserver/CMakeLists.txt @@ -0,0 +1,20 @@ + +set(qhttpserver_MOC_HDRS + qhttpserver.h + qhttpresponse.h + qhttprequest.h + qhttpconnection.h +) + +IF (NOT Qt5Core_FOUND) + qt4_wrap_cpp(qhttpserver_MOC_SRCS ${qhttpserver_MOC_HDRS}) +ENDIF() + +set (qhttpserver_SRCS qhttpconnection.cpp qhttprequest.cpp qhttpresponse.cpp qhttpserver.cpp + http-parser/http_parser.c http-parser/url_parser.c) +set (qhttpserver_HEADERS qhttpconnection.h qhttprequest.h qhttpresponse.h qhttpserver.h + http-parser/http_parser.h) + +INCLUDE_DIRECTORIES(http-parser) + +add_library (qhttpserver STATIC ${qhttpserver_SRCS} ${qhttpserver_MOC_SRCS} ${qhttpserver_HEADERS}) diff --git a/src/http/qhttpserver/LICENSE b/src/http/qhttpserver/LICENSE new file mode 100644 index 000000000..3351aecfc --- /dev/null +++ b/src/http/qhttpserver/LICENSE @@ -0,0 +1,19 @@ +Copyright (C) 2011-2012 Nikhil Marathe + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/src/http/qhttpserver/http-parser/AUTHORS b/src/http/qhttpserver/http-parser/AUTHORS new file mode 100644 index 000000000..0bfcf7604 --- /dev/null +++ b/src/http/qhttpserver/http-parser/AUTHORS @@ -0,0 +1,40 @@ +# Authors ordered by first contribution. +Ryan Dahl +Jeremy Hinegardner +Sergey Shepelev +Joe Damato +tomika +Phoenix Sol +Cliff Frey +Ewen Cheslack-Postava +Santiago Gala +Tim Becker +Jeff Terrace +Ben Noordhuis +Nathan Rajlich +Mark Nottingham +Aman Gupta +Tim Becker +Sean Cunningham +Peter Griess +Salman Haq +Cliff Frey +Jon Kolb +Fouad Mardini +Paul Querna +Felix Geisendörfer +koichik +Andre Caron +Ivo Raisr +James McLaughlin +David Gwynne +LE ROUX Thomas +Randy Rizun +Andre Louis Caron +Simon Zimmermann +Erik Dubbelboer +Martell Malone +Bertrand Paquet +BogDan Vatra +Peter Faiman +Corey Richardson diff --git a/src/http/qhttpserver/http-parser/CONTRIBUTIONS b/src/http/qhttpserver/http-parser/CONTRIBUTIONS new file mode 100644 index 000000000..11ba31e4b --- /dev/null +++ b/src/http/qhttpserver/http-parser/CONTRIBUTIONS @@ -0,0 +1,4 @@ +Contributors must agree to the Contributor License Agreement before patches +can be accepted. + +http://spreadsheets2.google.com/viewform?hl=en&formkey=dDJXOGUwbzlYaWM4cHN1MERwQS1CSnc6MQ diff --git a/src/http/qhttpserver/http-parser/LICENSE-MIT b/src/http/qhttpserver/http-parser/LICENSE-MIT new file mode 100644 index 000000000..58010b388 --- /dev/null +++ b/src/http/qhttpserver/http-parser/LICENSE-MIT @@ -0,0 +1,23 @@ +http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright +Igor Sysoev. + +Additional changes are licensed under the same terms as NGINX and +copyright Joyent, Inc. and other Node contributors. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/src/http/qhttpserver/http-parser/README.md b/src/http/qhttpserver/http-parser/README.md new file mode 100644 index 000000000..ef1bf6e90 --- /dev/null +++ b/src/http/qhttpserver/http-parser/README.md @@ -0,0 +1,178 @@ +HTTP Parser +=========== + +This is a parser for HTTP messages written in C. It parses both requests and +responses. The parser is designed to be used in performance HTTP +applications. It does not make any syscalls nor allocations, it does not +buffer data, it can be interrupted at anytime. Depending on your +architecture, it only requires about 40 bytes of data per message +stream (in a web server that is per connection). + +Features: + + * No dependencies + * Handles persistent streams (keep-alive). + * Decodes chunked encoding. + * Upgrade support + * Defends against buffer overflow attacks. + +The parser extracts the following information from HTTP messages: + + * Header fields and values + * Content-Length + * Request method + * Response status code + * Transfer-Encoding + * HTTP version + * Request URL + * Message body + + +Usage +----- + +One `http_parser` object is used per TCP connection. Initialize the struct +using `http_parser_init()` and set the callbacks. That might look something +like this for a request parser: + + http_parser_settings settings; + settings.on_url = my_url_callback; + settings.on_header_field = my_header_field_callback; + /* ... */ + + http_parser *parser = malloc(sizeof(http_parser)); + http_parser_init(parser, HTTP_REQUEST); + parser->data = my_socket; + +When data is received on the socket execute the parser and check for errors. + + size_t len = 80*1024, nparsed; + char buf[len]; + ssize_t recved; + + recved = recv(fd, buf, len, 0); + + if (recved < 0) { + /* Handle error. */ + } + + /* Start up / continue the parser. + * Note we pass recved==0 to signal that EOF has been recieved. + */ + nparsed = http_parser_execute(parser, &settings, buf, recved); + + if (parser->upgrade) { + /* handle new protocol */ + } else if (nparsed != recved) { + /* Handle error. Usually just close the connection. */ + } + +HTTP needs to know where the end of the stream is. For example, sometimes +servers send responses without Content-Length and expect the client to +consume input (for the body) until EOF. To tell http_parser about EOF, give +`0` as the forth parameter to `http_parser_execute()`. Callbacks and errors +can still be encountered during an EOF, so one must still be prepared +to receive them. + +Scalar valued message information such as `status_code`, `method`, and the +HTTP version are stored in the parser structure. This data is only +temporally stored in `http_parser` and gets reset on each new message. If +this information is needed later, copy it out of the structure during the +`headers_complete` callback. + +The parser decodes the transfer-encoding for both requests and responses +transparently. That is, a chunked encoding is decoded before being sent to +the on_body callback. + + +The Special Problem of Upgrade +------------------------------ + +HTTP supports upgrading the connection to a different protocol. An +increasingly common example of this is the Web Socket protocol which sends +a request like + + GET /demo HTTP/1.1 + Upgrade: WebSocket + Connection: Upgrade + Host: example.com + Origin: http://example.com + WebSocket-Protocol: sample + +followed by non-HTTP data. + +(See http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75 for more +information the Web Socket protocol.) + +To support this, the parser will treat this as a normal HTTP message without a +body. Issuing both on_headers_complete and on_message_complete callbacks. However +http_parser_execute() will stop parsing at the end of the headers and return. + +The user is expected to check if `parser->upgrade` has been set to 1 after +`http_parser_execute()` returns. Non-HTTP data begins at the buffer supplied +offset by the return value of `http_parser_execute()`. + + +Callbacks +--------- + +During the `http_parser_execute()` call, the callbacks set in +`http_parser_settings` will be executed. The parser maintains state and +never looks behind, so buffering the data is not necessary. If you need to +save certain data for later usage, you can do that from the callbacks. + +There are two types of callbacks: + +* notification `typedef int (*http_cb) (http_parser*);` + Callbacks: on_message_begin, on_headers_complete, on_message_complete. +* data `typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);` + Callbacks: (requests only) on_uri, + (common) on_header_field, on_header_value, on_body; + +Callbacks must return 0 on success. Returning a non-zero value indicates +error to the parser, making it exit immediately. + +In case you parse HTTP message in chunks (i.e. `read()` request line +from socket, parse, read half headers, parse, etc) your data callbacks +may be called more than once. Http-parser guarantees that data pointer is only +valid for the lifetime of callback. You can also `read()` into a heap allocated +buffer to avoid copying memory around if this fits your application. + +Reading headers may be a tricky task if you read/parse headers partially. +Basically, you need to remember whether last header callback was field or value +and apply following logic: + + (on_header_field and on_header_value shortened to on_h_*) + ------------------------ ------------ -------------------------------------------- + | State (prev. callback) | Callback | Description/action | + ------------------------ ------------ -------------------------------------------- + | nothing (first call) | on_h_field | Allocate new buffer and copy callback data | + | | | into it | + ------------------------ ------------ -------------------------------------------- + | value | on_h_field | New header started. | + | | | Copy current name,value buffers to headers | + | | | list and allocate new buffer for new name | + ------------------------ ------------ -------------------------------------------- + | field | on_h_field | Previous name continues. Reallocate name | + | | | buffer and append callback data to it | + ------------------------ ------------ -------------------------------------------- + | field | on_h_value | Value for current header started. Allocate | + | | | new buffer and copy callback data to it | + ------------------------ ------------ -------------------------------------------- + | value | on_h_value | Value continues. Reallocate value buffer | + | | | and append callback data to it | + ------------------------ ------------ -------------------------------------------- + + +Parsing URLs +------------ + +A simplistic zero-copy URL parser is provided as `http_parser_parse_url()`. +Users of this library may wish to use it to parse URLs constructed from +consecutive `on_url` callbacks. + +See examples of reading in headers: + +* [partial example](http://gist.github.com/155877) in C +* [from http-parser tests](http://github.com/joyent/http-parser/blob/37a0ff8/test.c#L403) in C +* [from Node library](http://github.com/joyent/node/blob/842eaf4/src/http.js#L284) in Javascript diff --git a/src/http/qhttpserver/http-parser/http_parser.c b/src/http/qhttpserver/http-parser/http_parser.c new file mode 100644 index 000000000..c71098249 --- /dev/null +++ b/src/http/qhttpserver/http-parser/http_parser.c @@ -0,0 +1,2174 @@ +/* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev + * + * Additional changes are licensed under the same terms as NGINX and + * copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include "http_parser.h" +#include +#include +#include +#include +#include +#include + +#ifndef ULLONG_MAX +# define ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */ +#endif + +#ifndef MIN +# define MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#endif + +#ifndef BIT_AT +# define BIT_AT(a, i) \ + (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \ + (1 << ((unsigned int) (i) & 7)))) +#endif + +#ifndef ELEM_AT +# define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v)) +#endif + +#define SET_ERRNO(e) \ +do { \ + parser->http_errno = (e); \ +} while(0) + + +/* Run the notify callback FOR, returning ER if it fails */ +#define CALLBACK_NOTIFY_(FOR, ER) \ +do { \ + assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ + \ + if (settings->on_##FOR) { \ + if (0 != settings->on_##FOR(parser)) { \ + SET_ERRNO(HPE_CB_##FOR); \ + } \ + \ + /* We either errored above or got paused; get out */ \ + if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \ + return (ER); \ + } \ + } \ +} while (0) + +/* Run the notify callback FOR and consume the current byte */ +#define CALLBACK_NOTIFY(FOR) CALLBACK_NOTIFY_(FOR, p - data + 1) + +/* Run the notify callback FOR and don't consume the current byte */ +#define CALLBACK_NOTIFY_NOADVANCE(FOR) CALLBACK_NOTIFY_(FOR, p - data) + +/* Run data callback FOR with LEN bytes, returning ER if it fails */ +#define CALLBACK_DATA_(FOR, LEN, ER) \ +do { \ + assert(HTTP_PARSER_ERRNO(parser) == HPE_OK); \ + \ + if (FOR##_mark) { \ + if (settings->on_##FOR) { \ + if (0 != settings->on_##FOR(parser, FOR##_mark, (LEN))) { \ + SET_ERRNO(HPE_CB_##FOR); \ + } \ + \ + /* We either errored above or got paused; get out */ \ + if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { \ + return (ER); \ + } \ + } \ + FOR##_mark = NULL; \ + } \ +} while (0) + +/* Run the data callback FOR and consume the current byte */ +#define CALLBACK_DATA(FOR) \ + CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1) + +/* Run the data callback FOR and don't consume the current byte */ +#define CALLBACK_DATA_NOADVANCE(FOR) \ + CALLBACK_DATA_(FOR, p - FOR##_mark, p - data) + +/* Set the mark FOR; non-destructive if mark is already set */ +#define MARK(FOR) \ +do { \ + if (!FOR##_mark) { \ + FOR##_mark = p; \ + } \ +} while (0) + + +#define PROXY_CONNECTION "proxy-connection" +#define CONNECTION "connection" +#define CONTENT_LENGTH "content-length" +#define TRANSFER_ENCODING "transfer-encoding" +#define UPGRADE "upgrade" +#define CHUNKED "chunked" +#define KEEP_ALIVE "keep-alive" +#define CLOSE "close" + + +static const char *method_strings[] = + { +#define XX(num, name, string) #string, + HTTP_METHOD_MAP(XX) +#undef XX + }; + + +/* Tokens as defined by rfc 2616. Also lowercases them. + * token = 1* + * separators = "(" | ")" | "<" | ">" | "@" + * | "," | ";" | ":" | "\" | <"> + * | "/" | "[" | "]" | "?" | "=" + * | "{" | "}" | SP | HT + */ +static const char tokens[256] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + 0, '!', 0, '#', '$', '%', '&', '\'', +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 0, 0, '*', '+', 0, '-', '.', 0, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + '0', '1', '2', '3', '4', '5', '6', '7', +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + '8', '9', 0, 0, 0, 0, 0, 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 'x', 'y', 'z', 0, 0, 0, '^', '_', +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 'x', 'y', 'z', 0, '|', 0, '~', 0 }; + + +static const int8_t unhex[256] = + {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + }; + + +#if HTTP_PARSER_STRICT +# define T(v) 0 +#else +# define T(v) v +#endif + + +static const uint8_t normal_url_char[32] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128, +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, }; + +#undef T + +enum state + { s_dead = 1 /* important that this is > 0 */ + + , s_start_req_or_res + , s_res_or_resp_H + , s_start_res + , s_res_H + , s_res_HT + , s_res_HTT + , s_res_HTTP + , s_res_first_http_major + , s_res_http_major + , s_res_first_http_minor + , s_res_http_minor + , s_res_first_status_code + , s_res_status_code + , s_res_status + , s_res_line_almost_done + + , s_start_req + + , s_req_method + , s_req_spaces_before_url + , s_req_schema + , s_req_schema_slash + , s_req_schema_slash_slash + , s_req_server_start + , s_req_server + , s_req_server_with_at + , s_req_path + , s_req_query_string_start + , s_req_query_string + , s_req_fragment_start + , s_req_fragment + , s_req_http_start + , s_req_http_H + , s_req_http_HT + , s_req_http_HTT + , s_req_http_HTTP + , s_req_first_http_major + , s_req_http_major + , s_req_first_http_minor + , s_req_http_minor + , s_req_line_almost_done + + , s_header_field_start + , s_header_field + , s_header_value_start + , s_header_value + , s_header_value_lws + + , s_header_almost_done + + , s_chunk_size_start + , s_chunk_size + , s_chunk_parameters + , s_chunk_size_almost_done + + , s_headers_almost_done + , s_headers_done + + /* Important: 's_headers_done' must be the last 'header' state. All + * states beyond this must be 'body' states. It is used for overflow + * checking. See the PARSING_HEADER() macro. + */ + + , s_chunk_data + , s_chunk_data_almost_done + , s_chunk_data_done + + , s_body_identity + , s_body_identity_eof + + , s_message_done + }; + + +#define PARSING_HEADER(state) (state <= s_headers_done) + + +enum header_states + { h_general = 0 + , h_C + , h_CO + , h_CON + + , h_matching_connection + , h_matching_proxy_connection + , h_matching_content_length + , h_matching_transfer_encoding + , h_matching_upgrade + + , h_connection + , h_content_length + , h_transfer_encoding + , h_upgrade + + , h_matching_transfer_encoding_chunked + , h_matching_connection_keep_alive + , h_matching_connection_close + + , h_transfer_encoding_chunked + , h_connection_keep_alive + , h_connection_close + }; + +enum http_host_state + { + s_http_host_dead = 1 + , s_http_userinfo_start + , s_http_userinfo + , s_http_host_start + , s_http_host_v6_start + , s_http_host + , s_http_host_v6 + , s_http_host_v6_end + , s_http_host_port_start + , s_http_host_port +}; + +/* Macros for character classes; depends on strict-mode */ +#define CR '\r' +#define LF '\n' +#define LOWER(c) (unsigned char)(c | 0x20) +#define IS_ALPHA(c) (LOWER(c) >= 'a' && LOWER(c) <= 'z') +#define IS_NUM(c) ((c) >= '0' && (c) <= '9') +#define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c)) +#define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f')) +#define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \ + (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \ + (c) == ')') +#define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \ + (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \ + (c) == '$' || (c) == ',') + +#if HTTP_PARSER_STRICT +#define TOKEN(c) (tokens[(unsigned char)c]) +#define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c)) +#define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-') +#else +#define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c]) +#define IS_URL_CHAR(c) \ + (BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80)) +#define IS_HOST_CHAR(c) \ + (IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_') +#endif + + +#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res) + + +#if HTTP_PARSER_STRICT +# define STRICT_CHECK(cond) \ +do { \ + if (cond) { \ + SET_ERRNO(HPE_STRICT); \ + goto error; \ + } \ +} while (0) +# define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead) +#else +# define STRICT_CHECK(cond) +# define NEW_MESSAGE() start_state +#endif + + +/* Map errno values to strings for human-readable output */ +#define HTTP_STRERROR_GEN(n, s) { "HPE_" #n, s }, +static struct { + const char *name; + const char *description; +} http_strerror_tab[] = { + HTTP_ERRNO_MAP(HTTP_STRERROR_GEN) +}; +#undef HTTP_STRERROR_GEN + +int http_message_needs_eof(const http_parser *parser); + +/* Our URL parser. + * + * This is designed to be shared by http_parser_execute() for URL validation, + * hence it has a state transition + byte-for-byte interface. In addition, it + * is meant to be embedded in http_parser_parse_url(), which does the dirty + * work of turning state transitions URL components for its API. + * + * This function should only be invoked with non-space characters. It is + * assumed that the caller cares about (and can detect) the transition between + * URL and non-URL states by looking for these. + */ +static enum state +parse_url_char(enum state s, const char ch) +{ + if (ch == ' ' || ch == '\r' || ch == '\n') { + return s_dead; + } + +#if HTTP_PARSER_STRICT + if (ch == '\t' || ch == '\f') { + return s_dead; + } +#endif + + switch (s) { + case s_req_spaces_before_url: + /* Proxied requests are followed by scheme of an absolute URI (alpha). + * All methods except CONNECT are followed by '/' or '*'. + */ + + if (ch == '/' || ch == '*') { + return s_req_path; + } + + if (IS_ALPHA(ch)) { + return s_req_schema; + } + + break; + + case s_req_schema: + if (IS_ALPHA(ch)) { + return s; + } + + if (ch == ':') { + return s_req_schema_slash; + } + + break; + + case s_req_schema_slash: + if (ch == '/') { + return s_req_schema_slash_slash; + } + + break; + + case s_req_schema_slash_slash: + if (ch == '/') { + return s_req_server_start; + } + + break; + + case s_req_server_with_at: + if (ch == '@') { + return s_dead; + } + + /* FALLTHROUGH */ + case s_req_server_start: + case s_req_server: + if (ch == '/') { + return s_req_path; + } + + if (ch == '?') { + return s_req_query_string_start; + } + + if (ch == '@') { + return s_req_server_with_at; + } + + if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') { + return s_req_server; + } + + break; + + case s_req_path: + if (IS_URL_CHAR(ch)) { + return s; + } + + switch (ch) { + case '?': + return s_req_query_string_start; + + case '#': + return s_req_fragment_start; + } + + break; + + case s_req_query_string_start: + case s_req_query_string: + if (IS_URL_CHAR(ch)) { + return s_req_query_string; + } + + switch (ch) { + case '?': + /* allow extra '?' in query string */ + return s_req_query_string; + + case '#': + return s_req_fragment_start; + } + + break; + + case s_req_fragment_start: + if (IS_URL_CHAR(ch)) { + return s_req_fragment; + } + + switch (ch) { + case '?': + return s_req_fragment; + + case '#': + return s; + } + + break; + + case s_req_fragment: + if (IS_URL_CHAR(ch)) { + return s; + } + + switch (ch) { + case '?': + case '#': + return s; + } + + break; + + default: + break; + } + + /* We should never fall out of the switch above unless there's an error */ + return s_dead; +} + +size_t http_parser_execute (http_parser *parser, + const http_parser_settings *settings, + const char *data, + size_t len) +{ + char c, ch; + int8_t unhex_val; + const char *p = data; + const char *header_field_mark = 0; + const char *header_value_mark = 0; + const char *url_mark = 0; + const char *body_mark = 0; + + /* We're in an error state. Don't bother doing anything. */ + if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { + return 0; + } + + if (len == 0) { + switch (parser->state) { + case s_body_identity_eof: + /* Use of CALLBACK_NOTIFY() here would erroneously return 1 byte read if + * we got paused. + */ + CALLBACK_NOTIFY_NOADVANCE(message_complete); + return 0; + + case s_dead: + case s_start_req_or_res: + case s_start_res: + case s_start_req: + return 0; + + default: + SET_ERRNO(HPE_INVALID_EOF_STATE); + return 1; + } + } + + + if (parser->state == s_header_field) + header_field_mark = data; + if (parser->state == s_header_value) + header_value_mark = data; + switch (parser->state) { + case s_req_path: + case s_req_schema: + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + case s_req_server: + case s_req_server_with_at: + case s_req_query_string_start: + case s_req_query_string: + case s_req_fragment_start: + case s_req_fragment: + url_mark = data; + break; + } + + for (p=data; p != data + len; p++) { + ch = *p; + + if (PARSING_HEADER(parser->state)) { + ++parser->nread; + /* Buffer overflow attack */ + if (parser->nread > HTTP_MAX_HEADER_SIZE) { + SET_ERRNO(HPE_HEADER_OVERFLOW); + goto error; + } + } + + reexecute_byte: + switch (parser->state) { + + case s_dead: + /* this state is used after a 'Connection: close' message + * the parser will error out if it reads another message + */ + if (ch == CR || ch == LF) + break; + + SET_ERRNO(HPE_CLOSED_CONNECTION); + goto error; + + case s_start_req_or_res: + { + if (ch == CR || ch == LF) + break; + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + if (ch == 'H') { + parser->state = s_res_or_resp_H; + + CALLBACK_NOTIFY(message_begin); + } else { + parser->type = HTTP_REQUEST; + parser->state = s_start_req; + goto reexecute_byte; + } + + break; + } + + case s_res_or_resp_H: + if (ch == 'T') { + parser->type = HTTP_RESPONSE; + parser->state = s_res_HT; + } else { + if (ch != 'E') { + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + + parser->type = HTTP_REQUEST; + parser->method = HTTP_HEAD; + parser->index = 2; + parser->state = s_req_method; + } + break; + + case s_start_res: + { + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + switch (ch) { + case 'H': + parser->state = s_res_H; + break; + + case CR: + case LF: + break; + + default: + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + + CALLBACK_NOTIFY(message_begin); + break; + } + + case s_res_H: + STRICT_CHECK(ch != 'T'); + parser->state = s_res_HT; + break; + + case s_res_HT: + STRICT_CHECK(ch != 'T'); + parser->state = s_res_HTT; + break; + + case s_res_HTT: + STRICT_CHECK(ch != 'P'); + parser->state = s_res_HTTP; + break; + + case s_res_HTTP: + STRICT_CHECK(ch != '/'); + parser->state = s_res_first_http_major; + break; + + case s_res_first_http_major: + if (ch < '0' || ch > '9') { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major = ch - '0'; + parser->state = s_res_http_major; + break; + + /* major HTTP version or dot */ + case s_res_http_major: + { + if (ch == '.') { + parser->state = s_res_first_http_minor; + break; + } + + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major *= 10; + parser->http_major += ch - '0'; + + if (parser->http_major > 999) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + break; + } + + /* first digit of minor HTTP version */ + case s_res_first_http_minor: + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor = ch - '0'; + parser->state = s_res_http_minor; + break; + + /* minor HTTP version or end of request line */ + case s_res_http_minor: + { + if (ch == ' ') { + parser->state = s_res_first_status_code; + break; + } + + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor *= 10; + parser->http_minor += ch - '0'; + + if (parser->http_minor > 999) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + break; + } + + case s_res_first_status_code: + { + if (!IS_NUM(ch)) { + if (ch == ' ') { + break; + } + + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + parser->status_code = ch - '0'; + parser->state = s_res_status_code; + break; + } + + case s_res_status_code: + { + if (!IS_NUM(ch)) { + switch (ch) { + case ' ': + parser->state = s_res_status; + break; + case CR: + parser->state = s_res_line_almost_done; + break; + case LF: + parser->state = s_header_field_start; + break; + default: + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + break; + } + + parser->status_code *= 10; + parser->status_code += ch - '0'; + + if (parser->status_code > 999) { + SET_ERRNO(HPE_INVALID_STATUS); + goto error; + } + + break; + } + + case s_res_status: + /* the human readable status. e.g. "NOT FOUND" + * we are not humans so just ignore this */ + if (ch == CR) { + parser->state = s_res_line_almost_done; + break; + } + + if (ch == LF) { + parser->state = s_header_field_start; + break; + } + break; + + case s_res_line_almost_done: + STRICT_CHECK(ch != LF); + parser->state = s_header_field_start; + break; + + case s_start_req: + { + if (ch == CR || ch == LF) + break; + parser->flags = 0; + parser->content_length = ULLONG_MAX; + + if (!IS_ALPHA(ch)) { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + parser->method = (enum http_method) 0; + parser->index = 1; + switch (ch) { + case 'C': parser->method = HTTP_CONNECT; /* or COPY, CHECKOUT */ break; + case 'D': parser->method = HTTP_DELETE; break; + case 'G': parser->method = HTTP_GET; break; + case 'H': parser->method = HTTP_HEAD; break; + case 'L': parser->method = HTTP_LOCK; break; + case 'M': parser->method = HTTP_MKCOL; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH */ break; + case 'N': parser->method = HTTP_NOTIFY; break; + case 'O': parser->method = HTTP_OPTIONS; break; + case 'P': parser->method = HTTP_POST; + /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */ + break; + case 'R': parser->method = HTTP_REPORT; break; + case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break; + case 'T': parser->method = HTTP_TRACE; break; + case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE */ break; + default: + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + parser->state = s_req_method; + + CALLBACK_NOTIFY(message_begin); + + break; + } + + case s_req_method: + { + const char *matcher; + if (ch == '\0') { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + matcher = method_strings[parser->method]; + if (ch == ' ' && matcher[parser->index] == '\0') { + parser->state = s_req_spaces_before_url; + } else if (ch == matcher[parser->index]) { + ; /* nada */ + } else if (parser->method == HTTP_CONNECT) { + if (parser->index == 1 && ch == 'H') { + parser->method = HTTP_CHECKOUT; + } else if (parser->index == 2 && ch == 'P') { + parser->method = HTTP_COPY; + } else { + goto error; + } + } else if (parser->method == HTTP_MKCOL) { + if (parser->index == 1 && ch == 'O') { + parser->method = HTTP_MOVE; + } else if (parser->index == 1 && ch == 'E') { + parser->method = HTTP_MERGE; + } else if (parser->index == 1 && ch == '-') { + parser->method = HTTP_MSEARCH; + } else if (parser->index == 2 && ch == 'A') { + parser->method = HTTP_MKACTIVITY; + } else { + goto error; + } + } else if (parser->method == HTTP_SUBSCRIBE) { + if (parser->index == 1 && ch == 'E') { + parser->method = HTTP_SEARCH; + } else { + goto error; + } + } else if (parser->index == 1 && parser->method == HTTP_POST) { + if (ch == 'R') { + parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */ + } else if (ch == 'U') { + parser->method = HTTP_PUT; /* or HTTP_PURGE */ + } else if (ch == 'A') { + parser->method = HTTP_PATCH; + } else { + goto error; + } + } else if (parser->index == 2) { + if (parser->method == HTTP_PUT) { + if (ch == 'R') parser->method = HTTP_PURGE; + } else if (parser->method == HTTP_UNLOCK) { + if (ch == 'S') parser->method = HTTP_UNSUBSCRIBE; + } + } else if (parser->index == 4 && parser->method == HTTP_PROPFIND && ch == 'P') { + parser->method = HTTP_PROPPATCH; + } else { + SET_ERRNO(HPE_INVALID_METHOD); + goto error; + } + + ++parser->index; + break; + } + + case s_req_spaces_before_url: + { + if (ch == ' ') break; + + MARK(url); + if (parser->method == HTTP_CONNECT) { + parser->state = s_req_server_start; + } + + parser->state = parse_url_char((enum state)parser->state, ch); + if (parser->state == s_dead) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + + break; + } + + case s_req_schema: + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + { + switch (ch) { + /* No whitespace allowed here */ + case ' ': + case CR: + case LF: + SET_ERRNO(HPE_INVALID_URL); + goto error; + default: + parser->state = parse_url_char((enum state)parser->state, ch); + if (parser->state == s_dead) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + } + + break; + } + + case s_req_server: + case s_req_server_with_at: + case s_req_path: + case s_req_query_string_start: + case s_req_query_string: + case s_req_fragment_start: + case s_req_fragment: + { + switch (ch) { + case ' ': + parser->state = s_req_http_start; + CALLBACK_DATA(url); + break; + case CR: + case LF: + parser->http_major = 0; + parser->http_minor = 9; + parser->state = (ch == CR) ? + s_req_line_almost_done : + s_header_field_start; + CALLBACK_DATA(url); + break; + default: + parser->state = parse_url_char((enum state)parser->state, ch); + if (parser->state == s_dead) { + SET_ERRNO(HPE_INVALID_URL); + goto error; + } + } + break; + } + + case s_req_http_start: + switch (ch) { + case 'H': + parser->state = s_req_http_H; + break; + case ' ': + break; + default: + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; + } + break; + + case s_req_http_H: + STRICT_CHECK(ch != 'T'); + parser->state = s_req_http_HT; + break; + + case s_req_http_HT: + STRICT_CHECK(ch != 'T'); + parser->state = s_req_http_HTT; + break; + + case s_req_http_HTT: + STRICT_CHECK(ch != 'P'); + parser->state = s_req_http_HTTP; + break; + + case s_req_http_HTTP: + STRICT_CHECK(ch != '/'); + parser->state = s_req_first_http_major; + break; + + /* first digit of major HTTP version */ + case s_req_first_http_major: + if (ch < '1' || ch > '9') { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major = ch - '0'; + parser->state = s_req_http_major; + break; + + /* major HTTP version or dot */ + case s_req_http_major: + { + if (ch == '.') { + parser->state = s_req_first_http_minor; + break; + } + + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_major *= 10; + parser->http_major += ch - '0'; + + if (parser->http_major > 999) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + break; + } + + /* first digit of minor HTTP version */ + case s_req_first_http_minor: + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor = ch - '0'; + parser->state = s_req_http_minor; + break; + + /* minor HTTP version or end of request line */ + case s_req_http_minor: + { + if (ch == CR) { + parser->state = s_req_line_almost_done; + break; + } + + if (ch == LF) { + parser->state = s_header_field_start; + break; + } + + /* XXX allow spaces after digit? */ + + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + parser->http_minor *= 10; + parser->http_minor += ch - '0'; + + if (parser->http_minor > 999) { + SET_ERRNO(HPE_INVALID_VERSION); + goto error; + } + + break; + } + + /* end of request line */ + case s_req_line_almost_done: + { + if (ch != LF) { + SET_ERRNO(HPE_LF_EXPECTED); + goto error; + } + + parser->state = s_header_field_start; + break; + } + + case s_header_field_start: + { + if (ch == CR) { + parser->state = s_headers_almost_done; + break; + } + + if (ch == LF) { + /* they might be just sending \n instead of \r\n so this would be + * the second \n to denote the end of headers*/ + parser->state = s_headers_almost_done; + goto reexecute_byte; + } + + c = TOKEN(ch); + + if (!c) { + SET_ERRNO(HPE_INVALID_HEADER_TOKEN); + goto error; + } + + MARK(header_field); + + parser->index = 0; + parser->state = s_header_field; + + switch (c) { + case 'c': + parser->header_state = h_C; + break; + + case 'p': + parser->header_state = h_matching_proxy_connection; + break; + + case 't': + parser->header_state = h_matching_transfer_encoding; + break; + + case 'u': + parser->header_state = h_matching_upgrade; + break; + + default: + parser->header_state = h_general; + break; + } + break; + } + + case s_header_field: + { + c = TOKEN(ch); + + if (c) { + switch (parser->header_state) { + case h_general: + break; + + case h_C: + parser->index++; + parser->header_state = (c == 'o' ? h_CO : h_general); + break; + + case h_CO: + parser->index++; + parser->header_state = (c == 'n' ? h_CON : h_general); + break; + + case h_CON: + parser->index++; + switch (c) { + case 'n': + parser->header_state = h_matching_connection; + break; + case 't': + parser->header_state = h_matching_content_length; + break; + default: + parser->header_state = h_general; + break; + } + break; + + /* connection */ + + case h_matching_connection: + parser->index++; + if (parser->index > sizeof(CONNECTION)-1 + || c != CONNECTION[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CONNECTION)-2) { + parser->header_state = h_connection; + } + break; + + /* proxy-connection */ + + case h_matching_proxy_connection: + parser->index++; + if (parser->index > sizeof(PROXY_CONNECTION)-1 + || c != PROXY_CONNECTION[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(PROXY_CONNECTION)-2) { + parser->header_state = h_connection; + } + break; + + /* content-length */ + + case h_matching_content_length: + parser->index++; + if (parser->index > sizeof(CONTENT_LENGTH)-1 + || c != CONTENT_LENGTH[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CONTENT_LENGTH)-2) { + parser->header_state = h_content_length; + } + break; + + /* transfer-encoding */ + + case h_matching_transfer_encoding: + parser->index++; + if (parser->index > sizeof(TRANSFER_ENCODING)-1 + || c != TRANSFER_ENCODING[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(TRANSFER_ENCODING)-2) { + parser->header_state = h_transfer_encoding; + } + break; + + /* upgrade */ + + case h_matching_upgrade: + parser->index++; + if (parser->index > sizeof(UPGRADE)-1 + || c != UPGRADE[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(UPGRADE)-2) { + parser->header_state = h_upgrade; + } + break; + + case h_connection: + case h_content_length: + case h_transfer_encoding: + case h_upgrade: + if (ch != ' ') parser->header_state = h_general; + break; + + default: + assert(0 && "Unknown header_state"); + break; + } + break; + } + + if (ch == ':') { + parser->state = s_header_value_start; + CALLBACK_DATA(header_field); + break; + } + + if (ch == CR) { + parser->state = s_header_almost_done; + CALLBACK_DATA(header_field); + break; + } + + if (ch == LF) { + parser->state = s_header_field_start; + CALLBACK_DATA(header_field); + break; + } + + SET_ERRNO(HPE_INVALID_HEADER_TOKEN); + goto error; + } + + case s_header_value_start: + { + if (ch == ' ' || ch == '\t') break; + + MARK(header_value); + + parser->state = s_header_value; + parser->index = 0; + + if (ch == CR) { + parser->header_state = h_general; + parser->state = s_header_almost_done; + CALLBACK_DATA(header_value); + break; + } + + if (ch == LF) { + parser->state = s_header_field_start; + CALLBACK_DATA(header_value); + break; + } + + c = LOWER(ch); + + switch (parser->header_state) { + case h_upgrade: + parser->flags |= F_UPGRADE; + parser->header_state = h_general; + break; + + case h_transfer_encoding: + /* looking for 'Transfer-Encoding: chunked' */ + if ('c' == c) { + parser->header_state = h_matching_transfer_encoding_chunked; + } else { + parser->header_state = h_general; + } + break; + + case h_content_length: + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + } + + parser->content_length = ch - '0'; + break; + + case h_connection: + /* looking for 'Connection: keep-alive' */ + if (c == 'k') { + parser->header_state = h_matching_connection_keep_alive; + /* looking for 'Connection: close' */ + } else if (c == 'c') { + parser->header_state = h_matching_connection_close; + } else { + parser->header_state = h_general; + } + break; + + default: + parser->header_state = h_general; + break; + } + break; + } + + case s_header_value: + { + + if (ch == CR) { + parser->state = s_header_almost_done; + CALLBACK_DATA(header_value); + break; + } + + if (ch == LF) { + parser->state = s_header_almost_done; + CALLBACK_DATA_NOADVANCE(header_value); + goto reexecute_byte; + } + + c = LOWER(ch); + + switch (parser->header_state) { + case h_general: + break; + + case h_connection: + case h_transfer_encoding: + assert(0 && "Shouldn't get here."); + break; + + case h_content_length: + { + uint64_t t; + + if (ch == ' ') break; + + if (!IS_NUM(ch)) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + } + + t = parser->content_length; + t *= 10; + t += ch - '0'; + + /* Overflow? */ + if (t < parser->content_length || t == ULLONG_MAX) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + } + + parser->content_length = t; + break; + } + + /* Transfer-Encoding: chunked */ + case h_matching_transfer_encoding_chunked: + parser->index++; + if (parser->index > sizeof(CHUNKED)-1 + || c != CHUNKED[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CHUNKED)-2) { + parser->header_state = h_transfer_encoding_chunked; + } + break; + + /* looking for 'Connection: keep-alive' */ + case h_matching_connection_keep_alive: + parser->index++; + if (parser->index > sizeof(KEEP_ALIVE)-1 + || c != KEEP_ALIVE[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(KEEP_ALIVE)-2) { + parser->header_state = h_connection_keep_alive; + } + break; + + /* looking for 'Connection: close' */ + case h_matching_connection_close: + parser->index++; + if (parser->index > sizeof(CLOSE)-1 || c != CLOSE[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CLOSE)-2) { + parser->header_state = h_connection_close; + } + break; + + case h_transfer_encoding_chunked: + case h_connection_keep_alive: + case h_connection_close: + if (ch != ' ') parser->header_state = h_general; + break; + + default: + parser->state = s_header_value; + parser->header_state = h_general; + break; + } + break; + } + + case s_header_almost_done: + { + STRICT_CHECK(ch != LF); + + parser->state = s_header_value_lws; + + switch (parser->header_state) { + case h_connection_keep_alive: + parser->flags |= F_CONNECTION_KEEP_ALIVE; + break; + case h_connection_close: + parser->flags |= F_CONNECTION_CLOSE; + break; + case h_transfer_encoding_chunked: + parser->flags |= F_CHUNKED; + break; + default: + break; + } + + break; + } + + case s_header_value_lws: + { + if (ch == ' ' || ch == '\t') + parser->state = s_header_value_start; + else + { + parser->state = s_header_field_start; + goto reexecute_byte; + } + break; + } + + case s_headers_almost_done: + { + STRICT_CHECK(ch != LF); + + if (parser->flags & F_TRAILING) { + /* End of a chunked request */ + parser->state = NEW_MESSAGE(); + CALLBACK_NOTIFY(message_complete); + break; + } + + parser->state = s_headers_done; + + /* Set this here so that on_headers_complete() callbacks can see it */ + parser->upgrade = + (parser->flags & F_UPGRADE || parser->method == HTTP_CONNECT); + + /* Here we call the headers_complete callback. This is somewhat + * different than other callbacks because if the user returns 1, we + * will interpret that as saying that this message has no body. This + * is needed for the annoying case of recieving a response to a HEAD + * request. + * + * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so + * we have to simulate it by handling a change in errno below. + */ + if (settings->on_headers_complete) { + switch (settings->on_headers_complete(parser)) { + case 0: + break; + + case 1: + parser->flags |= F_SKIPBODY; + break; + + default: + SET_ERRNO(HPE_CB_headers_complete); + return p - data; /* Error */ + } + } + + if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { + return p - data; + } + + goto reexecute_byte; + } + + case s_headers_done: + { + STRICT_CHECK(ch != LF); + + parser->nread = 0; + + /* Exit, the rest of the connect is in a different protocol. */ + if (parser->upgrade) { + parser->state = NEW_MESSAGE(); + CALLBACK_NOTIFY(message_complete); + return (p - data) + 1; + } + + if (parser->flags & F_SKIPBODY) { + parser->state = NEW_MESSAGE(); + CALLBACK_NOTIFY(message_complete); + } else if (parser->flags & F_CHUNKED) { + /* chunked encoding - ignore Content-Length header */ + parser->state = s_chunk_size_start; + } else { + if (parser->content_length == 0) { + /* Content-Length header given but zero: Content-Length: 0\r\n */ + parser->state = NEW_MESSAGE(); + CALLBACK_NOTIFY(message_complete); + } else if (parser->content_length != ULLONG_MAX) { + /* Content-Length header given and non-zero */ + parser->state = s_body_identity; + } else { + if (parser->type == HTTP_REQUEST || + !http_message_needs_eof(parser)) { + /* Assume content-length 0 - read the next */ + parser->state = NEW_MESSAGE(); + CALLBACK_NOTIFY(message_complete); + } else { + /* Read body until EOF */ + parser->state = s_body_identity_eof; + } + } + } + + break; + } + + case s_body_identity: + { + uint64_t to_read = MIN(parser->content_length, + (uint64_t) ((data + len) - p)); + + assert(parser->content_length != 0 + && parser->content_length != ULLONG_MAX); + + /* The difference between advancing content_length and p is because + * the latter will automaticaly advance on the next loop iteration. + * Further, if content_length ends up at 0, we want to see the last + * byte again for our message complete callback. + */ + MARK(body); + parser->content_length -= to_read; + p += to_read - 1; + + if (parser->content_length == 0) { + parser->state = s_message_done; + + /* Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte. + * + * The alternative to doing this is to wait for the next byte to + * trigger the data callback, just as in every other case. The + * problem with this is that this makes it difficult for the test + * harness to distinguish between complete-on-EOF and + * complete-on-length. It's not clear that this distinction is + * important for applications, but let's keep it for now. + */ + CALLBACK_DATA_(body, p - body_mark + 1, p - data); + goto reexecute_byte; + } + + break; + } + + /* read until EOF */ + case s_body_identity_eof: + MARK(body); + p = data + len - 1; + + break; + + case s_message_done: + parser->state = NEW_MESSAGE(); + CALLBACK_NOTIFY(message_complete); + break; + + case s_chunk_size_start: + { + assert(parser->nread == 1); + assert(parser->flags & F_CHUNKED); + + unhex_val = unhex[(unsigned char)ch]; + if (unhex_val == -1) { + SET_ERRNO(HPE_INVALID_CHUNK_SIZE); + goto error; + } + + parser->content_length = unhex_val; + parser->state = s_chunk_size; + break; + } + + case s_chunk_size: + { + uint64_t t; + + assert(parser->flags & F_CHUNKED); + + if (ch == CR) { + parser->state = s_chunk_size_almost_done; + break; + } + + unhex_val = unhex[(unsigned char)ch]; + + if (unhex_val == -1) { + if (ch == ';' || ch == ' ') { + parser->state = s_chunk_parameters; + break; + } + + SET_ERRNO(HPE_INVALID_CHUNK_SIZE); + goto error; + } + + t = parser->content_length; + t *= 16; + t += unhex_val; + + /* Overflow? */ + if (t < parser->content_length || t == ULLONG_MAX) { + SET_ERRNO(HPE_INVALID_CONTENT_LENGTH); + goto error; + } + + parser->content_length = t; + break; + } + + case s_chunk_parameters: + { + assert(parser->flags & F_CHUNKED); + /* just ignore this shit. TODO check for overflow */ + if (ch == CR) { + parser->state = s_chunk_size_almost_done; + break; + } + break; + } + + case s_chunk_size_almost_done: + { + assert(parser->flags & F_CHUNKED); + STRICT_CHECK(ch != LF); + + parser->nread = 0; + + if (parser->content_length == 0) { + parser->flags |= F_TRAILING; + parser->state = s_header_field_start; + } else { + parser->state = s_chunk_data; + } + break; + } + + case s_chunk_data: + { + uint64_t to_read = MIN(parser->content_length, + (uint64_t) ((data + len) - p)); + + assert(parser->flags & F_CHUNKED); + assert(parser->content_length != 0 + && parser->content_length != ULLONG_MAX); + + /* See the explanation in s_body_identity for why the content + * length and data pointers are managed this way. + */ + MARK(body); + parser->content_length -= to_read; + p += to_read - 1; + + if (parser->content_length == 0) { + parser->state = s_chunk_data_almost_done; + } + + break; + } + + case s_chunk_data_almost_done: + assert(parser->flags & F_CHUNKED); + assert(parser->content_length == 0); + STRICT_CHECK(ch != CR); + parser->state = s_chunk_data_done; + CALLBACK_DATA(body); + break; + + case s_chunk_data_done: + assert(parser->flags & F_CHUNKED); + STRICT_CHECK(ch != LF); + parser->nread = 0; + parser->state = s_chunk_size_start; + break; + + default: + assert(0 && "unhandled state"); + SET_ERRNO(HPE_INVALID_INTERNAL_STATE); + goto error; + } + } + + /* Run callbacks for any marks that we have leftover after we ran our of + * bytes. There should be at most one of these set, so it's OK to invoke + * them in series (unset marks will not result in callbacks). + * + * We use the NOADVANCE() variety of callbacks here because 'p' has already + * overflowed 'data' and this allows us to correct for the off-by-one that + * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p' + * value that's in-bounds). + */ + + assert(((header_field_mark ? 1 : 0) + + (header_value_mark ? 1 : 0) + + (url_mark ? 1 : 0) + + (body_mark ? 1 : 0)) <= 1); + + CALLBACK_DATA_NOADVANCE(header_field); + CALLBACK_DATA_NOADVANCE(header_value); + CALLBACK_DATA_NOADVANCE(url); + CALLBACK_DATA_NOADVANCE(body); + + return len; + +error: + if (HTTP_PARSER_ERRNO(parser) == HPE_OK) { + SET_ERRNO(HPE_UNKNOWN); + } + + return (p - data); +} + + +/* Does the parser need to see an EOF to find the end of the message? */ +int +http_message_needs_eof (const http_parser *parser) +{ + if (parser->type == HTTP_REQUEST) { + return 0; + } + + /* See RFC 2616 section 4.4 */ + if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */ + parser->status_code == 204 || /* No Content */ + parser->status_code == 304 || /* Not Modified */ + parser->flags & F_SKIPBODY) { /* response to a HEAD request */ + return 0; + } + + if ((parser->flags & F_CHUNKED) || parser->content_length != ULLONG_MAX) { + return 0; + } + + return 1; +} + + +int +http_should_keep_alive (const http_parser *parser) +{ + if (parser->http_major > 0 && parser->http_minor > 0) { + /* HTTP/1.1 */ + if (parser->flags & F_CONNECTION_CLOSE) { + return 0; + } + } else { + /* HTTP/1.0 or earlier */ + if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) { + return 0; + } + } + + return !http_message_needs_eof(parser); +} + + +const char * +http_method_str (enum http_method m) +{ + return ELEM_AT(method_strings, m, ""); +} + + +void +http_parser_init (http_parser *parser, enum http_parser_type t) +{ + void *data = parser->data; /* preserve application data */ + memset(parser, 0, sizeof(*parser)); + parser->data = data; + parser->type = t; + parser->state = (t == HTTP_REQUEST ? s_start_req : (t == HTTP_RESPONSE ? s_start_res : s_start_req_or_res)); + parser->http_errno = HPE_OK; +} + +const char * +http_errno_name(enum http_errno err) { + assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0]))); + return http_strerror_tab[err].name; +} + +const char * +http_errno_description(enum http_errno err) { + assert(err < (sizeof(http_strerror_tab)/sizeof(http_strerror_tab[0]))); + return http_strerror_tab[err].description; +} + +static enum http_host_state +http_parse_host_char(enum http_host_state s, const char ch) { + switch(s) { + case s_http_userinfo: + case s_http_userinfo_start: + if (ch == '@') { + return s_http_host_start; + } + + if (IS_USERINFO_CHAR(ch)) { + return s_http_userinfo; + } + break; + + case s_http_host_start: + if (ch == '[') { + return s_http_host_v6_start; + } + + if (IS_HOST_CHAR(ch)) { + return s_http_host; + } + + break; + + case s_http_host: + if (IS_HOST_CHAR(ch)) { + return s_http_host; + } + + /* FALLTHROUGH */ + case s_http_host_v6_end: + if (ch == ':') { + return s_http_host_port_start; + } + + break; + + case s_http_host_v6: + if (ch == ']') { + return s_http_host_v6_end; + } + + /* FALLTHROUGH */ + case s_http_host_v6_start: + if (IS_HEX(ch) || ch == ':' || ch == '.') { + return s_http_host_v6; + } + + break; + + case s_http_host_port: + case s_http_host_port_start: + if (IS_NUM(ch)) { + return s_http_host_port; + } + + break; + + default: + break; + } + return s_http_host_dead; +} + +static int +http_parse_host(const char * buf, struct http_parser_url *u, int found_at) { + enum http_host_state s; + + const char *p; + size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len; + + u->field_data[UF_HOST].len = 0; + + s = found_at ? s_http_userinfo_start : s_http_host_start; + + for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) { + enum http_host_state new_s = http_parse_host_char(s, *p); + + if (new_s == s_http_host_dead) { + return 1; + } + + switch(new_s) { + case s_http_host: + if (s != s_http_host) { + u->field_data[UF_HOST].off = p - buf; + } + u->field_data[UF_HOST].len++; + break; + + case s_http_host_v6: + if (s != s_http_host_v6) { + u->field_data[UF_HOST].off = p - buf; + } + u->field_data[UF_HOST].len++; + break; + + case s_http_host_port: + if (s != s_http_host_port) { + u->field_data[UF_PORT].off = p - buf; + u->field_data[UF_PORT].len = 0; + u->field_set |= (1 << UF_PORT); + } + u->field_data[UF_PORT].len++; + break; + + case s_http_userinfo: + if (s != s_http_userinfo) { + u->field_data[UF_USERINFO].off = p - buf ; + u->field_data[UF_USERINFO].len = 0; + u->field_set |= (1 << UF_USERINFO); + } + u->field_data[UF_USERINFO].len++; + break; + + default: + break; + } + s = new_s; + } + + /* Make sure we don't end somewhere unexpected */ + switch (s) { + case s_http_host_start: + case s_http_host_v6_start: + case s_http_host_v6: + case s_http_host_port_start: + case s_http_userinfo: + case s_http_userinfo_start: + return 1; + default: + break; + } + + return 0; +} + +int +http_parser_parse_url(const char *buf, size_t buflen, int is_connect, + struct http_parser_url *u) +{ + enum state s; + const char *p; + enum http_parser_url_fields uf, old_uf; + int found_at = 0; + + u->port = u->field_set = 0; + s = is_connect ? s_req_server_start : s_req_spaces_before_url; + uf = old_uf = UF_MAX; + + for (p = buf; p < buf + buflen; p++) { + s = parse_url_char(s, *p); + + /* Figure out the next field that we're operating on */ + switch (s) { + case s_dead: + return 1; + + /* Skip delimeters */ + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + case s_req_query_string_start: + case s_req_fragment_start: + continue; + + case s_req_schema: + uf = UF_SCHEMA; + break; + + case s_req_server_with_at: + found_at = 1; + + /* FALLTROUGH */ + case s_req_server: + uf = UF_HOST; + break; + + case s_req_path: + uf = UF_PATH; + break; + + case s_req_query_string: + uf = UF_QUERY; + break; + + case s_req_fragment: + uf = UF_FRAGMENT; + break; + + default: + assert(!"Unexpected state"); + return 1; + } + + /* Nothing's changed; soldier on */ + if (uf == old_uf) { + u->field_data[uf].len++; + continue; + } + + u->field_data[uf].off = p - buf; + u->field_data[uf].len = 1; + + u->field_set |= (1 << uf); + old_uf = uf; + } + + /* host must be present if there is a schema */ + /* parsing http:///toto will fail */ + if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) { + if (http_parse_host(buf, u, found_at) != 0) { + return 1; + } + } + + /* CONNECT requests can only contain "hostname:port" */ + if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) { + return 1; + } + + if (u->field_set & (1 << UF_PORT)) { + /* Don't bother with endp; we've already validated the string */ + unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10); + + /* Ports have a max value of 2^16 */ + if (v > 0xffff) { + return 1; + } + + u->port = (uint16_t) v; + } + + return 0; +} + +void +http_parser_pause(http_parser *parser, int paused) { + /* Users should only be pausing/unpausing a parser that is not in an error + * state. In non-debug builds, there's not much that we can do about this + * other than ignore it. + */ + if (HTTP_PARSER_ERRNO(parser) == HPE_OK || + HTTP_PARSER_ERRNO(parser) == HPE_PAUSED) { + SET_ERRNO((paused) ? HPE_PAUSED : HPE_OK); + } else { + assert(0 && "Attempting to pause parser in error state"); + } +} + +int +http_body_is_final(const struct http_parser *parser) { + return parser->state == s_message_done; +} diff --git a/src/http/qhttpserver/http-parser/http_parser.gyp b/src/http/qhttpserver/http-parser/http_parser.gyp new file mode 100644 index 000000000..ef34ecaea --- /dev/null +++ b/src/http/qhttpserver/http-parser/http_parser.gyp @@ -0,0 +1,111 @@ +# This file is used with the GYP meta build system. +# http://code.google.com/p/gyp/ +# To build try this: +# svn co http://gyp.googlecode.com/svn/trunk gyp +# ./gyp/gyp -f make --depth=`pwd` http_parser.gyp +# ./out/Debug/test +{ + 'target_defaults': { + 'default_configuration': 'Debug', + 'configurations': { + # TODO: hoist these out and put them somewhere common, because + # RuntimeLibrary MUST MATCH across the entire project + 'Debug': { + 'defines': [ 'DEBUG', '_DEBUG' ], + 'cflags': [ '-Wall', '-Wextra', '-O0', '-g', '-ftrapv' ], + 'msvs_settings': { + 'VCCLCompilerTool': { + 'RuntimeLibrary': 1, # static debug + }, + }, + }, + 'Release': { + 'defines': [ 'NDEBUG' ], + 'cflags': [ '-Wall', '-Wextra', '-O3' ], + 'msvs_settings': { + 'VCCLCompilerTool': { + 'RuntimeLibrary': 0, # static release + }, + }, + } + }, + 'msvs_settings': { + 'VCCLCompilerTool': { + }, + 'VCLibrarianTool': { + }, + 'VCLinkerTool': { + 'GenerateDebugInformation': 'true', + }, + }, + 'conditions': [ + ['OS == "win"', { + 'defines': [ + 'WIN32' + ], + }] + ], + }, + + 'targets': [ + { + 'target_name': 'http_parser', + 'type': 'static_library', + 'include_dirs': [ '.' ], + 'direct_dependent_settings': { + 'defines': [ 'HTTP_PARSER_STRICT=0' ], + 'include_dirs': [ '.' ], + }, + 'defines': [ 'HTTP_PARSER_STRICT=0' ], + 'sources': [ './http_parser.c', ], + 'conditions': [ + ['OS=="win"', { + 'msvs_settings': { + 'VCCLCompilerTool': { + # Compile as C++. http_parser.c is actually C99, but C++ is + # close enough in this case. + 'CompileAs': 2, + }, + }, + }] + ], + }, + + { + 'target_name': 'http_parser_strict', + 'type': 'static_library', + 'include_dirs': [ '.' ], + 'direct_dependent_settings': { + 'defines': [ 'HTTP_PARSER_STRICT=1' ], + 'include_dirs': [ '.' ], + }, + 'defines': [ 'HTTP_PARSER_STRICT=1' ], + 'sources': [ './http_parser.c', ], + 'conditions': [ + ['OS=="win"', { + 'msvs_settings': { + 'VCCLCompilerTool': { + # Compile as C++. http_parser.c is actually C99, but C++ is + # close enough in this case. + 'CompileAs': 2, + }, + }, + }] + ], + }, + + { + 'target_name': 'test-nonstrict', + 'type': 'executable', + 'dependencies': [ 'http_parser' ], + 'sources': [ 'test.c' ] + }, + + { + 'target_name': 'test-strict', + 'type': 'executable', + 'dependencies': [ 'http_parser_strict' ], + 'sources': [ 'test.c' ] + } + ] +} diff --git a/src/http/qhttpserver/http-parser/http_parser.h b/src/http/qhttpserver/http-parser/http_parser.h new file mode 100644 index 000000000..f1d605d6f --- /dev/null +++ b/src/http/qhttpserver/http-parser/http_parser.h @@ -0,0 +1,302 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#ifndef http_parser_h +#define http_parser_h +#ifdef __cplusplus +extern "C" { +#endif + +#define HTTP_PARSER_VERSION_MAJOR 2 +#define HTTP_PARSER_VERSION_MINOR 0 + +#include +#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600) +#include +#include +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#else +#include +#endif + +/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run + * faster + */ +#ifndef HTTP_PARSER_STRICT +# define HTTP_PARSER_STRICT 1 +#endif + +/* Maximium header size allowed */ +#define HTTP_MAX_HEADER_SIZE (80*1024) + + +typedef struct http_parser http_parser; +typedef struct http_parser_settings http_parser_settings; + + +/* Callbacks should return non-zero to indicate an error. The parser will + * then halt execution. + * + * The one exception is on_headers_complete. In a HTTP_RESPONSE parser + * returning '1' from on_headers_complete will tell the parser that it + * should not expect a body. This is used when receiving a response to a + * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding: + * chunked' headers that indicate the presence of a body. + * + * http_data_cb does not return data chunks. It will be call arbitrarally + * many times for each string. E.G. you might get 10 callbacks for "on_url" + * each providing just a few characters more data. + */ +typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); +typedef int (*http_cb) (http_parser*); + + +/* Request Methods */ +#define HTTP_METHOD_MAP(XX) \ + XX(0, DELETE, DELETE) \ + XX(1, GET, GET) \ + XX(2, HEAD, HEAD) \ + XX(3, POST, POST) \ + XX(4, PUT, PUT) \ + /* pathological */ \ + XX(5, CONNECT, CONNECT) \ + XX(6, OPTIONS, OPTIONS) \ + XX(7, TRACE, TRACE) \ + /* webdav */ \ + XX(8, COPY, COPY) \ + XX(9, LOCK, LOCK) \ + XX(10, MKCOL, MKCOL) \ + XX(11, MOVE, MOVE) \ + XX(12, PROPFIND, PROPFIND) \ + XX(13, PROPPATCH, PROPPATCH) \ + XX(14, SEARCH, SEARCH) \ + XX(15, UNLOCK, UNLOCK) \ + /* subversion */ \ + XX(16, REPORT, REPORT) \ + XX(17, MKACTIVITY, MKACTIVITY) \ + XX(18, CHECKOUT, CHECKOUT) \ + XX(19, MERGE, MERGE) \ + /* upnp */ \ + XX(20, MSEARCH, M-SEARCH) \ + XX(21, NOTIFY, NOTIFY) \ + XX(22, SUBSCRIBE, SUBSCRIBE) \ + XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \ + /* RFC-5789 */ \ + XX(24, PATCH, PATCH) \ + XX(25, PURGE, PURGE) \ + +enum http_method + { +#define XX(num, name, string) HTTP_##name = num, + HTTP_METHOD_MAP(XX) +#undef XX + }; + + +enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH }; + + +/* Flag values for http_parser.flags field */ +enum flags + { F_CHUNKED = 1 << 0 + , F_CONNECTION_KEEP_ALIVE = 1 << 1 + , F_CONNECTION_CLOSE = 1 << 2 + , F_TRAILING = 1 << 3 + , F_UPGRADE = 1 << 4 + , F_SKIPBODY = 1 << 5 + }; + + +/* Map for errno-related constants + * + * The provided argument should be a macro that takes 2 arguments. + */ +#define HTTP_ERRNO_MAP(XX) \ + /* No error */ \ + XX(OK, "success") \ + \ + /* Callback-related errors */ \ + XX(CB_message_begin, "the on_message_begin callback failed") \ + XX(CB_url, "the on_url callback failed") \ + XX(CB_header_field, "the on_header_field callback failed") \ + XX(CB_header_value, "the on_header_value callback failed") \ + XX(CB_headers_complete, "the on_headers_complete callback failed") \ + XX(CB_body, "the on_body callback failed") \ + XX(CB_message_complete, "the on_message_complete callback failed") \ + \ + /* Parsing-related errors */ \ + XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \ + XX(HEADER_OVERFLOW, \ + "too many header bytes seen; overflow detected") \ + XX(CLOSED_CONNECTION, \ + "data received after completed connection: close message") \ + XX(INVALID_VERSION, "invalid HTTP version") \ + XX(INVALID_STATUS, "invalid HTTP status code") \ + XX(INVALID_METHOD, "invalid HTTP method") \ + XX(INVALID_URL, "invalid URL") \ + XX(INVALID_HOST, "invalid host") \ + XX(INVALID_PORT, "invalid port") \ + XX(INVALID_PATH, "invalid path") \ + XX(INVALID_QUERY_STRING, "invalid query string") \ + XX(INVALID_FRAGMENT, "invalid fragment") \ + XX(LF_EXPECTED, "LF character expected") \ + XX(INVALID_HEADER_TOKEN, "invalid character in header") \ + XX(INVALID_CONTENT_LENGTH, \ + "invalid character in content-length header") \ + XX(INVALID_CHUNK_SIZE, \ + "invalid character in chunk size header") \ + XX(INVALID_CONSTANT, "invalid constant string") \ + XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\ + XX(STRICT, "strict mode assertion failed") \ + XX(PAUSED, "parser is paused") \ + XX(UNKNOWN, "an unknown error occurred") + + +/* Define HPE_* values for each errno value above */ +#define HTTP_ERRNO_GEN(n, s) HPE_##n, +enum http_errno { + HTTP_ERRNO_MAP(HTTP_ERRNO_GEN) +}; +#undef HTTP_ERRNO_GEN + + +/* Get an http_errno value from an http_parser */ +#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno) + + +struct http_parser { + /** PRIVATE **/ + unsigned char type : 2; /* enum http_parser_type */ + unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */ + unsigned char state; /* enum state from http_parser.c */ + unsigned char header_state; /* enum header_state from http_parser.c */ + unsigned char index; /* index into current matcher */ + + uint32_t nread; /* # bytes read in various scenarios */ + uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */ + + /** READ-ONLY **/ + unsigned short http_major; + unsigned short http_minor; + unsigned short status_code; /* responses only */ + unsigned char method; /* requests only */ + unsigned char http_errno : 7; + + /* 1 = Upgrade header was present and the parser has exited because of that. + * 0 = No upgrade header present. + * Should be checked when http_parser_execute() returns in addition to + * error checking. + */ + unsigned char upgrade : 1; + + /** PUBLIC **/ + void *data; /* A pointer to get hook to the "connection" or "socket" object */ +}; + + +struct http_parser_settings { + http_cb on_message_begin; + http_data_cb on_url; + http_data_cb on_header_field; + http_data_cb on_header_value; + http_cb on_headers_complete; + http_data_cb on_body; + http_cb on_message_complete; +}; + + +enum http_parser_url_fields + { UF_SCHEMA = 0 + , UF_HOST = 1 + , UF_PORT = 2 + , UF_PATH = 3 + , UF_QUERY = 4 + , UF_FRAGMENT = 5 + , UF_USERINFO = 6 + , UF_MAX = 7 + }; + + +/* Result structure for http_parser_parse_url(). + * + * Callers should index into field_data[] with UF_* values iff field_set + * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and + * because we probably have padding left over), we convert any port to + * a uint16_t. + */ +struct http_parser_url { + uint16_t field_set; /* Bitmask of (1 << UF_*) values */ + uint16_t port; /* Converted UF_PORT string */ + + struct { + uint16_t off; /* Offset into buffer in which field starts */ + uint16_t len; /* Length of run in buffer */ + } field_data[UF_MAX]; +}; + + +void http_parser_init(http_parser *parser, enum http_parser_type type); + + +size_t http_parser_execute(http_parser *parser, + const http_parser_settings *settings, + const char *data, + size_t len); + + +/* If http_should_keep_alive() in the on_headers_complete or + * on_message_complete callback returns 0, then this should be + * the last message on the connection. + * If you are the server, respond with the "Connection: close" header. + * If you are the client, close the connection. + */ +int http_should_keep_alive(const http_parser *parser); + +/* Returns a string version of the HTTP method. */ +const char *http_method_str(enum http_method m); + +/* Return a string name of the given error */ +const char *http_errno_name(enum http_errno err); + +/* Return a string description of the given error */ +const char *http_errno_description(enum http_errno err); + +/* Parse a URL; return nonzero on failure */ +int http_parser_parse_url(const char *buf, size_t buflen, + int is_connect, + struct http_parser_url *u); + +/* Pause or un-pause the parser; a nonzero value pauses */ +void http_parser_pause(http_parser *parser, int paused); + +/* Checks if this is the final chunk of the body. */ +int http_body_is_final(const http_parser *parser); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/http/qhttpserver/http-parser/test.c b/src/http/qhttpserver/http-parser/test.c new file mode 100644 index 000000000..b6c2acbc1 --- /dev/null +++ b/src/http/qhttpserver/http-parser/test.c @@ -0,0 +1,3402 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ +#include "http_parser.h" +#include +#include +#include +#include /* rand */ +#include +#include + +#undef TRUE +#define TRUE 1 +#undef FALSE +#define FALSE 0 + +#define MAX_HEADERS 13 +#define MAX_ELEMENT_SIZE 2048 + +#define MIN(a,b) ((a) < (b) ? (a) : (b)) + +static http_parser *parser; + +struct message { + const char *name; // for debugging purposes + const char *raw; + enum http_parser_type type; + enum http_method method; + int status_code; + char request_path[MAX_ELEMENT_SIZE]; + char request_url[MAX_ELEMENT_SIZE]; + char fragment[MAX_ELEMENT_SIZE]; + char query_string[MAX_ELEMENT_SIZE]; + char body[MAX_ELEMENT_SIZE]; + size_t body_size; + const char *host; + const char *userinfo; + uint16_t port; + int num_headers; + enum { NONE=0, FIELD, VALUE } last_header_element; + char headers [MAX_HEADERS][2][MAX_ELEMENT_SIZE]; + int should_keep_alive; + + const char *upgrade; // upgraded body + + unsigned short http_major; + unsigned short http_minor; + + int message_begin_cb_called; + int headers_complete_cb_called; + int message_complete_cb_called; + int message_complete_on_eof; + int body_is_final; +}; + +static int currently_parsing_eof; + +static struct message messages[5]; +static int num_messages; +static http_parser_settings *current_pause_parser; + +/* * R E Q U E S T S * */ +const struct message requests[] = +#define CURL_GET 0 +{ {.name= "curl get" + ,.type= HTTP_REQUEST + ,.raw= "GET /test HTTP/1.1\r\n" + "User-Agent: curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1\r\n" + "Host: 0.0.0.0=5000\r\n" + "Accept: */*\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/test" + ,.request_url= "/test" + ,.num_headers= 3 + ,.headers= + { { "User-Agent", "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1" } + , { "Host", "0.0.0.0=5000" } + , { "Accept", "*/*" } + } + ,.body= "" + } + +#define FIREFOX_GET 1 +, {.name= "firefox get" + ,.type= HTTP_REQUEST + ,.raw= "GET /favicon.ico HTTP/1.1\r\n" + "Host: 0.0.0.0=5000\r\n" + "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n" + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" + "Accept-Language: en-us,en;q=0.5\r\n" + "Accept-Encoding: gzip,deflate\r\n" + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" + "Keep-Alive: 300\r\n" + "Connection: keep-alive\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/favicon.ico" + ,.request_url= "/favicon.ico" + ,.num_headers= 8 + ,.headers= + { { "Host", "0.0.0.0=5000" } + , { "User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0" } + , { "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" } + , { "Accept-Language", "en-us,en;q=0.5" } + , { "Accept-Encoding", "gzip,deflate" } + , { "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7" } + , { "Keep-Alive", "300" } + , { "Connection", "keep-alive" } + } + ,.body= "" + } + +#define DUMBFUCK 2 +, {.name= "dumbfuck" + ,.type= HTTP_REQUEST + ,.raw= "GET /dumbfuck HTTP/1.1\r\n" + "aaaaaaaaaaaaa:++++++++++\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/dumbfuck" + ,.request_url= "/dumbfuck" + ,.num_headers= 1 + ,.headers= + { { "aaaaaaaaaaaaa", "++++++++++" } + } + ,.body= "" + } + +#define FRAGMENT_IN_URI 3 +, {.name= "fragment in url" + ,.type= HTTP_REQUEST + ,.raw= "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "page=1" + ,.fragment= "posts-17408" + ,.request_path= "/forums/1/topics/2375" + /* XXX request url does include fragment? */ + ,.request_url= "/forums/1/topics/2375?page=1#posts-17408" + ,.num_headers= 0 + ,.body= "" + } + +#define GET_NO_HEADERS_NO_BODY 4 +, {.name= "get no headers no body" + ,.type= HTTP_REQUEST + ,.raw= "GET /get_no_headers_no_body/world HTTP/1.1\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE /* would need Connection: close */ + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/get_no_headers_no_body/world" + ,.request_url= "/get_no_headers_no_body/world" + ,.num_headers= 0 + ,.body= "" + } + +#define GET_ONE_HEADER_NO_BODY 5 +, {.name= "get one header no body" + ,.type= HTTP_REQUEST + ,.raw= "GET /get_one_header_no_body HTTP/1.1\r\n" + "Accept: */*\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE /* would need Connection: close */ + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/get_one_header_no_body" + ,.request_url= "/get_one_header_no_body" + ,.num_headers= 1 + ,.headers= + { { "Accept" , "*/*" } + } + ,.body= "" + } + +#define GET_FUNKY_CONTENT_LENGTH 6 +, {.name= "get funky content length body hello" + ,.type= HTTP_REQUEST + ,.raw= "GET /get_funky_content_length_body_hello HTTP/1.0\r\n" + "conTENT-Length: 5\r\n" + "\r\n" + "HELLO" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 0 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/get_funky_content_length_body_hello" + ,.request_url= "/get_funky_content_length_body_hello" + ,.num_headers= 1 + ,.headers= + { { "conTENT-Length" , "5" } + } + ,.body= "HELLO" + } + +#define POST_IDENTITY_BODY_WORLD 7 +, {.name= "post identity body world" + ,.type= HTTP_REQUEST + ,.raw= "POST /post_identity_body_world?q=search#hey HTTP/1.1\r\n" + "Accept: */*\r\n" + "Transfer-Encoding: identity\r\n" + "Content-Length: 5\r\n" + "\r\n" + "World" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_POST + ,.query_string= "q=search" + ,.fragment= "hey" + ,.request_path= "/post_identity_body_world" + ,.request_url= "/post_identity_body_world?q=search#hey" + ,.num_headers= 3 + ,.headers= + { { "Accept", "*/*" } + , { "Transfer-Encoding", "identity" } + , { "Content-Length", "5" } + } + ,.body= "World" + } + +#define POST_CHUNKED_ALL_YOUR_BASE 8 +, {.name= "post - chunked body: all your base are belong to us" + ,.type= HTTP_REQUEST + ,.raw= "POST /post_chunked_all_your_base HTTP/1.1\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "1e\r\nall your base are belong to us\r\n" + "0\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_POST + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/post_chunked_all_your_base" + ,.request_url= "/post_chunked_all_your_base" + ,.num_headers= 1 + ,.headers= + { { "Transfer-Encoding" , "chunked" } + } + ,.body= "all your base are belong to us" + } + +#define TWO_CHUNKS_MULT_ZERO_END 9 +, {.name= "two chunks ; triple zero ending" + ,.type= HTTP_REQUEST + ,.raw= "POST /two_chunks_mult_zero_end HTTP/1.1\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "5\r\nhello\r\n" + "6\r\n world\r\n" + "000\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_POST + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/two_chunks_mult_zero_end" + ,.request_url= "/two_chunks_mult_zero_end" + ,.num_headers= 1 + ,.headers= + { { "Transfer-Encoding", "chunked" } + } + ,.body= "hello world" + } + +#define CHUNKED_W_TRAILING_HEADERS 10 +, {.name= "chunked with trailing headers. blech." + ,.type= HTTP_REQUEST + ,.raw= "POST /chunked_w_trailing_headers HTTP/1.1\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "5\r\nhello\r\n" + "6\r\n world\r\n" + "0\r\n" + "Vary: *\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_POST + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/chunked_w_trailing_headers" + ,.request_url= "/chunked_w_trailing_headers" + ,.num_headers= 3 + ,.headers= + { { "Transfer-Encoding", "chunked" } + , { "Vary", "*" } + , { "Content-Type", "text/plain" } + } + ,.body= "hello world" + } + +#define CHUNKED_W_BULLSHIT_AFTER_LENGTH 11 +, {.name= "with bullshit after the length" + ,.type= HTTP_REQUEST + ,.raw= "POST /chunked_w_bullshit_after_length HTTP/1.1\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "5; ihatew3;whatthefuck=aretheseparametersfor\r\nhello\r\n" + "6; blahblah; blah\r\n world\r\n" + "0\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_POST + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/chunked_w_bullshit_after_length" + ,.request_url= "/chunked_w_bullshit_after_length" + ,.num_headers= 1 + ,.headers= + { { "Transfer-Encoding", "chunked" } + } + ,.body= "hello world" + } + +#define WITH_QUOTES 12 +, {.name= "with quotes" + ,.type= HTTP_REQUEST + ,.raw= "GET /with_\"stupid\"_quotes?foo=\"bar\" HTTP/1.1\r\n\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "foo=\"bar\"" + ,.fragment= "" + ,.request_path= "/with_\"stupid\"_quotes" + ,.request_url= "/with_\"stupid\"_quotes?foo=\"bar\"" + ,.num_headers= 0 + ,.headers= { } + ,.body= "" + } + +#define APACHEBENCH_GET 13 +/* The server receiving this request SHOULD NOT wait for EOF + * to know that content-length == 0. + * How to represent this in a unit test? message_complete_on_eof + * Compare with NO_CONTENT_LENGTH_RESPONSE. + */ +, {.name = "apachebench get" + ,.type= HTTP_REQUEST + ,.raw= "GET /test HTTP/1.0\r\n" + "Host: 0.0.0.0:5000\r\n" + "User-Agent: ApacheBench/2.3\r\n" + "Accept: */*\r\n\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 0 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/test" + ,.request_url= "/test" + ,.num_headers= 3 + ,.headers= { { "Host", "0.0.0.0:5000" } + , { "User-Agent", "ApacheBench/2.3" } + , { "Accept", "*/*" } + } + ,.body= "" + } + +#define QUERY_URL_WITH_QUESTION_MARK_GET 14 +/* Some clients include '?' characters in query strings. + */ +, {.name = "query url with question mark" + ,.type= HTTP_REQUEST + ,.raw= "GET /test.cgi?foo=bar?baz HTTP/1.1\r\n\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "foo=bar?baz" + ,.fragment= "" + ,.request_path= "/test.cgi" + ,.request_url= "/test.cgi?foo=bar?baz" + ,.num_headers= 0 + ,.headers= {} + ,.body= "" + } + +#define PREFIX_NEWLINE_GET 15 +/* Some clients, especially after a POST in a keep-alive connection, + * will send an extra CRLF before the next request + */ +, {.name = "newline prefix get" + ,.type= HTTP_REQUEST + ,.raw= "\r\nGET /test HTTP/1.1\r\n\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/test" + ,.request_url= "/test" + ,.num_headers= 0 + ,.headers= { } + ,.body= "" + } + +#define UPGRADE_REQUEST 16 +, {.name = "upgrade request" + ,.type= HTTP_REQUEST + ,.raw= "GET /demo HTTP/1.1\r\n" + "Host: example.com\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n" + "Sec-WebSocket-Protocol: sample\r\n" + "Upgrade: WebSocket\r\n" + "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n" + "Origin: http://example.com\r\n" + "\r\n" + "Hot diggity dogg" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/demo" + ,.request_url= "/demo" + ,.num_headers= 7 + ,.upgrade="Hot diggity dogg" + ,.headers= { { "Host", "example.com" } + , { "Connection", "Upgrade" } + , { "Sec-WebSocket-Key2", "12998 5 Y3 1 .P00" } + , { "Sec-WebSocket-Protocol", "sample" } + , { "Upgrade", "WebSocket" } + , { "Sec-WebSocket-Key1", "4 @1 46546xW%0l 1 5" } + , { "Origin", "http://example.com" } + } + ,.body= "" + } + +#define CONNECT_REQUEST 17 +, {.name = "connect request" + ,.type= HTTP_REQUEST + ,.raw= "CONNECT 0-home0.netscape.com:443 HTTP/1.0\r\n" + "User-agent: Mozilla/1.1N\r\n" + "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n" + "\r\n" + "some data\r\n" + "and yet even more data" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 0 + ,.method= HTTP_CONNECT + ,.query_string= "" + ,.fragment= "" + ,.request_path= "" + ,.request_url= "0-home0.netscape.com:443" + ,.num_headers= 2 + ,.upgrade="some data\r\nand yet even more data" + ,.headers= { { "User-agent", "Mozilla/1.1N" } + , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" } + } + ,.body= "" + } + +#define REPORT_REQ 18 +, {.name= "report request" + ,.type= HTTP_REQUEST + ,.raw= "REPORT /test HTTP/1.1\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_REPORT + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/test" + ,.request_url= "/test" + ,.num_headers= 0 + ,.headers= {} + ,.body= "" + } + +#define NO_HTTP_VERSION 19 +, {.name= "request with no http version" + ,.type= HTTP_REQUEST + ,.raw= "GET /\r\n" + "\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 0 + ,.http_minor= 9 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/" + ,.request_url= "/" + ,.num_headers= 0 + ,.headers= {} + ,.body= "" + } + +#define MSEARCH_REQ 20 +, {.name= "m-search request" + ,.type= HTTP_REQUEST + ,.raw= "M-SEARCH * HTTP/1.1\r\n" + "HOST: 239.255.255.250:1900\r\n" + "MAN: \"ssdp:discover\"\r\n" + "ST: \"ssdp:all\"\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_MSEARCH + ,.query_string= "" + ,.fragment= "" + ,.request_path= "*" + ,.request_url= "*" + ,.num_headers= 3 + ,.headers= { { "HOST", "239.255.255.250:1900" } + , { "MAN", "\"ssdp:discover\"" } + , { "ST", "\"ssdp:all\"" } + } + ,.body= "" + } + +#define LINE_FOLDING_IN_HEADER 21 +, {.name= "line folding in header value" + ,.type= HTTP_REQUEST + ,.raw= "GET / HTTP/1.1\r\n" + "Line1: abc\r\n" + "\tdef\r\n" + " ghi\r\n" + "\t\tjkl\r\n" + " mno \r\n" + "\t \tqrs\r\n" + "Line2: \t line2\t\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/" + ,.request_url= "/" + ,.num_headers= 2 + ,.headers= { { "Line1", "abcdefghijklmno qrs" } + , { "Line2", "line2\t" } + } + ,.body= "" + } + + +#define QUERY_TERMINATED_HOST 22 +, {.name= "host terminated by a query string" + ,.type= HTTP_REQUEST + ,.raw= "GET http://hypnotoad.org?hail=all HTTP/1.1\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "hail=all" + ,.fragment= "" + ,.request_path= "" + ,.request_url= "http://hypnotoad.org?hail=all" + ,.host= "hypnotoad.org" + ,.num_headers= 0 + ,.headers= { } + ,.body= "" + } + +#define QUERY_TERMINATED_HOSTPORT 23 +, {.name= "host:port terminated by a query string" + ,.type= HTTP_REQUEST + ,.raw= "GET http://hypnotoad.org:1234?hail=all HTTP/1.1\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "hail=all" + ,.fragment= "" + ,.request_path= "" + ,.request_url= "http://hypnotoad.org:1234?hail=all" + ,.host= "hypnotoad.org" + ,.port= 1234 + ,.num_headers= 0 + ,.headers= { } + ,.body= "" + } + +#define SPACE_TERMINATED_HOSTPORT 24 +, {.name= "host:port terminated by a space" + ,.type= HTTP_REQUEST + ,.raw= "GET http://hypnotoad.org:1234 HTTP/1.1\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "" + ,.fragment= "" + ,.request_path= "" + ,.request_url= "http://hypnotoad.org:1234" + ,.host= "hypnotoad.org" + ,.port= 1234 + ,.num_headers= 0 + ,.headers= { } + ,.body= "" + } + +#define PATCH_REQ 25 +, {.name = "PATCH request" + ,.type= HTTP_REQUEST + ,.raw= "PATCH /file.txt HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "Content-Type: application/example\r\n" + "If-Match: \"e0023aa4e\"\r\n" + "Content-Length: 10\r\n" + "\r\n" + "cccccccccc" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_PATCH + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/file.txt" + ,.request_url= "/file.txt" + ,.num_headers= 4 + ,.headers= { { "Host", "www.example.com" } + , { "Content-Type", "application/example" } + , { "If-Match", "\"e0023aa4e\"" } + , { "Content-Length", "10" } + } + ,.body= "cccccccccc" + } + +#define CONNECT_CAPS_REQUEST 26 +, {.name = "connect caps request" + ,.type= HTTP_REQUEST + ,.raw= "CONNECT HOME0.NETSCAPE.COM:443 HTTP/1.0\r\n" + "User-agent: Mozilla/1.1N\r\n" + "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n" + "\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 0 + ,.method= HTTP_CONNECT + ,.query_string= "" + ,.fragment= "" + ,.request_path= "" + ,.request_url= "HOME0.NETSCAPE.COM:443" + ,.num_headers= 2 + ,.upgrade="" + ,.headers= { { "User-agent", "Mozilla/1.1N" } + , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" } + } + ,.body= "" + } + +#if !HTTP_PARSER_STRICT +#define UTF8_PATH_REQ 27 +, {.name= "utf-8 path request" + ,.type= HTTP_REQUEST + ,.raw= "GET /δ¶/δt/pope?q=1#narf HTTP/1.1\r\n" + "Host: github.com\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.query_string= "q=1" + ,.fragment= "narf" + ,.request_path= "/δ¶/δt/pope" + ,.request_url= "/δ¶/δt/pope?q=1#narf" + ,.num_headers= 1 + ,.headers= { {"Host", "github.com" } + } + ,.body= "" + } + +#define HOSTNAME_UNDERSCORE 28 +, {.name = "hostname underscore" + ,.type= HTTP_REQUEST + ,.raw= "CONNECT home_0.netscape.com:443 HTTP/1.0\r\n" + "User-agent: Mozilla/1.1N\r\n" + "Proxy-authorization: basic aGVsbG86d29ybGQ=\r\n" + "\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 0 + ,.method= HTTP_CONNECT + ,.query_string= "" + ,.fragment= "" + ,.request_path= "" + ,.request_url= "home_0.netscape.com:443" + ,.num_headers= 2 + ,.upgrade="" + ,.headers= { { "User-agent", "Mozilla/1.1N" } + , { "Proxy-authorization", "basic aGVsbG86d29ybGQ=" } + } + ,.body= "" + } +#endif /* !HTTP_PARSER_STRICT */ + +/* see https://github.com/ry/http-parser/issues/47 */ +#define EAT_TRAILING_CRLF_NO_CONNECTION_CLOSE 29 +, {.name = "eat CRLF between requests, no \"Connection: close\" header" + ,.raw= "POST / HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: 4\r\n" + "\r\n" + "q=42\r\n" /* note the trailing CRLF */ + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_POST + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/" + ,.request_url= "/" + ,.num_headers= 3 + ,.upgrade= 0 + ,.headers= { { "Host", "www.example.com" } + , { "Content-Type", "application/x-www-form-urlencoded" } + , { "Content-Length", "4" } + } + ,.body= "q=42" + } + +/* see https://github.com/ry/http-parser/issues/47 */ +#define EAT_TRAILING_CRLF_WITH_CONNECTION_CLOSE 30 +, {.name = "eat CRLF between requests even if \"Connection: close\" is set" + ,.raw= "POST / HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: 4\r\n" + "Connection: close\r\n" + "\r\n" + "q=42\r\n" /* note the trailing CRLF */ + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE /* input buffer isn't empty when on_message_complete is called */ + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_POST + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/" + ,.request_url= "/" + ,.num_headers= 4 + ,.upgrade= 0 + ,.headers= { { "Host", "www.example.com" } + , { "Content-Type", "application/x-www-form-urlencoded" } + , { "Content-Length", "4" } + , { "Connection", "close" } + } + ,.body= "q=42" + } + +#define PURGE_REQ 31 +, {.name = "PURGE request" + ,.type= HTTP_REQUEST + ,.raw= "PURGE /file.txt HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_PURGE + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/file.txt" + ,.request_url= "/file.txt" + ,.num_headers= 1 + ,.headers= { { "Host", "www.example.com" } } + ,.body= "" + } + +#define SEARCH_REQ 32 +, {.name = "SEARCH request" + ,.type= HTTP_REQUEST + ,.raw= "SEARCH / HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_SEARCH + ,.query_string= "" + ,.fragment= "" + ,.request_path= "/" + ,.request_url= "/" + ,.num_headers= 1 + ,.headers= { { "Host", "www.example.com" } } + ,.body= "" + } + +#define PROXY_WITH_BASIC_AUTH 33 +, {.name= "host:port and basic_auth" + ,.type= HTTP_REQUEST + ,.raw= "GET http://a%12:b!&*$@hypnotoad.org:1234/toto HTTP/1.1\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.method= HTTP_GET + ,.fragment= "" + ,.request_path= "/toto" + ,.request_url= "http://a%12:b!&*$@hypnotoad.org:1234/toto" + ,.host= "hypnotoad.org" + ,.userinfo= "a%12:b!&*$" + ,.port= 1234 + ,.num_headers= 0 + ,.headers= { } + ,.body= "" + } + + +, {.name= NULL } /* sentinel */ +}; + +/* * R E S P O N S E S * */ +const struct message responses[] = +#define GOOGLE_301 0 +{ {.name= "google 301" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 301 Moved Permanently\r\n" + "Location: http://www.google.com/\r\n" + "Content-Type: text/html; charset=UTF-8\r\n" + "Date: Sun, 26 Apr 2009 11:11:49 GMT\r\n" + "Expires: Tue, 26 May 2009 11:11:49 GMT\r\n" + "X-$PrototypeBI-Version: 1.6.0.3\r\n" /* $ char in header field */ + "Cache-Control: public, max-age=2592000\r\n" + "Server: gws\r\n" + "Content-Length: 219 \r\n" + "\r\n" + "\n" + "301 Moved\n" + "

301 Moved

\n" + "The document has moved\n" + "here.\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 301 + ,.num_headers= 8 + ,.headers= + { { "Location", "http://www.google.com/" } + , { "Content-Type", "text/html; charset=UTF-8" } + , { "Date", "Sun, 26 Apr 2009 11:11:49 GMT" } + , { "Expires", "Tue, 26 May 2009 11:11:49 GMT" } + , { "X-$PrototypeBI-Version", "1.6.0.3" } + , { "Cache-Control", "public, max-age=2592000" } + , { "Server", "gws" } + , { "Content-Length", "219 " } + } + ,.body= "\n" + "301 Moved\n" + "

301 Moved

\n" + "The document has moved\n" + "here.\r\n" + "\r\n" + } + +#define NO_CONTENT_LENGTH_RESPONSE 1 +/* The client should wait for the server's EOF. That is, when content-length + * is not specified, and "Connection: close", the end of body is specified + * by the EOF. + * Compare with APACHEBENCH_GET + */ +, {.name= "no content-length response" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "Date: Tue, 04 Aug 2009 07:59:32 GMT\r\n" + "Server: Apache\r\n" + "X-Powered-By: Servlet/2.5 JSP/2.1\r\n" + "Content-Type: text/xml; charset=utf-8\r\n" + "Connection: close\r\n" + "\r\n" + "\n" + "\n" + " \n" + " \n" + " SOAP-ENV:Client\n" + " Client Error\n" + " \n" + " \n" + "" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= TRUE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 5 + ,.headers= + { { "Date", "Tue, 04 Aug 2009 07:59:32 GMT" } + , { "Server", "Apache" } + , { "X-Powered-By", "Servlet/2.5 JSP/2.1" } + , { "Content-Type", "text/xml; charset=utf-8" } + , { "Connection", "close" } + } + ,.body= "\n" + "\n" + " \n" + " \n" + " SOAP-ENV:Client\n" + " Client Error\n" + " \n" + " \n" + "" + } + +#define NO_HEADERS_NO_BODY_404 2 +, {.name= "404 no headers no body" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 404 Not Found\r\n\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= TRUE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 404 + ,.num_headers= 0 + ,.headers= {} + ,.body_size= 0 + ,.body= "" + } + +#define NO_REASON_PHRASE 3 +, {.name= "301 no response phrase" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 301\r\n\r\n" + ,.should_keep_alive = FALSE + ,.message_complete_on_eof= TRUE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 301 + ,.num_headers= 0 + ,.headers= {} + ,.body= "" + } + +#define TRAILING_SPACE_ON_CHUNKED_BODY 4 +, {.name="200 trailing space on chunked body" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "25 \r\n" + "This is the data in the first chunk\r\n" + "\r\n" + "1C\r\n" + "and this is the second one\r\n" + "\r\n" + "0 \r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 2 + ,.headers= + { {"Content-Type", "text/plain" } + , {"Transfer-Encoding", "chunked" } + } + ,.body_size = 37+28 + ,.body = + "This is the data in the first chunk\r\n" + "and this is the second one\r\n" + + } + +#define NO_CARRIAGE_RET 5 +, {.name="no carriage ret" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\n" + "Content-Type: text/html; charset=utf-8\n" + "Connection: close\n" + "\n" + "these headers are from http://news.ycombinator.com/" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= TRUE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 2 + ,.headers= + { {"Content-Type", "text/html; charset=utf-8" } + , {"Connection", "close" } + } + ,.body= "these headers are from http://news.ycombinator.com/" + } + +#define PROXY_CONNECTION 6 +, {.name="proxy connection" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html; charset=UTF-8\r\n" + "Content-Length: 11\r\n" + "Proxy-Connection: close\r\n" + "Date: Thu, 31 Dec 2009 20:55:48 +0000\r\n" + "\r\n" + "hello world" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 4 + ,.headers= + { {"Content-Type", "text/html; charset=UTF-8" } + , {"Content-Length", "11" } + , {"Proxy-Connection", "close" } + , {"Date", "Thu, 31 Dec 2009 20:55:48 +0000"} + } + ,.body= "hello world" + } + +#define UNDERSTORE_HEADER_KEY 7 + // shown by + // curl -o /dev/null -v "http://ad.doubleclick.net/pfadx/DARTSHELLCONFIGXML;dcmt=text/xml;" +, {.name="underscore header key" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "Server: DCLK-AdSvr\r\n" + "Content-Type: text/xml\r\n" + "Content-Length: 0\r\n" + "DCLK_imp: v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o\r\n\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 4 + ,.headers= + { {"Server", "DCLK-AdSvr" } + , {"Content-Type", "text/xml" } + , {"Content-Length", "0" } + , {"DCLK_imp", "v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o" } + } + ,.body= "" + } + +#define BONJOUR_MADAME_FR 8 +/* The client should not merge two headers fields when the first one doesn't + * have a value. + */ +, {.name= "bonjourmadame.fr" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.0 301 Moved Permanently\r\n" + "Date: Thu, 03 Jun 2010 09:56:32 GMT\r\n" + "Server: Apache/2.2.3 (Red Hat)\r\n" + "Cache-Control: public\r\n" + "Pragma: \r\n" + "Location: http://www.bonjourmadame.fr/\r\n" + "Vary: Accept-Encoding\r\n" + "Content-Length: 0\r\n" + "Content-Type: text/html; charset=UTF-8\r\n" + "Connection: keep-alive\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 0 + ,.status_code= 301 + ,.num_headers= 9 + ,.headers= + { { "Date", "Thu, 03 Jun 2010 09:56:32 GMT" } + , { "Server", "Apache/2.2.3 (Red Hat)" } + , { "Cache-Control", "public" } + , { "Pragma", "" } + , { "Location", "http://www.bonjourmadame.fr/" } + , { "Vary", "Accept-Encoding" } + , { "Content-Length", "0" } + , { "Content-Type", "text/html; charset=UTF-8" } + , { "Connection", "keep-alive" } + } + ,.body= "" + } + +#define RES_FIELD_UNDERSCORE 9 +/* Should handle spaces in header fields */ +, {.name= "field underscore" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "Date: Tue, 28 Sep 2010 01:14:13 GMT\r\n" + "Server: Apache\r\n" + "Cache-Control: no-cache, must-revalidate\r\n" + "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n" + ".et-Cookie: PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com\r\n" + "Vary: Accept-Encoding\r\n" + "_eep-Alive: timeout=45\r\n" /* semantic value ignored */ + "_onnection: Keep-Alive\r\n" /* semantic value ignored */ + "Transfer-Encoding: chunked\r\n" + "Content-Type: text/html\r\n" + "Connection: close\r\n" + "\r\n" + "0\r\n\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 11 + ,.headers= + { { "Date", "Tue, 28 Sep 2010 01:14:13 GMT" } + , { "Server", "Apache" } + , { "Cache-Control", "no-cache, must-revalidate" } + , { "Expires", "Mon, 26 Jul 1997 05:00:00 GMT" } + , { ".et-Cookie", "PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com" } + , { "Vary", "Accept-Encoding" } + , { "_eep-Alive", "timeout=45" } + , { "_onnection", "Keep-Alive" } + , { "Transfer-Encoding", "chunked" } + , { "Content-Type", "text/html" } + , { "Connection", "close" } + } + ,.body= "" + } + +#define NON_ASCII_IN_STATUS_LINE 10 +/* Should handle non-ASCII in status line */ +, {.name= "non-ASCII in status line" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 500 Oriëntatieprobleem\r\n" + "Date: Fri, 5 Nov 2010 23:07:12 GMT+2\r\n" + "Content-Length: 0\r\n" + "Connection: close\r\n" + "\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 500 + ,.num_headers= 3 + ,.headers= + { { "Date", "Fri, 5 Nov 2010 23:07:12 GMT+2" } + , { "Content-Length", "0" } + , { "Connection", "close" } + } + ,.body= "" + } + +#define HTTP_VERSION_0_9 11 +/* Should handle HTTP/0.9 */ +, {.name= "http version 0.9" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/0.9 200 OK\r\n" + "\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= TRUE + ,.http_major= 0 + ,.http_minor= 9 + ,.status_code= 200 + ,.num_headers= 0 + ,.headers= + {} + ,.body= "" + } + +#define NO_CONTENT_LENGTH_NO_TRANSFER_ENCODING_RESPONSE 12 +/* The client should wait for the server's EOF. That is, when neither + * content-length nor transfer-encoding is specified, the end of body + * is specified by the EOF. + */ +, {.name= "neither content-length nor transfer-encoding response" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "hello world" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= TRUE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 1 + ,.headers= + { { "Content-Type", "text/plain" } + } + ,.body= "hello world" + } + +#define NO_BODY_HTTP10_KA_200 13 +, {.name= "HTTP/1.0 with keep-alive and EOF-terminated 200 status" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.0 200 OK\r\n" + "Connection: keep-alive\r\n" + "\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= TRUE + ,.http_major= 1 + ,.http_minor= 0 + ,.status_code= 200 + ,.num_headers= 1 + ,.headers= + { { "Connection", "keep-alive" } + } + ,.body_size= 0 + ,.body= "" + } + +#define NO_BODY_HTTP10_KA_204 14 +, {.name= "HTTP/1.0 with keep-alive and a 204 status" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.0 204 No content\r\n" + "Connection: keep-alive\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 0 + ,.status_code= 204 + ,.num_headers= 1 + ,.headers= + { { "Connection", "keep-alive" } + } + ,.body_size= 0 + ,.body= "" + } + +#define NO_BODY_HTTP11_KA_200 15 +, {.name= "HTTP/1.1 with an EOF-terminated 200 status" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= TRUE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 0 + ,.headers={} + ,.body_size= 0 + ,.body= "" + } + +#define NO_BODY_HTTP11_KA_204 16 +, {.name= "HTTP/1.1 with a 204 status" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 204 No content\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 204 + ,.num_headers= 0 + ,.headers={} + ,.body_size= 0 + ,.body= "" + } + +#define NO_BODY_HTTP11_NOKA_204 17 +, {.name= "HTTP/1.1 with a 204 status and keep-alive disabled" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 204 No content\r\n" + "Connection: close\r\n" + "\r\n" + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 204 + ,.num_headers= 1 + ,.headers= + { { "Connection", "close" } + } + ,.body_size= 0 + ,.body= "" + } + +#define NO_BODY_HTTP11_KA_CHUNKED_200 18 +, {.name= "HTTP/1.1 with chunked endocing and a 200 response" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "0\r\n" + "\r\n" + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 1 + ,.headers= + { { "Transfer-Encoding", "chunked" } + } + ,.body_size= 0 + ,.body= "" + } + +#if !HTTP_PARSER_STRICT +#define SPACE_IN_FIELD_RES 19 +/* Should handle spaces in header fields */ +, {.name= "field space" + ,.type= HTTP_RESPONSE + ,.raw= "HTTP/1.1 200 OK\r\n" + "Server: Microsoft-IIS/6.0\r\n" + "X-Powered-By: ASP.NET\r\n" + "en-US Content-Type: text/xml\r\n" /* this is the problem */ + "Content-Type: text/xml\r\n" + "Content-Length: 16\r\n" + "Date: Fri, 23 Jul 2010 18:45:38 GMT\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "hello" /* fake body */ + ,.should_keep_alive= TRUE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 1 + ,.status_code= 200 + ,.num_headers= 7 + ,.headers= + { { "Server", "Microsoft-IIS/6.0" } + , { "X-Powered-By", "ASP.NET" } + , { "en-US Content-Type", "text/xml" } + , { "Content-Type", "text/xml" } + , { "Content-Length", "16" } + , { "Date", "Fri, 23 Jul 2010 18:45:38 GMT" } + , { "Connection", "keep-alive" } + } + ,.body= "hello" + } +#endif /* !HTTP_PARSER_STRICT */ + +, {.name= NULL } /* sentinel */ +}; + +/* strnlen() is a POSIX.2008 addition. Can't rely on it being available so + * define it ourselves. + */ +size_t +strnlen(const char *s, size_t maxlen) +{ + const char *p; + + p = memchr(s, '\0', maxlen); + if (p == NULL) + return maxlen; + + return p - s; +} + +size_t +strlncat(char *dst, size_t len, const char *src, size_t n) +{ + size_t slen; + size_t dlen; + size_t rlen; + size_t ncpy; + + slen = strnlen(src, n); + dlen = strnlen(dst, len); + + if (dlen < len) { + rlen = len - dlen; + ncpy = slen < rlen ? slen : (rlen - 1); + memcpy(dst + dlen, src, ncpy); + dst[dlen + ncpy] = '\0'; + } + + assert(len > slen + dlen); + return slen + dlen; +} + +size_t +strlcat(char *dst, const char *src, size_t len) +{ + return strlncat(dst, len, src, (size_t) -1); +} + +size_t +strlncpy(char *dst, size_t len, const char *src, size_t n) +{ + size_t slen; + size_t ncpy; + + slen = strnlen(src, n); + + if (len > 0) { + ncpy = slen < len ? slen : (len - 1); + memcpy(dst, src, ncpy); + dst[ncpy] = '\0'; + } + + assert(len > slen); + return slen; +} + +size_t +strlcpy(char *dst, const char *src, size_t len) +{ + return strlncpy(dst, len, src, (size_t) -1); +} + +int +request_url_cb (http_parser *p, const char *buf, size_t len) +{ + assert(p == parser); + strlncat(messages[num_messages].request_url, + sizeof(messages[num_messages].request_url), + buf, + len); + return 0; +} + +int +header_field_cb (http_parser *p, const char *buf, size_t len) +{ + assert(p == parser); + struct message *m = &messages[num_messages]; + + if (m->last_header_element != FIELD) + m->num_headers++; + + strlncat(m->headers[m->num_headers-1][0], + sizeof(m->headers[m->num_headers-1][0]), + buf, + len); + + m->last_header_element = FIELD; + + return 0; +} + +int +header_value_cb (http_parser *p, const char *buf, size_t len) +{ + assert(p == parser); + struct message *m = &messages[num_messages]; + + strlncat(m->headers[m->num_headers-1][1], + sizeof(m->headers[m->num_headers-1][1]), + buf, + len); + + m->last_header_element = VALUE; + + return 0; +} + +void +check_body_is_final (const http_parser *p) +{ + if (messages[num_messages].body_is_final) { + fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 " + "on last on_body callback call " + "but it doesn't! ***\n\n"); + assert(0); + abort(); + } + messages[num_messages].body_is_final = http_body_is_final(p); +} + +int +body_cb (http_parser *p, const char *buf, size_t len) +{ + assert(p == parser); + strlncat(messages[num_messages].body, + sizeof(messages[num_messages].body), + buf, + len); + messages[num_messages].body_size += len; + check_body_is_final(p); + // printf("body_cb: '%s'\n", requests[num_messages].body); + return 0; +} + +int +count_body_cb (http_parser *p, const char *buf, size_t len) +{ + assert(p == parser); + assert(buf); + messages[num_messages].body_size += len; + check_body_is_final(p); + return 0; +} + +int +message_begin_cb (http_parser *p) +{ + assert(p == parser); + messages[num_messages].message_begin_cb_called = TRUE; + return 0; +} + +int +headers_complete_cb (http_parser *p) +{ + assert(p == parser); + messages[num_messages].method = parser->method; + messages[num_messages].status_code = parser->status_code; + messages[num_messages].http_major = parser->http_major; + messages[num_messages].http_minor = parser->http_minor; + messages[num_messages].headers_complete_cb_called = TRUE; + messages[num_messages].should_keep_alive = http_should_keep_alive(parser); + return 0; +} + +int +message_complete_cb (http_parser *p) +{ + assert(p == parser); + if (messages[num_messages].should_keep_alive != http_should_keep_alive(parser)) + { + fprintf(stderr, "\n\n *** Error http_should_keep_alive() should have same " + "value in both on_message_complete and on_headers_complete " + "but it doesn't! ***\n\n"); + assert(0); + abort(); + } + + if (messages[num_messages].body_size && + http_body_is_final(p) && + !messages[num_messages].body_is_final) + { + fprintf(stderr, "\n\n *** Error http_body_is_final() should return 1 " + "on last on_body callback call " + "but it doesn't! ***\n\n"); + assert(0); + abort(); + } + + messages[num_messages].message_complete_cb_called = TRUE; + + messages[num_messages].message_complete_on_eof = currently_parsing_eof; + + num_messages++; + return 0; +} + +/* These dontcall_* callbacks exist so that we can verify that when we're + * paused, no additional callbacks are invoked */ +int +dontcall_message_begin_cb (http_parser *p) +{ + if (p) { } // gcc + fprintf(stderr, "\n\n*** on_message_begin() called on paused parser ***\n\n"); + abort(); +} + +int +dontcall_header_field_cb (http_parser *p, const char *buf, size_t len) +{ + if (p || buf || len) { } // gcc + fprintf(stderr, "\n\n*** on_header_field() called on paused parser ***\n\n"); + abort(); +} + +int +dontcall_header_value_cb (http_parser *p, const char *buf, size_t len) +{ + if (p || buf || len) { } // gcc + fprintf(stderr, "\n\n*** on_header_value() called on paused parser ***\n\n"); + abort(); +} + +int +dontcall_request_url_cb (http_parser *p, const char *buf, size_t len) +{ + if (p || buf || len) { } // gcc + fprintf(stderr, "\n\n*** on_request_url() called on paused parser ***\n\n"); + abort(); +} + +int +dontcall_body_cb (http_parser *p, const char *buf, size_t len) +{ + if (p || buf || len) { } // gcc + fprintf(stderr, "\n\n*** on_body_cb() called on paused parser ***\n\n"); + abort(); +} + +int +dontcall_headers_complete_cb (http_parser *p) +{ + if (p) { } // gcc + fprintf(stderr, "\n\n*** on_headers_complete() called on paused " + "parser ***\n\n"); + abort(); +} + +int +dontcall_message_complete_cb (http_parser *p) +{ + if (p) { } // gcc + fprintf(stderr, "\n\n*** on_message_complete() called on paused " + "parser ***\n\n"); + abort(); +} + +static http_parser_settings settings_dontcall = + {.on_message_begin = dontcall_message_begin_cb + ,.on_header_field = dontcall_header_field_cb + ,.on_header_value = dontcall_header_value_cb + ,.on_url = dontcall_request_url_cb + ,.on_body = dontcall_body_cb + ,.on_headers_complete = dontcall_headers_complete_cb + ,.on_message_complete = dontcall_message_complete_cb + }; + +/* These pause_* callbacks always pause the parser and just invoke the regular + * callback that tracks content. Before returning, we overwrite the parser + * settings to point to the _dontcall variety so that we can verify that + * the pause actually did, you know, pause. */ +int +pause_message_begin_cb (http_parser *p) +{ + http_parser_pause(p, 1); + *current_pause_parser = settings_dontcall; + return message_begin_cb(p); +} + +int +pause_header_field_cb (http_parser *p, const char *buf, size_t len) +{ + http_parser_pause(p, 1); + *current_pause_parser = settings_dontcall; + return header_field_cb(p, buf, len); +} + +int +pause_header_value_cb (http_parser *p, const char *buf, size_t len) +{ + http_parser_pause(p, 1); + *current_pause_parser = settings_dontcall; + return header_value_cb(p, buf, len); +} + +int +pause_request_url_cb (http_parser *p, const char *buf, size_t len) +{ + http_parser_pause(p, 1); + *current_pause_parser = settings_dontcall; + return request_url_cb(p, buf, len); +} + +int +pause_body_cb (http_parser *p, const char *buf, size_t len) +{ + http_parser_pause(p, 1); + *current_pause_parser = settings_dontcall; + return body_cb(p, buf, len); +} + +int +pause_headers_complete_cb (http_parser *p) +{ + http_parser_pause(p, 1); + *current_pause_parser = settings_dontcall; + return headers_complete_cb(p); +} + +int +pause_message_complete_cb (http_parser *p) +{ + http_parser_pause(p, 1); + *current_pause_parser = settings_dontcall; + return message_complete_cb(p); +} + +static http_parser_settings settings_pause = + {.on_message_begin = pause_message_begin_cb + ,.on_header_field = pause_header_field_cb + ,.on_header_value = pause_header_value_cb + ,.on_url = pause_request_url_cb + ,.on_body = pause_body_cb + ,.on_headers_complete = pause_headers_complete_cb + ,.on_message_complete = pause_message_complete_cb + }; + +static http_parser_settings settings = + {.on_message_begin = message_begin_cb + ,.on_header_field = header_field_cb + ,.on_header_value = header_value_cb + ,.on_url = request_url_cb + ,.on_body = body_cb + ,.on_headers_complete = headers_complete_cb + ,.on_message_complete = message_complete_cb + }; + +static http_parser_settings settings_count_body = + {.on_message_begin = message_begin_cb + ,.on_header_field = header_field_cb + ,.on_header_value = header_value_cb + ,.on_url = request_url_cb + ,.on_body = count_body_cb + ,.on_headers_complete = headers_complete_cb + ,.on_message_complete = message_complete_cb + }; + +static http_parser_settings settings_null = + {.on_message_begin = 0 + ,.on_header_field = 0 + ,.on_header_value = 0 + ,.on_url = 0 + ,.on_body = 0 + ,.on_headers_complete = 0 + ,.on_message_complete = 0 + }; + +void +parser_init (enum http_parser_type type) +{ + num_messages = 0; + + assert(parser == NULL); + + parser = malloc(sizeof(http_parser)); + + http_parser_init(parser, type); + + memset(&messages, 0, sizeof messages); + +} + +void +parser_free () +{ + assert(parser); + free(parser); + parser = NULL; +} + +size_t parse (const char *buf, size_t len) +{ + size_t nparsed; + currently_parsing_eof = (len == 0); + nparsed = http_parser_execute(parser, &settings, buf, len); + return nparsed; +} + +size_t parse_count_body (const char *buf, size_t len) +{ + size_t nparsed; + currently_parsing_eof = (len == 0); + nparsed = http_parser_execute(parser, &settings_count_body, buf, len); + return nparsed; +} + +size_t parse_pause (const char *buf, size_t len) +{ + size_t nparsed; + http_parser_settings s = settings_pause; + + currently_parsing_eof = (len == 0); + current_pause_parser = &s; + nparsed = http_parser_execute(parser, current_pause_parser, buf, len); + return nparsed; +} + +static inline int +check_str_eq (const struct message *m, + const char *prop, + const char *expected, + const char *found) { + if ((expected == NULL) != (found == NULL)) { + printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name); + printf("expected %s\n", (expected == NULL) ? "NULL" : expected); + printf(" found %s\n", (found == NULL) ? "NULL" : found); + return 0; + } + if (expected != NULL && 0 != strcmp(expected, found)) { + printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name); + printf("expected '%s'\n", expected); + printf(" found '%s'\n", found); + return 0; + } + return 1; +} + +static inline int +check_num_eq (const struct message *m, + const char *prop, + int expected, + int found) { + if (expected != found) { + printf("\n*** Error: %s in '%s' ***\n\n", prop, m->name); + printf("expected %d\n", expected); + printf(" found %d\n", found); + return 0; + } + return 1; +} + +#define MESSAGE_CHECK_STR_EQ(expected, found, prop) \ + if (!check_str_eq(expected, #prop, expected->prop, found->prop)) return 0 + +#define MESSAGE_CHECK_NUM_EQ(expected, found, prop) \ + if (!check_num_eq(expected, #prop, expected->prop, found->prop)) return 0 + +#define MESSAGE_CHECK_URL_EQ(u, expected, found, prop, fn) \ +do { \ + char ubuf[256]; \ + \ + if ((u)->field_set & (1 << (fn))) { \ + memcpy(ubuf, (found)->request_url + (u)->field_data[(fn)].off, \ + (u)->field_data[(fn)].len); \ + ubuf[(u)->field_data[(fn)].len] = '\0'; \ + } else { \ + ubuf[0] = '\0'; \ + } \ + \ + check_str_eq(expected, #prop, expected->prop, ubuf); \ +} while(0) + +int +message_eq (int index, const struct message *expected) +{ + int i; + struct message *m = &messages[index]; + + MESSAGE_CHECK_NUM_EQ(expected, m, http_major); + MESSAGE_CHECK_NUM_EQ(expected, m, http_minor); + + if (expected->type == HTTP_REQUEST) { + MESSAGE_CHECK_NUM_EQ(expected, m, method); + } else { + MESSAGE_CHECK_NUM_EQ(expected, m, status_code); + } + + MESSAGE_CHECK_NUM_EQ(expected, m, should_keep_alive); + MESSAGE_CHECK_NUM_EQ(expected, m, message_complete_on_eof); + + assert(m->message_begin_cb_called); + assert(m->headers_complete_cb_called); + assert(m->message_complete_cb_called); + + + MESSAGE_CHECK_STR_EQ(expected, m, request_url); + + /* Check URL components; we can't do this w/ CONNECT since it doesn't + * send us a well-formed URL. + */ + if (*m->request_url && m->method != HTTP_CONNECT) { + struct http_parser_url u; + + if (http_parser_parse_url(m->request_url, strlen(m->request_url), 0, &u)) { + fprintf(stderr, "\n\n*** failed to parse URL %s ***\n\n", + m->request_url); + abort(); + } + + if (expected->host) { + MESSAGE_CHECK_URL_EQ(&u, expected, m, host, UF_HOST); + } + + if (expected->userinfo) { + MESSAGE_CHECK_URL_EQ(&u, expected, m, userinfo, UF_USERINFO); + } + + m->port = (u.field_set & (1 << UF_PORT)) ? + u.port : 0; + + MESSAGE_CHECK_URL_EQ(&u, expected, m, query_string, UF_QUERY); + MESSAGE_CHECK_URL_EQ(&u, expected, m, fragment, UF_FRAGMENT); + MESSAGE_CHECK_URL_EQ(&u, expected, m, request_path, UF_PATH); + MESSAGE_CHECK_NUM_EQ(expected, m, port); + } + + if (expected->body_size) { + MESSAGE_CHECK_NUM_EQ(expected, m, body_size); + } else { + MESSAGE_CHECK_STR_EQ(expected, m, body); + } + + MESSAGE_CHECK_NUM_EQ(expected, m, num_headers); + + int r; + for (i = 0; i < m->num_headers; i++) { + r = check_str_eq(expected, "header field", expected->headers[i][0], m->headers[i][0]); + if (!r) return 0; + r = check_str_eq(expected, "header value", expected->headers[i][1], m->headers[i][1]); + if (!r) return 0; + } + + MESSAGE_CHECK_STR_EQ(expected, m, upgrade); + + return 1; +} + +/* Given a sequence of varargs messages, return the number of them that the + * parser should successfully parse, taking into account that upgraded + * messages prevent all subsequent messages from being parsed. + */ +size_t +count_parsed_messages(const size_t nmsgs, ...) { + size_t i; + va_list ap; + + va_start(ap, nmsgs); + + for (i = 0; i < nmsgs; i++) { + struct message *m = va_arg(ap, struct message *); + + if (m->upgrade) { + va_end(ap); + return i + 1; + } + } + + va_end(ap); + return nmsgs; +} + +/* Given a sequence of bytes and the number of these that we were able to + * parse, verify that upgrade bodies are correct. + */ +void +upgrade_message_fix(char *body, const size_t nread, const size_t nmsgs, ...) { + va_list ap; + size_t i; + size_t off = 0; + + va_start(ap, nmsgs); + + for (i = 0; i < nmsgs; i++) { + struct message *m = va_arg(ap, struct message *); + + off += strlen(m->raw); + + if (m->upgrade) { + off -= strlen(m->upgrade); + + /* Check the portion of the response after its specified upgrade */ + if (!check_str_eq(m, "upgrade", body + off, body + nread)) { + abort(); + } + + /* Fix up the response so that message_eq() will verify the beginning + * of the upgrade */ + *(body + nread + strlen(m->upgrade)) = '\0'; + messages[num_messages -1 ].upgrade = body + nread; + + va_end(ap); + return; + } + } + + va_end(ap); + printf("\n\n*** Error: expected a message with upgrade ***\n"); + + abort(); +} + +static void +print_error (const char *raw, size_t error_location) +{ + fprintf(stderr, "\n*** %s ***\n\n", + http_errno_description(HTTP_PARSER_ERRNO(parser))); + + int this_line = 0, char_len = 0; + size_t i, j, len = strlen(raw), error_location_line = 0; + for (i = 0; i < len; i++) { + if (i == error_location) this_line = 1; + switch (raw[i]) { + case '\r': + char_len = 2; + fprintf(stderr, "\\r"); + break; + + case '\n': + char_len = 2; + fprintf(stderr, "\\n\n"); + + if (this_line) goto print; + + error_location_line = 0; + continue; + + default: + char_len = 1; + fputc(raw[i], stderr); + break; + } + if (!this_line) error_location_line += char_len; + } + + fprintf(stderr, "[eof]\n"); + + print: + for (j = 0; j < error_location_line; j++) { + fputc(' ', stderr); + } + fprintf(stderr, "^\n\nerror location: %u\n", (unsigned int)error_location); +} + +void +test_preserve_data (void) +{ + char my_data[] = "application-specific data"; + http_parser parser; + parser.data = my_data; + http_parser_init(&parser, HTTP_REQUEST); + if (parser.data != my_data) { + printf("\n*** parser.data not preserved accross http_parser_init ***\n\n"); + abort(); + } +} + +struct url_test { + const char *name; + const char *url; + int is_connect; + struct http_parser_url u; + int rv; +}; + +const struct url_test url_tests[] = +{ {.name="proxy request" + ,.url="http://hostname/" + ,.is_connect=0 + ,.u= + {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH) + ,.port=0 + ,.field_data= + {{ 0, 4 } /* UF_SCHEMA */ + ,{ 7, 8 } /* UF_HOST */ + ,{ 0, 0 } /* UF_PORT */ + ,{ 15, 1 } /* UF_PATH */ + ,{ 0, 0 } /* UF_QUERY */ + ,{ 0, 0 } /* UF_FRAGMENT */ + ,{ 0, 0 } /* UF_USERINFO */ + } + } + ,.rv=0 + } + +, {.name="proxy request with port" + ,.url="http://hostname:444/" + ,.is_connect=0 + ,.u= + {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH) + ,.port=444 + ,.field_data= + {{ 0, 4 } /* UF_SCHEMA */ + ,{ 7, 8 } /* UF_HOST */ + ,{ 16, 3 } /* UF_PORT */ + ,{ 19, 1 } /* UF_PATH */ + ,{ 0, 0 } /* UF_QUERY */ + ,{ 0, 0 } /* UF_FRAGMENT */ + ,{ 0, 0 } /* UF_USERINFO */ + } + } + ,.rv=0 + } + +, {.name="CONNECT request" + ,.url="hostname:443" + ,.is_connect=1 + ,.u= + {.field_set=(1 << UF_HOST) | (1 << UF_PORT) + ,.port=443 + ,.field_data= + {{ 0, 0 } /* UF_SCHEMA */ + ,{ 0, 8 } /* UF_HOST */ + ,{ 9, 3 } /* UF_PORT */ + ,{ 0, 0 } /* UF_PATH */ + ,{ 0, 0 } /* UF_QUERY */ + ,{ 0, 0 } /* UF_FRAGMENT */ + ,{ 0, 0 } /* UF_USERINFO */ + } + } + ,.rv=0 + } + +, {.name="CONNECT request but not connect" + ,.url="hostname:443" + ,.is_connect=0 + ,.rv=1 + } + +, {.name="proxy ipv6 request" + ,.url="http://[1:2::3:4]/" + ,.is_connect=0 + ,.u= + {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH) + ,.port=0 + ,.field_data= + {{ 0, 4 } /* UF_SCHEMA */ + ,{ 8, 8 } /* UF_HOST */ + ,{ 0, 0 } /* UF_PORT */ + ,{ 17, 1 } /* UF_PATH */ + ,{ 0, 0 } /* UF_QUERY */ + ,{ 0, 0 } /* UF_FRAGMENT */ + ,{ 0, 0 } /* UF_USERINFO */ + } + } + ,.rv=0 + } + +, {.name="proxy ipv6 request with port" + ,.url="http://[1:2::3:4]:67/" + ,.is_connect=0 + ,.u= + {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PORT) | (1 << UF_PATH) + ,.port=67 + ,.field_data= + {{ 0, 4 } /* UF_SCHEMA */ + ,{ 8, 8 } /* UF_HOST */ + ,{ 18, 2 } /* UF_PORT */ + ,{ 20, 1 } /* UF_PATH */ + ,{ 0, 0 } /* UF_QUERY */ + ,{ 0, 0 } /* UF_FRAGMENT */ + ,{ 0, 0 } /* UF_USERINFO */ + } + } + ,.rv=0 + } + +, {.name="CONNECT ipv6 address" + ,.url="[1:2::3:4]:443" + ,.is_connect=1 + ,.u= + {.field_set=(1 << UF_HOST) | (1 << UF_PORT) + ,.port=443 + ,.field_data= + {{ 0, 0 } /* UF_SCHEMA */ + ,{ 1, 8 } /* UF_HOST */ + ,{ 11, 3 } /* UF_PORT */ + ,{ 0, 0 } /* UF_PATH */ + ,{ 0, 0 } /* UF_QUERY */ + ,{ 0, 0 } /* UF_FRAGMENT */ + ,{ 0, 0 } /* UF_USERINFO */ + } + } + ,.rv=0 + } + +, {.name="ipv4 in ipv6 address" + ,.url="http://[2001:0000:0000:0000:0000:0000:1.9.1.1]/" + ,.is_connect=0 + ,.u= + {.field_set=(1 << UF_SCHEMA) | (1 << UF_HOST) | (1 << UF_PATH) + ,.port=0 + ,.field_data= + {{ 0, 4 } /* UF_SCHEMA */ + ,{ 8, 37 } /* UF_HOST */ + ,{ 0, 0 } /* UF_PORT */ + ,{ 46, 1 } /* UF_PATH */ + ,{ 0, 0 } /* UF_QUERY */ + ,{ 0, 0 } /* UF_FRAGMENT */ + ,{ 0, 0 } /* UF_USERINFO */ + } + } + ,.rv=0 + } + +, {.name="extra ? in query string" + ,.url="http://a.tbcdn.cn/p/fp/2010c/??fp-header-min.css,fp-base-min.css," + "fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css," + "fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css" + ,.is_connect=0 + ,.u= + {.field_set=(1<field_set, u->port); + for (i = 0; i < UF_MAX; i++) { + if ((u->field_set & (1 << i)) == 0) { + printf("\tfield_data[%u]: unset\n", i); + continue; + } + + printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n\"", + i, + u->field_data[i].off, + u->field_data[i].len, + u->field_data[i].len, + url + u->field_data[i].off); + } +} + +void +test_parse_url (void) +{ + struct http_parser_url u; + const struct url_test *test; + unsigned int i; + int rv; + + for (i = 0; i < (sizeof(url_tests) / sizeof(url_tests[0])); i++) { + test = &url_tests[i]; + memset(&u, 0, sizeof(u)); + + rv = http_parser_parse_url(test->url, + strlen(test->url), + test->is_connect, + &u); + + if (test->rv == 0) { + if (rv != 0) { + printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, " + "unexpected rv %d ***\n\n", test->url, test->name, rv); + abort(); + } + + if (memcmp(&u, &test->u, sizeof(u)) != 0) { + printf("\n*** http_parser_parse_url(\"%s\") \"%s\" failed ***\n", + test->url, test->name); + + printf("target http_parser_url:\n"); + dump_url(test->url, &test->u); + printf("result http_parser_url:\n"); + dump_url(test->url, &u); + + abort(); + } + } else { + /* test->rv != 0 */ + if (rv == 0) { + printf("\n*** http_parser_parse_url(\"%s\") \"%s\" test failed, " + "unexpected rv %d ***\n\n", test->url, test->name, rv); + abort(); + } + } + } +} + +void +test_method_str (void) +{ + assert(0 == strcmp("GET", http_method_str(HTTP_GET))); + assert(0 == strcmp("", http_method_str(1337))); +} + +void +test_message (const struct message *message) +{ + size_t raw_len = strlen(message->raw); + size_t msg1len; + for (msg1len = 0; msg1len < raw_len; msg1len++) { + parser_init(message->type); + + size_t read; + const char *msg1 = message->raw; + const char *msg2 = msg1 + msg1len; + size_t msg2len = raw_len - msg1len; + + if (msg1len) { + read = parse(msg1, msg1len); + + if (message->upgrade && parser->upgrade) { + messages[num_messages - 1].upgrade = msg1 + read; + goto test; + } + + if (read != msg1len) { + print_error(msg1, read); + abort(); + } + } + + + read = parse(msg2, msg2len); + + if (message->upgrade && parser->upgrade) { + messages[num_messages - 1].upgrade = msg2 + read; + goto test; + } + + if (read != msg2len) { + print_error(msg2, read); + abort(); + } + + read = parse(NULL, 0); + + if (read != 0) { + print_error(message->raw, read); + abort(); + } + + test: + + if (num_messages != 1) { + printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name); + abort(); + } + + if(!message_eq(0, message)) abort(); + + parser_free(); + } +} + +void +test_message_count_body (const struct message *message) +{ + parser_init(message->type); + + size_t read; + size_t l = strlen(message->raw); + size_t i, toread; + size_t chunk = 4024; + + for (i = 0; i < l; i+= chunk) { + toread = MIN(l-i, chunk); + read = parse_count_body(message->raw + i, toread); + if (read != toread) { + print_error(message->raw, read); + abort(); + } + } + + + read = parse_count_body(NULL, 0); + if (read != 0) { + print_error(message->raw, read); + abort(); + } + + if (num_messages != 1) { + printf("\n*** num_messages != 1 after testing '%s' ***\n\n", message->name); + abort(); + } + + if(!message_eq(0, message)) abort(); + + parser_free(); +} + +void +test_simple (const char *buf, enum http_errno err_expected) +{ + parser_init(HTTP_REQUEST); + + size_t parsed; + int pass; + enum http_errno err; + + parsed = parse(buf, strlen(buf)); + pass = (parsed == strlen(buf)); + err = HTTP_PARSER_ERRNO(parser); + parsed = parse(NULL, 0); + pass &= (parsed == 0); + + parser_free(); + + /* In strict mode, allow us to pass with an unexpected HPE_STRICT as + * long as the caller isn't expecting success. + */ +#if HTTP_PARSER_STRICT + if (err_expected != err && err_expected != HPE_OK && err != HPE_STRICT) { +#else + if (err_expected != err) { +#endif + fprintf(stderr, "\n*** test_simple expected %s, but saw %s ***\n\n%s\n", + http_errno_name(err_expected), http_errno_name(err), buf); + abort(); + } +} + +void +test_header_overflow_error (int req) +{ + http_parser parser; + http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE); + size_t parsed; + const char *buf; + buf = req ? "GET / HTTP/1.1\r\n" : "HTTP/1.0 200 OK\r\n"; + parsed = http_parser_execute(&parser, &settings_null, buf, strlen(buf)); + assert(parsed == strlen(buf)); + + buf = "header-key: header-value\r\n"; + size_t buflen = strlen(buf); + + int i; + for (i = 0; i < 10000; i++) { + parsed = http_parser_execute(&parser, &settings_null, buf, buflen); + if (parsed != buflen) { + //fprintf(stderr, "error found on iter %d\n", i); + assert(HTTP_PARSER_ERRNO(&parser) == HPE_HEADER_OVERFLOW); + return; + } + } + + fprintf(stderr, "\n*** Error expected but none in header overflow test ***\n"); + abort(); +} + +static void +test_content_length_overflow (const char *buf, size_t buflen, int expect_ok) +{ + http_parser parser; + http_parser_init(&parser, HTTP_RESPONSE); + http_parser_execute(&parser, &settings_null, buf, buflen); + + if (expect_ok) + assert(HTTP_PARSER_ERRNO(&parser) == HPE_OK); + else + assert(HTTP_PARSER_ERRNO(&parser) == HPE_INVALID_CONTENT_LENGTH); +} + +void +test_header_content_length_overflow_error (void) +{ +#define X(size) \ + "HTTP/1.1 200 OK\r\n" \ + "Content-Length: " #size "\r\n" \ + "\r\n" + const char a[] = X(18446744073709551614); /* 2^64-2 */ + const char b[] = X(18446744073709551615); /* 2^64-1 */ + const char c[] = X(18446744073709551616); /* 2^64 */ +#undef X + test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok */ + test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */ + test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */ +} + +void +test_chunk_content_length_overflow_error (void) +{ +#define X(size) \ + "HTTP/1.1 200 OK\r\n" \ + "Transfer-Encoding: chunked\r\n" \ + "\r\n" \ + #size "\r\n" \ + "..." + const char a[] = X(FFFFFFFFFFFFFFFE); /* 2^64-2 */ + const char b[] = X(FFFFFFFFFFFFFFFF); /* 2^64-1 */ + const char c[] = X(10000000000000000); /* 2^64 */ +#undef X + test_content_length_overflow(a, sizeof(a) - 1, 1); /* expect ok */ + test_content_length_overflow(b, sizeof(b) - 1, 0); /* expect failure */ + test_content_length_overflow(c, sizeof(c) - 1, 0); /* expect failure */ +} + +void +test_no_overflow_long_body (int req, size_t length) +{ + http_parser parser; + http_parser_init(&parser, req ? HTTP_REQUEST : HTTP_RESPONSE); + size_t parsed; + size_t i; + char buf1[3000]; + size_t buf1len = sprintf(buf1, "%s\r\nConnection: Keep-Alive\r\nContent-Length: %lu\r\n\r\n", + req ? "POST / HTTP/1.0" : "HTTP/1.0 200 OK", (unsigned long)length); + parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len); + if (parsed != buf1len) + goto err; + + for (i = 0; i < length; i++) { + char foo = 'a'; + parsed = http_parser_execute(&parser, &settings_null, &foo, 1); + if (parsed != 1) + goto err; + } + + parsed = http_parser_execute(&parser, &settings_null, buf1, buf1len); + if (parsed != buf1len) goto err; + return; + + err: + fprintf(stderr, + "\n*** error in test_no_overflow_long_body %s of length %lu ***\n", + req ? "REQUEST" : "RESPONSE", + (unsigned long)length); + abort(); +} + +void +test_multiple3 (const struct message *r1, const struct message *r2, const struct message *r3) +{ + int message_count = count_parsed_messages(3, r1, r2, r3); + + char total[ strlen(r1->raw) + + strlen(r2->raw) + + strlen(r3->raw) + + 1 + ]; + total[0] = '\0'; + + strcat(total, r1->raw); + strcat(total, r2->raw); + strcat(total, r3->raw); + + parser_init(r1->type); + + size_t read; + + read = parse(total, strlen(total)); + + if (parser->upgrade) { + upgrade_message_fix(total, read, 3, r1, r2, r3); + goto test; + } + + if (read != strlen(total)) { + print_error(total, read); + abort(); + } + + read = parse(NULL, 0); + + if (read != 0) { + print_error(total, read); + abort(); + } + +test: + + if (message_count != num_messages) { + fprintf(stderr, "\n\n*** Parser didn't see 3 messages only %d *** \n", num_messages); + abort(); + } + + if (!message_eq(0, r1)) abort(); + if (message_count > 1 && !message_eq(1, r2)) abort(); + if (message_count > 2 && !message_eq(2, r3)) abort(); + + parser_free(); +} + +/* SCAN through every possible breaking to make sure the + * parser can handle getting the content in any chunks that + * might come from the socket + */ +void +test_scan (const struct message *r1, const struct message *r2, const struct message *r3) +{ + char total[80*1024] = "\0"; + char buf1[80*1024] = "\0"; + char buf2[80*1024] = "\0"; + char buf3[80*1024] = "\0"; + + strcat(total, r1->raw); + strcat(total, r2->raw); + strcat(total, r3->raw); + + size_t read; + + int total_len = strlen(total); + + int total_ops = 2 * (total_len - 1) * (total_len - 2) / 2; + int ops = 0 ; + + size_t buf1_len, buf2_len, buf3_len; + int message_count = count_parsed_messages(3, r1, r2, r3); + + int i,j,type_both; + for (type_both = 0; type_both < 2; type_both ++ ) { + for (j = 2; j < total_len; j ++ ) { + for (i = 1; i < j; i ++ ) { + + if (ops % 1000 == 0) { + printf("\b\b\b\b%3.0f%%", 100 * (float)ops /(float)total_ops); + fflush(stdout); + } + ops += 1; + + parser_init(type_both ? HTTP_BOTH : r1->type); + + buf1_len = i; + strlncpy(buf1, sizeof(buf1), total, buf1_len); + buf1[buf1_len] = 0; + + buf2_len = j - i; + strlncpy(buf2, sizeof(buf1), total+i, buf2_len); + buf2[buf2_len] = 0; + + buf3_len = total_len - j; + strlncpy(buf3, sizeof(buf1), total+j, buf3_len); + buf3[buf3_len] = 0; + + read = parse(buf1, buf1_len); + + if (parser->upgrade) goto test; + + if (read != buf1_len) { + print_error(buf1, read); + goto error; + } + + read += parse(buf2, buf2_len); + + if (parser->upgrade) goto test; + + if (read != buf1_len + buf2_len) { + print_error(buf2, read); + goto error; + } + + read += parse(buf3, buf3_len); + + if (parser->upgrade) goto test; + + if (read != buf1_len + buf2_len + buf3_len) { + print_error(buf3, read); + goto error; + } + + parse(NULL, 0); + +test: + if (parser->upgrade) { + upgrade_message_fix(total, read, 3, r1, r2, r3); + } + + if (message_count != num_messages) { + fprintf(stderr, "\n\nParser didn't see %d messages only %d\n", + message_count, num_messages); + goto error; + } + + if (!message_eq(0, r1)) { + fprintf(stderr, "\n\nError matching messages[0] in test_scan.\n"); + goto error; + } + + if (message_count > 1 && !message_eq(1, r2)) { + fprintf(stderr, "\n\nError matching messages[1] in test_scan.\n"); + goto error; + } + + if (message_count > 2 && !message_eq(2, r3)) { + fprintf(stderr, "\n\nError matching messages[2] in test_scan.\n"); + goto error; + } + + parser_free(); + } + } + } + puts("\b\b\b\b100%"); + return; + + error: + fprintf(stderr, "i=%d j=%d\n", i, j); + fprintf(stderr, "buf1 (%u) %s\n\n", (unsigned int)buf1_len, buf1); + fprintf(stderr, "buf2 (%u) %s\n\n", (unsigned int)buf2_len , buf2); + fprintf(stderr, "buf3 (%u) %s\n", (unsigned int)buf3_len, buf3); + abort(); +} + +// user required to free the result +// string terminated by \0 +char * +create_large_chunked_message (int body_size_in_kb, const char* headers) +{ + int i; + size_t wrote = 0; + size_t headers_len = strlen(headers); + size_t bufsize = headers_len + (5+1024+2)*body_size_in_kb + 6; + char * buf = malloc(bufsize); + + memcpy(buf, headers, headers_len); + wrote += headers_len; + + for (i = 0; i < body_size_in_kb; i++) { + // write 1kb chunk into the body. + memcpy(buf + wrote, "400\r\n", 5); + wrote += 5; + memset(buf + wrote, 'C', 1024); + wrote += 1024; + strcpy(buf + wrote, "\r\n"); + wrote += 2; + } + + memcpy(buf + wrote, "0\r\n\r\n", 6); + wrote += 6; + assert(wrote == bufsize); + + return buf; +} + +/* Verify that we can pause parsing at any of the bytes in the + * message and still get the result that we're expecting. */ +void +test_message_pause (const struct message *msg) +{ + char *buf = (char*) msg->raw; + size_t buflen = strlen(msg->raw); + size_t nread; + + parser_init(msg->type); + + do { + nread = parse_pause(buf, buflen); + + // We can only set the upgrade buffer once we've gotten our message + // completion callback. + if (messages[0].message_complete_cb_called && + msg->upgrade && + parser->upgrade) { + messages[0].upgrade = buf + nread; + goto test; + } + + if (nread < buflen) { + + // Not much do to if we failed a strict-mode check + if (HTTP_PARSER_ERRNO(parser) == HPE_STRICT) { + parser_free(); + return; + } + + assert (HTTP_PARSER_ERRNO(parser) == HPE_PAUSED); + } + + buf += nread; + buflen -= nread; + http_parser_pause(parser, 0); + } while (buflen > 0); + + nread = parse_pause(NULL, 0); + assert (nread == 0); + +test: + if (num_messages != 1) { + printf("\n*** num_messages != 1 after testing '%s' ***\n\n", msg->name); + abort(); + } + + if(!message_eq(0, msg)) abort(); + + parser_free(); +} + +int +main (void) +{ + parser = NULL; + int i, j, k; + int request_count; + int response_count; + + printf("sizeof(http_parser) = %u\n", (unsigned int)sizeof(http_parser)); + + for (request_count = 0; requests[request_count].name; request_count++); + for (response_count = 0; responses[response_count].name; response_count++); + + //// API + test_preserve_data(); + test_parse_url(); + test_method_str(); + + //// OVERFLOW CONDITIONS + + test_header_overflow_error(HTTP_REQUEST); + test_no_overflow_long_body(HTTP_REQUEST, 1000); + test_no_overflow_long_body(HTTP_REQUEST, 100000); + + test_header_overflow_error(HTTP_RESPONSE); + test_no_overflow_long_body(HTTP_RESPONSE, 1000); + test_no_overflow_long_body(HTTP_RESPONSE, 100000); + + test_header_content_length_overflow_error(); + test_chunk_content_length_overflow_error(); + + //// RESPONSES + + for (i = 0; i < response_count; i++) { + test_message(&responses[i]); + } + + for (i = 0; i < response_count; i++) { + test_message_pause(&responses[i]); + } + + for (i = 0; i < response_count; i++) { + if (!responses[i].should_keep_alive) continue; + for (j = 0; j < response_count; j++) { + if (!responses[j].should_keep_alive) continue; + for (k = 0; k < response_count; k++) { + test_multiple3(&responses[i], &responses[j], &responses[k]); + } + } + } + + test_message_count_body(&responses[NO_HEADERS_NO_BODY_404]); + test_message_count_body(&responses[TRAILING_SPACE_ON_CHUNKED_BODY]); + + // test very large chunked response + { + char * msg = create_large_chunked_message(31337, + "HTTP/1.0 200 OK\r\n" + "Transfer-Encoding: chunked\r\n" + "Content-Type: text/plain\r\n" + "\r\n"); + struct message large_chunked = + {.name= "large chunked" + ,.type= HTTP_RESPONSE + ,.raw= msg + ,.should_keep_alive= FALSE + ,.message_complete_on_eof= FALSE + ,.http_major= 1 + ,.http_minor= 0 + ,.status_code= 200 + ,.num_headers= 2 + ,.headers= + { { "Transfer-Encoding", "chunked" } + , { "Content-Type", "text/plain" } + } + ,.body_size= 31337*1024 + }; + test_message_count_body(&large_chunked); + free(msg); + } + + + + printf("response scan 1/2 "); + test_scan( &responses[TRAILING_SPACE_ON_CHUNKED_BODY] + , &responses[NO_BODY_HTTP10_KA_204] + , &responses[NO_REASON_PHRASE] + ); + + printf("response scan 2/2 "); + test_scan( &responses[BONJOUR_MADAME_FR] + , &responses[UNDERSTORE_HEADER_KEY] + , &responses[NO_CARRIAGE_RET] + ); + + puts("responses okay"); + + + /// REQUESTS + + test_simple("hello world", HPE_INVALID_METHOD); + test_simple("GET / HTP/1.1\r\n\r\n", HPE_INVALID_VERSION); + + + test_simple("ASDF / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD); + test_simple("PROPPATCHA / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD); + test_simple("GETA / HTTP/1.1\r\n\r\n", HPE_INVALID_METHOD); + + // Well-formed but incomplete + test_simple("GET / HTTP/1.1\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 6\r\n" + "\r\n" + "fooba", + HPE_OK); + + static const char *all_methods[] = { + "DELETE", + "GET", + "HEAD", + "POST", + "PUT", + //"CONNECT", //CONNECT can't be tested like other methods, it's a tunnel + "OPTIONS", + "TRACE", + "COPY", + "LOCK", + "MKCOL", + "MOVE", + "PROPFIND", + "PROPPATCH", + "UNLOCK", + "REPORT", + "MKACTIVITY", + "CHECKOUT", + "MERGE", + "M-SEARCH", + "NOTIFY", + "SUBSCRIBE", + "UNSUBSCRIBE", + "PATCH", + 0 }; + const char **this_method; + for (this_method = all_methods; *this_method; this_method++) { + char buf[200]; + sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method); + test_simple(buf, HPE_OK); + } + + static const char *bad_methods[] = { + "C******", + "M****", + 0 }; + for (this_method = bad_methods; *this_method; this_method++) { + char buf[200]; + sprintf(buf, "%s / HTTP/1.1\r\n\r\n", *this_method); + test_simple(buf, HPE_UNKNOWN); + } + + const char *dumbfuck2 = + "GET / HTTP/1.1\r\n" + "X-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n" + "\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n" + "\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n" + "\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n" + "\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n" + "\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n" + "\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n" + "\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n" + "\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n" + "\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n" + "\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n" + "\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n" + "\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n" + "\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n" + "\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n" + "\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n" + "\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n" + "\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n" + "\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n" + "\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n" + "\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n" + "\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n" + "\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n" + "\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n" + "\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n" + "\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n" + "\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n" + "\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n" + "\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n" + "\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n" + "\tRA==\r\n" + "\t-----END CERTIFICATE-----\r\n" + "\r\n"; + test_simple(dumbfuck2, HPE_OK); + +#if 0 + // NOTE(Wed Nov 18 11:57:27 CET 2009) this seems okay. we just read body + // until EOF. + // + // no content-length + // error if there is a body without content length + const char *bad_get_no_headers_no_body = "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\n" + "Accept: */*\r\n" + "\r\n" + "HELLO"; + test_simple(bad_get_no_headers_no_body, 0); +#endif + /* TODO sending junk and large headers gets rejected */ + + + /* check to make sure our predefined requests are okay */ + for (i = 0; requests[i].name; i++) { + test_message(&requests[i]); + } + + for (i = 0; i < request_count; i++) { + test_message_pause(&requests[i]); + } + + for (i = 0; i < request_count; i++) { + if (!requests[i].should_keep_alive) continue; + for (j = 0; j < request_count; j++) { + if (!requests[j].should_keep_alive) continue; + for (k = 0; k < request_count; k++) { + test_multiple3(&requests[i], &requests[j], &requests[k]); + } + } + } + + printf("request scan 1/4 "); + test_scan( &requests[GET_NO_HEADERS_NO_BODY] + , &requests[GET_ONE_HEADER_NO_BODY] + , &requests[GET_NO_HEADERS_NO_BODY] + ); + + printf("request scan 2/4 "); + test_scan( &requests[POST_CHUNKED_ALL_YOUR_BASE] + , &requests[POST_IDENTITY_BODY_WORLD] + , &requests[GET_FUNKY_CONTENT_LENGTH] + ); + + printf("request scan 3/4 "); + test_scan( &requests[TWO_CHUNKS_MULT_ZERO_END] + , &requests[CHUNKED_W_TRAILING_HEADERS] + , &requests[CHUNKED_W_BULLSHIT_AFTER_LENGTH] + ); + + printf("request scan 4/4 "); + test_scan( &requests[QUERY_URL_WITH_QUESTION_MARK_GET] + , &requests[PREFIX_NEWLINE_GET ] + , &requests[CONNECT_REQUEST] + ); + + puts("requests okay"); + + return 0; +} diff --git a/src/http/qhttpserver/http-parser/url_parser.c b/src/http/qhttpserver/http-parser/url_parser.c new file mode 100644 index 000000000..b1f9c979f --- /dev/null +++ b/src/http/qhttpserver/http-parser/url_parser.c @@ -0,0 +1,44 @@ +#include "http_parser.h" +#include +#include + +void +dump_url (const char *url, const struct http_parser_url *u) +{ + unsigned int i; + + printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port); + for (i = 0; i < UF_MAX; i++) { + if ((u->field_set & (1 << i)) == 0) { + printf("\tfield_data[%u]: unset\n", i); + continue; + } + + printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n", + i, + u->field_data[i].off, + u->field_data[i].len, + u->field_data[i].len, + url + u->field_data[i].off); + } +} + +int main(int argc, char ** argv) { + if (argc != 3) { + printf("Syntax : %s connect|get url\n", argv[0]); + return 1; + } + struct http_parser_url u; + int len = strlen(argv[2]); + int connect = strcmp("connect", argv[1]) == 0 ? 1 : 0; + printf("Parsing %s, connect %d\n", argv[2], connect); + + int result = http_parser_parse_url(argv[2], len, connect, &u); + if (result != 0) { + printf("Parse error : %d\n", result); + return result; + } + printf("Parse ok, result : \n"); + dump_url(argv[2], &u); + return 0; +} \ No newline at end of file diff --git a/src/http/qhttpserver/qhttpconnection.cpp b/src/http/qhttpserver/qhttpconnection.cpp new file mode 100644 index 000000000..5685dfd32 --- /dev/null +++ b/src/http/qhttpserver/qhttpconnection.cpp @@ -0,0 +1,202 @@ +/* + * Copyright 2011 Nikhil Marathe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "qhttpconnection.h" + +#include +#include +#include + +#include "qhttprequest.h" +#include "qhttpresponse.h" + +QHttpConnection::QHttpConnection(QTcpSocket *socket, QObject *parent) + : QObject(parent) + , m_socket(socket) + , m_parser(0) + , m_request(0) +{ + qDebug() << "Got new connection" << socket->peerAddress() << socket->peerPort(); + + m_parser = (http_parser*)malloc(sizeof(http_parser)); + http_parser_init(m_parser, HTTP_REQUEST); + + m_parserSettings.on_message_begin = MessageBegin; + m_parserSettings.on_url = Url; + m_parserSettings.on_header_field = HeaderField; + m_parserSettings.on_header_value = HeaderValue; + m_parserSettings.on_headers_complete = HeadersComplete; + m_parserSettings.on_body = Body; + m_parserSettings.on_message_complete = MessageComplete; + + m_parser->data = this; + + connect(socket, SIGNAL(readyRead()), this, SLOT(parseRequest())); + connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); +} + +QHttpConnection::~QHttpConnection() +{ + delete m_socket; + m_socket = 0; + + free(m_parser); + m_parser = 0; +} + +void QHttpConnection::socketDisconnected() +{ + if(m_request) { + if(m_request->successful()) { + return; + } + m_request->setSuccessful(false); + Q_EMIT m_request->end(); + } + + deleteLater(); +} + +void QHttpConnection::parseRequest() +{ + Q_ASSERT(m_parser); + + while(m_socket->bytesAvailable()) + { + QByteArray arr = m_socket->readAll(); + http_parser_execute(m_parser, &m_parserSettings, arr.constData(), arr.size()); + } +} + +void QHttpConnection::write(const QByteArray &data) +{ + m_socket->write(data); +} + +void QHttpConnection::flush() +{ + m_socket->flush(); +} + +/******************** + * Static Callbacks * + *******************/ +int QHttpConnection::MessageBegin(http_parser *parser) +{ + QHttpConnection *theConnection = (QHttpConnection *)parser->data; + theConnection->m_currentHeaders.clear(); + theConnection->m_request = new QHttpRequest(theConnection); + return 0; +} + +int QHttpConnection::HeadersComplete(http_parser *parser) +{ + QHttpConnection *theConnection = (QHttpConnection *)parser->data; + Q_ASSERT(theConnection->m_request); + + /** set method **/ + theConnection->m_request->setMethod(static_cast(parser->method)); + + /** set version **/ + theConnection->m_request->setVersion(QString("%1.%2").arg(parser->http_major).arg(parser->http_minor)); + + // Insert last remaining header + theConnection->m_currentHeaders[theConnection->m_currentHeaderField.toLower()] = theConnection->m_currentHeaderValue; + theConnection->m_request->setHeaders(theConnection->m_currentHeaders); + + /** set client information **/ + theConnection->m_request->m_remoteAddress = theConnection->m_socket->peerAddress().toString(); + theConnection->m_request->m_remotePort = theConnection->m_socket->peerPort(); + + QHttpResponse *response = new QHttpResponse(theConnection); + if( parser->http_major < 1 || parser->http_minor < 1 ) + response->m_keepAlive = false; + + connect(theConnection, SIGNAL(destroyed()), response, SLOT(connectionClosed())); + + // we are good to go! + Q_EMIT theConnection->newRequest(theConnection->m_request, response); + return 0; +} + +int QHttpConnection::MessageComplete(http_parser *parser) +{ + // TODO: do cleanup and prepare for next request + QHttpConnection *theConnection = (QHttpConnection *)parser->data; + Q_ASSERT(theConnection->m_request); + + theConnection->m_request->setSuccessful(true); + Q_EMIT theConnection->m_request->end(); + return 0; +} + +int QHttpConnection::Url(http_parser *parser, const char *at, size_t length) +{ + QHttpConnection *theConnection = (QHttpConnection *)parser->data; + Q_ASSERT(theConnection->m_request); + + QString url = QString::fromAscii(at, length); + theConnection->m_request->setUrl(QUrl(url)); + return 0; +} + +int QHttpConnection::HeaderField(http_parser *parser, const char *at, size_t length) +{ + QHttpConnection *theConnection = (QHttpConnection *)parser->data; + Q_ASSERT(theConnection->m_request); + + // insert the header we parsed previously + // into the header map + if( !theConnection->m_currentHeaderField.isEmpty() && !theConnection->m_currentHeaderValue.isEmpty() ) + { + // header names are always lower-cased + theConnection->m_currentHeaders[theConnection->m_currentHeaderField.toLower()] = theConnection->m_currentHeaderValue; + // clear header value. this sets up a nice + // feedback loop where the next time + // HeaderValue is called, it can simply append + theConnection->m_currentHeaderField = QString(); + theConnection->m_currentHeaderValue = QString(); + } + + QString fieldSuffix = QString::fromAscii(at, length); + theConnection->m_currentHeaderField += fieldSuffix; + return 0; +} + +int QHttpConnection::HeaderValue(http_parser *parser, const char *at, size_t length) +{ + QHttpConnection *theConnection = (QHttpConnection *)parser->data; + Q_ASSERT(theConnection->m_request); + + QString valueSuffix = QString::fromAscii(at, length); + theConnection->m_currentHeaderValue += valueSuffix; + return 0; +} + +int QHttpConnection::Body(http_parser *parser, const char *at, size_t length) +{ + QHttpConnection *theConnection = (QHttpConnection *)parser->data; + Q_ASSERT(theConnection->m_request); + + Q_EMIT theConnection->m_request->data(QByteArray(at, length)); + return 0; +} diff --git a/src/http/qhttpserver/qhttpconnection.h b/src/http/qhttpserver/qhttpconnection.h new file mode 100644 index 000000000..451fe642a --- /dev/null +++ b/src/http/qhttpserver/qhttpconnection.h @@ -0,0 +1,80 @@ +/* + * Copyright 2011 Nikhil Marathe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef Q_HTTP_CONNECTION +#define Q_HTTP_CONNECTION + +#include +#include + +#include "http_parser.h" + +class QTcpSocket; + +class QHttpRequest; +class QHttpResponse; + +typedef QHash HeaderHash; + +class QHttpConnection : public QObject +{ + Q_OBJECT + +public: + QHttpConnection(QTcpSocket *socket, QObject *parent = 0); + virtual ~QHttpConnection(); + + void write(const QByteArray &data); + void flush(); + +Q_SIGNALS: + void newRequest(QHttpRequest*, QHttpResponse*); + +private Q_SLOTS: + void parseRequest(); + void socketDisconnected(); + +private: + static int MessageBegin(http_parser *parser); + static int Url(http_parser *parser, const char *at, size_t length); + static int HeaderField(http_parser *parser, const char *at, size_t length); + static int HeaderValue(http_parser *parser, const char *at, size_t length); + static int HeadersComplete(http_parser *parser); + static int Body(http_parser *parser, const char *at, size_t length); + static int MessageComplete(http_parser *parser); + +private: + QTcpSocket *m_socket; + http_parser_settings m_parserSettings; + http_parser *m_parser; + + // since there can only be one request at any time + // even with pipelining + QHttpRequest *m_request; + + // the ones we are reading in from the parser + HeaderHash m_currentHeaders; + QString m_currentHeaderField; + QString m_currentHeaderValue; +}; + +#endif diff --git a/src/http/qhttpserver/qhttprequest.cpp b/src/http/qhttpserver/qhttprequest.cpp new file mode 100644 index 000000000..2ebd38834 --- /dev/null +++ b/src/http/qhttpserver/qhttprequest.cpp @@ -0,0 +1,38 @@ +/* + * Copyright 2011 Nikhil Marathe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "qhttprequest.h" + +#include "qhttpconnection.h" + +QHttpRequest::QHttpRequest(QHttpConnection *connection, QObject *parent) + : QObject(parent) + , m_connection(connection) + , m_url("http://localhost/") + , m_success(false) +{ +} + +QHttpRequest::~QHttpRequest() +{ +} + diff --git a/src/http/qhttpserver/qhttprequest.h b/src/http/qhttpserver/qhttprequest.h new file mode 100644 index 000000000..23747169d --- /dev/null +++ b/src/http/qhttpserver/qhttprequest.h @@ -0,0 +1,245 @@ +/* + * Copyright 2011 Nikhil Marathe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef Q_HTTP_REQUEST +#define Q_HTTP_REQUEST + +#include +#include +#include +#include +#include + +class QTcpSocket; + +class QHttpConnection; + +typedef QHash HeaderHash; + +/* Request Methods */ + +/*! \class QHttpRequest + * + * The QHttpRequest class represents the header and data + * sent by the client. + * + * Header data is available immediately. + * + * Body data is streamed as it comes in via the data(const QByteArray&) signal. + * As a consequence the application's request callback should ensure that it + * connects to the data() signal before control returns back to the event loop. + * Otherwise there is a risk of some data never being received by the + * application. + * + * The class is read-only by users of %QHttpServer. + */ +class QHttpRequest : public QObject +{ + Q_OBJECT + + Q_PROPERTY(HeaderHash headers READ headers); + Q_PROPERTY(QString remoteAddress READ remoteAddress); + Q_PROPERTY(quint16 remotePort READ remotePort); + Q_PROPERTY(QString method READ method); + Q_PROPERTY(QUrl url READ url); + Q_PROPERTY(QString path READ path); + Q_PROPERTY(QString httpVersion READ httpVersion); + Q_ENUMS(HttpMethod); + +public: + virtual ~QHttpRequest(); + + /*! + * Request Methods + * Taken from http_parser.h -- make sure to keep synced + */ + enum HttpMethod { + HTTP_DELETE = 0, + HTTP_GET, + HTTP_HEAD, + HTTP_POST, + HTTP_PUT, + /* pathological */ + HTTP_CONNECT, + HTTP_OPTIONS, + HTTP_TRACE, + /* webdav */ + HTTP_COPY, + HTTP_LOCK, + HTTP_MKCOL, + HTTP_MOVE, + HTTP_PROPFIND, + HTTP_PROPPATCH, + HTTP_SEARCH, + HTTP_UNLOCK, + /* subversion */ + HTTP_REPORT, + HTTP_MKACTIVITY, + HTTP_CHECKOUT, + HTTP_MERGE, + /* upnp */ + HTTP_MSEARCH, + HTTP_NOTIFY, + HTTP_SUBSCRIBE, + HTTP_UNSUBSCRIBE, + /* RFC-5789 */ + HTTP_PATCH, + HTTP_PURGE + }; + + /*! + * Returns the method string for the request + */ + const QString methodString() const { return MethodToString(method()); } + + /*! + * The method used for the request. + */ + HttpMethod method() const { return m_method; }; + + /*! + * The complete URL for the request. This + * includes the path and query string. + * + */ + const QUrl& url() const { return m_url; }; + + /*! + * The path portion of the query URL. + * + * \sa url() + */ + const QString path() const { return m_url.path(); }; + + /*! + * The HTTP version used by the client as a + * 'x.x' string. + */ + const QString& httpVersion() const { return m_version; }; + + /*! + * Any query string included as part of a request. + * Usually used to send data in a GET request. + */ + const QString& queryString() const; + + /*! + * Get a hash of the headers sent by the client. + * NOTE: All header names are lowercase + * so that Content-Length becomes content-length and so on. + * + * This returns a reference! If you want to store headers + * somewhere else, where the request may be deleted, + * make sure you store them as a copy. + */ + const HeaderHash& headers() const { return m_headers; }; + + /*! + * Get the value of a header + * + * \param field Name of the header field (lowercase). + * \return Value of the header or null QString() + */ + QString header(const QString &field) { return m_headers[field]; }; + + /*! + * IP Address of the client in dotted decimal format + */ + const QString& remoteAddress() const { return m_remoteAddress; }; + + /*! + * Outbound connection port for the client. + */ + quint16 remotePort() const { return m_remotePort; }; + + /*! + * Post data + */ + const QByteArray &body() const { return m_body; } + + /*! + * Set immediately before end has been emitted, + * stating whether the message was properly received. + * Defaults to false untiil the message has completed. + */ + bool successful() const { return m_success; } + + /*! + * connect to data and store all data in a QByteArray + * accessible at body() + */ + void storeBody() + { + connect(this, SIGNAL(data(const QByteArray &)), + this, SLOT(appendBody(const QByteArray &)), + Qt::UniqueConnection); + } + +Q_SIGNALS: + /*! + * This signal is emitted whenever body data is encountered + * in a message. + * This may be emitted zero or more times. + */ + void data(const QByteArray &); + + /*! + * Emitted at the end of the HTTP request. + * No data() signals will be emitted after this. + */ + void end(); + +private: + QHttpRequest(QHttpConnection *connection, QObject *parent = 0); + + static QString MethodToString(HttpMethod method) + { + int index = staticMetaObject.indexOfEnumerator("HttpMethod"); + return staticMetaObject.enumerator(index).valueToKey(method); + } + + void setMethod(HttpMethod method) { m_method = method; } + void setVersion(const QString &version) { m_version = version; } + void setUrl(const QUrl &url) { m_url = url; } + void setHeaders(const HeaderHash headers) { m_headers = headers; } + void setSuccessful(bool success) { m_success = success; } + + QHttpConnection *m_connection; + HeaderHash m_headers; + HttpMethod m_method; + QUrl m_url; + QString m_version; + QString m_remoteAddress; + quint16 m_remotePort; + QByteArray m_body; + bool m_success; + + friend class QHttpConnection; + + private Q_SLOTS: + void appendBody(const QByteArray &body) + { + m_body.append(body); + } +}; + +#endif diff --git a/src/http/qhttpserver/qhttpresponse.cpp b/src/http/qhttpserver/qhttpresponse.cpp new file mode 100644 index 000000000..a713c12f1 --- /dev/null +++ b/src/http/qhttpserver/qhttpresponse.cpp @@ -0,0 +1,193 @@ +/* + * Copyright 2011 Nikhil Marathe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "qhttpresponse.h" + +#include + +#include "qhttpserver.h" +#include "qhttpconnection.h" + +QHttpResponse::QHttpResponse(QHttpConnection *connection) + // TODO: parent child relation + : QObject(0) + , m_connection(connection) + , m_headerWritten(false) + , m_sentConnectionHeader(false) + , m_sentContentLengthHeader(false) + , m_sentTransferEncodingHeader(false) + , m_sentDate(false) + , m_keepAlive(true) + , m_last(false) + , m_useChunkedEncoding(false) + , m_finished(false) +{ +} + +QHttpResponse::~QHttpResponse() +{ +} + +void QHttpResponse::setHeader(const QString &field, const QString &value) +{ + if(m_finished) { + return; + } + + m_headers[field] = value; +} + +void QHttpResponse::writeHeader(const char *field, const QString &value) +{ + if(m_finished) { + return; + } + + m_connection->write(field); + m_connection->write(": "); + m_connection->write(value.toUtf8()); + m_connection->write("\r\n"); +} + +void QHttpResponse::writeHeaders() +{ + if(m_finished) { + return; + } + + Q_FOREACH(QString name, m_headers.keys()) + { + QString value = m_headers[name]; + if( name.compare("connection", Qt::CaseInsensitive) == 0 ) + { + m_sentConnectionHeader = true; + if( value == "close" ) + m_last = true; + else + m_keepAlive = true; + } + else if( name.compare("transfer-encoding", Qt::CaseInsensitive) == 0 ) + { + m_sentTransferEncodingHeader = true; + if( value == "chunked" ) + m_useChunkedEncoding = true; + } + else if( name.compare("content-length", Qt::CaseInsensitive) == 0 ) + { + m_sentContentLengthHeader = true; + } + else if( name.compare("date", Qt::CaseInsensitive) == 0 ) + { + m_sentDate = true; + } + //TODO: Expect case + + writeHeader(name.toAscii(), value.toAscii()); + } + + if( !m_sentConnectionHeader ) + { + if( m_keepAlive && + ( m_sentContentLengthHeader || m_useChunkedEncoding ) ) + { + writeHeader("Connection", "keep-alive"); + } + else + { + m_last = true; + writeHeader("Connection", "close"); + } + } + + if( !m_sentContentLengthHeader && !m_sentTransferEncodingHeader ) + { + if( m_useChunkedEncoding ) + writeHeader("Transfer-Encoding", "chunked"); + else + m_last = true; + } + + if( !m_sentDate ) + { + writeHeader("Date", QDateTime::currentDateTimeUtc().toString("ddd, dd MMM yyyy hh:mm:ss G'M'T")); + } +} + +void QHttpResponse::writeHead(int status) +{ + if(m_finished) { + return; + } + + if( m_headerWritten ) return; + + m_connection->write(QString("HTTP/1.1 %1 %2\r\n").arg(status).arg(STATUS_CODES[status]).toAscii()); + + writeHeaders(); + + m_connection->write("\r\n"); + m_headerWritten = true; +} + +void QHttpResponse::write(const QByteArray &data) +{ + if(m_finished) { + return; + } + + if( !m_headerWritten ) + { + qDebug() << "You MUST call writeHead() before writing body data"; + return; + } + + m_connection->write(data); +} + +void QHttpResponse::write(const QString &data) +{ + if(m_finished) { + return; + } + + m_connection->write(data.toUtf8()); +} + +void QHttpResponse::end(const QString &data) +{ + if(m_finished) { + return; + } + m_finished = true; + + write(data); + + Q_EMIT done(); + deleteLater(); + // TODO: end connection and delete ourselves +} + +void QHttpResponse::connectionClosed() +{ + m_finished = true; + deleteLater(); +} diff --git a/src/http/qhttpserver/qhttpresponse.h b/src/http/qhttpserver/qhttpresponse.h new file mode 100644 index 000000000..40a92c271 --- /dev/null +++ b/src/http/qhttpserver/qhttpresponse.h @@ -0,0 +1,174 @@ +/* + * Copyright 2011 Nikhil Marathe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef Q_HTTP_RESPONSE +#define Q_HTTP_RESPONSE + +#include +#include + +// +class QTcpSocket; + +class QHttpConnection; + +typedef QHash HeaderHash; + +/*! + * The QHttpResponse class handles sending + * data back to the client in response to a request. + * + * The way to respond is to: + *
    + *
  1. Set headers (optional).
  2. + *
  3. Call writeHead() with the HTTP status code.
  4. + *
  5. Call write() zero or more times.
  6. + *
  7. Call end() when you are ready to end the request.
  8. + *
+ * + */ +class QHttpResponse : public QObject +{ + Q_OBJECT + +public: + enum StatusCode { + STATUS_CONTINUE = 100, + STATUS_SWITCH_PROTOCOLS = 101, + STATUS_OK = 200, + STATUS_CREATED = 201, + STATUS_ACCEPTED = 202, + STATUS_NON_AUTHORITATIVE_INFORMATION = 203, + STATUS_NO_CONTENT = 204, + STATUS_RESET_CONTENT = 205, + STATUS_PARTIAL_CONTENT = 206, + STATUS_MULTIPLE_CHOICES = 300, + STATUS_MOVED_PERMANENTLY = 301, + STATUS_FOUND = 302, + STATUS_SEE_OTHER = 303, + STATUS_NOT_MODIFIED = 304, + STATUS_USE_PROXY = 305, + STATUS_TEMPORARY_REDIRECT = 307, + STATUS_BAD_REQUEST = 400, + STATUS_UNAUTHORIZED = 401, + STATUS_PAYMENT_REQUIRED = 402, + STATUS_FORBIDDEN = 403, + STATUS_NOT_FOUND = 404, + STATUS_METHOD_NOT_ALLOWED = 405, + STATUS_NOT_ACCEPTABLE = 406, + STATUS_PROXY_AUTHENTICATION_REQUIRED = 407, + STATUS_REQUEST_TIMEOUT = 408, + STATUS_CONFLICT = 409, + STATUS_GONE = 410, + STATUS_LENGTH_REQUIRED = 411, + STATUS_PRECONDITION_FAILED = 412, + STATUS_REQUEST_ENTITY_TOO_LARGE = 413, + STATUS_REQUEST_URI_TOO_LONG = 414, + STATUS_REQUEST_UNSUPPORTED_MEDIA_TYPE = 415, + STATUS_REQUESTED_RANGE_NOT_SATISFIABLE = 416, + STATUS_EXPECTATION_FAILED = 417, + STATUS_INTERNAL_SERVER_ERROR = 500, + STATUS_NOT_IMPLEMENTED = 501, + STATUS_BAD_GATEWAY = 502, + STATUS_SERVICE_UNAVAILABLE = 503, + STATUS_GATEWAY_TIMEOUT = 504, + STATUS_HTTP_VERSION_NOT_SUPPORTED = 505 + }; + + virtual ~QHttpResponse(); + +public Q_SLOTS: + /*! + * Write the header of the response + * using @c status as the response status + * code. Any headers should be set before this + * is called. + */ + void writeHead(int status); + + /*! + * Write the block of data to the client. + * + * \note + * writeHead() has to be called before write(), otherwise the call will + * fail. + */ + void write(const QByteArray &data); + + /*! + * Write a QString instead of a QByteArray. + * \see write(const QByteArray &); + */ + void write(const QString &data); + + /*! + * End the response. Data will be flushed + * to the underlying socket and the connection + * itself will be closed if this is the last + * response. + * + * This will emit done() and queue this object + * for deletion. For details see \ref memorymanagement + */ + void end(const QString &data=QString()); + + /*! + * Set a response header @c field to @c value + */ + void setHeader(const QString &field, const QString &value); + +Q_SIGNALS: + /*! + * Emitted once the response is finished. + * You should NOT interact with this object + * after done() has been emitted as the object + * is scheduled for deletion at any time. + */ + void done(); + +private: + QHttpResponse(QHttpConnection *connection); + + void writeHeaders(); + void writeHeader(const char *field, const QString &value); + + QHttpConnection *m_connection; + + bool m_headerWritten; + HeaderHash m_headers; + friend class QHttpConnection; + + bool m_sentConnectionHeader; + bool m_sentContentLengthHeader; + bool m_sentTransferEncodingHeader; + bool m_sentDate; + bool m_keepAlive; + bool m_last; + bool m_useChunkedEncoding; + bool m_finished; + +private Q_SLOTS: + void connectionClosed(); + +}; + +#endif diff --git a/src/http/qhttpserver/qhttpserver.cpp b/src/http/qhttpserver/qhttpserver.cpp new file mode 100644 index 000000000..13d62e33d --- /dev/null +++ b/src/http/qhttpserver/qhttpserver.cpp @@ -0,0 +1,125 @@ +/* + * Copyright 2011 Nikhil Marathe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "qhttpserver.h" + +#include +#include +#include +#include + +#include "qhttpconnection.h" + +QHash STATUS_CODES; + +QHttpServer::QHttpServer(QObject *parent) + : QObject(parent) + , m_tcpServer(0) +{ +#define STATUS_CODE(num, reason) STATUS_CODES.insert(num, reason); +// {{{ + STATUS_CODE(100, "Continue") + STATUS_CODE(101, "Switching Protocols") + STATUS_CODE(102, "Processing") // RFC 2518) obsoleted by RFC 4918 + STATUS_CODE(200, "OK") + STATUS_CODE(201, "Created") + STATUS_CODE(202, "Accepted") + STATUS_CODE(203, "Non-Authoritative Information") + STATUS_CODE(204, "No Content") + STATUS_CODE(205, "Reset Content") + STATUS_CODE(206, "Partial Content") + STATUS_CODE(207, "Multi-Status") // RFC 4918 + STATUS_CODE(300, "Multiple Choices") + STATUS_CODE(301, "Moved Permanently") + STATUS_CODE(302, "Moved Temporarily") + STATUS_CODE(303, "See Other") + STATUS_CODE(304, "Not Modified") + STATUS_CODE(305, "Use Proxy") + STATUS_CODE(307, "Temporary Redirect") + STATUS_CODE(400, "Bad Request") + STATUS_CODE(401, "Unauthorized") + STATUS_CODE(402, "Payment Required") + STATUS_CODE(403, "Forbidden") + STATUS_CODE(404, "Not Found") + STATUS_CODE(405, "Method Not Allowed") + STATUS_CODE(406, "Not Acceptable") + STATUS_CODE(407, "Proxy Authentication Required") + STATUS_CODE(408, "Request Time-out") + STATUS_CODE(409, "Conflict") + STATUS_CODE(410, "Gone") + STATUS_CODE(411, "Length Required") + STATUS_CODE(412, "Precondition Failed") + STATUS_CODE(413, "Request Entity Too Large") + STATUS_CODE(414, "Request-URI Too Large") + STATUS_CODE(415, "Unsupported Media Type") + STATUS_CODE(416, "Requested Range Not Satisfiable") + STATUS_CODE(417, "Expectation Failed") + STATUS_CODE(418, "I\"m a teapot") // RFC 2324 + STATUS_CODE(422, "Unprocessable Entity") // RFC 4918 + STATUS_CODE(423, "Locked") // RFC 4918 + STATUS_CODE(424, "Failed Dependency") // RFC 4918 + STATUS_CODE(425, "Unordered Collection") // RFC 4918 + STATUS_CODE(426, "Upgrade Required") // RFC 2817 + STATUS_CODE(500, "Internal Server Error") + STATUS_CODE(501, "Not Implemented") + STATUS_CODE(502, "Bad Gateway") + STATUS_CODE(503, "Service Unavailable") + STATUS_CODE(504, "Gateway Time-out") + STATUS_CODE(505, "HTTP Version not supported") + STATUS_CODE(506, "Variant Also Negotiates") // RFC 2295 + STATUS_CODE(507, "Insufficient Storage") // RFC 4918 + STATUS_CODE(509, "Bandwidth Limit Exceeded") + STATUS_CODE(510, "Not Extended") // RFC 2774 +// }}} +} + +QHttpServer::~QHttpServer() +{ +} + +void QHttpServer::newConnection() +{ + Q_ASSERT(m_tcpServer); + while(m_tcpServer->hasPendingConnections()) { + QHttpConnection *connection = new QHttpConnection(m_tcpServer->nextPendingConnection(), this); + connect(connection, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)), + this, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*))); + } +} + +bool QHttpServer::listen(const QHostAddress &address, quint16 port) +{ + m_tcpServer = new QTcpServer; + + connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection())); + return m_tcpServer->listen(address, port); +} + +bool QHttpServer::listen(quint16 port) +{ + return listen(QHostAddress::Any, port); +} + +void QHttpServer::close() +{ + m_tcpServer->close(); +} diff --git a/src/http/qhttpserver/qhttpserver.h b/src/http/qhttpserver/qhttpserver.h new file mode 100644 index 000000000..bfe88051e --- /dev/null +++ b/src/http/qhttpserver/qhttpserver.h @@ -0,0 +1,230 @@ +/* + * Copyright 2011 Nikhil Marathe + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef Q_HTTP_SERVER +#define Q_HTTP_SERVER + +#define QHTTPSERVER_VERSION_MAJOR 0 +#define QHTTPSERVER_VERSION_MINOR 1 +#define QHTTPSERVER_VERSION_PATCH 0 + +#include +#include + +class QTcpServer; + +class QHttpRequest; +class QHttpResponse; + +/*! + * A map of request or response headers + */ +typedef QHash HeaderHash; + +/*! + * Maps status codes to string reason phrases + */ +extern QHash STATUS_CODES; + +/*! \mainpage %QHttpServer Documentation + * + * \section introduction Introduction + * + * %QHttpServer is a easy to use, fast and light-weight + * HTTP Server suitable for C++ web applications backed + * by Qt. Since C++ web applications are pretty uncommon + * the market for this project is pretty low. + * + * But integrating this with a module like QtScript + * and using it to write JavaScript web applications is + * a tempting possibility, and something that I want to + * demonstrate at conf.kde.in 2011. + * + * %QHttpServer uses a signal-slots based mechanism + * for all communication, so no inheritance is required. + * It tries to be as asynchronous as possible, to the + * extent that request body data is also delivered as and + * when it is received over the socket via signals. This + * kind of programming may take some getting used to. + * + * %QHttpServer is backed by Ryan + * Dahl's secure and fast http parser which makes it streaming + * till the lowest level. + * + * \section usage Usage + * + * Using %QHttpServer is very simple. Simply create a QHttpServer, + * connect a slot to the newRequest() signal and use the request and + * response objects. + * See the QHttpServer class documentation for an example. + * + * \example helloworld/helloworld.cpp + * \example helloworld/helloworld.h + * \example greeting/greeting.cpp + * \example greeting/greeting.h + * \example bodydata/bodydata.cpp + * \example bodydata/bodydata.h + */ + +/*! \class QHttpServer + * The QHttpServer class forms the basis of the %QHttpServer + * project. It is a fast, non-blocking HTTP server. + * + * These are the steps to create a server and respond to requests. + * + *
    + *
  1. Create an instance of QHttpServer.
  2. + *
  3. Connect a slot to the newRequest(QHttpRequest*, QHttpResponse*) + * signal.
  4. + *
  5. Create a QCoreApplication to drive the server event loop.
  6. + *
  7. Respond to clients by writing out to the QHttpResponse object.
  8. + *
+ * + * helloworld.cpp + * \include helloworld/helloworld.cpp + * helloworld.h + * \include helloworld/helloworld.h + * + */ +class QHttpServer : public QObject +{ + Q_OBJECT + +public: + /*! + * Create a new HTTP Server + */ + QHttpServer(QObject *parent = 0); + virtual ~QHttpServer(); + + /*! + * Start the server bound to the @c address and @c port. + * This function returns immediately! + * + * \param address Address on which to listen to. Default is to listen on + * all interfaces which means the server can be accessed from anywhere. + * \param port Port number on which the server should run. + * \return true if the server was started successfully, false otherwise. + * \sa listen(quint16) + */ + bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port=0); + + /*! + * Starts the server on @c port listening on all interfaces. + * + * \param port Port number on which the server should run. + * \return true if the server was started successfully, false otherwise. + * \sa listen(const QHostAddress&, quint16) + */ + bool listen(quint16 port); + + /*! + * Stop listening for connections + */ + void close(); + +Q_SIGNALS: + /*! + * This signal is emitted whenever a client + * makes a new request to the server. + * + * The slot should use the @c request and @c response + * objects to communicate with the client. + * + * \section memorymanagement Memory Management + * + * The QHttpRequest and QHttpResponse deletion policies + * are such. + * + * QHttpRequest is never deleted by %QHttpServer. + * Since it is not possible to determine till what point the application + * may want access to its data, it is up to the application to delete it. + * A recommended way to handle this is to create a new responder object for + * every request and to delete the request in that object's destructor. The + * object itself can be deleted by connecting to QHttpResponse's done() + * slot as explained below. + * + * You should NOT delete the QHttpRequest object until it + * has emitted an QHttpRequest::end() signal. + * + * QHttpResponse queues itself up for auto-deletion once the application + * calls its end() method. Once the data has been flushed to the underlying + * socket, the object will emit a QHttpResponse::done() signal before queueing itself up + * for deletion. You should NOT interact with the response + * object once it has emitted QHttpResponse::done() although actual deletion does not + * happen until QHttpResponse::destroyed() is emitted. + * QHttpResponse::done() serves as a useful way to handle memory management of the + * application itself. For example: + * + * \code + * MyApp::MyApp() + * : QObject(0) + * { + * QHttpServer *s = new QHttpServer; + * connect(s, SIGNAL(newRequest(...)), this, SLOT(handle(...))); + * s.listen(8000); + * } + * + * void MyApp::handle(QHttpRequest *request, QHttpResponse *response) + * { + * if( request->url() matches a route ) + * new Responder(request, response); + * else + * new PageNotFound(request, response); + * } + * + * ... + * + * Responder::Responder(QHttpRequest *request, QHttpResponse *response) + * { + * m_request = request; + * + * connect(request, SIGNAL(end()), response, SLOT(end())); + * // Once the request is complete, the response is ended. + * // when the response ends, it deletes itself + * // the Responder object connects to done() + * // which will lead to it being deleted + * // and this will delete the request. + * // So all 3 are properly deleted. + * connect(response, SIGNAL(done()), this, SLOT(deleteLater())); + * response->writeHead(200); + * response->write("Quitting soon"); + * } + * + * Responder::~Responder() + * { + * delete m_request; + * m_request = 0; + * } + * \endcode + * + */ + void newRequest(QHttpRequest *request, QHttpResponse *response); + +private Q_SLOTS: + void newConnection(); + +private: + QTcpServer *m_tcpServer; +}; + +#endif diff --git a/src/http/qjson/.gitignore b/src/http/qjson/.gitignore new file mode 100644 index 000000000..a3ed5a764 --- /dev/null +++ b/src/http/qjson/.gitignore @@ -0,0 +1,7 @@ +lib +Makefile +doc/html + +# -- kdevelop +.kdev4 +*.kdev4 diff --git a/src/http/qjson/CMakeLists.txt b/src/http/qjson/CMakeLists.txt new file mode 100644 index 000000000..2e7d7258e --- /dev/null +++ b/src/http/qjson/CMakeLists.txt @@ -0,0 +1,121 @@ +PROJECT(qjson) + +set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DQT_NO_CAST_FROM_ASCII -DQT_NO_CAST_TO_ASCII" ) + +# Force cmake 2.8.8 in order to have a decent support of Qt5 +CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8) +CMAKE_POLICY(SET CMP0003 NEW) + +# Do not link against qtmain on Windows +cmake_policy(SET CMP0020 OLD) + +set(CMAKE_INSTALL_NAME_DIR ${LIB_INSTALL_DIR}) + +IF("${CMAKE_BUILD_TYPE}" MATCHES "^Rel.*") + ADD_DEFINITIONS("-DQT_NO_DEBUG_OUTPUT") +ENDIF("${CMAKE_BUILD_TYPE}" MATCHES "^Rel.*") + +# Ability to disable verbose debug output +IF(QJSON_VERBOSE_DEBUG_OUTPUT) + ADD_DEFINITIONS("-DQJSON_VERBOSE_DEBUG_OUTPUT") +endif(QJSON_VERBOSE_DEBUG_OUTPUT) + +# On Windows debug library should have 'd' postfix. +IF (WIN32) + SET(CMAKE_DEBUG_POSTFIX "d") +ENDIF (WIN32) + +OPTION(OSX_FRAMEWORK "Build a Mac OS X Framework") +SET(FRAMEWORK_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/Library/Frameworks" + CACHE PATH "Where to place qjson.framework if OSX_FRAMEWORK is selected") + +# Don't use absolute path in qjson-targets-*.cmake +# (This will have no effect with CMake < 2.8) +SET(QT_USE_IMPORTED_TARGETS TRUE) + +option(QT4_BUILD "Force building with Qt4 even if Qt5 is found") +IF (NOT QT4_BUILD) + FIND_PACKAGE( Qt5Core QUIET ) +ENDIF() + +IF (Qt5Core_FOUND) + MESSAGE ("Qt5 found") + + INCLUDE_DIRECTORIES(${Qt5Core_INCLUDE_DIRS}) + ADD_DEFINITIONS(${Qt5Core_DEFINITIONS}) + + # Tell CMake to run moc when necessary: + set(CMAKE_AUTOMOC ON) + # As moc files are generated in the binary dir, tell CMake + # to always look for includes there: + set(CMAKE_INCLUDE_CURRENT_DIR ON) +ELSE() + MESSAGE ("Qt5 not found, searching for Qt4") + # Find Qt4 + FIND_PACKAGE( Qt4 REQUIRED ) + + # Include the cmake file needed to use qt4 + INCLUDE( ${QT_USE_FILE} ) + +ENDIF() + +IF (NOT WIN32) + SET( QT_DONT_USE_QTGUI TRUE ) +ENDIF() + + +#add extra search paths for libraries and includes +SET (LIB_SUFFIX "" CACHE STRING "Define suffix of directory name (32/64)" ) +SET (LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}" CACHE STRING "Directory where lib will install") +SET (INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "The directory the headers are installed in") +SET (CMAKECONFIG_INSTALL_DIR "${LIB_INSTALL_DIR}/cmake/${CMAKE_PROJECT_NAME}" CACHE PATH "Directory where to install QJSONConfig.cmake") + +set(QJSON_LIB_MAJOR_VERSION "0") +set(QJSON_LIB_MINOR_VERSION "8") +set(QJSON_LIB_PATCH_VERSION "1") + +set(QJSON_LIB_VERSION_STRING "${QJSON_LIB_MAJOR_VERSION}.${QJSON_LIB_MINOR_VERSION}.${QJSON_LIB_PATCH_VERSION}") + +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" ) + +# pkg-config +IF (NOT WIN32) + CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/QJson.pc.in + ${CMAKE_CURRENT_BINARY_DIR}/QJson.pc + @ONLY) + INSTALL (FILES ${CMAKE_CURRENT_BINARY_DIR}/QJson.pc + DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) +ENDIF (NOT WIN32) + +# Subdirs +ADD_SUBDIRECTORY(src) +IF (KDE4_BUILD_TESTS OR QJSON_BUILD_TESTS) + enable_testing() + ADD_SUBDIRECTORY(tests) +ENDIF (KDE4_BUILD_TESTS OR QJSON_BUILD_TESTS) + +CONFIGURE_FILE( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) + +INSTALL(EXPORT qjson-export DESTINATION ${CMAKECONFIG_INSTALL_DIR} FILE QJSONTargets.cmake) + +# figure out the relative path from the installed Config.cmake file to the install prefix (which may be at +# runtime different from the chosen CMAKE_INSTALL_PREFIX if under Windows the package was installed anywhere) +# This relative path will be configured into the QJSONConfig.cmake +file(RELATIVE_PATH relInstallDir ${CMAKE_INSTALL_PREFIX}/${CMAKECONFIG_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX} ) + +# cmake-modules +CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/QJSONConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/QJSONConfig.cmake + @ONLY) +CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/QJSONConfigVersion.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/QJSONConfigVersion.cmake + @ONLY) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/QJSONConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/QJSONConfigVersion.cmake + DESTINATION "${CMAKECONFIG_INSTALL_DIR}") + +ADD_CUSTOM_TARGET(uninstall + "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") diff --git a/src/http/qjson/COPYING.lib b/src/http/qjson/COPYING.lib new file mode 100644 index 000000000..08f25cd2f --- /dev/null +++ b/src/http/qjson/COPYING.lib @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1, as published by the Free Software Foundation. + + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/src/http/qjson/ChangeLog b/src/http/qjson/ChangeLog new file mode 100644 index 000000000..817d5849d --- /dev/null +++ b/src/http/qjson/ChangeLog @@ -0,0 +1,103 @@ +Mon Jan 28 23:01:40 CET 2013 Flavio Castelli + + * Fix compilation on BlackBerry 10. + +Tue Nov 27 11:04:12 CET 2012 Flavio Castelli + + * Relase 0.8.1: + - ensure API and ABI compatibility with 0.7.1 + +Thu Nov 22 21:20:11 CET 2012 Flavio Castelli + + * Fix unsafe pointer usage in Serializer::serialize() + +---------------------------------------------------------------------- +Wed Nov 21 22:01:51 CET 2012 Flavio Castelli + + * Version 0.8.0 released + +Tue Nov 20 11:19:49 CET 2012 Flavio Castelli + + * Serializer: handle QVariantHash + +Tue Oct 30 15:50:10 CET 2012 Flavio Castelli + + * Improve error handling inside of Serializer + * Serializer: handle quint16. + +Tue Jan 31 10:15:06 CET 2012 Flavio Castelli + + * Make possible to build qjson as an OS X framework. + +Fri Nov 04 16:50:56 CET 2011 Flavio Castelli + + * Make possible to set double precision during serialization. + +Wed Aug 24 17:58:56 CEST 2011 Flavio Castelli + + * Buildsystem adjustments, fix issues mentioned here: + - http://lists.kde.org/?l=kde-buildsystem&m=130947194605100&w=3 + - http://lists.kde.org/?l=kde-buildsystem&m=128835747626464&w=3 + The biggest difference now is that FindQJSON.cmake is not provided + anymore. Instead, QJSONConfig.cmake and QJSONConfigVersion.cmake are + installed and can be used in find_package(QJSON) calls. Applications + using QJson can write their own FindQJSON.cmake files if they need to. + +Fri Apr 23:04:29 CEST 2011 Flavio Castelli + + * Fixed QVariant de-serialization. QVariant properties were ignored + during QVariant -> QObject conversion. + +Sun Dec 18:59:28 CET 2010 Flavio Castelli + + * It's now possible to indent the output produced by the Serializer. + +Mon Sep 06 18:53:02 CEST 2010 Flavio Castelli + + * 50% performance improvement when parsing numbers. + +Sun Jul 04 15:41:08 CEST 2010 Flavio Castelli + + * fix make install when not installing as root + * provide "make uninstall" + +Tue Jun 15 13:16:57 CEST 2010 Flavio Castelli + + * Allow top level values + +---------------------------------------------------------------------- +Sat Mar 13 23:57:00 CEST 2009 - flavio@castelli.name + + * Merged the symbian branch into master, + +---------------------------------------------------------------------- +Sun Oct 11 19:18:00 CEST 2009 - flavio@castelli.name + + * Updated to 0.6.3: fixed a bug affecting ulonglong numbers serialization. + +------------------------------------------------------------------- +Wed Sep 15 19:21:00 CEST 2009 - flavio@castelli.name + + * Updated to 0.6.2: fixed a bug affecting ulonglong numbers parsing. + +------------------------------------------------------------------- +Wed Sep 09 09:55:00 CEST 2009 - flavio@castelli.name + + * Updated to 0.6.1: relevant bugs fixed. + * Moved the SerializerRunnable class inside QJson namespace. + * Fixed a bug in cmdline_tester. + +------------------------------------------------------------------- +Mon Jul 20 15:24:32 CEST 2009 - prusnak@suse.cz + + * Updated to 0.6.0 (KDE SVN rev 999750). + +------------------------------------------------------------------- +Mon Apr 07 00:00:00 UTC 2009 - flavio@castelli.name + + * Released 0.5.1 - added unicode support. + +------------------------------------------------------------------- +Mon Apr 03 00:00:00 UTC 2009 - flavio@castelli.name + + * First release. diff --git a/src/http/qjson/QJSONConfig.cmake.in b/src/http/qjson/QJSONConfig.cmake.in new file mode 100644 index 000000000..def072cbb --- /dev/null +++ b/src/http/qjson/QJSONConfig.cmake.in @@ -0,0 +1,6 @@ +GET_FILENAME_COMPONENT(myDir ${CMAKE_CURRENT_LIST_FILE} PATH) + +SET(QJSON_LIBRARIES qjson) +SET(QJSON_INCLUDE_DIR "@INCLUDE_INSTALL_DIR@") + +include(${myDir}/QJSONTargets.cmake) diff --git a/src/http/qjson/QJSONConfigVersion.cmake.in b/src/http/qjson/QJSONConfigVersion.cmake.in new file mode 100644 index 000000000..6005d2dc8 --- /dev/null +++ b/src/http/qjson/QJSONConfigVersion.cmake.in @@ -0,0 +1,9 @@ +SET(PACKAGE_VERSION "@QJSON_LIB_VERSION_STRING@") +IF (PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION) + SET(PACKAGE_VERSION_EXACT TRUE) +ENDIF (PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION) +IF (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION) + SET(PACKAGE_VERSION_COMPATIBLE TRUE) +ELSE (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION) + SET(PACKAGE_VERSION_UNSUITABLE TRUE) +ENDIF (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION) diff --git a/src/http/qjson/QJson.pc.in b/src/http/qjson/QJson.pc.in new file mode 100644 index 000000000..e4704bda7 --- /dev/null +++ b/src/http/qjson/QJson.pc.in @@ -0,0 +1,11 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=@LIB_INSTALL_DIR@ +includedir=@INCLUDE_INSTALL_DIR@ + +Name: QJson +Description: QJson is a qt-based library that maps JSON data to QVariant objects +Version: @QJSON_LIB_MAJOR_VERSION@.@QJSON_LIB_MINOR_VERSION@.@QJSON_LIB_PATCH_VERSION@ +Requires: QtCore +Libs: -L${libdir} -lqjson +Cflags: -I${includedir} \ No newline at end of file diff --git a/src/http/qjson/README.license b/src/http/qjson/README.license new file mode 100644 index 000000000..3ede31323 --- /dev/null +++ b/src/http/qjson/README.license @@ -0,0 +1,89 @@ +Qjson version xxxx, Date + +The following files are licensed under LGPL V2.1: +------------------------------------------------ +src/json_parser.yy +src/json_scanner.cpp +src/json_scanner.h +src/parser.cpp +src/parser.h +src/parser_p.h +src/parserrunnable.cpp +src/parserrunnable.h +src/qjson_debug.h +src/qjson_export.h +src/qobjecthelper.cpp +src/serializer.cpp +src/qobjecthelper.h +src/serializer.h +src/serializerrunnable.cpp +src/serializerrunnable.h +tests/cmdline_tester/cmdline_tester.cpp +tests/cmdline_tester/cmdlineparser.cpp +tests/cmdline_tester/cmdlineparser.h +tests/parser/testparser.cpp +tests/qobjecthelper/person.h +tests/qobjecthelper/testqobjecthelper.cpp +tests/serializer/testserializer.cpp + + +The following files are licensed under GPL V2 with Bison Exception: +-------------------------------------------------------------------- +/src/json_parser.cc +/src/stack.hh +/src/location.hh +/src/position.hh +/src/json_parser.hh + + +Copyrights: +---------- +Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. +Copyright (C) 2009 Flavio Castelli 2009 Frank Osterfeld +Copyright (C) 2008 Flavio Castelli +Copyright (C) 2009 Till Adam +Copyright (C) 2009 Michael Leupold +Copyright (C) 2009 Flavio Castelli +Copyright (C) 2009 Frank Osterfeld +Copyright (C) 2009 Pino Toscano +Copyright (C) 2010 Flavio Castelli + + +GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999: +------------------------------------------------------------- + +Checkout COPYING.lib + + +GPL V2 with Bison Exception: +---------------------------- +Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, +Boston, MA 02110-1301, USA. + +As a special exception, you may create a larger work that contains +part or all of the Bison parser skeleton and distribute that work +under terms of your choice, so long as that work isn't itself a +parser generator using the skeleton or a modified version thereof +as a parser skeleton. Alternatively, if you modify or redistribute +the parser skeleton itself, you may (at your option) remove this +special exception, which will cause the skeleton and the resulting +Bison output files to be licensed under the GNU General Public +License without this special exception. + +This special exception was added by the Free Software Foundation in +version 2.2 of Bison. + diff --git a/src/http/qjson/README.md b/src/http/qjson/README.md new file mode 100644 index 000000000..80ff3e9a2 --- /dev/null +++ b/src/http/qjson/README.md @@ -0,0 +1,44 @@ +# QJson +JSON (JavaScript Object Notation) is a lightweight data-interchange format. +It can represents integer, real number, string, an ordered sequence of value, and a collection of name/value pairs. + +QJson is a qt-based library that maps JSON data to QVariant objects. +JSON arrays will be mapped to QVariantList instances, while JSON's objects will be mapped to QVariantMap. + +# Install + +QJson requires: + - Qt 4.0 or greater + - cmake 2.6 or greater + +Some possible cmake options: + - `-DCMAKE_BUILD_TYPE=DEBUG`: enables some debug output (other than making + easier to debug the code) + - `-DQJSON_BUILD_TESTS=yes` or `-DKDE4_BUILD_TESTS=yes`: builds the unit tests + - `-DCMAKE_INSTALL_PREFIX=${HOME}/testinstall`: install qjson in a custom directory + - `-DCMAKE_INCLUDE_PATH=${HOME}/testinstall/include`: include a custom include directory + - `-DCMAKE_LIBRARY_PATH=${HOME}/testinstall/lib`: include a custom library directory + - `-DLIB_DESTINATION=lib64`: if you have a 64 bit system with separate + libraries for 64 bit libraries + - `-DQJSON_VERBOSE_DEBUG_OUTPUT:BOOL=ON`: more debugging statements are + generated by the parser. It's useful only if you are trying to fix + the bison grammar. + +For Unix/Linux/Mac: + + mkdir build + cd build + cmake -DCMAKE_INSTALL_PREFIX=_preferred_path_ .. + make + make install + /sbin/ldconfig #if necessary + +# License + This library is licensed under the Lesser GNU General Public License version 2.1. + See the COPYING.lib file for more information. + +# Resources + +* [Website](http://qjson.sourceforge.net/) +* [Mailing List](https://lists.sourceforge.net/mailman/listinfo/qjson-devel) +* Project Lead/Maintainer (2008-current): [Flavio Castelli](mailto:flavio@castelli.name). diff --git a/src/http/qjson/cmake_uninstall.cmake.in b/src/http/qjson/cmake_uninstall.cmake.in new file mode 100644 index 000000000..776e9c965 --- /dev/null +++ b/src/http/qjson/cmake_uninstall.cmake.in @@ -0,0 +1,17 @@ +IF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") + MESSAGE(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"") +ENDIF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") + +FILE(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files) +STRING(REGEX REPLACE "\n" ";" files "${files}") +FOREACH(file ${files}) + MESSAGE(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"") + EXEC_PROGRAM( + "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval + ) + IF(NOT "${rm_retval}" STREQUAL 0) + MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"") + ENDIF(NOT "${rm_retval}" STREQUAL 0) +ENDFOREACH(file) diff --git a/src/http/qjson/doc/Doxyfile b/src/http/qjson/doc/Doxyfile new file mode 100644 index 000000000..85c630c2b --- /dev/null +++ b/src/http/qjson/doc/Doxyfile @@ -0,0 +1,1851 @@ +# Doxyfile 1.8.1 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project. +# +# All text after a hash (#) is considered a comment and will be ignored. +# The format is: +# TAG = value [value, ...] +# For lists items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (" "). + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# This tag specifies the encoding used for all characters in the config file +# that follow. The default is UTF-8 which is also the encoding used for all +# text before the first occurrence of this tag. Doxygen uses libiconv (or the +# iconv built into libc) for the transcoding. See +# http://www.gnu.org/software/libiconv for the list of possible encodings. + +DOXYFILE_ENCODING = UTF-8 + +# The PROJECT_NAME tag is a single word (or sequence of words) that should +# identify the project. Note that if you do not use Doxywizard you need +# to put quotes around the project name if it contains spaces. + +PROJECT_NAME = QJson + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. +# This could be handy for archiving the generated documentation or +# if some version control system is used. + +PROJECT_NUMBER = 0.8.1 + +# Using the PROJECT_BRIEF tag one can provide an optional one line description +# for a project that appears at the top of each page and should give viewer +# a quick idea about the purpose of the project. Keep the description short. + +PROJECT_BRIEF = the easiest way to manage JSON objects with Qt + +# With the PROJECT_LOGO tag one can specify an logo or icon that is +# included in the documentation. The maximum height of the logo should not +# exceed 55 pixels and the maximum width should not exceed 200 pixels. +# Doxygen will copy the logo to the output directory. + +PROJECT_LOGO = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) +# base path where the generated documentation will be put. +# If a relative path is entered, it will be relative to the location +# where doxygen was started. If left blank the current directory will be used. + +OUTPUT_DIRECTORY = ./ + +# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create +# 4096 sub-directories (in 2 levels) under the output directory of each output +# format and will distribute the generated files over these directories. +# Enabling this option can be useful when feeding doxygen a huge amount of +# source files, where putting all generated files in the same directory would +# otherwise cause performance problems for the file system. + +CREATE_SUBDIRS = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# The default language is English, other supported languages are: +# Afrikaans, Arabic, Brazilian, Catalan, Chinese, Chinese-Traditional, +# Croatian, Czech, Danish, Dutch, Esperanto, Farsi, Finnish, French, German, +# Greek, Hungarian, Italian, Japanese, Japanese-en (Japanese with English +# messages), Korean, Korean-en, Lithuanian, Norwegian, Macedonian, Persian, +# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrillic, Slovak, +# Slovene, Spanish, Swedish, Ukrainian, and Vietnamese. + +OUTPUT_LANGUAGE = English + +# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will +# include brief member descriptions after the members that are listed in +# the file and class documentation (similar to JavaDoc). +# Set to NO to disable this. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend +# the brief description of a member or function before the detailed description. +# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator +# that is used to form the text in various listings. Each string +# in this list, if found as the leading text of the brief description, will be +# stripped from the text and the result after processing the whole list, is +# used as the annotated text. Otherwise, the brief description is used as-is. +# If left blank, the following values are used ("$name" is automatically +# replaced with the name of the entity): "The $name class" "The $name widget" +# "The $name file" "is" "provides" "specifies" "contains" +# "represents" "a" "an" "the" + +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# Doxygen will generate a detailed section even if there is only a brief +# description. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full +# path before files name in the file list and in the header files. If set +# to NO the shortest path that makes the file name unique will be used. + +FULL_PATH_NAMES = YES + +# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag +# can be used to strip a user-defined part of the path. Stripping is +# only done if one of the specified strings matches the left-hand part of +# the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the +# path to strip. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of +# the path mentioned in the documentation of a class, which tells +# the reader which header file to include in order to use a class. +# If left blank only the name of the header file containing the class +# definition is used. Otherwise one should specify the include paths that +# are normally passed to the compiler using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter +# (but less readable) file names. This can be useful if your file system +# doesn't support long names like on DOS, Mac, or CD-ROM. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen +# will interpret the first line (until the first dot) of a JavaDoc-style +# comment as the brief description. If set to NO, the JavaDoc +# comments will behave just like regular Qt-style comments +# (thus requiring an explicit @brief command for a brief description.) + +JAVADOC_AUTOBRIEF = NO + +# If the QT_AUTOBRIEF tag is set to YES then Doxygen will +# interpret the first line (until the first dot) of a Qt-style +# comment as the brief description. If set to NO, the comments +# will behave just like regular Qt-style comments (thus requiring +# an explicit \brief command for a brief description.) + +QT_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen +# treat a multi-line C++ special comment block (i.e. a block of //! or /// +# comments) as a brief description. This used to be the default behaviour. +# The new default is to treat a multi-line C++ comment block as a detailed +# description. Set this tag to YES if you prefer the old behaviour instead. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented +# member inherits the documentation from any documented member that it +# re-implements. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce +# a new page for each member. If set to NO, the documentation of a member will +# be part of the file/class/namespace that contains it. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. +# Doxygen uses this value to replace tabs by spaces in code fragments. + +TAB_SIZE = 8 + +# This tag can be used to specify a number of aliases that acts +# as commands in the documentation. An alias has the form "name=value". +# For example adding "sideeffect=\par Side Effects:\n" will allow you to +# put the command \sideeffect (or @sideeffect) in the documentation, which +# will result in a user-defined paragraph with heading "Side Effects:". +# You can put \n's in the value part of an alias to insert newlines. + +ALIASES = + +# This tag can be used to specify a number of word-keyword mappings (TCL only). +# A mapping has the form "name=value". For example adding +# "class=itcl::class" will allow you to use the command class in the +# itcl::class meaning. + +TCL_SUBST = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C +# sources only. Doxygen will then generate output that is more tailored for C. +# For instance, some of the names that are used will be different. The list +# of all members will be omitted, etc. + +OPTIMIZE_OUTPUT_FOR_C = NO + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java +# sources only. Doxygen will then generate output that is more tailored for +# Java. For instance, namespaces will be presented as packages, qualified +# scopes will look different, etc. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran +# sources only. Doxygen will then generate output that is more tailored for +# Fortran. + +OPTIMIZE_FOR_FORTRAN = NO + +# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL +# sources. Doxygen will then generate output that is tailored for +# VHDL. + +OPTIMIZE_OUTPUT_VHDL = NO + +# Doxygen selects the parser to use depending on the extension of the files it +# parses. With this tag you can assign which parser to use for a given extension. +# Doxygen has a built-in mapping, but you can override or extend it using this +# tag. The format is ext=language, where ext is a file extension, and language +# is one of the parsers supported by doxygen: IDL, Java, Javascript, CSharp, C, +# C++, D, PHP, Objective-C, Python, Fortran, VHDL, C, C++. For instance to make +# doxygen treat .inc files as Fortran files (default is PHP), and .f files as C +# (default is Fortran), use: inc=Fortran f=C. Note that for custom extensions +# you also need to set FILE_PATTERNS otherwise the files are not read by doxygen. + +EXTENSION_MAPPING = + +# If MARKDOWN_SUPPORT is enabled (the default) then doxygen pre-processes all +# comments according to the Markdown format, which allows for more readable +# documentation. See http://daringfireball.net/projects/markdown/ for details. +# The output of markdown processing is further processed by doxygen, so you +# can mix doxygen, HTML, and XML commands with Markdown formatting. +# Disable only in case of backward compatibilities issues. + +MARKDOWN_SUPPORT = YES + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want +# to include (a tag file for) the STL sources as input, then you should +# set this tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); v.s. +# func(std::string) {}). This also makes the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. + +BUILTIN_STL_SUPPORT = NO + +# If you use Microsoft's C++/CLI language, you should set this option to YES to +# enable parsing support. + +CPP_CLI_SUPPORT = NO + +# Set the SIP_SUPPORT tag to YES if your project consists of sip sources only. +# Doxygen will parse them like normal C++ but will assume all classes use public +# instead of private inheritance when no explicit protection keyword is present. + +SIP_SUPPORT = NO + +# For Microsoft's IDL there are propget and propput attributes to indicate getter +# and setter methods for a property. Setting this option to YES (the default) +# will make doxygen replace the get and set methods by a property in the +# documentation. This will only work if the methods are indeed getting or +# setting a simple type. If this is not the case, or you want to show the +# methods anyway, you should set this option to NO. + +IDL_PROPERTY_SUPPORT = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES, then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. + +DISTRIBUTE_GROUP_DOC = NO + +# Set the SUBGROUPING tag to YES (the default) to allow class member groups of +# the same type (for instance a group of public functions) to be put as a +# subgroup of that type (e.g. under the Public Functions section). Set it to +# NO to prevent subgrouping. Alternatively, this can be done per class using +# the \nosubgrouping command. + +SUBGROUPING = YES + +# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and +# unions are shown inside the group in which they are included (e.g. using +# @ingroup) instead of on a separate page (for HTML and Man pages) or +# section (for LaTeX and RTF). + +INLINE_GROUPED_CLASSES = NO + +# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and +# unions with only public data fields will be shown inline in the documentation +# of the scope in which they are defined (i.e. file, namespace, or group +# documentation), provided this scope is documented. If set to NO (the default), +# structs, classes, and unions are shown on a separate page (for HTML and Man +# pages) or section (for LaTeX and RTF). + +INLINE_SIMPLE_STRUCTS = NO + +# When TYPEDEF_HIDES_STRUCT is enabled, a typedef of a struct, union, or enum +# is documented as struct, union, or enum with the name of the typedef. So +# typedef struct TypeS {} TypeT, will appear in the documentation as a struct +# with name TypeT. When disabled the typedef will appear as a member of a file, +# namespace, or class. And the struct will be named TypeS. This can typically +# be useful for C code in case the coding convention dictates that all compound +# types are typedef'ed and only the typedef is referenced, never the tag name. + +TYPEDEF_HIDES_STRUCT = NO + +# The SYMBOL_CACHE_SIZE determines the size of the internal cache use to +# determine which symbols to keep in memory and which to flush to disk. +# When the cache is full, less often used symbols will be written to disk. +# For small to medium size projects (<1000 input files) the default value is +# probably good enough. For larger projects a too small cache size can cause +# doxygen to be busy swapping symbols to and from disk most of the time +# causing a significant performance penalty. +# If the system has enough physical memory increasing the cache will improve the +# performance by keeping more symbols in memory. Note that the value works on +# a logarithmic scale so increasing the size by one will roughly double the +# memory usage. The cache size is given by this formula: +# 2^(16+SYMBOL_CACHE_SIZE). The valid range is 0..9, the default is 0, +# corresponding to a cache size of 2^16 = 65536 symbols. + +SYMBOL_CACHE_SIZE = 0 + +# Similar to the SYMBOL_CACHE_SIZE the size of the symbol lookup cache can be +# set using LOOKUP_CACHE_SIZE. This cache is used to resolve symbols given +# their name and scope. Since this can be an expensive process and often the +# same symbol appear multiple times in the code, doxygen keeps a cache of +# pre-resolved symbols. If the cache is too small doxygen will become slower. +# If the cache is too large, memory is wasted. The cache size is given by this +# formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range is 0..9, the default is 0, +# corresponding to a cache size of 2^16 = 65536 symbols. + +LOOKUP_CACHE_SIZE = 0 + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in +# documentation are documented, even if no documentation was available. +# Private class members and static file members will be hidden unless +# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES all private members of a class +# will be included in the documentation. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_PACKAGE tag is set to YES all members with package or internal scope will be included in the documentation. + +EXTRACT_PACKAGE = NO + +# If the EXTRACT_STATIC tag is set to YES all static members of a file +# will be included in the documentation. + +EXTRACT_STATIC = NO + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) +# defined locally in source files will be included in the documentation. +# If set to NO only classes defined in header files are included. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. When set to YES local +# methods, which are defined in the implementation section but not in +# the interface are included in the documentation. +# If set to NO (the default) only methods in the interface are included. + +EXTRACT_LOCAL_METHODS = NO + +# If this flag is set to YES, the members of anonymous namespaces will be +# extracted and appear in the documentation as a namespace called +# 'anonymous_namespace{file}', where file will be replaced with the base +# name of the file that contains the anonymous namespace. By default +# anonymous namespaces are hidden. + +EXTRACT_ANON_NSPACES = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all +# undocumented members of documented classes, files or namespaces. +# If set to NO (the default) these members will be included in the +# various overviews, but no documentation section is generated. +# This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_MEMBERS = YES + +# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. +# If set to NO (the default) these classes will be included in the various +# overviews. This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_CLASSES = YES + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all +# friend (class|struct|union) declarations. +# If set to NO (the default) these declarations will be included in the +# documentation. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any +# documentation blocks found inside the body of a function. +# If set to NO (the default) these blocks will be appended to the +# function's detailed documentation block. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation +# that is typed after a \internal command is included. If the tag is set +# to NO (the default) then the documentation will be excluded. +# Set it to YES to include the internal documentation. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate +# file names in lower-case letters. If set to YES upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. + +CASE_SENSE_NAMES = YES + +# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen +# will show members with their full class and namespace scopes in the +# documentation. If set to YES the scope will be hidden. + +HIDE_SCOPE_NAMES = NO + +# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen +# will put a list of the files that are included by a file in the documentation +# of that file. + +SHOW_INCLUDE_FILES = YES + +# If the FORCE_LOCAL_INCLUDES tag is set to YES then Doxygen +# will list include files with double quotes in the documentation +# rather than with sharp brackets. + +FORCE_LOCAL_INCLUDES = NO + +# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] +# is inserted in the documentation for inline members. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen +# will sort the (detailed) documentation of file and class members +# alphabetically by member name. If set to NO the members will appear in +# declaration order. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the +# brief documentation of file, namespace and class members alphabetically +# by member name. If set to NO (the default) the members will appear in +# declaration order. + +SORT_BRIEF_DOCS = NO + +# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen +# will sort the (brief and detailed) documentation of class members so that +# constructors and destructors are listed first. If set to NO (the default) +# the constructors will appear in the respective orders defined by +# SORT_MEMBER_DOCS and SORT_BRIEF_DOCS. +# This tag will be ignored for brief docs if SORT_BRIEF_DOCS is set to NO +# and ignored for detailed docs if SORT_MEMBER_DOCS is set to NO. + +SORT_MEMBERS_CTORS_1ST = NO + +# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the +# hierarchy of group names into alphabetical order. If set to NO (the default) +# the group names will appear in their defined order. + +SORT_GROUP_NAMES = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be +# sorted by fully-qualified names, including namespaces. If set to +# NO (the default), the class list will be sorted only by class name, +# not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the +# alphabetical list. + +SORT_BY_SCOPE_NAME = NO + +# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to +# do proper type resolution of all parameters of a function it will reject a +# match between the prototype and the implementation of a member function even +# if there is only one candidate or it is obvious which candidate to choose +# by doing a simple string match. By disabling STRICT_PROTO_MATCHING doxygen +# will still accept a match between prototype and implementation in such cases. + +STRICT_PROTO_MATCHING = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or +# disable (NO) the todo list. This list is created by putting \todo +# commands in the documentation. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or +# disable (NO) the test list. This list is created by putting \test +# commands in the documentation. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or +# disable (NO) the bug list. This list is created by putting \bug +# commands in the documentation. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or +# disable (NO) the deprecated list. This list is created by putting +# \deprecated commands in the documentation. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional +# documentation sections, marked by \if sectionname ... \endif. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines +# the initial value of a variable or macro consists of for it to appear in +# the documentation. If the initializer consists of more lines than specified +# here it will be hidden. Use a value of 0 to hide initializers completely. +# The appearance of the initializer of individual variables and macros in the +# documentation can be controlled using \showinitializer or \hideinitializer +# command in the documentation regardless of this setting. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated +# at the bottom of the documentation of classes and structs. If set to YES the +# list will mention the files that were used to generate the documentation. + +SHOW_USED_FILES = YES + +# Set the SHOW_FILES tag to NO to disable the generation of the Files page. +# This will remove the Files entry from the Quick Index and from the +# Folder Tree View (if specified). The default is YES. + +SHOW_FILES = YES + +# Set the SHOW_NAMESPACES tag to NO to disable the generation of the +# Namespaces page. +# This will remove the Namespaces entry from the Quick Index +# and from the Folder Tree View (if specified). The default is YES. + +SHOW_NAMESPACES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from +# the version control system). Doxygen will invoke the program by executing (via +# popen()) the command , where is the value of +# the FILE_VERSION_FILTER tag, and is the name of an input file +# provided by doxygen. Whatever the program writes to standard output +# is used as the file version. See the manual for examples. + +FILE_VERSION_FILTER = + +# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed +# by doxygen. The layout file controls the global structure of the generated +# output files in an output format independent way. The create the layout file +# that represents doxygen's defaults, run doxygen with the -l option. +# You can optionally specify a file name after the option, if omitted +# DoxygenLayout.xml will be used as the name of the layout file. + +LAYOUT_FILE = + +# The CITE_BIB_FILES tag can be used to specify one or more bib files +# containing the references data. This must be a list of .bib files. The +# .bib extension is automatically appended if omitted. Using this command +# requires the bibtex tool to be installed. See also +# http://en.wikipedia.org/wiki/BibTeX for more info. For LaTeX the style +# of the bibliography can be controlled using LATEX_BIB_STYLE. To use this +# feature you need bibtex and perl available in the search path. + +CITE_BIB_FILES = + +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated +# by doxygen. Possible values are YES and NO. If left blank NO is used. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated by doxygen. Possible values are YES and NO. If left blank +# NO is used. + +WARNINGS = YES + +# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings +# for undocumented members. If EXTRACT_ALL is set to YES then this flag will +# automatically be disabled. + +WARN_IF_UNDOCUMENTED = YES + +# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some +# parameters in a documented function, or documenting parameters that +# don't exist or using markup commands wrongly. + +WARN_IF_DOC_ERROR = YES + +# The WARN_NO_PARAMDOC option can be enabled to get warnings for +# functions that are documented, but have no documentation for their parameters +# or return value. If set to NO (the default) doxygen will only warn about +# wrong or incomplete parameter documentation, but not about the absence of +# documentation. + +WARN_NO_PARAMDOC = NO + +# The WARN_FORMAT tag determines the format of the warning messages that +# doxygen can produce. The string should contain the $file, $line, and $text +# tags, which will be replaced by the file and line number from which the +# warning originated and the warning text. Optionally the format may contain +# $version, which will be replaced by the version of the file (if it could +# be obtained via FILE_VERSION_FILTER) + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning +# and error messages should be written. If left blank the output is written +# to stderr. + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag can be used to specify the files and/or directories that contain +# documented source files. You may enter file names like "myfile.cpp" or +# directories like "/usr/src/myproject". Separate the files or directories +# with spaces. + +INPUT = ./qjson.dox \ + ../src + +# This tag can be used to specify the character encoding of the source files +# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is +# also the default input encoding. Doxygen uses libiconv (or the iconv built +# into libc) for the transcoding. See http://www.gnu.org/software/libiconv for +# the list of possible encodings. + +INPUT_ENCODING = UTF-8 + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank the following patterns are tested: +# *.c *.cc *.cxx *.cpp *.c++ *.d *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh +# *.hxx *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.dox *.py +# *.f90 *.f *.for *.vhd *.vhdl + +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.d \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.idl \ + *.odl \ + *.cs \ + *.php \ + *.php3 \ + *.inc \ + *.m \ + *.mm \ + *.dox \ + *.py \ + *.f90 \ + *.f \ + *.vhd \ + *.vhdl \ + *.C \ + *.CC \ + *.C++ \ + *.II \ + *.I++ \ + *.H \ + *.HH \ + *.H++ \ + *.CS \ + *.PHP \ + *.PHP3 \ + *.M \ + *.MM \ + *.PY \ + *.F90 \ + *.F \ + *.VHD \ + *.VHDL + +# The RECURSIVE tag can be used to turn specify whether or not subdirectories +# should be searched for input files as well. Possible values are YES and NO. +# If left blank NO is used. + +RECURSIVE = YES + +# The EXCLUDE tag can be used to specify files and/or directories that should be +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. +# Note that relative paths are relative to the directory from which doxygen is +# run. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or +# directories that are symbolic links (a Unix file system feature) are excluded +# from the input. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. Note that the wildcards are matched +# against the file with absolute path, so to exclude all test directories +# for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names +# (namespaces, classes, functions, etc.) that should be excluded from the +# output. The symbol name can be a fully qualified name, a word, or if the +# wildcard * is used, a substring. Examples: ANamespace, AClass, +# AClass::ANamespace, ANamespace::*Test + +EXCLUDE_SYMBOLS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or +# directories that contain example code fragments that are included (see +# the \include command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank all files are included. + +EXAMPLE_PATTERNS = * + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude +# commands irrespective of the value of the RECURSIVE tag. +# Possible values are YES and NO. If left blank NO is used. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or +# directories that contain image that are included in the documentation (see +# the \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command , where +# is the value of the INPUT_FILTER tag, and is the name of an +# input file. Doxygen will then use the output that the filter program writes +# to standard output. +# If FILTER_PATTERNS is specified, this tag will be +# ignored. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. +# Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. +# The filters are a list of the form: +# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further +# info on how filters are used. If FILTER_PATTERNS is empty or if +# non of the patterns match the file name, INPUT_FILTER is applied. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will be used to filter the input files when producing source +# files to browse (i.e. when SOURCE_BROWSER is set to YES). + +FILTER_SOURCE_FILES = NO + +# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file +# pattern. A pattern will override the setting for FILTER_PATTERN (if any) +# and it is also possible to disable source filtering for a specific pattern +# using *.ext= (so without naming a filter). This option only has effect when +# FILTER_SOURCE_FILES is enabled. + +FILTER_SOURCE_PATTERNS = + +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will +# be generated. Documented entities will be cross-referenced with these sources. +# Note: To get rid of all source code in the generated output, make sure also +# VERBATIM_HEADERS is set to NO. + +SOURCE_BROWSER = YES + +# Setting the INLINE_SOURCES tag to YES will include the body +# of functions and classes directly in the documentation. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct +# doxygen to hide any special comment blocks from generated source code +# fragments. Normal C and C++ comments will always remain visible. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES +# then for each documented function all documented +# functions referencing it will be listed. + +REFERENCED_BY_RELATION = YES + +# If the REFERENCES_RELATION tag is set to YES +# then for each documented function all documented entities +# called/used by that function will be listed. + +REFERENCES_RELATION = YES + +# If the REFERENCES_LINK_SOURCE tag is set to YES (the default) +# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from +# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will +# link to the source code. +# Otherwise they will link to the documentation. + +REFERENCES_LINK_SOURCE = YES + +# If the USE_HTAGS tag is set to YES then the references to source code +# will point to the HTML generated by the htags(1) tool instead of doxygen +# built-in source browser. The htags tool is part of GNU's global source +# tagging system (see http://www.gnu.org/software/global/global.html). You +# will need version 4.8.6 or higher. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen +# will generate a verbatim copy of the header file for each class for +# which an include is specified. Set to NO to disable this. + +VERBATIM_HEADERS = YES + +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index +# of all compounds will be generated. Enable this if the project +# contains a lot of classes, structs, unions or interfaces. + +ALPHABETICAL_INDEX = NO + +# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then +# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns +# in which this list will be split (can be a number in the range [1..20]) + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all +# classes will be put under the same header in the alphabetical index. +# The IGNORE_PREFIX tag can be used to specify one or more prefixes that +# should be ignored while generating the index headers. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES (the default) Doxygen will +# generate HTML output. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `html' will be used as the default path. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for +# each generated HTML page (for example: .htm,.php,.asp). If it is left blank +# doxygen will generate files with .html extension. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a personal HTML header for +# each generated HTML page. If it is left blank doxygen will generate a +# standard header. Note that when using a custom header you are responsible +# for the proper inclusion of any scripts and style sheets that doxygen +# needs, which is dependent on the configuration options used. +# It is advised to generate a default header using "doxygen -w html +# header.html footer.html stylesheet.css YourConfigFile" and then modify +# that header. Note that the header is subject to change so you typically +# have to redo this when upgrading to a newer version of doxygen or when +# changing the value of configuration settings such as GENERATE_TREEVIEW! + +HTML_HEADER = ./header.html + +# The HTML_FOOTER tag can be used to specify a personal HTML footer for +# each generated HTML page. If it is left blank doxygen will generate a +# standard footer. + +HTML_FOOTER = ./footer.html + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading +# style sheet that is used by each HTML page. It can be used to +# fine-tune the look of the HTML output. If the tag is left blank doxygen +# will generate a default style sheet. Note that doxygen will try to copy +# the style sheet file to the HTML output directory, so don't put your own +# style sheet in the HTML output directory as well, or it will be erased! + +HTML_STYLESHEET = + +# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or +# other source files which should be copied to the HTML output directory. Note +# that these files will be copied to the base HTML output directory. Use the +# $relpath$ marker in the HTML_HEADER and/or HTML_FOOTER files to load these +# files. In the HTML_STYLESHEET file, use the file name only. Also note that +# the files will be copied as-is; there are no commands or markers available. + +HTML_EXTRA_FILES = + +# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. +# Doxygen will adjust the colors in the style sheet and background images +# according to this color. Hue is specified as an angle on a colorwheel, +# see http://en.wikipedia.org/wiki/Hue for more information. +# For instance the value 0 represents red, 60 is yellow, 120 is green, +# 180 is cyan, 240 is blue, 300 purple, and 360 is red again. +# The allowed range is 0 to 359. + +HTML_COLORSTYLE_HUE = 220 + +# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of +# the colors in the HTML output. For a value of 0 the output will use +# grayscales only. A value of 255 will produce the most vivid colors. + +HTML_COLORSTYLE_SAT = 100 + +# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to +# the luminance component of the colors in the HTML output. Values below +# 100 gradually make the output lighter, whereas values above 100 make +# the output darker. The value divided by 100 is the actual gamma applied, +# so 80 represents a gamma of 0.8, The value 220 represents a gamma of 2.2, +# and 100 does not change the gamma. + +HTML_COLORSTYLE_GAMMA = 80 + +# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML +# page will contain the date and time when the page was generated. Setting +# this to NO can help when comparing the output of multiple runs. + +HTML_TIMESTAMP = NO + +# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML +# documentation will contain sections that can be hidden and shown after the +# page has loaded. + +HTML_DYNAMIC_SECTIONS = NO + +# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of +# entries shown in the various tree structured indices initially; the user +# can expand and collapse entries dynamically later on. Doxygen will expand +# the tree to such a level that at most the specified number of entries are +# visible (unless a fully collapsed tree already exceeds this amount). +# So setting the number of entries 1 will produce a full collapsed tree by +# default. 0 is a special value representing an infinite number of entries +# and will result in a full expanded tree by default. + +HTML_INDEX_NUM_ENTRIES = 100 + +# If the GENERATE_DOCSET tag is set to YES, additional index files +# will be generated that can be used as input for Apple's Xcode 3 +# integrated development environment, introduced with OSX 10.5 (Leopard). +# To create a documentation set, doxygen will generate a Makefile in the +# HTML output directory. Running make will produce the docset in that +# directory and running "make install" will install the docset in +# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find +# it at startup. +# See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html +# for more information. + +GENERATE_DOCSET = NO + +# When GENERATE_DOCSET tag is set to YES, this tag determines the name of the +# feed. A documentation feed provides an umbrella under which multiple +# documentation sets from a single provider (such as a company or product suite) +# can be grouped. + +DOCSET_FEEDNAME = "Doxygen generated docs" + +# When GENERATE_DOCSET tag is set to YES, this tag specifies a string that +# should uniquely identify the documentation set bundle. This should be a +# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen +# will append .docset to the name. + +DOCSET_BUNDLE_ID = org.doxygen.Project + +# When GENERATE_PUBLISHER_ID tag specifies a string that should uniquely identify +# the documentation publisher. This should be a reverse domain-name style +# string, e.g. com.mycompany.MyDocSet.documentation. + +DOCSET_PUBLISHER_ID = org.doxygen.Publisher + +# The GENERATE_PUBLISHER_NAME tag identifies the documentation publisher. + +DOCSET_PUBLISHER_NAME = Publisher + +# If the GENERATE_HTMLHELP tag is set to YES, additional index files +# will be generated that can be used as input for tools like the +# Microsoft HTML help workshop to generate a compiled HTML help file (.chm) +# of the generated HTML documentation. + +GENERATE_HTMLHELP = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can +# be used to specify the file name of the resulting .chm file. You +# can add a path in front of the file if the result should not be +# written to the html output directory. + +CHM_FILE = + +# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can +# be used to specify the location (absolute path including file name) of +# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run +# the HTML help compiler on the generated index.hhp. + +HHC_LOCATION = + +# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag +# controls if a separate .chi index file is generated (YES) or that +# it should be included in the master .chm file (NO). + +GENERATE_CHI = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_INDEX_ENCODING +# is used to encode HtmlHelp index (hhk), content (hhc) and project file +# content. + +CHM_INDEX_ENCODING = + +# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag +# controls whether a binary table of contents is generated (YES) or a +# normal table of contents (NO) in the .chm file. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members +# to the contents of the HTML help documentation and to the tree view. + +TOC_EXPAND = NO + +# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and +# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated +# that can be used as input for Qt's qhelpgenerator to generate a +# Qt Compressed Help (.qch) of the generated HTML documentation. + +GENERATE_QHP = NO + +# If the QHG_LOCATION tag is specified, the QCH_FILE tag can +# be used to specify the file name of the resulting .qch file. +# The path specified is relative to the HTML output folder. + +QCH_FILE = + +# The QHP_NAMESPACE tag specifies the namespace to use when generating +# Qt Help Project output. For more information please see +# http://doc.trolltech.com/qthelpproject.html#namespace + +QHP_NAMESPACE = org.doxygen.Project + +# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating +# Qt Help Project output. For more information please see +# http://doc.trolltech.com/qthelpproject.html#virtual-folders + +QHP_VIRTUAL_FOLDER = doc + +# If QHP_CUST_FILTER_NAME is set, it specifies the name of a custom filter to +# add. For more information please see +# http://doc.trolltech.com/qthelpproject.html#custom-filters + +QHP_CUST_FILTER_NAME = + +# The QHP_CUST_FILT_ATTRS tag specifies the list of the attributes of the +# custom filter to add. For more information please see +# +# Qt Help Project / Custom Filters. + +QHP_CUST_FILTER_ATTRS = + +# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this +# project's +# filter section matches. +# +# Qt Help Project / Filter Attributes. + +QHP_SECT_FILTER_ATTRS = + +# If the GENERATE_QHP tag is set to YES, the QHG_LOCATION tag can +# be used to specify the location of Qt's qhelpgenerator. +# If non-empty doxygen will try to run qhelpgenerator on the generated +# .qhp file. + +QHG_LOCATION = + +# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files +# will be generated, which together with the HTML files, form an Eclipse help +# plugin. To install this plugin and make it available under the help contents +# menu in Eclipse, the contents of the directory containing the HTML and XML +# files needs to be copied into the plugins directory of eclipse. The name of +# the directory within the plugins directory should be the same as +# the ECLIPSE_DOC_ID value. After copying Eclipse needs to be restarted before +# the help appears. + +GENERATE_ECLIPSEHELP = NO + +# A unique identifier for the eclipse help plugin. When installing the plugin +# the directory name containing the HTML and XML files should also have +# this name. + +ECLIPSE_DOC_ID = org.doxygen.Project + +# The DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) +# at top of each HTML page. The value NO (the default) enables the index and +# the value YES disables it. Since the tabs have the same information as the +# navigation tree you can set this option to NO if you already set +# GENERATE_TREEVIEW to YES. + +DISABLE_INDEX = NO + +# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index +# structure should be generated to display hierarchical information. +# If the tag value is set to YES, a side panel will be generated +# containing a tree-like index structure (just like the one that +# is generated for HTML Help). For this to work a browser that supports +# JavaScript, DHTML, CSS and frames is required (i.e. any modern browser). +# Windows users are probably better off using the HTML help feature. +# Since the tree basically has the same information as the tab index you +# could consider to set DISABLE_INDEX to NO when enabling this option. + +GENERATE_TREEVIEW = NO + +# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values +# (range [0,1..20]) that doxygen will group on one line in the generated HTML +# documentation. Note that a value of 0 will completely suppress the enum +# values from appearing in the overview section. + +ENUM_VALUES_PER_LINE = 4 + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be +# used to set the initial width (in pixels) of the frame in which the tree +# is shown. + +TREEVIEW_WIDTH = 250 + +# When the EXT_LINKS_IN_WINDOW option is set to YES doxygen will open +# links to external symbols imported via tag files in a separate window. + +EXT_LINKS_IN_WINDOW = NO + +# Use this tag to change the font size of Latex formulas included +# as images in the HTML documentation. The default is 10. Note that +# when you change the font size after a successful doxygen run you need +# to manually remove any form_*.png images from the HTML output directory +# to force them to be regenerated. + +FORMULA_FONTSIZE = 10 + +# Use the FORMULA_TRANPARENT tag to determine whether or not the images +# generated for formulas are transparent PNGs. Transparent PNGs are +# not supported properly for IE 6.0, but are supported on all modern browsers. +# Note that when changing this option you need to delete any form_*.png files +# in the HTML output before the changes have effect. + +FORMULA_TRANSPARENT = YES + +# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax +# (see http://www.mathjax.org) which uses client side Javascript for the +# rendering instead of using prerendered bitmaps. Use this if you do not +# have LaTeX installed or if you want to formulas look prettier in the HTML +# output. When enabled you may also need to install MathJax separately and +# configure the path to it using the MATHJAX_RELPATH option. + +USE_MATHJAX = NO + +# When MathJax is enabled you need to specify the location relative to the +# HTML output directory using the MATHJAX_RELPATH option. The destination +# directory should contain the MathJax.js script. For instance, if the mathjax +# directory is located at the same level as the HTML output directory, then +# MATHJAX_RELPATH should be ../mathjax. The default value points to +# the MathJax Content Delivery Network so you can quickly see the result without +# installing MathJax. +# However, it is strongly recommended to install a local +# copy of MathJax from http://www.mathjax.org before deployment. + +MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest + +# The MATHJAX_EXTENSIONS tag can be used to specify one or MathJax extension +# names that should be enabled during MathJax rendering. + +MATHJAX_EXTENSIONS = + +# When the SEARCHENGINE tag is enabled doxygen will generate a search box +# for the HTML output. The underlying search engine uses javascript +# and DHTML and should work on any modern browser. Note that when using +# HTML help (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets +# (GENERATE_DOCSET) there is already a search function so this one should +# typically be disabled. For large projects the javascript based search engine +# can be slow, then enabling SERVER_BASED_SEARCH may provide a better solution. + +SEARCHENGINE = NO + +# When the SERVER_BASED_SEARCH tag is enabled the search engine will be +# implemented using a PHP enabled web server instead of at the web client +# using Javascript. Doxygen will generate the search PHP script and index +# file to put on the web server. The advantage of the server +# based approach is that it scales better to large projects and allows +# full text search. The disadvantages are that it is more difficult to setup +# and does not have live searching capabilities. + +SERVER_BASED_SEARCH = NO + +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- + +# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will +# generate Latex output. + +GENERATE_LATEX = NO + +# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `latex' will be used as the default path. + +LATEX_OUTPUT = latex + +# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be +# invoked. If left blank `latex' will be used as the default command name. +# Note that when enabling USE_PDFLATEX this option is only used for +# generating bitmaps for formulas in the HTML output, but not in the +# Makefile that is written to the output directory. + +LATEX_CMD_NAME = latex + +# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to +# generate index for LaTeX. If left blank `makeindex' will be used as the +# default command name. + +MAKEINDEX_CMD_NAME = makeindex + +# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact +# LaTeX documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_LATEX = NO + +# The PAPER_TYPE tag can be used to set the paper type that is used +# by the printer. Possible values are: a4, letter, legal and +# executive. If left blank a4wide will be used. + +PAPER_TYPE = a4wide + +# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX +# packages that should be included in the LaTeX output. + +EXTRA_PACKAGES = + +# The LATEX_HEADER tag can be used to specify a personal LaTeX header for +# the generated latex document. The header should contain everything until +# the first chapter. If it is left blank doxygen will generate a +# standard header. Notice: only use this tag if you know what you are doing! + +LATEX_HEADER = + +# The LATEX_FOOTER tag can be used to specify a personal LaTeX footer for +# the generated latex document. The footer should contain everything after +# the last chapter. If it is left blank doxygen will generate a +# standard footer. Notice: only use this tag if you know what you are doing! + +LATEX_FOOTER = + +# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated +# is prepared for conversion to pdf (using ps2pdf). The pdf file will +# contain links (just like the HTML output) instead of page references +# This makes the output suitable for online browsing using a pdf viewer. + +PDF_HYPERLINKS = YES + +# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of +# plain latex in the generated Makefile. Set this option to YES to get a +# higher quality PDF documentation. + +USE_PDFLATEX = YES + +# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. +# command to the generated LaTeX files. This will instruct LaTeX to keep +# running if errors occur, instead of asking the user for help. +# This option is also used when generating formulas in HTML. + +LATEX_BATCHMODE = NO + +# If LATEX_HIDE_INDICES is set to YES then doxygen will not +# include the index chapters (such as File Index, Compound Index, etc.) +# in the output. + +LATEX_HIDE_INDICES = NO + +# If LATEX_SOURCE_CODE is set to YES then doxygen will include +# source code with syntax highlighting in the LaTeX output. +# Note that which sources are shown also depends on other settings +# such as SOURCE_BROWSER. + +LATEX_SOURCE_CODE = NO + +# The LATEX_BIB_STYLE tag can be used to specify the style to use for the +# bibliography, e.g. plainnat, or ieeetr. The default style is "plain". See +# http://en.wikipedia.org/wiki/BibTeX for more info. + +LATEX_BIB_STYLE = plain + +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- + +# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output +# The RTF output is optimized for Word 97 and may not look very pretty with +# other RTF readers or editors. + +GENERATE_RTF = NO + +# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `rtf' will be used as the default path. + +RTF_OUTPUT = rtf + +# If the COMPACT_RTF tag is set to YES Doxygen generates more compact +# RTF documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_RTF = NO + +# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated +# will contain hyperlink fields. The RTF file will +# contain links (just like the HTML output) instead of page references. +# This makes the output suitable for online browsing using WORD or other +# programs which support those fields. +# Note: wordpad (write) and others do not support links. + +RTF_HYPERLINKS = NO + +# Load style sheet definitions from file. Syntax is similar to doxygen's +# config file, i.e. a series of assignments. You only have to provide +# replacements, missing definitions are set to their default value. + +RTF_STYLESHEET_FILE = + +# Set optional variables used in the generation of an rtf document. +# Syntax is similar to doxygen's config file. + +RTF_EXTENSIONS_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- + +# If the GENERATE_MAN tag is set to YES (the default) Doxygen will +# generate man pages + +GENERATE_MAN = NO + +# The MAN_OUTPUT tag is used to specify where the man pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `man' will be used as the default path. + +MAN_OUTPUT = man + +# The MAN_EXTENSION tag determines the extension that is added to +# the generated man pages (default is the subroutine's section .3) + +MAN_EXTENSION = .3 + +# If the MAN_LINKS tag is set to YES and Doxygen generates man output, +# then it will generate one additional man file for each entity +# documented in the real man page(s). These additional files +# only source the real man page, but without them the man command +# would be unable to find the correct page. The default is NO. + +MAN_LINKS = NO + +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- + +# If the GENERATE_XML tag is set to YES Doxygen will +# generate an XML file that captures the structure of +# the code including all documentation. + +GENERATE_XML = NO + +# The XML_OUTPUT tag is used to specify where the XML pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `xml' will be used as the default path. + +XML_OUTPUT = xml + +# The XML_SCHEMA tag can be used to specify an XML schema, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_SCHEMA = + +# The XML_DTD tag can be used to specify an XML DTD, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_DTD = + +# If the XML_PROGRAMLISTING tag is set to YES Doxygen will +# dump the program listings (including syntax highlighting +# and cross-referencing information) to the XML output. Note that +# enabling this will significantly increase the size of the XML output. + +XML_PROGRAMLISTING = YES + +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- + +# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will +# generate an AutoGen Definitions (see autogen.sf.net) file +# that captures the structure of the code including all +# documentation. Note that this feature is still experimental +# and incomplete at the moment. + +GENERATE_AUTOGEN_DEF = NO + +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- + +# If the GENERATE_PERLMOD tag is set to YES Doxygen will +# generate a Perl module file that captures the structure of +# the code including all documentation. Note that this +# feature is still experimental and incomplete at the +# moment. + +GENERATE_PERLMOD = NO + +# If the PERLMOD_LATEX tag is set to YES Doxygen will generate +# the necessary Makefile rules, Perl scripts and LaTeX code to be able +# to generate PDF and DVI output from the Perl module output. + +PERLMOD_LATEX = NO + +# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be +# nicely formatted so it can be parsed by a human reader. +# This is useful +# if you want to understand what is going on. +# On the other hand, if this +# tag is set to NO the size of the Perl module output will be much smaller +# and Perl will parse it just the same. + +PERLMOD_PRETTY = YES + +# The names of the make variables in the generated doxyrules.make file +# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. +# This is useful so different doxyrules.make files included by the same +# Makefile don't overwrite each other's variables. + +PERLMOD_MAKEVAR_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- + +# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will +# evaluate all C-preprocessor directives found in the sources and include +# files. + +ENABLE_PREPROCESSING = YES + +# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro +# names in the source code. If set to NO (the default) only conditional +# compilation will be performed. Macro expansion can be done in a controlled +# way by setting EXPAND_ONLY_PREDEF to YES. + +MACRO_EXPANSION = NO + +# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES +# then the macro expansion is limited to the macros specified with the +# PREDEFINED and EXPAND_AS_DEFINED tags. + +EXPAND_ONLY_PREDEF = NO + +# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files +# pointed to by INCLUDE_PATH will be searched when a #include is found. + +SEARCH_INCLUDES = YES + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by +# the preprocessor. + +INCLUDE_PATH = + +# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard +# patterns (like *.h and *.hpp) to filter out the header-files in the +# directories. If left blank, the patterns specified with FILE_PATTERNS will +# be used. + +INCLUDE_FILE_PATTERNS = + +# The PREDEFINED tag can be used to specify one or more macro names that +# are defined before the preprocessor is started (similar to the -D option of +# gcc). The argument of the tag is a list of macros of the form: name +# or name=definition (no spaces). If the definition and the = are +# omitted =1 is assumed. To prevent a macro definition from being +# undefined via #undef or recursively expanded use the := operator +# instead of the = operator. + +PREDEFINED = + +# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then +# this tag can be used to specify a list of macro names that should be expanded. +# The macro definition that is found in the sources will be used. +# Use the PREDEFINED tag if you want to use a different macro definition that +# overrules the definition found in the source code. + +EXPAND_AS_DEFINED = + +# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then +# doxygen's preprocessor will remove all references to function-like macros +# that are alone on a line, have an all uppercase name, and do not end with a +# semicolon, because these will confuse the parser if not removed. + +SKIP_FUNCTION_MACROS = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- + +# The TAGFILES option can be used to specify one or more tagfiles. For each +# tag file the location of the external documentation should be added. The +# format of a tag file without this location is as follows: +# +# TAGFILES = file1 file2 ... +# Adding location for the tag files is done as follows: +# +# TAGFILES = file1=loc1 "file2 = loc2" ... +# where "loc1" and "loc2" can be relative or absolute paths +# or URLs. Note that each tag file must have a unique name (where the name does +# NOT include the path). If a tag file is not located in the directory in which +# doxygen is run, you must also specify the path to the tagfile here. + +TAGFILES = + +# When a file name is specified after GENERATE_TAGFILE, doxygen will create +# a tag file that is based on the input files it reads. + +GENERATE_TAGFILE = + +# If the ALLEXTERNALS tag is set to YES all external classes will be listed +# in the class index. If set to NO only the inherited external classes +# will be listed. + +ALLEXTERNALS = NO + +# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed +# in the modules index. If set to NO, only the current project's groups will +# be listed. + +EXTERNAL_GROUPS = YES + +# The PERL_PATH should be the absolute path and name of the perl script +# interpreter (i.e. the result of `which perl'). + +PERL_PATH = /usr/bin/perl + +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- + +# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will +# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base +# or super classes. Setting the tag to NO turns the diagrams off. Note that +# this option also works with HAVE_DOT disabled, but it is recommended to +# install and use dot, since it yields more powerful graphs. + +CLASS_DIAGRAMS = NO + +# You can define message sequence charts within doxygen comments using the \msc +# command. Doxygen will then run the mscgen tool (see +# http://www.mcternan.me.uk/mscgen/) to produce the chart and insert it in the +# documentation. The MSCGEN_PATH tag allows you to specify the directory where +# the mscgen tool resides. If left empty the tool is assumed to be found in the +# default search path. + +MSCGEN_PATH = + +# If set to YES, the inheritance and collaboration graphs will hide +# inheritance and usage relations if the target is undocumented +# or is not a class. + +HIDE_UNDOC_RELATIONS = YES + +# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is +# available from the path. This tool is part of Graphviz, a graph visualization +# toolkit from AT&T and Lucent Bell Labs. The other options in this section +# have no effect if this option is set to NO (the default) + +HAVE_DOT = NO + +# The DOT_NUM_THREADS specifies the number of dot invocations doxygen is +# allowed to run in parallel. When set to 0 (the default) doxygen will +# base this on the number of processors available in the system. You can set it +# explicitly to a value larger than 0 to get control over the balance +# between CPU load and processing speed. + +DOT_NUM_THREADS = 0 + +# By default doxygen will use the Helvetica font for all dot files that +# doxygen generates. When you want a differently looking font you can specify +# the font name using DOT_FONTNAME. You need to make sure dot is able to find +# the font, which can be done by putting it in a standard location or by setting +# the DOTFONTPATH environment variable or by setting DOT_FONTPATH to the +# directory containing the font. + +DOT_FONTNAME = FreeSans + +# The DOT_FONTSIZE tag can be used to set the size of the font of dot graphs. +# The default size is 10pt. + +DOT_FONTSIZE = 10 + +# By default doxygen will tell dot to use the Helvetica font. +# If you specify a different font using DOT_FONTNAME you can use DOT_FONTPATH to +# set the path where dot can find it. + +DOT_FONTPATH = + +# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect inheritance relations. Setting this tag to YES will force the +# CLASS_DIAGRAMS tag to NO. + +CLASS_GRAPH = YES + +# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect implementation dependencies (inheritance, containment, and +# class references variables) of the class with other documented classes. + +COLLABORATION_GRAPH = YES + +# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for groups, showing the direct groups dependencies + +GROUP_GRAPHS = YES + +# If the UML_LOOK tag is set to YES doxygen will generate inheritance and +# collaboration diagrams in a style similar to the OMG's Unified Modeling +# Language. + +UML_LOOK = NO + +# If the UML_LOOK tag is enabled, the fields and methods are shown inside +# the class node. If there are many fields or methods and many nodes the +# graph may become too big to be useful. The UML_LIMIT_NUM_FIELDS +# threshold limits the number of items for each type to make the size more +# managable. Set this to 0 for no limit. Note that the threshold may be +# exceeded by 50% before the limit is enforced. + +UML_LIMIT_NUM_FIELDS = 10 + +# If set to YES, the inheritance and collaboration graphs will show the +# relations between templates and their instances. + +TEMPLATE_RELATIONS = NO + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT +# tags are set to YES then doxygen will generate a graph for each documented +# file showing the direct and indirect include dependencies of the file with +# other documented files. + +INCLUDE_GRAPH = YES + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and +# HAVE_DOT tags are set to YES then doxygen will generate a graph for each +# documented header file showing the documented files that directly or +# indirectly include this file. + +INCLUDED_BY_GRAPH = YES + +# If the CALL_GRAPH and HAVE_DOT options are set to YES then +# doxygen will generate a call dependency graph for every global function +# or class method. Note that enabling this option will significantly increase +# the time of a run. So in most cases it will be better to enable call graphs +# for selected functions only using the \callgraph command. + +CALL_GRAPH = NO + +# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then +# doxygen will generate a caller dependency graph for every global function +# or class method. Note that enabling this option will significantly increase +# the time of a run. So in most cases it will be better to enable caller +# graphs for selected functions only using the \callergraph command. + +CALLER_GRAPH = NO + +# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen +# will generate a graphical hierarchy of all classes instead of a textual one. + +GRAPHICAL_HIERARCHY = YES + +# If the DIRECTORY_GRAPH and HAVE_DOT tags are set to YES +# then doxygen will show the dependencies a directory has on other directories +# in a graphical way. The dependency relations are determined by the #include +# relations between the files in the directories. + +DIRECTORY_GRAPH = YES + +# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images +# generated by dot. Possible values are svg, png, jpg, or gif. +# If left blank png will be used. If you choose svg you need to set +# HTML_FILE_EXTENSION to xhtml in order to make the SVG files +# visible in IE 9+ (other browsers do not have this requirement). + +DOT_IMAGE_FORMAT = png + +# If DOT_IMAGE_FORMAT is set to svg, then this option can be set to YES to +# enable generation of interactive SVG images that allow zooming and panning. +# Note that this requires a modern browser other than Internet Explorer. +# Tested and working are Firefox, Chrome, Safari, and Opera. For IE 9+ you +# need to set HTML_FILE_EXTENSION to xhtml in order to make the SVG files +# visible. Older versions of IE do not have SVG support. + +INTERACTIVE_SVG = NO + +# The tag DOT_PATH can be used to specify the path where the dot tool can be +# found. If left blank, it is assumed the dot tool can be found in the path. + +DOT_PATH = + +# The DOTFILE_DIRS tag can be used to specify one or more directories that +# contain dot files that are included in the documentation (see the +# \dotfile command). + +DOTFILE_DIRS = + +# The MSCFILE_DIRS tag can be used to specify one or more directories that +# contain msc files that are included in the documentation (see the +# \mscfile command). + +MSCFILE_DIRS = + +# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of +# nodes that will be shown in the graph. If the number of nodes in a graph +# becomes larger than this value, doxygen will truncate the graph, which is +# visualized by representing a node as a red box. Note that doxygen if the +# number of direct children of the root node in a graph is already larger than +# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note +# that the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH. + +DOT_GRAPH_MAX_NODES = 50 + +# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the +# graphs generated by dot. A depth value of 3 means that only nodes reachable +# from the root by following a path via at most 3 edges will be shown. Nodes +# that lay further from the root node will be omitted. Note that setting this +# option to 1 or 2 may greatly reduce the computation time needed for large +# code bases. Also note that the size of a graph can be further restricted by +# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction. + +MAX_DOT_GRAPH_DEPTH = 1000 + +# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent +# background. This is disabled by default, because dot on Windows does not +# seem to support this out of the box. Warning: Depending on the platform used, +# enabling this option may lead to badly anti-aliased labels on the edges of +# a graph (i.e. they become hard to read). + +DOT_TRANSPARENT = YES + +# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output +# files in one run (i.e. multiple -o and -T options on the command line). This +# makes dot run faster, but since only newer versions of dot (>1.8.10) +# support this, this feature is disabled by default. + +DOT_MULTI_TARGETS = NO + +# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will +# generate a legend page explaining the meaning of the various boxes and +# arrows in the dot generated graphs. + +GENERATE_LEGEND = YES + +# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will +# remove the intermediate dot files that are used to generate +# the various graphs. + +DOT_CLEANUP = YES diff --git a/src/http/qjson/doc/footer.html b/src/http/qjson/doc/footer.html new file mode 100644 index 000000000..fe2e2a770 --- /dev/null +++ b/src/http/qjson/doc/footer.html @@ -0,0 +1,32 @@ +
+ + + + + + + +
+ + SourceForge Logo + + hosts this site. + + + Send comments to:
+ QJson Developers +
+ + + + + + diff --git a/src/http/qjson/doc/header.html b/src/http/qjson/doc/header.html new file mode 100644 index 000000000..d0ca99369 --- /dev/null +++ b/src/http/qjson/doc/header.html @@ -0,0 +1,13 @@ + + + +QJson - a Qt based library for mapping JSON data to QVariant objects + + + + + + + QJson home page + +
diff --git a/src/http/qjson/doc/qjson.dox b/src/http/qjson/doc/qjson.dox new file mode 100644 index 000000000..ea84e7554 --- /dev/null +++ b/src/http/qjson/doc/qjson.dox @@ -0,0 +1,87 @@ +/** +\mainpage +\section _intro Introduction + +JSON (JavaScript Object Notation) + is a lightweight data-interchange format. +It can represents integer, real number, string, an ordered sequence of value, and +a collection of name/value pairs. + +QJson is a qt-based library that maps JSON data to QVariant objects. + +JSON arrays will be mapped to QVariantList instances, while JSON's objects will +be mapped to QVariantMap. + +\section _usage Usage +Converting JSON's data to QVariant instance is really simple: +\code +// create a JSonDriver instance +QJson::Parser parser; + +bool ok; + +// json is a QString containing the data to convert +QVariant result = parser.parse (json, &ok); +\endcode + +Suppose you're going to convert this JSON data: +\verbatim +{ + "encoding" : "UTF-8", + "plug-ins" : [ + "python", + "c++", + "ruby" + ], + "indent" : { "length" : 3, "use_space" : true } +} +\endverbatim + +The following code would convert the JSON data and parse it: +\code +QJson::Parser parser; +bool ok; + +QVariantMap result = parser.parse (json, &ok).toMap(); +if (!ok) { + qFatal("An error occured during parsing"); + exit (1); +} + +qDebug() << "encoding:" << result["encoding"].toString(); +qDebug() << "plugins:"; + +foreach (QVariant plugin, result["plug-ins"].toList()) { + qDebug() << "\t-" << plugin.toString(); +} + +QVariantMap nestedMap = result["indent"].toMap(); +qDebug() << "length:" << nestedMap["length"].toInt(); +qDebug() << "use_space:" << nestedMap["use_space"].toBool(); +\endcode +The output would be: +\verbatim +encoding: "UTF-8" +plugins: + - "python" + - "c++" + - "ruby" +length: 3 +use_space: true +\endverbatim + +The QJson::QObjectHelper class permits to serialize QObject instances into JSON. QJson::QObjectHelper also allows to +initialize a QObject using the values stored inside of a JSON object. + +\section _build Build instructions +QJson build system is based on cmake. Download QJson sources, extract them, move inside the sources directory and then: +\code +mkdir build +cd build +cmake .. +make +sudo make install +\endcode + +\author Flavio Castelli +*/ diff --git a/src/http/qjson/include/QJson/Parser b/src/http/qjson/include/QJson/Parser new file mode 100644 index 000000000..68f06e432 --- /dev/null +++ b/src/http/qjson/include/QJson/Parser @@ -0,0 +1 @@ +#include "../../src/parser.h" diff --git a/src/http/qjson/include/QJson/QObjectHelper b/src/http/qjson/include/QJson/QObjectHelper new file mode 100644 index 000000000..1b72c2e0a --- /dev/null +++ b/src/http/qjson/include/QJson/QObjectHelper @@ -0,0 +1 @@ +#include "../../src/qobjecthelper.h" diff --git a/src/http/qjson/include/QJson/Serializer b/src/http/qjson/include/QJson/Serializer new file mode 100644 index 000000000..2b7fe7a9e --- /dev/null +++ b/src/http/qjson/include/QJson/Serializer @@ -0,0 +1 @@ +#include "../../src/serializer.h" diff --git a/src/http/qjson/src/.gitignore b/src/http/qjson/src/.gitignore new file mode 100644 index 000000000..04ec50a55 --- /dev/null +++ b/src/http/qjson/src/.gitignore @@ -0,0 +1,3 @@ +moc_* +*.o +Makefile diff --git a/src/http/qjson/src/CMakeLists.txt b/src/http/qjson/src/CMakeLists.txt new file mode 100644 index 000000000..82ba6a6a3 --- /dev/null +++ b/src/http/qjson/src/CMakeLists.txt @@ -0,0 +1,63 @@ +# add_custom_command (OUTPUT ${qjson_SOURCE_DIR}/lib/json_parser.cc +# PRE_BUILD +# COMMAND bison -t -o json_parser.cc -d json_parser.yy +# DEPENDS json_parser.yy +# WORKING_DIRECTORY ${qjson_SOURCE_DIR}/lib/ +# ) + +# To regenerate json_scanner.cc use: +# flex json_scanner.yy + +set(qjson_MOC_HDRS + parserrunnable.h + serializerrunnable.h +) + +IF (NOT Qt5Core_FOUND) + qt4_wrap_cpp(qjson_MOC_SRCS ${qjson_MOC_HDRS}) +ENDIF() + +set (qjson_SRCS parser.cpp qobjecthelper.cpp json_scanner.cpp json_parser.cc parserrunnable.cpp serializer.cpp serializerrunnable.cpp) +set (qjson_HEADERS parser.h parserrunnable.h qobjecthelper.h serializer.h serializerrunnable.h qjson_export.h) + +# Required to use the intree copy of FlexLexer.h +INCLUDE_DIRECTORIES(.) + +add_library (qjson STATIC ${qjson_SRCS} ${qjson_MOC_SRCS} ${qjson_HEADERS}) +IF (Qt5Core_FOUND) + target_link_libraries( qjson ${Qt5Core_LIBRARIES}) +ELSE() + target_link_libraries( qjson ${QT_LIBRARIES}) +ENDIF() + +if(NOT android) + set_target_properties(qjson PROPERTIES + VERSION ${QJSON_LIB_MAJOR_VERSION}.${QJSON_LIB_MINOR_VERSION}.${QJSON_LIB_PATCH_VERSION} + SOVERSION ${QJSON_LIB_MAJOR_VERSION} + ) +endif() +set_target_properties(qjson PROPERTIES + DEFINE_SYMBOL QJSON_MAKEDLL + PUBLIC_HEADER "${qjson_HEADERS}" + FRAMEWORK ${OSX_FRAMEWORK} + ) + +add_gcc_compiler_cxxflags("-fexceptions") + +INSTALL(TARGETS qjson EXPORT qjson-export + LIBRARY DESTINATION ${LIB_INSTALL_DIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin + ARCHIVE DESTINATION ${LIB_INSTALL_DIR} + FRAMEWORK DESTINATION ${FRAMEWORK_INSTALL_DIR} + PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/qjson +) + +if(MSVC) + get_target_property(LOCATION qjson LOCATION_DEBUG) + string(REGEX REPLACE "\\.[^.]*$" ".pdb" LOCATION "${LOCATION}") + install(FILES ${LOCATION} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin CONFIGURATIONS Debug) + + get_target_property(LOCATION qjson LOCATION_RELWITHDEBINFO) + string(REGEX REPLACE "\\.[^.]*$" ".pdb" LOCATION "${LOCATION}") + install(FILES ${LOCATION} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin CONFIGURATIONS RelWithDebInfo) +endif(MSVC) diff --git a/src/http/qjson/src/FlexLexer.h b/src/http/qjson/src/FlexLexer.h new file mode 100644 index 000000000..bad4ce03f --- /dev/null +++ b/src/http/qjson/src/FlexLexer.h @@ -0,0 +1,206 @@ +// -*-C++-*- +// FlexLexer.h -- define interfaces for lexical analyzer classes generated +// by flex + +// Copyright (c) 1993 The Regents of the University of California. +// All rights reserved. +// +// This code is derived from software contributed to Berkeley by +// Kent Williams and Tom Epperly. +// +// 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. + +// Neither the name of the University 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR +// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// PURPOSE. + +// This file defines FlexLexer, an abstract class which specifies the +// external interface provided to flex C++ lexer objects, and yyFlexLexer, +// which defines a particular lexer class. +// +// If you want to create multiple lexer classes, you use the -P flag +// to rename each yyFlexLexer to some other xxFlexLexer. You then +// include in your other sources once per lexer class: +// +// #undef yyFlexLexer +// #define yyFlexLexer xxFlexLexer +// #include +// +// #undef yyFlexLexer +// #define yyFlexLexer zzFlexLexer +// #include +// ... + +#ifndef __FLEX_LEXER_H +// Never included before - need to define base class. +#define __FLEX_LEXER_H + +#include +# ifndef FLEX_STD +# define FLEX_STD std:: +# endif + +extern "C++" { + +struct yy_buffer_state; +typedef int yy_state_type; + +class FlexLexer { +public: + virtual ~FlexLexer() { } + + const char* YYText() const { return yytext; } + int YYLeng() const { return yyleng; } + + virtual void + yy_switch_to_buffer( struct yy_buffer_state* new_buffer ) = 0; + virtual struct yy_buffer_state* + yy_create_buffer( FLEX_STD istream* s, int size ) = 0; + virtual void yy_delete_buffer( struct yy_buffer_state* b ) = 0; + virtual void yyrestart( FLEX_STD istream* s ) = 0; + + virtual int yylex() = 0; + + // Call yylex with new input/output sources. + int yylex( FLEX_STD istream* new_in, FLEX_STD ostream* new_out = 0 ) + { + switch_streams( new_in, new_out ); + return yylex(); + } + + // Switch to new input/output streams. A nil stream pointer + // indicates "keep the current one". + virtual void switch_streams( FLEX_STD istream* new_in = 0, + FLEX_STD ostream* new_out = 0 ) = 0; + + int lineno() const { return yylineno; } + + int debug() const { return yy_flex_debug; } + void set_debug( int flag ) { yy_flex_debug = flag; } + +protected: + char* yytext; + int yyleng; + int yylineno; // only maintained if you use %option yylineno + int yy_flex_debug; // only has effect with -d or "%option debug" +}; + +} +#endif // FLEXLEXER_H + +#if defined(yyFlexLexer) || ! defined(yyFlexLexerOnce) +// Either this is the first time through (yyFlexLexerOnce not defined), +// or this is a repeated include to define a different flavor of +// yyFlexLexer, as discussed in the flex manual. +#define yyFlexLexerOnce + +extern "C++" { + +class yyFlexLexer : public FlexLexer { +public: + // arg_yyin and arg_yyout default to the cin and cout, but we + // only make that assignment when initializing in yylex(). + yyFlexLexer( FLEX_STD istream* arg_yyin = 0, FLEX_STD ostream* arg_yyout = 0 ); + + virtual ~yyFlexLexer(); + + void yy_switch_to_buffer( struct yy_buffer_state* new_buffer ); + struct yy_buffer_state* yy_create_buffer( FLEX_STD istream* s, int size ); + void yy_delete_buffer( struct yy_buffer_state* b ); + void yyrestart( FLEX_STD istream* s ); + + void yypush_buffer_state( struct yy_buffer_state* new_buffer ); + void yypop_buffer_state(); + + virtual int yylex(); + virtual void switch_streams( FLEX_STD istream* new_in, FLEX_STD ostream* new_out = 0 ); + virtual int yywrap(); + +protected: + virtual int LexerInput( char* buf, int max_size ); + virtual void LexerOutput( const char* buf, int size ); + virtual void LexerError( const char* msg ); + + void yyunput( int c, char* buf_ptr ); + int yyinput(); + + void yy_load_buffer_state(); + void yy_init_buffer( struct yy_buffer_state* b, FLEX_STD istream* s ); + void yy_flush_buffer( struct yy_buffer_state* b ); + + int yy_start_stack_ptr; + int yy_start_stack_depth; + int* yy_start_stack; + + void yy_push_state( int new_state ); + void yy_pop_state(); + int yy_top_state(); + + yy_state_type yy_get_previous_state(); + yy_state_type yy_try_NUL_trans( yy_state_type current_state ); + int yy_get_next_buffer(); + + FLEX_STD istream* yyin; // input source for default LexerInput + FLEX_STD ostream* yyout; // output sink for default LexerOutput + + // yy_hold_char holds the character lost when yytext is formed. + char yy_hold_char; + + // Number of characters read into yy_ch_buf. + int yy_n_chars; + + // Points to current character in buffer. + char* yy_c_buf_p; + + int yy_init; // whether we need to initialize + int yy_start; // start state number + + // Flag which is used to allow yywrap()'s to do buffer switches + // instead of setting up a fresh yyin. A bit of a hack ... + int yy_did_buffer_switch_on_eof; + + + size_t yy_buffer_stack_top; /**< index of top of stack. */ + size_t yy_buffer_stack_max; /**< capacity of stack. */ + struct yy_buffer_state ** yy_buffer_stack; /**< Stack as an array. */ + void yyensure_buffer_stack(void); + + // The following are not always needed, but may be depending + // on use of certain flex features (like REJECT or yymore()). + + yy_state_type yy_last_accepting_state; + char* yy_last_accepting_cpos; + + yy_state_type* yy_state_buf; + yy_state_type* yy_state_ptr; + + char* yy_full_match; + int* yy_full_state; + int yy_full_lp; + + int yy_lp; + int yy_looking_for_trail_begin; + + int yy_more_flag; + int yy_more_len; + int yy_more_offset; + int yy_prev_more_offset; +}; + +} + +#endif // yyFlexLexer || ! yyFlexLexerOnce + diff --git a/src/http/qjson/src/json_parser.cc b/src/http/qjson/src/json_parser.cc new file mode 100644 index 000000000..84fdfac73 --- /dev/null +++ b/src/http/qjson/src/json_parser.cc @@ -0,0 +1,1103 @@ +/* A Bison parser, made by GNU Bison 2.7. */ + +/* Skeleton implementation for Bison LALR(1) parsers in C++ + + Copyright (C) 2002-2012 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + + +/* First part of user declarations. */ + +/* Line 279 of lalr1.cc */ +#line 38 "json_parser.cc" + + +#include "json_parser.hh" + +/* User implementation prologue. */ + +/* Line 285 of lalr1.cc */ +#line 46 "json_parser.cc" + + +# ifndef YY_NULL +# if defined __cplusplus && 201103L <= __cplusplus +# define YY_NULL nullptr +# else +# define YY_NULL 0 +# endif +# endif + +#ifndef YY_ +# if defined YYENABLE_NLS && YYENABLE_NLS +# if ENABLE_NLS +# include /* FIXME: INFRINGES ON USER NAME SPACE */ +# define YY_(msgid) dgettext ("bison-runtime", msgid) +# endif +# endif +# ifndef YY_ +# define YY_(msgid) msgid +# endif +#endif + +#define YYRHSLOC(Rhs, K) ((Rhs)[K]) +/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. + If N is 0, then set CURRENT to the empty location which ends + the previous symbol: RHS[0] (always defined). */ + +# ifndef YYLLOC_DEFAULT +# define YYLLOC_DEFAULT(Current, Rhs, N) \ + do \ + if (N) \ + { \ + (Current).begin = YYRHSLOC (Rhs, 1).begin; \ + (Current).end = YYRHSLOC (Rhs, N).end; \ + } \ + else \ + { \ + (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \ + } \ + while (/*CONSTCOND*/ false) +# endif + + +/* Suppress unused-variable warnings by "using" E. */ +#define YYUSE(e) ((void) (e)) + +/* Enable debugging if requested. */ +#if YYDEBUG + +/* A pseudo ostream that takes yydebug_ into account. */ +# define YYCDEBUG if (yydebug_) (*yycdebug_) + +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ +do { \ + if (yydebug_) \ + { \ + *yycdebug_ << Title << ' '; \ + yy_symbol_print_ ((Type), (Value), (Location)); \ + *yycdebug_ << std::endl; \ + } \ +} while (false) + +# define YY_REDUCE_PRINT(Rule) \ +do { \ + if (yydebug_) \ + yy_reduce_print_ (Rule); \ +} while (false) + +# define YY_STACK_PRINT() \ +do { \ + if (yydebug_) \ + yystack_print_ (); \ +} while (false) + +#else /* !YYDEBUG */ + +# define YYCDEBUG if (false) std::cerr +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) YYUSE(Type) +# define YY_REDUCE_PRINT(Rule) static_cast(0) +# define YY_STACK_PRINT() static_cast(0) + +#endif /* !YYDEBUG */ + +#define yyerrok (yyerrstatus_ = 0) +#define yyclearin (yychar = yyempty_) + +#define YYACCEPT goto yyacceptlab +#define YYABORT goto yyabortlab +#define YYERROR goto yyerrorlab +#define YYRECOVERING() (!!yyerrstatus_) + + +namespace yy { +/* Line 353 of lalr1.cc */ +#line 141 "json_parser.cc" + + /* Return YYSTR after stripping away unnecessary quotes and + backslashes, so that it's suitable for yyerror. The heuristic is + that double-quoting is unnecessary unless the string contains an + apostrophe, a comma, or backslash (other than backslash-backslash). + YYSTR is taken from yytname. */ + std::string + json_parser::yytnamerr_ (const char *yystr) + { + if (*yystr == '"') + { + std::string yyr = ""; + char const *yyp = yystr; + + for (;;) + switch (*++yyp) + { + case '\'': + case ',': + goto do_not_strip_quotes; + + case '\\': + if (*++yyp != '\\') + goto do_not_strip_quotes; + /* Fall through. */ + default: + yyr += *yyp; + break; + + case '"': + return yyr; + } + do_not_strip_quotes: ; + } + + return yystr; + } + + + /// Build a parser object. + json_parser::json_parser (QJson::ParserPrivate* driver_yyarg) + : +#if YYDEBUG + yydebug_ (false), + yycdebug_ (&std::cerr), +#endif + driver (driver_yyarg) + { + } + + json_parser::~json_parser () + { + } + +#if YYDEBUG + /*--------------------------------. + | Print this symbol on YYOUTPUT. | + `--------------------------------*/ + + inline void + json_parser::yy_symbol_value_print_ (int yytype, + const semantic_type* yyvaluep, const location_type* yylocationp) + { + YYUSE (yylocationp); + YYUSE (yyvaluep); + std::ostream& yyo = debug_stream (); + std::ostream& yyoutput = yyo; + YYUSE (yyoutput); + switch (yytype) + { + default: + break; + } + } + + + void + json_parser::yy_symbol_print_ (int yytype, + const semantic_type* yyvaluep, const location_type* yylocationp) + { + *yycdebug_ << (yytype < yyntokens_ ? "token" : "nterm") + << ' ' << yytname_[yytype] << " (" + << *yylocationp << ": "; + yy_symbol_value_print_ (yytype, yyvaluep, yylocationp); + *yycdebug_ << ')'; + } +#endif + + void + json_parser::yydestruct_ (const char* yymsg, + int yytype, semantic_type* yyvaluep, location_type* yylocationp) + { + YYUSE (yylocationp); + YYUSE (yymsg); + YYUSE (yyvaluep); + + if (yymsg) + YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp); + + switch (yytype) + { + + default: + break; + } + } + + void + json_parser::yypop_ (unsigned int n) + { + yystate_stack_.pop (n); + yysemantic_stack_.pop (n); + yylocation_stack_.pop (n); + } + +#if YYDEBUG + std::ostream& + json_parser::debug_stream () const + { + return *yycdebug_; + } + + void + json_parser::set_debug_stream (std::ostream& o) + { + yycdebug_ = &o; + } + + + json_parser::debug_level_type + json_parser::debug_level () const + { + return yydebug_; + } + + void + json_parser::set_debug_level (debug_level_type l) + { + yydebug_ = l; + } +#endif + + inline bool + json_parser::yy_pact_value_is_default_ (int yyvalue) + { + return yyvalue == yypact_ninf_; + } + + inline bool + json_parser::yy_table_value_is_error_ (int yyvalue) + { + return yyvalue == yytable_ninf_; + } + + int + json_parser::parse () + { + /// Lookahead and lookahead in internal form. + int yychar = yyempty_; + int yytoken = 0; + + // State. + int yyn; + int yylen = 0; + int yystate = 0; + + // Error handling. + int yynerrs_ = 0; + int yyerrstatus_ = 0; + + /// Semantic value of the lookahead. + static semantic_type yyval_default; + semantic_type yylval = yyval_default; + /// Location of the lookahead. + location_type yylloc; + /// The locations where the error started and ended. + location_type yyerror_range[3]; + + /// $$. + semantic_type yyval; + /// @$. + location_type yyloc; + + int yyresult; + + // FIXME: This shoud be completely indented. It is not yet to + // avoid gratuitous conflicts when merging into the master branch. + try + { + YYCDEBUG << "Starting parse" << std::endl; + + + /* Initialize the stacks. The initial state will be pushed in + yynewstate, since the latter expects the semantical and the + location values to have been already stored, initialize these + stacks with a primary value. */ + yystate_stack_ = state_stack_type (0); + yysemantic_stack_ = semantic_stack_type (0); + yylocation_stack_ = location_stack_type (0); + yysemantic_stack_.push (yylval); + yylocation_stack_.push (yylloc); + + /* New state. */ + yynewstate: + yystate_stack_.push (yystate); + YYCDEBUG << "Entering state " << yystate << std::endl; + + /* Accept? */ + if (yystate == yyfinal_) + goto yyacceptlab; + + goto yybackup; + + /* Backup. */ + yybackup: + + /* Try to take a decision without lookahead. */ + yyn = yypact_[yystate]; + if (yy_pact_value_is_default_ (yyn)) + goto yydefault; + + /* Read a lookahead token. */ + if (yychar == yyempty_) + { + YYCDEBUG << "Reading a token: "; + yychar = yylex (&yylval, &yylloc, driver); + } + + /* Convert token to internal form. */ + if (yychar <= yyeof_) + { + yychar = yytoken = yyeof_; + YYCDEBUG << "Now at end of input." << std::endl; + } + else + { + yytoken = yytranslate_ (yychar); + YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc); + } + + /* If the proper action on seeing token YYTOKEN is to reduce or to + detect an error, take that action. */ + yyn += yytoken; + if (yyn < 0 || yylast_ < yyn || yycheck_[yyn] != yytoken) + goto yydefault; + + /* Reduce or error. */ + yyn = yytable_[yyn]; + if (yyn <= 0) + { + if (yy_table_value_is_error_ (yyn)) + goto yyerrlab; + yyn = -yyn; + goto yyreduce; + } + + /* Shift the lookahead token. */ + YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); + + /* Discard the token being shifted. */ + yychar = yyempty_; + + yysemantic_stack_.push (yylval); + yylocation_stack_.push (yylloc); + + /* Count tokens shifted since error; after three, turn off error + status. */ + if (yyerrstatus_) + --yyerrstatus_; + + yystate = yyn; + goto yynewstate; + + /*-----------------------------------------------------------. + | yydefault -- do the default action for the current state. | + `-----------------------------------------------------------*/ + yydefault: + yyn = yydefact_[yystate]; + if (yyn == 0) + goto yyerrlab; + goto yyreduce; + + /*-----------------------------. + | yyreduce -- Do a reduction. | + `-----------------------------*/ + yyreduce: + yylen = yyr2_[yyn]; + /* If YYLEN is nonzero, implement the default value of the action: + `$$ = $1'. Otherwise, use the top of the stack. + + Otherwise, the following line sets YYVAL to garbage. + This behavior is undocumented and Bison + users should not rely upon it. */ + if (yylen) + yyval = yysemantic_stack_[yylen - 1]; + else + yyval = yysemantic_stack_[0]; + + // Compute the default @$. + { + slice slice (yylocation_stack_, yylen); + YYLLOC_DEFAULT (yyloc, slice, yylen); + } + + // Perform the reduction. + YY_REDUCE_PRINT (yyn); + switch (yyn) + { + case 2: +/* Line 670 of lalr1.cc */ +#line 82 "json_parser.yy" + { + driver->m_result = (yysemantic_stack_[(1) - (1)]); + qjsonDebug() << "json_parser - parsing finished"; + } + break; + + case 3: +/* Line 670 of lalr1.cc */ +#line 87 "json_parser.yy" + { (yyval) = (yysemantic_stack_[(1) - (1)]); } + break; + + case 4: +/* Line 670 of lalr1.cc */ +#line 89 "json_parser.yy" + { + qCritical()<< "json_parser - syntax error found, " + << "forcing abort, Line" << (yyloc).begin.line << "Column" << (yyloc).begin.column; + YYABORT; + } + break; + + case 6: +/* Line 670 of lalr1.cc */ +#line 96 "json_parser.yy" + { + (yyval) = QVariant(QVariantMap()); + } + break; + + case 7: +/* Line 670 of lalr1.cc */ +#line 99 "json_parser.yy" + { + QVariantMap* map = (yysemantic_stack_[(3) - (2)]).value(); + (yyval) = QVariant(*map); + delete map; + } + break; + + case 8: +/* Line 670 of lalr1.cc */ +#line 105 "json_parser.yy" + { + QVariantMap* pair = new QVariantMap(); + pair->insert((yysemantic_stack_[(3) - (1)]).toString(), (yysemantic_stack_[(3) - (3)])); + (yyval).setValue(pair); + } + break; + + case 9: +/* Line 670 of lalr1.cc */ +#line 110 "json_parser.yy" + { + (yyval).value()->insert((yysemantic_stack_[(5) - (3)]).toString(), (yysemantic_stack_[(5) - (5)])); + } + break; + + case 10: +/* Line 670 of lalr1.cc */ +#line 114 "json_parser.yy" + { + (yyval) = QVariant(QVariantList()); + } + break; + + case 11: +/* Line 670 of lalr1.cc */ +#line 117 "json_parser.yy" + { + QVector* list = (yysemantic_stack_[(3) - (2)]).value* >(); + (yyval) = QVariant(list->toList()); + delete list; + } + break; + + case 12: +/* Line 670 of lalr1.cc */ +#line 123 "json_parser.yy" + { + QVector* list = new QVector(1); + list->replace(0, (yysemantic_stack_[(1) - (1)])); + (yyval).setValue(list); + } + break; + + case 13: +/* Line 670 of lalr1.cc */ +#line 128 "json_parser.yy" + { + (yyval).value* >()->append((yysemantic_stack_[(3) - (3)])); + } + break; + + +/* Line 670 of lalr1.cc */ +#line 549 "json_parser.cc" + default: + break; + } + + /* User semantic actions sometimes alter yychar, and that requires + that yytoken be updated with the new translation. We take the + approach of translating immediately before every use of yytoken. + One alternative is translating here after every semantic action, + but that translation would be missed if the semantic action + invokes YYABORT, YYACCEPT, or YYERROR immediately after altering + yychar. In the case of YYABORT or YYACCEPT, an incorrect + destructor might then be invoked immediately. In the case of + YYERROR, subsequent parser actions might lead to an incorrect + destructor call or verbose syntax error message before the + lookahead is translated. */ + YY_SYMBOL_PRINT ("-> $$ =", yyr1_[yyn], &yyval, &yyloc); + + yypop_ (yylen); + yylen = 0; + YY_STACK_PRINT (); + + yysemantic_stack_.push (yyval); + yylocation_stack_.push (yyloc); + + /* Shift the result of the reduction. */ + yyn = yyr1_[yyn]; + yystate = yypgoto_[yyn - yyntokens_] + yystate_stack_[0]; + if (0 <= yystate && yystate <= yylast_ + && yycheck_[yystate] == yystate_stack_[0]) + yystate = yytable_[yystate]; + else + yystate = yydefgoto_[yyn - yyntokens_]; + goto yynewstate; + + /*------------------------------------. + | yyerrlab -- here on detecting error | + `------------------------------------*/ + yyerrlab: + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = yytranslate_ (yychar); + + /* If not already recovering from an error, report this error. */ + if (!yyerrstatus_) + { + ++yynerrs_; + if (yychar == yyempty_) + yytoken = yyempty_; + error (yylloc, yysyntax_error_ (yystate, yytoken)); + } + + yyerror_range[1] = yylloc; + if (yyerrstatus_ == 3) + { + /* If just tried and failed to reuse lookahead token after an + error, discard it. */ + if (yychar <= yyeof_) + { + /* Return failure if at end of input. */ + if (yychar == yyeof_) + YYABORT; + } + else + { + yydestruct_ ("Error: discarding", yytoken, &yylval, &yylloc); + yychar = yyempty_; + } + } + + /* Else will try to reuse lookahead token after shifting the error + token. */ + goto yyerrlab1; + + + /*---------------------------------------------------. + | yyerrorlab -- error raised explicitly by YYERROR. | + `---------------------------------------------------*/ + yyerrorlab: + + /* Pacify compilers like GCC when the user code never invokes + YYERROR and the label yyerrorlab therefore never appears in user + code. */ + if (false) + goto yyerrorlab; + + yyerror_range[1] = yylocation_stack_[yylen - 1]; + /* Do not reclaim the symbols of the rule which action triggered + this YYERROR. */ + yypop_ (yylen); + yylen = 0; + yystate = yystate_stack_[0]; + goto yyerrlab1; + + /*-------------------------------------------------------------. + | yyerrlab1 -- common code for both syntax error and YYERROR. | + `-------------------------------------------------------------*/ + yyerrlab1: + yyerrstatus_ = 3; /* Each real token shifted decrements this. */ + + for (;;) + { + yyn = yypact_[yystate]; + if (!yy_pact_value_is_default_ (yyn)) + { + yyn += yyterror_; + if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yyterror_) + { + yyn = yytable_[yyn]; + if (0 < yyn) + break; + } + } + + /* Pop the current state because it cannot handle the error token. */ + if (yystate_stack_.height () == 1) + YYABORT; + + yyerror_range[1] = yylocation_stack_[0]; + yydestruct_ ("Error: popping", + yystos_[yystate], + &yysemantic_stack_[0], &yylocation_stack_[0]); + yypop_ (); + yystate = yystate_stack_[0]; + YY_STACK_PRINT (); + } + + yyerror_range[2] = yylloc; + // Using YYLLOC is tempting, but would change the location of + // the lookahead. YYLOC is available though. + YYLLOC_DEFAULT (yyloc, yyerror_range, 2); + yysemantic_stack_.push (yylval); + yylocation_stack_.push (yyloc); + + /* Shift the error token. */ + YY_SYMBOL_PRINT ("Shifting", yystos_[yyn], + &yysemantic_stack_[0], &yylocation_stack_[0]); + + yystate = yyn; + goto yynewstate; + + /* Accept. */ + yyacceptlab: + yyresult = 0; + goto yyreturn; + + /* Abort. */ + yyabortlab: + yyresult = 1; + goto yyreturn; + + yyreturn: + if (yychar != yyempty_) + { + /* Make sure we have latest lookahead translation. See comments + at user semantic actions for why this is necessary. */ + yytoken = yytranslate_ (yychar); + yydestruct_ ("Cleanup: discarding lookahead", yytoken, &yylval, + &yylloc); + } + + /* Do not reclaim the symbols of the rule which action triggered + this YYABORT or YYACCEPT. */ + yypop_ (yylen); + while (1 < yystate_stack_.height ()) + { + yydestruct_ ("Cleanup: popping", + yystos_[yystate_stack_[0]], + &yysemantic_stack_[0], + &yylocation_stack_[0]); + yypop_ (); + } + + return yyresult; + } + catch (...) + { + YYCDEBUG << "Exception caught: cleaning lookahead and stack" + << std::endl; + // Do not try to display the values of the reclaimed symbols, + // as their printer might throw an exception. + if (yychar != yyempty_) + { + /* Make sure we have latest lookahead translation. See + comments at user semantic actions for why this is + necessary. */ + yytoken = yytranslate_ (yychar); + yydestruct_ (YY_NULL, yytoken, &yylval, &yylloc); + } + + while (1 < yystate_stack_.height ()) + { + yydestruct_ (YY_NULL, + yystos_[yystate_stack_[0]], + &yysemantic_stack_[0], + &yylocation_stack_[0]); + yypop_ (); + } + throw; + } + } + + // Generate an error message. + std::string + json_parser::yysyntax_error_ (int yystate, int yytoken) + { + std::string yyres; + // Number of reported tokens (one for the "unexpected", one per + // "expected"). + size_t yycount = 0; + // Its maximum. + enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; + // Arguments of yyformat. + char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; + + /* There are many possibilities here to consider: + - If this state is a consistent state with a default action, then + the only way this function was invoked is if the default action + is an error action. In that case, don't check for expected + tokens because there are none. + - The only way there can be no lookahead present (in yytoken) is + if this state is a consistent state with a default action. + Thus, detecting the absence of a lookahead is sufficient to + determine that there is no unexpected or expected token to + report. In that case, just report a simple "syntax error". + - Don't assume there isn't a lookahead just because this state is + a consistent state with a default action. There might have + been a previous inconsistent state, consistent state with a + non-default action, or user semantic action that manipulated + yychar. + - Of course, the expected token list depends on states to have + correct lookahead information, and it depends on the parser not + to perform extra reductions after fetching a lookahead from the + scanner and before detecting a syntax error. Thus, state + merging (from LALR or IELR) and default reductions corrupt the + expected token list. However, the list is correct for + canonical LR with one exception: it will still contain any + token that will not be accepted due to an error action in a + later state. + */ + if (yytoken != yyempty_) + { + yyarg[yycount++] = yytname_[yytoken]; + int yyn = yypact_[yystate]; + if (!yy_pact_value_is_default_ (yyn)) + { + /* Start YYX at -YYN if negative to avoid negative indexes in + YYCHECK. In other words, skip the first -YYN actions for + this state because they are default actions. */ + int yyxbegin = yyn < 0 ? -yyn : 0; + /* Stay within bounds of both yycheck and yytname. */ + int yychecklim = yylast_ - yyn + 1; + int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_; + for (int yyx = yyxbegin; yyx < yyxend; ++yyx) + if (yycheck_[yyx + yyn] == yyx && yyx != yyterror_ + && !yy_table_value_is_error_ (yytable_[yyx + yyn])) + { + if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM) + { + yycount = 1; + break; + } + else + yyarg[yycount++] = yytname_[yyx]; + } + } + } + + char const* yyformat = YY_NULL; + switch (yycount) + { +#define YYCASE_(N, S) \ + case N: \ + yyformat = S; \ + break + YYCASE_(0, YY_("syntax error")); + YYCASE_(1, YY_("syntax error, unexpected %s")); + YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); + YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); + YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); + YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); +#undef YYCASE_ + } + + // Argument number. + size_t yyi = 0; + for (char const* yyp = yyformat; *yyp; ++yyp) + if (yyp[0] == '%' && yyp[1] == 's' && yyi < yycount) + { + yyres += yytnamerr_ (yyarg[yyi++]); + ++yyp; + } + else + yyres += *yyp; + return yyres; + } + + + /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing + STATE-NUM. */ + const signed char json_parser::yypact_ninf_ = -5; + const signed char + json_parser::yypact_[] = + { + 1, -5, -5, 3, 18, -5, -5, -5, -5, -5, + 8, -5, -5, -5, -5, -5, 2, 11, -5, -3, + -5, -5, 29, -5, 4, -5, 29, -5, 13, -5, + 29, -5 + }; + + /* YYDEFACT[S] -- default reduction number in state S. Performed when + YYTABLE doesn't specify something else to do. Zero means the + default is an error. */ + const unsigned char + json_parser::yydefact_[] = + { + 0, 5, 4, 0, 0, 15, 16, 17, 18, 14, + 0, 2, 19, 20, 3, 6, 0, 0, 10, 0, + 12, 1, 0, 7, 0, 11, 0, 8, 0, 13, + 0, 9 + }; + + /* YYPGOTO[NTERM-NUM]. */ + const signed char + json_parser::yypgoto_[] = + { + -5, -5, -5, -5, -5, -5, -5, -4 + }; + + /* YYDEFGOTO[NTERM-NUM]. */ + const signed char + json_parser::yydefgoto_[] = + { + -1, 10, 11, 12, 17, 13, 19, 14 + }; + + /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If + positive, shift that token. If negative, reduce the rule which + number is the opposite. If YYTABLE_NINF_, syntax error. */ + const signed char json_parser::yytable_ninf_ = -1; + const unsigned char + json_parser::yytable_[] = + { + 20, 1, 2, 25, 3, 26, 4, 15, 21, 22, + 5, 6, 7, 8, 9, 23, 16, 28, 27, 24, + 30, 3, 29, 4, 18, 0, 31, 5, 6, 7, + 8, 9, 3, 0, 4, 0, 0, 0, 5, 6, + 7, 8, 9 + }; + + /* YYCHECK. */ + const signed char + json_parser::yycheck_[] = + { + 4, 0, 1, 6, 3, 8, 5, 4, 0, 7, + 9, 10, 11, 12, 13, 4, 13, 13, 22, 8, + 7, 3, 26, 5, 6, -1, 30, 9, 10, 11, + 12, 13, 3, -1, 5, -1, -1, -1, 9, 10, + 11, 12, 13 + }; + + /* STOS_[STATE-NUM] -- The (internal number of the) accessing + symbol of state STATE-NUM. */ + const unsigned char + json_parser::yystos_[] = + { + 0, 0, 1, 3, 5, 9, 10, 11, 12, 13, + 16, 17, 18, 20, 22, 4, 13, 19, 6, 21, + 22, 0, 7, 4, 8, 6, 8, 22, 13, 22, + 7, 22 + }; + +#if YYDEBUG + /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding + to YYLEX-NUM. */ + const unsigned short int + json_parser::yytoken_number_[] = + { + 0, 256, 257, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12 + }; +#endif + + /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ + const unsigned char + json_parser::yyr1_[] = + { + 0, 15, 16, 17, 17, 17, 18, 18, 19, 19, + 20, 20, 21, 21, 22, 22, 22, 22, 22, 22, + 22 + }; + + /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */ + const unsigned char + json_parser::yyr2_[] = + { + 0, 2, 1, 1, 1, 1, 2, 3, 3, 5, + 2, 3, 1, 3, 1, 1, 1, 1, 1, 1, + 1 + }; + + + /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. + First, the terminals, then, starting at \a yyntokens_, nonterminals. */ + const char* + const json_parser::yytname_[] = + { + "\"end of file\"", "error", "$undefined", "\"{\"", "\"}\"", "\"[\"", + "\"]\"", "\":\"", "\",\"", "\"number\"", "\"true\"", "\"false\"", + "\"null\"", "\"string\"", "\"invalid\"", "$accept", "start", "data", + "object", "members", "array", "values", "value", YY_NULL + }; + +#if YYDEBUG + /* YYRHS -- A `-1'-separated list of the rules' RHS. */ + const json_parser::rhs_number_type + json_parser::yyrhs_[] = + { + 16, 0, -1, 17, -1, 22, -1, 1, -1, 0, + -1, 3, 4, -1, 3, 19, 4, -1, 13, 7, + 22, -1, 19, 8, 13, 7, 22, -1, 5, 6, + -1, 5, 21, 6, -1, 22, -1, 21, 8, 22, + -1, 13, -1, 9, -1, 10, -1, 11, -1, 12, + -1, 18, -1, 20, -1 + }; + + /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in + YYRHS. */ + const unsigned char + json_parser::yyprhs_[] = + { + 0, 0, 3, 5, 7, 9, 11, 14, 18, 22, + 28, 31, 35, 37, 41, 43, 45, 47, 49, 51, + 53 + }; + + /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ + const unsigned char + json_parser::yyrline_[] = + { + 0, 82, 82, 87, 88, 94, 96, 99, 105, 110, + 114, 117, 123, 128, 132, 133, 134, 135, 136, 137, + 138 + }; + + // Print the state stack on the debug stream. + void + json_parser::yystack_print_ () + { + *yycdebug_ << "Stack now"; + for (state_stack_type::const_iterator i = yystate_stack_.begin (); + i != yystate_stack_.end (); ++i) + *yycdebug_ << ' ' << *i; + *yycdebug_ << std::endl; + } + + // Report on the debug stream that the rule \a yyrule is going to be reduced. + void + json_parser::yy_reduce_print_ (int yyrule) + { + unsigned int yylno = yyrline_[yyrule]; + int yynrhs = yyr2_[yyrule]; + /* Print the symbols being reduced, and their result. */ + *yycdebug_ << "Reducing stack by rule " << yyrule - 1 + << " (line " << yylno << "):" << std::endl; + /* The symbols being reduced. */ + for (int yyi = 0; yyi < yynrhs; yyi++) + YY_SYMBOL_PRINT (" $" << yyi + 1 << " =", + yyrhs_[yyprhs_[yyrule] + yyi], + &(yysemantic_stack_[(yynrhs) - (yyi + 1)]), + &(yylocation_stack_[(yynrhs) - (yyi + 1)])); + } +#endif // YYDEBUG + + /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */ + json_parser::token_number_type + json_parser::yytranslate_ (int t) + { + static + const token_number_type + translate_table[] = + { + 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 1, 2 + }; + if ((unsigned int) t <= yyuser_token_number_max_) + return translate_table[t]; + else + return yyundef_token_; + } + + const int json_parser::yyeof_ = 0; + const int json_parser::yylast_ = 42; + const int json_parser::yynnts_ = 8; + const int json_parser::yyempty_ = -2; + const int json_parser::yyfinal_ = 21; + const int json_parser::yyterror_ = 1; + const int json_parser::yyerrcode_ = 256; + const int json_parser::yyntokens_ = 15; + + const unsigned int json_parser::yyuser_token_number_max_ = 257; + const json_parser::token_number_type json_parser::yyundef_token_ = 2; + + +} // yy +/* Line 1141 of lalr1.cc */ +#line 1079 "json_parser.cc" +/* Line 1142 of lalr1.cc */ +#line 140 "json_parser.yy" + + +int yy::yylex(YYSTYPE *yylval, yy::location *yylloc, QJson::ParserPrivate* driver) +{ + JSonScanner* scanner = driver->m_scanner; + yylval->clear(); + int ret = scanner->yylex(yylval, yylloc); + + qjsonDebug() << "json_parser::yylex - calling scanner yylval==|" + << yylval->toByteArray() << "|, ret==|" << QString::number(ret) << "|"; + + return ret; +} + +void yy::json_parser::error (const yy::location& yyloc, const std::string& error) +{ + /*qjsonDebug() << yyloc.begin.line; + qjsonDebug() << yyloc.begin.column; + qjsonDebug() << yyloc.end.line; + qjsonDebug() << yyloc.end.column;*/ + qjsonDebug() << "json_parser::error [line" << yyloc.end.line << "] -" << error.c_str() ; + driver->setError(QString::fromLatin1(error.c_str()), yyloc.end.line); +} diff --git a/src/http/qjson/src/json_parser.hh b/src/http/qjson/src/json_parser.hh new file mode 100644 index 000000000..25e00fa1b --- /dev/null +++ b/src/http/qjson/src/json_parser.hh @@ -0,0 +1,300 @@ +/* A Bison parser, made by GNU Bison 2.7. */ + +/* Skeleton interface for Bison LALR(1) parsers in C++ + + Copyright (C) 2002-2012 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/** + ** \file json_parser.hh + ** Define the yy::parser class. + */ + +/* C++ LALR(1) parser skeleton written by Akim Demaille. */ + +#ifndef YY_YY_JSON_PARSER_HH_INCLUDED +# define YY_YY_JSON_PARSER_HH_INCLUDED + +/* "%code requires" blocks. */ +/* Line 33 of lalr1.cc */ +#line 26 "json_parser.yy" + + #include "parser_p.h" + #include "json_scanner.h" + #include "qjson_debug.h" + + #include + #include + #include + #include + + #include + + class JSonScanner; + + namespace QJson { + class Parser; + } + + #define YYERROR_VERBOSE 1 + + Q_DECLARE_METATYPE(QVector*) + Q_DECLARE_METATYPE(QVariantMap*) + + +/* Line 33 of lalr1.cc */ +#line 72 "json_parser.hh" + + +#include +#include +#include "stack.hh" +#include "location.hh" + +/* Enabling traces. */ +#ifndef YYDEBUG +# define YYDEBUG 1 +#endif + + +namespace yy { +/* Line 33 of lalr1.cc */ +#line 88 "json_parser.hh" + + /// A Bison parser. + class json_parser + { + public: + /// Symbol semantic values. +#ifndef YYSTYPE + typedef int semantic_type; +#else + typedef YYSTYPE semantic_type; +#endif + /// Symbol locations. + typedef location location_type; + /// Tokens. + struct token + { + /* Tokens. */ + enum yytokentype { + END = 0, + CURLY_BRACKET_OPEN = 1, + CURLY_BRACKET_CLOSE = 2, + SQUARE_BRACKET_OPEN = 3, + SQUARE_BRACKET_CLOSE = 4, + COLON = 5, + COMMA = 6, + NUMBER = 7, + TRUE_VAL = 8, + FALSE_VAL = 9, + NULL_VAL = 10, + STRING = 11, + INVALID = 12 + }; + + }; + /// Token type. + typedef token::yytokentype token_type; + + /// Build a parser object. + json_parser (QJson::ParserPrivate* driver_yyarg); + virtual ~json_parser (); + + /// Parse. + /// \returns 0 iff parsing succeeded. + virtual int parse (); + +#if YYDEBUG + /// The current debugging stream. + std::ostream& debug_stream () const; + /// Set the current debugging stream. + void set_debug_stream (std::ostream &); + + /// Type for debugging levels. + typedef int debug_level_type; + /// The current debugging level. + debug_level_type debug_level () const; + /// Set the current debugging level. + void set_debug_level (debug_level_type l); +#endif + + private: + /// Report a syntax error. + /// \param loc where the syntax error is found. + /// \param msg a description of the syntax error. + virtual void error (const location_type& loc, const std::string& msg); + + /// Generate an error message. + /// \param state the state where the error occurred. + /// \param tok the lookahead token. + virtual std::string yysyntax_error_ (int yystate, int tok); + +#if YYDEBUG + /// \brief Report a symbol value on the debug stream. + /// \param yytype The token type. + /// \param yyvaluep Its semantic value. + /// \param yylocationp Its location. + virtual void yy_symbol_value_print_ (int yytype, + const semantic_type* yyvaluep, + const location_type* yylocationp); + /// \brief Report a symbol on the debug stream. + /// \param yytype The token type. + /// \param yyvaluep Its semantic value. + /// \param yylocationp Its location. + virtual void yy_symbol_print_ (int yytype, + const semantic_type* yyvaluep, + const location_type* yylocationp); +#endif + + + /// State numbers. + typedef int state_type; + /// State stack type. + typedef stack state_stack_type; + /// Semantic value stack type. + typedef stack semantic_stack_type; + /// location stack type. + typedef stack location_stack_type; + + /// The state stack. + state_stack_type yystate_stack_; + /// The semantic value stack. + semantic_stack_type yysemantic_stack_; + /// The location stack. + location_stack_type yylocation_stack_; + + /// Whether the given \c yypact_ value indicates a defaulted state. + /// \param yyvalue the value to check + static bool yy_pact_value_is_default_ (int yyvalue); + + /// Whether the given \c yytable_ value indicates a syntax error. + /// \param yyvalue the value to check + static bool yy_table_value_is_error_ (int yyvalue); + + /// Internal symbol numbers. + typedef unsigned char token_number_type; + /* Tables. */ + /// For a state, the index in \a yytable_ of its portion. + static const signed char yypact_[]; + static const signed char yypact_ninf_; + + /// For a state, default reduction number. + /// Unless\a yytable_ specifies something else to do. + /// Zero means the default is an error. + static const unsigned char yydefact_[]; + + static const signed char yypgoto_[]; + static const signed char yydefgoto_[]; + + /// What to do in a state. + /// \a yytable_[yypact_[s]]: what to do in state \a s. + /// - if positive, shift that token. + /// - if negative, reduce the rule which number is the opposite. + /// - if zero, do what YYDEFACT says. + static const unsigned char yytable_[]; + static const signed char yytable_ninf_; + + static const signed char yycheck_[]; + + /// For a state, its accessing symbol. + static const unsigned char yystos_[]; + + /// For a rule, its LHS. + static const unsigned char yyr1_[]; + /// For a rule, its RHS length. + static const unsigned char yyr2_[]; + + /// Convert the symbol name \a n to a form suitable for a diagnostic. + static std::string yytnamerr_ (const char *n); + + + /// For a symbol, its name in clear. + static const char* const yytname_[]; +#if YYDEBUG + /// A type to store symbol numbers and -1. + typedef signed char rhs_number_type; + /// A `-1'-separated list of the rules' RHS. + static const rhs_number_type yyrhs_[]; + /// For each rule, the index of the first RHS symbol in \a yyrhs_. + static const unsigned char yyprhs_[]; + /// For each rule, its source line number. + static const unsigned char yyrline_[]; + /// For each scanner token number, its symbol number. + static const unsigned short int yytoken_number_[]; + /// Report on the debug stream that the rule \a r is going to be reduced. + virtual void yy_reduce_print_ (int r); + /// Print the state stack on the debug stream. + virtual void yystack_print_ (); + + /* Debugging. */ + int yydebug_; + std::ostream* yycdebug_; +#endif + + /// Convert a scanner token number \a t to a symbol number. + token_number_type yytranslate_ (int t); + + /// \brief Reclaim the memory associated to a symbol. + /// \param yymsg Why this token is reclaimed. + /// If null, do not display the symbol, just free it. + /// \param yytype The symbol type. + /// \param yyvaluep Its semantic value. + /// \param yylocationp Its location. + inline void yydestruct_ (const char* yymsg, + int yytype, + semantic_type* yyvaluep, + location_type* yylocationp); + + /// Pop \a n symbols the three stacks. + inline void yypop_ (unsigned int n = 1); + + /* Constants. */ + static const int yyeof_; + /* LAST_ -- Last index in TABLE_. */ + static const int yylast_; + static const int yynnts_; + static const int yyempty_; + static const int yyfinal_; + static const int yyterror_; + static const int yyerrcode_; + static const int yyntokens_; + static const unsigned int yyuser_token_number_max_; + static const token_number_type yyundef_token_; + + /* User arguments. */ + QJson::ParserPrivate* driver; + }; + +} // yy +/* Line 33 of lalr1.cc */ +#line 297 "json_parser.hh" + + + +#endif /* !YY_YY_JSON_PARSER_HH_INCLUDED */ diff --git a/src/http/qjson/src/json_parser.yy b/src/http/qjson/src/json_parser.yy new file mode 100644 index 000000000..fa4656bab --- /dev/null +++ b/src/http/qjson/src/json_parser.yy @@ -0,0 +1,162 @@ +/* This file is part of QJSon + * + * Copyright (C) 2008 Flavio Castelli + * Copyright (C) 2013 Silvio Moioli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +%skeleton "lalr1.cc" +%defines +%define "parser_class_name" "json_parser" + +%code requires{ + #include "parser_p.h" + #include "json_scanner.h" + #include "qjson_debug.h" + + #include + #include + #include + #include + + #include + + class JSonScanner; + + namespace QJson { + class Parser; + } + + #define YYERROR_VERBOSE 1 + + Q_DECLARE_METATYPE(QVector*) + Q_DECLARE_METATYPE(QVariantMap*) +} + +%parse-param { QJson::ParserPrivate* driver } +%lex-param { QJson::ParserPrivate* driver } + +%locations + +%debug +%error-verbose + +%token END 0 "end of file" + +%token CURLY_BRACKET_OPEN 1 "{" +%token CURLY_BRACKET_CLOSE 2 "}" +%token SQUARE_BRACKET_OPEN 3 "[" +%token SQUARE_BRACKET_CLOSE 4 "]" +%token COLON 5 ":" +%token COMMA 6 "," + +%token NUMBER 7 "number" +%token TRUE_VAL 8 "true" +%token FALSE_VAL 9 "false" +%token NULL_VAL 10 "null" +%token STRING 11 "string" + +%token INVALID 12 "invalid" + +// define the initial token +%start start + +%% + +// grammar rules + +start: data { + driver->m_result = $1; + qjsonDebug() << "json_parser - parsing finished"; + }; + +data: value { $$ = $1; } + | error + { + qCritical()<< "json_parser - syntax error found, " + << "forcing abort, Line" << @$.begin.line << "Column" << @$.begin.column; + YYABORT; + } + | END; + +object: CURLY_BRACKET_OPEN CURLY_BRACKET_CLOSE { + $$ = QVariant(QVariantMap()); + } + | CURLY_BRACKET_OPEN members CURLY_BRACKET_CLOSE { + QVariantMap* map = $2.value(); + $$ = QVariant(*map); + delete map; + }; + +members: STRING COLON value { + QVariantMap* pair = new QVariantMap(); + pair->insert($1.toString(), $3); + $$.setValue(pair); + } + | members COMMA STRING COLON value { + $$.value()->insert($3.toString(), $5); + }; + +array: SQUARE_BRACKET_OPEN SQUARE_BRACKET_CLOSE { + $$ = QVariant(QVariantList()); + } + | SQUARE_BRACKET_OPEN values SQUARE_BRACKET_CLOSE { + QVector* list = $2.value* >(); + $$ = QVariant(list->toList()); + delete list; + }; + +values: value { + QVector* list = new QVector(1); + list->replace(0, $1); + $$.setValue(list); + } + | values COMMA value { + $$.value* >()->append($3); + }; + +value: STRING + | NUMBER + | TRUE_VAL + | FALSE_VAL + | NULL_VAL + | object + | array; + +%% + +int yy::yylex(YYSTYPE *yylval, yy::location *yylloc, QJson::ParserPrivate* driver) +{ + JSonScanner* scanner = driver->m_scanner; + yylval->clear(); + int ret = scanner->yylex(yylval, yylloc); + + qjsonDebug() << "json_parser::yylex - calling scanner yylval==|" + << yylval->toByteArray() << "|, ret==|" << QString::number(ret) << "|"; + + return ret; +} + +void yy::json_parser::error (const yy::location& yyloc, const std::string& error) +{ + /*qjsonDebug() << yyloc.begin.line; + qjsonDebug() << yyloc.begin.column; + qjsonDebug() << yyloc.end.line; + qjsonDebug() << yyloc.end.column;*/ + qjsonDebug() << "json_parser::error [line" << yyloc.end.line << "] -" << error.c_str() ; + driver->setError(QString::fromLatin1(error.c_str()), yyloc.end.line); +} diff --git a/src/http/qjson/src/json_scanner.cc b/src/http/qjson/src/json_scanner.cc new file mode 100644 index 000000000..d148b0cd6 --- /dev/null +++ b/src/http/qjson/src/json_scanner.cc @@ -0,0 +1,4513 @@ +#line 2 "json_scanner.cc" + +#line 4 "json_scanner.cc" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 5 +#define YY_FLEX_SUBMINOR_VERSION 37 +#if YY_FLEX_SUBMINOR_VERSION > 0 +#define FLEX_BETA +#endif + + /* The c++ scanner is a mess. The FlexLexer.h header file relies on the + * following macro. This is required in order to pass the c++-multiple-scanners + * test in the regression suite. We get reports that it breaks inheritance. + * We will address this in a future release of flex, or omit the C++ scanner + * altogether. + */ + #define yyFlexLexer yyFlexLexer + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +#define FLEXINT_H + +/* C99 systems have . Non-C99 systems may or may not. */ + +#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +#ifndef __STDC_LIMIT_MACROS +#define __STDC_LIMIT_MACROS 1 +#endif + +#include +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +#else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; + +/* Limits of integral types. */ +#ifndef INT8_MIN +#define INT8_MIN (-128) +#endif +#ifndef INT16_MIN +#define INT16_MIN (-32767-1) +#endif +#ifndef INT32_MIN +#define INT32_MIN (-2147483647-1) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127) +#endif +#ifndef INT16_MAX +#define INT16_MAX (32767) +#endif +#ifndef INT32_MAX +#define INT32_MAX (2147483647) +#endif +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef UINT16_MAX +#define UINT16_MAX (65535U) +#endif +#ifndef UINT32_MAX +#define UINT32_MAX (4294967295U) +#endif + +#endif /* ! C99 */ + +#endif /* ! FLEXINT_H */ + +/* begin standard C++ headers. */ +#include +#include +#include +#include +#include +/* end standard C++ headers. */ + +#ifdef __cplusplus + +/* The "const" storage-class-modifier is valid. */ +#define YY_USE_CONST + +#else /* ! __cplusplus */ + +/* C99 requires __STDC__ to be defined as 1. */ +#if defined (__STDC__) + +#define YY_USE_CONST + +#endif /* defined (__STDC__) */ +#endif /* ! __cplusplus */ + +#ifdef YY_USE_CONST +#define yyconst const +#else +#define yyconst +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an unsigned + * integer for use as an array index. If the signed char is negative, + * we want to instead treat it as an 8-bit unsigned char, hence the + * double cast. + */ +#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN (yy_start) = 1 + 2 * + +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START (((yy_start) - 1) / 2) +#define YYSTATE YY_START + +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) + +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart( yyin ) + +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +#define YY_BUF_SIZE 16384 +#endif + +/* The state buf must be large enough to hold one state per character in the main buffer. + */ +#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +#define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state *YY_BUFFER_STATE; +#endif + +#ifndef YY_TYPEDEF_YY_SIZE_T +#define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +extern yy_size_t yyleng; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + + #define YY_LESS_LINENO(n) + +/* Return all but the first "n" matched characters back to the input stream. */ +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + *yy_cp = (yy_hold_char); \ + YY_RESTORE_YY_MORE_OFFSET \ + (yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } \ + while ( 0 ) + +#define unput(c) yyunput( c, (yytext_ptr) ) + +#ifndef YY_STRUCT_YY_BUFFER_STATE +#define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state + { + + std::istream* yy_input_file; + + char *yy_ch_buf; /* input buffer */ + char *yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + yy_size_t yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + yy_size_t yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + +#define YY_BUFFER_NEW 0 +#define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +#define YY_BUFFER_EOF_PENDING 2 + + }; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + * + * Returns the top of the stack, or NULL. + */ +#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \ + ? (yy_buffer_stack)[(yy_buffer_stack_top)] \ + : NULL) + +/* Same as previous macro, but useful when we know that the buffer stack is not + * NULL or when we need an lvalue. For internal use only. + */ +#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] + +void *yyalloc (yy_size_t ); +void *yyrealloc (void *,yy_size_t ); +void yyfree (void * ); + +#define yy_new_buffer yy_create_buffer + +#define yy_set_interactive(is_interactive) \ + { \ + if ( ! YY_CURRENT_BUFFER ){ \ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer( yyin, YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ + } + +#define yy_set_bol(at_bol) \ + { \ + if ( ! YY_CURRENT_BUFFER ){\ + yyensure_buffer_stack (); \ + YY_CURRENT_BUFFER_LVALUE = \ + yy_create_buffer( yyin, YY_BUF_SIZE ); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ + } + +#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) + +#define YY_SKIP_YYWRAP + +typedef unsigned char YY_CHAR; + +#define yytext_ptr yytext + +#include + +int yyFlexLexer::yywrap() { return 1; } +int yyFlexLexer::yylex() + { + LexerError( "yyFlexLexer::yylex invoked but %option yyclass used" ); + return 0; + } + +#define YY_DECL int JSonScanner::yylex() +static yyconst flex_int16_t yy_nxt[][256] = + { + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0 + }, + + { + 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, + 12, 11, 11, 13, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 11, 10, 14, 10, 10, 10, 10, 10, + + 10, 10, 10, 10, 15, 16, 10, 10, 17, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 19, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 20, 10, 21, 10, 10, 10, 10, 10, 10, + 10, 10, 22, 10, 10, 10, 10, 10, 10, 10, + 23, 10, 10, 10, 10, 10, 24, 10, 10, 10, + 10, 10, 10, 25, 10, 26, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10 + }, + + { + 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, + 12, 11, 11, 13, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 11, 10, 14, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 15, 16, 10, 10, 17, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 19, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 20, 10, 21, 10, 10, 10, 10, 10, 10, + 10, 10, 22, 10, 10, 10, 10, 10, 10, 10, + 23, 10, 10, 10, 10, 10, 24, 10, 10, 10, + 10, 10, 10, 25, 10, 26, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10 + }, + + { + 9, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 29, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27 + }, + + { + 9, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 29, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27 + + }, + + { + 9, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, + 31, 31, 31, 31, 31, 31, 31, 31, 30, 30, + 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, + 31, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, + + 31, 31, 31, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30 + }, + + { + 9, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + + 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, + 31, 31, 31, 31, 31, 31, 31, 31, 30, 30, + 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, + 31, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, + 31, 31, 31, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + + 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, + 30, 30, 30, 30, 30, 30 + }, + + { + 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, + 12, 11, 11, 13, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 11, 10, 14, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 15, 32, 10, 10, 17, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 19, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 33, 10, 10, 10, 10, 34, 10, + + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 20, 10, 21, 10, 10, 10, 10, 10, 10, + 10, 10, 22, 10, 10, 33, 10, 10, 10, 10, + 35, 10, 10, 10, 10, 10, 24, 10, 10, 10, + 10, 10, 10, 25, 10, 26, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10 + }, + + { + 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, + 12, 11, 11, 13, 10, 10, 10, 10, 10, 10, + + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 11, 10, 14, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 15, 32, 10, 10, 17, 18, + 18, 18, 18, 18, 18, 18, 18, 18, 19, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 33, 10, 10, 10, 10, 34, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 20, 10, 21, 10, 10, 10, 10, 10, 10, + 10, 10, 22, 10, 10, 33, 10, 10, 10, 10, + 35, 10, 10, 10, 10, 10, 24, 10, 10, 10, + + 10, 10, 10, 25, 10, 26, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10 + }, + + { + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, + -9, -9, -9, -9, -9, -9 + + }, + + { + 9, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, + -10, -10, -10, -10, -10, -10 + }, + + { + 9, -11, -11, -11, -11, -11, -11, -11, -11, 36, + -11, 36, 36, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, 36, -11, -11, -11, -11, -11, -11, -11, + + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + + -11, -11, -11, -11, -11, -11, -11, -11, -11, -11, + -11, -11, -11, -11, -11, -11 + }, + + { + 9, -12, -12, -12, -12, -12, -12, -12, -12, -12, + 37, -12, -12, 37, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12, -12, -12, -12, -12, + -12, -12, -12, -12, -12, -12 + }, + + { + 9, -13, -13, -13, -13, -13, -13, -13, -13, -13, + 37, -13, -13, 37, -13, -13, -13, -13, -13, -13, + + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13, -13, -13, -13, -13, + -13, -13, -13, -13, -13, -13 + }, + + { + 9, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14, -14, -14, -14, -14, + -14, -14, -14, -14, -14, -14 + + }, + + { + 9, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15, -15, -15, -15, -15, + -15, -15, -15, -15, -15, -15 + }, + + { + 9, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + + -16, -16, -16, -16, -16, -16, -16, -16, 38, 39, + 39, 39, 39, 39, 39, 39, 39, 39, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + + -16, -16, -16, -16, -16, -16, -16, -16, -16, -16, + -16, -16, -16, -16, -16, -16 + }, + + { + 9, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, 40, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, 41, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, 41, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17, -17, -17, -17, -17, + -17, -17, -17, -17, -17, -17 + }, + + { + 9, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, 40, -18, 42, 42, + 42, 42, 42, 42, 42, 42, 42, 42, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, 41, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, 41, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18, -18, -18, -18, -18, + -18, -18, -18, -18, -18, -18 + }, + + { + 9, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19, -19, -19, -19, -19, + -19, -19, -19, -19, -19, -19 + + }, + + { + 9, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, + -20, -20, -20, -20, -20, -20 + }, + + { + 9, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + + -21, -21, -21, -21, -21, -21, -21, -21, -21, -21, + -21, -21, -21, -21, -21, -21 + }, + + { + 9, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, 43, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22, -22, -22, -22, -22, + -22, -22, -22, -22, -22, -22 + }, + + { + 9, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, 44, -23, -23, + + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23, -23, -23, -23, -23, + -23, -23, -23, -23, -23, -23 + }, + + { + 9, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, 45, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, + -24, -24, -24, -24, -24, -24 + + }, + + { + 9, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25, -25, -25, -25, -25, + -25, -25, -25, -25, -25, -25 + }, + + { + 9, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + + -26, -26, -26, -26, -26, -26, -26, -26, -26, -26, + -26, -26, -26, -26, -26, -26 + }, + + { + 9, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, -27, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, -27, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46 + }, + + { + 9, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28, -28, -28, -28, -28, + -28, -28, -28, -28, -28, -28 + }, + + { + 9, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, 47, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, 48, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, 49, -29, -29, -29, -29, -29, 50, -29, + -29, -29, 51, -29, -29, -29, -29, -29, -29, -29, + 52, -29, -29, -29, 53, -29, 54, 55, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29, -29, -29, -29, -29, + -29, -29, -29, -29, -29, -29 + + }, + + { + 9, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30, -30, -30, -30, -30, + -30, -30, -30, -30, -30, -30 + }, + + { + 9, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + + -31, -31, -31, -31, -31, -31, -31, -31, 56, 56, + 56, 56, 56, 56, 56, 56, 56, 56, -31, -31, + -31, -31, -31, -31, -31, 56, 56, 56, 56, 56, + 56, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, 56, 56, 56, + 56, 56, 56, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + + -31, -31, -31, -31, -31, -31, -31, -31, -31, -31, + -31, -31, -31, -31, -31, -31 + }, + + { + 9, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, 38, 39, + 39, 39, 39, 39, 39, 39, 39, 39, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, 57, -32, -32, -32, -32, -32, -32, + + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, 57, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, + -32, -32, -32, -32, -32, -32 + }, + + { + 9, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + 58, -33, -33, -33, -33, -33, -33, -33, -33, -33, + + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33, -33, -33, -33, -33, + -33, -33, -33, -33, -33, -33 + }, + + { + 9, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + + -34, -34, -34, -34, -34, 59, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, 59, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34, -34, -34, -34, -34, + -34, -34, -34, -34, -34, -34 + + }, + + { + 9, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, 59, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, 59, -35, -35, + + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, 44, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, + -35, -35, -35, -35, -35, -35 + }, + + { + 9, -36, -36, -36, -36, -36, -36, -36, -36, 36, + -36, 36, 36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, 36, -36, -36, -36, -36, -36, -36, -36, + + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + + -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, + -36, -36, -36, -36, -36, -36 + }, + + { + 9, -37, -37, -37, -37, -37, -37, -37, -37, -37, + 37, -37, -37, 37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, + -37, -37, -37, -37, -37, -37 + }, + + { + 9, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, 40, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, 41, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, 41, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38, -38, -38, -38, -38, + -38, -38, -38, -38, -38, -38 + }, + + { + 9, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, 40, -39, 60, 60, + 60, 60, 60, 60, 60, 60, 60, 60, -39, -39, + + -39, -39, -39, -39, -39, -39, -39, -39, -39, 41, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, 41, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39, -39, -39, -39, -39, + -39, -39, -39, -39, -39, -39 + + }, + + { + 9, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, 61, 61, + 61, 61, 61, 61, 61, 61, 61, 61, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40, -40, -40, -40, -40, + -40, -40, -40, -40, -40, -40 + }, + + { + 9, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + + -41, -41, -41, 62, -41, 62, -41, -41, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + + -41, -41, -41, -41, -41, -41, -41, -41, -41, -41, + -41, -41, -41, -41, -41, -41 + }, + + { + 9, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, 40, -42, 42, 42, + 42, 42, 42, 42, 42, 42, 42, 42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, 41, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, 41, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42, -42, -42, -42, -42, + -42, -42, -42, -42, -42, -42 + }, + + { + 9, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, 64, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43, -43, -43, -43, -43, + -43, -43, -43, -43, -43, -43 + }, + + { + 9, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, 65, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44, -44, -44, -44, -44, + -44, -44, -44, -44, -44, -44 + + }, + + { + 9, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, 66, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, + -45, -45, -45, -45, -45, -45 + }, + + { + 9, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, -46, 46, 46, 46, 46, 46, + + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, -46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + + 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, + 46, 46, 46, 46, 46, 46 + }, + + { + 9, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47, -47, -47, -47, -47, + -47, -47, -47, -47, -47, -47 + }, + + { + 9, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48, -48, -48, -48, -48, + -48, -48, -48, -48, -48, -48 + }, + + { + 9, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49, -49, -49, -49, -49, + -49, -49, -49, -49, -49, -49 + + }, + + { + 9, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50, -50, -50, -50, -50, + -50, -50, -50, -50, -50, -50 + }, + + { + 9, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + + -51, -51, -51, -51, -51, -51, -51, -51, -51, -51, + -51, -51, -51, -51, -51, -51 + }, + + { + 9, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52, -52, -52, -52, -52, + -52, -52, -52, -52, -52, -52 + }, + + { + 9, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, + -53, -53, -53, -53, -53, -53 + }, + + { + 9, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54, -54, -54, -54, -54, + -54, -54, -54, -54, -54, -54 + + }, + + { + 9, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55, -55, -55, -55, -55, + -55, -55, -55, -55, -55, -55 + }, + + { + 9, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + + -56, -56, -56, -56, -56, -56, -56, -56, 67, 67, + 67, 67, 67, 67, 67, 67, 67, 67, -56, -56, + -56, -56, -56, -56, -56, 67, 67, 67, 67, 67, + 67, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, 67, 67, 67, + 67, 67, 67, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + + -56, -56, -56, -56, -56, -56, -56, -56, -56, -56, + -56, -56, -56, -56, -56, -56 + }, + + { + 9, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + 68, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57, -57, -57, -57, -57, + -57, -57, -57, -57, -57, -57 + }, + + { + 9, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, 69, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58, -58, -58, -58, -58, + -58, -58, -58, -58, -58, -58 + }, + + { + 9, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, 70, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + 70, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59, -59, -59, -59, -59, + -59, -59, -59, -59, -59, -59 + + }, + + { + 9, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, 40, -60, 60, 60, + 60, 60, 60, 60, 60, 60, 60, 60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, 41, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + + -60, 41, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60, -60, -60, -60, -60, + -60, -60, -60, -60, -60, -60 + }, + + { + 9, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + + -61, -61, -61, -61, -61, -61, -61, -61, 61, 61, + 61, 61, 61, 61, 61, 61, 61, 61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, 41, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, 41, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + + -61, -61, -61, -61, -61, -61, -61, -61, -61, -61, + -61, -61, -61, -61, -61, -61 + }, + + { + 9, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62, -62, -62, -62, -62, + -62, -62, -62, -62, -62, -62 + }, + + { + 9, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63, -63, -63, -63, -63, + -63, -63, -63, -63, -63, -63 + }, + + { + 9, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, 71, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64, -64, -64, -64, -64, + -64, -64, -64, -64, -64, -64 + + }, + + { + 9, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + + -65, -65, -65, -65, -65, -65, -65, -65, 72, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65, -65, -65, -65, -65, + -65, -65, -65, -65, -65, -65 + }, + + { + 9, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, 73, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + + -66, -66, -66, -66, -66, -66, -66, -66, -66, -66, + -66, -66, -66, -66, -66, -66 + }, + + { + 9, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, 74, 74, + 74, 74, 74, 74, 74, 74, 74, 74, -67, -67, + -67, -67, -67, -67, -67, 74, 74, 74, 74, 74, + 74, -67, -67, -67, -67, -67, -67, -67, -67, -67, + + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, 74, 74, 74, + 74, 74, 74, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67, -67, -67, -67, -67, + -67, -67, -67, -67, -67, -67 + }, + + { + 9, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, 75, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, + -68, -68, -68, -68, -68, -68 + }, + + { + 9, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, 76, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69, -69, -69, -69, -69, + -69, -69, -69, -69, -69, -69 + + }, + + { + 9, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, + -70, -70, -70, -70, -70, -70 + }, + + { + 9, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, 77, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + + -71, -71, -71, -71, -71, -71, -71, -71, -71, -71, + -71, -71, -71, -71, -71, -71 + }, + + { + 9, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72, -72, -72, -72, -72, + -72, -72, -72, -72, -72, -72 + }, + + { + 9, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73, -73, -73, -73, -73, + -73, -73, -73, -73, -73, -73 + }, + + { + 9, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74, -74, -74, -74, -74, + -74, -74, -74, -74, -74, -74 + + }, + + { + 9, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + + -75, -75, -75, -75, -75, 78, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75, -75, -75, -75, -75, + -75, -75, -75, -75, -75, -75 + }, + + { + 9, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + 79, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + + -76, -76, -76, -76, -76, -76, -76, -76, -76, -76, + -76, -76, -76, -76, -76, -76 + }, + + { + 9, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77, -77, -77, -77, -77, + -77, -77, -77, -77, -77, -77 + }, + + { + 9, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + 80, -78, -78, -78, -78, -78, -78, -78, -78, -78, + + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78, -78, -78, -78, -78, + -78, -78, -78, -78, -78, -78 + }, + + { + 9, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, 81, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79, -79, -79, -79, -79, + -79, -79, -79, -79, -79, -79 + + }, + + { + 9, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + + -80, -80, -80, -80, -80, 82, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, + -80, -80, -80, -80, -80, -80 + }, + + { + 9, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, 83, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + + -81, -81, -81, -81, -81, -81, -81, -81, -81, -81, + -81, -81, -81, -81, -81, -81 + }, + + { + 9, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, 84, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82, -82, -82, -82, -82, + -82, -82, -82, -82, -82, -82 + }, + + { + 9, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + + -83, 85, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83, -83, -83, -83, -83, + -83, -83, -83, -83, -83, -83 + }, + + { + 9, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, 86, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84, -84, -84, -84, -84, + -84, -84, -84, -84, -84, -84 + + }, + + { + 9, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85, -85, -85, -85, -85, + -85, -85, -85, -85, -85, -85 + }, + + { + 9, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + + -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, + -86, -86, -86, -86, -86, -86 + }, + + } ; + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + (yytext_ptr) = yy_bp; \ + yyleng = (size_t) (yy_cp - yy_bp); \ + (yy_hold_char) = *yy_cp; \ + *yy_cp = '\0'; \ + (yy_c_buf_p) = yy_cp; + +#define YY_NUM_RULES 36 +#define YY_END_OF_BUFFER 37 +/* This struct is not used in this scanner, + but its presence is necessary. */ +struct yy_trans_info + { + flex_int32_t yy_verify; + flex_int32_t yy_nxt; + }; +static yyconst flex_int16_t yy_accept[87] = + { 0, + 0, 0, 0, 0, 0, 0, 0, 0, 37, 35, + 1, 2, 2, 11, 27, 35, 6, 6, 26, 28, + 29, 35, 35, 35, 30, 31, 21, 23, 22, 25, + 25, 35, 35, 35, 35, 1, 2, 8, 8, 0, + 0, 7, 0, 0, 0, 21, 12, 14, 13, 15, + 16, 17, 18, 19, 20, 0, 0, 0, 0, 9, + 10, 0, 10, 0, 0, 0, 0, 0, 0, 32, + 0, 5, 3, 24, 0, 0, 4, 0, 0, 0, + 0, 0, 0, 0, 33, 34 + } ; + +static yyconst yy_state_type yy_NUL_trans[87] = + { 0, + 10, 10, 27, 27, 30, 30, 10, 10, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 46, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 46, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0 + } ; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +#line 1 "json_scanner.yy" +/* This file is part of QJson + * + * Copyright (C) 2013 Silvio Moioli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110yy::json_parser::token::INVALID301, USA. + */ +/* Flex output settings */ +#define YY_NO_UNISTD_H 1 +#define YY_NO_INPUT 1 +#line 29 "json_scanner.yy" + #include "json_scanner.h" + #include "json_parser.hh" + + #if defined(_WIN32) && !defined(__MINGW32__) + #define strtoll _strtoi64 + #define strtoull _strtoui64 + #endif + + #define YY_USER_INIT if(m_allowSpecialNumbers) { \ + BEGIN(ALLOW_SPECIAL_NUMBERS); \ + } +/* Exclusive subscanners for strings and escaped hex sequences */ + +/* Extra-JSON rules active iff m_allowSpecialNumbers is true */ + +#line 3168 "json_scanner.cc" + +#define INITIAL 0 +#define QUOTMARK_OPEN 1 +#define HEX_OPEN 2 +#define ALLOW_SPECIAL_NUMBERS 3 + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +#include +#endif + +#ifndef YY_EXTRA_TYPE +#define YY_EXTRA_TYPE void * +#endif + +#ifndef yytext_ptr +static void yy_flex_strncpy (char *,yyconst char *,int ); +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * ); +#endif + +#ifndef YY_NO_INPUT + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +#define YY_READ_BUF_SIZE 8192 +#endif + +/* Copy whatever the last rule matched to the standard output. */ +#ifndef ECHO +#define ECHO LexerOutput( yytext, yyleng ) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +#define YY_INPUT(buf,result,max_size) \ +\ + if ( (result = LexerInput( (char *) buf, max_size )) < 0 ) \ + YY_FATAL_ERROR( "input in flex scanner failed" ); + +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +#define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +#define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +#define YY_FATAL_ERROR(msg) LexerError( msg ) +#endif + +/* end tables serialization structures and prototypes */ + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +#define YY_DECL_IS_OURS 1 +#define YY_DECL int yyFlexLexer::yylex() +#endif /* !YY_DECL */ + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +#define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +#define YY_BREAK break; +#endif + +#define YY_RULE_SETUP \ + YY_USER_ACTION + +/** The main scanner function which does all the work. + */ +YY_DECL +{ + register yy_state_type yy_current_state; + register char *yy_cp, *yy_bp; + register int yy_act; + +#line 48 "json_scanner.yy" + + + /* Whitespace */ +#line 3275 "json_scanner.cc" + + if ( !(yy_init) ) + { + (yy_init) = 1; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if ( ! (yy_start) ) + (yy_start) = 1; /* first start state */ + + if ( ! yyin ) + yyin = & std::cin; + + if ( ! yyout ) + yyout = & std::cout; + + if ( ! YY_CURRENT_BUFFER ) { + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer( yyin, YY_BUF_SIZE ); + } + + yy_load_buffer_state( ); + } + + while ( 1 ) /* loops until end-of-file is reached */ + { + yy_cp = (yy_c_buf_p); + + /* Support of yytext. */ + *yy_cp = (yy_hold_char); + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = (yy_start); +yy_match: + while ( (yy_current_state = yy_nxt[yy_current_state][ YY_SC_TO_UI(*yy_cp) ]) > 0 ) + { + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + + ++yy_cp; + } + + yy_current_state = -yy_current_state; + +yy_find_action: + yy_act = yy_accept[yy_current_state]; + + YY_DO_BEFORE_ACTION; + +do_action: /* This label is used only to access EOF actions. */ + + switch ( yy_act ) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = (yy_hold_char); + yy_cp = (yy_last_accepting_cpos) + 1; + yy_current_state = (yy_last_accepting_state); + goto yy_find_action; + +case 1: +YY_RULE_SETUP +#line 51 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + } + YY_BREAK +case 2: +/* rule 2 can match eol */ +YY_RULE_SETUP +#line 55 "json_scanner.yy" +{ + m_yylloc->lines(yyleng); + } + YY_BREAK +/* Special values */ +case 3: +YY_RULE_SETUP +#line 61 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(true); + return yy::json_parser::token::TRUE_VAL; + } + YY_BREAK +case 4: +YY_RULE_SETUP +#line 67 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(false); + return yy::json_parser::token::FALSE_VAL; + } + YY_BREAK +case 5: +YY_RULE_SETUP +#line 73 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(); + return yy::json_parser::token::NULL_VAL; + } + YY_BREAK +/* Numbers */ +case 6: +#line 82 "json_scanner.yy" +case 7: +YY_RULE_SETUP +#line 82 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(strtoull(yytext, NULL, 10)); + if (errno == ERANGE) { + qCritical() << "Number is out of range: " << yytext; + return yy::json_parser::token::INVALID; + } + return yy::json_parser::token::NUMBER; + } + YY_BREAK +case 8: +#line 93 "json_scanner.yy" +case 9: +YY_RULE_SETUP +#line 93 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(strtoll(yytext, NULL, 10)); + if (errno == ERANGE) { + qCritical() << "Number is out of range: " << yytext; + return yy::json_parser::token::INVALID; + } + return yy::json_parser::token::NUMBER; + } + YY_BREAK +case 10: +YY_RULE_SETUP +#line 103 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + bool ok; + *m_yylval = QVariant(m_C_locale.toDouble(QLatin1String(yytext),&ok)); + if (!ok) { + qCritical() << "Number is out of range: " << yytext; + return yy::json_parser::token::INVALID; + } + return yy::json_parser::token::NUMBER; + } + YY_BREAK +/* Strings */ +case 11: +YY_RULE_SETUP +#line 115 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + BEGIN(QUOTMARK_OPEN); + } + YY_BREAK + +case 12: +YY_RULE_SETUP +#line 121 "json_scanner.yy" +{ + m_currentString.append(QLatin1String("\"")); + } + YY_BREAK +case 13: +YY_RULE_SETUP +#line 125 "json_scanner.yy" +{ + m_currentString.append(QLatin1String("\\")); + } + YY_BREAK +case 14: +YY_RULE_SETUP +#line 129 "json_scanner.yy" +{ + m_currentString.append(QLatin1String("/")); + } + YY_BREAK +case 15: +YY_RULE_SETUP +#line 133 "json_scanner.yy" +{ + m_currentString.append(QLatin1String("\b")); + } + YY_BREAK +case 16: +YY_RULE_SETUP +#line 137 "json_scanner.yy" +{ + m_currentString.append(QLatin1String("\f")); + } + YY_BREAK +case 17: +YY_RULE_SETUP +#line 141 "json_scanner.yy" +{ + m_currentString.append(QLatin1String("\n")); + } + YY_BREAK +case 18: +YY_RULE_SETUP +#line 145 "json_scanner.yy" +{ + m_currentString.append(QLatin1String("\r")); + } + YY_BREAK +case 19: +YY_RULE_SETUP +#line 149 "json_scanner.yy" +{ + m_currentString.append(QLatin1String("\t")); + } + YY_BREAK +case 20: +YY_RULE_SETUP +#line 153 "json_scanner.yy" +{ + BEGIN(HEX_OPEN); + } + YY_BREAK +case 21: +/* rule 21 can match eol */ +YY_RULE_SETUP +#line 157 "json_scanner.yy" +{ + m_currentString.append(QString::fromUtf8(yytext)); + } + YY_BREAK +case 22: +YY_RULE_SETUP +#line 161 "json_scanner.yy" +{ + // ignore + } + YY_BREAK +case 23: +YY_RULE_SETUP +#line 165 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(m_currentString); + m_currentString.clear(); + BEGIN(INITIAL); + return yy::json_parser::token::STRING; + } + YY_BREAK + + +case 24: +YY_RULE_SETUP +#line 175 "json_scanner.yy" +{ + QString hexDigits = QString::fromUtf8(yytext, yyleng); + bool ok; + ushort hexDigit1 = hexDigits.left(2).toShort(&ok, 16); + ushort hexDigit2 = hexDigits.right(2).toShort(&ok, 16); + m_currentString.append(QChar(hexDigit2, hexDigit1)); + BEGIN(QUOTMARK_OPEN); + } + YY_BREAK +case 25: +/* rule 25 can match eol */ +YY_RULE_SETUP +#line 184 "json_scanner.yy" +{ + qCritical() << "Invalid hex string"; + m_yylloc->columns(yyleng); + *m_yylval = QVariant(QLatin1String("")); + BEGIN(QUOTMARK_OPEN); + return yy::json_parser::token::INVALID; + } + YY_BREAK + +/* "Compound type" related tokens */ +case 26: +YY_RULE_SETUP +#line 196 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + return yy::json_parser::token::COLON; + } + YY_BREAK +case 27: +YY_RULE_SETUP +#line 201 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + return yy::json_parser::token::COMMA; + } + YY_BREAK +case 28: +YY_RULE_SETUP +#line 206 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + return yy::json_parser::token::SQUARE_BRACKET_OPEN; + } + YY_BREAK +case 29: +YY_RULE_SETUP +#line 211 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + return yy::json_parser::token::SQUARE_BRACKET_CLOSE; + } + YY_BREAK +case 30: +YY_RULE_SETUP +#line 216 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + return yy::json_parser::token::CURLY_BRACKET_OPEN; + } + YY_BREAK +case 31: +YY_RULE_SETUP +#line 221 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + return yy::json_parser::token::CURLY_BRACKET_CLOSE; + } + YY_BREAK +/* Extra-JSON numbers */ + +case 32: +YY_RULE_SETUP +#line 229 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(std::numeric_limits::quiet_NaN()); + return yy::json_parser::token::NUMBER; + } + YY_BREAK +case 33: +YY_RULE_SETUP +#line 235 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(std::numeric_limits::infinity()); + return yy::json_parser::token::NUMBER; + } + YY_BREAK +case 34: +YY_RULE_SETUP +#line 241 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + *m_yylval = QVariant(-std::numeric_limits::infinity()); + return yy::json_parser::token::NUMBER; + } + YY_BREAK + +/* If all else fails */ +case 35: +YY_RULE_SETUP +#line 249 "json_scanner.yy" +{ + m_yylloc->columns(yyleng); + return yy::json_parser::token::INVALID; + } + YY_BREAK +case YY_STATE_EOF(INITIAL): +case YY_STATE_EOF(QUOTMARK_OPEN): +case YY_STATE_EOF(HEX_OPEN): +case YY_STATE_EOF(ALLOW_SPECIAL_NUMBERS): +#line 254 "json_scanner.yy" +return yy::json_parser::token::END; + YY_BREAK +case 36: +YY_RULE_SETUP +#line 255 "json_scanner.yy" +ECHO; + YY_BREAK +#line 3660 "json_scanner.cc" + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = (yy_hold_char); + YY_RESTORE_YY_MORE_OFFSET + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW ) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between YY_CURRENT_BUFFER and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans( yy_current_state ); + + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + + if ( yy_next_state ) + { + /* Consume the NUL. */ + yy_cp = ++(yy_c_buf_p); + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = (yy_c_buf_p); + goto yy_find_action; + } + } + + else switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_END_OF_FILE: + { + (yy_did_buffer_switch_on_eof) = 0; + + if ( yywrap( ) ) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = + (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + (yy_c_buf_p) = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; + + yy_current_state = yy_get_previous_state( ); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: + YY_FATAL_ERROR( + "fatal flex scanner internal error--no action found" ); + } /* end of action switch */ + } /* end of scanning one token */ +} /* end of yylex */ + +/* The contents of this function are C++ specific, so the () macro is not used. + */ +yyFlexLexer::yyFlexLexer( std::istream* arg_yyin, std::ostream* arg_yyout ) +{ + yyin = arg_yyin; + yyout = arg_yyout; + yy_c_buf_p = 0; + yy_init = 0; + yy_start = 0; + yy_flex_debug = 0; + yylineno = 1; // this will only get updated if %option yylineno + + yy_did_buffer_switch_on_eof = 0; + + yy_looking_for_trail_begin = 0; + yy_more_flag = 0; + yy_more_len = 0; + yy_more_offset = yy_prev_more_offset = 0; + + yy_start_stack_ptr = yy_start_stack_depth = 0; + yy_start_stack = NULL; + + yy_buffer_stack = 0; + yy_buffer_stack_top = 0; + yy_buffer_stack_max = 0; + + yy_state_buf = 0; + +} + +/* The contents of this function are C++ specific, so the () macro is not used. + */ +yyFlexLexer::~yyFlexLexer() +{ + delete [] yy_state_buf; + yyfree(yy_start_stack ); + yy_delete_buffer( YY_CURRENT_BUFFER ); + yyfree(yy_buffer_stack ); +} + +/* The contents of this function are C++ specific, so the () macro is not used. + */ +void yyFlexLexer::switch_streams( std::istream* new_in, std::ostream* new_out ) +{ + if ( new_in ) + { + yy_delete_buffer( YY_CURRENT_BUFFER ); + yy_switch_to_buffer( yy_create_buffer( new_in, YY_BUF_SIZE ) ); + } + + if ( new_out ) + yyout = new_out; +} + +#ifdef YY_INTERACTIVE +int yyFlexLexer::LexerInput( char* buf, int /* max_size */ ) +#else +int yyFlexLexer::LexerInput( char* buf, int max_size ) +#endif +{ + if ( yyin->eof() || yyin->fail() ) + return 0; + +#ifdef YY_INTERACTIVE + yyin->get( buf[0] ); + + if ( yyin->eof() ) + return 0; + + if ( yyin->bad() ) + return -1; + + return 1; + +#else + (void) yyin->read( buf, max_size ); + + if ( yyin->bad() ) + return -1; + else + return yyin->gcount(); +#endif +} + +void yyFlexLexer::LexerOutput( const char* buf, int size ) +{ + (void) yyout->write( buf, size ); +} + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ +int yyFlexLexer::yy_get_next_buffer() +{ + register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; + register char *source = (yytext_ptr); + register int number_to_move, i; + int ret_val; + + if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] ) + YY_FATAL_ERROR( + "fatal flex scanner internal error--end of buffer missed" ); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 ) + { /* Don't try to fill the buffer, so this is an EOF. */ + if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 ) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1; + + for ( i = 0; i < number_to_move; ++i ) + *(dest++) = *(source++); + + if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING ) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; + + else + { + yy_size_t num_to_read = + YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + + while ( num_to_read <= 0 ) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; + + int yy_c_buf_p_offset = + (int) ((yy_c_buf_p) - b->yy_ch_buf); + + if ( b->yy_is_our_buffer ) + { + yy_size_t new_size = b->yy_buf_size * 2; + + if ( new_size <= 0 ) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char *) + /* Include room in for 2 EOB chars. */ + yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2 ); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = 0; + + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( + "fatal error - scanner input buffer overflow" ); + + (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - + number_to_move - 1; + + } + + if ( num_to_read > YY_READ_BUF_SIZE ) + num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), + (yy_n_chars), num_to_read ); + + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + if ( (yy_n_chars) == 0 ) + { + if ( number_to_move == YY_MORE_ADJ ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart( yyin ); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = + YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + if ((yy_size_t) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) { + /* Extend the array by 50%, plus the number we really need. */ + yy_size_t new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size ); + if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" ); + } + + (yy_n_chars) += number_to_move; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; + + (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; + + return ret_val; +} + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + + yy_state_type yyFlexLexer::yy_get_previous_state() +{ + register yy_state_type yy_current_state; + register char *yy_cp; + + yy_current_state = (yy_start); + + for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp ) + { + if ( *yy_cp ) + { + yy_current_state = yy_nxt[yy_current_state][YY_SC_TO_UI(*yy_cp)]; + } + else + yy_current_state = yy_NUL_trans[yy_current_state]; + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + } + + return yy_current_state; +} + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ + yy_state_type yyFlexLexer::yy_try_NUL_trans( yy_state_type yy_current_state ) +{ + register int yy_is_jam; + register char *yy_cp = (yy_c_buf_p); + + yy_current_state = yy_NUL_trans[yy_current_state]; + yy_is_jam = (yy_current_state == 0); + + if ( ! yy_is_jam ) + { + if ( yy_accept[yy_current_state] ) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + } + + return yy_is_jam ? 0 : yy_current_state; +} + + void yyFlexLexer::yyunput( int c, register char* yy_bp) +{ + register char *yy_cp; + + yy_cp = (yy_c_buf_p); + + /* undo effects of setting up yytext */ + *yy_cp = (yy_hold_char); + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + { /* need to shift things up to make room */ + /* +2 for EOB chars. */ + register yy_size_t number_to_move = (yy_n_chars) + 2; + register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2]; + register char *source = + &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]; + + while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf ) + *--dest = *--source; + + yy_cp += (int) (dest - source); + yy_bp += (int) (dest - source); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size; + + if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 ) + YY_FATAL_ERROR( "flex scanner push-back overflow" ); + } + + *--yy_cp = (char) c; + + (yytext_ptr) = yy_bp; + (yy_hold_char) = *yy_cp; + (yy_c_buf_p) = yy_cp; +} + + int yyFlexLexer::yyinput() +{ + int c; + + *(yy_c_buf_p) = (yy_hold_char); + + if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR ) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] ) + /* This was really a NUL. */ + *(yy_c_buf_p) = '\0'; + + else + { /* need more input */ + yy_size_t offset = (yy_c_buf_p) - (yytext_ptr); + ++(yy_c_buf_p); + + switch ( yy_get_next_buffer( ) ) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart( yyin ); + + /*FALLTHROUGH*/ + + case EOB_ACT_END_OF_FILE: + { + if ( yywrap( ) ) + return EOF; + + if ( ! (yy_did_buffer_switch_on_eof) ) + YY_NEW_FILE; +#ifdef __cplusplus + return yyinput(); +#else + return input(); +#endif + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = (yytext_ptr) + offset; + break; + } + } + } + + c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ + *(yy_c_buf_p) = '\0'; /* preserve yytext */ + (yy_hold_char) = *++(yy_c_buf_p); + + return c; +} + +/** Immediately switch to a different input stream. + * @param input_file A readable stream. + * + * @note This function does not reset the start condition to @c INITIAL . + */ + void yyFlexLexer::yyrestart( std::istream* input_file ) +{ + + if ( ! YY_CURRENT_BUFFER ){ + yyensure_buffer_stack (); + YY_CURRENT_BUFFER_LVALUE = + yy_create_buffer( yyin, YY_BUF_SIZE ); + } + + yy_init_buffer( YY_CURRENT_BUFFER, input_file ); + yy_load_buffer_state( ); +} + +/** Switch to a different input buffer. + * @param new_buffer The new input buffer. + * + */ + void yyFlexLexer::yy_switch_to_buffer( YY_BUFFER_STATE new_buffer ) +{ + + /* TODO. We should be able to replace this entire function body + * with + * yypop_buffer_state(); + * yypush_buffer_state(new_buffer); + */ + yyensure_buffer_stack (); + if ( YY_CURRENT_BUFFER == new_buffer ) + return; + + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + YY_CURRENT_BUFFER_LVALUE = new_buffer; + yy_load_buffer_state( ); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + (yy_did_buffer_switch_on_eof) = 1; +} + + void yyFlexLexer::yy_load_buffer_state() +{ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; + yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; + (yy_hold_char) = *(yy_c_buf_p); +} + +/** Allocate and initialize an input buffer state. + * @param file A readable stream. + * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. + * + * @return the allocated buffer state. + */ + YY_BUFFER_STATE yyFlexLexer::yy_create_buffer( std::istream* file, int size ) +{ + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state ) ); + if ( ! b ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_buf_size = size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2 ); + if ( ! b->yy_ch_buf ) + YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" ); + + b->yy_is_our_buffer = 1; + + yy_init_buffer( b, file ); + + return b; +} + +/** Destroy the buffer. + * @param b a buffer created with yy_create_buffer() + * + */ + void yyFlexLexer::yy_delete_buffer( YY_BUFFER_STATE b ) +{ + + if ( ! b ) + return; + + if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */ + YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; + + if ( b->yy_is_our_buffer ) + yyfree((void *) b->yy_ch_buf ); + + yyfree((void *) b ); +} + +/* Initializes or reinitializes a buffer. + * This function is sometimes called more than once on the same buffer, + * such as during a yyrestart() or at EOF. + */ + void yyFlexLexer::yy_init_buffer( YY_BUFFER_STATE b, std::istream* file ) + +{ + int oerrno = errno; + + yy_flush_buffer( b ); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + + /* If b is the current buffer, then yy_init_buffer was _probably_ + * called from yyrestart() or through yy_get_next_buffer. + * In that case, we don't want to reset the lineno or column. + */ + if (b != YY_CURRENT_BUFFER){ + b->yy_bs_lineno = 1; + b->yy_bs_column = 0; + } + + b->yy_is_interactive = 0; + errno = oerrno; +} + +/** Discard all buffered characters. On the next scan, YY_INPUT will be called. + * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. + * + */ + void yyFlexLexer::yy_flush_buffer( YY_BUFFER_STATE b ) +{ + if ( ! b ) + return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if ( b == YY_CURRENT_BUFFER ) + yy_load_buffer_state( ); +} + +/** Pushes the new state onto the stack. The new state becomes + * the current state. This function will allocate the stack + * if necessary. + * @param new_buffer The new state. + * + */ +void yyFlexLexer::yypush_buffer_state (YY_BUFFER_STATE new_buffer) +{ + if (new_buffer == NULL) + return; + + yyensure_buffer_stack(); + + /* This block is copied from yy_switch_to_buffer. */ + if ( YY_CURRENT_BUFFER ) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + /* Only push if top exists. Otherwise, replace top. */ + if (YY_CURRENT_BUFFER) + (yy_buffer_stack_top)++; + YY_CURRENT_BUFFER_LVALUE = new_buffer; + + /* copied from yy_switch_to_buffer. */ + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; +} + +/** Removes and deletes the top of the stack, if present. + * The next element becomes the new top. + * + */ +void yyFlexLexer::yypop_buffer_state (void) +{ + if (!YY_CURRENT_BUFFER) + return; + + yy_delete_buffer(YY_CURRENT_BUFFER ); + YY_CURRENT_BUFFER_LVALUE = NULL; + if ((yy_buffer_stack_top) > 0) + --(yy_buffer_stack_top); + + if (YY_CURRENT_BUFFER) { + yy_load_buffer_state( ); + (yy_did_buffer_switch_on_eof) = 1; + } +} + +/* Allocates the stack if it does not exist. + * Guarantees space for at least one push. + */ +void yyFlexLexer::yyensure_buffer_stack(void) +{ + yy_size_t num_to_alloc; + + if (!(yy_buffer_stack)) { + + /* First allocation is just for 2 elements, since we don't know if this + * scanner will even need a stack. We use 2 instead of 1 to avoid an + * immediate realloc on the next call. + */ + num_to_alloc = 1; + (yy_buffer_stack) = (struct yy_buffer_state**)yyalloc + (num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); + + (yy_buffer_stack_max) = num_to_alloc; + (yy_buffer_stack_top) = 0; + return; + } + + if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){ + + /* Increase the buffer to prepare for a possible push. */ + int grow_size = 8 /* arbitrary grow size */; + + num_to_alloc = (yy_buffer_stack_max) + grow_size; + (yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc + ((yy_buffer_stack), + num_to_alloc * sizeof(struct yy_buffer_state*) + ); + if ( ! (yy_buffer_stack) ) + YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" ); + + /* zero only the new slots.*/ + memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*)); + (yy_buffer_stack_max) = num_to_alloc; + } +} + +#ifndef YY_EXIT_FAILURE +#define YY_EXIT_FAILURE 2 +#endif + +void yyFlexLexer::LexerError( yyconst char msg[] ) +{ + std::cerr << msg << std::endl; + exit( YY_EXIT_FAILURE ); +} + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg);\ + yytext[yyleng] = (yy_hold_char); \ + (yy_c_buf_p) = yytext + yyless_macro_arg; \ + (yy_hold_char) = *(yy_c_buf_p); \ + *(yy_c_buf_p) = '\0'; \ + yyleng = yyless_macro_arg; \ + } \ + while ( 0 ) + +/* Accessor methods (get/set functions) to struct members. */ + +/* + * Internal utility routines. + */ + +#ifndef yytext_ptr +static void yy_flex_strncpy (char* s1, yyconst char * s2, int n ) +{ + register int i; + for ( i = 0; i < n; ++i ) + s1[i] = s2[i]; +} +#endif + +#ifdef YY_NEED_STRLEN +static int yy_flex_strlen (yyconst char * s ) +{ + register int n; + for ( n = 0; s[n]; ++n ) + ; + + return n; +} +#endif + +void *yyalloc (yy_size_t size ) +{ + return (void *) malloc( size ); +} + +void *yyrealloc (void * ptr, yy_size_t size ) +{ + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return (void *) realloc( (char *) ptr, size ); +} + +void yyfree (void * ptr ) +{ + free( (char *) ptr ); /* see yyrealloc() for (char *) cast */ +} + +#define YYTABLES_NAME "yytables" + +#line 255 "json_scanner.yy" diff --git a/src/http/qjson/src/json_scanner.cpp b/src/http/qjson/src/json_scanner.cpp new file mode 100644 index 000000000..eb4ec8357 --- /dev/null +++ b/src/http/qjson/src/json_scanner.cpp @@ -0,0 +1,82 @@ +/* This file is part of QJson + * + * Copyright (C) 2008 Flavio Castelli + * Copyright (C) 2013 Silvio Moioli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#include "json_scanner.cc" + +#include "qjson_debug.h" +#include "json_scanner.h" +#include "json_parser.hh" + +#include + +#include +#include + +#include + + +JSonScanner::JSonScanner(QIODevice* io) + : m_allowSpecialNumbers(false), + m_io (io), + m_criticalError(false), + m_C_locale(QLocale::C) +{ + +} + +JSonScanner::~JSonScanner() +{ +} + +void JSonScanner::allowSpecialNumbers(bool allow) { + m_allowSpecialNumbers = allow; +} + +int JSonScanner::yylex(YYSTYPE* yylval, yy::location *yylloc) { + m_yylval = yylval; + m_yylloc = yylloc; + m_yylloc->step(); + int result = yylex(); + + if (m_criticalError) { + return -1; + } + + return result; +} + +int JSonScanner::LexerInput(char* buf, int max_size) { + if (!m_io->isOpen()) { + qCritical() << "JSonScanner::yylex - io device is not open"; + m_criticalError = true; + return 0; + } + + int readBytes = m_io->read(buf, max_size); + if(readBytes < 0) { + qCritical() << "JSonScanner::yylex - error while reading from io device"; + m_criticalError = true; + return 0; + } + + return readBytes; +} + + diff --git a/src/http/qjson/src/json_scanner.h b/src/http/qjson/src/json_scanner.h new file mode 100644 index 000000000..6a0e97b6f --- /dev/null +++ b/src/http/qjson/src/json_scanner.h @@ -0,0 +1,66 @@ +/* This file is part of QJson + * + * Copyright (C) 2008 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef _JSON_SCANNER +#define _JSON_SCANNER + +#include +#include +#include + +#define YYSTYPE QVariant + +// Only include FlexLexer.h if it hasn't been already included +#if ! defined(yyFlexLexerOnce) +#include +#endif + +#include "parser_p.h" + + + +namespace yy { + class location; + int yylex(YYSTYPE *yylval, yy::location *yylloc, QJson::ParserPrivate* driver); +} + +class JSonScanner : public yyFlexLexer +{ + public: + explicit JSonScanner(QIODevice* io); + ~JSonScanner(); + + void allowSpecialNumbers(bool allow); + + int yylex(YYSTYPE* yylval, yy::location *yylloc); + int yylex(); + int LexerInput(char* buf, int max_size); + protected: + bool m_allowSpecialNumbers; + QIODevice* m_io; + + YYSTYPE* m_yylval; + yy::location* m_yylloc; + bool m_criticalError; + QString m_currentString; + QLocale m_C_locale; +}; + +#endif diff --git a/src/http/qjson/src/json_scanner.yy b/src/http/qjson/src/json_scanner.yy new file mode 100644 index 000000000..27104b28e --- /dev/null +++ b/src/http/qjson/src/json_scanner.yy @@ -0,0 +1,254 @@ +/* This file is part of QJson + * + * Copyright (C) 2013 Silvio Moioli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110yy::json_parser::token::INVALID301, USA. + */ + +/* Flex output settings */ +%option 8bit c++ full warn +%option noyywrap nounistd +%option noinput nounput noyy_push_state noyy_pop_state noyy_top_state noyy_scan_buffer noyy_scan_bytes noyy_scan_string noyyget_extra noyyset_extra noyyget_leng noyyget_text noyyget_lineno noyyset_lineno noyyget_in noyyset_in noyyget_out noyyset_out noyyget_lval noyyset_lval noyyget_lloc noyyset_lloc noyyget_debug noyyset_debug +%option yyclass="JSonScanner" +%option outfile="json_scanner.cc" + +%{ + #include "json_scanner.h" + #include "json_parser.hh" + + #if defined(_WIN32) && !defined(__MINGW32__) + #define strtoll _strtoi64 + #define strtoull _strtoui64 + #endif + + #define YY_USER_INIT if(m_allowSpecialNumbers) { \ + BEGIN(ALLOW_SPECIAL_NUMBERS); \ + } +%} + +/* Exclusive subscanners for strings and escaped hex sequences */ +%x QUOTMARK_OPEN HEX_OPEN + +/* Extra-JSON rules active iff m_allowSpecialNumbers is true */ +%s ALLOW_SPECIAL_NUMBERS + +%% + + /* Whitespace */ +[\v\f\t ]+ { + m_yylloc->columns(yyleng); + } + +[\r\n]+ { + m_yylloc->lines(yyleng); + } + + + /* Special values */ +true { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(true); + return yy::json_parser::token::TRUE_VAL; + } + +false { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(false); + return yy::json_parser::token::FALSE_VAL; + } + +null { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(); + return yy::json_parser::token::NULL_VAL; + } + + + /* Numbers */ +[0-9] | +[1-9][0-9]+ { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(strtoull(yytext, NULL, 10)); + if (errno == ERANGE) { + qCritical() << "Number is out of range: " << yytext; + return yy::json_parser::token::INVALID; + } + return yy::json_parser::token::NUMBER; + } + +-[0-9] | +-[1-9][0-9]+ { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(strtoll(yytext, NULL, 10)); + if (errno == ERANGE) { + qCritical() << "Number is out of range: " << yytext; + return yy::json_parser::token::INVALID; + } + return yy::json_parser::token::NUMBER; + } + +-?(([0-9])|([1-9][0-9]+))(\.[0-9]+)?([Ee][+\-]?[0-9]+)? { + m_yylloc->columns(yyleng); + bool ok; + *m_yylval = QVariant(m_C_locale.toDouble(QLatin1String(yytext),&ok)); + if (!ok) { + qCritical() << "Number is out of range: " << yytext; + return yy::json_parser::token::INVALID; + } + return yy::json_parser::token::NUMBER; + } + + /* Strings */ +\" { + m_yylloc->columns(yyleng); + BEGIN(QUOTMARK_OPEN); + } + +{ + \\\" { + m_currentString.append(QLatin1String("\"")); + } + + \\\\ { + m_currentString.append(QLatin1String("\\")); + } + + \\\/ { + m_currentString.append(QLatin1String("/")); + } + + \\b { + m_currentString.append(QLatin1String("\b")); + } + + \\f { + m_currentString.append(QLatin1String("\f")); + } + + \\n { + m_currentString.append(QLatin1String("\n")); + } + + \\r { + m_currentString.append(QLatin1String("\r")); + } + + \\t { + m_currentString.append(QLatin1String("\t")); + } + + \\u { + BEGIN(HEX_OPEN); + } + + [^\"\\]+ { + m_currentString.append(QString::fromUtf8(yytext)); + } + + \\ { + // ignore + } + + \" { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(m_currentString); + m_currentString.clear(); + BEGIN(INITIAL); + return yy::json_parser::token::STRING; + } +} + +{ + [0-9A-Fa-f]{4} { + QString hexDigits = QString::fromUtf8(yytext, yyleng); + bool ok; + ushort hexDigit1 = hexDigits.left(2).toShort(&ok, 16); + ushort hexDigit2 = hexDigits.right(2).toShort(&ok, 16); + m_currentString.append(QChar(hexDigit2, hexDigit1)); + BEGIN(QUOTMARK_OPEN); + } + + .|\n { + qCritical() << "Invalid hex string"; + m_yylloc->columns(yyleng); + *m_yylval = QVariant(QLatin1String("")); + BEGIN(QUOTMARK_OPEN); + return yy::json_parser::token::INVALID; + } +} + + + + /* "Compound type" related tokens */ +: { + m_yylloc->columns(yyleng); + return yy::json_parser::token::COLON; + } + +, { + m_yylloc->columns(yyleng); + return yy::json_parser::token::COMMA; + } + +\[ { + m_yylloc->columns(yyleng); + return yy::json_parser::token::SQUARE_BRACKET_OPEN; + } + +\] { + m_yylloc->columns(yyleng); + return yy::json_parser::token::SQUARE_BRACKET_CLOSE; + } + +\{ { + m_yylloc->columns(yyleng); + return yy::json_parser::token::CURLY_BRACKET_OPEN; + } + +\} { + m_yylloc->columns(yyleng); + return yy::json_parser::token::CURLY_BRACKET_CLOSE; + } + + + /* Extra-JSON numbers */ +{ + (?i:nan) { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(std::numeric_limits::quiet_NaN()); + return yy::json_parser::token::NUMBER; + } + + [Ii]nfinity { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(std::numeric_limits::infinity()); + return yy::json_parser::token::NUMBER; + } + + -[Ii]nfinity { + m_yylloc->columns(yyleng); + *m_yylval = QVariant(-std::numeric_limits::infinity()); + return yy::json_parser::token::NUMBER; + } +} + + /* If all else fails */ +. { + m_yylloc->columns(yyleng); + return yy::json_parser::token::INVALID; + } + +<> return yy::json_parser::token::END; diff --git a/src/http/qjson/src/location.hh b/src/http/qjson/src/location.hh new file mode 100644 index 000000000..0bf1a74e8 --- /dev/null +++ b/src/http/qjson/src/location.hh @@ -0,0 +1,181 @@ +/* A Bison parser, made by GNU Bison 2.7. */ + +/* Locations for Bison parsers in C++ + + Copyright (C) 2002-2007, 2009-2012 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/** + ** \file location.hh + ** Define the yy::location class. + */ + +#ifndef YY_YY_LOCATION_HH_INCLUDED +# define YY_YY_LOCATION_HH_INCLUDED + +# include "position.hh" + + +namespace yy { +/* Line 166 of location.cc */ +#line 47 "location.hh" + + /// Abstract a location. + class location + { + public: + + /// Construct a location from \a b to \a e. + location (const position& b, const position& e) + : begin (b) + , end (e) + { + } + + /// Construct a 0-width location in \a p. + explicit location (const position& p = position ()) + : begin (p) + , end (p) + { + } + + /// Construct a 0-width location in \a f, \a l, \a c. + explicit location (std::string* f, + unsigned int l = 1u, + unsigned int c = 1u) + : begin (f, l, c) + , end (f, l, c) + { + } + + + /// Initialization. + void initialize (std::string* f = YY_NULL, + unsigned int l = 1u, + unsigned int c = 1u) + { + begin.initialize (f, l, c); + end = begin; + } + + /** \name Line and Column related manipulators + ** \{ */ + public: + /// Reset initial location to final location. + void step () + { + begin = end; + } + + /// Extend the current location to the COUNT next columns. + void columns (unsigned int count = 1) + { + end += count; + } + + /// Extend the current location to the COUNT next lines. + void lines (unsigned int count = 1) + { + end.lines (count); + } + /** \} */ + + + public: + /// Beginning of the located region. + position begin; + /// End of the located region. + position end; + }; + + /// Join two location objects to create a location. + inline const location operator+ (const location& begin, const location& end) + { + location res = begin; + res.end = end.end; + return res; + } + + /// Add two location objects. + inline const location operator+ (const location& begin, unsigned int width) + { + location res = begin; + res.columns (width); + return res; + } + + /// Add and assign a location. + inline location& operator+= (location& res, unsigned int width) + { + res.columns (width); + return res; + } + + /// Compare two location objects. + inline bool + operator== (const location& loc1, const location& loc2) + { + return loc1.begin == loc2.begin && loc1.end == loc2.end; + } + + /// Compare two location objects. + inline bool + operator!= (const location& loc1, const location& loc2) + { + return !(loc1 == loc2); + } + + /** \brief Intercept output stream redirection. + ** \param ostr the destination output stream + ** \param loc a reference to the location to redirect + ** + ** Avoid duplicate information. + */ + template + inline std::basic_ostream& + operator<< (std::basic_ostream& ostr, const location& loc) + { + position last = loc.end - 1; + ostr << loc.begin; + if (last.filename + && (!loc.begin.filename + || *loc.begin.filename != *last.filename)) + ostr << '-' << last; + else if (loc.begin.line != last.line) + ostr << '-' << last.line << '.' << last.column; + else if (loc.begin.column != last.column) + ostr << '-' << last.column; + return ostr; + } + + +} // yy +/* Line 296 of location.cc */ +#line 180 "location.hh" + +#endif /* !YY_YY_LOCATION_HH_INCLUDED */ diff --git a/src/http/qjson/src/parser.cpp b/src/http/qjson/src/parser.cpp new file mode 100644 index 000000000..73de8ceae --- /dev/null +++ b/src/http/qjson/src/parser.cpp @@ -0,0 +1,141 @@ +/* This file is part of QJson + * + * Copyright (C) 2008 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "parser.h" +#include "parser_p.h" +#include "json_parser.hh" +#include "json_scanner.h" + +#include +#include +#include +#include + +using namespace QJson; + +ParserPrivate::ParserPrivate() : + m_scanner(0) +{ + m_specialNumbersAllowed = false; + reset(); +} + +ParserPrivate::~ParserPrivate() +{ + if (m_scanner) + delete m_scanner; +} + +void ParserPrivate::setError(QString errorMsg, int errorLine) { + m_error = true; + m_errorMsg = errorMsg; + m_errorLine = errorLine; +} + +void ParserPrivate::reset() +{ + m_error = false; + m_errorLine = 0; + m_errorMsg.clear(); + if (m_scanner) { + delete m_scanner; + m_scanner = 0; + } +} + +Parser::Parser() : + d(new ParserPrivate) +{ +} + +Parser::~Parser() +{ + delete d; +} + +QVariant Parser::parse (QIODevice* io, bool* ok) +{ + d->reset(); + + if (!io->isOpen()) { + if (!io->open(QIODevice::ReadOnly)) { + if (ok != 0) + *ok = false; + qCritical ("Error opening device"); + return QVariant(); + } + } + + if (!io->isReadable()) { + if (ok != 0) + *ok = false; + qCritical ("Device is not readable"); + io->close(); + return QVariant(); + } + + if (io->atEnd()) { + if (ok != 0) + *ok = false; + d->setError(QLatin1String("No data"), 0); + io->close(); + return QVariant(); + } + + d->m_scanner = new JSonScanner (io); + d->m_scanner->allowSpecialNumbers(d->m_specialNumbersAllowed); + yy::json_parser parser(d); + parser.parse(); + + delete d->m_scanner; + d->m_scanner = 0; + + if (ok != 0) + *ok = !d->m_error; + + io->close(); + return d->m_result; +} + +QVariant Parser::parse(const QByteArray& jsonString, bool* ok) { + QBuffer buffer; + buffer.open(QBuffer::ReadWrite); + buffer.write(jsonString); + buffer.seek(0); + return parse (&buffer, ok); +} + +QString Parser::errorString() const +{ + return d->m_errorMsg; +} + +int Parser::errorLine() const +{ + return d->m_errorLine; +} + +void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers) { + d->m_specialNumbersAllowed = allowSpecialNumbers; +} + +bool Parser::specialNumbersAllowed() const { + return d->m_specialNumbersAllowed; +} diff --git a/src/http/qjson/src/parser.h b/src/http/qjson/src/parser.h new file mode 100644 index 000000000..c3132f506 --- /dev/null +++ b/src/http/qjson/src/parser.h @@ -0,0 +1,99 @@ +/* This file is part of QJson + * + * Copyright (C) 2008 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef QJSON_PARSER_H +#define QJSON_PARSER_H + +#include "qjson_export.h" + +QT_BEGIN_NAMESPACE +class QIODevice; +class QVariant; +QT_END_NAMESPACE + +/** + * Namespace used by QJson + */ +namespace QJson { + + class ParserPrivate; + + /** + * @brief Main class used to convert JSON data to QVariant objects + */ + class QJSON_EXPORT Parser + { + public: + Parser(); + ~Parser(); + + /** + * Read JSON string from the I/O Device and converts it to a QVariant object + * @param io Input output device + * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true. + * @returns a QVariant object generated from the JSON string + */ + QVariant parse(QIODevice* io, bool* ok = 0); + + /** + * This is a method provided for convenience. + * @param jsonData data containing the JSON object representation + * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true. + * @returns a QVariant object generated from the JSON string + * @sa errorString + * @sa errorLine + */ + QVariant parse(const QByteArray& jsonData, bool* ok = 0); + + /** + * This method returns the error message + * @returns a QString object containing the error message of the last parse operation + * @sa errorLine + */ + QString errorString() const; + + /** + * This method returns line number where the error occurred + * @returns the line number where the error occurred + * @sa errorString + */ + int errorLine() const; + + /** + * Sets whether special numbers (Infinity, -Infinity, NaN) are allowed as an extension to + * the standard + * @param allowSpecialNumbers new value of whether special numbers are allowed + * @sa specialNumbersAllowed + */ + void allowSpecialNumbers(bool allowSpecialNumbers); + + /** + * @returns whether special numbers (Infinity, -Infinity, NaN) are allowed + * @sa allowSpecialNumbers + */ + bool specialNumbersAllowed() const; + + private: + Q_DISABLE_COPY(Parser) + ParserPrivate* const d; + }; +} + +#endif // QJSON_PARSER_H diff --git a/src/http/qjson/src/parser_p.h b/src/http/qjson/src/parser_p.h new file mode 100644 index 000000000..de98b9b6b --- /dev/null +++ b/src/http/qjson/src/parser_p.h @@ -0,0 +1,57 @@ +/* This file is part of QJson + * + * Copyright (C) 2008 Flavio Castelli + * Copyright (C) 2009 Michael Leupold + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef QJSON_PARSER_P_H +#define QJSON_PARSER_P_H + +#include "parser.h" + +#include +#include + +class JSonScanner; + +namespace yy { + class json_parser; +} + +namespace QJson { + + class ParserPrivate + { + public: + ParserPrivate(); + ~ParserPrivate(); + + void reset(); + + void setError(QString errorMsg, int line); + + JSonScanner* m_scanner; + bool m_error; + int m_errorLine; + QString m_errorMsg; + QVariant m_result; + bool m_specialNumbersAllowed; + }; +} + +#endif // QJSON_PARSER_H diff --git a/src/http/qjson/src/parserrunnable.cpp b/src/http/qjson/src/parserrunnable.cpp new file mode 100644 index 000000000..5eb92883b --- /dev/null +++ b/src/http/qjson/src/parserrunnable.cpp @@ -0,0 +1,68 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "parserrunnable.h" + +#include "parser.h" + +#include +#include + +using namespace QJson; + +class QJson::ParserRunnable::Private +{ + public: + QByteArray m_data; +}; + +ParserRunnable::ParserRunnable(QObject* parent) + : QObject(parent), + QRunnable(), + d(new Private) +{ + qRegisterMetaType("QVariant"); +} + +ParserRunnable::~ParserRunnable() +{ + delete d; +} + +void ParserRunnable::setData( const QByteArray& data ) { + d->m_data = data; +} + +void ParserRunnable::run() +{ + qDebug() << Q_FUNC_INFO; + + bool ok; + Parser parser; + QVariant result = parser.parse (d->m_data, &ok); + if (ok) { + qDebug() << "successfully converted json item to QVariant object"; + Q_EMIT parsingFinished(result, true, QString()); + } else { + const QString errorText = tr("An error occurred while parsing json: %1").arg(parser.errorString()); + qCritical() << errorText; + Q_EMIT parsingFinished(QVariant(), false, errorText); + } +} diff --git a/src/http/qjson/src/parserrunnable.h b/src/http/qjson/src/parserrunnable.h new file mode 100644 index 000000000..fddcacd3f --- /dev/null +++ b/src/http/qjson/src/parserrunnable.h @@ -0,0 +1,64 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef PARSERRUNNABLE_H +#define PARSERRUNNABLE_H + +#include "qjson_export.h" + +#include +#include + +QT_BEGIN_NAMESPACE +class QVariant; +QT_END_NAMESPACE + +namespace QJson { + /** + * @brief Convenience class for converting JSON data to QVariant objects using a dedicated thread + */ + class QJSON_EXPORT ParserRunnable : public QObject, public QRunnable + { + Q_OBJECT + public: + explicit ParserRunnable(QObject* parent = 0); + ~ParserRunnable(); + + void setData( const QByteArray& data ); + + void run(); + + Q_SIGNALS: + /** + * This signal is emitted when the parsing process has been completed + * @param json contains the result of the parsing + * @param ok if a parsing error occurs ok is set to false, otherwise it's set to true. + * @param error_msg contains a string explaining the failure reason + **/ + void parsingFinished(const QVariant& json, bool ok, const QString& error_msg); + + private: + Q_DISABLE_COPY(ParserRunnable) + class Private; + Private* const d; + }; +} + +#endif // PARSERRUNNABLE_H diff --git a/src/http/qjson/src/position.hh b/src/http/qjson/src/position.hh new file mode 100644 index 000000000..3b33c2734 --- /dev/null +++ b/src/http/qjson/src/position.hh @@ -0,0 +1,172 @@ +/* A Bison parser, made by GNU Bison 2.7. */ + +/* Positions for Bison parsers in C++ + + Copyright (C) 2002-2007, 2009-2012 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/** + ** \file position.hh + ** Define the yy::position class. + */ + +#ifndef YY_YY_POSITION_HH_INCLUDED +# define YY_YY_POSITION_HH_INCLUDED + +# include // std::max +# include +# include + +# ifndef YY_NULL +# if defined __cplusplus && 201103L <= __cplusplus +# define YY_NULL nullptr +# else +# define YY_NULL 0 +# endif +# endif + + +namespace yy { +/* Line 36 of location.cc */ +#line 57 "position.hh" + /// Abstract a position. + class position + { + public: + + /// Construct a position. + explicit position (std::string* f = YY_NULL, + unsigned int l = 1u, + unsigned int c = 1u) + : filename (f) + , line (l) + , column (c) + { + } + + + /// Initialization. + void initialize (std::string* fn = YY_NULL, + unsigned int l = 1u, + unsigned int c = 1u) + { + filename = fn; + line = l; + column = c; + } + + /** \name Line and Column related manipulators + ** \{ */ + /// (line related) Advance to the COUNT next lines. + void lines (int count = 1) + { + column = 1u; + line += count; + } + + /// (column related) Advance to the COUNT next columns. + void columns (int count = 1) + { + column = std::max (1u, column + count); + } + /** \} */ + + /// File name to which this position refers. + std::string* filename; + /// Current line number. + unsigned int line; + /// Current column number. + unsigned int column; + }; + + /// Add and assign a position. + inline position& + operator+= (position& res, const int width) + { + res.columns (width); + return res; + } + + /// Add two position objects. + inline const position + operator+ (const position& begin, const int width) + { + position res = begin; + return res += width; + } + + /// Add and assign a position. + inline position& + operator-= (position& res, const int width) + { + return res += -width; + } + + /// Add two position objects. + inline const position + operator- (const position& begin, const int width) + { + return begin + -width; + } + + /// Compare two position objects. + inline bool + operator== (const position& pos1, const position& pos2) + { + return (pos1.line == pos2.line + && pos1.column == pos2.column + && (pos1.filename == pos2.filename + || (pos1.filename && pos2.filename + && *pos1.filename == *pos2.filename))); + } + + /// Compare two position objects. + inline bool + operator!= (const position& pos1, const position& pos2) + { + return !(pos1 == pos2); + } + + /** \brief Intercept output stream redirection. + ** \param ostr the destination output stream + ** \param pos a reference to the position to redirect + */ + template + inline std::basic_ostream& + operator<< (std::basic_ostream& ostr, const position& pos) + { + if (pos.filename) + ostr << *pos.filename << ':'; + return ostr << pos.line << '.' << pos.column; + } + + +} // yy +/* Line 148 of location.cc */ +#line 172 "position.hh" +#endif /* !YY_YY_POSITION_HH_INCLUDED */ diff --git a/src/http/qjson/src/qjson_debug.h b/src/http/qjson/src/qjson_debug.h new file mode 100644 index 000000000..6036b2268 --- /dev/null +++ b/src/http/qjson/src/qjson_debug.h @@ -0,0 +1,34 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Michael Leupold + * Copyright (C) 2013 Silvio Moioli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef QJSON_DEBUG_H +#define QJSON_DEBUG_H + +#include + +// define qjsonDebug() +#ifdef QJSON_VERBOSE_DEBUG_OUTPUT + inline QDebug qjsonDebug() { return QDebug(QtDebugMsg); } +#else + #define qjsonDebug() if(false) QDebug(QtDebugMsg) +#endif + +#endif diff --git a/src/http/qjson/src/qjson_export.h b/src/http/qjson/src/qjson_export.h new file mode 100644 index 000000000..9a807b91b --- /dev/null +++ b/src/http/qjson/src/qjson_export.h @@ -0,0 +1,35 @@ +/* This file is part of the KDE project + Copyright (C) 2009 Pino Toscano + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1, as published by the Free Software Foundation. + + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef QJSON_EXPORT_H +#define QJSON_EXPORT_H + +#include + +#ifndef QJSON_EXPORT +# if defined(QJSON_MAKEDLL) + /* We are building this library */ +# define QJSON_EXPORT Q_DECL_EXPORT +# else + /* We are using this library */ +# define QJSON_EXPORT Q_DECL_IMPORT +# endif +#endif + +#endif diff --git a/src/http/qjson/src/qobjecthelper.cpp b/src/http/qjson/src/qobjecthelper.cpp new file mode 100644 index 000000000..0ea440522 --- /dev/null +++ b/src/http/qjson/src/qobjecthelper.cpp @@ -0,0 +1,94 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Till Adam + * Copyright (C) 2009 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#include "qobjecthelper.h" + +#include +#include +#include + +using namespace QJson; + +class QObjectHelper::QObjectHelperPrivate { +}; + +QObjectHelper::QObjectHelper() + : d (new QObjectHelperPrivate) +{ +} + +QObjectHelper::~QObjectHelper() +{ + delete d; +} + +QVariantMap QObjectHelper::qobject2qvariant(const QObject* object, Flags flags, + const QStringList& ignoredProperties) +{ + QVariantMap result; + const QMetaObject *metaobject = object->metaObject(); + int count = metaobject->propertyCount(); + for (int i=0; iproperty(i); + const char *name = metaproperty.name(); + + if (ignoredProperties.contains(QLatin1String(name)) || (!metaproperty.isReadable())) + continue; + + QVariant value = object->property(name); + if (value.isNull() && !flags.testFlag(Flag_StoreNullVariants)) + continue; + if (!value.isValid() && !flags.testFlag(Flag_StoreInvalidVariants)) + continue; + result[QLatin1String(name)] = value; + } + return result; +} + +QVariantMap QObjectHelper::qobject2qvariant(const QObject *object, const QStringList &ignoredProperties) + { + return qobject2qvariant(object, Flag_All, ignoredProperties); + } + +void QObjectHelper::qvariant2qobject(const QVariantMap& variant, QObject* object) +{ + const QMetaObject *metaobject = object->metaObject(); + + QVariantMap::const_iterator iter; + for (iter = variant.constBegin(); iter != variant.constEnd(); ++iter) { + int pIdx = metaobject->indexOfProperty( iter.key().toLatin1() ); + + if ( pIdx < 0 ) { + continue; + } + + QMetaProperty metaproperty = metaobject->property( pIdx ); + QVariant::Type type = metaproperty.type(); + QVariant v( iter.value() ); + if ( v.canConvert( type ) ) { + v.convert( type ); + metaproperty.write( object, v ); + } else if (QString(QLatin1String("QVariant")).compare(QLatin1String(metaproperty.typeName())) == 0) { + metaproperty.write( object, v ); + } + } +} diff --git a/src/http/qjson/src/qobjecthelper.h b/src/http/qjson/src/qobjecthelper.h new file mode 100644 index 000000000..9a819b448 --- /dev/null +++ b/src/http/qjson/src/qobjecthelper.h @@ -0,0 +1,157 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef QOBJECTHELPER_H +#define QOBJECTHELPER_H + +#include "qjson_export.h" + +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QObject; +QT_END_NAMESPACE + +namespace QJson { + /** + * @brief Class used to convert QObject into QVariant and vivce-versa. + * During these operations only the class attributes defined as properties will + * be considered. + * Properties marked as 'non-stored' will be ignored. + * + * Suppose the declaration of the Person class looks like this: + * \code + * class Person : public QObject + { + Q_OBJECT + + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber) + Q_PROPERTY(Gender gender READ gender WRITE setGender) + Q_PROPERTY(QDate dob READ dob WRITE setDob) + Q_ENUMS(Gender) + + public: + Person(QObject* parent = 0); + ~Person(); + + QString name() const; + void setName(const QString& name); + + int phoneNumber() const; + void setPhoneNumber(const int phoneNumber); + + enum Gender {Male, Female}; + void setGender(Gender gender); + Gender gender() const; + + QDate dob() const; + void setDob(const QDate& dob); + + private: + QString m_name; + int m_phoneNumber; + Gender m_gender; + QDate m_dob; + }; + \endcode + + The following code will serialize an instance of Person to JSON : + + \code + Person person; + person.setName("Flavio"); + person.setPhoneNumber(123456); + person.setGender(Person::Male); + person.setDob(QDate(1982, 7, 12)); + + QVariantMap variant = QObjectHelper::qobject2qvariant(&person); + Serializer serializer; + qDebug() << serializer.serialize( variant); + \endcode + + The generated output will be: + \code + { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 } + \endcode + + It's also possible to initialize a QObject using the values stored inside of + a QVariantMap. + + Suppose you have the following JSON data stored into a QString: + \code + { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 } + \endcode + + The following code will initialize an already allocated instance of Person + using the JSON values: + \code + Parser parser; + QVariant variant = parser.parse(json); + + Person person; + QObjectHelper::qvariant2qobject(variant.toMap(), &person); + \endcode + + \sa Parser + \sa Serializer + */ + class QJSON_EXPORT QObjectHelper { + public: + QObjectHelper(); + ~QObjectHelper(); + + enum Flag { + Flag_None, + Flag_StoreNullVariants, + Flag_StoreInvalidVariants, + Flag_All = Flag_StoreNullVariants | Flag_StoreInvalidVariants + }; + Q_DECLARE_FLAGS(Flags, Flag) + + /** + * This method converts a QObject instance into a QVariantMap. + * + * @param object The QObject instance to be converted. + * @param ignoredProperties Properties that won't be converted. + */ + static QVariantMap qobject2qvariant(const QObject* object, Flags flags = Flag_All, + const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName")))); + static QVariantMap qobject2qvariant(const QObject* object, + const QStringList& ignoredProperties); + + /** + * This method converts a QVariantMap instance into a QObject + * + * @param variant Attributes to assign to the object. + * @param object The QObject instance to update. + */ + static void qvariant2qobject(const QVariantMap& variant, QObject* object); + + private: + Q_DISABLE_COPY(QObjectHelper) + class QObjectHelperPrivate; + QObjectHelperPrivate* const d; + }; +} + +#endif // QOBJECTHELPER_H diff --git a/src/http/qjson/src/serializer.cpp b/src/http/qjson/src/serializer.cpp new file mode 100644 index 000000000..25f9bb981 --- /dev/null +++ b/src/http/qjson/src/serializer.cpp @@ -0,0 +1,444 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Till Adam + * Copyright (C) 2009 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "serializer.h" + +#include +#include +#include + +#include + +#ifdef Q_OS_SOLARIS +# ifndef isinf +# include +# define isinf(x) (!finite((x)) && (x)==(x)) +# endif +#endif + +#ifdef _MSC_VER // using MSVC compiler +#include +#endif + +using namespace QJson; + +class Serializer::SerializerPrivate { + public: + SerializerPrivate() : + specialNumbersAllowed(false), + indentMode(QJson::IndentNone), + doublePrecision(6) { + errorMessage.clear(); + } + QString errorMessage; + bool specialNumbersAllowed; + IndentMode indentMode; + int doublePrecision; + QByteArray buildIndent(int spaces); + QByteArray serialize( const QVariant &v, bool *ok, int indentLevel = 0); + QString sanitizeString( QString str ); + QByteArray join( const QList& list, const QByteArray& sep ); +}; + +QByteArray Serializer::SerializerPrivate::join( const QList& list, const QByteArray& sep ) { + QByteArray res; + Q_FOREACH( const QByteArray& i, list ) { + if ( !res.isEmpty() ) + res += sep; + res += i; + } + return res; +} + +QByteArray Serializer::SerializerPrivate::serialize( const QVariant &v, bool *ok, int indentLevel) +{ + QByteArray str; + + if ( ! v.isValid() ) { // invalid or null? + str = "null"; + } else if (( v.type() == QVariant::List ) || ( v.type() == QVariant::StringList )){ // an array or a stringlist? + const QVariantList list = v.toList(); + QList values; + Q_FOREACH( const QVariant& var, list ) + { + QByteArray serializedValue; + + serializedValue = serialize( var, ok, indentLevel+1); + + if ( !*ok ) { + break; + } + switch(indentMode) { + case QJson::IndentFull : + case QJson::IndentMedium : + case QJson::IndentMinimum : + values << serializedValue; + break; + case QJson::IndentCompact : + case QJson::IndentNone : + default: + values << serializedValue.trimmed(); + break; + } + } + + if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull ) { + QByteArray indent = buildIndent(indentLevel); + str = indent + "[\n" + join( values, ",\n" ) + "\n" + indent + "]"; + } + else if (indentMode == QJson::IndentMinimum) { + QByteArray indent = buildIndent(indentLevel); + str = indent + "[\n" + join( values, ",\n" ) + "\n" + indent + "]"; + } + else if (indentMode == QJson::IndentCompact) { + str = "[" + join( values, "," ) + "]"; + } + else { + str = "[ " + join( values, ", " ) + " ]"; + } + + } else if ( v.type() == QVariant::Map ) { // variant is a map? + const QVariantMap vmap = v.toMap(); + QMapIterator it( vmap ); + + if (indentMode == QJson::IndentMinimum) { + QByteArray indent = buildIndent(indentLevel); + str = indent + "{ "; + } + else if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) { + QByteArray indent = buildIndent(indentLevel); + QByteArray nextindent = buildIndent(indentLevel + 1); + str = indent + "{\n" + nextindent; + } + else if (indentMode == QJson::IndentCompact) { + str = "{"; + } + else { + str = "{ "; + } + + QList pairs; + while ( it.hasNext() ) { + it.next(); + indentLevel++; + QByteArray serializedValue = serialize( it.value(), ok, indentLevel); + indentLevel--; + if ( !*ok ) { + break; + } + QByteArray key = sanitizeString( it.key() ).toUtf8(); + QByteArray value = serializedValue.trimmed(); + if (indentMode == QJson::IndentCompact) { + pairs << key + ":" + value; + } else { + pairs << key + " : " + value; + } + } + + if (indentMode == QJson::IndentFull) { + QByteArray indent = buildIndent(indentLevel + 1); + str += join( pairs, ",\n" + indent); + } + else if (indentMode == QJson::IndentCompact) { + str += join( pairs, "," ); + } + else { + str += join( pairs, ", " ); + } + + if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) { + QByteArray indent = buildIndent(indentLevel); + str += "\n" + indent + "}"; + } + else if (indentMode == QJson::IndentCompact) { + str += "}"; + } + else { + str += " }"; + } + + } else if ( v.type() == QVariant::Hash ) { // variant is a hash? + const QVariantHash vhash = v.toHash(); + QHashIterator it( vhash ); + + if (indentMode == QJson::IndentMinimum) { + QByteArray indent = buildIndent(indentLevel); + str = indent + "{ "; + } + else if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) { + QByteArray indent = buildIndent(indentLevel); + QByteArray nextindent = buildIndent(indentLevel + 1); + str = indent + "{\n" + nextindent; + } + else if (indentMode == QJson::IndentCompact) { + str = "{"; + } + else { + str = "{ "; + } + + QList pairs; + while ( it.hasNext() ) { + it.next(); + + QByteArray serializedValue = serialize( it.value(), ok, indentLevel + 1); + + if ( !*ok ) { + break; + } + QByteArray key = sanitizeString( it.key() ).toUtf8(); + QByteArray value = serializedValue.trimmed(); + if (indentMode == QJson::IndentCompact) { + pairs << key + ":" + value; + } else { + pairs << key + " : " + value; + } + } + + if (indentMode == QJson::IndentFull) { + QByteArray indent = buildIndent(indentLevel + 1); + str += join( pairs, ",\n" + indent); + } + else if (indentMode == QJson::IndentCompact) { + str += join( pairs, "," ); + } + else { + str += join( pairs, ", " ); + } + + if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) { + QByteArray indent = buildIndent(indentLevel); + str += "\n" + indent + "}"; + } + else if (indentMode == QJson::IndentCompact) { + str += "}"; + } + else { + str += " }"; + } + + } else { + // Add indent, we may need to remove it later for some layouts + switch(indentMode) { + case QJson::IndentFull : + case QJson::IndentMedium : + case QJson::IndentMinimum : + str += buildIndent(indentLevel); + break; + case QJson::IndentCompact : + case QJson::IndentNone : + default: + break; + } + + if (( v.type() == QVariant::String ) || ( v.type() == QVariant::ByteArray )) { // a string or a byte array? + str = sanitizeString( v.toString() ).toUtf8(); + } else if (( v.type() == QVariant::Double) || ((QMetaType::Type)v.type() == QMetaType::Float)) { // a double or a float? + const double value = v.toDouble(); + #if defined _WIN32 && !defined(Q_OS_SYMBIAN) + const bool special = _isnan(value) || !_finite(value); + #elif defined(Q_OS_SYMBIAN) || defined(Q_OS_ANDROID) || defined(Q_OS_BLACKBERRY) || defined(Q_OS_SOLARIS) + const bool special = isnan(value) || isinf(value); + #else + const bool special = std::isnan(value) || std::isinf(value); + #endif + if (special) { + if (specialNumbersAllowed) { + #if defined _WIN32 && !defined(Q_OS_SYMBIAN) + if (_isnan(value)) { + #elif defined(Q_OS_SYMBIAN) || defined(Q_OS_ANDROID) || defined(Q_OS_BLACKBERRY) || defined(Q_OS_SOLARIS) + if (isnan(value)) { + #else + if (std::isnan(value)) { + #endif + str += "NaN"; + } else { + if (value<0) { + str += '-'; + } + str += "Infinity"; + } + } else { + errorMessage += QLatin1String("Attempt to write NaN or infinity, which is not supported by json\n"); + *ok = false; + } + } else { + str = QByteArray::number( value , 'g', doublePrecision); + if( ! str.contains( "." ) && ! str.contains( "e" ) ) { + str += ".0"; + } + } + } else if ( v.type() == QVariant::Bool ) { // boolean value? + str += ( v.toBool() ? "true" : "false" ); + } else if ( v.type() == QVariant::ULongLong ) { // large unsigned number? + str += QByteArray::number( v.value() ); + } else if ( v.type() == QVariant::UInt ) { // unsigned int number? + str += QByteArray::number( v.value() ); + } else if ( v.canConvert() ) { // any signed number? + str += QByteArray::number( v.value() ); + } else if ( v.canConvert() ) { // unsigned short number? + str += QByteArray::number( v.value() ); + } else if ( v.canConvert() ){ // can value be converted to string? + // this will catch QDate, QDateTime, QUrl, ... + str += sanitizeString( v.toString() ).toUtf8(); + //TODO: catch other values like QImage, QRect, ... + } else { + *ok = false; + errorMessage += QLatin1String("Cannot serialize "); + errorMessage += v.toString(); + errorMessage += QLatin1String(" because type "); + errorMessage += QLatin1String(v.typeName()); + errorMessage += QLatin1String(" is not supported by QJson\n"); + } + } + if ( *ok ) + { + return str; + } + else + return QByteArray(); +} + +QByteArray Serializer::SerializerPrivate::buildIndent(int spaces) +{ + QByteArray indent; + if (spaces < 0) { + spaces = 0; + } + for (int i = 0; i < spaces; i++ ) { + indent += " "; + } + return indent; +} + +QString Serializer::SerializerPrivate::sanitizeString( QString str ) +{ + str.replace( QLatin1String( "\\" ), QLatin1String( "\\\\" ) ); + + // escape unicode chars + QString result; + const ushort* unicode = str.utf16(); + unsigned int i = 0; + + while ( unicode[ i ] ) { + if ( unicode[ i ] < 128 ) { + result.append( QChar( unicode[ i ] ) ); + } + else { + QString hexCode = QString::number( unicode[ i ], 16 ).rightJustified( 4, + QLatin1Char('0') ); + + result.append( QLatin1String ("\\u") ).append( hexCode ); + } + ++i; + } + str = result; + + str.replace( QLatin1String( "\"" ), QLatin1String( "\\\"" ) ); + str.replace( QLatin1String( "\b" ), QLatin1String( "\\b" ) ); + str.replace( QLatin1String( "\f" ), QLatin1String( "\\f" ) ); + str.replace( QLatin1String( "\n" ), QLatin1String( "\\n" ) ); + str.replace( QLatin1String( "\r" ), QLatin1String( "\\r" ) ); + str.replace( QLatin1String( "\t" ), QLatin1String( "\\t" ) ); + + return QString( QLatin1String( "\"%1\"" ) ).arg( str ); +} + +Serializer::Serializer() + : d( new SerializerPrivate ) +{ +} + +Serializer::~Serializer() { + delete d; +} + +void Serializer::serialize( const QVariant& v, QIODevice* io, bool* ok) +{ + Q_ASSERT( io ); + *ok = true; + + if (!io->isOpen()) { + if (!io->open(QIODevice::WriteOnly)) { + d->errorMessage = QLatin1String("Error opening device"); + *ok = false; + return; + } + } + + if (!io->isWritable()) { + d->errorMessage = QLatin1String("Device is not readable"); + io->close(); + *ok = false; + return; + } + + const QByteArray str = serialize( v, ok); + if (*ok && (io->write(str) != str.count())) { + *ok = false; + d->errorMessage = QLatin1String("Something went wrong while writing to IO device"); + } +} + +QByteArray Serializer::serialize( const QVariant &v) +{ + bool ok; + + return serialize(v, &ok); +} + +QByteArray Serializer::serialize( const QVariant &v, bool *ok) +{ + bool _ok = true; + d->errorMessage.clear(); + + if (ok) { + *ok = true; + } else { + ok = &_ok; + } + + return d->serialize(v, ok); +} + +void QJson::Serializer::allowSpecialNumbers(bool allow) { + d->specialNumbersAllowed = allow; +} + +bool QJson::Serializer::specialNumbersAllowed() const { + return d->specialNumbersAllowed; +} + +void QJson::Serializer::setIndentMode(IndentMode mode) { + d->indentMode = mode; +} + +void QJson::Serializer::setDoublePrecision(int precision) { + d->doublePrecision = precision; +} + +IndentMode QJson::Serializer::indentMode() const { + return d->indentMode; +} + +QString QJson::Serializer::errorMessage() const { + return d->errorMessage; +} + diff --git a/src/http/qjson/src/serializer.h b/src/http/qjson/src/serializer.h new file mode 100644 index 000000000..48dc9ae98 --- /dev/null +++ b/src/http/qjson/src/serializer.h @@ -0,0 +1,230 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Till Adam + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef QJSON_SERIALIZER_H +#define QJSON_SERIALIZER_H + +#include "qjson_export.h" + +QT_BEGIN_NAMESPACE +class QIODevice; +class QString; +class QVariant; +QT_END_NAMESPACE + +namespace QJson { + /** + @brief How the indentation should work. + \verbatim + none (default) : + { "foo" : 0, "foo1" : 1, "foo2" : [ { "bar" : 1, "foo" : 0, "foobar" : 0 }, { "bar" : 1, "foo" : 1, "foobar" : 1 } ], "foo3" : [ 1, 2, 3, 4, 5, 6 ] } + + compact : + {"foo":0,"foo1":1,"foo2":[{"bar":1,"foo":0,"foobar":0},{"bar":1,"foo":1,"foobar":1}],"foo3":[1,2,3,4,5,6]} + + minimum : + { "foo" : 0, "foo1" : 1, "foo2" : [ + { "bar" : 1, "foo" : 0, "foobar" : 0 }, + { "bar" : 1, "foo" : 1, "foobar" : 1 } + ], "foo3" : [ + 1, + 2, + 3, + 4, + 5, + 6 + ] } + + medium : + { + "foo" : 0, "foo1" : 1, "foo2" : [ + { + "bar" : 1, "foo" : 0, "foobar" : 0 + }, + { + "bar" : 1, "foo" : 1, "foobar" : 1 + } + ], "foo3" : [ + 1, + 2, + 3, + 4, + 5, + 6 + ] + } + + full : + { + "foo" : 0, + "foo1" : 1, + "foo2" : [ + { + "bar" : 1, + "foo" : 0, + "foobar" : 0 + }, + { + "bar" : 1, + "foo" : 1, + "foobar" : 1 + } + ], + "foo3" : [ + 1, + 2, + 3, + 4, + 5, + 6 + ] + } + + + \endverbatim + */ + enum IndentMode { + IndentNone, + IndentCompact, + IndentMinimum, + IndentMedium, + IndentFull + }; + /** + * @brief Main class used to convert QVariant objects to JSON data. + * + * QVariant objects are converted to a string containing the JSON data. + * + * + * Usage: + * + * \code + * QVariantList people; + * + * QVariantMap bob; + * bob.insert("Name", "Bob"); + * bob.insert("Phonenumber", 123); + * + * QVariantMap alice; + * alice.insert("Name", "Alice"); + * alice.insert("Phonenumber", 321); + * + * people << bob << alice; + * + * QJson::Serializer serializer; + * bool ok; + * QByteArray json = serializer.serialize(people, &ok); + * + * if (ok) { + * qDebug() << json; + * } else { + * qCritical() << "Something went wrong:" << serializer.errorMessage(); + * } + * \endcode + * + * The output will be: + * + * \code + * "[ { "Name" : "Bob", "Phonenumber" : 123 }, + * { "Name" : "Alice", "Phonenumber" : 321 } ]" + * \endcode + * + * It's possible to tune the indentation level of the resulting string. \sa setIndentMode + */ + class QJSON_EXPORT Serializer { + public: + Serializer(); + ~Serializer(); + + /** + * This method generates a textual JSON representation and outputs it to the + * passed in I/O Device. + * @param variant The JSON document in its in-memory representation as generated by the + * parser. + * @param out Input output device + * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true + */ + void serialize( const QVariant& variant, QIODevice* out, bool* ok); + + /** + * This is a method provided for convenience. It turns the passed in in-memory + * representation of the JSON document into a textual one, which is returned. + * If the returned string is empty, the document was empty. If it was null, there + * was a parsing error. + * + * @param variant The JSON document in its in-memory representation as generated by the + * parser. + * + * \deprecated This method is going to be removed with the next major release of QJson. + */ + QByteArray serialize( const QVariant& variant); + + /** + * This is a method provided for convenience. It turns the passed in in-memory + * representation of the JSON document into a textual one, which is returned. + * If the returned string is empty, the document was empty. If it was null, there + * was a parsing error. + * + * @param variant The JSON document in its in-memory representation as generated by the + * parser. + * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true + */ + QByteArray serialize( const QVariant& variant, bool *ok); + + /** + * Allow or disallow writing of NaN and/or Infinity (as an extension to QJson) + */ + void allowSpecialNumbers(bool allow); + + /** + * Is Nan and/or Infinity allowed? + */ + bool specialNumbersAllowed() const; + + /** + * set output indentation mode as defined in QJson::IndentMode + */ + void setIndentMode(IndentMode mode = QJson::IndentNone); + + + /** + * set double precision used while converting Double + * \sa QByteArray::number + */ + void setDoublePrecision(int precision); + + /** + * Returns one of the indentation modes defined in QJson::IndentMode + */ + IndentMode indentMode() const; + + /** + * Returns the error message + */ + QString errorMessage() const; + + private: + Q_DISABLE_COPY(Serializer) + class SerializerPrivate; + SerializerPrivate* const d; + }; +} + +#endif // QJSON_SERIALIZER_H diff --git a/src/http/qjson/src/serializerrunnable.cpp b/src/http/qjson/src/serializerrunnable.cpp new file mode 100644 index 000000000..1af03c366 --- /dev/null +++ b/src/http/qjson/src/serializerrunnable.cpp @@ -0,0 +1,62 @@ +#include "serializerrunnable.h" + +/* This file is part of qjson + * + * Copyright (C) 2009 Flavio Castelli + * 2009 Frank Osterfeld + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "parserrunnable.h" +#include "serializer.h" + +#include +#include + +using namespace QJson; + +class SerializerRunnable::Private +{ +public: + QVariant json; +}; + +SerializerRunnable::SerializerRunnable(QObject* parent) + : QObject(parent), + QRunnable(), + d(new Private) +{ + qRegisterMetaType("QVariant"); +} + +SerializerRunnable::~SerializerRunnable() +{ + delete d; +} + +void SerializerRunnable::setJsonObject( const QVariant& json ) +{ + d->json = json; +} + +void SerializerRunnable::run() +{ + Serializer serializer; + bool ok; + const QByteArray serialized = serializer.serialize( d->json, &ok); + Q_EMIT parsingFinished( serialized, ok, serializer.errorMessage() ); +} diff --git a/src/http/qjson/src/serializerrunnable.h b/src/http/qjson/src/serializerrunnable.h new file mode 100644 index 000000000..1a3df7c1c --- /dev/null +++ b/src/http/qjson/src/serializerrunnable.h @@ -0,0 +1,71 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Frank Osterfeld + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef SERIALIZERRUNNABLE_H +#define SERIALIZERRUNNABLE_H + +#include "qjson_export.h" + +#include +#include + +QT_BEGIN_NAMESPACE +class QByteArray; +class QString; +class QVariant; +QT_END_NAMESPACE + +namespace QJson { + /** + * @brief Convenience class for converting JSON data to QVariant objects using a dedicated thread + */ + class QJSON_EXPORT SerializerRunnable : public QObject, public QRunnable + { + Q_OBJECT + public: + explicit SerializerRunnable(QObject* parent = 0); + ~SerializerRunnable(); + + /** + * Sets the json object to serialize. + * + * @param json QVariant containing the json representation to be serialized + */ + void setJsonObject( const QVariant& json ); + + /* reimp */ void run(); + + Q_SIGNALS: + /** + * This signal is emitted when the serialization process has been completed + * @param serialized contains the result of the serialization + * @param ok if a serialization error occurs ok is set to false, otherwise it's set to true. + * @param error_msg contains a string explaining the failure reason + **/ + void parsingFinished(const QByteArray& serialized, bool ok, const QString& error_msg); + + private: + Q_DISABLE_COPY(SerializerRunnable) + class Private; + Private* const d; + }; +} + +#endif // SERIALIZERRUNNABLE_H diff --git a/src/http/qjson/src/stack.hh b/src/http/qjson/src/stack.hh new file mode 100644 index 000000000..590accbaf --- /dev/null +++ b/src/http/qjson/src/stack.hh @@ -0,0 +1,133 @@ +/* A Bison parser, made by GNU Bison 2.7. */ + +/* Stack handling for Bison parsers in C++ + + Copyright (C) 2002-2012 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/** + ** \file stack.hh + ** Define the yy::stack class. + */ + +#ifndef YY_YY_STACK_HH_INCLUDED +# define YY_YY_STACK_HH_INCLUDED + +# include + + +namespace yy { +/* Line 34 of stack.hh */ +#line 47 "stack.hh" + template > + class stack + { + public: + // Hide our reversed order. + typedef typename S::reverse_iterator iterator; + typedef typename S::const_reverse_iterator const_iterator; + + stack () : seq_ () + { + } + + stack (unsigned int n) : seq_ (n) + { + } + + inline + T& + operator [] (unsigned int i) + { + return seq_[i]; + } + + inline + const T& + operator [] (unsigned int i) const + { + return seq_[i]; + } + + inline + void + push (const T& t) + { + seq_.push_front (t); + } + + inline + void + pop (unsigned int n = 1) + { + for (; n; --n) + seq_.pop_front (); + } + + inline + unsigned int + height () const + { + return seq_.size (); + } + + inline const_iterator begin () const { return seq_.rbegin (); } + inline const_iterator end () const { return seq_.rend (); } + + private: + S seq_; + }; + + /// Present a slice of the top of a stack. + template > + class slice + { + public: + slice (const S& stack, unsigned int range) + : stack_ (stack) + , range_ (range) + { + } + + inline + const T& + operator [] (unsigned int i) const + { + return stack_[range_ - i]; + } + + private: + const S& stack_; + unsigned int range_; + }; + +} // yy +/* Line 116 of stack.hh */ +#line 132 "stack.hh" + +#endif /* !YY_YY_STACK_HH_INCLUDED */ diff --git a/src/http/qjson/tests/.gitignore b/src/http/qjson/tests/.gitignore new file mode 100644 index 000000000..f3c7a7c5d --- /dev/null +++ b/src/http/qjson/tests/.gitignore @@ -0,0 +1 @@ +Makefile diff --git a/src/http/qjson/tests/CMakeLists.txt b/src/http/qjson/tests/CMakeLists.txt new file mode 100644 index 000000000..dab0cdca8 --- /dev/null +++ b/src/http/qjson/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +IF (Qt5Core_FOUND) + FIND_PACKAGE( Qt5Test REQUIRED ) + + INCLUDE_DIRECTORIES(${Qt5Test_INCLUDE_DIRS}) + ADD_DEFINITIONS(${Qt5Test_DEFINITIONS}) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Test_EXECUTABLE_COMPILE_FLAGS}") + + SET (TEST_LIBRARIES ${Qt5Test_LIBRARIES}) +ENDIF() + +ADD_SUBDIRECTORY(cmdline_tester) +ADD_SUBDIRECTORY(parser) +ADD_SUBDIRECTORY(scanner) +ADD_SUBDIRECTORY(qobjecthelper) +ADD_SUBDIRECTORY(serializer) diff --git a/src/http/qjson/tests/benchmarks/CMakeLists.txt b/src/http/qjson/tests/benchmarks/CMakeLists.txt new file mode 100644 index 000000000..824b56ec3 --- /dev/null +++ b/src/http/qjson/tests/benchmarks/CMakeLists.txt @@ -0,0 +1,38 @@ +##### Probably don't want to edit below this line ##### + +SET( QT_USE_QTTEST TRUE ) + +INCLUDE(AddFileDependencies) + +# Include the library include directories, and the current build directory (moc) +INCLUDE_DIRECTORIES( + ../../include + ${CMAKE_CURRENT_BINARY_DIR} +) + +SET( UNIT_TESTS + parsingbenchmark + qlocalevsstrtod_l +) + +# Build the tests +FOREACH(test ${UNIT_TESTS}) + MESSAGE(STATUS "Building ${test}") + ADD_EXECUTABLE( + ${test} + ${test}.cpp + ) + + TARGET_LINK_LIBRARIES( + ${test} + ${QT_LIBRARIES} + ${TEST_LIBRARIES} + qjson + ) + if (QJSON_TEST_OUTPUT STREQUAL "xml") + # produce XML output + add_test( ${test} ${test} -xml -o ${test}.tml ) + else (QJSON_TEST_OUTPUT STREQUAL "xml") + add_test( ${test} ${test} ) + endif (QJSON_TEST_OUTPUT STREQUAL "xml") +ENDFOREACH() diff --git a/src/http/qjson/tests/benchmarks/parsingbenchmark.cpp b/src/http/qjson/tests/benchmarks/parsingbenchmark.cpp new file mode 100644 index 000000000..51cbd8606 --- /dev/null +++ b/src/http/qjson/tests/benchmarks/parsingbenchmark.cpp @@ -0,0 +1,55 @@ +/* This file is part of QJson + * + * Copyright (C) 2014 Sune Vuorela + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include + +class ParsingBenchmark: public QObject { + Q_OBJECT + private Q_SLOTS: + void benchmark(); +}; + +void ParsingBenchmark::benchmark() { + QString path = QFINDTESTDATA("largefile.json"); + + QVERIFY(QFile::exists(path)); + + QFile f(path); + QVERIFY(f.open(QIODevice::ReadOnly)); + + QByteArray data = f.readAll(); + + QVariant result; + + QJson::Parser parser; + QBENCHMARK { + result = parser.parse(data); + } + + Q_UNUSED(result); +} + + +QTEST_MAIN(ParsingBenchmark) + +#include "parsingbenchmark.moc" diff --git a/src/http/qjson/tests/benchmarks/qlocalevsstrtod_l.cpp b/src/http/qjson/tests/benchmarks/qlocalevsstrtod_l.cpp new file mode 100644 index 000000000..9bda9d454 --- /dev/null +++ b/src/http/qjson/tests/benchmarks/qlocalevsstrtod_l.cpp @@ -0,0 +1,70 @@ +/* This file is part of QJson + * + * Copyright (C) 2014 Sune Vuorela + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include + +class QLocaleVsStrtod_l : public QObject { + Q_OBJECT + private Q_SLOTS: + void benchmark(); + void benchmark_data(); + +}; + +void QLocaleVsStrtod_l::benchmark() { + QFETCH(bool, useQLocale); + QList l; + l << strdup("0.123") << strdup("0.947834") << strdup("8.8373") << strdup("884.82921"); + + double result; + + if(useQLocale) { + QLocale c(QLocale::C); + QBENCHMARK { + Q_FOREACH(const char* str, l) { + result = c.toDouble(QString(str)); + } + } + } else { + locale_t c = newlocale(LC_NUMERIC_MASK, "C", NULL); + QBENCHMARK { + Q_FOREACH(const char* str, l) { + result = strtod_l(str, NULL, c); + } + } + } + + + Q_FOREACH(char* str, l) { + free(str); + } +} + +void QLocaleVsStrtod_l::benchmark_data() { + QTest::addColumn("useQLocale"); + + QTest::newRow("using QLocale") << true; + QTest::newRow("using strtod_l") << false; +} + +QTEST_MAIN(QLocaleVsStrtod_l); +#include "qlocalevsstrtod_l.moc" diff --git a/src/http/qjson/tests/cmdline_tester/.gitignore b/src/http/qjson/tests/cmdline_tester/.gitignore new file mode 100644 index 000000000..1347175f2 --- /dev/null +++ b/src/http/qjson/tests/cmdline_tester/.gitignore @@ -0,0 +1,4 @@ +Makefile +*.o +*.moc +cmdline_tester diff --git a/src/http/qjson/tests/cmdline_tester/CMakeLists.txt b/src/http/qjson/tests/cmdline_tester/CMakeLists.txt new file mode 100644 index 000000000..c7cf4cd0b --- /dev/null +++ b/src/http/qjson/tests/cmdline_tester/CMakeLists.txt @@ -0,0 +1,35 @@ +##### Probably don't want to edit below this line ##### + +IF (WIN32 AND Qt5Core_FOUND) + FIND_PACKAGE( Qt5Widgets REQUIRED ) + + INCLUDE_DIRECTORIES(${Qt5Widgets_INCLUDE_DIRS}) + ADD_DEFINITIONS(${Qt5Widgets_DEFINITIONS}) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}") +ENDIF() + +IF (NOT Qt5Core_FOUND) + # Use it + INCLUDE( ${QT_USE_FILE} ) +ENDIF() + +INCLUDE(AddFileDependencies) + +# Include the library include directories, and the current build directory (moc) +INCLUDE_DIRECTORIES( + ../../include + ${CMAKE_CURRENT_BINARY_DIR} +) + +ADD_EXECUTABLE( + cmdline_tester + cmdline_tester.cpp + cmdlineparser.cpp +) + +TARGET_LINK_LIBRARIES( + cmdline_tester + ${QT_LIBRARIES} + ${Qt5Widgets_LIBRARIES} + qjson +) diff --git a/src/http/qjson/tests/cmdline_tester/cmdline_tester.cpp b/src/http/qjson/tests/cmdline_tester/cmdline_tester.cpp new file mode 100644 index 000000000..81289f12d --- /dev/null +++ b/src/http/qjson/tests/cmdline_tester/cmdline_tester.cpp @@ -0,0 +1,99 @@ +/* This file is part of QJson + * + * Copyright (C) 2009 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "cmdlineparser.h" + +using namespace QJson; + +int main(int argc, char *argv[]) { + QCoreApplication app (argc, argv); + +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) + QTextCodec *codec = QTextCodec::codecForName("UTF-8"); + QTextCodec::setCodecForCStrings(codec); +#endif + + QTime time; + int duration; + + + CmdLineParser cmd (app.arguments()); + CmdLineParser::Result res = cmd.parse(); + if (res == CmdLineParser::Help) + return 0; + else if (res == CmdLineParser::Error) + return -1; + + QString filename = cmd.file(); + if (!QFile::exists ( filename )) { + qCritical ("The file you specified doesn't exist!"); + exit (1); + } + + Parser parser; + bool ok; + + QFile file (filename); + time.start(); + QVariant data = parser.parse (&file, &ok); + duration = time.elapsed(); + if (!ok) { + qCritical("%s:%i - Error: %s", filename.toLatin1().data(), parser.errorLine(), qPrintable(parser.errorString())); + exit (1); + } + else { + qDebug() << "Parsing of" << filename << "took" << duration << "ms"; + if (!cmd.quiet()) + qDebug() << data; + } + + if (cmd.serialize()) { + // serializer tests + qDebug() << "Serializing... "; + QJson::Serializer serializer; + serializer.setIndentMode(cmd.indentationMode()); + time.start(); + QByteArray b = serializer.serialize(data, &ok); + if (!ok) { + qCritical() << "Serialization failed:" << serializer.errorMessage(); + exit(1); + } else { + duration = time.elapsed(); + qDebug() << "Serialization took:" << duration << "ms"; + if (!cmd.quiet()) + qDebug() << b; + } + } + + qDebug() << "JOB DONE, BYE"; + return 0; +} + diff --git a/src/http/qjson/tests/cmdline_tester/cmdlineparser.cpp b/src/http/qjson/tests/cmdline_tester/cmdlineparser.cpp new file mode 100644 index 000000000..289fe0d4a --- /dev/null +++ b/src/http/qjson/tests/cmdline_tester/cmdlineparser.cpp @@ -0,0 +1,170 @@ +/* This file is part of qjson + * + * Copyright (C) 2010 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include + +#include +#ifdef Q_OS_WIN +//using Qt5 +#ifdef QT_WIDGETS_LIB +#include +#else +//using Qt4 +#include +#endif +#endif + +#include "cmdlineparser.h" + +using namespace QJson; + +const QString CmdLineParser::m_helpMessage = QLatin1String( + "Usage: cmdline_tester [options] file\n\n" + "This program converts the json data read from 'file' to a QVariant object.\n" + "--quiet Do not print output generated by parser and serializer.\n" + "--serialize Parses the QVariant object back to json.\n" + "--indent Sets the indentation level used by the 'serialize' option.\n" + " Allowed values:\n" + " - none [default]\n" + " - compact\n" + " - minimum\n" + " - medium\n" + " - full\n" + "--help Displays this help.\n" + ); + + +CmdLineParser::CmdLineParser(const QStringList &arguments) + : m_pos(0), + m_indentationMode(IndentNone), + m_serialize(false), + m_quiet(false) +{ + for (int i = 1; i < arguments.count(); ++i) { + const QString &arg = arguments.at(i); + m_arguments.append(arg); + } +} + +CmdLineParser::Result CmdLineParser::parse() +{ + bool showHelp = false; + + while (m_error.isEmpty() && hasMoreArgs()) { + const QString &arg = nextArg(); + if (arg.toLower() == QLatin1String("--indent")) + handleSetIndentationMode(); + else if (arg.toLower() == QLatin1String("--help")) + showHelp = true; + else if (arg.toLower() == QLatin1String("--serialize")) + m_serialize = true; + else if (arg.toLower() == QLatin1String("--quiet")) + m_quiet = true; + else if (!arg.startsWith(QLatin1String("--"))) + m_file = arg; + else + m_error = QString(QLatin1String("Unknown option: %1")).arg(arg); + } + + if (m_file.isEmpty()) { + m_error = QLatin1String("You have to specify the file containing the json data."); + } + + if (!m_error.isEmpty()) { + showMessage(m_error + QLatin1String("\n\n\n") + m_helpMessage, true); + return Error; + } else if (showHelp) { + showMessage(m_helpMessage, false); + return Help; + } + return Ok; +} + +bool CmdLineParser::hasMoreArgs() const +{ + return m_pos < m_arguments.count(); +} + +const QString &CmdLineParser::nextArg() +{ + Q_ASSERT(hasMoreArgs()); + return m_arguments.at(m_pos++); +} + +void CmdLineParser::handleSetIndentationMode() +{ + if (hasMoreArgs()) { + const QString &indentationMode = nextArg(); + if (indentationMode.compare(QLatin1String("none"), Qt::CaseInsensitive) == 0) + m_indentationMode = IndentNone; + else if (indentationMode.compare(QLatin1String("compact"), Qt::CaseInsensitive) == 0) + m_indentationMode = IndentCompact; + else if (indentationMode.compare(QLatin1String("minimum"), Qt::CaseInsensitive) == 0) + m_indentationMode = IndentMinimum; + else if (indentationMode.compare(QLatin1String("medium"), Qt::CaseInsensitive) == 0) + m_indentationMode = IndentMedium; + else if (indentationMode.compare(QLatin1String("full"), Qt::CaseInsensitive) == 0) + m_indentationMode = IndentFull; + else + m_error = QString(QLatin1String("Unknown indentation mode '%1'.")). + arg(indentationMode); + } else { + m_error = QLatin1String("Missing indentation level."); + } +} + +void CmdLineParser::showMessage(const QString &msg, bool error) +{ +#ifdef Q_OS_WIN + QString message = QLatin1String("
") % msg % QLatin1String("
"); + if (error) + QMessageBox::critical(0, QLatin1String("Error"), message); + else + QMessageBox::information(0, QLatin1String("Notice"), message); +#else + fprintf(error ? stderr : stdout, "%s\n", qPrintable(msg)); +#endif +} + +void CmdLineParser::setIndentationMode(const IndentMode &mode) +{ + m_indentationMode = mode; +} + +IndentMode CmdLineParser::indentationMode() const +{ + return m_indentationMode; +} + +QString CmdLineParser::file() const +{ + return m_file; +} + +bool CmdLineParser::serialize() +{ + return m_serialize; +} + +bool CmdLineParser::quiet() +{ + return m_quiet; +} + diff --git a/src/http/qjson/tests/cmdline_tester/cmdlineparser.h b/src/http/qjson/tests/cmdline_tester/cmdlineparser.h new file mode 100644 index 000000000..994a2692f --- /dev/null +++ b/src/http/qjson/tests/cmdline_tester/cmdlineparser.h @@ -0,0 +1,64 @@ +/* This file is part of qjson + * + * Copyright (C) 2010 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef CMDLINEPARSER_H +#define CMDLINEPARSER_H + +#include +#include + +#include + +namespace QJson { + class CmdLineParser + { + public: + enum Result {Ok, Help, Error}; + + CmdLineParser(const QStringList &arguments); + Result parse(); + + void setIndentationMode(const IndentMode &mode); + IndentMode indentationMode() const; + QString helpFile() const; + QString file() const; + bool serialize(); + bool quiet(); + + void showMessage(const QString &msg, bool error); + + private: + bool hasMoreArgs() const; + const QString &nextArg(); + void handleSetIndentationMode(); + + QStringList m_arguments; + int m_pos; + IndentMode m_indentationMode; + QString m_file; + bool m_serialize; + bool m_quiet; + static const QString m_helpMessage; + QString m_error; + }; +} + +#endif + diff --git a/src/http/qjson/tests/cmdline_tester/example.txt b/src/http/qjson/tests/cmdline_tester/example.txt new file mode 100644 index 000000000..eacfbf5e6 --- /dev/null +++ b/src/http/qjson/tests/cmdline_tester/example.txt @@ -0,0 +1,22 @@ +{ + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": ["GML", "XML"] + }, + "GlossSee": "markup" + } + } + } + } +} diff --git a/src/http/qjson/tests/parser/.gitignore b/src/http/qjson/tests/parser/.gitignore new file mode 100644 index 000000000..46e5733e2 --- /dev/null +++ b/src/http/qjson/tests/parser/.gitignore @@ -0,0 +1,4 @@ +Makefile +*.o +*.moc +parser diff --git a/src/http/qjson/tests/parser/CMakeLists.txt b/src/http/qjson/tests/parser/CMakeLists.txt new file mode 100644 index 000000000..bd86cee9b --- /dev/null +++ b/src/http/qjson/tests/parser/CMakeLists.txt @@ -0,0 +1,46 @@ +##### Probably don't want to edit below this line ##### + +SET( QT_USE_QTTEST TRUE ) + +IF (NOT Qt5Core_FOUND) + # Use it + INCLUDE( ${QT_USE_FILE} ) +ENDIF() + +INCLUDE(AddFileDependencies) + +# Include the library include directories, and the current build directory (moc) +INCLUDE_DIRECTORIES( + ../../include + ${CMAKE_CURRENT_BINARY_DIR} +) + +SET( UNIT_TESTS + testparser +) + +# Build the tests +FOREACH(test ${UNIT_TESTS}) + MESSAGE(STATUS "Building ${test}") + IF (NOT Qt5Core_FOUND) + QT4_WRAP_CPP(MOC_SOURCE ${test}.cpp) + ENDIF() + ADD_EXECUTABLE( + ${test} + ${test}.cpp + ) + + ADD_FILE_DEPENDENCIES(${test}.cpp ${MOC_SOURCE}) + TARGET_LINK_LIBRARIES( + ${test} + ${QT_LIBRARIES} + ${TEST_LIBRARIES} + qjson + ) + if (QJSON_TEST_OUTPUT STREQUAL "xml") + # produce XML output + add_test( ${test} ${test} -xml -o ${test}.tml ) + else (QJSON_TEST_OUTPUT STREQUAL "xml") + add_test( ${test} ${test} ) + endif (QJSON_TEST_OUTPUT STREQUAL "xml") +ENDFOREACH() diff --git a/src/http/qjson/tests/parser/testparser.cpp b/src/http/qjson/tests/parser/testparser.cpp new file mode 100644 index 000000000..e1923a7f3 --- /dev/null +++ b/src/http/qjson/tests/parser/testparser.cpp @@ -0,0 +1,456 @@ +/* This file is part of QJson + * + * Copyright (C) 2008 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include + +#include + +#include + +#include +#include + +#include + +class TestParser: public QObject +{ + Q_OBJECT + private slots: + void parseNonAsciiString(); + void parseSimpleObject(); + void parseEmptyObject(); + void parseEmptyValue(); + void parseUrl(); + void parseMultipleObject(); + + void parseSimpleArray(); + void parseInvalidObject(); + void parseInvalidObject_data(); + void parseMultipleArray(); + + void reuseSameParser(); + + void testTrueFalseNullValues(); + void testEscapeChars(); + void testNumbers(); + void testNumbers_data(); + void testDoubleParsingWithDifferentLocale(); + void testTopLevelValues(); + void testTopLevelValues_data(); + void testReadWrite(); + void testReadWrite_data(); +}; + +Q_DECLARE_METATYPE(QVariant) +Q_DECLARE_METATYPE(QVariant::Type) + +using namespace QJson; + +void TestParser::parseSimpleObject() { + QByteArray json = "{\"foo\":\"bar\"}"; + QVariantMap map; + map.insert (QLatin1String("foo"), QLatin1String("bar")); + QVariant expected(map); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); +} + +void TestParser::parseEmptyObject() { + QByteArray json = "{}"; + QVariantMap map; + QVariant expected (map); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); +} + +void TestParser::parseEmptyValue() { + QByteArray json = "{\"value\": \"\"}"; + + QVariantMap map; + map.insert (QLatin1String("value"), QString(QLatin1String(""))); + QVariant expected (map); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); + QVERIFY (result.toMap().value(QLatin1String("value")).type() == QVariant::String); + + QString value = result.toMap().value(QLatin1String("value")).toString(); + QVERIFY (value.isEmpty()); +} + +void TestParser::parseInvalidObject() { + QFETCH(QByteArray, json); + + Parser parser; + bool ok; + parser.parse (json, &ok); + QVERIFY (!ok); + QVERIFY(!parser.errorString().isEmpty()); +} + +void TestParser::parseInvalidObject_data() { + QTest::addColumn("json"); + + QTest::newRow("unclosed object") << QByteArray("{\"foo\":\"bar\""); + QTest::newRow("infinum (disallow") << QByteArray("Infinum"); + QTest::newRow("Nan (disallow") << QByteArray("NaN"); + QTest::newRow("no data") << QByteArray(""); +} + + +void TestParser::parseNonAsciiString() { + QByteArray json = "{\"artist\":\"Queensr\\u00ffche\"}"; + QVariantMap map; + + QChar unicode_char (0x00ff); + QString unicode_string; + unicode_string.setUnicode(&unicode_char, 1); + unicode_string = QLatin1String("Queensr") + unicode_string + QLatin1String("che"); + + map.insert (QLatin1String("artist"), unicode_string); + QVariant expected (map); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); +} + +void TestParser::parseMultipleObject() { + //put also some extra spaces inside the json string + QByteArray json = "{ \"foo\":\"bar\",\n\"number\" : 51.3 , \"array\":[\"item1\", 123]}"; + QVariantMap map; + map.insert (QLatin1String("foo"), QLatin1String("bar")); + map.insert (QLatin1String("number"), 51.3); + QVariantList list; + list.append (QLatin1String("item1")); + list.append (QLatin1String("123")); + map.insert (QLatin1String("array"), list); + QVariant expected (map); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); + QVERIFY (result.toMap().value(QLatin1String("number")).canConvert()); + QVERIFY (result.toMap().value(QLatin1String("array")).canConvert()); +} + +void TestParser::parseUrl(){ + //"http:\/\/www.last.fm\/venue\/8926427" + QByteArray json = "[\"http:\\/\\/www.last.fm\\/venue\\/8926427\"]"; + QVariantList list; + list.append (QVariant(QLatin1String("http://www.last.fm/venue/8926427"))); + QVariant expected (list); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); +} + + void TestParser::parseSimpleArray() { + QByteArray json = "[\"foo\",\"bar\"]"; + QVariantList list; + list.append (QLatin1String("foo")); + list.append (QLatin1String("bar")); + QVariant expected (list); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); +} + +void TestParser::parseMultipleArray() { + //put also some extra spaces inside the json string + QByteArray json = "[ {\"foo\":\"bar\"},\n\"number\",51.3 , [\"item1\", 123]]"; + QVariantMap map; + map.insert (QLatin1String("foo"), QLatin1String("bar")); + + QVariantList array; + array.append (QLatin1String("item1")); + array.append (123); + + QVariantList list; + list.append (map); + list.append (QLatin1String("number")); + list.append (QLatin1String("51.3")); + list.append ((QVariant) array); + + QVariant expected (list); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); +} + +void TestParser::testTrueFalseNullValues() { + QByteArray json = "[true,false, null, {\"foo\" : true}]"; + QVariantList list; + list.append (QVariant(true)); + list.append (QVariant(false)); + list.append (QVariant()); + QVariantMap map; + map.insert (QLatin1String("foo"), true); + list.append (map); + QVariant expected (list); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result, expected); + QCOMPARE (result.toList().at(0).toBool(), true); + QCOMPARE (result.toList().at(1).toBool(), false); + QVERIFY (result.toList().at(2).isNull()); +} + +void TestParser::testEscapeChars() { + QByteArray json = "[\"\\b \\f \\n \\r \\t \", \" \\\\ \\/ \\\\\", \"http:\\/\\/foo.com\"]"; + + QVariantList list; + list.append (QLatin1String("\b \f \n \r \t ")); + list.append (QLatin1String(" \\ / \\")); + list.append (QLatin1String("http://foo.com")); + + QVariant expected (list); + + Parser parser; + bool ok; + QVariant result = parser.parse (json, &ok); + QVERIFY (ok); + QCOMPARE(result.toList().size(), expected.toList().size() ); + QCOMPARE(result, expected); +} + +void TestParser::testNumbers() { + QFETCH(QByteArray, input); + QFETCH(QVariant, expected); + QFETCH(QVariant::Type, type); + + Parser parser; + bool ok; + QVariant result = parser.parse ('[' + input + ']', &ok); + QVERIFY (ok); + + QVariant value = result.toList().at(0); + QCOMPARE(value, expected); + QCOMPARE( value.type(), type); +} + +void TestParser::testNumbers_data() { + QTest::addColumn( "input" ); + QTest::addColumn( "expected" ); + QTest::addColumn( "type" ); + + QByteArray input; + QVariant output; + + // simple ulonglong + input = QByteArray("1"); + output = QVariant(QVariant::ULongLong); + output.setValue(1); + + QTest::newRow("simple ulonglong") << input << output << QVariant::ULongLong; + + // big number + input = QByteArray("12345678901234567890"); + output = QVariant(QVariant::ULongLong); + output.setValue(12345678901234567890ull); + + QTest::newRow("big number") << input << output << QVariant::ULongLong; + + // simple double + input = QByteArray("2.004"); + output = QVariant(QVariant::Double); + output.setValue(2.004); + + QTest::newRow("simple double") << input << output << QVariant::Double; + + // negative int + input = QByteArray("-100"); + output = QVariant(QVariant::LongLong); + output.setValue(-100); + + QTest::newRow("negative int") << input << output << QVariant::LongLong; + + // negative double + input = QByteArray("-3.4"); + output = QVariant(QVariant::Double); + output.setValue(-3.4); + + QTest::newRow("negative double") << input << output << QVariant::Double; +} + +void TestParser::testTopLevelValues() { + QFETCH(QByteArray, input); + QFETCH(QVariant, expected); + QFETCH(QVariant::Type, type); + + Parser parser; + bool ok; + QVariant result = parser.parse (input, &ok); + QVERIFY (ok); + + QCOMPARE(result, expected); + QCOMPARE(result.type(), type); +} + +void TestParser::testTopLevelValues_data() { + QTest::addColumn( "input" ); + QTest::addColumn( "expected" ); + QTest::addColumn( "type" ); + + QByteArray input; + QVariant output; + + // string + input = QByteArray("\"foo bar\""); + output = QVariant(QLatin1String("foo bar")); + QTest::newRow("string") << input << output << QVariant::String; + + // number + input = QByteArray("2.4"); + output = QVariant(QVariant::Double); + output.setValue(2.4); + QTest::newRow("simple double") << input << output << QVariant::Double; + + // boolean + input = QByteArray("true"); + output = QVariant(QVariant::Bool); + output.setValue(true); + QTest::newRow("bool") << input << output << QVariant::Bool; + + // null + input = QByteArray("null"); + output = QVariant(); + QTest::newRow("null") << input << output << QVariant::Invalid; + + // array + input = QByteArray("[1,2,3]"); + QVariantList list; + list << QVariant(1) << QVariant(2) << QVariant(3); + output = QVariant(QVariant::List); + output.setValue(list); + QTest::newRow("array") << input << output << QVariant::List; + + // object + input = QByteArray("{\"foo\" : \"bar\"}"); + QVariantMap map; + map.insert(QLatin1String("foo"), QLatin1String("bar")); + output = QVariant(QVariant::Map); + output.setValue(map); + QTest::newRow("object") << input << output << QVariant::Map; +} + +void TestParser::testDoubleParsingWithDifferentLocale() { + QLocale oldLocale; + QLocale itLocale(QLatin1String("it_IT.utf8")); + + QCOMPARE(itLocale.name(), QLatin1String("it_IT") ); + + // the Italian locale uses ',' as digit separator. + QLocale::setDefault(itLocale); + + Parser parser; + bool ok; + QVariant result = parser.parse ("12.3", &ok); + QVERIFY (ok); + + QCOMPARE(result.toDouble(), 12.3); + + QLocale::setDefault(oldLocale); +} + +void TestParser::testReadWrite() +{ + QFETCH( QVariant, variant ); + Serializer serializer; + bool ok; + + QByteArray json = serializer.serialize(variant, &ok); + QVERIFY(ok); + + Parser parser; + QVariant result = parser.parse( json, &ok ); + QVERIFY(ok); + QCOMPARE( result, variant ); +} + +void TestParser::testReadWrite_data() +{ + QTest::addColumn( "variant" ); + + // array tests + QTest::newRow( "empty array" ) << QVariant(QVariantList()); + + // basic array + QVariantList list; + list << QString(QLatin1String("hello")); + list << 12; + QTest::newRow( "basic array" ) << QVariant(list); + + // simple map + QVariantMap map; + map[QString(QLatin1String("Name"))] = 32; + QTest::newRow( "complicated array" ) << QVariant(map); +} + +void TestParser::reuseSameParser() +{ + Parser parser; + bool ok; + + parser.parse ("12.3", &ok); + QVERIFY (ok); + + parser.parse ("wrong entry", &ok); + QVERIFY (!ok); + + parser.parse ("12.3", &ok); + QVERIFY (ok); +} + +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) +// using Qt4 rather then Qt5 +QTEST_MAIN(TestParser) +#include "moc_testparser.cxx" +#else +QTEST_GUILESS_MAIN(TestParser) +#include "testparser.moc" +#endif diff --git a/src/http/qjson/tests/qobjecthelper/.gitignore b/src/http/qjson/tests/qobjecthelper/.gitignore new file mode 100644 index 000000000..e64e23248 --- /dev/null +++ b/src/http/qjson/tests/qobjecthelper/.gitignore @@ -0,0 +1,5 @@ +Makefile +*.o +*.moc +moc_* +qobjecthelper diff --git a/src/http/qjson/tests/qobjecthelper/CMakeLists.txt b/src/http/qjson/tests/qobjecthelper/CMakeLists.txt new file mode 100644 index 000000000..d1636e6fb --- /dev/null +++ b/src/http/qjson/tests/qobjecthelper/CMakeLists.txt @@ -0,0 +1,55 @@ +##### Probably don't want to edit below this line ##### + +SET( QT_USE_QTTEST TRUE ) + +IF (NOT Qt5Core_FOUND) + # Use it + INCLUDE( ${QT_USE_FILE} ) +ENDIF() + +INCLUDE(AddFileDependencies) + +# Include the library include directories, and the current build directory (moc) +INCLUDE_DIRECTORIES( + ../../include + ${CMAKE_CURRENT_BINARY_DIR} +) + +SET (qjson_test_support_SRCS person.cpp) +IF (NOT Qt5Core_FOUND) + QT4_WRAP_CPP(qjson_test_support_MOC_SRCS person.h) +ENDIF() + +ADD_LIBRARY (qjson_test_support STATIC ${qjson_test_support_SRCS} + ${qjson_test_support_MOC_SRCS}) + +SET( UNIT_TESTS + testqobjecthelper +) + +# Build the tests +FOREACH(test ${UNIT_TESTS}) + MESSAGE(STATUS "Building ${test}") + IF (NOT Qt5Core_FOUND) + QT4_WRAP_CPP(MOC_SOURCE ${test}.cpp) + ENDIF() + ADD_EXECUTABLE( + ${test} + ${test}.cpp + ) + + ADD_FILE_DEPENDENCIES(${test}.cpp ${MOC_SOURCE}) + TARGET_LINK_LIBRARIES( + ${test} + ${QT_LIBRARIES} + ${TEST_LIBRARIES} + qjson + qjson_test_support + ) + if (QJSON_TEST_OUTPUT STREQUAL "xml") + # produce XML output + add_test( ${test} ${test} -xml -o ${test}.tml ) + else (QJSON_TEST_OUTPUT STREQUAL "xml") + add_test( ${test} ${test} ) + endif (QJSON_TEST_OUTPUT STREQUAL "xml") +ENDFOREACH() diff --git a/src/http/qjson/tests/qobjecthelper/person.cpp b/src/http/qjson/tests/qobjecthelper/person.cpp new file mode 100644 index 000000000..23291798b --- /dev/null +++ b/src/http/qjson/tests/qobjecthelper/person.cpp @@ -0,0 +1,75 @@ +#include "person.h" + +Person::Person(QObject* parent) + : QObject(parent), + m_name(), + m_phoneNumber(0), + m_gender(Female), + m_luckyNumber(0) +{ +} + +Person::~Person() +{ +} + +QString Person::name() const +{ + return m_name; +} + +void Person::setName(const QString& name) +{ + m_name = name; +} + +int Person::phoneNumber() const +{ + return m_phoneNumber; +} + +void Person::setPhoneNumber(const int phoneNumber) +{ + m_phoneNumber = phoneNumber; +} + +void Person::setGender(Gender gender) +{ + m_gender = gender; +} + +Person::Gender Person::gender() const +{ + return m_gender; +} + +QDate Person::dob() const +{ + return m_dob; +} + +void Person::setDob(const QDate& dob) +{ + m_dob = dob; +} + +QVariant Person::customField() const +{ + return m_customField; +} + +void Person::setCustomField(const QVariant& customField) +{ + m_customField = customField; +} + +const quint16 Person::luckyNumber() const +{ + return m_luckyNumber; +} + +void Person::setLuckyNumber(const quint16 luckyNumber) +{ + m_luckyNumber = luckyNumber; +} + diff --git a/src/http/qjson/tests/qobjecthelper/person.h b/src/http/qjson/tests/qobjecthelper/person.h new file mode 100644 index 000000000..3026bf782 --- /dev/null +++ b/src/http/qjson/tests/qobjecthelper/person.h @@ -0,0 +1,73 @@ +/* This file is part of qjson + * + * Copyright (C) 2009 Till Adam + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef PERSON_H +#define PERSON_H + +#include +#include +#include +#include + +class Person : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber) + Q_PROPERTY(Gender gender READ gender WRITE setGender) + Q_PROPERTY(QDate dob READ dob WRITE setDob) + Q_PROPERTY(QVariant customField READ customField WRITE setCustomField) + Q_PROPERTY(quint16 luckyNumber READ luckyNumber WRITE setLuckyNumber) + Q_ENUMS(Gender) + + public: + Person(QObject* parent = 0); + ~Person(); + + QString name() const; + void setName(const QString& name); + + int phoneNumber() const; + void setPhoneNumber(const int phoneNumber); + + enum Gender {Male, Female}; + void setGender(Gender gender); + Gender gender() const; + + QDate dob() const; + void setDob(const QDate& dob); + + QVariant customField() const; + void setCustomField(const QVariant& customField); + + const quint16 luckyNumber() const; + void setLuckyNumber(const quint16 luckyNumber); + + private: + QString m_name; + int m_phoneNumber; + Gender m_gender; + QDate m_dob; + QVariant m_customField; + quint16 m_luckyNumber; +}; + +#endif diff --git a/src/http/qjson/tests/qobjecthelper/testqobjecthelper.cpp b/src/http/qjson/tests/qobjecthelper/testqobjecthelper.cpp new file mode 100644 index 000000000..2abb9e91e --- /dev/null +++ b/src/http/qjson/tests/qobjecthelper/testqobjecthelper.cpp @@ -0,0 +1,126 @@ + +/* This file is part of QJson + * + * Copyright (C) 2009 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include + +#include +#include + +#include + +#include +#include +#include + +#include "person.h" + +class TestQObjectHelper: public QObject +{ + Q_OBJECT + private slots: + void testQObject2QVariant(); + void testQVariant2QObject(); +}; + +using namespace QJson; + +void TestQObjectHelper::testQObject2QVariant() +{ + QString name = QLatin1String("Flavio Castelli"); + int phoneNumber = 123456; + Person::Gender gender = Person::Male; + QDate dob (1982, 7, 12); + QVariantList nicknames; + nicknames << QLatin1String("nickname1") << QLatin1String("nickname2"); + quint16 luckyNumber = 123; + + Person person; + person.setName(name); + person.setPhoneNumber(phoneNumber); + person.setGender(gender); + person.setDob(dob); + person.setCustomField(nicknames); + person.setLuckyNumber(luckyNumber); + + QVariantMap expected; + expected[QLatin1String("name")] = QVariant(name); + expected[QLatin1String("phoneNumber")] = QVariant(phoneNumber); + expected[QLatin1String("gender")] = QVariant(gender); + expected[QLatin1String("dob")] = QVariant(dob); + expected[QLatin1String("customField")] = nicknames; + expected[QLatin1String("luckyNumber")] = luckyNumber; + + QVariantMap result = QObjectHelper::qobject2qvariant(&person); + QCOMPARE(result, expected); +} + +void TestQObjectHelper::testQVariant2QObject() +{ + bool ok; + QString name = QLatin1String("Flavio Castelli"); + int phoneNumber = 123456; + Person::Gender gender = Person::Male; + QDate dob (1982, 7, 12); + QVariantList nicknames; + nicknames << QLatin1String("nickname1") << QLatin1String("nickname2"); + quint16 luckyNumber = 123; + + Person expected_person; + expected_person.setName(name); + expected_person.setPhoneNumber(phoneNumber); + expected_person.setGender(gender); + expected_person.setDob(dob); + expected_person.setCustomField(nicknames); + expected_person.setLuckyNumber(luckyNumber); + + QVariantMap variant = QObjectHelper::qobject2qvariant(&expected_person); + + Serializer serializer; + QByteArray json = serializer.serialize(variant, &ok); + qDebug() << "json is" << json; + QVERIFY(ok); + + Parser parser; + QVariant parsedVariant = parser.parse(json,&ok); + QVERIFY(ok); + qDebug() << parsedVariant; + QVERIFY(parsedVariant.canConvert(QVariant::Map)); + + Person person; + QCOMPARE(Person::Female, person.gender()); + QObjectHelper::qvariant2qobject(parsedVariant.toMap(), &person); + + QCOMPARE(person.name(), name); + QCOMPARE(person.phoneNumber(), phoneNumber); + QCOMPARE(person.gender(), gender); + QCOMPARE(person.dob(), dob); + QCOMPARE(person.customField(), QVariant(nicknames)); + QCOMPARE(person.luckyNumber(), luckyNumber); +} + +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) +// using Qt4 rather then Qt5 +QTEST_MAIN(TestQObjectHelper) +#include "moc_testqobjecthelper.cxx" +#else +QTEST_GUILESS_MAIN(TestQObjectHelper) +#include "testqobjecthelper.moc" +#endif diff --git a/src/http/qjson/tests/scanner/CMakeLists.txt b/src/http/qjson/tests/scanner/CMakeLists.txt new file mode 100644 index 000000000..185231e6b --- /dev/null +++ b/src/http/qjson/tests/scanner/CMakeLists.txt @@ -0,0 +1,47 @@ +##### Probably don't want to edit below this line ##### + +SET( QT_USE_QTTEST TRUE ) + +IF (NOT Qt5Core_FOUND) + # Use it + INCLUDE( ${QT_USE_FILE} ) +ENDIF() + +INCLUDE(AddFileDependencies) + +# Include the library include directories, and the current build directory (moc) +INCLUDE_DIRECTORIES( + ../../src + ../../include + ${CMAKE_CURRENT_BINARY_DIR} +) + +SET( UNIT_TESTS + testscanner +) + +# Build the tests +FOREACH(test ${UNIT_TESTS}) + MESSAGE(STATUS "Building ${test}") + IF (NOT Qt5Core_FOUND) + QT4_WRAP_CPP(MOC_SOURCE ${test}.cpp) + ENDIF() + ADD_EXECUTABLE( + ${test} + ${test}.cpp + ) + + ADD_FILE_DEPENDENCIES(${test}.cpp ${MOC_SOURCE}) + TARGET_LINK_LIBRARIES( + ${test} + ${QT_LIBRARIES} + ${TEST_LIBRARIES} + qjson + ) + if (QJSON_TEST_OUTPUT STREQUAL "xml") + # produce XML output + add_test( ${test} ${test} -xml -o ${test}.tml ) + else (QJSON_TEST_OUTPUT STREQUAL "xml") + add_test( ${test} ${test} ) + endif (QJSON_TEST_OUTPUT STREQUAL "xml") +ENDFOREACH() diff --git a/src/http/qjson/tests/scanner/testscanner.cpp b/src/http/qjson/tests/scanner/testscanner.cpp new file mode 100644 index 000000000..456c12fe7 --- /dev/null +++ b/src/http/qjson/tests/scanner/testscanner.cpp @@ -0,0 +1,256 @@ +/* This file is part of QJson + * + * Copyright (C) 2013 Silvio Moioli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include + +#include +#include + +#include + +#include "json_scanner.h" +#include "json_parser.hh" +#include "location.hh" + +#define TOKEN(type) (int)yy::json_parser::token::type + +class TestScanner: public QObject +{ + Q_OBJECT + private slots: + void scanClosedDevice(); + void scanTokens(); + void scanTokens_data(); + void scanSpecialNumbers(); + void scanSpecialNumbers_data(); +}; + +Q_DECLARE_METATYPE(QVariant) +Q_DECLARE_METATYPE(QVariant::Type) + +using namespace QJson; + +void TestScanner::scanClosedDevice() { + QBuffer buffer; + int expectedResult = -1; + + JSonScanner scanner(&buffer); + QVariant yylval; + yy::location location; + int result = scanner.yylex(&yylval, &location); + QCOMPARE(result, expectedResult); +} + +void TestScanner::scanTokens() { + QFETCH(QByteArray, input); + QFETCH(bool, allowSpecialNumbers); + QFETCH(bool, skipFirstToken); + QFETCH(int, expectedResult); + QFETCH(QVariant, expectedYylval); + QFETCH(int, expectedLocationBeginLine); + QFETCH(int, expectedLocationBeginColumn); + QFETCH(int, expectedLocationEndLine); + QFETCH(int, expectedLocationEndColumn); + + QBuffer buffer; + buffer.open(QBuffer::ReadWrite); + buffer.write(input); + buffer.seek(0); + JSonScanner scanner(&buffer); + scanner.allowSpecialNumbers(allowSpecialNumbers); + + QVariant yylval; + yy::position position(YY_NULL, 1, 0); + yy::location location(position, position); + int result = scanner.yylex(&yylval, &location); + + if (skipFirstToken) { + result = scanner.yylex(&yylval, &location); + } + + QCOMPARE(result, expectedResult); + QCOMPARE(yylval, expectedYylval); + QCOMPARE(location.begin.line, (uint)expectedLocationBeginLine); + QCOMPARE(location.begin.column, (uint)expectedLocationBeginColumn); + QCOMPARE(location.end.line, (uint)expectedLocationEndLine); + QCOMPARE(location.end.column, (uint)expectedLocationEndColumn); +} + +void TestScanner::scanTokens_data() { + QTest::addColumn("input"); + QTest::addColumn("allowSpecialNumbers"); + QTest::addColumn("skipFirstToken"); + QTest::addColumn("expectedResult"); + QTest::addColumn("expectedYylval"); + QTest::addColumn("expectedLocationBeginLine"); + QTest::addColumn("expectedLocationBeginColumn"); + QTest::addColumn("expectedLocationEndLine"); + QTest::addColumn("expectedLocationEndColumn"); + + QTest::newRow("empty string") << QByteArray("") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 1 << 0; + + QTest::newRow("carriage return") << QByteArray("\r") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 2 << 1; + QTest::newRow("new line") << QByteArray("\n") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 2 << 1; + QTest::newRow("formfeed") << QByteArray("\f") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("vertical tab") << QByteArray("\v") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("space") << QByteArray(" ") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("tab") << QByteArray("\t") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("all spaces") << QByteArray("\r\n\f\v \t") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 3 << 5; + + QTest::newRow("true") << QByteArray("true") << true << false << TOKEN(TRUE_VAL) << QVariant(true) << 1 << 0 << 1 << 4; + QTest::newRow("false") << QByteArray("false") << true << false << TOKEN(FALSE_VAL) << QVariant(false) << 1 << 0 << 1 << 5; + QTest::newRow("null") << QByteArray("null") << true << false << TOKEN(NULL_VAL) << QVariant() << 1 << 0 << 1 << 4; + + QTest::newRow("alphabetic string") << QByteArray("\"abcde\"") << true << false << TOKEN(STRING) << QVariant(QLatin1String("abcde")) << 1 << 0 << 1 << 2; + QTest::newRow("ecaped string") << QByteArray("\"abcde\\b\\f\\n\\r\\t\"") << true << false << TOKEN(STRING) << QVariant(QLatin1String("abcde\b\f\n\r\t")) << 1 << 0 << 1 << 2; + QTest::newRow("invalid ecaped string") << QByteArray("\"\\x\"") << true << false << TOKEN(STRING) << QVariant(QLatin1String("x")) << 1 << 0 << 1 << 2; + QTest::newRow("escaped unicode sequence") << QByteArray("\"\\u005A\"") << true << false << TOKEN(STRING) << QVariant(QLatin1String("Z")) << 1 << 0 << 1 << 2; + QTest::newRow("invalid unicode sequence") << QByteArray("\"\\u005Z\"") << true << false << TOKEN(INVALID) << QVariant(QLatin1String("")) << 1 << 0 << 1 << 2; + QTest::newRow("empty string") << QByteArray("\"") << true << false << TOKEN(END) << QVariant() << 1 << 0 << 1 << 1; + + QTest::newRow("single digit") << QByteArray("0") << true << false << TOKEN(NUMBER) << QVariant(0u) << 1 << 0 << 1 << 1; + QTest::newRow("multiple digits") << QByteArray("123456789") << true << false << TOKEN(NUMBER) << QVariant(123456789u) << 1 << 0 << 1 << 9; + QTest::newRow("negative single digit") << QByteArray("-0") << true << false << TOKEN(NUMBER) << QVariant(0) << 1 << 0 << 1 << 2; + QTest::newRow("negative multiple digits") << QByteArray("-123456789") << true << false << TOKEN(NUMBER) << QVariant(-123456789) << 1 << 0 << 1 << 10; + QTest::newRow("fractional single digit") << QByteArray("0.1") << true << false << TOKEN(NUMBER) << QVariant(0.1) << 1 << 0 << 1 << 3; + QTest::newRow("fractional multiple digits") << QByteArray("123456789.12") << true << false << TOKEN(NUMBER) << QVariant(123456789.12) << 1 << 0 << 1 << 12; + QTest::newRow("fractional negative single digit") << QByteArray("-0.3") << true << false << TOKEN(NUMBER) << QVariant(-0.3) << 1 << 0 << 1 << 4; + QTest::newRow("fractional negative multiple digits") << QByteArray("-123456789.23") << true << false << TOKEN(NUMBER) << QVariant(-123456789.23) << 1 << 0 << 1 << 13; + QTest::newRow("exponential single digit") << QByteArray("10e2") << true << false << TOKEN(NUMBER) << QVariant(1000) << 1 << 0 << 1 << 4; + QTest::newRow("exponential multiple digits") << QByteArray("10e23") << true << false << TOKEN(NUMBER) << QVariant(10e23) << 1 << 0 << 1 << 5; + QTest::newRow("exponential zero") << QByteArray("0e23") << true << false << TOKEN(NUMBER) << QVariant(0) << 1 << 0 << 1 << 4; + QTest::newRow("exponential fractional") << QByteArray("0.12354e23") << true << false << TOKEN(NUMBER) << QVariant(0.12354e23) << 1 << 0 << 1 << 10; + QTest::newRow("exponential fractional multiple digits") << QByteArray("120.12354e23") << true << false << TOKEN(NUMBER) << QVariant(120.12354e23) << 1 << 0 << 1 << 12; + QTest::newRow("uppercase exponential") << QByteArray("120.12354E23") << true << false << TOKEN(NUMBER) << QVariant(120.12354E23) << 1 << 0 << 1 << 12; + QTest::newRow("negative exponential single digit") << QByteArray("-10e2") << true << false << TOKEN(NUMBER) << QVariant(-1000) << 1 << 0 << 1 << 5; + QTest::newRow("negative exponential multiple digits") << QByteArray("-10e23") << true << false << TOKEN(NUMBER) << QVariant(-10e23) << 1 << 0 << 1 << 6; + QTest::newRow("negative exponential zero") << QByteArray("-0e23") << true << false << TOKEN(NUMBER) << QVariant(0) << 1 << 0 << 1 << 5; + QTest::newRow("negative exponential fractional") << QByteArray("-0.12354e23") << true << false << TOKEN(NUMBER) << QVariant(-0.12354e23) << 1 << 0 << 1 << 11; + QTest::newRow("negative exponential fractional multiple digits") << QByteArray("-120.12354e23") << true << false << TOKEN(NUMBER) << QVariant(-120.12354e23) << 1 << 0 << 1 << 13; + QTest::newRow("negative exponent") << QByteArray("10e-2") << true << false << TOKEN(NUMBER) << QVariant(10e-2) << 1 << 0 << 1 << 5; + QTest::newRow("positive exponent with plus") << QByteArray("10e+2") << true << false << TOKEN(NUMBER) << QVariant(1000) << 1 << 0 << 1 << 5; + + QTest::newRow("invalid multiple digits") << QByteArray("001") << true << false << TOKEN(NUMBER) << QVariant(0) << 1 << 0 << 1 << 1; + QTest::newRow("invalid negative multiple digits") << QByteArray("-001") << true << false << TOKEN(NUMBER) << QVariant(0) << 1 << 0 << 1 << 2; + QTest::newRow("invalid fractional") << QByteArray("12.") << true << true << TOKEN(INVALID) << QVariant(12) << 1 << 2 << 1 << 3; + QTest::newRow("invalid exponential 1") << QByteArray("-5e+") << true << true << TOKEN(INVALID) << QVariant(-5) << 1 << 2 << 1 << 3; + QTest::newRow("invalid exponential 2") << QByteArray("2e") << true << true << TOKEN(INVALID) << QVariant(2) << 1 << 1 << 1 << 2; + QTest::newRow("invalid exponential 3") << QByteArray("3e+") << true << true << TOKEN(INVALID) << QVariant(3) << 1 << 1 << 1 << 2; + QTest::newRow("invalid exponential 4") << QByteArray("4.3E") << true << true << TOKEN(INVALID) << QVariant(4.3) << 1 << 3 << 1 << 4; + QTest::newRow("invalid exponential 5") << QByteArray("5.4E-") << true << true << TOKEN(INVALID) << QVariant(5.4) << 1 << 3 << 1 << 4; + + QTest::newRow("colon") << QByteArray(":") << true << false << TOKEN(COLON) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("comma") << QByteArray(",") << true << false << TOKEN(COMMA) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("square bracket open") << QByteArray("[") << true << false << TOKEN(SQUARE_BRACKET_OPEN) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("square bracket close") << QByteArray("]") << true << false << TOKEN(SQUARE_BRACKET_CLOSE) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("curly bracket open") << QByteArray("{") << true << false << TOKEN(CURLY_BRACKET_OPEN) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("curly bracket close") << QByteArray("}") << true << false << TOKEN(CURLY_BRACKET_CLOSE) << QVariant() << 1 << 0 << 1 << 1; + + QTest::newRow("too large unsinged number") << QByteArray("18446744073709551616") << false << false << TOKEN(INVALID) << QVariant(ULLONG_MAX) << 1 << 0 << 1 << 20; + QTest::newRow("too large signed number") << QByteArray("-9223372036854775808") << false << false << TOKEN(INVALID) << QVariant(LLONG_MIN) << 1 << 0 << 1 << 20; + QTest::newRow("too large exponential") << QByteArray("1.7976931348623157e309") << false << false << TOKEN(INVALID) << QVariant(0) << 1 << 0 << 1 << 22; + QTest::newRow("not allowed nan") << QByteArray("nan") << false << false << TOKEN(INVALID) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("not allowed infinity") << QByteArray("Infinity") << false << false << TOKEN(INVALID) << QVariant() << 1 << 0 << 1 << 1; + QTest::newRow("unknown") << QByteArray("*") << true << false << TOKEN(INVALID) << QVariant() << 1 << 0 << 1 << 1; +} + + +void TestScanner::scanSpecialNumbers() { + QFETCH(QByteArray, input); + QFETCH(bool, isInfinity); + QFETCH(bool, isNegative); + QFETCH(bool, isNan); + QFETCH(int, expectedLocationBeginLine); + QFETCH(int, expectedLocationBeginColumn); + QFETCH(int, expectedLocationEndLine); + QFETCH(int, expectedLocationEndColumn); + + QBuffer buffer; + buffer.open(QBuffer::ReadWrite); + buffer.write(input); + buffer.seek(0); + JSonScanner scanner(&buffer); + scanner.allowSpecialNumbers(true); + + QVariant yylval; + yy::position position(YY_NULL, 1, 0); + yy::location location(position, position); + int result = scanner.yylex(&yylval, &location); + + QCOMPARE(result, TOKEN(NUMBER)); + QVERIFY(yylval.type() == QVariant::Double); + + double doubleResult = yylval.toDouble(); + + #if defined(Q_OS_SYMBIAN) || defined(Q_OS_ANDROID) || defined(Q_OS_BLACKBERRY) + QCOMPARE(bool(isinf(doubleResult)), isInfinity); + #else + // skip this test for MSVC, because there is no "isinf" function. + #ifndef Q_CC_MSVC + QCOMPARE(bool(std::isinf(doubleResult)), isInfinity); + #endif + #endif + + QCOMPARE(doubleResult<0, isNegative); + + #if defined(Q_OS_SYMBIAN) || defined(Q_OS_ANDROID) || defined(Q_OS_BLACKBERRY) + QCOMPARE(bool(isnan(doubleResult)), isNan); + #else + // skip this test for MSVC, because there is no "isinf" function. + #ifndef Q_CC_MSVC + QCOMPARE(bool(std::isnan(doubleResult)), isNan); + #endif + #endif + + QCOMPARE(location.begin.line, (uint)expectedLocationBeginLine); + QCOMPARE(location.begin.column, (uint)expectedLocationBeginColumn); + QCOMPARE(location.end.line, (uint)expectedLocationEndLine); + QCOMPARE(location.end.column, (uint)expectedLocationEndColumn); +} + +void TestScanner::scanSpecialNumbers_data() { + QTest::addColumn("input"); + QTest::addColumn("isInfinity"); + QTest::addColumn("isNegative"); + QTest::addColumn("isNan"); + QTest::addColumn("expectedLocationBeginLine"); + QTest::addColumn("expectedLocationBeginColumn"); + QTest::addColumn("expectedLocationEndLine"); + QTest::addColumn("expectedLocationEndColumn"); + + QTest::newRow("nan") << QByteArray("nan") << false << false << true << 1 << 0 << 1 << 3; + QTest::newRow("NAN") << QByteArray("NAN") << false << false << true << 1 << 0 << 1 << 3; + QTest::newRow("NaN") << QByteArray("NaN") << false << false << true << 1 << 0 << 1 << 3; + + QTest::newRow("infinity") << QByteArray("infinity") << true << false << false << 1 << 0 << 1 << 8; + QTest::newRow("Infinity") << QByteArray("infinity") << true << false << false << 1 << 0 << 1 << 8; + + QTest::newRow("-infinity") << QByteArray("-infinity") << true << true << false << 1 << 0 << 1 << 9; + QTest::newRow("-Infinity") << QByteArray("-Infinity") << true << true << false << 1 << 0 << 1 << 9; +} + +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) +// using Qt4 rather then Qt5 +QTEST_MAIN(TestScanner) +#include "moc_testscanner.cxx" +#else +QTEST_GUILESS_MAIN(TestScanner) +#include "testscanner.moc" +#endif diff --git a/src/http/qjson/tests/serializer/.gitignore b/src/http/qjson/tests/serializer/.gitignore new file mode 100644 index 000000000..4a080f160 --- /dev/null +++ b/src/http/qjson/tests/serializer/.gitignore @@ -0,0 +1,4 @@ +Makefile +*.o +*.moc +serializer diff --git a/src/http/qjson/tests/serializer/CMakeLists.txt b/src/http/qjson/tests/serializer/CMakeLists.txt new file mode 100644 index 000000000..c3411bc75 --- /dev/null +++ b/src/http/qjson/tests/serializer/CMakeLists.txt @@ -0,0 +1,46 @@ +##### Probably don't want to edit below this line ##### + +SET( QT_USE_QTTEST TRUE ) + +IF (NOT Qt5Core_FOUND) + # Use it + INCLUDE( ${QT_USE_FILE} ) +ENDIF() + +INCLUDE(AddFileDependencies) + +# Include the library include directories, and the current build directory (moc) +INCLUDE_DIRECTORIES( + ../../include + ${CMAKE_CURRENT_BINARY_DIR} +) + +SET( UNIT_TESTS + testserializer +) + +# Build the tests +FOREACH(test ${UNIT_TESTS}) + MESSAGE(STATUS "Building ${test}") + IF (NOT Qt5Core_FOUND) + QT4_WRAP_CPP(MOC_SOURCE ${test}.cpp) + ENDIF() + ADD_EXECUTABLE( + ${test} + ${test}.cpp + ) + + ADD_FILE_DEPENDENCIES(${test}.cpp ${MOC_SOURCE}) + TARGET_LINK_LIBRARIES( + ${test} + ${QT_LIBRARIES} + ${TEST_LIBRARIES} + qjson + ) + if (QJSON_TEST_OUTPUT STREQUAL "xml") + # produce XML output + add_test( ${test} ${test} -xml -o ${test}.tml ) + else (QJSON_TEST_OUTPUT STREQUAL "xml") + add_test( ${test} ${test} ) + endif (QJSON_TEST_OUTPUT STREQUAL "xml") +ENDFOREACH() diff --git a/src/http/qjson/tests/serializer/testserializer.cpp b/src/http/qjson/tests/serializer/testserializer.cpp new file mode 100644 index 000000000..9a6e8fb34 --- /dev/null +++ b/src/http/qjson/tests/serializer/testserializer.cpp @@ -0,0 +1,542 @@ +/* This file is part of QJson + * + * Copyright (C) 2009 Flavio Castelli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include + +#include + +#include + +#include +#include + +class TestSerializer: public QObject +{ + Q_OBJECT + private slots: + void testReadWriteEmptyDocument(); + void testReadWrite(); + void testReadWrite_data(); + void testValueNull(); + void testValueString(); + void testValueString_data(); + void testValueStringList(); + void testValueStringList_data(); + void testValueHashMap(); + void testValueInteger(); + void testValueInteger_data(); + void testValueDouble(); + void testValueDouble_data(); + void testSetDoublePrecision(); + void testValueFloat(); + void testValueFloat_data(); + void testValueBoolean(); + void testValueBoolean_data(); + void testSpecialNumbers(); + void testSpecialNumbers_data(); + void testIndentation(); + void testIndentation_data(); + void testSerializetoQIODevice(); + void testSerializeWithoutOkParam(); + + private: + void valueTest( const QVariant& value, const QString& expectedRegExp, bool errorExpected = false ); + void valueTest( const QObject* object, const QString& expectedRegExp ); +}; + +Q_DECLARE_METATYPE(QVariant) + +using namespace QJson; + +void TestSerializer::testReadWriteEmptyDocument() +{ + QByteArray json = ""; + Parser parser; + bool ok; + QVariant result = parser.parse( json, &ok ); + QVERIFY(!ok); + QVERIFY( ! result.isValid() ); + Serializer serializer; + const QByteArray serialized = serializer.serialize( result, &ok); + QVERIFY( ok ); + QByteArray expected = "null"; + QCOMPARE(expected, serialized); +} + +void TestSerializer::testReadWrite() +{ + QFETCH( QByteArray, json ); + Parser parser; + bool ok; + QVariant result = parser.parse( json, &ok ); + QVERIFY(ok); + Serializer serializer; + const QByteArray serialized = serializer.serialize( result, &ok); + QVERIFY(ok); + QVariant writtenThenRead = parser.parse( serialized, &ok ); + QVERIFY(ok); + QCOMPARE( result, writtenThenRead ); +} + +void TestSerializer::testReadWrite_data() +{ + QTest::addColumn( "json" ); + + // array tests + QTest::newRow( "empty array" ) << QByteArray("[]"); + QTest::newRow( "basic array" ) << QByteArray("[\"person\",\"bar\"]"); + QTest::newRow( "single int array" ) << QByteArray("[6]"); + QTest::newRow( "int array" ) << QByteArray("[6,5,6,7]"); + const QByteArray json = "[1,2.4, -100, -3.4, -5e+0, 2e0,3e+0,4.3E0,5.4E-0]"; + QTest::newRow( QByteArray("array of various numbers") ) << json; + + // document tests + QTest::newRow( "empty object" ) << QByteArray("{}"); + QTest::newRow( "basic document" ) << QByteArray("{\"person\":\"bar\"}"); + QTest::newRow( "object with ints" ) << QByteArray("{\"person\":6}"); + const QByteArray json2 = "{ \"person\":\"bar\",\n\"number\" : 51.3 , \"array\":[\"item1\", 123]}"; + QTest::newRow( "complicated document" ) << json2; + + // more complex cases + const QByteArray json3 = "[ {\"person\":\"bar\"},\n\"number\",51.3 , [\"item1\", 123]]"; + QTest::newRow( "complicated array" ) << json3; +} + +void TestSerializer::testIndentation() +{ + QFETCH( QByteArray, json ); + QFETCH( QByteArray, expected_compact ); + QFETCH( QByteArray, expected_min ); + QFETCH( QByteArray, expected_med ); + QFETCH( QByteArray, expected_full ); + + // parse + Parser parser; + bool ok; + QVariant parsed = parser.parse( json, &ok ); + QVERIFY(ok); + + Serializer serializer; + QVariant reparsed; + QByteArray serialized; + + // serialize with indent compact and reparse + serializer.setIndentMode(QJson::IndentCompact); + serialized = serializer.serialize( parsed, &ok); + QVERIFY(ok); + QCOMPARE( serialized, expected_compact); + reparsed = parser.parse( serialized, &ok); + QVERIFY(ok); + QCOMPARE( parsed, reparsed); + + // serialize with indent minimum and reparse + serializer.setIndentMode(QJson::IndentMinimum); + serialized = serializer.serialize( parsed, &ok); + QVERIFY(ok); + QCOMPARE( serialized, expected_min); + reparsed = parser.parse( serialized, &ok); + QVERIFY(ok); + QCOMPARE( parsed, reparsed); + + // serialize with indent medium and reparse + serializer.setIndentMode(QJson::IndentMedium); + serialized = serializer.serialize( parsed, &ok); + QVERIFY(ok); + QCOMPARE( serialized, expected_med); + reparsed = parser.parse( serialized, &ok ); + QVERIFY(ok); + QCOMPARE( parsed, reparsed); + + // serialize with indent full and reparse + serializer.setIndentMode(QJson::IndentFull); + serialized = serializer.serialize( parsed, &ok); + QVERIFY(ok); + QCOMPARE( serialized, expected_full); + reparsed = parser.parse( serialized, &ok ); + QVERIFY(ok); + QCOMPARE( parsed, reparsed); +} + +void TestSerializer::testIndentation_data() +{ + QTest::addColumn( "json" ); + QTest::addColumn( "expected_compact" ); + QTest::addColumn( "expected_min" ); + QTest::addColumn( "expected_med" ); + QTest::addColumn( "expected_full" ); + const QByteArray json = " { \"foo\" : 0, \"foo1\" : 1, \"foo2\" : [ { \"bar\" : 1, \"foo\" : 0, \"foobar\" : 0 }, { \"bar\" : 1, \"foo\" : 1, \"foobar\" : 1 } ], \"foo3\" : [ 1, 2, 3, 4, 5, 6 ] }"; + const QByteArray ex_compact = "{\"foo\":0,\"foo1\":1,\"foo2\":[{\"bar\":1,\"foo\":0,\"foobar\":0},{\"bar\":1,\"foo\":1,\"foobar\":1}],\"foo3\":[1,2,3,4,5,6]}"; + const QByteArray ex_min = "{ \"foo\" : 0, \"foo1\" : 1, \"foo2\" : [\n { \"bar\" : 1, \"foo\" : 0, \"foobar\" : 0 },\n { \"bar\" : 1, \"foo\" : 1, \"foobar\" : 1 }\n ], \"foo3\" : [\n 1,\n 2,\n 3,\n 4,\n 5,\n 6\n ] }"; + const QByteArray ex_med = "{\n \"foo\" : 0, \"foo1\" : 1, \"foo2\" : [\n {\n \"bar\" : 1, \"foo\" : 0, \"foobar\" : 0\n },\n {\n \"bar\" : 1, \"foo\" : 1, \"foobar\" : 1\n }\n ], \"foo3\" : [\n 1,\n 2,\n 3,\n 4,\n 5,\n 6\n ]\n}"; + const QByteArray ex_full = "{\n \"foo\" : 0,\n \"foo1\" : 1,\n \"foo2\" : [\n {\n \"bar\" : 1,\n \"foo\" : 0,\n \"foobar\" : 0\n },\n {\n \"bar\" : 1,\n \"foo\" : 1,\n \"foobar\" : 1\n }\n ],\n \"foo3\" : [\n 1,\n 2,\n 3,\n 4,\n 5,\n 6\n ]\n}"; + QTest::newRow( "test indents" ) << json << ex_compact << ex_min << ex_med << ex_full; +} + +void TestSerializer::valueTest( const QVariant& value, const QString& expectedRegExp, bool errorExpected ) +{ + Serializer serializer; + bool ok; + const QByteArray serialized = serializer.serialize( value, &ok); + QCOMPARE(ok, !errorExpected); + QCOMPARE(serialized.isNull(), errorExpected); + const QString serializedUnicode = QString::fromUtf8( serialized ); + if (!errorExpected) { + QRegExp expected( expectedRegExp ); + QVERIFY( expected.isValid() ); + QVERIFY2( expected.exactMatch( serializedUnicode ), + qPrintable( QString( QLatin1String( "Expected regexp \"%1\" but got \"%2\"." ) ) + .arg( expectedRegExp ).arg( serializedUnicode ) ) ); + } else { + QVERIFY(!serializer.errorMessage().isEmpty()); + } +} + +void TestSerializer::valueTest( const QObject* object, const QString& expectedRegExp ) +{ + Serializer serializer; + bool ok; + const QByteArray serialized = serializer.serialize( object, &ok); + QVERIFY(ok); + const QString serializedUnicode = QString::fromUtf8( serialized ); + QRegExp expected( expectedRegExp ); + QVERIFY( expected.isValid() ); + QVERIFY2( expected.exactMatch( serializedUnicode ), + qPrintable( QString( QLatin1String( "Expected regexp \"%1\" but got \"%2\"." ) ) + .arg( expectedRegExp ).arg( serializedUnicode ) ) ); +} + +void TestSerializer::testValueNull() +{ + valueTest( QVariant(), QLatin1String( "\\s*null\\s*" ) ); + QVariantMap map; + map[QLatin1String("value")] = QVariant(); + valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:\\s*null\\s*\\}\\s*" ) ); +} + +void TestSerializer::testValueString() +{ + QFETCH( QVariant, value ); + QFETCH( QString, expected ); + valueTest( value, expected ); + + QVariantMap map; + map[QLatin1String("value")] = value; + valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) ); +} + +void TestSerializer::testValueString_data() +{ + QTest::addColumn( "value" ); + QTest::addColumn( "expected" ); + QTest::newRow( "null string" ) << QVariant( QString() ) << QString( QLatin1String( "\\s*\"\"\\s*" ) ); + QTest::newRow( "empty string" ) << QVariant( QString( QLatin1String( "" ) ) ) << QString( QLatin1String( "\\s*\"\"\\s*" ) ); + QTest::newRow( "Simple String" ) << QVariant( QString( QLatin1String( "simpleString" ) ) ) << QString( QLatin1String( "\\s*\"simpleString\"\\s*" ) ); + QTest::newRow( "string with tab" ) << QVariant( QString( QLatin1String( "string\tstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\tstring\"\\s*" ) ); + QTest::newRow( "string with newline" ) << QVariant( QString( QLatin1String( "string\nstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\nstring\"\\s*" ) ); + QTest::newRow( "string with bell" ) << QVariant( QString( QLatin1String( "string\bstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\bstring\"\\s*" ) ); + QTest::newRow( "string with return" ) << QVariant( QString( QLatin1String( "string\rstring" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\rstring\"\\s*" ) ); + QTest::newRow( "string with double quote" ) << QVariant( QString( QLatin1String( "string\"string" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\\"string\"\\s*" ) ); + QTest::newRow( "string with backslash" ) << QVariant( QString( QLatin1String( "string\\string" ) ) ) << QString( QLatin1String( "\\s*\"string\\\\\\\\string\"\\s*" ) ); + QString testStringWithUnicode = QString( QLatin1String( "string" ) ) + QChar( 0x2665 ) + QLatin1String( "string" ); + QString testEscapedString = QString( QLatin1String( "string" ) ) + QLatin1String("\\\\u2665") + QLatin1String( "string" ); + QTest::newRow( "string with unicode" ) << QVariant( testStringWithUnicode ) << QLatin1String( "\\s*\"" ) + testEscapedString + QLatin1String( "\"\\s*" ); +} + +void TestSerializer::testValueStringList() +{ + QFETCH( QVariant, value ); + QFETCH( QString, expected ); + valueTest( value, expected ); + + QVariantMap map; + map[QLatin1String("value")] = value; + valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) ); +} + +void TestSerializer::testValueStringList_data() +{ + QTest::addColumn( "value" ); + QTest::addColumn( "expected" ); + + QStringList stringlist; + QString expected; + + // simple QStringList + stringlist << QLatin1String("hello") << QLatin1String("world"); + expected = QLatin1String( "\\s*\\[\\s*\"hello\"\\s*,\\s*\"world\"\\s*\\]\\s*" ); + QTest::newRow( "simple QStringList" ) << QVariant( stringlist) << expected; +} + +void TestSerializer::testValueInteger() +{ + QFETCH( QVariant, value ); + QFETCH( QString, expected ); + valueTest( value, expected ); + + QVariantMap map; + map[QLatin1String("value")] = value; + valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) ); +} + +void TestSerializer::testValueInteger_data() +{ + QTest::addColumn( "value" ); + QTest::addColumn( "expected" ); + + QTest::newRow( "int 0" ) << QVariant( static_cast( 0 ) ) << QString( QLatin1String( "\\s*0\\s*" ) ); + QTest::newRow( "uint 0" ) << QVariant( static_cast( 0 ) ) << QString( QLatin1String( "\\s*0\\s*" ) ); + QTest::newRow( "int -1" ) << QVariant( static_cast( -1 ) ) << QString( QLatin1String( "\\s*-1\\s*" ) ); + QTest::newRow( "int 2133149800" ) << QVariant( static_cast(2133149800) ) << QString( QLatin1String( "\\s*2133149800\\s*" ) ); + QTest::newRow( "uint 4133149800" ) << QVariant( static_cast(4133149800u) ) << QString( QLatin1String( "\\s*4133149800\\s*" ) ); + QTest::newRow( "uint64 932838457459459" ) << QVariant( Q_UINT64_C(932838457459459) ) << QString( QLatin1String( "\\s*932838457459459\\s*" ) ); + QTest::newRow( "max unsigned long long" ) << QVariant( std::numeric_limits::max() ) << QString( QLatin1String( "\\s*%1\\s*" ) ).arg(std::numeric_limits::max()); +} + +void TestSerializer::testValueDouble() +{ + QFETCH( QVariant, value ); + QFETCH( QString, expected ); + QFETCH( bool, errorExpected ); + valueTest( value, expected, errorExpected ); + + QVariantMap map; + map[QLatin1String("value")] = value; + valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ), errorExpected ); +} + +void TestSerializer::testValueDouble_data() +{ + QTest::addColumn( "value" ); + QTest::addColumn( "expected" ); + QTest::addColumn( "errorExpected" ); + + QTest::newRow( "double 0" ) << QVariant( 0.0 ) << QString( QLatin1String( "\\s*0.0\\s*" ) ) << false; + QTest::newRow( "double -1" ) << QVariant( -1.0 ) << QString( QLatin1String( "\\s*-1.0\\s*" ) ) << false; + QTest::newRow( "double 1.5E-20" ) << QVariant( 1.5e-20 ) << QString( QLatin1String( "\\s*1.5[Ee]-20\\s*" ) ) << false; + QTest::newRow( "double -1.5E-20" ) << QVariant( -1.5e-20 ) << QString( QLatin1String( "\\s*-1.5[Ee]-20\\s*" ) ) << false; + QTest::newRow( "double 2.0E-20" ) << QVariant( 2.0e-20 ) << QString( QLatin1String( "\\s*2(?:.0)?[Ee]-20\\s*" ) ) << false; + QTest::newRow( "double infinity" ) << QVariant( std::numeric_limits< double >::infinity() ) << QString( ) << true; + QTest::newRow( "double -infinity" ) << QVariant( -std::numeric_limits< double >::infinity() ) << QString( ) << true; + QTest::newRow( "double NaN" ) << QVariant( std::numeric_limits< double >::quiet_NaN() ) << QString( ) << true; +} + +void TestSerializer::testSetDoublePrecision() +{ + bool ok; + Serializer serializer; + QByteArray actual; + QString expected, actualUnicode; + + double num = 0.12345678; + + // Set 1 as double precision + serializer.setDoublePrecision(1); + expected = QString(QLatin1String("0.1")); + actual = serializer.serialize( QVariant(num), &ok); + QVERIFY(ok); + actualUnicode = QString::fromUtf8(actual); + + QVERIFY2( QString::compare(expected, actualUnicode ) == 0, + qPrintable( QString( QLatin1String( "Expected \"%1\" but got \"%2\"." ) ) + .arg( expected ).arg( actualUnicode ) ) ); + + // Set 2 as double precision + serializer.setDoublePrecision(2); + expected = QString(QLatin1String("0.12")); + actual = serializer.serialize( QVariant(num), &ok); + QVERIFY(ok); + actualUnicode = QString::fromUtf8(actual); + + QVERIFY2( QString::compare(expected, actualUnicode ) == 0, + qPrintable( QString( QLatin1String( "Expected \"%1\" but got \"%2\"." ) ) + .arg( expected ).arg( actualUnicode ) ) ); + + // Set 4 as double precision + serializer.setDoublePrecision(4); + expected = QString(QLatin1String("0.1235")); + actual = serializer.serialize( QVariant(num), &ok); + QVERIFY(ok); + actualUnicode = QString::fromUtf8(actual); + + QVERIFY2( QString::compare(expected, actualUnicode ) == 0, + qPrintable( QString( QLatin1String( "Expected \"%1\" but got \"%2\"." ) ) + .arg( expected ).arg( actualUnicode ) ) ); + + // Set 14 as double precision + serializer.setDoublePrecision(14); + expected = QString(QLatin1String("0.12345678")); + actual = serializer.serialize( QVariant(num), &ok); + QVERIFY(ok); + actualUnicode = QString::fromUtf8(actual); + + QVERIFY2( QString::compare(expected, actualUnicode ) == 0, + qPrintable( QString( QLatin1String( "Expected \"%1\" but got \"%2\"." ) ) + .arg( expected ).arg( actualUnicode ) ) ); +} + +void TestSerializer::testValueFloat() +{ + QFETCH( QVariant, value ); + QFETCH( QString, expected ); + QFETCH( bool, errorExpected ); + valueTest( value, expected, errorExpected ); + + QVariantMap map; + map[QLatin1String("value")] = value; + valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ), errorExpected ); +} + +void TestSerializer::testValueFloat_data() +{ + QVariant v; + float value; + + QTest::addColumn( "value" ); + QTest::addColumn( "expected" ); + QTest::addColumn( "errorExpected" ); + + value = 0; + v.setValue(value); + QTest::newRow( "float 0" ) << v << QString( QLatin1String( "\\s*0.0\\s*" ) ) << false; + + value = -1; + v.setValue(value); + QTest::newRow( "float -1" ) << v << QString( QLatin1String( "\\s*-1.0\\s*" ) ) << false; + + value = 1.12f; + v.setValue(value); + QTest::newRow( "float 1.12" ) << v << QString( QLatin1String( "\\s*1.12\\s*" ) ) << false; +} + +void TestSerializer::testValueBoolean() +{ + QFETCH( QVariant, value ); + QFETCH( QString, expected ); + valueTest( value, expected ); + + QVariantMap map; + map[QLatin1String("value")] = value; + valueTest( QVariant(map), QLatin1String( "\\s*\\{\\s*\"value\"\\s*:" ) + expected + QLatin1String( "\\}\\s*" ) ); +} + +void TestSerializer::testValueBoolean_data() +{ + QTest::addColumn( "value" ); + QTest::addColumn( "expected" ); + + QTest::newRow( "bool false" ) << QVariant( false ) << QString( QLatin1String( "\\s*false\\s*" ) ); + QTest::newRow( "bool true" ) << QVariant( true ) << QString( QLatin1String( "\\s*true\\s*" ) ); +} + +void TestSerializer::testSpecialNumbers() { + bool ok; + QFETCH( QVariant, value ); + QFETCH( QString, expected ); + Serializer specialSerializer; + QVERIFY(!specialSerializer.specialNumbersAllowed()); + specialSerializer.allowSpecialNumbers(true); + QVERIFY(specialSerializer.specialNumbersAllowed()); + QByteArray serialized = specialSerializer.serialize(value, &ok); + QVERIFY(ok); + QCOMPARE(QString::fromLocal8Bit(serialized), expected); +} + +void TestSerializer::testSpecialNumbers_data() { + QTest::addColumn( "value" ); + QTest::addColumn( "expected" ); + + QTest::newRow( "Infinity" ) << QVariant( std::numeric_limits< double >::infinity() ) << QString::fromLocal8Bit("Infinity"); + QTest::newRow( "-Infinity" ) << QVariant( -std::numeric_limits< double >::infinity() ) << QString::fromLocal8Bit("-Infinity"); + QTest::newRow( "Infinity" ) << QVariant( std::numeric_limits< double >::quiet_NaN() ) << QString::fromLocal8Bit("NaN"); +} + +void TestSerializer::testSerializetoQIODevice() { + QBuffer buffer; + QVariantList variant; + variant << QVariant(QLatin1String("Hello")); + variant << QVariant(QLatin1String("world!")); + + Serializer serializer; + bool ok; + + serializer.serialize(variant, &buffer, &ok); + + QCOMPARE(QString(QLatin1String(buffer.data())), + QString(QLatin1String("[ \"Hello\", \"world!\" ]"))); + QVERIFY(ok); +} + +void TestSerializer::testSerializeWithoutOkParam() { + QBuffer buffer; + QVariantList variant; + variant << QVariant(QLatin1String("Hello")); + variant << QVariant(QLatin1String("world!")); + + Serializer serializer; + + const QByteArray serialized = serializer.serialize(variant); + const QByteArray expected = "[ \"Hello\", \"world!\" ]"; + QCOMPARE(expected, serialized); + + + // test a serialization which produces an error + QVariant brokenVariant ( std::numeric_limits< double >::quiet_NaN() ); + QVERIFY(serializer.serialize(brokenVariant).isEmpty()); +} + +void TestSerializer::testValueHashMap() +{ + Serializer serializer; + bool ok; + + QVariantHash hash; + hash[QLatin1String("one")] = 1; + hash[QLatin1String("three")] = 3; + hash[QLatin1String("seven")] = 7; + + QByteArray json = serializer.serialize(hash, &ok); + QVERIFY(ok); + + Parser parser; + QVariant var = parser.parse(json, &ok); + QVERIFY(ok); + + QVariantMap vmap = var.toMap(); + QHashIterator hIt( hash ); + while ( hIt.hasNext() ) { + hIt.next(); + QString key = hIt.key(); + QVariant value = hIt.value(); + + QMap::const_iterator mIt = vmap.constFind(key); + QVERIFY(mIt != vmap.constEnd()); + QCOMPARE(mIt.value(), value); + } + +} + +#if QT_VERSION < QT_VERSION_CHECK(5,0,0) +// using Qt4 rather then Qt5 +QTEST_MAIN(TestSerializer) +#include "moc_testserializer.cxx" +#else +QTEST_GUILESS_MAIN(TestSerializer) +#include "testserializer.moc" +#endif