decode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /* decode.c - ber input decoding routines */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2024 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. /* Portions Copyright (c) 1990 Regents of the University of Michigan.
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms are permitted
  20. * provided that this notice is preserved and that due credit is given
  21. * to the University of Michigan at Ann Arbor. The name of the University
  22. * may not be used to endorse or promote products derived from this
  23. * software without specific prior written permission. This software
  24. * is provided ``as is'' without express or implied warranty.
  25. */
  26. /* ACKNOWLEDGEMENTS:
  27. * This work was originally developed by the University of Michigan
  28. * (as part of U-MICH LDAP).
  29. */
  30. #include "portable.h"
  31. #include <stdio.h>
  32. #include <ac/stdlib.h>
  33. #include <ac/stdarg.h>
  34. #include <ac/string.h>
  35. #include <ac/socket.h>
  36. #include "lber-int.h"
  37. /* out->bv_len should be the buffer size on input */
  38. int
  39. ber_decode_oid( BerValue *in, BerValue *out )
  40. {
  41. const unsigned char *der;
  42. unsigned long val;
  43. unsigned val1;
  44. ber_len_t i;
  45. char *ptr;
  46. assert( in != NULL );
  47. assert( out != NULL );
  48. /* need 4 chars/inbyte + \0 for input={7f 7f 7f...} */
  49. if ( !out->bv_val || (out->bv_len+3)/4 <= in->bv_len )
  50. return -1;
  51. ptr = NULL;
  52. der = (unsigned char *) in->bv_val;
  53. val = 0;
  54. for ( i=0; i < in->bv_len; i++ ) {
  55. val |= der[i] & 0x7f;
  56. if ( !( der[i] & 0x80 )) {
  57. if ( ptr == NULL ) {
  58. /* Initial "x.y": val=x*40+y, x<=2, y<40 if x<2 */
  59. ptr = out->bv_val;
  60. val1 = (val < 80 ? val/40 : 2);
  61. val -= val1*40;
  62. ptr += sprintf( ptr, "%u", val1 );
  63. }
  64. ptr += sprintf( ptr, ".%lu", val );
  65. val = 0;
  66. } else if ( val - 1UL < LBER_OID_COMPONENT_MAX >> 7 ) {
  67. val <<= 7;
  68. } else {
  69. /* val would overflow, or is 0 from invalid initial 0x80 octet */
  70. return -1;
  71. }
  72. }
  73. if ( ptr == NULL || val != 0 )
  74. return -1;
  75. out->bv_len = ptr - out->bv_val;
  76. return 0;
  77. }
  78. /* Return tag, with *bv = rest of element (starting at length octets) */
  79. static ber_tag_t
  80. ber_tag_and_rest( const BerElement *ber, struct berval *bv )
  81. {
  82. ber_tag_t tag;
  83. ptrdiff_t rest;
  84. unsigned char *ptr;
  85. assert( ber != NULL );
  86. assert( LBER_VALID( ber ) );
  87. ptr = (unsigned char *) ber->ber_ptr;
  88. rest = (unsigned char *) ber->ber_end - ptr;
  89. if ( rest <= 0 ) {
  90. goto fail;
  91. }
  92. tag = ber->ber_tag;
  93. if ( (char *) ptr == ber->ber_buf ) {
  94. tag = *ptr;
  95. }
  96. ptr++;
  97. rest--;
  98. if ( (tag & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {
  99. goto done;
  100. }
  101. do {
  102. if ( rest <= 0 ) {
  103. break;
  104. }
  105. tag <<= 8;
  106. tag |= *ptr++ & 0xffU;
  107. rest--;
  108. if ( ! (tag & LBER_MORE_TAG_MASK) ) {
  109. goto done;
  110. }
  111. } while ( tag <= (ber_tag_t)-1 / 256 );
  112. fail:
  113. /* Error or unsupported tag size */
  114. tag = LBER_DEFAULT;
  115. done:
  116. bv->bv_len = rest;
  117. bv->bv_val = (char *) ptr;
  118. return tag;
  119. }
  120. /* Return the tag - LBER_DEFAULT returned means trouble */
  121. ber_tag_t
  122. ber_get_tag( BerElement *ber )
  123. {
  124. struct berval bv;
  125. ber_tag_t tag = ber_tag_and_rest( ber, &bv );
  126. ber->ber_ptr = bv.bv_val;
  127. return tag;
  128. }
  129. /* Return next element's tag and point *bv at its contents in-place */
  130. ber_tag_t
  131. ber_peek_element( const BerElement *ber, struct berval *bv )
  132. {
  133. ber_tag_t tag;
  134. ber_len_t len, rest;
  135. unsigned i;
  136. unsigned char *ptr;
  137. assert( bv != NULL );
  138. /*
  139. * Any ber element looks like this: tag length contents.
  140. * Assuming everything's ok, we return the tag, and point
  141. * bv at the contents.
  142. *
  143. * Assumptions:
  144. * 1) definite lengths
  145. * 2) primitive encodings used whenever possible
  146. */
  147. len = 0;
  148. /*
  149. * First, we read the tag.
  150. */
  151. tag = ber_tag_and_rest( ber, bv );
  152. rest = bv->bv_len;
  153. ptr = (unsigned char *) bv->bv_val;
  154. if ( tag == LBER_DEFAULT || rest == 0 ) {
  155. goto fail;
  156. }
  157. /*
  158. * Next, read the length. The first octet determines the length
  159. * of the length. If bit 8 is 0, the length is the short form,
  160. * otherwise if the octet != 0x80 it's the long form, otherwise
  161. * the ber element has the unsupported indefinite-length format.
  162. * Lengths that do not fit in a ber_len_t are not accepted.
  163. */
  164. len = *ptr++;
  165. rest--;
  166. if ( len & 0x80U ) {
  167. len &= 0x7fU;
  168. if ( len - 1U > sizeof(ber_len_t) - 1U || rest < len ) {
  169. /* Indefinite-length/too long length/not enough data */
  170. goto fail;
  171. }
  172. rest -= len;
  173. i = len;
  174. for( len = *ptr++ & 0xffU; --i; len |= *ptr++ & 0xffU ) {
  175. len <<= 8;
  176. }
  177. }
  178. /* BER element should have enough data left */
  179. if( len > rest ) {
  180. fail:
  181. tag = LBER_DEFAULT;
  182. }
  183. bv->bv_len = len;
  184. bv->bv_val = (char *) ptr;
  185. return tag;
  186. }
  187. /* Move past next element, point *bv at it in-place, and return its tag.
  188. * The caller may \0-terminate *bv, as next octet is saved in ber->ber_tag.
  189. * Similar to ber_get_stringbv(ber, bv, LBER_BV_NOTERM) except on error.
  190. */
  191. ber_tag_t
  192. ber_skip_element( BerElement *ber, struct berval *bv )
  193. {
  194. ber_tag_t tag = ber_peek_element( ber, bv );
  195. if ( tag != LBER_DEFAULT ) {
  196. ber->ber_ptr = bv->bv_val + bv->bv_len;
  197. ber->ber_tag = *(unsigned char *) ber->ber_ptr;
  198. }
  199. return tag;
  200. }
  201. /* Move past next element, point *bv at the complete element in-place, and
  202. * return its tag. The caller may \0-terminate *bv, as next octet is saved in
  203. * ber->ber_tag. Similar to ber_skip_element(ber, bv) except the tag+length
  204. * header is also included in *bv.
  205. */
  206. ber_tag_t
  207. ber_skip_raw( BerElement *ber, struct berval *bv )
  208. {
  209. char *val = ber->ber_ptr;
  210. ber_tag_t tag = ber_skip_element( ber, bv );
  211. if ( tag != LBER_DEFAULT ) {
  212. bv->bv_len += bv->bv_val - val;
  213. bv->bv_val = val;
  214. }
  215. return tag;
  216. }
  217. ber_tag_t
  218. ber_peek_tag(
  219. BerElement *ber,
  220. ber_len_t *len )
  221. {
  222. struct berval bv;
  223. ber_tag_t tag = ber_peek_element( ber, &bv );
  224. *len = bv.bv_len;
  225. return tag;
  226. }
  227. ber_tag_t
  228. ber_skip_tag( BerElement *ber, ber_len_t *lenp )
  229. {
  230. struct berval bv;
  231. ber_tag_t tag = ber_peek_element( ber, &bv );
  232. ber->ber_ptr = bv.bv_val;
  233. ber->ber_tag = *(unsigned char *) ber->ber_ptr;
  234. *lenp = bv.bv_len;
  235. return tag;
  236. }
  237. ber_tag_t
  238. ber_get_int(
  239. BerElement *ber,
  240. ber_int_t *num )
  241. {
  242. struct berval bv;
  243. ber_tag_t tag = ber_skip_element( ber, &bv );
  244. if ( tag == LBER_DEFAULT ) {
  245. return tag;
  246. }
  247. return ber_decode_int( &bv, num ) ? LBER_DEFAULT : tag;
  248. }
  249. int
  250. ber_decode_int( const struct berval *bv, ber_int_t *num )
  251. {
  252. ber_len_t len = bv->bv_len;
  253. if ( len > sizeof(ber_int_t) )
  254. return -1;
  255. assert( num != NULL );
  256. /* parse two's complement integer */
  257. if( len ) {
  258. unsigned char *buf = (unsigned char *) bv->bv_val;
  259. ber_len_t i;
  260. ber_int_t netnum = buf[0] & 0xff;
  261. /* sign extend */
  262. netnum = (netnum ^ 0x80) - 0x80;
  263. /* shift in the bytes */
  264. for( i = 1; i < len; i++ ) {
  265. netnum = (netnum << 8 ) | buf[i];
  266. }
  267. *num = netnum;
  268. } else {
  269. *num = 0;
  270. }
  271. return 0;
  272. }
  273. ber_tag_t
  274. ber_get_enum(
  275. BerElement *ber,
  276. ber_int_t *num )
  277. {
  278. return ber_get_int( ber, num );
  279. }
  280. ber_tag_t
  281. ber_get_stringb(
  282. BerElement *ber,
  283. char *buf,
  284. ber_len_t *len )
  285. {
  286. struct berval bv;
  287. ber_tag_t tag;
  288. if ( (tag = ber_skip_element( ber, &bv )) == LBER_DEFAULT ) {
  289. return LBER_DEFAULT;
  290. }
  291. /* must fit within allocated space with termination */
  292. if ( bv.bv_len >= *len ) {
  293. return LBER_DEFAULT;
  294. }
  295. memcpy( buf, bv.bv_val, bv.bv_len );
  296. buf[bv.bv_len] = '\0';
  297. *len = bv.bv_len;
  298. return tag;
  299. }
  300. /* Definitions for get_string vector
  301. *
  302. * ChArray, BvArray, and BvVec are self-explanatory.
  303. * BvOff is a struct berval embedded in an array of larger structures
  304. * of siz bytes at off bytes from the beginning of the struct.
  305. */
  306. enum bgbvc { ChArray, BvArray, BvVec, BvOff };
  307. /* Use this single cookie for state, to keep actual
  308. * stack use to the absolute minimum.
  309. */
  310. typedef struct bgbvr {
  311. const enum bgbvc choice;
  312. const int option; /* (ALLOC unless BvOff) | (STRING if ChArray) */
  313. ber_len_t siz; /* input array element size, output count */
  314. ber_len_t off; /* BvOff offset to the struct berval */
  315. void *result;
  316. } bgbvr;
  317. static ber_tag_t
  318. ber_get_stringbvl( BerElement *ber, bgbvr *b )
  319. {
  320. int i = 0, n;
  321. ber_tag_t tag;
  322. ber_len_t tot_size = 0, siz = b->siz;
  323. char *last, *orig;
  324. struct berval bv, *bvp = NULL;
  325. union stringbvl_u {
  326. char **ca; /* ChArray */
  327. BerVarray ba; /* BvArray */
  328. struct berval **bv; /* BvVec */
  329. char *bo; /* BvOff */
  330. } res;
  331. tag = ber_skip_tag( ber, &bv.bv_len );
  332. if ( tag != LBER_DEFAULT ) {
  333. tag = 0;
  334. orig = ber->ber_ptr;
  335. last = orig + bv.bv_len;
  336. for ( ; ber->ber_ptr < last; i++, tot_size += siz ) {
  337. if ( ber_skip_element( ber, &bv ) == LBER_DEFAULT )
  338. break;
  339. }
  340. if ( ber->ber_ptr != last ) {
  341. i = 0;
  342. tag = LBER_DEFAULT;
  343. }
  344. ber->ber_ptr = orig;
  345. ber->ber_tag = *(unsigned char *) orig;
  346. }
  347. b->siz = i;
  348. if ( i == 0 ) {
  349. return tag;
  350. }
  351. /* Allocate and NULL-terminate the result vector */
  352. b->result = ber_memalloc_x( tot_size + siz, ber->ber_memctx );
  353. if ( b->result == NULL ) {
  354. return LBER_DEFAULT;
  355. }
  356. switch (b->choice) {
  357. case ChArray:
  358. res.ca = b->result;
  359. res.ca[i] = NULL;
  360. break;
  361. case BvArray:
  362. res.ba = b->result;
  363. res.ba[i].bv_val = NULL;
  364. break;
  365. case BvVec:
  366. res.bv = b->result;
  367. res.bv[i] = NULL;
  368. break;
  369. case BvOff:
  370. res.bo = (char *) b->result + b->off;
  371. ((struct berval *) (res.bo + tot_size))->bv_val = NULL;
  372. tot_size = 0;
  373. break;
  374. }
  375. n = 0;
  376. do {
  377. tag = ber_get_stringbv( ber, &bv, b->option );
  378. if ( tag == LBER_DEFAULT ) {
  379. goto failed;
  380. }
  381. /* store my result */
  382. switch (b->choice) {
  383. case ChArray:
  384. res.ca[n] = bv.bv_val;
  385. break;
  386. case BvArray:
  387. res.ba[n] = bv;
  388. break;
  389. case BvVec:
  390. bvp = ber_memalloc_x( sizeof( struct berval ),
  391. ber->ber_memctx );
  392. if ( !bvp ) {
  393. ber_memfree_x( bv.bv_val, ber->ber_memctx );
  394. goto failed;
  395. }
  396. res.bv[n] = bvp;
  397. *bvp = bv;
  398. break;
  399. case BvOff:
  400. *(struct berval *)(res.bo + tot_size) = bv;
  401. tot_size += siz;
  402. break;
  403. }
  404. } while (++n < i);
  405. return tag;
  406. failed:
  407. if (b->choice != BvOff) { /* BvOff does not have LBER_BV_ALLOC set */
  408. while (--n >= 0) {
  409. switch(b->choice) {
  410. case ChArray:
  411. ber_memfree_x(res.ca[n], ber->ber_memctx);
  412. break;
  413. case BvArray:
  414. ber_memfree_x(res.ba[n].bv_val, ber->ber_memctx);
  415. break;
  416. case BvVec:
  417. ber_memfree_x(res.bv[n]->bv_val, ber->ber_memctx);
  418. ber_memfree_x(res.bv[n], ber->ber_memctx);
  419. break;
  420. default:
  421. break;
  422. }
  423. }
  424. }
  425. ber_memfree_x(b->result, ber->ber_memctx);
  426. b->result = NULL;
  427. return LBER_DEFAULT;
  428. }
  429. ber_tag_t
  430. ber_get_stringbv( BerElement *ber, struct berval *bv, int option )
  431. {
  432. ber_tag_t tag;
  433. char *data;
  434. tag = ber_skip_element( ber, bv );
  435. if ( tag == LBER_DEFAULT ||
  436. (( option & LBER_BV_STRING ) &&
  437. bv->bv_len && memchr( bv->bv_val, 0, bv->bv_len - 1 )))
  438. {
  439. bv->bv_val = NULL;
  440. return LBER_DEFAULT;
  441. }
  442. data = bv->bv_val;
  443. if ( option & LBER_BV_ALLOC ) {
  444. bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1,
  445. ber->ber_memctx );
  446. if ( bv->bv_val == NULL ) {
  447. return LBER_DEFAULT;
  448. }
  449. if ( bv->bv_len != 0 ) {
  450. memcpy( bv->bv_val, data, bv->bv_len );
  451. }
  452. data = bv->bv_val;
  453. }
  454. if ( !( option & LBER_BV_NOTERM ))
  455. data[bv->bv_len] = '\0';
  456. return tag;
  457. }
  458. ber_tag_t
  459. ber_get_stringbv_null( BerElement *ber, struct berval *bv, int option )
  460. {
  461. ber_tag_t tag;
  462. char *data;
  463. tag = ber_skip_element( ber, bv );
  464. if ( tag == LBER_DEFAULT || bv->bv_len == 0 ) {
  465. bv->bv_val = NULL;
  466. return tag;
  467. }
  468. if (( option & LBER_BV_STRING ) &&
  469. memchr( bv->bv_val, 0, bv->bv_len - 1 ))
  470. {
  471. bv->bv_val = NULL;
  472. return LBER_DEFAULT;
  473. }
  474. data = bv->bv_val;
  475. if ( option & LBER_BV_ALLOC ) {
  476. bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1,
  477. ber->ber_memctx );
  478. if ( bv->bv_val == NULL ) {
  479. return LBER_DEFAULT;
  480. }
  481. memcpy( bv->bv_val, data, bv->bv_len );
  482. data = bv->bv_val;
  483. }
  484. if ( !( option & LBER_BV_NOTERM ))
  485. data[bv->bv_len] = '\0';
  486. return tag;
  487. }
  488. ber_tag_t
  489. ber_get_stringa( BerElement *ber, char **buf )
  490. {
  491. BerValue bv;
  492. ber_tag_t tag;
  493. assert( buf != NULL );
  494. tag = ber_get_stringbv( ber, &bv, LBER_BV_ALLOC | LBER_BV_STRING );
  495. *buf = bv.bv_val;
  496. return tag;
  497. }
  498. ber_tag_t
  499. ber_get_stringa_null( BerElement *ber, char **buf )
  500. {
  501. BerValue bv;
  502. ber_tag_t tag;
  503. assert( buf != NULL );
  504. tag = ber_get_stringbv_null( ber, &bv, LBER_BV_ALLOC | LBER_BV_STRING );
  505. *buf = bv.bv_val;
  506. return tag;
  507. }
  508. ber_tag_t
  509. ber_get_stringal( BerElement *ber, struct berval **bv )
  510. {
  511. ber_tag_t tag;
  512. assert( ber != NULL );
  513. assert( bv != NULL );
  514. *bv = (struct berval *) ber_memalloc_x( sizeof(struct berval),
  515. ber->ber_memctx );
  516. if ( *bv == NULL ) {
  517. return LBER_DEFAULT;
  518. }
  519. tag = ber_get_stringbv( ber, *bv, LBER_BV_ALLOC );
  520. if ( tag == LBER_DEFAULT ) {
  521. ber_memfree_x( *bv, ber->ber_memctx );
  522. *bv = NULL;
  523. }
  524. return tag;
  525. }
  526. ber_tag_t
  527. ber_get_bitstringa(
  528. BerElement *ber,
  529. char **buf,
  530. ber_len_t *blen )
  531. {
  532. ber_tag_t tag;
  533. struct berval data;
  534. unsigned char unusedbits;
  535. assert( buf != NULL );
  536. assert( blen != NULL );
  537. if ( (tag = ber_skip_element( ber, &data )) == LBER_DEFAULT ) {
  538. goto fail;
  539. }
  540. if ( --data.bv_len > (ber_len_t)-1 / 8 ) {
  541. goto fail;
  542. }
  543. unusedbits = *(unsigned char *) data.bv_val++;
  544. if ( unusedbits > 7 ) {
  545. goto fail;
  546. }
  547. if ( memchr( data.bv_val, 0, data.bv_len )) {
  548. goto fail;
  549. }
  550. *buf = (char *) ber_memalloc_x( data.bv_len, ber->ber_memctx );
  551. if ( *buf == NULL ) {
  552. return LBER_DEFAULT;
  553. }
  554. memcpy( *buf, data.bv_val, data.bv_len );
  555. *blen = data.bv_len * 8 - unusedbits;
  556. return tag;
  557. fail:
  558. *buf = NULL;
  559. return LBER_DEFAULT;
  560. }
  561. ber_tag_t
  562. ber_get_null( BerElement *ber )
  563. {
  564. ber_len_t len;
  565. ber_tag_t tag = ber_skip_tag( ber, &len );
  566. return( len == 0 ? tag : LBER_DEFAULT );
  567. }
  568. ber_tag_t
  569. ber_get_boolean(
  570. BerElement *ber,
  571. ber_int_t *boolval )
  572. {
  573. return ber_get_int( ber, boolval );
  574. }
  575. ber_tag_t
  576. ber_first_element(
  577. BerElement *ber,
  578. ber_len_t *len,
  579. char **last )
  580. {
  581. assert( last != NULL );
  582. /* skip the sequence header, use the len to mark where to stop */
  583. if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
  584. *last = NULL;
  585. return LBER_DEFAULT;
  586. }
  587. *last = ber->ber_ptr + *len;
  588. if ( *len == 0 ) {
  589. return LBER_DEFAULT;
  590. }
  591. return ber_peek_tag( ber, len );
  592. }
  593. ber_tag_t
  594. ber_next_element(
  595. BerElement *ber,
  596. ber_len_t *len,
  597. LDAP_CONST char *last )
  598. {
  599. assert( ber != NULL );
  600. assert( last != NULL );
  601. assert( LBER_VALID( ber ) );
  602. if ( ber->ber_ptr >= last ) {
  603. return LBER_DEFAULT;
  604. }
  605. return ber_peek_tag( ber, len );
  606. }
  607. /* VARARGS */
  608. ber_tag_t
  609. ber_scanf ( BerElement *ber,
  610. LDAP_CONST char *fmt,
  611. ... )
  612. {
  613. va_list ap;
  614. LDAP_CONST char *fmt_reset;
  615. char *s, **ss, ***sss;
  616. struct berval data, *bval, **bvp, ***bvpp;
  617. ber_int_t *i;
  618. ber_len_t *l;
  619. ber_tag_t *t;
  620. ber_tag_t rc;
  621. ber_len_t len;
  622. va_start( ap, fmt );
  623. assert( ber != NULL );
  624. assert( fmt != NULL );
  625. assert( LBER_VALID( ber ) );
  626. fmt_reset = fmt;
  627. if ( ber->ber_debug & (LDAP_DEBUG_TRACE|LDAP_DEBUG_BER)) {
  628. ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
  629. "ber_scanf fmt (%s) ber:\n", fmt );
  630. ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
  631. }
  632. for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
  633. /* When this is modified, remember to update
  634. * the error-cleanup code below accordingly. */
  635. switch ( *fmt ) {
  636. case '!': { /* Hook */
  637. BERDecodeCallback *f;
  638. void *p;
  639. f = va_arg( ap, BERDecodeCallback * );
  640. p = va_arg( ap, void * );
  641. rc = (*f)( ber, p, 0 );
  642. } break;
  643. case 'a': /* octet string - allocate storage as needed */
  644. ss = va_arg( ap, char ** );
  645. rc = ber_get_stringa( ber, ss );
  646. break;
  647. case 'A': /* octet string - allocate storage as needed,
  648. * but return NULL if len == 0 */
  649. ss = va_arg( ap, char ** );
  650. rc = ber_get_stringa_null( ber, ss );
  651. break;
  652. case 'b': /* boolean */
  653. i = va_arg( ap, ber_int_t * );
  654. rc = ber_get_boolean( ber, i );
  655. break;
  656. case 'B': /* bit string - allocate storage as needed */
  657. ss = va_arg( ap, char ** );
  658. l = va_arg( ap, ber_len_t * ); /* for length, in bits */
  659. rc = ber_get_bitstringa( ber, ss, l );
  660. break;
  661. case 'e': /* enumerated */
  662. case 'i': /* integer */
  663. i = va_arg( ap, ber_int_t * );
  664. rc = ber_get_int( ber, i );
  665. break;
  666. case 'l': /* length of next item */
  667. l = va_arg( ap, ber_len_t * );
  668. rc = ber_peek_tag( ber, l );
  669. break;
  670. case 'm': /* octet string in berval, in-place */
  671. bval = va_arg( ap, struct berval * );
  672. rc = ber_get_stringbv( ber, bval, 0 );
  673. break;
  674. case 'M': /* bvoffarray - must include address of
  675. * a record len, and record offset.
  676. * number of records will be returned thru
  677. * len ptr on finish. parsed in-place.
  678. */
  679. {
  680. bgbvr cookie = { BvOff, 0 };
  681. bvp = va_arg( ap, struct berval ** );
  682. l = va_arg( ap, ber_len_t * );
  683. cookie.siz = *l;
  684. cookie.off = va_arg( ap, ber_len_t );
  685. rc = ber_get_stringbvl( ber, &cookie );
  686. *bvp = cookie.result;
  687. *l = cookie.siz;
  688. break;
  689. }
  690. case 'n': /* null */
  691. rc = ber_get_null( ber );
  692. break;
  693. case 'o': /* octet string in a supplied berval */
  694. bval = va_arg( ap, struct berval * );
  695. rc = ber_get_stringbv( ber, bval, LBER_BV_ALLOC );
  696. break;
  697. case 'O': /* octet string - allocate & include length */
  698. bvp = va_arg( ap, struct berval ** );
  699. rc = ber_get_stringal( ber, bvp );
  700. break;
  701. case 's': /* octet string - in a buffer */
  702. s = va_arg( ap, char * );
  703. l = va_arg( ap, ber_len_t * );
  704. rc = ber_get_stringb( ber, s, l );
  705. break;
  706. case 't': /* tag of next item */
  707. t = va_arg( ap, ber_tag_t * );
  708. *t = rc = ber_peek_tag( ber, &len );
  709. break;
  710. case 'T': /* skip tag of next item */
  711. t = va_arg( ap, ber_tag_t * );
  712. *t = rc = ber_skip_tag( ber, &len );
  713. break;
  714. case 'v': /* sequence of strings */
  715. {
  716. bgbvr cookie = {
  717. ChArray, LBER_BV_ALLOC | LBER_BV_STRING, sizeof( char * )
  718. };
  719. rc = ber_get_stringbvl( ber, &cookie );
  720. *(va_arg( ap, char *** )) = cookie.result;
  721. break;
  722. }
  723. case 'V': /* sequence of strings + lengths */
  724. {
  725. bgbvr cookie = {
  726. BvVec, LBER_BV_ALLOC, sizeof( struct berval * )
  727. };
  728. rc = ber_get_stringbvl( ber, &cookie );
  729. *(va_arg( ap, struct berval *** )) = cookie.result;
  730. break;
  731. }
  732. case 'W': /* bvarray */
  733. {
  734. bgbvr cookie = {
  735. BvArray, LBER_BV_ALLOC, sizeof( struct berval )
  736. };
  737. rc = ber_get_stringbvl( ber, &cookie );
  738. *(va_arg( ap, struct berval ** )) = cookie.result;
  739. break;
  740. }
  741. case 'x': /* skip the next element - whatever it is */
  742. rc = ber_skip_element( ber, &data );
  743. break;
  744. case '{': /* begin sequence */
  745. case '[': /* begin set */
  746. switch ( fmt[1] ) {
  747. case 'v': case 'V': case 'W': case 'M':
  748. break;
  749. default:
  750. rc = ber_skip_tag( ber, &len );
  751. break;
  752. }
  753. break;
  754. case '}': /* end sequence */
  755. case ']': /* end set */
  756. break;
  757. default:
  758. if( ber->ber_debug ) {
  759. ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
  760. "ber_scanf: unknown fmt %c\n", *fmt );
  761. }
  762. rc = LBER_DEFAULT;
  763. break;
  764. }
  765. }
  766. va_end( ap );
  767. if ( rc == LBER_DEFAULT ) {
  768. /*
  769. * Error. Reclaim malloced memory that was given to the caller.
  770. * Set allocated pointers to NULL, "data length" outvalues to 0.
  771. */
  772. va_start( ap, fmt );
  773. for ( ; fmt_reset < fmt; fmt_reset++ ) {
  774. switch ( *fmt_reset ) {
  775. case '!': { /* Hook */
  776. BERDecodeCallback *f;
  777. void *p;
  778. f = va_arg( ap, BERDecodeCallback * );
  779. p = va_arg( ap, void * );
  780. (void) (*f)( ber, p, 1 );
  781. } break;
  782. case 'a': /* octet string - allocate storage as needed */
  783. case 'A':
  784. ss = va_arg( ap, char ** );
  785. ber_memfree_x( *ss, ber->ber_memctx );
  786. *ss = NULL;
  787. break;
  788. case 'b': /* boolean */
  789. case 'e': /* enumerated */
  790. case 'i': /* integer */
  791. (void) va_arg( ap, ber_int_t * );
  792. break;
  793. case 'l': /* length of next item */
  794. *(va_arg( ap, ber_len_t * )) = 0;
  795. break;
  796. case 'm': /* berval in-place */
  797. bval = va_arg( ap, struct berval * );
  798. BER_BVZERO( bval );
  799. break;
  800. case 'M': /* BVoff array in-place */
  801. bvp = va_arg( ap, struct berval ** );
  802. ber_memfree_x( *bvp, ber->ber_memctx );
  803. *bvp = NULL;
  804. *(va_arg( ap, ber_len_t * )) = 0;
  805. (void) va_arg( ap, ber_len_t );
  806. break;
  807. case 'o': /* octet string in a supplied berval */
  808. bval = va_arg( ap, struct berval * );
  809. ber_memfree_x( bval->bv_val, ber->ber_memctx );
  810. BER_BVZERO( bval );
  811. break;
  812. case 'O': /* octet string - allocate & include length */
  813. bvp = va_arg( ap, struct berval ** );
  814. ber_bvfree_x( *bvp, ber->ber_memctx );
  815. *bvp = NULL;
  816. break;
  817. case 's': /* octet string - in a buffer */
  818. (void) va_arg( ap, char * );
  819. *(va_arg( ap, ber_len_t * )) = 0;
  820. break;
  821. case 't': /* tag of next item */
  822. case 'T': /* skip tag of next item */
  823. (void) va_arg( ap, ber_tag_t * );
  824. break;
  825. case 'B': /* bit string - allocate storage as needed */
  826. ss = va_arg( ap, char ** );
  827. ber_memfree_x( *ss, ber->ber_memctx );
  828. *ss = NULL;
  829. *(va_arg( ap, ber_len_t * )) = 0; /* for length, in bits */
  830. break;
  831. case 'v': /* sequence of strings */
  832. sss = va_arg( ap, char *** );
  833. ber_memvfree_x( (void **) *sss, ber->ber_memctx );
  834. *sss = NULL;
  835. break;
  836. case 'V': /* sequence of strings + lengths */
  837. bvpp = va_arg( ap, struct berval *** );
  838. ber_bvecfree_x( *bvpp, ber->ber_memctx );
  839. *bvpp = NULL;
  840. break;
  841. case 'W': /* BerVarray */
  842. bvp = va_arg( ap, struct berval ** );
  843. ber_bvarray_free_x( *bvp, ber->ber_memctx );
  844. *bvp = NULL;
  845. break;
  846. case 'n': /* null */
  847. case 'x': /* skip the next element - whatever it is */
  848. case '{': /* begin sequence */
  849. case '[': /* begin set */
  850. case '}': /* end sequence */
  851. case ']': /* end set */
  852. break;
  853. default:
  854. /* format should be good */
  855. assert( 0 );
  856. }
  857. }
  858. va_end( ap );
  859. }
  860. return rc;
  861. }