<commit_before># Copyright (C) 2015-2019  <NAME> <<EMAIL>>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

'''
Class to support ISO9660 Path Table Records.
'''

from __future__ import absolute_import

import struct

from pycdlib import pycdlibexception
from pycdlib import utils


class PathTableRecord(object):
    '''
    A class that represents a single ISO9660 Path Table Record.
    '''
    __slots__ = ('_initialized', 'len_di', 'xattr_length', 'extent_location',
                 'parent_directory_num', 'directory_identifier', 'dirrecord')

    FMT = '=BBLH'

    def __init__(self):
        self._initialized = False

    def parse(self, data):
        '''
        A method to parse an ISO9660 Path Table Record out of a string.

        Parameters:
         data - The string to parse.
        Returns:
         Nothing.
        '''
        (self.len_di, self.xattr_length, self.extent_location,
         self.parent_directory_num) = struct.unpack_from(self.FMT, data[:8], 0)

        if self.len_di % 2 != 0:
            self.directory_identifier = data[8:-1]
        else:
            self.directory_identifier = data[8:]
        self.dirrecord = None
        self._initialized = True

    def _record(self, ext_loc, parent_dir_num):
        '''
        An internal method to generate a string representing this Path Table Record.

        Parameters:
         ext_loc - The extent location to place in this Path Table Record.
         parent_dir_num - The parent directory number to place in this Path Table
                          Record.
        Returns:
         A string representing this Path Table Record.
        '''
        return struct.pack(self.FMT, self.len_di, self.xattr_length,
                           ext_loc, parent_dir_num) + self.directory_identifier + b'\x00' * (self.len_di % 2)

    def record_little_endian(self):
        '''
        A method to generate a string representing the little endian version of
        this Path Table Record.

        Parameters:
         None.
        Returns:
         A string representing the little endian version of this Path Table Record.
        '''
        if not self._initialized:
            raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized')

        return self._record(self.extent_location, self.parent_directory_num)

    def record_big_endian(self):
        '''
        A method to generate a string representing the big endian version of
        this Path Table Record.

        Parameters:
         None.
        Returns:
         A string representing the big endian version of this Path Table Record.
        '''
        if not self._initialized:
            raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized')

        return self._record(utils.swab_32bit(self.extent_location),
                            utils.swab_16bit(self.parent_directory_num))

    @classmethod
    def record_length(cls, len_di):
        '''
        A class method to calculate the length of a Path Table Record.

        Parameters:
         len_di - The length of the name for this Path Directory Record.
        Returns:
         The total length that a Path Directory Record with this name would occupy.
        '''
        return struct.calcsize(cls.FMT) + len_di + (len_di % 2)

    def _new(self, name, parent_dir_num):
        '''
        An internal method to create a new Path Table Record.

        Parameters:
         name - The name for this Path Table Record.
         parent_dir_num - The directory number of the parent of this Path Table
                          Record.
        Returns:
         Nothing.
        '''
        self.len_di = len(name)
        self.xattr_length = 0  # FIXME: we don't support xattr for now
        self.extent_location = 0
        if parent_dir_num is None:
            self.parent_directory_num = 1
        else:
            self.parent_directory_num = parent_dir_num
        self.directory_identifier = name
        self._initialized = True

    def new_root(self):
        '''
        A method to create a new root Path Table Record.

        Parameters:
         None.
        Returns:
         Nothing.
        '''
        if self._initialized:
            raise pycdlibexception.PyCdlibInternalError('Path Table Record already initialized')

        self._new(b'\x00', None)

    def new_dir(self, name):
        '''
        A method to create a new Path Table Record.

        Parameters:
         name - The name for this Path Table Record.
        Returns:
         Nothing.
        '''
        if self._initialized:
            raise pycdlibexception.PyCdlibInternalError('Path Table Record already initialized')
<commit_msg>Simplify the code for assigning parent directory numbers.<commit_after># Copyright (C) 2015-2019  <NAME> <<EMAIL>>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

