ber.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * WLDAP32 - LDAP support for Wine
 *
 * Copyright 2005 Hans Leidekker
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22
 */

#include "config.h"

Mike McCormack's avatar
Mike McCormack committed
23
#include <stdarg.h>
24
#include "windef.h"
Mike McCormack's avatar
Mike McCormack committed
25
#include "winbase.h"
26
#include "winldap.h"
27 28 29
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
30

31 32 33
#ifndef LBER_ERROR
# define LBER_ERROR (~0U)
#endif
34

35 36
/***********************************************************************
 *      ber_alloc_t     (WLDAP32.@)
37 38 39 40 41 42 43 44 45 46 47 48
 *
 * Allocate a berelement structure.
 *
 * PARAMS
 *  options [I] Must be LBER_USE_DER.
 *
 * RETURNS
 *  Success: Pointer to an allocated berelement structure.
 *  Failure: NULL
 *
 * NOTES
 *  Free the berelement structure with ber_free.
49
 */
50
BerElement * CDECL WLDAP32_ber_alloc_t( INT options )
51
{
52 53 54
#ifdef HAVE_LDAP
    return ber_alloc_t( options );
#else
55
    return NULL;
56
#endif
57 58
}

59 60 61

/***********************************************************************
 *      ber_bvdup     (WLDAP32.@)
62 63 64 65 66 67 68 69 70 71 72 73
 *
 * Copy a berval structure.
 *
 * PARAMS
 *  berval [I] Pointer to the berval structure to be copied.
 *
 * RETURNS
 *  Success: Pointer to a copy of the berval structure.
 *  Failure: NULL
 *
 * NOTES
 *  Free the copy with ber_bvfree.
74
 */
75
BERVAL * CDECL WLDAP32_ber_bvdup( BERVAL *berval )
76
{
77 78 79
#ifdef HAVE_LDAP
    return ber_bvdup( berval );
#else
80
    return NULL;
81
#endif
82 83
}

84 85 86

/***********************************************************************
 *      ber_bvecfree     (WLDAP32.@)
87 88 89 90 91 92 93 94 95 96 97 98
 *
 * Free an array of berval structures.
 *
 * PARAMS
 *  berval [I] Pointer to an array of berval structures.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  Use this function only to free an array of berval structures
 *  returned by a call to ber_scanf with a 'V' in the format string.
99
 */
100
void CDECL WLDAP32_ber_bvecfree( PBERVAL *berval )
101
{
102 103 104
#ifdef HAVE_LDAP
    ber_bvecfree( berval );
#endif
105 106
}

107 108 109

/***********************************************************************
 *      ber_bvfree     (WLDAP32.@)
110 111 112 113 114 115 116 117 118 119 120 121
 *
 * Free a berval structure.
 *
 * PARAMS
 *  berval [I] Pointer to a berval structure.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  Use this function only to free berval structures allocated by
 *  an LDAP API.
122
 */
123
void CDECL WLDAP32_ber_bvfree( BERVAL *berval )
124
{
125 126 127
#ifdef HAVE_LDAP
    ber_bvfree( berval );
#endif
128 129
}

130 131 132

/***********************************************************************
 *      ber_first_element     (WLDAP32.@)
133 134 135 136 137 138 139 140 141 142 143 144 145 146
 *
 * Return the tag of the first element in a set or sequence.
 *
 * PARAMS
 *  berelement [I] Pointer to a berelement structure.
 *  len        [O] Receives the length of the first element.
 *  opaque     [O] Receives a pointer to a cookie.
 *
 * RETURNS
 *  Success: Tag of the first element.
 *  Failure: LBER_DEFAULT (no more data).
 *
 * NOTES
 *  len and cookie should be passed to ber_next_element.
147
 */
148
ULONG CDECL WLDAP32_ber_first_element( BerElement *berelement, ULONG *len, CHAR **opaque )
149
{
150 151 152
#ifdef HAVE_LDAP
    return ber_first_element( berelement, len, opaque );
#else
153
    return LBER_ERROR;
154
#endif
155 156
}

157 158 159

/***********************************************************************
 *      ber_flatten     (WLDAP32.@)
160 161 162 163 164 165 166 167 168 169 170 171 172
 *
 * Flatten a berelement structure into a berval structure.
 *
 * PARAMS
 *  berlement [I] Pointer to a berelement structure.
 *  berval    [O] Pointer to a berval structure.
 *
 * RETURNS
 *  Success: 0
 *  Failure: LBER_ERROR
 *
 * NOTES
 *  Free the berval structure with ber_bvfree.
173
 */
