private static void decodeValidityPeriod(ValidityPeriod validityPeriod, TlvDecoder decoder) { int endOffset = decoder .readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.ValidityPeriod_ValidityPeriod); validityPeriod.clear(); // Set copy false since we just immediately get the string. Blob isoString = new Blob( decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.ValidityPeriod_NotBefore), false); double notBefore = net.named_data.jndn.encrypt.Schedule.fromIsoString("" + isoString); isoString = new Blob(decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.ValidityPeriod_NotAfter), false); double notAfter = net.named_data.jndn.encrypt.Schedule.fromIsoString("" + isoString); validityPeriod.setPeriod(notBefore, notAfter); decoder.finishNestedTlvs(endOffset); }
/// <summary> /// Decode the input as an NDN-TLV RepetitiveInterval. /// </summary> /// /// <param name="decoder">The decoder with the input to decode.</param> /// <returns>A new RepetitiveInterval with the decoded result.</returns> private static RepetitiveInterval decodeRepetitiveInterval( TlvDecoder decoder) { int endOffset = decoder .readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_RepetitiveInterval); // Use Blob to convert UTF8 to a string. double startDate = fromIsoString(new Blob( decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_StartDate), true).toString()); double endDate = fromIsoString(new Blob( decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EndDate), true).toString()); int startHour = (int) decoder .readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_IntervalStartHour); int endHour = (int) decoder .readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_IntervalEndHour); int nRepeats = (int) decoder .readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_NRepeats); int repeatUnitCode = (int) decoder .readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_RepeatUnit); RepetitiveInterval.RepeatUnit repeatUnit; if (repeatUnitCode == net.named_data.jndn.encoding.tlv.Tlv.Encrypt_RepeatUnit_NONE) repeatUnit = net.named_data.jndn.encrypt.RepetitiveInterval.RepeatUnit.NONE; else if (repeatUnitCode == net.named_data.jndn.encoding.tlv.Tlv.Encrypt_RepeatUnit_DAY) repeatUnit = net.named_data.jndn.encrypt.RepetitiveInterval.RepeatUnit.DAY; else if (repeatUnitCode == net.named_data.jndn.encoding.tlv.Tlv.Encrypt_RepeatUnit_MONTH) repeatUnit = net.named_data.jndn.encrypt.RepetitiveInterval.RepeatUnit.MONTH; else if (repeatUnitCode == net.named_data.jndn.encoding.tlv.Tlv.Encrypt_RepeatUnit_YEAR) repeatUnit = net.named_data.jndn.encrypt.RepetitiveInterval.RepeatUnit.YEAR; else throw new EncodingException( "Unrecognized RepetitiveInterval RepeatUnit code: " + repeatUnitCode); decoder.finishNestedTlvs(endOffset); return new RepetitiveInterval(startDate, endDate, startHour, endHour, nRepeats, repeatUnit); }
/// <summary> /// Decode input as a control parameters in NDN-TLV and set the fields of the /// controlResponse object. /// </summary> /// /// <param name="controlResponse"></param> /// <param name="input"></param> /// <param name="copy">unchanged while the Blob values are used.</param> /// <exception cref="EncodingException">For invalid encoding</exception> public override void decodeControlResponse(ControlResponse controlResponse, ByteBuffer input, bool copy) { TlvDecoder decoder = new TlvDecoder(input); int endOffset = decoder .readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_ControlResponse); controlResponse.setStatusCode((int) decoder .readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_StatusCode)); // Set copy false since we just immediately get a string. Blob statusText = new Blob( decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.NfdCommand_StatusText), false); controlResponse.setStatusText(statusText.toString()); // Decode the body. if (decoder .peekType(net.named_data.jndn.encoding.tlv.Tlv.ControlParameters_ControlParameters, endOffset)) { controlResponse.setBodyAsControlParameters(new ControlParameters()); // Decode into the existing ControlParameters to avoid copying. decodeControlParameters( controlResponse.getBodyAsControlParameters(), decoder, copy); } else controlResponse.setBodyAsControlParameters(null); decoder.finishNestedTlvs(endOffset); }
/// <summary> /// Decode the name component as NDN-TLV and return the component. This handles /// different component types such as ImplicitSha256DigestComponent. /// </summary> /// /// <param name="decoder">The decoder with the input to decode.</param> /// <param name="copy">unchanged while the Blob values are used.</param> /// <returns>A new Name.Component.</returns> /// <exception cref="EncodingException"></exception> private static Name.Component decodeNameComponent(TlvDecoder decoder, bool copy) { int savePosition = decoder.getOffset(); int type = decoder.readVarNumber(); // Restore the position. decoder.seek(savePosition); Blob value_ren = new Blob(decoder.readBlobTlv(type), copy); if (type == net.named_data.jndn.encoding.tlv.Tlv.ImplicitSha256DigestComponent) return net.named_data.jndn.Name.Component.fromImplicitSha256Digest(value_ren); else return new Name.Component(value_ren); }
private static void decodeKeyLocator(int expectedType, KeyLocator keyLocator, TlvDecoder decoder, bool copy) { int endOffset = decoder.readNestedTlvsStart(expectedType); keyLocator.clear(); if (decoder.getOffset() == endOffset) // The KeyLocator is omitted, so leave the fields as none. return; if (decoder.peekType(net.named_data.jndn.encoding.tlv.Tlv.Name, endOffset)) { // KeyLocator is a Name. keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME); decodeName(keyLocator.getKeyName(), new int[1], new int[1], decoder, copy); } else if (decoder.peekType(net.named_data.jndn.encoding.tlv.Tlv.KeyLocatorDigest, endOffset)) { // KeyLocator is a KeyLocatorDigest. keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEY_LOCATOR_DIGEST); keyLocator.setKeyData(new Blob(decoder .readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.KeyLocatorDigest), copy)); } else throw new EncodingException( "decodeKeyLocator: Unrecognized key locator type"); decoder.finishNestedTlvs(endOffset); }
/// <summary> /// Decode signatureInfo as an NDN-TLV signature info and signatureValue as the /// related NDN-TLV SignatureValue, and return a new object which is a subclass /// of Signature. /// </summary> /// /// <param name="signatureInfo"></param> /// <param name="signatureValue"></param> /// <param name="copy">unchanged while the Blob values are used.</param> /// <returns>A new object which is a subclass of Signature.</returns> /// <exception cref="EncodingException">For invalid encoding.</exception> public override Signature decodeSignatureInfoAndValue(ByteBuffer signatureInfo, ByteBuffer signatureValue, bool copy) { // Use a SignatureHolder to imitate a Data object for _decodeSignatureInfo. Tlv0_2WireFormat.SimpleSignatureHolder signatureHolder = new Tlv0_2WireFormat.SimpleSignatureHolder (); TlvDecoder decoder = new TlvDecoder(signatureInfo); decodeSignatureInfo(signatureHolder, decoder, copy); decoder = new TlvDecoder(signatureValue); signatureHolder.getSignature().setSignature( new Blob(decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureValue), copy)); return signatureHolder.getSignature(); }
/// <summary> /// Decode input as an interest in NDN-TLV and set the fields of the interest /// object. /// </summary> /// /// <param name="interest">The Interest object whose fields are updated.</param> /// <param name="input"></param> /// <param name="signedPortionBeginOffset">name component and ends just before the final name component (which is assumed to be a signature for a signed interest).</param> /// <param name="signedPortionEndOffset">name component and ends just before the final name component (which is assumed to be a signature for a signed interest).</param> /// <param name="copy">unchanged while the Blob values are used.</param> /// <exception cref="EncodingException">For invalid encoding.</exception> public override void decodeInterest(Interest interest, ByteBuffer input, int[] signedPortionBeginOffset, int[] signedPortionEndOffset, bool copy) { TlvDecoder decoder = new TlvDecoder(input); int endOffset = decoder.readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.Interest); decodeName(interest.getName(), signedPortionBeginOffset, signedPortionEndOffset, decoder, copy); if (decoder.peekType(net.named_data.jndn.encoding.tlv.Tlv.Selectors, endOffset)) decodeSelectors(interest, decoder, copy); // Require a Nonce, but don't force it to be 4 bytes. ByteBuffer nonce = decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Nonce); interest.setInterestLifetimeMilliseconds(decoder .readOptionalNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.InterestLifetime, endOffset)); if (decoder.peekType(net.named_data.jndn.encoding.tlv.Tlv.Data, endOffset)) { // Get the bytes of the Link TLV. int linkBeginOffset = decoder.getOffset(); int linkEndOffset = decoder.readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.Data); decoder.seek(linkEndOffset); interest.setLinkWireEncoding( new Blob(decoder.getSlice(linkBeginOffset, linkEndOffset), copy), this); } else interest.unsetLink(); interest.setSelectedDelegationIndex((int) decoder .readOptionalNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.SelectedDelegation, endOffset)); if (interest.getSelectedDelegationIndex() >= 0 && !interest.hasLink()) throw new EncodingException( "Interest has a selected delegation, but no link object"); // Set the nonce last because setting other interest fields clears it. interest.setNonce(new Blob(nonce, copy)); decoder.finishNestedTlvs(endOffset); }
/// <summary> /// Decode input as a EncryptedContent in NDN-TLV and set the fields of the /// encryptedContent object. /// </summary> /// /// <param name="encryptedContent"></param> /// <param name="input"></param> /// <param name="copy">unchanged while the Blob values are used.</param> /// <exception cref="EncodingException">For invalid encoding</exception> public override void decodeEncryptedContent(EncryptedContent encryptedContent, ByteBuffer input, bool copy) { TlvDecoder decoder = new TlvDecoder(input); int endOffset = decoder .readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptedContent); Tlv0_2WireFormat.decodeKeyLocator(net.named_data.jndn.encoding.tlv.Tlv.KeyLocator, encryptedContent.getKeyLocator(), decoder, copy); int algorithmType = (int) decoder .readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptionAlgorithm); if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesEcb.getNumericType()) encryptedContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesEcb); else if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc.getNumericType()) encryptedContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc); else if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaPkcs.getNumericType()) encryptedContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaPkcs); else if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep.getNumericType()) encryptedContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep); else throw new EncodingException( "Unrecognized EncryptionAlgorithm code " + algorithmType); encryptedContent.setInitialVector(new Blob(decoder.readOptionalBlobTlv( net.named_data.jndn.encoding.tlv.Tlv.Encrypt_InitialVector, endOffset), copy)); encryptedContent.setPayload(new Blob(decoder .readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptedPayload), copy)); decoder.finishNestedTlvs(endOffset); }
/// <summary> /// Decode input as a data packet in NDN-TLV and set the fields in the data /// object. /// </summary> /// /// <param name="data">The Data object whose fields are updated.</param> /// <param name="input"></param> /// <param name="signedPortionBeginOffset">If you are not decoding in order to verify, you can call decodeData(data, input) to ignore this returned value.</param> /// <param name="signedPortionEndOffset">not decoding in order to verify, you can call decodeData(data, input) to ignore this returned value.</param> /// <param name="copy">unchanged while the Blob values are used.</param> /// <exception cref="EncodingException">For invalid encoding.</exception> public override void decodeData(Data data, ByteBuffer input, int[] signedPortionBeginOffset, int[] signedPortionEndOffset, bool copy) { TlvDecoder decoder = new TlvDecoder(input); int endOffset = decoder.readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.Data); signedPortionBeginOffset[0] = decoder.getOffset(); decodeName(data.getName(), new int[1], new int[1], decoder, copy); decodeMetaInfo(data.getMetaInfo(), decoder, copy); data.setContent(new Blob(decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Content), copy)); decodeSignatureInfo(data, decoder, copy); signedPortionEndOffset[0] = decoder.getOffset(); data.getSignature().setSignature( new Blob(decoder.readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.SignatureValue), copy)); decoder.finishNestedTlvs(endOffset); }