'''
Class to support ISO9660 Path Table Records.
'''

from __future__ import absolute_import

import struct

from pycdlib import pycdlibexception
from pycdlib import utils


class PathTableRecord(object):
    '''
    A class that represents a single ISO9660 Path Table Record.
    '''
    __slots__ = ('_initialized', 'len_di', 'xattr_length', 'extent_location',
                 'parent_directory_num', 'directory_identifier', 'dirrecord')

    FMT = '=BBLH'

    def __init__(self):
        self._initialized = False

    def parse(self, data):
        '''
        A method to parse an ISO9660 Path Table Record out of a string.

        Parameters:
         data - The string to parse.
        Returns:
         Nothing.
        '''
        (self.len_di, self.xattr_length, self.extent_location,
         self.parent_directory_num) = struct.unpack_from(self.FMT, data[:8], 0)

        if self.len_di % 2 != 0:
            self.directory_identifier = data[8:-1]
        else:
            self.directory_identifier = data[8:]
        self.dirrecord = None
        self._initialized = True

    def _record(self, ext_loc, parent_dir_num):
        '''
        An internal method to generate a string representing this Path Table Record.

        Parameters:
         ext_loc - The extent location to place in this Path Table Record.
         parent_dir_num - The parent directory number to place in this Path Table
                          Record.
        Returns:
         A string representing this Path Table Record.
        '''
        return struct.pack(self.FMT, self.len_di, self.xattr_length,
                           ext_loc, parent_dir_num) + self.directory_identifier + b'\x00' * (self.len_di % 2)

    def record_little_endian(self):
        '''
        A method to generate a string representing the little endian version of
        this Path Table Record.

        Parameters:
         None.
        Returns:
         A string representing the little endian version of this Path Table Record.
        '''
        if not self._initialized:
            raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized')

        return self._record(self.extent_location, self.parent_directory_num)

    def record_big_endian(self):
        '''
        A method to generate a string representing the big endian version of
        this Path Table Record.

        Parameters:
         None.
        Returns:
         A string representing the big endian version of this Path Table Record.
        '''
        if not self._initialized:
            raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized')

        return self._record(utils.swab_32bit(self.extent_location),
                            utils.swab_16bit(self.parent_directory_num))

    @classmethod
    def record_length(cls, len_di):
        '''
        A class method to calculate the length of a Path Table Record.

        Parameters:
         len_di - The length of the name for this Path Directory Record.
        Returns:
         The total length that a Path Directory Record with this name would occupy.
        '''
        return struct.calcsize(cls.FMT) + len_di + (len_di % 2)

    def _new(self, name, parent_dir_num):
        '''
        An internal method to create a new Path Table Record.

        Parameters:
         name - The name for this Path Table Record.
         parent_dir_num - The directory number of the parent of this Path Table
                          Record.
        Returns:
         Nothing.
        '''
        self.len_di = len(name)
        self.xattr_length = 0  # FIXME: we don't support xattr for now
        self.parent_directory_num = parent_dir_num
        self.directory_identifier = name
        self._initialized = True

    def new_root(self):
        '''
        A method to create a new root Path Table Record.

        Parameters:
         None.
        Returns:
         Nothing.
        '''
        if self._initialized:
            raise pycdlibexception.PyCdlibInternalError('Path Table Record already initialized')

        self._new(b'\x00', 1)

    def new_dir(self, name):
        '''
        A method to create a new Path Table Record.

        Parameters:
         name - The name for this Path Table Record.
        Returns:
         Nothing.
        '''
        if self._initialized:
            raise pycdlibexception.PyCdlibInternalError('Path Table Record already initialized')

{'fraction_non_alphanumeric': 0.06808550710989736, 'fraction_numerical': 0.00988793671720501, 'mean_word_length': 2.2142857142857144, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5368259', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2715, 'n_words': 1042}