initial commit

This commit is contained in:
Jörg Prante 2017-11-16 00:37:06 +01:00
commit 832c86a73d
192 changed files with 27423 additions and 0 deletions

9
.gitignore vendored Normal file
View file

@ -0,0 +1,9 @@
/.idea
/.settings
/.classpath
/.project
/.gradle
build
.DS_Store
*.iml
*.properties

14
CREDITS.txt Normal file
View file

@ -0,0 +1,14 @@
This software is based on the work of
Zebulun https://github.com/jafer-org/zebulun
a low level Java implementation of the NISO Z39.50 protocol,
and
Java Access For Electronic Resources (Jafer) https://github.com/jafer-org/jafer
The JAFER Project (Java Access For Electronic Resources) was a Jisc funded project (2000 - 2003)
based at Oxford University to produce an easy to use toolkit for building portals
and information sources without having to deal with the technical intricacies of the Z39.50 protocol.

202
LICENSE.txt Normal file
View file

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -0,0 +1,323 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<!-- This is a checkstyle configuration file. For descriptions of
what the following rules do, please see the checkstyle configuration
page at http://checkstyle.sourceforge.net/config.html -->
<module name="Checker">
<module name="FileTabCharacter">
<!-- Checks that there are no tab characters in the file.
-->
</module>
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf"/>
</module>
<module name="RegexpSingleline">
<!-- Checks that FIXME is not used in comments. TODO is preferred.
-->
<property name="format" value="((//.*)|(\*.*))FIXME" />
<property name="message" value='TODO is preferred to FIXME. e.g. "TODO(johndoe): Refactor when v2 is released."' />
</module>
<module name="RegexpSingleline">
<!-- Checks that TODOs are named. (Actually, just that they are followed
by an open paren.)
-->
<property name="format" value="((//.*)|(\*.*))TODO[^(]" />
<property name="message" value='All TODOs should be named. e.g. "TODO(johndoe): Refactor when v2 is released."' />
</module>
<module name="JavadocPackage">
<!-- Checks that each Java package has a Javadoc file used for commenting.
Only allows a package-info.java, not package.html. -->
</module>
<!-- All Java AST specific tests live under TreeWalker module. -->
<module name="TreeWalker">
<!--
IMPORT CHECKS
-->
<module name="RedundantImport">
<!-- Checks for redundant import statements. -->
<property name="severity" value="error"/>
</module>
<module name="ImportOrder">
<!-- Checks for out of order import statements. -->
<property name="severity" value="warning"/>
<property name="groups" value="com.google,android,junit,net,org,java,javax"/>
<!-- This ensures that static imports go first. -->
<property name="option" value="top"/>
<property name="tokens" value="STATIC_IMPORT, IMPORT"/>
</module>
<!--
JAVADOC CHECKS
-->
<!-- Checks for Javadoc comments. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
<module name="JavadocMethod">
<property name="scope" value="protected"/>
<property name="severity" value="warning"/>
<property name="allowMissingJavadoc" value="true"/>
<property name="allowMissingParamTags" value="true"/>
<property name="allowMissingReturnTag" value="true"/>
<property name="allowMissingThrowsTags" value="true"/>
<property name="allowThrowsTagsForSubclasses" value="true"/>
<property name="allowUndeclaredRTE" value="true"/>
</module>
<module name="JavadocType">
<property name="scope" value="protected"/>
<property name="severity" value="error"/>
</module>
<module name="JavadocStyle">
<property name="severity" value="warning"/>
</module>
<!--
NAMING CHECKS
-->
<!-- Item 38 - Adhere to generally accepted naming conventions -->
<module name="PackageName">
<!-- Validates identifiers for package names against the
supplied expression. -->
<!-- Here the default checkstyle rule restricts package name parts to
seven characters, this is not in line with common practice at Google.
-->
<property name="format" value="^[a-z]+(\.[a-z][a-z0-9]{1,})*$"/>
<property name="severity" value="warning"/>
</module>
<module name="TypeNameCheck">
<!-- Validates static, final fields against the
expression "^[A-Z][a-zA-Z0-9]*$". -->
<metadata name="altname" value="TypeName"/>
<property name="severity" value="warning"/>
</module>
<module name="ConstantNameCheck">
<!-- Validates non-private, static, final fields against the supplied
public/package final fields "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$". -->
<metadata name="altname" value="ConstantName"/>
<property name="applyToPublic" value="true"/>
<property name="applyToProtected" value="true"/>
<property name="applyToPackage" value="true"/>
<property name="applyToPrivate" value="false"/>
<property name="format" value="^([A-Z][A-Z0-9]*(_[A-Z0-9]+)*|FLAG_.*)$"/>
<message key="name.invalidPattern"
value="Variable ''{0}'' should be in ALL_CAPS (if it is a constant) or be private (otherwise)."/>
<property name="severity" value="warning"/>
</module>
<module name="StaticVariableNameCheck">
<!-- Validates static, non-final fields against the supplied
expression "^[a-z][a-zA-Z0-9]*_?$". -->
<metadata name="altname" value="StaticVariableName"/>
<property name="applyToPublic" value="true"/>
<property name="applyToProtected" value="true"/>
<property name="applyToPackage" value="true"/>
<property name="applyToPrivate" value="true"/>
<property name="format" value="^[a-z][a-zA-Z0-9]*_?$"/>
<property name="severity" value="warning"/>
</module>
<module name="MemberNameCheck">
<!-- Validates non-static members against the supplied expression. -->
<metadata name="altname" value="MemberName"/>
<property name="applyToPublic" value="true"/>
<property name="applyToProtected" value="true"/>
<property name="applyToPackage" value="true"/>
<property name="applyToPrivate" value="true"/>
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
<property name="severity" value="warning"/>
</module>
<module name="MethodNameCheck">
<!-- Validates identifiers for method names. -->
<metadata name="altname" value="MethodName"/>
<property name="format" value="^[a-z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$"/>
<property name="severity" value="warning"/>
</module>
<module name="ParameterName">
<!-- Validates identifiers for method parameters against the
expression "^[a-z][a-zA-Z0-9]*$". -->
<property name="severity" value="warning"/>
</module>
<module name="LocalFinalVariableName">
<!-- Validates identifiers for local final variables against the
expression "^[a-z][a-zA-Z0-9]*$". -->
<property name="severity" value="warning"/>
</module>
<module name="LocalVariableName">
<!-- Validates identifiers for local variables against the
expression "^[a-z][a-zA-Z0-9]*$". -->
<property name="severity" value="warning"/>
</module>
<!--
LENGTH and CODING CHECKS
-->
<module name="LineLength">
<!-- Checks if a line is too long. -->
<property name="max" value="${com.puppycrawl.tools.checkstyle.checks.sizes.LineLength.max}" default="128"/>
<property name="severity" value="error"/>
<!--
The default ignore pattern exempts the following elements:
- import statements
- long URLs inside comments
-->
<property name="ignorePattern"
value="${com.puppycrawl.tools.checkstyle.checks.sizes.LineLength.ignorePattern}"
default="^(package .*;\s*)|(import .*;\s*)|( *(\*|//).*https?://.*)$"/>
</module>
<module name="LeftCurly">
<!-- Checks for placement of the left curly brace ('{'). -->
<property name="severity" value="warning"/>
</module>
<module name="RightCurly">
<!-- Checks right curlies on CATCH, ELSE, and TRY blocks are on
the same line. e.g., the following example is fine:
<pre>
if {
...
} else
</pre>
-->
<!-- This next example is not fine:
<pre>
if {
...
}
else
</pre>
-->
<property name="option" value="same"/>
<property name="severity" value="warning"/>
</module>
<!-- Checks for braces around if and else blocks -->
<module name="NeedBraces">
<property name="severity" value="warning"/>
<property name="tokens" value="LITERAL_IF, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO"/>
</module>
<module name="UpperEll">
<!-- Checks that long constants are defined with an upper ell.-->
<property name="severity" value="error"/>
</module>
<module name="FallThrough">
<!-- Warn about falling through to the next case statement. Similar to
javac -Xlint:fallthrough, but the check is suppressed if a single-line comment
on the last non-blank line preceding the fallen-into case contains 'fall through' (or
some other variants which we don't publicized to promote consistency).
-->
<property name="reliefPattern"
value="fall through|Fall through|fallthru|Fallthru|falls through|Falls through|fallthrough|Fallthrough|No break|NO break|no break|continue on"/>
<property name="severity" value="error"/>
</module>
<!--
MODIFIERS CHECKS
-->
<module name="ModifierOrder">
<!-- Warn if modifier order is inconsistent with JLS3 8.1.1, 8.3.1, and
8.4.3. The prescribed order is:
public, protected, private, abstract, static, final, transient, volatile,
synchronized, native, strictfp
-->
</module>
<!--
WHITESPACE CHECKS
-->
<module name="WhitespaceAround">
<!-- Checks that various tokens are surrounded by whitespace.
This includes most binary operators and keywords followed
by regular or curly braces.
-->
<property name="tokens" value="ASSIGN, BAND, BAND_ASSIGN, BOR,
BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN,
EQUAL, GE, GT, LAND, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE,
LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN,
LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS,
MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION,
SL, SL_ASSIGN, SR_ASSIGN, STAR, STAR_ASSIGN"/>
<property name="severity" value="error"/>
</module>
<module name="WhitespaceAfter">
<!-- Checks that commas, semicolons and typecasts are followed by
whitespace.
-->
<property name="tokens" value="COMMA, SEMI, TYPECAST"/>
</module>
<module name="NoWhitespaceAfter">
<!-- Checks that there is no whitespace after various unary operators.
Linebreaks are allowed.
-->
<property name="tokens" value="BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS,
UNARY_PLUS"/>
<property name="allowLineBreaks" value="true"/>
<property name="severity" value="error"/>
</module>
<module name="NoWhitespaceBefore">
<!-- Checks that there is no whitespace before various unary operators.
Linebreaks are allowed.
-->
<property name="tokens" value="SEMI, DOT, POST_DEC, POST_INC"/>
<property name="allowLineBreaks" value="true"/>
<property name="severity" value="error"/>
</module>
<module name="ParenPad">
<!-- Checks that there is no whitespace before close parens or after
open parens.
-->
<property name="severity" value="warning"/>
</module>
</module>
</module>

View file

@ -0,0 +1,24 @@
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<localRepository>~/.m2/repository</localRepository>
<activeProfiles>
<activeProfile>xbib</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>xbib</id>
<repositories>
<repository>
<id>xbib</id>
<url>http://xbib.org/repository</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
</settings>

View file

@ -0,0 +1,94 @@
package org.xbib.asn1;
/**
* ASN1 ANY
* The ANY type denotes an arbitary value of an arbitary type.
* This class also serves as the base class for all ASN.1 classes.
* The ASN.1 syntax is defined in
* <strong>Information Technology - Open Systems Interconnection -
* Specification of Abstract Syntax Notation One (ASN.1)</strong>
* AS 3625-1991
* ISO/IEC 8824:1990
* The current implementation assumes values are limited to 32-bit
* signed integers for tags, lengths, etc.
*/
public class ASN1Any {
/* Hack to support creation of ASN1 ANY types from a BER and have
it behave normally. This is not used by any other ASN.1 subclasses.
It is a waste of space in that respect. */
private BEREncoding asn1anyBer;
public ASN1Any() {
//
}
/**
* Constructor for an ASN.1 ANY object from a BER encoding.
*
* @param ber The BER encoding to use.
* @param checkTag If true, it checks the tag. Does nothing for ASN1Any.
* @throws ASN1Exception if the BER encoding is incorrect.
*/
public ASN1Any(BEREncoding ber, boolean checkTag) throws ASN1Exception {
berDecode(ber, checkTag);
}
/**
* Method for initializing the object from a BER encoding.
* All classes derived from this one must implement a version of this.
* This method will be overridden by derived types.
*
* @param berEncoding The BER encoding to use.
* @param checkTag If true, it checks the tag. Does nothing for ASN1Any.
* @throws ASN1Exception If the BER encoding is incorrect.
* Never occurs for ASN1Any.
*/
public void berDecode(BEREncoding berEncoding, boolean checkTag)
throws ASN1Exception {
asn1anyBer = berEncoding;
}
/**
* Constructs a BER encoding for this ASN.1 object.
* This method is usually overridden by a subclass method.
*
* @return BER encoding
* @throws ASN1Exception If the object cannot be BER encoded.
*/
public BEREncoding berEncode() throws ASN1Exception {
if (asn1anyBer == null) {
throw new ASN1EncodingException("uninitialised");
}
return asn1anyBer;
}
/**
* Returns a BER encoding of ASN1Any, implicitly tagged.
*
* @param tagType The type of the implicit tag.
* @param tag The implicit tag number.
* @return The BER encoding of the object.
* @throws ASN1Exception when invalid or cannot be encoded.
*/
public BEREncoding berEncode(int tagType, int tag) throws ASN1Exception {
if (asn1anyBer == null) {
throw new ASN1EncodingException("uninitialised");
}
throw new ASN1EncodingException("cannot implicitly tag");
}
/**
* Returns a new String object representing this ASN.1 object's value.
*
* @return A text string representation.
*/
@Override
public String toString() {
if (asn1anyBer == null) {
return "<empty ASN.1 ANY>";
}
return asn1anyBer.toString();
}
}

View file

@ -0,0 +1,156 @@
package org.xbib.asn1;
/**
* Representation of an ASN.1 <code>BIT STRING</code>.
* The BIT STRING type denotes an arbitary string of bits (ones and zeros).
* A BIT STRING value can have any length, including zero. The type is a
* string type.
*/
public final class ASN1BitString extends ASN1Any {
/**
* This constant is the ASN.1 UNIVERSAL tag value for BIT STRING.
*/
public static final int BIT_STRING_TAG = 0x03;
/**
* The values of the BIT STRING are stored in this array of boolean
* values.
*/
private boolean[] bits;
/**
* Constructor for an ASN.1 BIT STRING object. It sets the tag
* to the default value of UNIVERSAL 3, and the bits to the
* given bit values.
*
* @param bitValues - array of booleans representing the bit string.
*/
public ASN1BitString(boolean[] bitValues) {
bits = bitValues;
}
/**
* Constructor for an ASN.1 BIT STRING object from a BER encoding.
*
* @param ber The BER encoding to use.
* @param checkTag If true, it checks the tag. Use false if is implicitly tagged.
* @throws ASN1Exception If the BER encoding is incorrect.
*/
public ASN1BitString(BEREncoding ber, boolean checkTag)
throws ASN1Exception {
super(ber, checkTag); // superclass will call berDecode
}
/**
* Method for initializing the object from a BER encoding.
*
* @param berEncoding The BER encoding to use.
* @param checkTag If true, it checks the tag. Use false if is implicitly tagged.
* @throws ASN1EncodingException If the BER encoding is incorrect.
*/
@Override
public void berDecode(BEREncoding berEncoding, boolean checkTag)
throws ASN1EncodingException {
if (checkTag && (berEncoding.tagGet() != BIT_STRING_TAG || berEncoding.tagTypeGet() != BEREncoding.UNIVERSAL_TAG)) {
throw new ASN1EncodingException("ASN.1 BIT STRING: bad BER: tag=" + berEncoding.tagGet() +
" expected " + BIT_STRING_TAG + "\n");
}
if (berEncoding instanceof BERPrimitive) {
BERPrimitive ber = (BERPrimitive) berEncoding;
int[] encoding = ber.peek();
if (encoding.length < 1) {
throw new ASN1EncodingException("ASN1 BIT STRING: invalid encoding, length = " + encoding.length);
}
int unusedBits = encoding[0] & 0x07;
int numBits = (encoding.length - 1) * 8 - unusedBits;
bits = new boolean[numBits];
for (int bit = 0; bit < numBits; bit++) {
int octet = encoding[bit / 8 + 1];
octet <<= (bit % 8);
bits[bit] = (octet & 0x80) != 0;
}
} else {
throw new ASN1EncodingException("ASN.1 BIT STRING: decoding constructed NOT IMPLEMENTED YET");
}
}
/**
* Returns a BER encoding of the BIT STRING.
* Bit strings can have a primitive encoding and a constructed
* encoding. This method performs the primitive encoding (which
* is the one specified for DER encoding).
*
* @return The BER encoding of the BIT STRING
* @throws ASN1Exception when the BIT STRING is invalid
* and cannot be encoded.
*/
@Override
public BEREncoding berEncode() throws ASN1Exception {
return berEncode(BEREncoding.UNIVERSAL_TAG, BIT_STRING_TAG);
}
/**
* Returns a BER encoding of the BIT STRING.
* Bit strings can have a primitive encoding and a constructed
* encoding. This method performs the primitive encoding (which
* is the one specified for DER encoding).
*
* @return The BER encoding of the BIT STRING
* @throws ASN1Exception when the BIT STRING is invalid
* and cannot be encoded.
*/
@Override
public BEREncoding berEncode(int tagType, int tag) throws ASN1Exception {
int numOctets = (bits.length + 7) / 8;
int[] encoding = new int[numOctets + 1];
encoding[0] = (numOctets * 8) - bits.length;
for (int count = 1; count <= numOctets; count++) {
encoding[count] = 0x00;
int bitBaseIndex = (count - 1) * 8;
for (int bitIndex = 0; bitIndex < 8; bitIndex++) {
int n = bitBaseIndex + bitIndex;
encoding[count] <<= 1;
if (n < bits.length && bits[n]) {
encoding[count] |= 0x01;
}
}
}
return new BERPrimitive(tagType, tag, encoding);
}
/**
* Method to set the bit string's value.
*
* @param newBits the value to set the BIT STRING to.
* @return the object.
*/
public ASN1BitString set(boolean[] newBits) {
bits = newBits;
return this;
}
/**
* Method to get the bit string's value.
*
* @return the BIT STRING's current value.
*/
public boolean[] get() {
return bits;
}
/**
* Returns a new String object representing this ASN.1 object's value.
*
* @return A text string representation of the BitString.
*/
@Override
public String toString() {
StringBuilder str = new StringBuilder();
str.append('\'');
for (boolean bit : bits) {
str.append(bit ? '1' : '0');
}
str.append("'B");
return str.toString();
}
}

View file

@ -0,0 +1,127 @@
package org.xbib.asn1;
/**
* Representation of an ASN.1 <code>BOOLEAN</code>.
* The BOOLEAN type denotes a Boolean value: either true or false.
*/
public final class ASN1Boolean extends ASN1Any {
/**
* This constant is the ASN.1 UNIVERSAL tag value for BOOLEAN.
*/
public static final int TAG = 0x01;
/**
* The value of the BOOLEAN is stored in this variable.
*/
private boolean value;
/**
* Default constructor for an ASN.1 BOOLEAN object. It sets the tag
* to the default of UNIVERSAL 1, and the value to bool.
*
* @param bool the value of the BOOLEAN.
*/
public ASN1Boolean(boolean bool) {
value = bool;
}
/**
* Constructor for an ASN.1 BOOLEAN object from a BER encoding.
*
* @param ber The BER encoding to use.
* @param checkTag If true, it checks the tag. Use false if is implicitly tagged.
* @throws ASN1Exception if the BER encoding is incorrect.
*/
public ASN1Boolean(BEREncoding ber, boolean checkTag)
throws ASN1Exception {
super(ber, checkTag); // superclass will call berDecode
}
/**
* Method for initializing the object from a BER encoding.
*
* @param berEncoding The BER encoding to use.
* @param checkTag If true, it checks the tag. Use false if is implicitly tagged.
* @throws ASN1EncodingException if the BER encoding is incorrect.
*/
@Override
public void berDecode(BEREncoding berEncoding, boolean checkTag)
throws ASN1EncodingException {
if (checkTag && (berEncoding.tagGet() != TAG || berEncoding.tagTypeGet() != BEREncoding.UNIVERSAL_TAG)) {
throw new ASN1EncodingException
("ASN.1 BOOLEAN: bad BER: tag=" + berEncoding.tagGet() +
" expected " + "TAG\n");
}
if (berEncoding instanceof BERPrimitive) {
BERPrimitive ber = (BERPrimitive) berEncoding;
int[] encoding = ber.peek();
if (encoding.length != 1) {
throw new ASN1EncodingException("ASN.1 BOOLEAN: invalid encoding, length = " + encoding.length);
}
value = encoding[0] != 0;
} else {
throw new ASN1EncodingException
("ASN.1 BOOLEAN: bad BER: decoding constructed NOT IMPLEMENTED YET");
}
}
/**
* Returns a BER encoding of the BOOLEAN.
*
* @return The BER encoding of the BOOLEAN
* @throws ASN1Exception when the BOOLEAN is invalid
* and cannot be encoded.
*/
@Override
public BEREncoding berEncode()
throws ASN1Exception {
return berEncode(BEREncoding.UNIVERSAL_TAG, TAG);
}
/**
* Returns a BER encoding of the BOOLEAN. Implicitly tagged.
*
* @return The BER encoding of the BOOLEAN
* @throws ASN1Exception when the BOOLEAN is invalid
* and cannot be encoded.
*/
@Override
public BEREncoding berEncode(int tagType, int tag) throws ASN1Exception {
int[] encoding = new int[1];
if (value) {
encoding[0] = 0xff; // TRUE (in fact, any non-zero will do)
} else {
encoding[0] = 0x00; // FALSE
}
return new BERPrimitive(tagType, tag, encoding);
}
/**
* Method to set the boolean's value.
*
* @param newVal the value to set the BOOLEAN to.
* @return the boolean
*/
public ASN1Boolean set(boolean newVal) {
value = newVal;
return this;
}
/**
* Method to get the boolean's value.
*
* @return the BOOLEAN's current value.
*/
public boolean get() {
return value;
}
/**
* Returns a new String object representing this ASN.1 object's value.
*/
@Override
public String toString() {
return value ? "true" : "false";
}
}

View file

@ -0,0 +1,66 @@
package org.xbib.asn1;
/**
* ASN1Decoder.
* This is an ASN1 decoder which can handle all the standard ASN.1 types
* (those with UNIVERSAL tag types).
* It is used for decoding generic BER encodings into ASN.1 objects.
*/
public class ASN1Decoder {
private ASN1Decoder() {
// private constructor
}
public static ASN1Any toASN1(BEREncoding ber) throws ASN1Exception {
if (ber.tagTypeGet() == BEREncoding.UNIVERSAL_TAG) {
switch (ber.tagGet()) {
case ASN1Boolean.TAG:
return new ASN1Boolean(ber, true);
case ASN1Integer.INTEGER_TAG:
return new ASN1Integer(ber, true);
case ASN1BitString.BIT_STRING_TAG:
return new ASN1BitString(ber, true);
case ASN1OctetString.OCTET_STRING_TAG:
return new ASN1OctetString(ber, true);
case ASN1Null.NULL_TAG:
return new ASN1Null(ber, true);
case ASN1ObjectIdentifier.OBJECT_IDENTIFIER_TAG:
return new ASN1ObjectIdentifier(ber, true);
case ASN1ObjectDescriptor.OBJECT_DESCRIPTOR_TAG:
return new ASN1ObjectDescriptor(ber, true);
case ASN1External.EXTERNAL_TAG:
return new ASN1External(ber, true);
case ASN1Enumerated.ENUMERATED_TAG:
return new ASN1Enumerated(ber, true);
case ASN1Sequence.SEQUENCE_TAG:
return new ASN1Sequence(ber, true);
case ASN1Set.SET_TAG:
return new ASN1Set(ber, true);
case ASN1NumericString.NUMERIC_STRING_TAG:
return new ASN1NumericString(ber, true);
case ASN1PrintableString.PRINTABLE_STRING_TAG:
return new ASN1PrintableString(ber, true);
case ASN1T61String.T61_STRING_TAG:
return new ASN1T61String(ber, true);
case ASN1VideotexString.VIDEOTEX_STRING_TAG:
return new ASN1VideotexString(ber, true);
case ASN1IA5String.IA5_STRING_TAG:
return new ASN1IA5String(ber, true);
case ASN1UTCTime.UTC_TIME_TAG:
return new ASN1UTCTime(ber, true);
case ASN1GeneralizedTime.GENERALIZED_TIME_TAG:
return new ASN1GeneralizedTime(ber, true);
case ASN1GraphicString.GRAPHIC_STRING_TAG:
return new ASN1GraphicString(ber, true);
case ASN1VisibleString.TAG:
return new ASN1VisibleString(ber, true);
case ASN1GeneralString.GENERAL_STRING_TAG:
return new ASN1GeneralString(ber, true);
default:
break;
}
}
return new ASN1Any(ber, true);
}
}

View file

@ -0,0 +1,16 @@
package org.xbib.asn1;
/**
* ASN1EncodingException.
*/
public class ASN1EncodingException extends ASN1Exception {
private static final long serialVersionUID = 2063392457642967553L;
public ASN1EncodingException() {
super("ASN.1 encoding exception");
}
public ASN1EncodingException(String message) {
super(message);
}
}

View file

@ -0,0 +1,147 @@
package org.xbib.asn1;
/**
* Representation of an ASN.1 ENUMERATED.
* The <code>ENUMERATED</code> type denotes an integer from a selected set.
* ASN.1 ENUMERATED values can be positive, negative, or zero; and can
* have any magnitude.
* The current implementation limits the values of ENUMERATED to 32-bit
* two's complement values.
*/
public final class ASN1Enumerated extends ASN1Any {
/**
* This constant is the ASN.1 UNIVERSAL tag value for ENUMERATED.
*/
public static final int ENUMERATED_TAG = 0x0A;
/**
* The value of the ENUMERATED is stored in this variable.
* This is private for good information hiding, so that we are able
* to change its representation (e.g. to a long) at a later date
* without affecting the interface.
*/
private int value;
/**
* Constructor for an ASN.1 ENUMERATED object. The tag is
* set to the default tag of UNIVERSAL 2, and the value to the
* given number.
*
* @param number the value of the ENUMERATED.
*/
public ASN1Enumerated(int number) {
value = number;
}
/**
* Constructor for an ASN.1 ENUMERATED object from a BER encoding.
* @param ber the BER encoding
* @param checkTag the check tag
* @throws ASN1Exception if the BER encoding is incorrect.
*/
public ASN1Enumerated(BEREncoding ber, boolean checkTag)
throws ASN1Exception {
super(ber, checkTag);
}
/**
* Method for initializing the object from a BER encoding.
*
* @throws ASN1EncodingException if the BER encoding is incorrect.
*/
@Override
public void berDecode(BEREncoding berEncoding, boolean checkTag)
throws ASN1EncodingException {
if (checkTag && (berEncoding.tagGet() != ENUMERATED_TAG ||
berEncoding.tagTypeGet() != BEREncoding.UNIVERSAL_TAG)) {
throw new ASN1EncodingException("bad BER: tag=" + berEncoding.tagGet() +
" expected " + ENUMERATED_TAG + "\n");
}
if (!(berEncoding instanceof BERPrimitive)) {
throw new ASN1EncodingException("bad form, constructed");
}
BERPrimitive ber = (BERPrimitive) berEncoding;
int[] encoding = ber.peek();
if (encoding.length < 1) {
throw new ASN1EncodingException("invalid encoding, length = " + encoding.length);
}
value = (byte) encoding[0]; // to ensure sign extension
for (int x = 1; x < encoding.length; x++) {
value <<= 8;
value |= (encoding[x] & 0xff);
}
}
/**
* Returns a BER encoding of the ENUMERATED.
*
* @return The BER encoding of the ENUMERATED
* @throws ASN1Exception when the ENUMERATED is invalid
* and cannot be encoded.
*/
@Override
public BEREncoding berEncode() throws ASN1Exception {
return berEncode(BEREncoding.UNIVERSAL_TAG, ENUMERATED_TAG);
}
/**
* Returns a BER encoding of the ENUMERATED. Explicitly tagged with
* the supplied tag.
*
* @return The BER encoding of the ENUMERATED
* @throws ASN1Exception when the ENUMERATED is invalid
* and cannot be encoded.
*/
@Override
public BEREncoding berEncode(int tagType, int tag) throws ASN1Exception {
int length = 0;
int shifted = value;
if (value < 0) {
shifted = ~value;
}
boolean needPad;
do {
needPad = (shifted & 0x80) == 0x80;
shifted >>= 8;
length++;
} while (shifted != 0);
if (needPad) {
length++;
}
int[] encoding = new int[length];
int index = 0;
while (0 < length) {
encoding[index++] = (value >> (8 * (length - 1))) & 0xff;
length--;
}
return new BERPrimitive(tagType, tag, encoding);
}
/**
* Method to set the integer's value.
*
* @param newVal the value to set the ENUMERATED to.
* @return the object.
*/
public ASN1Enumerated set(int newVal) {
value = newVal;
return this;
}
/**
* Method to get the integer's value.
*
* @return the ENUMERATED's current value.
*/
public int get() {
return value;
}
/**
* Returns a new String object representing this ASN.1 object's value.
*/
@Override
public String toString() {
return String.valueOf(value);
}
}

View file

@ -0,0 +1,18 @@
package org.xbib.asn1;
import java.io.IOException;
/**
* ASN1Exception.
*/
public class ASN1Exception extends IOException {
private static final long serialVersionUID = -339688617660261367L;
public ASN1Exception() {
super("ASN.1 exception");
}
public ASN1Exception(String message) {
super(message);
}
}

View file

@ -0,0 +1,282 @@
package org.xbib.asn1;
/**
* ASN1 EXTERNAL
* The <code>EXTERNAL</code> type represents an external object.
* According to clause 34.4 of the ASN.1 standard,
* the EXTERNAL type can be defined as:
* <pre>
* EXTERNAL := [UNIVERSAL 8] IMPLICIT SEQUENCE {
* direct_reference OBJECT IDENTIFIER OPTIONAL,
* indirect_reference INTEGER OPTIONAL,
* data_value_descriptor ObjectDescriptor OPTIONAL,
* encoding CHOICE {
* single_ASN1_type [0] ANY,
* octet_Aligned [1] IMPLICIT OCTET STRING,
* arbitrary [2] IMPLICIT BIT STRING
* }
* }
* </pre>
* This construct has been represented by a class with six
* variables:
* sDirectReference,
* sIndirectReference,
* sDataValueDescriptor,
* cSingleASN1Type,
* cOctetAligned,
* cArbitrary.
* The first three should be set to point to the appropriate object
* if present, or null if not.
* One of the last three variables should be set to non-null (the choice)
* and the rest to null.
*/
public final class ASN1External extends ASN1Any {
/**
* This constant is the ASN.1 UNIVERSAL tag value for an EXTERNAL.
*/
public static final int EXTERNAL_TAG = 0x08;
/*
* The values are stored in these variables.
*/
private ASN1ObjectIdentifier sDirectReference;
private ASN1Integer sIndirectReference;
private ASN1ObjectDescriptor sDataValueDescriptor;
private ASN1Any cSingleASN1Type;
private ASN1OctetString cOctetAligned;
private ASN1BitString cArbitrary;
/**
* Constructor for an ASN.1 EXTERNAL object. It sets the tag to the
* default value of UNIVERSAL 8.
*/
public ASN1External() {
//
}
/**
* Constructor for an ASN.1 EXTERNAL object from a BER encoding.
*
* @param ber The BER encoding to use.
* @param checkTag If true, it checks the tag. Use false if is implicitly tagged.
* @throws ASN1Exception If the BER encoding is incorrect.
*/
public ASN1External(BEREncoding ber, boolean checkTag)
throws ASN1Exception {
super(ber, checkTag);
}
/**
* Method for initializing the object from a BER encoding.
*
* @param berEncoding The BER encoding to use.
* @param checkTag If true, it checks the tag. Use false if is implicitly tagged.
* @throws ASN1Exception If the BER encoding is incorrect.
*/
@Override
public void berDecode(BEREncoding berEncoding, boolean checkTag)
throws ASN1Exception {
if (checkTag && (berEncoding.tagGet() != EXTERNAL_TAG || berEncoding.tagTypeGet() != BEREncoding.UNIVERSAL_TAG)) {
throw new ASN1EncodingException("bad BER: tag=" + berEncoding.tagGet() +
" expected " + EXTERNAL_TAG + "\n");
}
if (berEncoding instanceof BERPrimitive) {
throw new ASN1EncodingException("incorrect form, primitive encoding");
}
BERConstructed ber = (BERConstructed) berEncoding;
sDirectReference = null;
sIndirectReference = null;
sDataValueDescriptor = null;
cSingleASN1Type = null;
cOctetAligned = null;
cArbitrary = null;
int numParts = ber.numberComponents();
if (numParts < 1) {
throw new ASN1EncodingException("incomplete");
}
int part = 0;
BEREncoding p = ber.elementAt(part);
if (p.tagGet() == ASN1ObjectIdentifier.OBJECT_IDENTIFIER_TAG &&
p.tagTypeGet() == BEREncoding.UNIVERSAL_TAG) {
sDirectReference = new ASN1ObjectIdentifier(p, true);
if (numParts <= ++part) {
throw new ASN1EncodingException("incomplete");
}
p = ber.elementAt(part);
}
if (p.tagGet() == ASN1Integer.INTEGER_TAG &&
p.tagTypeGet() == BEREncoding.UNIVERSAL_TAG) {
sIndirectReference = new ASN1Integer(p, true);
if (numParts <= ++part) {
throw new ASN1EncodingException("incomplete");
}
p = ber.elementAt(part);
}
if (p.tagGet() == ASN1ObjectDescriptor.OBJECT_DESCRIPTOR_TAG &&
p.tagTypeGet() == BEREncoding.UNIVERSAL_TAG) {
sDataValueDescriptor = new ASN1ObjectDescriptor(p, true);
if (numParts <= ++part) {
throw new ASN1EncodingException("incomplete");
}
p = ber.elementAt(part);
}
switch (p.tagGet()) {
case 0:
if (!(p instanceof BERConstructed)) {
throw new ASN1EncodingException("singleASN1type: bad form, primitive");
}
if (((BERConstructed) p).numberComponents() != 1) {
throw new ASN1EncodingException("singleASN1type: bad form, no explicit tag");
}
cSingleASN1Type = ASN1Decoder.toASN1(((BERConstructed) p).elementAt(0));
break;
case 1:
// octetAligned [1] IMPLICIT OCTET STRING
if (p.tagTypeGet() != BEREncoding.CONTEXT_SPECIFIC_TAG) {
throw new ASN1EncodingException("encoding: bad tag type " + p);
}
cOctetAligned = new ASN1OctetString(p, false);
break;
case 2:
// arbitrary [2] IMPLICIT BIT STRING
if (p.tagTypeGet() != BEREncoding.CONTEXT_SPECIFIC_TAG) {
throw new ASN1EncodingException("encoding: bad tag type " + p);
}
cArbitrary = new ASN1BitString(p, false);
break;
default:
throw new ASN1EncodingException("encoding: tag = " + p.tagGet());
}
if (part != (numParts - 1)) {
throw new ASN1Exception("extra element(s)");
}
}
public ASN1OctetString getcOctetAligned() {
return cOctetAligned;
}
public ASN1Any getSingleASN1Type() {
return cSingleASN1Type;
}
/**
* Returns a BER encoding of the EXTERNAL.
*
* @return The BER encoding of the EXTERNAL
* @throws ASN1Exception when the EXTERNAL is invalid
* and cannot be encoded.
*/
@Override
public BEREncoding berEncode() throws ASN1Exception {
return berEncode(BEREncoding.UNIVERSAL_TAG, EXTERNAL_TAG);