/security/nss/cmd/atob/atob.c

http://github.com/zpao/v8monkey · C · 180 lines · 125 code · 18 blank · 37 comment · 25 complexity · 30a83acacbbfba429383d8ae1d3d5c8f MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. #include "plgetopt.h"
  37. #include "secutil.h"
  38. #include "nssb64.h"
  39. #include <errno.h>
  40. #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
  41. #if !defined(WIN32)
  42. extern int fread(char *, size_t, size_t, FILE*);
  43. extern int fwrite(char *, size_t, size_t, FILE*);
  44. extern int fprintf(FILE *, char *, ...);
  45. #endif
  46. #endif
  47. #if defined(WIN32)
  48. #include "fcntl.h"
  49. #include "io.h"
  50. #endif
  51. static PRInt32
  52. output_binary (void *arg, const unsigned char *obuf, PRInt32 size)
  53. {
  54. FILE *outFile = arg;
  55. int nb;
  56. nb = fwrite(obuf, 1, size, outFile);
  57. if (nb != size) {
  58. PORT_SetError(SEC_ERROR_IO);
  59. return -1;
  60. }
  61. return nb;
  62. }
  63. static SECStatus
  64. decode_file(FILE *outFile, FILE *inFile)
  65. {
  66. NSSBase64Decoder *cx;
  67. int nb;
  68. SECStatus status = SECFailure;
  69. char ibuf[4096];
  70. cx = NSSBase64Decoder_Create(output_binary, outFile);
  71. if (!cx) {
  72. return -1;
  73. }
  74. for (;;) {
  75. if (feof(inFile)) break;
  76. nb = fread(ibuf, 1, sizeof(ibuf), inFile);
  77. if (nb != sizeof(ibuf)) {
  78. if (nb == 0) {
  79. if (ferror(inFile)) {
  80. PORT_SetError(SEC_ERROR_IO);
  81. goto loser;
  82. }
  83. /* eof */
  84. break;
  85. }
  86. }
  87. status = NSSBase64Decoder_Update(cx, ibuf, nb);
  88. if (status != SECSuccess) goto loser;
  89. }
  90. return NSSBase64Decoder_Destroy(cx, PR_FALSE);
  91. loser:
  92. (void) NSSBase64Decoder_Destroy(cx, PR_TRUE);
  93. return status;
  94. }
  95. static void Usage(char *progName)
  96. {
  97. fprintf(stderr,
  98. "Usage: %s [-i input] [-o output]\n",
  99. progName);
  100. fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
  101. "-i input");
  102. fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
  103. "-o output");
  104. exit(-1);
  105. }
  106. int main(int argc, char **argv)
  107. {
  108. char *progName;
  109. SECStatus rv;
  110. FILE *inFile, *outFile;
  111. PLOptState *optstate;
  112. PLOptStatus status;
  113. inFile = 0;
  114. outFile = 0;
  115. progName = strrchr(argv[0], '/');
  116. progName = progName ? progName+1 : argv[0];
  117. /* Parse command line arguments */
  118. optstate = PL_CreateOptState(argc, argv, "i:o:");
  119. while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
  120. switch (optstate->option) {
  121. case '?':
  122. Usage(progName);
  123. break;
  124. case 'i':
  125. inFile = fopen(optstate->value, "r");
  126. if (!inFile) {
  127. fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
  128. progName, optstate->value);
  129. return -1;
  130. }
  131. break;
  132. case 'o':
  133. outFile = fopen(optstate->value, "wb");
  134. if (!outFile) {
  135. fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
  136. progName, optstate->value);
  137. return -1;
  138. }
  139. break;
  140. }
  141. }
  142. if (!inFile) inFile = stdin;
  143. if (!outFile) {
  144. #if defined(WIN32)
  145. int smrv = _setmode(_fileno(stdout), _O_BINARY);
  146. if (smrv == -1) {
  147. fprintf(stderr,
  148. "%s: Cannot change stdout to binary mode. Use -o option instead.\n",
  149. progName);
  150. return smrv;
  151. }
  152. #endif
  153. outFile = stdout;
  154. }
  155. rv = decode_file(outFile, inFile);
  156. if (rv != SECSuccess) {
  157. fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
  158. progName, PORT_GetError(), errno);
  159. return -1;
  160. }
  161. return 0;
  162. }