From f5cf3600eb5904517d2777805667a38e5314785b Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Fri, 24 Aug 2007 20:36:15 +0000
Subject: [PATCH] Add a tool that can be used to base64 encode or decode information.  The source can be provided as a string, contained in a file, or piped in via standard input, and the result can be sent to standard output or written to a file.

---
 opends/resource/bin/base64.bat                       |   33 ++++
 opends/src/server/org/opends/server/util/Base64.java |  318 ++++++++++++++++++++++++++++++++++++++++++++
 opends/resource/bin/base64                           |   37 +++++
 opends/src/messages/messages/utility.properties      |   25 +++
 4 files changed, 409 insertions(+), 4 deletions(-)

diff --git a/opends/resource/bin/base64 b/opends/resource/bin/base64
new file mode 100755
index 0000000..eb290f9
--- /dev/null
+++ b/opends/resource/bin/base64
@@ -0,0 +1,37 @@
+#!/bin/sh
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License, Version 1.0 only
+# (the "License").  You may not use this file except in compliance
+# with the License.
+#
+# You can obtain a copy of the license at
+# trunk/opends/resource/legal-notices/OpenDS.LICENSE
+# or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at
+# trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+# add the following below this CDDL HEADER, with the fields enclosed
+# by brackets "[]" replaced with your own identifying information:
+#      Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#
+#      Portions Copyright 2007 Sun Microsystems, Inc.
+
+
+# This script may be used to display the status panel.
+OPENDS_INVOKE_CLASS="org.opends.server.util.Base64"
+export OPENDS_INVOKE_CLASS
+
+SCRIPT_NAME_ARG="-Dorg.opends.server.scriptName=base64"
+export SCRIPT_NAME_ARG
+
+SCRIPT_DIR=`dirname "${0}"`
+"${SCRIPT_DIR}/../lib/_client-script.sh" "${@}"
diff --git a/opends/resource/bin/base64.bat b/opends/resource/bin/base64.bat
new file mode 100644
index 0000000..233343e
--- /dev/null
+++ b/opends/resource/bin/base64.bat
@@ -0,0 +1,33 @@
+
+@echo off
+rem CDDL HEADER START
+rem
+rem The contents of this file are subject to the terms of the
+rem Common Development and Distribution License, Version 1.0 only
+rem (the "License").  You may not use this file except in compliance
+rem with the License.
+rem
+rem You can obtain a copy of the license at
+rem trunk/opends/resource/legal-notices/OpenDS.LICENSE
+rem or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+rem See the License for the specific language governing permissions
+rem and limitations under the License.
+rem
+rem When distributing Covered Code, include this CDDL HEADER in each
+rem file and include the License file at
+rem trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+rem add the following below this CDDL HEADER, with the fields enclosed
+rem by brackets "[]" replaced with your own identifying information:
+rem      Portions Copyright [yyyy] [name of copyright owner]
+rem
+rem CDDL HEADER END
+rem
+rem
+rem      Portions Copyright 2006-2007 Sun Microsystems, Inc.
+
+setlocal
+
+set OPENDS_INVOKE_CLASS="org.opends.server.util.Base64"
+set SCRIPT_NAME_ARG="-Dorg.opends.server.scriptName=base64"
+for %%i in (%~sf0) do call "%%~dPsi\..\lib\_client-script.bat" %*
+
diff --git a/opends/src/messages/messages/utility.properties b/opends/src/messages/messages/utility.properties
index 47e1299..780cd34 100644
--- a/opends/src/messages/messages/utility.properties
+++ b/opends/src/messages/messages/utility.properties
@@ -448,3 +448,28 @@
  attach file %s:  %s
 SEVERE_ERR_EMAIL_CANNOT_SEND_MESSAGE_185=An error occurred while trying to \
  send the e-mail message:  %s