174
INT CDECL WLDAP32_ber_flatten( BerElement *berelement, PBERVAL *berval )
175
{
176 177 178
#ifdef HAVE_LDAP
    return ber_flatten( berelement, berval );
#else
179
    return LBER_ERROR;
180
#endif
181 182
}

183 184 185

/***********************************************************************
 *      ber_free     (WLDAP32.@)
186 187 188 189 190 191 192 193 194 195 196 197 198
 *
 * Free a berelement structure.
 *
 * PARAMS
 *  berlement [I] Pointer to the berelement structure to be freed.
 *  buf       [I] Flag.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  Set buf to 0 if the berelement was allocated with ldap_first_attribute
 *  or ldap_next_attribute, otherwise set it to 1.
199
 */
200
void CDECL WLDAP32_ber_free( BerElement *berelement, INT buf )
201
{
202 203 204
#ifdef HAVE_LDAP
    ber_free( berelement, buf );
#endif
205 206
}

207 208 209

/***********************************************************************
 *      ber_init     (WLDAP32.@)
210 211 212 213 214 215 216 217 218 219 220 221
 *
 * Initialise a berelement structure from a berval structure.
 *
 * PARAMS
 *  berval [I] Pointer to a berval structure.
 *
 * RETURNS
 *  Success: Pointer to a berelement structure.
 *  Failure: NULL
 *
 * NOTES
 *  Call ber_free to free the returned berelement structure.
222
 */
223
BerElement * CDECL WLDAP32_ber_init( BERVAL *berval )
224
{
225 226 227
#ifdef HAVE_LDAP
    return ber_init( berval );
#else
228
    return NULL;
229
#endif
230 231
}

232 233 234

/***********************************************************************
 *      ber_next_element     (WLDAP32.@)
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
 *
 * Return the tag of the next element in a set or sequence.
 *
 * PARAMS
 *  berelement [I]   Pointer to a berelement structure.
 *  len        [I/O] Receives the length of the next element.
 *  opaque     [I/O] Pointer to a cookie.
 *
 * RETURNS
 *  Success: Tag of the next element.
 *  Failure: LBER_DEFAULT (no more data).
 *
 * NOTES
 *  len and cookie are intitialised by ber_first_element and should
 *  be passed on in subsequent calls to ber_next_element.
250
 */
251
ULONG CDECL WLDAP32_ber_next_element( BerElement *berelement, ULONG *len, CHAR *opaque )
252
{
253 254 255
#ifdef HAVE_LDAP
    return ber_next_element( berelement, len, opaque );
#else
256
    return LBER_ERROR;
257
#endif
258 259
}

260 261 262

/***********************************************************************
 *      ber_peek_tag     (WLDAP32.@)
263 264 265 266 267 268 269 270 271 272
 *
 * Return the tag of the next element.
 *
 * PARAMS
 *  berelement [I] Pointer to a berelement structure.
 *  len        [O] Receives the length of the next element.
 *
 * RETURNS
 *  Success: Tag of the next element.
 *  Failure: LBER_DEFAULT (no more data).
273
 */
274
ULONG CDECL WLDAP32_ber_peek_tag( BerElement *berelement, ULONG *len )
275
{
276 277 278
#ifdef HAVE_LDAP
    return ber_peek_tag( berelement, len );
#else
279
    return LBER_ERROR;
280
#endif
281 282
}

283 284 285

/***********************************************************************
 *      ber_skip_tag     (WLDAP32.@)
286 287 288 289 290 291 292 293 294 295
 *
 * Skip the current tag and return the tag of the next element.
 *
 * PARAMS
 *  berelement [I] Pointer to a berelement structure.
 *  len        [O] Receives the length of the skipped element.
 *
 * RETURNS
 *  Success: Tag of the next element.
 *  Failure: LBER_DEFAULT (no more data).
296
 */
297
ULONG CDECL WLDAP32_ber_skip_tag( BerElement *berelement, ULONG *len )
298
{
299 300 301
#ifdef HAVE_LDAP
    return ber_skip_tag( berelement, len );
#else
302
    return LBER_ERROR;
303
#endif
304 305
}

306

307 308
/***********************************************************************
 *      ber_printf     (WLDAP32.@)
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
 *
 * Encode a berelement structure.
 *
 * PARAMS
 *  berelement [I/O] Pointer to a berelement structure.
 *  fmt        [I]   Format string.
 *  ...        [I]   Values to encode.
 *
 * RETURNS
 *  Success: Non-negative number. 
 *  Failure: LBER_ERROR
 *
 * NOTES
 *  berelement must have been allocated with ber_alloc_t. This function
 *  can be called multiple times to append data.
324
 */
