Skip to content

Commit

Permalink
Merge pull request #1 from CBonnell/remove-box
Browse files Browse the repository at this point in the history
Remove unneeded Box allocation for decoders
  • Loading branch information
CBonnell authored Aug 8, 2024
2 parents 636698f + 108b8e0 commit 7a07995
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

## v0.1.1 - 2024-08-07

### Fixes

- Remove unneeded Box allocation for decoders

## v0.1.0 - 2024-01-02

- Initial release
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "pyasn1_fasder"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.20.0", features = ["num-bigint"] }
pyo3 = { version = "0.20.3", features = ["num-bigint"] }
der = { version = "0.7.8", features = ["oid"] }
num-bigint = "0.4.4"
itertools = "0.12.0"
num-bigint = "0.4.6"
itertools = "0.13.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ assert str(decoded) == 'ABC'
* There is no encoding counterpart.
* Trailing octets present after the `substrate` TLV are not tolerated and will result in an exception being raised. In other words, the `rest` component of the tuple return value will always be an empty `bytes` object.
* Schemaless decoding is not supported. In other words, a non-`None` `asn1Spec` must be passed to `decode_der`.
* `Set`s with `namedValues` are not supported. These are (almost?) never used in cryptography standards, but support can be added if there are valid use cases.
* `Set`s with `namedTypes` are not supported. These are (almost?) never used in cryptography standards, but support can be added if there are valid use cases.
* `openTypes` decoding is currently not supported. This can be added if there is interest.
* The pedantic checks for correctness of encoding cannot be disabled.

