nssb64d.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * Base64 decoding (ascii to binary).
  6. */
  7. #include "nssb64.h"
  8. #include "nspr.h"
  9. #include "secitem.h"
  10. #include "secerr.h"
  11. /*
  12. * XXX We want this basic support to go into NSPR (the PL part).
  13. * Until that can happen, the PL interface is going to be kept entirely
  14. * internal here -- all static functions and opaque data structures.
  15. * When someone can get it moved over into NSPR, that should be done:
  16. * - giving everything names that are accepted by the NSPR module owners
  17. * (though I tried to choose ones that would work without modification)
  18. * - exporting the functions (remove static declarations and add
  19. * to nssutil.def as necessary)
  20. * - put prototypes into appropriate header file (probably replacing
  21. * the entire current lib/libc/include/plbase64.h in NSPR)
  22. * along with a typedef for the context structure (which should be
  23. * kept opaque -- definition in the source file only, but typedef
  24. * ala "typedef struct PLBase64FooStr PLBase64Foo;" in header file)
  25. * - modify anything else as necessary to conform to NSPR required style
  26. * (I looked but found no formatting guide to follow)
  27. *
  28. * You will want to move over everything from here down to the comment
  29. * which says "XXX End of base64 decoding code to be moved into NSPR",
  30. * into a new file in NSPR.
  31. */
  32. /*
  33. **************************************************************
  34. * XXX Beginning of base64 decoding code to be moved into NSPR.
  35. */
  36. /*
  37. * This typedef would belong in the NSPR header file (i.e. plbase64.h).
  38. */
  39. typedef struct PLBase64DecoderStr PLBase64Decoder;
  40. /*
  41. * The following implementation of base64 decoding was based on code
  42. * found in libmime (specifically, in mimeenc.c). It has been adapted to
  43. * use PR types and naming as well as to provide other necessary semantics
  44. * (like buffer-in/buffer-out in addition to "streaming" without undue
  45. * performance hit of extra copying if you made the buffer versions
  46. * use the output_fn). It also incorporates some aspects of the current
  47. * NSPR base64 decoding code. As such, you may find similarities to
  48. * both of those implementations. I tried to use names that reflected
  49. * the original code when possible. For this reason you may find some
  50. * inconsistencies -- libmime used lots of "in" and "out" whereas the
  51. * NSPR version uses "src" and "dest"; sometimes I changed one to the other
  52. * and sometimes I left them when I thought the subroutines were at least
  53. * self-consistent.
  54. */
  55. PR_BEGIN_EXTERN_C
  56. /*
  57. * Opaque object used by the decoder to store state.
  58. */
  59. struct PLBase64DecoderStr {
  60. /* Current token (or portion, if token_size < 4) being decoded. */
  61. unsigned char token[4];
  62. int token_size;
  63. /*
  64. * Where to write the decoded data (used when streaming, not when
  65. * doing all in-memory (buffer) operations).
  66. *
  67. * Note that this definition is chosen to be compatible with PR_Write.
  68. */
  69. PRInt32 (*output_fn)(void *output_arg, const unsigned char *buf,
  70. PRInt32 size);
  71. void *output_arg;
  72. /*
  73. * Where the decoded output goes -- either temporarily (in the streaming
  74. * case, staged here before it goes to the output function) or what will
  75. * be the entire buffered result for users of the buffer version.
  76. */
  77. unsigned char *output_buffer;
  78. PRUint32 output_buflen; /* the total length of allocated buffer */
  79. PRUint32 output_length; /* the length that is currently populated */
  80. };
  81. PR_END_EXTERN_C
  82. /*
  83. * Table to convert an ascii "code" to its corresponding binary value.
  84. * For ease of use, the binary values in the table are the actual values
  85. * PLUS ONE. This is so that the special value of zero can denote an
  86. * invalid mapping; that was much easier than trying to fill in the other
  87. * values with some value other than zero, and to check for it.
  88. * Just remember to SUBTRACT ONE when using the value retrieved.
  89. */
  90. static unsigned char base64_codetovaluep1[256] = {
  91. /* 0: */ 0, 0, 0, 0, 0, 0, 0, 0,
  92. /* 8: */ 0, 0, 0, 0, 0, 0, 0, 0,
  93. /* 16: */ 0, 0, 0, 0, 0, 0, 0, 0,
  94. /* 24: */ 0, 0, 0, 0, 0, 0, 0, 0,
  95. /* 32: */ 0, 0, 0, 0, 0, 0, 0, 0,
  96. /* 40: */ 0, 0, 0, 63, 0, 0, 0, 64,
  97. /* 48: */ 53, 54, 55, 56, 57, 58, 59, 60,
  98. /* 56: */ 61, 62, 0, 0, 0, 0, 0, 0,
  99. /* 64: */ 0, 1, 2, 3, 4, 5, 6, 7,
  100. /* 72: */ 8, 9, 10, 11, 12, 13, 14, 15,
  101. /* 80: */ 16, 17, 18, 19, 20, 21, 22, 23,
  102. /* 88: */ 24, 25, 26, 0, 0, 0, 0, 0,
  103. /* 96: */ 0, 27, 28, 29, 30, 31, 32, 33,
  104. /* 104: */ 34, 35, 36, 37, 38, 39, 40, 41,
  105. /* 112: */ 42, 43, 44, 45, 46, 47, 48, 49,
  106. /* 120: */ 50, 51, 52, 0, 0, 0, 0, 0,
  107. /* 128: */ 0, 0, 0, 0, 0, 0, 0, 0
  108. /* and rest are all zero as well */
  109. };
  110. #define B64_PAD '='
  111. /*
  112. * Reads 4; writes 3 (known, or expected, to have no trailing padding).
  113. * Returns bytes written; -1 on error (unexpected character).
  114. */
  115. static int
  116. pl_base64_decode_4to3(const unsigned char *in, unsigned char *out)
  117. {
  118. int j;
  119. PRUint32 num = 0;
  120. unsigned char bits;
  121. for (j = 0; j < 4; j++) {
  122. bits = base64_codetovaluep1[in[j]];
  123. if (bits == 0)
  124. return -1;
  125. num = (num << 6) | (bits - 1);
  126. }
  127. out[0] = (unsigned char)(num >> 16);
  128. out[1] = (unsigned char)((num >> 8) & 0xFF);
  129. out[2] = (unsigned char)(num & 0xFF);
  130. return 3;
  131. }
  132. /*
  133. * Reads 3; writes 2 (caller already confirmed EOF or trailing padding).
  134. * Returns bytes written; -1 on error (unexpected character).
  135. */
  136. static int
  137. pl_base64_decode_3to2(const unsigned char *in, unsigned char *out)
  138. {
  139. PRUint32 num = 0;
  140. unsigned char bits1, bits2, bits3;
  141. bits1 = base64_codetovaluep1[in[0]];
  142. bits2 = base64_codetovaluep1[in[1]];
  143. bits3 = base64_codetovaluep1[in[2]];
  144. if ((bits1 == 0) || (bits2 == 0) || (bits3 == 0))
  145. return -1;
  146. num = ((PRUint32)(bits1 - 1)) << 10;
  147. num |= ((PRUint32)(bits2 - 1)) << 4;
  148. num |= ((PRUint32)(bits3 - 1)) >> 2;
  149. out[0] = (unsigned char)(num >> 8);
  150. out[1] = (unsigned char)(num & 0xFF);
  151. return 2;
  152. }
  153. /*
  154. * Reads 2; writes 1 (caller already confirmed EOF or trailing padding).
  155. * Returns bytes written; -1 on error (unexpected character).
  156. */
  157. static int
  158. pl_base64_decode_2to1(const unsigned char *in, unsigned char *out)
  159. {
  160. PRUint32 num = 0;
  161. unsigned char bits1, bits2;
  162. bits1 = base64_codetovaluep1[in[0]];
  163. bits2 = base64_codetovaluep1[in[1]];
  164. if ((bits1 == 0) || (bits2 == 0))
  165. return -1;
  166. num = ((PRUint32)(bits1 - 1)) << 2;
  167. num |= ((PRUint32)(bits2 - 1)) >> 4;
  168. out[0] = (unsigned char)num;
  169. return 1;
  170. }
  171. /*
  172. * Reads 4; writes 0-3. Returns bytes written or -1 on error.
  173. * (Writes less than 3 only at (presumed) EOF.)
  174. */
  175. static int
  176. pl_base64_decode_token(const unsigned char *in, unsigned char *out)
  177. {
  178. if (in[3] != B64_PAD)
  179. return pl_base64_decode_4to3(in, out);
  180. if (in[2] == B64_PAD)
  181. return pl_base64_decode_2to1(in, out);
  182. return pl_base64_decode_3to2(in, out);
  183. }
  184. static PRStatus
  185. pl_base64_decode_buffer(PLBase64Decoder *data, const unsigned char *in,
  186. PRUint32 length)
  187. {
  188. unsigned char *out = data->output_buffer;
  189. unsigned char *token = data->token;
  190. int i, n = 0;
  191. i = data->token_size;
  192. data->token_size = 0;
  193. while (length > 0) {
  194. while (i < 4 && length > 0) {
  195. /*
  196. * XXX Note that the following simply ignores any unexpected
  197. * characters. This is exactly what the original code in
  198. * libmime did, and I am leaving it. We certainly want to skip
  199. * over whitespace (we must); this does much more than that.
  200. * I am not confident changing it, and I don't want to slow
  201. * the processing down doing more complicated checking, but
  202. * someone else might have different ideas in the future.
  203. */
  204. if (base64_codetovaluep1[*in] > 0 || *in == B64_PAD)
  205. token[i++] = *in;
  206. in++;
  207. length--;
  208. }
  209. if (i < 4) {
  210. /* Didn't get enough for a complete token. */
  211. data->token_size = i;
  212. break;
  213. }
  214. i = 0;
  215. PR_ASSERT((PRUint32)(out - data->output_buffer + 3) <= data->output_buflen);
  216. /*
  217. * Assume we are not at the end; the following function only works
  218. * for an internal token (no trailing padding characters) but is
  219. * faster that way. If it hits an invalid character (padding) it
  220. * will return an error; we break out of the loop and try again
  221. * calling the routine that will handle a final token.
  222. * Note that we intentionally do it this way rather than explicitly
  223. * add a check for padding here (because that would just slow down
  224. * the normal case) nor do we rely on checking whether we have more
  225. * input to process (because that would also slow it down but also
  226. * because we want to allow trailing garbage, especially white space
  227. * and cannot tell that without read-ahead, also a slow proposition).
  228. * Whew. Understand?
  229. */
  230. n = pl_base64_decode_4to3(token, out);
  231. if (n < 0)
  232. break;
  233. /* Advance "out" by the number of bytes just written to it. */
  234. out += n;
  235. n = 0;
  236. }
  237. /*
  238. * See big comment above, before call to pl_base64_decode_4to3.
  239. * Here we check if we error'd out of loop, and allow for the case
  240. * that we are processing the last interesting token. If the routine
  241. * which should handle padding characters also fails, then we just
  242. * have bad input and give up.
  243. */
  244. if (n < 0) {
  245. n = pl_base64_decode_token(token, out);
  246. if (n < 0)
  247. return PR_FAILURE;
  248. out += n;
  249. }
  250. /*
  251. * As explained above, we can get here with more input remaining, but
  252. * it should be all characters we do not care about (i.e. would be
  253. * ignored when transferring from "in" to "token" in loop above,
  254. * except here we choose to ignore extraneous pad characters, too).
  255. * Swallow it, performing that check. If we find more characters that
  256. * we would expect to decode, something is wrong.
  257. */
  258. while (length > 0) {
  259. if (base64_codetovaluep1[*in] > 0)
  260. return PR_FAILURE;
  261. in++;
  262. length--;
  263. }
  264. /* Record the length of decoded data we have left in output_buffer. */
  265. data->output_length = (PRUint32)(out - data->output_buffer);
  266. return PR_SUCCESS;
  267. }
  268. /*
  269. * Flush any remaining buffered characters. Given well-formed input,
  270. * this will have nothing to do. If the input was missing the padding
  271. * characters at the end, though, there could be 1-3 characters left
  272. * behind -- we will tolerate that by adding the padding for them.
  273. */
  274. static PRStatus
  275. pl_base64_decode_flush(PLBase64Decoder *data)
  276. {
  277. int count;
  278. /*
  279. * If no remaining characters, or all are padding (also not well-formed
  280. * input, but again, be tolerant), then nothing more to do. (And, that
  281. * is considered successful.)
  282. */
  283. if (data->token_size == 0 || data->token[0] == B64_PAD)
  284. return PR_SUCCESS;
  285. /*
  286. * Assume we have all the interesting input except for some expected
  287. * padding characters. Add them and decode the resulting token.
  288. */
  289. while (data->token_size < 4)
  290. data->token[data->token_size++] = B64_PAD;
  291. data->token_size = 0; /* so a subsequent flush call is a no-op */
  292. count = pl_base64_decode_token(data->token,
  293. data->output_buffer + data->output_length);
  294. if (count < 0)
  295. return PR_FAILURE;
  296. /*
  297. * If there is an output function, call it with this last bit of data.
  298. * Otherwise we are doing all buffered output, and the decoded bytes
  299. * are now there, we just need to reflect that in the length.
  300. */
  301. if (data->output_fn != NULL) {
  302. PRInt32 output_result;
  303. PR_ASSERT(data->output_length == 0);
  304. output_result = data->output_fn(data->output_arg,
  305. data->output_buffer,
  306. (PRInt32)count);
  307. if (output_result < 0)
  308. return PR_FAILURE;
  309. } else {
  310. data->output_length += count;
  311. }
  312. return PR_SUCCESS;
  313. }
  314. /*
  315. * The maximum space needed to hold the output of the decoder given
  316. * input data of length "size".
  317. */
  318. static PRUint32
  319. PL_Base64MaxDecodedLength(PRUint32 size)
  320. {
  321. return size * 0.75;
  322. }
  323. /*
  324. * A distinct internal creation function for the buffer version to use.
  325. * (It does not want to specify an output_fn, and we want the normal
  326. * Create function to require that.) If more common initialization
  327. * of the decoding context needs to be done, it should be done *here*.
  328. */
  329. static PLBase64Decoder *
  330. pl_base64_create_decoder(void)
  331. {
  332. return PR_NEWZAP(PLBase64Decoder);
  333. }
  334. /*
  335. * Function to start a base64 decoding context.
  336. * An "output_fn" is required; the "output_arg" parameter to that is optional.
  337. */
  338. static PLBase64Decoder *
  339. PL_CreateBase64Decoder(PRInt32 (*output_fn)(void *, const unsigned char *,
  340. PRInt32),
  341. void *output_arg)
  342. {
  343. PLBase64Decoder *data;
  344. if (output_fn == NULL) {
  345. PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
  346. return NULL;
  347. }
  348. data = pl_base64_create_decoder();
  349. if (data != NULL) {
  350. data->output_fn = output_fn;
  351. data->output_arg = output_arg;
  352. }
  353. return data;
  354. }
  355. /*
  356. * Push data through the decoder, causing the output_fn (provided to Create)
  357. * to be called with the decoded data.
  358. */
  359. static PRStatus
  360. PL_UpdateBase64Decoder(PLBase64Decoder *data, const char *buffer,
  361. PRUint32 size)
  362. {
  363. PRUint32 need_length;
  364. PRStatus status;
  365. /* XXX Should we do argument checking only in debug build? */
  366. if (data == NULL || buffer == NULL || size == 0) {
  367. PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
  368. return PR_FAILURE;
  369. }
  370. /*
  371. * How much space could this update need for decoding?
  372. */
  373. need_length = PL_Base64MaxDecodedLength(size + data->token_size);
  374. /*
  375. * Make sure we have at least that much. If not, (re-)allocate.
  376. */
  377. if (need_length > data->output_buflen) {
  378. unsigned char *output_buffer = data->output_buffer;
  379. if (output_buffer != NULL)
  380. output_buffer = (unsigned char *)PR_Realloc(output_buffer,
  381. need_length);
  382. else
  383. output_buffer = (unsigned char *)PR_Malloc(need_length);
  384. if (output_buffer == NULL)
  385. return PR_FAILURE;
  386. data->output_buffer = output_buffer;
  387. data->output_buflen = need_length;
  388. }
  389. /* There should not have been any leftover output data in the buffer. */
  390. PR_ASSERT(data->output_length == 0);
  391. data->output_length = 0;
  392. status = pl_base64_decode_buffer(data, (const unsigned char *)buffer,
  393. size);
  394. /* Now that we have some decoded data, write it. */
  395. if (status == PR_SUCCESS && data->output_length > 0) {
  396. PRInt32 output_result;
  397. PR_ASSERT(data->output_fn != NULL);
  398. output_result = data->output_fn(data->output_arg,
  399. data->output_buffer,
  400. (PRInt32)data->output_length);
  401. if (output_result < 0)
  402. status = PR_FAILURE;
  403. }
  404. data->output_length = 0;
  405. return status;
  406. }
  407. /*
  408. * When you're done decoding, call this to free the data. If "abort_p"
  409. * is false, then calling this may cause the output_fn to be called
  410. * one last time (as the last buffered data is flushed out).
  411. */
  412. static PRStatus
  413. PL_DestroyBase64Decoder(PLBase64Decoder *data, PRBool abort_p)
  414. {
  415. PRStatus status = PR_SUCCESS;
  416. /* XXX Should we do argument checking only in debug build? */
  417. if (data == NULL) {
  418. PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
  419. return PR_FAILURE;
  420. }
  421. /* Flush out the last few buffered characters. */
  422. if (!abort_p)
  423. status = pl_base64_decode_flush(data);
  424. if (data->output_buffer != NULL)
  425. PR_Free(data->output_buffer);
  426. PR_Free(data);
  427. return status;
  428. }
  429. /*
  430. * Perform base64 decoding from an input buffer to an output buffer.
  431. * The output buffer can be provided (as "dest"); you can also pass in
  432. * a NULL and this function will allocate a buffer large enough for you,
  433. * and return it. If you do provide the output buffer, you must also
  434. * provide the maximum length of that buffer (as "maxdestlen").
  435. * The actual decoded length of output will be returned to you in
  436. * "output_destlen".
  437. *
  438. * Return value is NULL on error, the output buffer (allocated or provided)
  439. * otherwise.
  440. */
  441. static unsigned char *
  442. PL_Base64DecodeBuffer(const char *src, PRUint32 srclen, unsigned char *dest,
  443. PRUint32 maxdestlen, PRUint32 *output_destlen)
  444. {
  445. PRUint32 need_length;
  446. unsigned char *output_buffer = NULL;
  447. PLBase64Decoder *data = NULL;
  448. PRStatus status;
  449. PR_ASSERT(srclen > 0);
  450. if (srclen == 0) {
  451. PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
  452. return NULL;
  453. }
  454. /*
  455. * How much space could we possibly need for decoding this input?
  456. */
  457. need_length = PL_Base64MaxDecodedLength(srclen);
  458. /*
  459. * Make sure we have at least that much, if output buffer provided.
  460. * If no output buffer provided, then we allocate that much.
  461. */
  462. if (dest != NULL) {
  463. PR_ASSERT(maxdestlen >= need_length);
  464. if (maxdestlen < need_length) {
  465. PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
  466. goto loser;
  467. }
  468. output_buffer = dest;
  469. } else {
  470. output_buffer = (unsigned char *)PR_Malloc(need_length);
  471. if (output_buffer == NULL)
  472. goto loser;
  473. maxdestlen = need_length;
  474. }
  475. data = pl_base64_create_decoder();
  476. if (data == NULL)
  477. goto loser;
  478. data->output_buflen = maxdestlen;
  479. data->output_buffer = output_buffer;
  480. status = pl_base64_decode_buffer(data, (const unsigned char *)src,
  481. srclen);
  482. /*
  483. * We do not wait for Destroy to flush, because Destroy will also
  484. * get rid of our decoder context, which we need to look at first!
  485. */
  486. if (status == PR_SUCCESS)
  487. status = pl_base64_decode_flush(data);
  488. /* Must clear this or Destroy will free it. */
  489. data->output_buffer = NULL;
  490. if (status == PR_SUCCESS) {
  491. *output_destlen = data->output_length;
  492. status = PL_DestroyBase64Decoder(data, PR_FALSE);
  493. data = NULL;
  494. if (status == PR_FAILURE)
  495. goto loser;
  496. return output_buffer;
  497. }
  498. loser:
  499. if (dest == NULL && output_buffer != NULL)
  500. PR_Free(output_buffer);
  501. if (data != NULL)
  502. (void)PL_DestroyBase64Decoder(data, PR_TRUE);
  503. return NULL;
  504. }
  505. /*
  506. * XXX End of base64 decoding code to be moved into NSPR.
  507. ********************************************************
  508. */
  509. /*
  510. * This is the beginning of the NSS cover functions. These will
  511. * provide the interface we want to expose as NSS-ish. For example,
  512. * they will operate on our Items, do any special handling or checking
  513. * we want to do, etc.
  514. */
  515. PR_BEGIN_EXTERN_C
  516. /*
  517. * A boring cover structure for now. Perhaps someday it will include
  518. * some more interesting fields.
  519. */
  520. struct NSSBase64DecoderStr {
  521. PLBase64Decoder *pl_data;
  522. };
  523. PR_END_EXTERN_C
  524. /*
  525. * Function to start a base64 decoding context.
  526. */
  527. NSSBase64Decoder *
  528. NSSBase64Decoder_Create(PRInt32 (*output_fn)(void *, const unsigned char *,
  529. PRInt32),
  530. void *output_arg)
  531. {
  532. PLBase64Decoder *pl_data;
  533. NSSBase64Decoder *nss_data;
  534. nss_data = PORT_ZNew(NSSBase64Decoder);
  535. if (nss_data == NULL)
  536. return NULL;
  537. pl_data = PL_CreateBase64Decoder(output_fn, output_arg);
  538. if (pl_data == NULL) {
  539. PORT_Free(nss_data);
  540. return NULL;
  541. }
  542. nss_data->pl_data = pl_data;
  543. return nss_data;
  544. }
  545. /*
  546. * Push data through the decoder, causing the output_fn (provided to Create)
  547. * to be called with the decoded data.
  548. */
  549. SECStatus
  550. NSSBase64Decoder_Update(NSSBase64Decoder *data, const char *buffer,
  551. PRUint32 size)
  552. {
  553. PRStatus pr_status;
  554. /* XXX Should we do argument checking only in debug build? */
  555. if (data == NULL) {
  556. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  557. return SECFailure;
  558. }
  559. pr_status = PL_UpdateBase64Decoder(data->pl_data, buffer, size);
  560. if (pr_status == PR_FAILURE)
  561. return SECFailure;
  562. return SECSuccess;
  563. }
  564. /*
  565. * When you're done decoding, call this to free the data. If "abort_p"
  566. * is false, then calling this may cause the output_fn to be called
  567. * one last time (as the last buffered data is flushed out).
  568. */
  569. SECStatus
  570. NSSBase64Decoder_Destroy(NSSBase64Decoder *data, PRBool abort_p)
  571. {
  572. PRStatus pr_status;
  573. /* XXX Should we do argument checking only in debug build? */
  574. if (data == NULL) {
  575. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  576. return SECFailure;
  577. }
  578. pr_status = PL_DestroyBase64Decoder(data->pl_data, abort_p);
  579. PORT_Free(data);
  580. if (pr_status == PR_FAILURE)
  581. return SECFailure;
  582. return SECSuccess;
  583. }
  584. /*
  585. * Perform base64 decoding from an ascii string "inStr" to an Item.
  586. * The length of the input must be provided as "inLen". The Item
  587. * may be provided (as "outItemOpt"); you can also pass in a NULL
  588. * and the Item will be allocated for you.
  589. *
  590. * In any case, the data within the Item will be allocated for you.
  591. * All allocation will happen out of the passed-in "arenaOpt", if non-NULL.
  592. * If "arenaOpt" is NULL, standard allocation (heap) will be used and
  593. * you will want to free the result via SECITEM_FreeItem.
  594. *
  595. * Return value is NULL on error, the Item (allocated or provided) otherwise.
  596. */
  597. SECItem *
  598. NSSBase64_DecodeBuffer(PLArenaPool *arenaOpt, SECItem *outItemOpt,
  599. const char *inStr, unsigned int inLen)
  600. {
  601. SECItem *out_item = NULL;
  602. PRUint32 max_out_len = 0;
  603. void *mark = NULL;
  604. unsigned char *dummy = NULL;
  605. if ((outItemOpt != NULL && outItemOpt->data != NULL) || inLen == 0) {
  606. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  607. return NULL;
  608. }
  609. if (arenaOpt != NULL)
  610. mark = PORT_ArenaMark(arenaOpt);
  611. max_out_len = PL_Base64MaxDecodedLength(inLen);
  612. if (max_out_len == 0) {
  613. goto loser;
  614. }
  615. out_item = SECITEM_AllocItem(arenaOpt, outItemOpt, max_out_len);
  616. if (out_item == NULL) {
  617. goto loser;
  618. }
  619. dummy = PL_Base64DecodeBuffer(inStr, inLen, out_item->data,
  620. max_out_len, &out_item->len);
  621. if (dummy == NULL) {
  622. goto loser;
  623. }
  624. if (arenaOpt != NULL) {
  625. PORT_ArenaUnmark(arenaOpt, mark);
  626. }
  627. return out_item;
  628. loser:
  629. if (arenaOpt != NULL) {
  630. PORT_ArenaRelease(arenaOpt, mark);
  631. if (outItemOpt != NULL) {
  632. outItemOpt->data = NULL;
  633. outItemOpt->len = 0;
  634. }
  635. } else if (dummy == NULL) {
  636. SECITEM_FreeItem(out_item, (PRBool)(outItemOpt == NULL));
  637. }
  638. return NULL;
  639. }
  640. /*
  641. * XXX Everything below is deprecated. If you add new stuff, put it
  642. * *above*, not below.
  643. */
  644. /*
  645. * XXX The following "ATOB" functions are provided for backward compatibility
  646. * with current code. They should be considered strongly deprecated.
  647. * When we can convert all our code over to using the new NSSBase64Decoder_
  648. * functions defined above, we should get rid of these altogether. (Remove
  649. * protoypes from base64.h as well -- actually, remove that file completely).
  650. * If someone thinks either of these functions provides such a very useful
  651. * interface (though, as shown, the same functionality can already be
  652. * obtained by calling NSSBase64_DecodeBuffer directly), fine -- but then
  653. * that API should be provided with a nice new NSSFoo name and using
  654. * appropriate types, etc.
  655. */
  656. #include "base64.h"
  657. /*
  658. ** Return an PORT_Alloc'd string which is the base64 decoded version
  659. ** of the input string; set *lenp to the length of the returned data.
  660. */
  661. unsigned char *
  662. ATOB_AsciiToData(const char *string, unsigned int *lenp)
  663. {
  664. SECItem binary_item, *dummy;
  665. binary_item.data = NULL;
  666. binary_item.len = 0;
  667. dummy = NSSBase64_DecodeBuffer(NULL, &binary_item, string,
  668. (PRUint32)PORT_Strlen(string));
  669. if (dummy == NULL)
  670. return NULL;
  671. PORT_Assert(dummy == &binary_item);
  672. *lenp = dummy->len;
  673. return dummy->data;
  674. }
  675. /*
  676. ** Convert from ascii to binary encoding of an item.
  677. */
  678. SECStatus
  679. ATOB_ConvertAsciiToItem(SECItem *binary_item, const char *ascii)
  680. {
  681. SECItem *dummy;
  682. if (binary_item == NULL) {
  683. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  684. return SECFailure;
  685. }
  686. /*
  687. * XXX Would prefer to assert here if data is non-null (actually,
  688. * don't need to, just let NSSBase64_DecodeBuffer do it), so as to
  689. * to catch unintended memory leaks, but callers are not clean in
  690. * this respect so we need to explicitly clear here to avoid the
  691. * assert in NSSBase64_DecodeBuffer.
  692. */
  693. binary_item->data = NULL;
  694. binary_item->len = 0;
  695. dummy = NSSBase64_DecodeBuffer(NULL, binary_item, ascii,
  696. (PRUint32)PORT_Strlen(ascii));
  697. if (dummy == NULL)
  698. return SECFailure;
  699. return SECSuccess;
  700. }