dbus-string-util.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-string-util.c Would be in dbus-string.c, but not used in libdbus
*
* Copyright 2002-2008 Red Hat, Inc.
* Copyright 2003 CodeFactory AB
* Copyright 2003 Mark McLoughlin
* Copyright 2004 Michael Meeks
* Copyright 2006-2015 Ralf Habacker <ralf.habacker@freenet.de>
* Copyright 2007 Allison Lortie
* Copyright 2011 Roberto Guido
* Copyright 2016-2018 Collabora Ltd.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* 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 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-internals.h"
#include "dbus-string.h"
#define DBUS_CAN_USE_DBUS_STRING_PRIVATE 1
#include "dbus-string-private.h"
dbus_bool_t
_dbus_string_ends_with_c_str (const DBusString *a,
const char *c_str)
{
const unsigned char *ap;
const unsigned char *bp;
const unsigned char *a_end;
unsigned long c_str_len;
const DBusRealString *real_a = (const DBusRealString*) a;
DBUS_GENERIC_STRING_PREAMBLE (real_a);
_dbus_assert (c_str != NULL);
c_str_len = strlen (c_str);
if (((unsigned long)real_a->len) < c_str_len)
return FALSE;
ap = real_a->str + (real_a->len - c_str_len);
bp = (const unsigned char*) c_str;
a_end = real_a->str + real_a->len;
while (ap != a_end)
{
if (*ap != *bp)
return FALSE;
++ap;
++bp;
}
_dbus_assert (*ap == '\0');
_dbus_assert (*bp == '\0');
return TRUE;
}
dbus_bool_t
_dbus_string_find_byte_backward (const DBusString *str,
int start,
unsigned char byte,
int *found)
{
int i;
DBUS_CONST_STRING_PREAMBLE (str);
_dbus_assert (start <= real->len);
_dbus_assert (start >= 0);
_dbus_assert (found != NULL);
i = start - 1;
while (i >= 0)
{
if (real->str[i] == byte)
break;
--i;
}
if (found)
*found = i;
return i >= 0;
}
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
DBUS_CONST_STRING_PREAMBLE
#define DBUS_CONST_STRING_PREAMBLE(str)
Checks assertions about a string that may be const or locked.
Definition dbus-string-private.h:116
DBUS_GENERIC_STRING_PREAMBLE
#define DBUS_GENERIC_STRING_PREAMBLE(real)
Checks a bunch of assertions about a string object.
Definition dbus-string-private.h:78
_dbus_string_ends_with_c_str
dbus_bool_t _dbus_string_ends_with_c_str(const DBusString *a, const char *c_str)
Returns whether a string ends with the given suffix.
Definition dbus-string-util.c:54
_dbus_string_find_byte_backward
dbus_bool_t _dbus_string_find_byte_backward(const DBusString *str, int start, unsigned char byte, int *found)
Find the given byte scanning backward from the given start.
Definition dbus-string-util.c:98
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusRealString
Internals of DBusString.
Definition dbus-string-private.h:46
DBusRealString::str
unsigned char * str
String data, plus nul termination.
Definition dbus-string-private.h:47
DBusRealString::len
int len
Length without nul.
Definition dbus-string-private.h:48
DBusString
Definition dbus-string.h:47