View Javadoc
1   /*
2    * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package davmail.ldap;
27  
28  import java.io.OutputStream;
29  import java.io.IOException;
30  import java.io.ByteArrayInputStream;
31  import java.nio.charset.StandardCharsets;
32  
33  
34  /**
35   * Base class that defines common fields, constants, and debug method.
36   *
37   * @author Jagane Sundar
38   */
39  @SuppressWarnings("unused")
40  public abstract class Ber {
41  
42      protected byte[] buf;
43      protected int offset;
44      protected int bufsize;
45  
46      protected Ber() {
47      }
48  
49      public static void dumpBER(OutputStream outStream, String tag, byte[] bytes,
50                                 int from, int to) {
51  
52          try {
53              outStream.write('\n');
54              outStream.write(tag.getBytes(StandardCharsets.UTF_8));
55  
56              new HexDumpEncoder().encodeBuffer(
57                      new ByteArrayInputStream(bytes, from, to),
58                      outStream);
59  
60              outStream.write('\n');
61          } catch (IOException e) {
62              try {
63                  outStream.write(
64                          "Ber.dumpBER(): error encountered\n".getBytes(StandardCharsets.UTF_8));
65              } catch (IOException e2) {
66                  // ignore
67              }
68          }
69      }
70  
71      ////////////////////////////////////////////////////////////////////////////
72      //
73      // some ASN defines
74      //
75      ////////////////////////////////////////////////////////////////////////////
76  
77      public static final int ASN_BOOLEAN         = 0x01;
78      public static final int ASN_INTEGER         = 0x02;
79      public static final int ASN_BIT_STRING      = 0x03;
80      public static final int ASN_SIMPLE_STRING   = 0x04;
81      public static final int ASN_OCTET_STR       = 0x04;
82      public static final int ASN_NULL            = 0x05;
83      public static final int ASN_OBJECT_ID       = 0x06;
84      public static final int ASN_SEQUENCE        = 0x10;
85      public static final int ASN_SET             = 0x11;
86  
87  
88      public static final int ASN_PRIMITIVE       = 0x00;
89      public static final int ASN_UNIVERSAL       = 0x00;
90      public static final int ASN_CONSTRUCTOR     = 0x20;
91      public static final int ASN_APPLICATION     = 0x40;
92      public static final int ASN_CONTEXT         = 0x80;
93      public static final int ASN_PRIVATE         = 0xC0;
94  
95      public static final int ASN_ENUMERATED      = 0x0a;
96  
97      final static class EncodeException extends IOException {
98          private static final long serialVersionUID = -5247359637775781768L;
99          EncodeException(String msg) {
100             super(msg);
101         }
102     }
103 
104     final static class DecodeException extends IOException {
105         private static final long serialVersionUID = 8735036969244425583L;
106         DecodeException(String msg) {
107             super(msg);
108         }
109     }
110 }