325
INT CDECL WLDAP32_ber_printf( BerElement *berelement, PCHAR fmt, ... )
326
{
327 328 329 330 331 332 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 363 364 365 366 367 368 369 370 371 372 373 374 375
#ifdef HAVE_LDAP
    va_list list;
    int ret = 0;
    char new_fmt[2];

    new_fmt[1] = 0;
    va_start( list, fmt );
    while (*fmt)
    {
        new_fmt[0] = *fmt++;
        switch(new_fmt[0])
        {
        case 'b':
        case 'e':
        case 'i':
            {
                int i = va_arg( list, int );
                ret = ber_printf( berelement, new_fmt, i );
                break;
            }
        case 'o':
        case 's':
            {
                char *str = va_arg( list, char * );
                ret = ber_printf( berelement, new_fmt, str );
                break;
            }
        case 't':
            {
                unsigned int tag = va_arg( list, unsigned int );
                ret = ber_printf( berelement, new_fmt, tag );
                break;
            }
        case 'v':
            {
                char **array = va_arg( list, char ** );
                ret = ber_printf( berelement, new_fmt, array );
                break;
            }
        case 'V':
            {
                struct berval **array = va_arg( list, struct berval ** );
                ret = ber_printf( berelement, new_fmt, array );
                break;
            }
        case 'X':
            {
                char *str = va_arg( list, char * );
                int len = va_arg( list, int );
376 377
                new_fmt[0] = 'B';  /* 'X' is deprecated */
                ret = ber_printf( berelement, new_fmt, str, len );
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
                break;
            }
        case 'n':
        case '{':
        case '}':
        case '[':
        case ']':
            ret = ber_printf( berelement, new_fmt );
            break;
        default:
            FIXME( "Unknown format '%c'\n", new_fmt[0] );
            ret = -1;
            break;
        }
        if (ret == -1) break;
    }
    va_end( list );
    return ret;
#else
397
    return LBER_ERROR;
398
#endif
399 400
}

401

402 403
/***********************************************************************
 *      ber_scanf     (WLDAP32.@)
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
 *
 * Decode a berelement structure.
 *
 * PARAMS
 *  berelement [I/O] Pointer to a berelement structure.
 *  fmt        [I]   Format string.
 *  ...        [I]   Pointers to values to be decoded.
 *
 * RETURNS
 *  Success: Non-negative number. 
 *  Failure: LBER_ERROR
 *
 * NOTES
 *  berelement must have been allocated with ber_init. This function
 *  can be called multiple times to decode data.
419
 */
420
INT CDECL WLDAP32_ber_scanf( BerElement *berelement, PCHAR fmt, ... )
421
{
422 423 424 425 426 427 428 429 430 431 432 433 434 435 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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
#ifdef HAVE_LDAP
    va_list list;
    int ret = 0;
    char new_fmt[2];

    new_fmt[1] = 0;
    va_start( list, fmt );
    while (*fmt)
    {
        new_fmt[0] = *fmt++;
        switch(new_fmt[0])
        {
        case 'a':
            {
                char **ptr = va_arg( list, char ** );
                ret = ber_scanf( berelement, new_fmt, ptr );
                break;
            }
        case 'b':
        case 'e':
        case 'i':
            {
                int *i = va_arg( list, int * );
                ret = ber_scanf( berelement, new_fmt, i );
                break;
            }
        case 't':
            {
                unsigned int *tag = va_arg( list, unsigned int * );
                ret = ber_scanf( berelement, new_fmt, tag );
                break;
            }
        case 'v':
            {
                char ***array = va_arg( list, char *** );
                ret = ber_scanf( berelement, new_fmt, array );
                break;
            }
        case 'B':
            {
                char **str = va_arg( list, char ** );
                int *len = va_arg( list, int * );
                ret = ber_scanf( berelement, new_fmt, str, len );
                break;
            }
        case 'O':
            {
                struct berval **ptr = va_arg( list, struct berval ** );
                ret = ber_scanf( berelement, new_fmt, ptr );
                break;
            }
        case 'V':
            {
                struct berval ***array = va_arg( list, struct berval *** );
                ret = ber_scanf( berelement, new_fmt, array );
                break;
            }
        case 'n':
        case 'x':
        case '{':
        case '}':
        case '[':
        case ']':
            ret = ber_scanf( berelement, new_fmt );
            break;
        default:
            FIXME( "Unknown format '%c'\n", new_fmt[0] );
            ret = -1;
            break;
        }
        if (ret == -1) break;
    }
    va_end( list );
    return ret;
#else
497
    return LBER_ERROR;
498
#endif
499
}