string.c 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2014 Martin Storsjo
 *
 * 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.1 of the License, 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <string.h>

#include "windows.h"
#include "winerror.h"
#include "hstring.h"
24
#include "wine/unicode.h"
25
#include "wine/debug.h"
26

27
WINE_DEFAULT_DEBUG_CHANNEL(winstring);
28 29 30 31 32 33 34 35 36

struct hstring_private
{
    LPWSTR buffer;
    UINT32 length;
    BOOL   reference;
    LONG   refcount;
};

37 38
static const WCHAR empty[1];

39 40 41 42 43 44 45 46 47 48 49 50
C_ASSERT(sizeof(struct hstring_private) <= sizeof(HSTRING_HEADER));

static inline struct hstring_private *impl_from_HSTRING(HSTRING string)
{
   return (struct hstring_private *)string;
}

static inline struct hstring_private *impl_from_HSTRING_HEADER(HSTRING_HEADER *header)
{
   return (struct hstring_private *)header;
}

51 52 53 54 55
static inline struct hstring_private *impl_from_HSTRING_BUFFER(HSTRING_BUFFER buffer)
{
   return (struct hstring_private *)buffer;
}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static BOOL alloc_string(UINT32 len, HSTRING *out)
{
    struct hstring_private *priv;
    priv = HeapAlloc(GetProcessHeap(), 0, sizeof(*priv) + (len + 1) * sizeof(*priv->buffer));
    if (!priv)
        return FALSE;
    priv->buffer = (LPWSTR)(priv + 1);
    priv->length = len;
    priv->reference = FALSE;
    priv->refcount = 1;
    priv->buffer[len] = '\0';
    *out = (HSTRING)priv;
    return TRUE;
}

/***********************************************************************
 *      WindowsCreateString (combase.@)
 */
HRESULT WINAPI WindowsCreateString(LPCWSTR ptr, UINT32 len,
                                   HSTRING *out)
{
    struct hstring_private *priv;
78 79 80

    TRACE("(%s, %u, %p)\n", debugstr_wn(ptr, len), len, out);

81 82 83 84 85 86 87
    if (out == NULL)
        return E_INVALIDARG;
    if (len == 0)
    {
        *out = NULL;
        return S_OK;
    }
88 89
    if (ptr == NULL)
        return E_POINTER;
90 91 92 93 94 95 96 97 98 99 100 101 102 103
    if (!alloc_string(len, out))
        return E_OUTOFMEMORY;
    priv = impl_from_HSTRING(*out);
    memcpy(priv->buffer, ptr, len * sizeof(*priv->buffer));
    return S_OK;
}

/***********************************************************************
 *      WindowsCreateStringReference (combase.@)
 */
HRESULT WINAPI WindowsCreateStringReference(LPCWSTR ptr, UINT32 len,
                                            HSTRING_HEADER *header, HSTRING *out)
{
    struct hstring_private *priv = impl_from_HSTRING_HEADER(header);
104 105 106

    TRACE("(%s, %u, %p, %p)\n", debugstr_wn(ptr, len), len, header, out);

107 108
    if (out == NULL || header == NULL)
        return E_INVALIDARG;
109 110
    if (ptr != NULL && ptr[len] != '\0')
        return E_INVALIDARG;
111 112 113 114 115
    if (len == 0)
    {
        *out = NULL;
        return S_OK;
    }
116 117
    if (ptr == NULL)
        return E_POINTER;
118 119 120 121 122 123 124 125 126 127 128 129 130
    priv->buffer = (LPWSTR)ptr;
    priv->length = len;
    priv->reference = TRUE;
    *out = (HSTRING)header;
    return S_OK;
}

/***********************************************************************
 *      WindowsDeleteString (combase.@)
 */
HRESULT WINAPI WindowsDeleteString(HSTRING str)
{
    struct hstring_private *priv = impl_from_HSTRING(str);
131 132 133

    TRACE("(%p)\n", str);

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    if (str == NULL)
        return S_OK;
    if (priv->reference)
        return S_OK;
    if (InterlockedDecrement(&priv->refcount) == 0)
        HeapFree(GetProcessHeap(), 0, priv);
    return S_OK;
}

/***********************************************************************
 *      WindowsDuplicateString (combase.@)
 */
HRESULT WINAPI WindowsDuplicateString(HSTRING str, HSTRING *out)
{
    struct hstring_private *priv = impl_from_HSTRING(str);
149 150 151

    TRACE("(%p, %p)\n", str, out);

152 153 154 155 156 157 158 159 160 161 162 163 164
    if (out == NULL)
        return E_INVALIDARG;
    if (str == NULL)
    {
        *out = NULL;
        return S_OK;
    }
    if (priv->reference)
        return WindowsCreateString(priv->buffer, priv->length, out);
    InterlockedIncrement(&priv->refcount);
    *out = str;
    return S_OK;
}
165

