<commit_before>import { ITlv, TlvType, TlvClass, TlvHelper } from './Tlv';
import { OctetBuffer } from '../node_modules/octet-buffer/dist/octet-buffer';

class TlvSerializationError implements Error {
    constructor(public name: string, public message: string) {}

    static errorPayloadToBig(tag: string, requested: number, maximum: number): TlvSerializationError {
      return new TlvSerializationError('Error while serializing item ' + tag + '"', 'Present length is ' + requested + ', maximum supported ' + maximum);
    }
}


export class TlvSerializer {

    static serializeItems(items: ITlv[]): Buffer {

        var serializedItems: Buffer[] = [];
        for (var item of items){
            var itemBuffer: Buffer = TlvSerializer.serializeItem(item);
            serializedItems.push(itemBuffer);
        }

        var serializedBuffer = Buffer.concat(serializedItems);
        return serializedBuffer;
    }

    static serializeItem(item: ITlv): Buffer {

        var serializedItem: Buffer;
        if (item.type === TlvType.CONSTRUCTED){
            serializedItem = TlvSerializer.serializeConstrucedItem(item);
        } else {
            serializedItem = TlvSerializer.serializePrimitiveItem(item);
        }

        return serializedItem;
    }



    static serializeConstrucedItem(item: ITlv): Buffer {
        var serializedItems: Buffer[] = [];
        for (var item of item.items){
            var itemBuffer: Buffer = TlvSerializer.serializeItem(item);
            serializedItems.push(itemBuffer);
        }
        var serializedItemsBuffer = Buffer.concat(serializedItems);

        var tagBuffer: Buffer = new Buffer(item.tag, 'hex');
        var lengthBuffer: Buffer = this.lengthBufferForLengt(item.tag, serializedItemsBuffer.length);

        var serializedItem: Buffer = Buffer.concat([tagBuffer, lengthBuffer, serializedItemsBuffer]);
        return serializedItem;
    }

    static serializePrimitiveItem(item: ITlv): Buffer {
        var tagBuffer: Buffer = new Buffer(item.tag, 'hex');
        var lengthBuffer: Buffer = this.lengthBufferForLengt(item.tag, item.value.length);

        var serializedItem: Buffer = Buffer.concat([tagBuffer, lengthBuffer, item.value]);
        return serializedItem;
    }

    static lengthBufferForLengt(tag: string, length: number): Buffer{

        //TODO: in the worst case we create an additional buffer internally, rethink this approach
        var octetBuffer: OctetBuffer = new OctetBuffer(new Buffer(1));

        if (length < 0x80){
            octetBuffer.writeUInt8(length);
        }
        else if (length <= 0xFF){
            octetBuffer.writeUInt8(0x81);
            octetBuffer.writeUInt8(length);
        }
        else if (length <= 0xFFFF){
          octetBuffer.writeUInt8(0x82);
          octetBuffer.writeUInt16(length);
        }
        else if (length <= 0xFFFFFF){
          octetBuffer.writeUInt8(0x83);
          octetBuffer.writeUInt24(length);
        }
        else if (length <= 0xFFFFFFFF){
          octetBuffer.writeUInt8(0x84);
          octetBuffer.writeUInt32(length);
        }
        else {
            throw TlvSerializationError.errorPayloadToBig(tag, length, 0xFFFFFFFF);
        }

        return octetBuffer.backingBuffer;
    }


}<commit_msg>refactored tlvserializer, now also using constants<commit_after>import { ITlv, TlvType, TlvClass, TlvHelper } from './Tlv';
import { OctetBuffer } from '../node_modules/octet-buffer/dist/octet-buffer';

export class TlvSerializerSerializeError implements Error {
    constructor(public name: string, public message: string) {}

    static errorPayloadToBig(tag: string, requested: number, maximum: number): TlvSerializerSerializeError {
      return new TlvSerializerSerializeError('Error while serializing item ' + tag + '"', 'Provided length is ' + requested + ', maximum supported ' + maximum);
    }
}

