Fix overflow issue
[platform/upstream/libxkbcommon.git] / src / text.c
index 0180747..e85b0eb 100644 (file)
 /************************************************************
- Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
-
- Permission to use, copy, modify, and distribute this
- software and its documentation for any purpose and without
- fee is hereby granted, provided that the above copyright
- notice appear in all copies and that both that copyright
- notice and this permission notice appear in supporting
- documentation, and that the name of Silicon Graphics not be
- used in advertising or publicity pertaining to distribution
- of the software without specific prior written permission.
- Silicon Graphics makes no representation about the suitability
- of this software for any purpose. It is provided "as is"
- without any express or implied warranty.
-
- SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
- SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
- GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
- DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
- THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
+ *
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting
documentation, and that the name of Silicon Graphics not be
used in advertising or publicity pertaining to distribution
of the software without specific prior written permission.
Silicon Graphics makes no representation about the suitability
of this software for any purpose. It is provided "as is"
without any express or implied warranty.
+ *
SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
  ********************************************************/
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-#include "X11/extensions/XKBcommon.h"
-#include "XKBcommonint.h"
-#include <X11/extensions/XKM.h>
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-
-#define BUFFER_SIZE 512
-static char textBuffer[BUFFER_SIZE];
-static int tbNext = 0;
-
-static char *
-tbGetBuffer(unsigned size)
-{
-    char *rtrn;
-
-    if (size >= BUFFER_SIZE)
-        return NULL;
-
-    if ((BUFFER_SIZE - tbNext) <= size)
-        tbNext = 0;
+#include "config.h"
 
-    rtrn = &textBuffer[tbNext];
-    tbNext += size;
+#include "keymap.h"
+#include "text.h"
 
-    return rtrn;
-}
-
-static char *
-XkbStringText(char *str)
+bool
+LookupString(const LookupEntry tab[], const char *string,
+              unsigned int *value_rtrn)
 {
-    char *buf, *in, *out;
-    int len;
-    Bool ok;
-
-    if (!str) {
-        buf = tbGetBuffer(2);
-        buf[0] = '\0';
-        return buf;
-    }
-
-    /* Find if there are any non-printable characters */
-    for (ok = True, len = 0, in = str; *in != '\0'; in++, len++) {
-        if (isprint(*in))
-            continue;
+    if (!string)
+        return false;
 
-        ok = False;
-        switch (*in) {
-        case '\n': case '\t': case '\v':
-        case '\b': case '\r': case '\f':
-            len++;
-            break;
-        default:
-            /* octal: \0ooo */
-            len += 4;
-            break;
+    for (const LookupEntry *entry = tab; entry->name; entry++) {
+        if (istreq(entry->name, string)) {
+            *value_rtrn = entry->value;
+            return true;
         }
     }
 
-    if (ok)
-        return str;
-
-    /* Cleanup non-printable characters */
-    buf = tbGetBuffer(len + 1);
-    for (in = str, out = buf; *in != '\0'; in++) {
-        if (isprint(*in)) {
-            *out++ = *in;
-            continue;
-        }
+    return false;
+}
 
-        *out++ = '\\';
-        switch (*in) {
-        case '\n':
-            *out++ = 'n';
-            break;
-        case '\t':
-            *out++ = 't';
-            break;
-        case '\v':
-            *out++ = 'v';
-            break;
-        case '\b':
-            *out++ = 'b';
-            break;
-        case '\r':
-            *out++ = 'r';
-            break;
-        case '\f':
-            *out++ = 'f';
-            break;
-        default:
-            *out++ = '0';
-            snprintf(out, 3, "%o", *in);
-            while (*out != '\0')
-                out++;
-        }
-    }
+const char *
+LookupValue(const LookupEntry tab[], unsigned int value)
+{
+    for (const LookupEntry *entry = tab; entry->name; entry++)
+        if (entry->value == value)
+            return entry->name;
 
-    *out++ = '\0';
-    return buf;
+    return NULL;
 }
 
-char *
-XkbcAtomText(Atom atm)
-{
-    char *tmp, *rtrn;
-    int len;
+const LookupEntry ctrlMaskNames[] = {
+    { "RepeatKeys", CONTROL_REPEAT },
+    { "Repeat", CONTROL_REPEAT },
+    { "AutoRepeat", CONTROL_REPEAT },
+    { "SlowKeys", CONTROL_SLOW },
+    { "BounceKeys", CONTROL_DEBOUNCE },
+    { "StickyKeys", CONTROL_STICKY },
+    { "MouseKeys", CONTROL_MOUSEKEYS },
+    { "MouseKeysAccel", CONTROL_MOUSEKEYS_ACCEL },
+    { "AccessXKeys", CONTROL_AX },
+    { "AccessXTimeout", CONTROL_AX_TIMEOUT },
+    { "AccessXFeedback", CONTROL_AX_FEEDBACK },
+    { "AudibleBell", CONTROL_BELL },
+    { "IgnoreGroupLock", CONTROL_IGNORE_GROUP_LOCK },
+    { "all", CONTROL_ALL },
+    { "none", 0 },
+    { "Overlay1", 0 },
+    { "Overlay2", 0 },
+    { NULL, 0 }
+};
 
-    tmp = XkbcAtomGetString(atm);
+const LookupEntry modComponentMaskNames[] = {
+    { "base", XKB_STATE_MODS_DEPRESSED },
+    { "latched", XKB_STATE_MODS_LATCHED },
+    { "locked", XKB_STATE_MODS_LOCKED },
+    { "effective", XKB_STATE_MODS_EFFECTIVE },
+    { "compat", XKB_STATE_MODS_EFFECTIVE },
+    { "any", XKB_STATE_MODS_EFFECTIVE },
+    { "none", 0 },
+    { NULL, 0 }
+};
 
-    if (!tmp)
-        return "";
+const LookupEntry groupComponentMaskNames[] = {
+    { "base", XKB_STATE_LAYOUT_DEPRESSED },
+    { "latched", XKB_STATE_LAYOUT_LATCHED },
+    { "locked", XKB_STATE_LAYOUT_LOCKED },
+    { "effective", XKB_STATE_LAYOUT_EFFECTIVE },
+    { "any", XKB_STATE_LAYOUT_EFFECTIVE },
+    { "none", 0 },
+    { NULL, 0 }
+};
 
-    len = strlen(tmp) + 1;
-    if (len >= BUFFER_SIZE)
-        len = BUFFER_SIZE - 2;
+const LookupEntry groupMaskNames[] = {
+    { "Group1", 0x01 },
+    { "Group2", 0x02 },
+    { "Group3", 0x04 },
+    { "Group4", 0x08 },
+    { "Group5", 0x10 },
+    { "Group6", 0x20 },
+    { "Group7", 0x40 },
+    { "Group8", 0x80 },
+    { "none", 0x00 },
+    { "all", 0xff },
+    { NULL, 0 }
+};
 
-    rtrn = tbGetBuffer(len);
-    strncpy(rtrn, tmp, len);
-    rtrn[len] = '\0';
+const LookupEntry groupNames[] = {
+    { "Group1", 1 },
+    { "Group2", 2 },
+    { "Group3", 3 },
+    { "Group4", 4 },
+    { "Group5", 5 },
+    { "Group6", 6 },
+    { "Group7", 7 },
+    { "Group8", 8 },
+    { NULL, 0 }
+};
 
-    _XkbFree(tmp);
+const LookupEntry levelNames[] = {
+    { "Level1", 1 },
+    { "Level2", 2 },
+    { "Level3", 3 },
+    { "Level4", 4 },
+    { "Level5", 5 },
+    { "Level6", 6 },
+    { "Level7", 7 },
+    { "Level8", 8 },
+    { NULL, 0 }
+};
 
-    return XkbStringText(rtrn);
-}
+const LookupEntry buttonNames[] = {
+    { "Button1", 1 },
+    { "Button2", 2 },
+    { "Button3", 3 },
+    { "Button4", 4 },
+    { "Button5", 5 },
+    { "default", 0 },
+    { NULL, 0 }
+};
 
-static char *modNames[XkbNumModifiers] = {
-    "Shift",
-    "Lock",
-    "Control",
-    "Mod1",
-    "Mod2",
-    "Mod3",
-    "Mod4",
-    "Mod5"
+const LookupEntry useModMapValueNames[] = {
+    { "LevelOne", 1 },
+    { "Level1", 1 },
+    { "AnyLevel", 0 },
+    { "any", 0 },
+    { NULL, 0 }
 };
 
-char *
-XkbcModMaskText(unsigned mask, Bool cFormat)
-{
-    int i, rem, bit;
-    char *str, *buf;
+const LookupEntry actionTypeNames[] = {
+    { "NoAction", ACTION_TYPE_NONE },
+    { "SetMods", ACTION_TYPE_MOD_SET },
+    { "LatchMods", ACTION_TYPE_MOD_LATCH },
+    { "LockMods", ACTION_TYPE_MOD_LOCK },
+    { "SetGroup", ACTION_TYPE_GROUP_SET },
+    { "LatchGroup", ACTION_TYPE_GROUP_LATCH },
+    { "LockGroup", ACTION_TYPE_GROUP_LOCK },
+    { "MovePtr", ACTION_TYPE_PTR_MOVE },
+    { "MovePointer", ACTION_TYPE_PTR_MOVE },
+    { "PtrBtn", ACTION_TYPE_PTR_BUTTON },
+    { "PointerButton", ACTION_TYPE_PTR_BUTTON },
+    { "LockPtrBtn", ACTION_TYPE_PTR_LOCK },
+    { "LockPtrButton", ACTION_TYPE_PTR_LOCK },
+    { "LockPointerButton", ACTION_TYPE_PTR_LOCK },
+    { "LockPointerBtn", ACTION_TYPE_PTR_LOCK },
+    { "SetPtrDflt", ACTION_TYPE_PTR_DEFAULT },
+    { "SetPointerDefault", ACTION_TYPE_PTR_DEFAULT },
+    { "Terminate", ACTION_TYPE_TERMINATE },
+    { "TerminateServer", ACTION_TYPE_TERMINATE },
+    { "SwitchScreen", ACTION_TYPE_SWITCH_VT },
+    { "SetControls", ACTION_TYPE_CTRL_SET },
+    { "LockControls", ACTION_TYPE_CTRL_LOCK },
+    { "Private", ACTION_TYPE_PRIVATE },
+    /* deprecated actions below here - unused */
+    { "RedirectKey", ACTION_TYPE_NONE },
+    { "Redirect", ACTION_TYPE_NONE },
+    { "ISOLock", ACTION_TYPE_NONE },
+    { "ActionMessage", ACTION_TYPE_NONE },
+    { "MessageAction", ACTION_TYPE_NONE },
+    { "Message", ACTION_TYPE_NONE },
+    { "DeviceBtn", ACTION_TYPE_NONE },
+    { "DevBtn", ACTION_TYPE_NONE },
+    { "DevButton", ACTION_TYPE_NONE },
+    { "DeviceButton", ACTION_TYPE_NONE },
+    { "LockDeviceBtn", ACTION_TYPE_NONE },
+    { "LockDevBtn", ACTION_TYPE_NONE },
+    { "LockDevButton", ACTION_TYPE_NONE },
+    { "LockDeviceButton", ACTION_TYPE_NONE },
+    { "DeviceValuator", ACTION_TYPE_NONE },
+    { "DevVal", ACTION_TYPE_NONE },
+    { "DeviceVal", ACTION_TYPE_NONE },
+    { "DevValuator", ACTION_TYPE_NONE },
+    { NULL, 0 },
+};
 
-    if ((mask & 0xff) == 0xff)
-        return (cFormat ? "0xff" : "all");
+const LookupEntry symInterpretMatchMaskNames[] = {
+    { "NoneOf", MATCH_NONE },
+    { "AnyOfOrNone", MATCH_ANY_OR_NONE },
+    { "AnyOf", MATCH_ANY },
+    { "AllOf", MATCH_ALL },
+    { "Exactly", MATCH_EXACTLY },
+    { NULL, 0 },
+};
 
-    if ((mask & 0xff) == 0)
-        return (cFormat ? "0" : "none");
+const char *
+ModIndexText(struct xkb_context *ctx, const struct xkb_mod_set *mods,
+             xkb_mod_index_t ndx)
+{
+    if (ndx == XKB_MOD_INVALID)
+        return "none";
 
-    rem = 64;
-    buf = tbGetBuffer(rem);
-    str = buf;
-    buf[0] = '\0';
-    for (i = 0, bit = 1; i < XkbNumModifiers && rem > 1; i++, bit <<= 1) {
-        int len;
+    if (ndx >= mods->num_mods)
+        return NULL;
 
-        if (!(mask & bit))
-            continue;
+    return xkb_atom_text(ctx, mods->mods[ndx].name);
+}
 
-        len = snprintf(str, rem, "%s%s%s",
-                       (str != buf) ? (cFormat ? "|" : "+") : "",
-                       modNames[i],
-                       cFormat ? "Mask" : "");
-        rem -= len;
-        str += len;
-    }
+const char *
+ActionTypeText(enum xkb_action_type type)
+{
+    const char *name = LookupValue(actionTypeNames, type);
+    return name ? name : "Private";
+}
 
+const char *
+KeysymText(struct xkb_context *ctx, xkb_keysym_t sym)
+{
+    char *buffer = xkb_context_get_buffer(ctx, 64);
+    xkb_keysym_get_name(sym, buffer, 64);
+    return buffer;
+}
+
+const char *
+KeyNameText(struct xkb_context *ctx, xkb_atom_t name)
+{
+    const char *sname = xkb_atom_text(ctx, name);
+    size_t len = strlen_safe(sname) + 3;
+    char *buf = xkb_context_get_buffer(ctx, len);
+    snprintf(buf, len, "<%s>", strempty(sname));
     return buf;
 }
 
-char *
-XkbcConfigText(unsigned config)
+const char *
+SIMatchText(enum xkb_match_operation type)
 {
-    switch (config) {
-    case XkmSemanticsFile:
-        return "Semantics";
-    case XkmLayoutFile:
-        return "Layout";
-    case XkmKeymapFile:
-        return "Keymap";
-    case XkmGeometryFile:
-    case XkmGeometryIndex:
-        return "Geometry";
-    case XkmTypesIndex:
-        return "Types";
-    case XkmCompatMapIndex:
-        return "CompatMap";
-    case XkmSymbolsIndex:
-        return "Symbols";
-    case XkmIndicatorsIndex:
-        return "Indicators";
-    case XkmKeyNamesIndex:
-        return "KeyNames";
-    case XkmVirtualModsIndex:
-        return "VirtualMods";
-    default:
-        return "unknown";
-    }
+    return LookupValue(symInterpretMatchMaskNames, type);
 }
 
-char *
-XkbcGeomFPText(int val)
+const char *
+ModMaskText(struct xkb_context *ctx, const struct xkb_mod_set *mods,
+            xkb_mod_mask_t mask)
 {
-    char *buf;
-    int whole, frac;
+    char buf[1024] = {0};
+    size_t pos = 0;
+    xkb_mod_index_t i;
+    const struct xkb_mod *mod;
 
-    buf = tbGetBuffer(12);
-    whole = val / XkbGeomPtsPerMM;
-    frac = val % XkbGeomPtsPerMM;
+    if (mask == 0)
+        return "none";
 
-    if (frac != 0)
-        snprintf(buf, 12, "%d.%d", whole, frac);
-    else
-        snprintf(buf, 12, "%d", whole);
+    if (mask == MOD_REAL_MASK_ALL)
+        return "all";
 
-    return buf;
-}
+    xkb_mods_enumerate(i, mod, mods) {
+        int ret;
 
-static char *actionTypeNames[XkbSA_NumActions]= {
-    "NoAction",         /* XkbSA_NoAction */
-    "SetMods",          /* XkbSA_SetMods */
-    "LatchMods",        /* XkbSA_LatchMods */
-    "LockMods",         /* XkbSA_LockMods */
-    "SetGroup",         /* XkbSA_SetGroup */
-    "LatchGroup",       /* XkbSA_LatchGroup */
-    "LockGroup",        /* XkbSA_LockGroup */
-    "MovePtr",          /* XkbSA_MovePtr */
-    "PtrBtn",           /* XkbSA_PtrBtn */
-    "LockPtrBtn",       /* XkbSA_LockPtrBtn */
-    "SetPtrDflt",       /* XkbSA_SetPtrDflt */
-    "ISOLock",          /* XkbSA_ISOLock */
-    "Terminate",        /* XkbSA_Terminate */
-    "SwitchScreen",     /* XkbSA_SwitchScreen */
-    "SetControls",      /* XkbSA_SetControls */
-    "LockControls",     /* XkbSA_LockControls */
-    "ActionMessage",    /* XkbSA_ActionMessage */
-    "RedirectKey",      /* XkbSA_RedirectKey */
-    "DeviceBtn",        /* XkbSA_DeviceBtn */
-    "LockDeviceBtn",    /* XkbSA_LockDeviceBtn */
-    "DeviceValuator"    /* XkbSA_DeviceValuator */
-};
+        if (!(mask & (1u << i)))
+            continue;
 
-char *
-XkbcActionTypeText(unsigned type)
-{
-    if (type <= XkbSA_LastAction)
-        return actionTypeNames[type];
-    return "Private";
+        ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s",
+                       pos == 0 ? "" : "+",
+                       xkb_atom_text(ctx, mod->name));
+        if (ret <= 0 || pos + ret >= sizeof(buf))
+            break;
+        else
+            pos += ret;
+    }
+
+    return strncpy(xkb_context_get_buffer(ctx, pos + 1), buf, pos + 1);
 }
 
-char *
-XkbcKeysymText(KeySym sym)
+const char *
+LedStateMaskText(struct xkb_context *ctx, enum xkb_state_component mask)
 {
-    char *buf;
+    char buf[1024] = {0};
+    size_t pos = 0;
 
-    if (sym == NoSymbol)
-        return "NoSymbol";
+    if (mask == 0)
+        return "0";
 
-    if ((buf = XkbcKeysymToString(sym)))
-        return buf;
+    for (unsigned i = 0; mask; i++) {
+        int ret;
 
-    buf = tbGetBuffer(32);
-    snprintf(buf, 32, "0x%lx", (long)sym);
-    return buf;
+        if (!(mask & (1u << i)))
+            continue;
+
+        mask &= ~(1u << i);
+
+        ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s",
+                       pos == 0 ? "" : "+",
+                       LookupValue(modComponentMaskNames, 1u << i));
+        if (ret <= 0 || pos + ret >= sizeof(buf))
+            break;
+        else
+            pos += ret;
+    }
+
+    return strncpy(xkb_context_get_buffer(ctx, pos + 1), buf, pos + 1);
 }
 
-char *
-XkbcKeyNameText(char *name)
+const char *
+ControlMaskText(struct xkb_context *ctx, enum xkb_action_controls mask)
 {
-    char *buf;
-    int len;
+    char buf[1024] = {0};
+    size_t pos = 0;
 
-    buf = tbGetBuffer(7);
-    buf[0] = '<';
-    strncpy(&buf[1], name, 4);
-    buf[5] = '\0';
-    len = strlen(buf);
-    buf[len++] = '>';
-    buf[len] = '\0';
+    if (mask == 0)
+        return "none";
 
-    return buf;
-}
+    if (mask == CONTROL_ALL)
+        return "all";
 
-static char *siMatchText[5] = {
-    "NoneOf",       /* XkbSI_NoneOf */
-    "AnyOfOrNone",  /* XkbSI_AnyOfOrNone */
-    "AnyOf",        /* XkbSI_AnyOf */
-    "AllOf",        /* XkbSI_AllOf */
-    "Exactly"       /* XkbSI_Exactly */
-};
+    for (unsigned i = 0; mask; i++) {
+        int ret;
 
-char *
-XkbcSIMatchText(unsigned type)
-{
-    char *buf;
-
-    switch (type & XkbSI_OpMask) {
-    case XkbSI_NoneOf:
-        return siMatchText[0];
-    case XkbSI_AnyOfOrNone:
-        return siMatchText[1];
-    case XkbSI_AnyOf:
-        return siMatchText[2];
-    case XkbSI_AllOf:
-        return siMatchText[3];
-    case XkbSI_Exactly:
-        return siMatchText[4];
-    default:
-        buf = tbGetBuffer(40);
-        snprintf(buf, 40, "0x%x", type & XkbSI_OpMask);
-        return buf;
+        if (!(mask & (1u << i)))
+            continue;
+
+        mask &= ~(1u << i);
+
+        ret = snprintf(buf + pos, sizeof(buf) - pos, "%s%s",
+                       pos == 0 ? "" : "+",
+                       LookupValue(ctrlMaskNames, 1u << i));
+        if (ret <= 0 || pos + ret >= sizeof(buf))
+            break;
+        else
+            pos += ret;
     }
+
+    return strncpy(xkb_context_get_buffer(ctx, pos + 1), buf, pos + 1);
 }