166 167 168 169 170 171 172 173
/***********************************************************************
 *      WindowsPreallocateStringBuffer (combase.@)
 */
HRESULT WINAPI WindowsPreallocateStringBuffer(UINT32 len, WCHAR **outptr,
                                              HSTRING_BUFFER *out)
{
    struct hstring_private *priv;
    HSTRING str;
174 175 176

    TRACE("(%u, %p, %p)\n", len, outptr, out);

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    if (outptr == NULL || out == NULL)
        return E_POINTER;
    if (len == 0)
    {
        *outptr = (LPWSTR)empty;
        *out = NULL;
        return S_OK;
    }

    if (!alloc_string(len, &str))
        return E_OUTOFMEMORY;
    priv = impl_from_HSTRING(str);
    *outptr = priv->buffer;
    *out = (HSTRING_BUFFER)str;
    return S_OK;
}

/***********************************************************************
 *      WindowsDeleteStringBuffer (combase.@)
 */
HRESULT WINAPI WindowsDeleteStringBuffer(HSTRING_BUFFER buf)
{
199 200
    TRACE("(%p)\n", buf);

201 202 203 204 205 206 207 208 209
    return WindowsDeleteString((HSTRING)buf);
}

/***********************************************************************
 *      WindowsPromoteStringBuffer (combase.@)
 */
HRESULT WINAPI WindowsPromoteStringBuffer(HSTRING_BUFFER buf, HSTRING *out)
{
    struct hstring_private *priv = impl_from_HSTRING_BUFFER(buf);
210 211 212

    TRACE("(%p, %p)\n", buf, out);

213 214 215 216 217 218 219 220 221 222 223 224 225
    if (out == NULL)
        return E_POINTER;
    if (buf == NULL)
    {
        *out = NULL;
        return S_OK;
    }
    if (priv->buffer[priv->length] != 0 || priv->reference || priv->refcount != 1)
        return E_INVALIDARG;
    *out = (HSTRING)buf;
    return S_OK;
}

226 227 228 229 230 231
/***********************************************************************
 *      WindowsGetStringLen (combase.@)
 */
UINT32 WINAPI WindowsGetStringLen(HSTRING str)
{
    struct hstring_private *priv = impl_from_HSTRING(str);
232 233 234

    TRACE("(%p)\n", str);

235 236 237 238 239 240 241 242 243 244 245
    if (str == NULL)
        return 0;
    return priv->length;
}

/***********************************************************************
 *      WindowsGetStringRawBuffer (combase.@)
 */
LPCWSTR WINAPI WindowsGetStringRawBuffer(HSTRING str, UINT32 *len)
{
    struct hstring_private *priv = impl_from_HSTRING(str);
246 247 248

    TRACE("(%p, %p)\n", str, len);

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    if (str == NULL)
    {
        if (len)
            *len = 0;
        return empty;
    }
    if (len)
        *len = priv->length;
    return priv->buffer;
}

/***********************************************************************
 *      WindowsStringHasEmbeddedNull (combase.@)
 */
HRESULT WINAPI WindowsStringHasEmbeddedNull(HSTRING str, BOOL *out)
{
    UINT32 i;
    struct hstring_private *priv = impl_from_HSTRING(str);
267 268 269

    TRACE("(%p, %p)\n", str, out);

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    if (out == NULL)
        return E_INVALIDARG;
    if (str == NULL)
    {
        *out = FALSE;
        return S_OK;
    }
    for (i = 0; i < priv->length; i++)
    {
        if (priv->buffer[i] == '\0')
        {
            *out = TRUE;
            return S_OK;
        }
    }
    *out = FALSE;
    return S_OK;
}

289 290 291 292 293 294 295
/***********************************************************************
 *      WindowsSubstring (combase.@)
 */