const TLV_SERIALIZE_MULTIBYTE_FLAG = 0x80;
const SERIALIZE_UINT8_MAX: number = 0xFF;
const SERIALIZE_UINT16_MAX: number = 0xFFFF;
const SERIALIZE_UINT24_MAX: number = 0xFFFFFF;
const SERIALIZE_UINT32_MAX: number = 0xFFFFFFFF;

export class TlvSerializer {

    static serializeItems(items: ITlv[]): Buffer {
        var serializedItems: Buffer[] = [];
        for (var item of items){
            var itemBuffer: Buffer = TlvSerializer.serializeItem(item);
            serializedItems.push(itemBuffer);
        }

        var serializedBuffer = Buffer.concat(serializedItems);
        return serializedBuffer;
    }

    static serializeItem(item: ITlv): Buffer {
        var serializedItem: Buffer;
        if (item.type === TlvType.CONSTRUCTED){
            serializedItem = TlvSerializer.serializeConstrucedItem(item);
        } else {
            serializedItem = TlvSerializer.serializePrimitiveItem(item);
        }

        return serializedItem;
    }

    static serializeConstrucedItem(item: ITlv): Buffer {
        var serializedItems: Buffer[] = [];
        for (var item of item.items){
            var itemBuffer: Buffer = TlvSerializer.serializeItem(item);
            serializedItems.push(itemBuffer);
        }
        var serializedItemsBuffer = Buffer.concat(serializedItems);

        var tagBuffer: Buffer = new Buffer(item.tag, 'hex');
        var lengthBuffer: Buffer = this.lengthBufferForLengt(item.tag, serializedItemsBuffer.length);

        var serializedItem: Buffer = Buffer.concat([tagBuffer, lengthBuffer, serializedItemsBuffer]);
        return serializedItem;
    }

    static serializePrimitiveItem(item: ITlv): Buffer {
        var tagBuffer: Buffer = new Buffer(item.tag, 'hex');
        var lengthBuffer: Buffer = this.lengthBufferForLengt(item.tag, item.value.length);

        var serializedItem: Buffer = Buffer.concat([tagBuffer, lengthBuffer, item.value]);
        return serializedItem;
    }

    static lengthBufferForLengt(tag: string, length: number): Buffer{
        var octetBuffer: OctetBuffer = new OctetBuffer(new Buffer(1));

        if (length < TLV_SERIALIZE_MULTIBYTE_FLAG){
            octetBuffer.writeUInt8(length);
        }
        else if (length <= SERIALIZE_UINT8_MAX){
            octetBuffer.writeUInt8(TLV_SERIALIZE_MULTIBYTE_FLAG | 0x01);
            octetBuffer.writeUInt8(length);
        }
        else if (length <= SERIALIZE_UINT16_MAX){
          octetBuffer.writeUInt8(TLV_SERIALIZE_MULTIBYTE_FLAG | 0x02);
          octetBuffer.writeUInt16(length);
        }
        else if (length <= SERIALIZE_UINT24_MAX){
          octetBuffer.writeUInt8(TLV_SERIALIZE_MULTIBYTE_FLAG | 0x03);
          octetBuffer.writeUInt24(length);
        }
        else if (length <= SERIALIZE_UINT32_MAX){
          octetBuffer.writeUInt8(TLV_SERIALIZE_MULTIBYTE_FLAG | 0x04);
          octetBuffer.writeUInt32(length);
        }
        else {
            throw TlvSerializerSerializeError.errorPayloadToBig(tag, length, SERIALIZE_UINT32_MAX);
        }

        return octetBuffer.backingBuffer;
    }

}
{'fraction_non_alphanumeric': 0.09557445581663543, 'fraction_numerical': 0.011676517226466772, 'mean_word_length': 2.9442865264354747, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, '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': '5750167', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1935, 'n_words': 431}