1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-07-27 12:23:29 +02:00
Files
build
docs
gio
completion
fam
fen
gdbus-2.0
gvdb
inotify
kqueue
tests
win32
xdgmime
.gitignore
ChangeLog
Makefile.am
data-to-c.pl
dbus-daemon.xml
gaction.c
gaction.h
gactiongroup.c
gactiongroup.h
gactiongroupexporter.c
gactiongroupexporter.h
gactionmap.c
gactionmap.h
gappinfo.c
gappinfo.h
gappinfoprivate.h
gapplication-tool.c
gapplication.c
gapplication.h
gapplicationcommandline.c
gapplicationcommandline.h
gapplicationimpl-dbus.c
gapplicationimpl.h
gasynchelper.c
gasynchelper.h
gasyncinitable.c
gasyncinitable.h
gasyncresult.c
gasyncresult.h
gbufferedinputstream.c
gbufferedinputstream.h
gbufferedoutputstream.c
gbufferedoutputstream.h
gbytesicon.c
gbytesicon.h
gcancellable.c
gcancellable.h
gcharsetconverter.c
gcharsetconverter.h
gcontenttype-win32.c
gcontenttype.c
gcontenttype.h
gcontenttypeprivate.h
gconverter.c
gconverter.h
gconverterinputstream.c
gconverterinputstream.h
gconverteroutputstream.c
gconverteroutputstream.h
gcredentials.c
gcredentials.h
gcredentialsprivate.h
gdatainputstream.c
gdatainputstream.h
gdataoutputstream.c
gdataoutputstream.h
gdbus-tool.c
gdbusactiongroup-private.h
gdbusactiongroup.c
gdbusactiongroup.h
gdbusaddress.c
gdbusaddress.h
gdbusauth.c
gdbusauth.h
gdbusauthmechanism.c
gdbusauthmechanism.h
gdbusauthmechanismanon.c
gdbusauthmechanismanon.h
gdbusauthmechanismexternal.c
gdbusauthmechanismexternal.h
gdbusauthmechanismsha1.c
gdbusauthmechanismsha1.h
gdbusauthobserver.c
gdbusauthobserver.h
gdbusconnection.c
gdbusconnection.h
gdbusdaemon.c
gdbusdaemon.h
gdbuserror.c
gdbuserror.h
gdbusinterface.c
gdbusinterface.h
gdbusinterfaceskeleton.c
gdbusinterfaceskeleton.h
gdbusintrospection.c
gdbusintrospection.h
gdbusmenumodel.c
gdbusmenumodel.h
gdbusmessage.c
gdbusmessage.h
gdbusmethodinvocation.c
gdbusmethodinvocation.h
gdbusnameowning.c
gdbusnameowning.h
gdbusnamewatching.c
gdbusnamewatching.h
gdbusobject.c
gdbusobject.h
gdbusobjectmanager.c
gdbusobjectmanager.h
gdbusobjectmanagerclient.c
gdbusobjectmanagerclient.h
gdbusobjectmanagerserver.c
gdbusobjectmanagerserver.h
gdbusobjectproxy.c
gdbusobjectproxy.h
gdbusobjectskeleton.c
gdbusobjectskeleton.h
gdbusprivate.c
gdbusprivate.h
gdbusproxy.c
gdbusproxy.h
gdbusserver.c
gdbusserver.h
gdbusutils.c
gdbusutils.h
gdelayedsettingsbackend.c
gdelayedsettingsbackend.h
gdesktopappinfo.c
gdesktopappinfo.h
gdrive.c
gdrive.h
gdummyfile.c
gdummyfile.h
gdummyproxyresolver.c
gdummyproxyresolver.h
gdummytlsbackend.c
gdummytlsbackend.h
gemblem.c
gemblem.h
gemblemedicon.c
gemblemedicon.h
gfdonotificationbackend.c
gfile.c
gfile.h
gfileattribute-priv.h
gfileattribute.c
gfileattribute.h
gfiledescriptorbased.c
gfiledescriptorbased.h
gfileenumerator.c
gfileenumerator.h
gfileicon.c
gfileicon.h
gfileinfo-priv.h
gfileinfo.c
gfileinfo.h
gfileinputstream.c
gfileinputstream.h
gfileiostream.c
gfileiostream.h
gfilemonitor.c
gfilemonitor.h
gfilenamecompleter.c
gfilenamecompleter.h
gfileoutputstream.c
gfileoutputstream.h
gfilterinputstream.c
gfilterinputstream.h
gfilteroutputstream.c
gfilteroutputstream.h
ggtknotificationbackend.c
gicon.c
gicon.h
ginetaddress.c
ginetaddress.h
ginetaddressmask.c
ginetaddressmask.h
ginetsocketaddress.c
ginetsocketaddress.h
ginitable.c
ginitable.h
ginputstream.c
ginputstream.h
gio-autocleanups.h
gio-querymodules.c
gio.h
gio.rc.in
gioenums.h
gioenumtypes.c.template
gioenumtypes.h.template
gioerror.c
gioerror.h
giomodule-priv.h
giomodule.c
giomodule.h
gioprivate.h
gioscheduler.c
gioscheduler.h
giostream.c
giostream.h
giotypes.h
giowin32-priv.h
gkeyfilesettingsbackend.c
glib-compile-resources.c
glib-compile-schemas.c
glistmodel.c
glistmodel.h
gliststore.c
gliststore.h
gloadableicon.c
gloadableicon.h
glocaldirectorymonitor.c
glocaldirectorymonitor.h
glocalfile.c
glocalfile.h
glocalfileenumerator.c
glocalfileenumerator.h
glocalfileinfo.c
glocalfileinfo.h
glocalfileinputstream.c
glocalfileinputstream.h
glocalfileiostream.c
glocalfileiostream.h
glocalfilemonitor.c
glocalfilemonitor.h
glocalfileoutputstream.c
glocalfileoutputstream.h
glocalvfs.c
glocalvfs.h
gmemoryinputstream.c
gmemoryinputstream.h
gmemoryoutputstream.c
gmemoryoutputstream.h
gmemorysettingsbackend.c
gmenu.c
gmenu.h
gmenuexporter.c
gmenuexporter.h
gmenumodel.c
gmenumodel.h
gmount.c
gmount.h
gmountoperation.c
gmountoperation.h
gmountprivate.h
gnativevolumemonitor.c
gnativevolumemonitor.h
gnetworkaddress.c
gnetworkaddress.h
gnetworking.c
gnetworking.h.in
gnetworking.h.win32
gnetworkingprivate.h
gnetworkmonitor.c
gnetworkmonitor.h
gnetworkmonitorbase.c
gnetworkmonitorbase.h
gnetworkmonitornetlink.c
gnetworkmonitornetlink.h
gnetworkmonitornm.c
gnetworkmonitornm.h
gnetworkservice.c
gnetworkservice.h
gnextstepsettingsbackend.c
gnotification-private.h
gnotification.c
gnotification.h
gnotificationbackend.c
gnotificationbackend.h
gnullsettingsbackend.c
goutputstream.c
goutputstream.h
gpermission.c
gpermission.h
gpollableinputstream.c
gpollableinputstream.h
gpollableoutputstream.c
gpollableoutputstream.h
gpollableutils.c
gpollableutils.h
gpollfilemonitor.c
gpollfilemonitor.h
gpropertyaction.c
gpropertyaction.h
gproxy.c
gproxy.h
gproxyaddress.c
gproxyaddress.h
gproxyaddressenumerator.c
gproxyaddressenumerator.h
gproxyresolver.c
gproxyresolver.h
gregistrysettingsbackend.c
gregistrysettingsbackend.h
gremoteactiongroup.c
gremoteactiongroup.h
gresolver.c
gresolver.h
gresource-tool.c
gresource.c
gresource.h
gresourcefile.c
gresourcefile.h
gschema.dtd
gseekable.c
gseekable.h
gsettings-mapping.c
gsettings-mapping.h
gsettings-tool.c
gsettings.c
gsettings.h
gsettingsbackend.c
gsettingsbackend.h
gsettingsbackendinternal.h
gsettingsschema-internal.h
gsettingsschema.c
gsettingsschema.h
gsimpleaction.c
gsimpleaction.h
gsimpleactiongroup.c
gsimpleactiongroup.h
gsimpleasyncresult.c
gsimpleasyncresult.h
gsimplepermission.c
gsimplepermission.h
gsimpleproxyresolver.c
gsimpleproxyresolver.h
gsocket.c
gsocket.h
gsocketaddress.c
gsocketaddress.h
gsocketaddressenumerator.c
gsocketaddressenumerator.h
gsocketclient.c
gsocketclient.h
gsocketconnectable.c
gsocketconnectable.h
gsocketconnection.c
gsocketconnection.h
gsocketcontrolmessage.c
gsocketcontrolmessage.h
gsocketinputstream.c
gsocketinputstream.h
gsocketlistener.c
gsocketlistener.h
gsocketoutputstream.c
gsocketoutputstream.h
gsocketservice.c
gsocketservice.h
gsocks4aproxy.c
gsocks4aproxy.h
gsocks4proxy.c
gsocks4proxy.h
gsocks5proxy.c
gsocks5proxy.h
gsrvtarget.c
gsrvtarget.h
gsubprocess.c
gsubprocess.h
gsubprocesslauncher-private.h
gsubprocesslauncher.c
gsubprocesslauncher.h
gtask.c
gtask.h
gtcpconnection.c
gtcpconnection.h
gtcpwrapperconnection.c
gtcpwrapperconnection.h
gtestdbus.c
gtestdbus.h
gthemedicon.c
gthemedicon.h
gthreadedresolver.c
gthreadedresolver.h
gthreadedsocketservice.c
gthreadedsocketservice.h
gtlsbackend.c
gtlsbackend.h
gtlscertificate.c
gtlscertificate.h
gtlsclientconnection.c
gtlsclientconnection.h
gtlsconnection.c
gtlsconnection.h
gtlsdatabase.c
gtlsdatabase.h
gtlsfiledatabase.c
gtlsfiledatabase.h
gtlsinteraction.c
gtlsinteraction.h
gtlspassword.c
gtlspassword.h
gtlsserverconnection.c
gtlsserverconnection.h
gunionvolumemonitor.c
gunionvolumemonitor.h
gunixconnection.c
gunixconnection.h
gunixcredentialsmessage.c
gunixcredentialsmessage.h
gunixfdlist.c
gunixfdlist.h
gunixfdmessage.c
gunixfdmessage.h
gunixinputstream.c
gunixinputstream.h
gunixmount.c
gunixmount.h
gunixmounts.c
gunixmounts.h
gunixoutputstream.c
gunixoutputstream.h
gunixsocketaddress.c
gunixsocketaddress.h
gunixvolume.c
gunixvolume.h
gunixvolumemonitor.c
gunixvolumemonitor.h
gvfs.c
gvfs.h
gvolume.c
gvolume.h
gvolumemonitor.c
gvolumemonitor.h
gwin32appinfo.c
gwin32appinfo.h
gwin32inputstream.c
gwin32inputstream.h
gwin32mount.c
gwin32mount.h
gwin32outputstream.c
gwin32outputstream.h
gwin32volumemonitor.c
gwin32volumemonitor.h
gzlibcompressor.c
gzlibcompressor.h
gzlibdecompressor.c
gzlibdecompressor.h
makefile.msc
strinfo.c
thumbnail-verify.c
thumbnail-verify.h
glib
gmodule
gobject
gthread
m4macros
po
tests
.dir-locals.el
.gitignore
AUTHORS
COPYING
ChangeLog.pre-1-2
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-14
ChangeLog.pre-2-16
ChangeLog.pre-2-18
ChangeLog.pre-2-2
ChangeLog.pre-2-20
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL.in
Makefile.am
NEWS
NEWS.pre-1-3
README.commits
README.in
README.rationale
README.win32
acglib.m4
acinclude.m4
autogen.sh
check-abis.sh
config.h.win32.in
configure.ac
gio-2.0.pc.in
gio-unix-2.0.pc.in
gio-windows-2.0.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-tap.mk
glib-zip.in
glib.doap
glib.mk
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0.pc.in
gthread-2.0.pc.in
makefile.msc
msvc_recommended_pragmas.h
sanity_check
tap-driver.sh
tap-test
win32-fixup.pl
glib/gio/strinfo.c

