<commit_before>//
//  PKCS7Container.swift
//  Kvitto
//
//  Created by <NAME> on 06/10/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation
import DTFoundation

/**
 A simplified handler for a PKCS#7 Container. Only retrieves the unencrypted payloadData.
*/
@objc(DTPKCS7Container) @objcMembers open class PKCS7Container: NSObject, DTASN1ParserDelegate
{
    /**
     The data of the payload
    */
    fileprivate(set) open var payloadData: Data!
    
    /**
     Internal Variable for parsing
    */
    var _objectIdentifier: String?
    
    /**
     The designated initializer
    */
    public init?(data: Data)
    {
        super.init()
        
        guard let rootSequence = DTASN1Serialization.object(with: data) as? [AnyObject],
                    let unwrappedData = unwrapRootSequence(rootSequence)
        else {
			return nil
		}

        payloadData = unwrappedData
    }
    
    func unwrapRootSequence(_ sequence: [AnyObject]) -> Data?
    {
		guard sequence.count==2,
			  let OID = sequence[0] as? String,
			  OID == "1.2.840.113549.1.7.2" else
		{
			return nil
		}
		
        if let containedSequence = sequence[1] as? [AnyObject],
		   containedSequence.count == 1,
              let actualSequence = containedSequence[0] as? [AnyObject],
			  actualSequence.count > 2,
              let dataSequence = actualSequence[2] as? [AnyObject]
        {
			return unwrapSignedDataSequence(dataSequence)
        }
		else if let context = sequence[1] as? [Int: AnyObject],
				let contained = context[0] as? [AnyObject],
				let containedSequence = contained.last as? [AnyObject],
				let actualSequence = containedSequence.last as? [AnyObject],
				let dataSequence = actualSequence[2] as? [AnyObject]
		{
			return unwrapSignedDataSequence(dataSequence)
		}
		
		return nil
    }
    
    func unwrapSignedDataSequence(_ sequence: [AnyObject]) -> Data?
    {
        guard sequence.count==2,
            let OID = sequence[0] as? String,
			OID == "1.2.840.113549.1.7.1"
            else
        {
            return nil
        }
		<commit_msg>Refactor to avoid duplication<commit_after>//
//  PKCS7Container.swift
//  Kvitto
//
//  Created by <NAME> on 06/10/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation
import DTFoundation

/**
 A simplified handler for a PKCS#7 Container. Only retrieves the unencrypted payloadData.
*/
@objc(DTPKCS7Container) @objcMembers open class PKCS7Container: NSObject, DTASN1ParserDelegate
{
    /**
     The data of the payload
    */
    fileprivate(set) open var payloadData: Data!
    
    /**
     Internal Variable for parsing
    */
    var _objectIdentifier: String?
    
    /**
     The designated initializer
    */
    public init?(data: Data)
    {
        super.init()
        
        guard let rootSequence = DTASN1Serialization.object(with: data) as? [AnyObject],
                    let unwrappedData = unwrapRootSequence(rootSequence)
        else {
			return nil
		}

        payloadData = unwrappedData
    }
    
    func unwrapRootSequence(_ sequence: [AnyObject]) -> Data?
    {
		guard sequence.count==2,
			  let OID = sequence[0] as? String,
			  OID == "1.2.840.113549.1.7.2" else
		{
			return nil
		}
		
		let containedSequence: [AnyObject]
		
        if let sequence = sequence[1] as? [AnyObject],
		   sequence.count == 1
        {
			containedSequence = sequence
        }
		else if let context = sequence[1] as? [Int: AnyObject],
				let contained = context[0] as? [AnyObject],
				let sequence = contained[0] as? [AnyObject]
		{
			containedSequence = sequence
		}
		else
		{
			return nil
		}
		
		guard let actualSequence = containedSequence[0] as? [AnyObject],
			actualSequence.count > 2,
			let dataSequence = actualSequence[2] as? [AnyObject] else
		{
			return nil
		}
		
		return unwrapSignedDataSequence(dataSequence)
    }
    
    func unwrapSignedDataSequence(_ sequence: [AnyObject]) -> Data?
    {
        guard sequence.count==2,
            let OID = sequence[0] as? String,
			OID == "1.2.840.113549.1.7.1"
            else
        {
            return nil
        }
		
{'fraction_non_alphanumeric': 0.10368217054263566, 'fraction_numerical': 0.027131782945736434, 'mean_word_length': 2.3325262308313155, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5914797', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1261, 'n_words': 333}