Expand Down
56 changes: 29 additions & 27 deletions src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use der::{Encode, Header, Reader};
use pyo3::{PyAny, PyErr, PyResult};
use pyo3::prelude::PyModule;
use pyo3::PyAny;
use pyo3::PyErr;
use pyo3::PyResult;
use pyo3::prelude::{PyModule};
use pyo3::types::PyDict;
use crate::{HELPER_MODULE_ATTR, NativeHelperModule, Pyasn1FasderError, TYPE_MAP};
use crate::asn1_type::{AnyDecoder, BitStringDecoder, BooleanDecoder, CharacterStringDecoder, ChoiceDecoder, Decoder, IntegerDecoder, NullDecoder, ObjectIdentifierDecoder, OctetStringDecoder, PrintableStringDecoder, SequenceDecoder, SequenceOfDecoder, SetOfDecoder};
Expand Down Expand Up @@ -129,31 +131,31 @@ pub fn decode_asn1_spec_value(step: DecodeStep) -> PyResult<&PyAny> {
Some(decoder_id) => {
let decoder_id_u8: usize = decoder_id.extract().unwrap();

let decoder: Box<dyn Decoder> = match decoder_id_u8 {
DECODER_TYPE_BOOLEAN => Box::new(BooleanDecoder::new(step)),
DECODER_TYPE_INTEGER => Box::new(IntegerDecoder::new(step, "INTEGER")),
DECODER_TYPE_BITSTRING => Box::new(BitStringDecoder::new(step)),
DECODER_TYPE_OCTETSTRING => Box::new(OctetStringDecoder::new(step)),
DECODER_TYPE_NULL => Box::new(NullDecoder::new(step)),
DECODER_TYPE_OBJECTIDENTIFIER => Box::new(ObjectIdentifierDecoder::new(step)),
DECODER_TYPE_ENUMERATED => Box::new(IntegerDecoder::new(step, "ENUMERATED")),
DECODER_TYPE_UTF8STRING => Box::new(CharacterStringDecoder::new(step, "UTF8STRING")),
DECODER_TYPE_SEQUENCE => Box::new(SequenceDecoder::new(step)),
DECODER_TYPE_SEQUENCEOF => Box::new(SequenceOfDecoder::new(step)),
DECODER_TYPE_SETOF => Box::new(SetOfDecoder::new(step)),
DECODER_TYPE_NUMERICSTRING => Box::new(CharacterStringDecoder::new(step, "NUMERICSTRING")),
DECODER_TYPE_PRINTABLESTRING => Box::new(PrintableStringDecoder::new(step)),
DECODER_TYPE_TELETEXSTRING => Box::new(CharacterStringDecoder::new(step, "TELETEXSTRING")),
DECODER_TYPE_VIDEOTEXSTRING => Box::new(CharacterStringDecoder::new(step, "VIDEOTEXSTRING")),
DECODER_TYPE_IA5STRING => Box::new(CharacterStringDecoder::new(step, "IA5STRING")),
DECODER_TYPE_UTCTIME => Box::new(CharacterStringDecoder::new(step, "UTCTIME")),
DECODER_TYPE_GENERALIZEDTIME => Box::new(CharacterStringDecoder::new(step, "GENERALIZEDTIME")),
DECODER_TYPE_GRAPHICSTRING => Box::new(CharacterStringDecoder::new(step, "GRAPHICSTRING")),
DECODER_TYPE_VISIBLESTRING => Box::new(CharacterStringDecoder::new(step, "VISIBLESTRING")),
DECODER_TYPE_UNIVERSALSTRING => Box::new(CharacterStringDecoder::new(step, "UNIVERSALSTRING")),
DECODER_TYPE_BMPSTRING => Box::new(CharacterStringDecoder::new(step, "BMPSTRING")),
DECODER_TYPE_ANY => Box::new(AnyDecoder::new(step)),
DECODER_TYPE_CHOICE => Box::new(ChoiceDecoder::new(step)),
let decoder: &dyn Decoder = match decoder_id_u8 {
DECODER_TYPE_BOOLEAN => &BooleanDecoder::new(step),
DECODER_TYPE_INTEGER => &IntegerDecoder::new(step, "INTEGER"),
DECODER_TYPE_BITSTRING => &BitStringDecoder::new(step),
DECODER_TYPE_OCTETSTRING => &OctetStringDecoder::new(step),
DECODER_TYPE_NULL => &NullDecoder::new(step),
DECODER_TYPE_OBJECTIDENTIFIER => &ObjectIdentifierDecoder::new(step),
DECODER_TYPE_ENUMERATED => &IntegerDecoder::new(step, "ENUMERATED"),
DECODER_TYPE_UTF8STRING => &CharacterStringDecoder::new(step, "UTF8STRING"),
DECODER_TYPE_SEQUENCE => &SequenceDecoder::new(step),
DECODER_TYPE_SEQUENCEOF => &SequenceOfDecoder::new(step),
DECODER_TYPE_SETOF => &SetOfDecoder::new(step),
DECODER_TYPE_NUMERICSTRING => &CharacterStringDecoder::new(step, "NUMERICSTRING"),
DECODER_TYPE_PRINTABLESTRING => &PrintableStringDecoder::new(step),
DECODER_TYPE_TELETEXSTRING => &CharacterStringDecoder::new(step, "TELETEXSTRING"),
DECODER_TYPE_VIDEOTEXSTRING => &CharacterStringDecoder::new(step, "VIDEOTEXSTRING"),
DECODER_TYPE_IA5STRING => &CharacterStringDecoder::new(step, "IA5STRING"),
DECODER_TYPE_UTCTIME => &CharacterStringDecoder::new(step, "UTCTIME"),
DECODER_TYPE_GENERALIZEDTIME => &CharacterStringDecoder::new(step, "GENERALIZEDTIME"),
DECODER_TYPE_GRAPHICSTRING => &CharacterStringDecoder::new(step, "GRAPHICSTRING"),
DECODER_TYPE_VISIBLESTRING => &CharacterStringDecoder::new(step, "VISIBLESTRING"),
DECODER_TYPE_UNIVERSALSTRING => &CharacterStringDecoder::new(step, "UNIVERSALSTRING"),
DECODER_TYPE_BMPSTRING => &CharacterStringDecoder::new(step, "BMPSTRING"),
DECODER_TYPE_ANY => &AnyDecoder::new(step),
DECODER_TYPE_CHOICE => &ChoiceDecoder::new(step),
_ => return Err(Pyasn1FasderError::new_err("ASN.1 type is unsuppported"))
};

Expand Down

0 comments on commit 7a07995

Please sign in to comment.