349 lines
10 KiB
C
Raw Normal View History

/*
* Copyright © 2010 Codethink Limited
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the licence, or (at your option) any later version.
*
* 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
2014-01-23 12:58:29 +01:00
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#include <string.h>
#include <glib.h>
/*
* The string info map is an efficient data structure designed to be
* used with a small set of items. It is used by GSettings schemas for
* three purposes:
*
* 1) Implement <choices> with a list of valid strings
*
* 2) Implement <alias> by mapping one string to another
*
* 3) Implement enumerated types by mapping strings to integer values
* (and back).
*
* The map is made out of an array of uint32s. Each entry in the array
* is an integer value, followed by a specially formatted string value:
*
* The string starts with the byte 0xff or 0xfe, followed by the
* content of the string, followed by a nul byte, followed by
* additional nul bytes for padding, followed by a 0xff byte.
*
* Padding is added so that the entire formatted string takes up a
* multiple of 4 bytes, and not less than 8 bytes. The requirement
* for a string to take up 8 bytes is so that the scanner doesn't lose
* synch and mistake a string for an integer value.
*
* The first byte of the formatted string depends on if the integer is
* an enum value (0xff) or an alias (0xfe). If it is an alias then the
* number refers to the word offset within the info map at which the
* integer corresponding to the "target" value is stored.
*
* For example, consider the case of the string info map representing an
* enumerated type of 'foo' (value 1) and 'bar' (value 2) and 'baz'
* (alias for 'bar'). Note that string info maps are always little
* endian.
*
* x01 x00 x00 x00 xff 'f' 'o' 'o' x00 x00 x00 xff x02 x00 x00 x00
* xff 'b' 'a' 'r' x00 x00 x00 xff x03 x00 x00 x00 xfe 'b' 'a' 'z'
* x00 x00 x00 xff
*
*
* The operations that someone may want to perform with the map:
*
* - lookup if a string is valid (and not an alias)
* - lookup the integer value for a enum 'nick'
* - lookup the integer value for the target of an alias
* - lookup an alias and convert it to its target string
* - lookup the enum nick for a given value
*
* In order to lookup if a string is valid, it is padded on either side
* (as described) and scanned for in the array. For example, you might
* look for "foo":
*
* xff 'f' 'o' 'o' x00 x00 x00 xff
*
* In order to lookup the integer value for a nick, the string is padded
* on either side and scanned for in the array, as above. Instead of
* merely succeeding, we look at the integer value to the left of the
* match. This is the enum value.
*
* In order to lookup an alias and convert it to its target enum value,
* the string is padded on either side (as described, with 0xfe) and
* scanned for. For example, you might look for "baz":
*
* xfe 'b' 'a' 'z' x00 x00 x00 xff
*
* The integer immediately preceding the match then contains the offset
* of the integer value of the target. In our example, that's '3'.
* This index is dereferenced to find the enum value of '2'.
*
* To convert the alias to its target string, 5 bytes just need to be
* added past the start of the integer value to find the start of the
* string.
*
* To lookup the enum nick for a given value, the value is searched for
* in the array. To ensure that the value isn't matching the inside of a
* string, we must check that it is either the first item in the array or
* immediately preceded by the byte 0xff. It must also be immediately
* followed by the byte 0xff.
*
* Because strings always take up a minimum of 2 words, because 0xff or
* 0xfe never appear inside of a utf-8 string and because no two integer
* values ever appear in sequence, the only way we can have the
* sequence:
*
* xff __ __ __ __ xff (or 0xfe)
*
* is in the event of an integer nested between two strings.
*
* For implementation simplicity/efficiency, strings may not be more
* than 65 characters in length (ie: 17 32bit words after padding).
*
* In the event that we are doing <choices> (ie: not an enum type) then
* the value of each choice is set to zero and ignored.
*/
#define STRINFO_MAX_WORDS 17
G_GNUC_UNUSED static guint
strinfo_string_to_words (const gchar *string,
guint32 *words,
gboolean alias)
{
guint n_words;
gsize size;
size = strlen (string);
n_words = MAX (2, (size + 6) >> 2);
if (n_words > STRINFO_MAX_WORDS)
return FALSE;
words[0] = GUINT32_TO_LE (alias ? 0xfe : 0xff);
words[n_words - 1] = GUINT32_TO_BE (0xff);
memcpy (((gchar *) words) + 1, string, size + 1);
return n_words;
}
G_GNUC_UNUSED static gint
strinfo_scan (const guint32 *strinfo,
guint length,
const guint32 *words,
guint n_words)
{
guint i = 0;
if (length < n_words)
return -1;
while (i <= length - n_words)
{
guint j = 0;
for (j = 0; j < n_words; j++)
if (strinfo[i + j] != words[j])
break;
if (j == n_words)
return i; /* match */
/* skip at least one word, continue */
i += j ? j : 1;
}
return -1;
}
G_GNUC_UNUSED static gint
strinfo_find_string (const guint32 *strinfo,
guint length,
const gchar *string,
gboolean alias)
{
guint32 words[STRINFO_MAX_WORDS];
guint n_words;
if (length == 0)
return -1;
n_words = strinfo_string_to_words (string, words, alias);
return strinfo_scan (strinfo + 1, length - 1, words, n_words);
}
G_GNUC_UNUSED static gint
strinfo_find_integer (const guint32 *strinfo,
guint length,
guint32 value)
{
guint i;
for (i = 0; i < length; i++)
if (strinfo[i] == GUINT32_TO_LE (value))
{
const guchar *charinfo = (const guchar *) &strinfo[i];
/* make sure it has 0xff on either side */
if ((i == 0 || charinfo[-1] == 0xff) && charinfo[4] == 0xff)
return i;
}
return -1;
}
G_GNUC_UNUSED static gboolean
strinfo_is_string_valid (const guint32 *strinfo,
guint length,
const gchar *string)
{
return strinfo_find_string (strinfo, length, string, FALSE) != -1;
}
G_GNUC_UNUSED static gboolean
strinfo_enum_from_string (const guint32 *strinfo,
guint length,
const gchar *string,
guint *result)
{
gint index;
index = strinfo_find_string (strinfo, length, string, FALSE);
if (index < 0)
return FALSE;
*result = GUINT32_FROM_LE (strinfo[index]);
return TRUE;
}
G_GNUC_UNUSED static const gchar *
strinfo_string_from_enum (const guint32 *strinfo,
guint length,
guint value)
{
gint index;
index = strinfo_find_integer (strinfo, length, value);
if (index < 0)
return NULL;
return 1 + (const gchar *) &strinfo[index + 1];
}
G_GNUC_UNUSED static const gchar *
strinfo_string_from_alias (const guint32 *strinfo,
guint length,
const gchar *alias)
{
gint index;
index = strinfo_find_string (strinfo, length, alias, TRUE);
if (index < 0)
return NULL;
return 1 + (const gchar *) &strinfo[GUINT32_TO_LE (strinfo[index]) + 1];
}
G_GNUC_UNUSED static GVariant *
strinfo_enumerate (const guint32 *strinfo,
guint length)
{
GVariantBuilder builder;
const gchar *ptr, *end;
ptr = (gpointer) strinfo;
end = ptr + 4 * length;
ptr += 4;
g_variant_builder_init (&builder, G_VARIANT_TYPE_STRING_ARRAY);
while (ptr < end)
{
/* don't include aliases */
if (*ptr == '\xff')
g_variant_builder_add (&builder, "s", ptr + 1);
/* find the end of this string */
ptr = memchr (ptr, '\xff', end - ptr);
g_assert (ptr != NULL);
/* skip over the int to the next string */
ptr += 5;
}
return g_variant_builder_end (&builder);
}
G_GNUC_UNUSED static void
strinfo_builder_append_item (GString *builder,
const gchar *string,
guint value)
{
guint32 words[STRINFO_MAX_WORDS];
guint n_words;
value = GUINT32_TO_LE (value);
n_words = strinfo_string_to_words (string, words, FALSE);
g_string_append_len (builder, (void *) &value, sizeof value);
g_string_append_len (builder, (void *) words, 4 * n_words);
}
G_GNUC_UNUSED static gboolean
strinfo_builder_append_alias (GString *builder,
const gchar *alias,
const gchar *target)
{
guint32 words[STRINFO_MAX_WORDS];
guint n_words;
guint value;
gint index;
index = strinfo_find_string ((const guint32 *) builder->str,
builder->len / 4, target, FALSE);
if (index == -1)
return FALSE;
value = GUINT32_TO_LE (index);
n_words = strinfo_string_to_words (alias, words, TRUE);
g_string_append_len (builder, (void *) &value, sizeof value);
g_string_append_len (builder, (void *) words, 4 * n_words);
return TRUE;
}
G_GNUC_UNUSED static gboolean
strinfo_builder_contains (GString *builder,
const gchar *string)
{
return strinfo_find_string ((const guint32 *) builder->str,
builder->len / 4, string, FALSE) != -1 ||
strinfo_find_string ((const guint32 *) builder->str,
builder->len / 4, string, TRUE) != -1;
}
G_GNUC_UNUSED static gboolean
strinfo_builder_contains_value (GString *builder,
guint value)
{
return strinfo_string_from_enum ((const guint32 *) builder->str,
builder->len / 4, value) != NULL;
}