+INFO_BASE64_TOOL_DESCRIPTION_186=Encode and decode information using base64
+INFO_BASE64_HELP_DESCRIPTION_187=Display this usage information
+INFO_BASE64_DECODE_DESCRIPTION_188=Decode base64-encoded information into \
+ raw data
+INFO_BASE64_ENCODE_DESCRIPTION_189=Encode raw data using base64
+INFO_BASE64_ENCODED_DATA_DESCRIPTION_190=The base64-encoded data to be decoded
+INFO_BASE64_ENCODED_FILE_DESCRIPTION_191=The path to a file containing the \
+ base64-encoded data to be decoded
+INFO_BASE64_RAW_DATA_DESCRIPTION_192=The raw data to be base64 encoded
+INFO_BASE64_RAW_FILE_DESCRIPTION_193=The path to a file containing the raw \
+ data to be base64 encoded
+INFO_BASE64_TO_ENCODED_FILE_DESCRIPTION_194=The path to a file to which the \
+ base64-encoded data should be written
+INFO_BASE64_TO_RAW_FILE_DESCRIPTION_195=The path to a file to which the raw \
+ base64-decoded data should be written
+SEVERE_ERR_BASE64_CANNOT_READ_RAW_DATA_196=An error occurred while attempting \
+ to read the raw data to encode:  %s
+SEVERE_ERR_BASE64_CANNOT_WRITE_ENCODED_DATA_197=An error occurred while \
+ attempting to write the encoded data:  %s
+SEVERE_ERR_BASE64_CANNOT_READ_ENCODED_DATA_198=An error occurred while \
+ attempting to read the base64-encoded data:  %s
+SEVERE_ERR_BASE64_CANNOT_WRITE_RAW_DATA_199=An error occurred while \
+ attempting to write the decoded data:  %s
+SEVERE_ERR_BASE64_UNKNOWN_SUBCOMMAND_200=Unknown subcommand %s
+
diff --git a/opends/src/server/org/opends/server/util/Base64.java b/opends/src/server/org/opends/server/util/Base64.java
index 267da86..657c214 100644
--- a/opends/src/server/org/opends/server/util/Base64.java
+++ b/opends/src/server/org/opends/server/util/Base64.java
@@ -25,15 +25,34 @@
  *      Portions Copyright 2006-2007 Sun Microsystems, Inc.
  */
 package org.opends.server.util;
-import org.opends.messages.Message;
 
 
 
-import static org.opends.messages.UtilityMessages.*;
-import static org.opends.server.util.Validator.*;
-
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.nio.ByteBuffer;
 import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.StringTokenizer;
+
+import org.opends.messages.Message;
+import org.opends.messages.MessageBuilder;
+import org.opends.server.util.args.ArgumentException;
+import org.opends.server.util.args.BooleanArgument;
+import org.opends.server.util.args.StringArgument;
+import org.opends.server.util.args.SubCommand;
+import org.opends.server.util.args.SubCommandArgumentParser;
+
+import static org.opends.messages.UtilityMessages.*;
+import static org.opends.server.util.StaticUtils.*;
+import static org.opends.server.util.Validator.*;
 
 
 
@@ -386,5 +405,296 @@
     buffer.get(returnArray);
     return returnArray;
   }
