<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace BCad.Dxf.Sections
{
    internal class DxfThumbnailImageSection : DxfSection
    {
        public override DxfSectionType Type
        {
            get { return DxfSectionType.Thumbnail; }
        }

        protected internal override IEnumerable<DxfCodePair> GetSpecificPairs(DxfAcadVersion version)
        {
            var list = new List<DxfCodePair>();
            list.Add(new DxfCodePair(90, RawData.Length));

            // write lines in 128-byte chunks (expands to 256 hex bytes)
            var sb = new StringBuilder();
            var chunkCount = (int)Math.Ceiling((double)RawData.Length / ChunkSize);
            for (int i = 0; i < chunkCount; i++)
            {
                sb.Clear();
                for (int offset = i * ChunkSize; offset < ChunkSize && offset < RawData.Length; offset++)
                {
                    sb.Append(RawData[offset].ToString("X2"));
                }

                list.Add(new DxfCodePair(310, sb.ToString()));
            }

            return list;
        }

        private const int ChunkSize = 128;

        public byte[] RawData { get; set; }

        public byte[] GetThumbnailBitmap()
        {
            var result = new byte[RawData.Length + BITMAPFILEHEADER.Length];

            // populate the bitmap header
            for (int i = 0; i < BITMAPFILEHEADER.Length; i++)
            {
                result[i] = BITMAPFILEHEADER[i];
            }

            // write the file length
            var lengthBytes = BitConverter.GetBytes(RawData.Length);
            for (int i = 0; i < lengthBytes.Length; i++)
            {
                result[i + BITMAPFILELENGTHOFFSET] = lengthBytes[i];
            }

            // copy the raw data
            Array.Copy(RawData, 0, result, BITMAPFILEHEADER.Length, RawData.Length);

            return result;
        }

        public void SetThumbnailBitmap(byte[] thumbnail)
        {
            // strip off bitmap header
            Debug.Assert(thumbnail != null);
            Debug.Assert(thumbnail.Length > BITMAPFILEHEADER.Length);
            Debug.Assert(thumbnail[0] == (byte)'B');
            Debug.Assert(thumbnail[0] == (byte)'M');
            RawData = new byte[thumbnail.Length - BITMAPFILEHEADER.Length];
            Array.Copy(thumbnail, BITMAPFILEHEADER.Length, RawData, 0, RawData.Length);
        }

        // BITMAPFILEHEADER structure
        private static byte[] BITMAPFILEHEADER
        {
            get
            {
                return new byte[]
                {<commit_msg>simplify some array copying code<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace BCad.Dxf.Sections
{
    internal class DxfThumbnailImageSection : DxfSection
    {
        public override DxfSectionType Type
        {
            get { return DxfSectionType.Thumbnail; }
        }

        protected internal override IEnumerable<DxfCodePair> GetSpecificPairs(DxfAcadVersion version)
        {
            var list = new List<DxfCodePair>();
            list.Add(new DxfCodePair(90, RawData.Length));

            // write lines in 128-byte chunks (expands to 256 hex bytes)
            var sb = new StringBuilder();
            var chunkCount = (int)Math.Ceiling((double)RawData.Length / ChunkSize);
            for (int i = 0; i < chunkCount; i++)
            {
                sb.Clear();
                for (int offset = i * ChunkSize; offset < ChunkSize && offset < RawData.Length; offset++)
                {
                    sb.Append(RawData[offset].ToString("X2"));
                }

                list.Add(new DxfCodePair(310, sb.ToString()));
            }

            return list;
        }

        private const int ChunkSize = 128;

        public byte[] RawData { get; set; }

        public byte[] GetThumbnailBitmap()
        {
            var result = new byte[RawData.Length + BITMAPFILEHEADER.Length];

            // populate the bitmap header
            Array.Copy(BITMAPFILEHEADER, 0, result, 0, BITMAPFILEHEADER.Length);

            // write the file length
            var lengthBytes = BitConverter.GetBytes(RawData.Length);
            Array.Copy(lengthBytes, 0, result, BITMAPFILELENGTHOFFSET, lengthBytes.Length);

            // copy the raw data
            Array.Copy(RawData, 0, result, BITMAPFILEHEADER.Length, RawData.Length);

            return result;
        }

        public void SetThumbnailBitmap(byte[] thumbnail)
        {
            // strip off bitmap header
            Debug.Assert(thumbnail != null);
            Debug.Assert(thumbnail.Length > BITMAPFILEHEADER.Length);
            Debug.Assert(thumbnail[0] == (byte)'B');
            Debug.Assert(thumbnail[0] == (byte)'M');
            RawData = new byte[thumbnail.Length - BITMAPFILEHEADER.Length];
            Array.Copy(thumbnail, BITMAPFILEHEADER.Length, RawData, 0, RawData.Length);
        }

        // BITMAPFILEHEADER structure
        private static byte[] BITMAPFILEHEADER
        {
            get
            {
                return new byte[]
                {
{'fraction_non_alphanumeric': 0.09787397050373492, 'fraction_numerical': 0.008619038498371959, 'mean_word_length': 1.7761828814460394, 'pattern_counts': {'":': 0, '<': 15, '<?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': '243772', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1357, 'n_words': 335}