<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Workshell.PE.Native;

namespace Workshell.PE
{

    internal class RelocationContentProvider : ISectionContentProvider
    {

        #region Methods

        public SectionContent Create(DataDirectory directory, Section section)
        {
            return new RelocationContent(directory,section);
        }

        #endregion

        #region Properties

        public DataDirectoryType DirectoryType
        {
            get
            {
                return DataDirectoryType.BaseRelocationTable;
            }
        }

        #endregion

    }

    public class RelocationContent : SectionContent, ILocationSupport, IRawDataSupport, IEnumerable<RelocationBlock>
    {

        private List<RelocationBlock> blocks;
        private StreamLocation location;

        internal RelocationContent(DataDirectory directory, Section section) : base(directory,section)
        {
            blocks = new List<RelocationBlock>();

            long offset = section.RVAToOffset(directory.VirtualAddress);
            long size = 0;
            Stream stream = Section.Sections.Reader.GetStream();

            stream.Seek(offset,SeekOrigin.Begin);

            while (true)
            {
                long block_offset = offset;
                IMAGE_BASE_RELOCATION block_data = Utils.Read<IMAGE_BASE_RELOCATION>(stream);

                offset += 8;
                size += 8;

                long num_relocations = (block_data.SizeOfBlock - 8) / 2;
                List<ushort> relocations = new List<ushort>();

                for(long i = 0; i < num_relocations; i++)
                {
                    ushort relocation = Utils.ReadUInt16(stream);

                    relocations.Add(relocation);

                    offset += 2;
                    size += 2;
                }

                RelocationBlock block = new RelocationBlock(this,block_offset,block_data.SizeOfBlock,block_data,relocations);
                
                blocks.Add(block);

                if (size >= directory.Size)
                    break;
            }

            location = new StreamLocation(offset,size);
        }

        #region Methods

        public IEnumerator<RelocationBlock> GetEnumerator()
        {
            return blocks.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public byte[] GetBytes()
        {
            Stream stream = Section.Sections.Reader.GetStream();
            byte[] buffer = Utils.ReadBytes(stream,location);

            return buffer;<commit_msg>Fixed another offset computation issue.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Workshell.PE.Native;

namespace Workshell.PE
{

    internal class RelocationContentProvider : ISectionContentProvider
    {

        #region Methods

        public SectionContent Create(DataDirectory directory, Section section)
        {
            return new RelocationContent(directory,section);
        }

        #endregion

        #region Properties

        public DataDirectoryType DirectoryType
        {
            get
            {
                return DataDirectoryType.BaseRelocationTable;
            }
        }

        #endregion

    }

    public class RelocationContent : SectionContent, ILocationSupport, IRawDataSupport, IEnumerable<RelocationBlock>
    {

        private List<RelocationBlock> blocks;
        private StreamLocation location;

        internal RelocationContent(DataDirectory directory, Section section) : base(directory,section)
        {
            blocks = new List<RelocationBlock>();

            long base_offset = section.RVAToOffset(directory.VirtualAddress);
            long offset = base_offset;
            long size = 0;
            Stream stream = Section.Sections.Reader.GetStream();

            stream.Seek(offset,SeekOrigin.Begin);

            while (true)
            {
                long block_offset = offset;
                IMAGE_BASE_RELOCATION block_data = Utils.Read<IMAGE_BASE_RELOCATION>(stream);

                offset += 8;
                size += 8;

                long num_relocations = (block_data.SizeOfBlock - 8) / 2;
                List<ushort> relocations = new List<ushort>();

                for(long i = 0; i < num_relocations; i++)
                {
                    ushort relocation = Utils.ReadUInt16(stream);

                    relocations.Add(relocation);

                    offset += 2;
                    size += 2;
                }

                RelocationBlock block = new RelocationBlock(this,block_offset,block_data.SizeOfBlock,block_data,relocations);
                
                blocks.Add(block);

                if (size >= directory.Size)
                    break;
            }

            location = new StreamLocation(base_offset,size);
        }

        #region Methods

        public IEnumerator<RelocationBlock> GetEnumerator()
        {
            return blocks.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public byte[] GetBytes()
        {
            Stream stream = Section.Sections.Reader.GetStream();
            byte[] buffer = Utils.ReadBytes(stream,location);

            return buffer;
{'fraction_non_alphanumeric': 0.07760296977196394, 'fraction_numerical': 0.00353544281421248, 'mean_word_length': 1.7817109144542773, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6098698', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1342, 'n_words': 290}