HRESULT WINAPI WindowsSubstring(HSTRING str, UINT32 start, HSTRING *out)
{
    struct hstring_private *priv = impl_from_HSTRING(str);
    UINT32 len = WindowsGetStringLen(str);
296 297 298

    TRACE("(%p, %u, %p)\n", str, start, out);

299 300 301 302 303 304 305 306 307 308 309 310
    if (out == NULL)
        return E_INVALIDARG;
    if (start > len)
        return E_BOUNDS;
    if (start == len)
    {
        *out = NULL;
        return S_OK;
    }
    return WindowsCreateString(&priv->buffer[start], len - start, out);
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/***********************************************************************
 *      WindowsSubstringWithSpecifiedLength (combase.@)
 */
HRESULT WINAPI WindowsSubstringWithSpecifiedLength(HSTRING str, UINT32 start, UINT32 len, HSTRING *out)
{
    struct hstring_private *priv = impl_from_HSTRING(str);

    TRACE("(%p, %u, %u, %p)\n", str, start, len, out);

    if (out == NULL)
        return E_INVALIDARG;
    if (start + len < start ||
        start + len > WindowsGetStringLen(str))
        return E_BOUNDS;
    if (len == 0)
    {
        *out = NULL;
        return S_OK;
    }
    return WindowsCreateString(&priv->buffer[start], len, out);
}

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
/***********************************************************************
 *      WindowsConcatString (combase.@)
 */
HRESULT WINAPI WindowsConcatString(HSTRING str1, HSTRING str2, HSTRING *out)
{
    struct hstring_private *priv1 = impl_from_HSTRING(str1);
    struct hstring_private *priv2 = impl_from_HSTRING(str2);
    struct hstring_private *priv;

    TRACE("(%p, %p, %p)\n", str1, str2, out);

    if (out == NULL)
        return E_INVALIDARG;
    if (str1 == NULL)
        return WindowsDuplicateString(str2, out);
    if (str2 == NULL)
        return WindowsDuplicateString(str1, out);
    if (!priv1->length && !priv2->length)
    {
        *out = NULL;
        return S_OK;
    }
    if (!alloc_string(priv1->length + priv2->length, out))
        return E_OUTOFMEMORY;
    priv = impl_from_HSTRING(*out);
    memcpy(priv->buffer, priv1->buffer, priv1->length * sizeof(*priv1->buffer));
    memcpy(priv->buffer + priv1->length, priv2->buffer, priv2->length * sizeof(*priv2->buffer));
    return S_OK;
}

363 364 365 366 367 368
/***********************************************************************
 *      WindowsIsStringEmpty (combase.@)
 */
BOOL WINAPI WindowsIsStringEmpty(HSTRING str)
{
    struct hstring_private *priv = impl_from_HSTRING(str);
369 370 371

    TRACE("(%p)\n", str);

372 373 374 375
    if (str == NULL)
        return TRUE;
    return priv->length == 0;
}
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

/***********************************************************************
 *      WindowsCompareStringOrdinal (combase.@)
 */
HRESULT WINAPI WindowsCompareStringOrdinal(HSTRING str1, HSTRING str2, INT32 *res)
{
    struct hstring_private *priv1 = impl_from_HSTRING(str1);
    struct hstring_private *priv2 = impl_from_HSTRING(str2);
    const WCHAR *buf1 = empty, *buf2 = empty;
    UINT32 len1 = 0, len2 = 0;

    TRACE("(%p, %p, %p)\n", str1, str2, res);

    if (res == NULL)
        return E_INVALIDARG;
    if (str1 == str2)
    {
        *res = 0;
        return S_OK;
    }
    if (str1)
    {
        buf1 = priv1->buffer;
        len1 = priv1->length;
    }
    if (str2)
    {
        buf2 = priv2->buffer;
        len2 = priv2->length;
    }
    *res = CompareStringOrdinal(buf1, len1, buf2, len2, FALSE) - CSTR_EQUAL;
    return S_OK;
}
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

/***********************************************************************
 *      WindowsTrimStringStart (combase.@)
 */
HRESULT WINAPI WindowsTrimStringStart(HSTRING str1, HSTRING str2, HSTRING *out)
{
    struct hstring_private *priv1 = impl_from_HSTRING(str1);
    struct hstring_private *priv2 = impl_from_HSTRING(str2);
    UINT32 start;

    TRACE("(%p, %p, %p)\n", str1, str2, out);

    if (!out || !str2 || !priv2->length)
        return E_INVALIDARG;
    if (!str1)
    {
        *out = NULL;
        return S_OK;
    }
    for (start = 0; start < priv1->length; start++)
    {
        if (!memchrW(priv2->buffer, priv1->buffer[start], priv2->length))
            break;
    }
    return start ? WindowsCreateString(&priv1->buffer[start], priv1->length - start, out) :
                   WindowsDuplicateString(str1, out);
}
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

/***********************************************************************
 *      WindowsTrimStringEnd (combase.@)
 */
HRESULT WINAPI WindowsTrimStringEnd(HSTRING str1, HSTRING str2, HSTRING *out)
{
    struct hstring_private *priv1 = impl_from_HSTRING(str1);
    struct hstring_private *priv2 = impl_from_HSTRING(str2);
    UINT32 len;

    TRACE("(%p, %p, %p)\n", str1, str2, out);

    if (!out || !str2 || !priv2->length)
        return E_INVALIDARG;
    if (!str1)
    {
        *out = NULL;
        return S_OK;
    }
    for (len = priv1->length; len > 0; len--)
    {
        if (!memchrW(priv2->buffer, priv1->buffer[len - 1], priv2->length))
            break;
    }
    return (len < priv1->length) ? WindowsCreateString(priv1->buffer, len, out) :
                                   WindowsDuplicateString(str1, out);
}