<commit_before># Copyright 2016 <NAME>
#
# 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.
#
from laikaboss.si_module import SI_MODULE
from laikaboss.objectmodel import ModuleObject, ExternalVars
from laikaboss import config
import tempfile
import os
from oletools import rtfobj

class EXPLODE_RTF(SI_MODULE):
	def __init__(self,):
		self.module_name = "EXPLODE_RTF"
		self.TEMP_DIR = '/tmp/laikaboss_tmp'
		if hasattr(config, 'tempdir'):
			self.TEMP_DIR = config.tempdir.rstrip('/')
		if not os.path.isdir(self.TEMP_DIR):
			os.mkdir(self.TEMP_DIR)
			os.chmod(self.TEMP_DIR, 0777)

	def _run(self, scanObject, result, depth, args):
		moduleResult = []

		with tempfile.NamedTemporaryFile(dir=self.TEMP_DIR) as temp_file:
			temp_file_name = temp_file.name
			temp_file.write(scanObject.buffer)
			temp_file.flush()

			# rtfobj v 0.02 has two objects in the rtf iter
			if rtfobj.__version__ == str(0.02):
				for index, obj_data in rtfobj.rtf_iter_objects(temp_file_name):
					name = 'index_' + str(index)
					try:
						moduleResult.append(ModuleObject(buffer=obj_data, externalVars=ExternalVars(filename='e_rtf_%s' % name)))
					except:
						scanObject.addFlag('explode_rtf:err:explode_%s_failed' % name)
						pass

			# rtfobj v 0.03 has three objects in the rtf iter
			elif rtfobj.__version__ == str(0.03):
				for index, obj_len, obj_data in rtfobj.rtf_iter_objects(temp_file_name):
					name = 'index_' + str(index)
					try:
						moduleResult.append(ModuleObject(buffer=obj_data, externalVars=ExternalVars(filename='e_rtf_%s' % name)))
					except:
						scanObject.addFlag('explode_rtf:err:explode_%s_failed' % name)
						pass

		return moduleResult<commit_msg>Refactored explode_rtf<commit_after># Copyright 2016 <NAME>
#
# 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.
#
from laikaboss.si_module import SI_MODULE
from laikaboss.objectmodel import ModuleObject, ExternalVars
from oletools import rtfobj

class EXPLODE_RTF(SI_MODULE):
    def __init__(self,):
        self.module_name = "EXPLODE_RTF"

    def _run(self, scanObject, result, depth, args):
        moduleResult = []

        for index, obj_len, obj_data in rtfobj.rtf_iter_objects(scanObject.buffer):
            # index location of the RTF object becomes the file name
            name = 'index_' + str(index)
            moduleResult.append(ModuleObject(buffer=obj_data, externalVars=ExternalVars(filename='e_rtf_%s' % name)))

        return moduleResult
{'fraction_non_alphanumeric': 0.08945221445221445, 'fraction_numerical': 0.009324009324009324, 'mean_word_length': 4.201515151515151, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '3680946', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1078, 'n_words': 330}