+
+
+
+  /**
+   * Provide a command-line utility that may be used to base64-encode and
+   * decode strings and file contents.
+   *
+   * @param  args  The command-line arguments provided to this program.
+   */
+  public static void main(String[] args)
+  {
+    Message description = INFO_BASE64_TOOL_DESCRIPTION.get();
+    SubCommandArgumentParser argParser =
+         new SubCommandArgumentParser(Base64.class.getName(), description,
+                                      false);
+
+    BooleanArgument showUsage        = null;
+    StringArgument  encodedData      = null;
+    StringArgument  encodedFile      = null;
+    StringArgument  rawData          = null;
+    StringArgument  rawFile          = null;
+    StringArgument  toEncodedFile    = null;
+    StringArgument  toRawFile        = null;
+    SubCommand      decodeSubCommand = null;
+    SubCommand      encodeSubCommand = null;
+
+    try
+    {
+      decodeSubCommand = new SubCommand(argParser, "decode",
+                                        INFO_BASE64_DECODE_DESCRIPTION.get());
+
+      encodeSubCommand = new SubCommand(argParser, "encode",
+                                        INFO_BASE64_ENCODE_DESCRIPTION.get());
+
+
+      encodedData = new StringArgument("encodeddata", 'd', "encodedData", false,
+                             false, true, "{data}", null, null,
+                             INFO_BASE64_ENCODED_DATA_DESCRIPTION.get());
+      decodeSubCommand.addArgument(encodedData);
+
+
+      encodedFile = new StringArgument("encodedfile", 'f', "encodedDataFile",
+                             false, false, true, "{path}", null, null,
+                             INFO_BASE64_ENCODED_FILE_DESCRIPTION.get());
+      decodeSubCommand.addArgument(encodedFile);
+
+
+      toRawFile = new StringArgument("torawfile", 'o', "toRawFile", false,
+                                     false, true, "{path}", null, null,
+                                     INFO_BASE64_TO_RAW_FILE_DESCRIPTION.get());
+      decodeSubCommand.addArgument(toRawFile);
+
+
+      rawData = new StringArgument("rawdata", 'd', "rawData", false, false,
+                                   true, "{data}", null, null,
+                                   INFO_BASE64_RAW_DATA_DESCRIPTION.get());
+      encodeSubCommand.addArgument(rawData);
+
+
+      rawFile = new StringArgument("rawfile", 'f', "rawDataFile", false, false,
+                                   true, "{path}", null, null,
+                                   INFO_BASE64_RAW_FILE_DESCRIPTION.get());
+      encodeSubCommand.addArgument(rawFile);
+
+
+      toEncodedFile = new StringArgument("toencodedfile", 'o', "toEncodedFile",
+                               false, false, true, "{path}", null, null,
+                               INFO_BASE64_TO_ENCODED_FILE_DESCRIPTION.get());
+      encodeSubCommand.addArgument(toEncodedFile);
+
+
+      ArrayList<SubCommand> subCommandList = new ArrayList<SubCommand>(2);
+      subCommandList.add(decodeSubCommand);
+      subCommandList.add(encodeSubCommand);
+
+
+      showUsage = new BooleanArgument("help", 'H', "help",
+                                      INFO_BASE64_HELP_DESCRIPTION.get());
+      argParser.addGlobalArgument(showUsage);
+      argParser.setUsageGroupArgument(showUsage, subCommandList);
+    }
+    catch (ArgumentException ae)
+    {
+      System.err.println(ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage()));
+      System.exit(1);
+    }
+
+    try
+    {
+      argParser.parseArguments(args);
+    }
+    catch (ArgumentException ae)
+    {
+      System.err.println(ERR_CANNOT_PARSE_ARGS.get(ae.getMessage()).toString());
+      System.exit(1);
+    }
+
+    SubCommand subCommand = argParser.getSubCommand();
+    if (showUsage.isPresent())
+    {
+      if (subCommand == null)
+      {
+        System.out.println(argParser.getUsage());
+      }
+      else
+      {
+        MessageBuilder messageBuilder = new MessageBuilder();
+        argParser.getSubCommandUsage(messageBuilder, subCommand);
+        System.out.println(messageBuilder.toString());
+      }
+
+      return;
+    }
+
+    if (subCommand == null)
+    {
+      System.err.println(argParser.getUsage());
+      System.exit(1);
+    }
+    if (subCommand.getName().equals(encodeSubCommand.getName()))
+    {
+      byte[] dataToEncode = null;
+      if (rawData.isPresent())
+      {
+        dataToEncode = rawData.getValue().getBytes();
+      }
+      else
+      {
+        try
+        {
+          boolean shouldClose;
+          InputStream inputStream;
+          if (rawFile.isPresent())
+          {
+            inputStream = new FileInputStream(rawFile.getValue());
+            shouldClose = true;
+          }
+          else
+          {
+            inputStream = System.in;
+            shouldClose = false;
+          }
+
+          ByteArrayOutputStream baos = new ByteArrayOutputStream();
+          byte[] buffer = new byte[8192];
+          while (true)
+          {
+            int bytesRead = inputStream.read(buffer);
+            if (bytesRead < 0)
+            {
+              break;
+            }
+            else
+            {
+              baos.write(buffer, 0, bytesRead);
+            }
+          }
+
+          if (shouldClose)
+          {
+            inputStream.close();
+          }
+
+          dataToEncode = baos.toByteArray();
+        }
+        catch (Exception e)
+        {
+          System.err.println(ERR_BASE64_CANNOT_READ_RAW_DATA.get(
+                                  getExceptionMessage(e)).toString());
+          System.exit(1);
+        }
+      }
+
+      String base64Data = encode(dataToEncode);
+      if (toEncodedFile.isPresent())
+      {
+        try
+        {
+          BufferedWriter writer =
+               new BufferedWriter(new FileWriter(toEncodedFile.getValue()));
+          writer.write(base64Data);
+          writer.newLine();
+          writer.close();
+        }
+        catch (Exception e)
+        {
+          System.err.println(ERR_BASE64_CANNOT_WRITE_ENCODED_DATA.get(
+                                  getExceptionMessage(e)).toString());
+          System.exit(1);
+        }
+      }
+      else
+      {
+        System.out.println(base64Data);
+      }
+    }
+    else if (subCommand.getName().equals(decodeSubCommand.getName()))
+    {
+      String dataToDecode = null;
+      if (rawData.isPresent())
+      {
+        dataToDecode = rawData.getValue();
+      }
+      else
+      {
+        try
+        {
+          boolean shouldClose;
+          BufferedReader reader;
+          if (encodedFile.isPresent())
+          {
+            reader = new BufferedReader(new FileReader(encodedFile.getValue()));
+            shouldClose = true;
+          }
+          else
+          {
+            reader = new BufferedReader(new InputStreamReader(System.in));
+            shouldClose = false;
+          }
+
+          StringBuilder buffer = new StringBuilder();
+          while (true)
+          {
+            String line = reader.readLine();
+            if (line == null)
+            {
+              break;
+            }
+
+            StringTokenizer tokenizer = new StringTokenizer(line);
+            while (tokenizer.hasMoreTokens())
+            {
+              buffer.append(tokenizer.nextToken());
+            }
+          }
+
+          if (shouldClose)
+          {
+            reader.close();
+          }
+
+          dataToDecode = buffer.toString();
+        }
+        catch (Exception e)
+        {
+          System.err.println(ERR_BASE64_CANNOT_READ_ENCODED_DATA.get(
+                                  getExceptionMessage(e)).toString());
+          System.exit(1);
+        }
+      }
+
+      byte[] decodedData = null;
+      try
+      {
+        decodedData = decode(dataToDecode);
+      }
+      catch (ParseException pe)
+      {
+        System.err.println(pe.getMessage());
+        System.exit(1);
+      }
+
+      try
+      {
+        if (toRawFile.isPresent())
+        {
+          FileOutputStream outputStream =
+               new FileOutputStream(toRawFile.getValue());
+          outputStream.write(decodedData);
+          outputStream.close();
+        }
+        else
+        {
+          System.out.write(decodedData);
+          System.out.flush();
+        }
+      }
+      catch (Exception e)
+      {
+        System.err.println(ERR_BASE64_CANNOT_WRITE_RAW_DATA.get(
+                                getExceptionMessage(e)).toString());
+        System.exit(1);
+      }
+    }
+    else
+    {
+      System.err.println(ERR_BASE64_UNKNOWN_SUBCOMMAND.get(
+                              subCommand.getName()).toString());
+      System.exit(1);
+    }
+  }
 }
 

--
Gitblit v1.10.0