librassection.cpp 4.01 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "librassection.h"
#include "crc32.h"

namespace Codificador{

	LibrasSection::LibrasSection(LibrasControlSection *libr, unsigned int type, unsigned char version){
		
                tableid = 145;
                section_syntax_indicator = 1;
                
                if( type == 0 )
                    table_id_extension = 0x0001;
                if( type == 1)
                    table_id_extension = 0x0002;

                version_number = version;
                current_next_indicator = 1;
                section_number = 0;
                last_section_number = 0;

                libras_section_length = 9;
                
		libras = libr;
	}
	
	LibrasSection::~LibrasSection(){

	}
	
	unsigned char LibrasSection::getSectionSyntaxIndicator(){
		return section_syntax_indicator;
	}
	
	unsigned short LibrasSection::getLibrasSectionLength(){
		return libras_section_length;
	}
	
	unsigned char LibrasSection::getTableIdExtension(){
		return table_id_extension;
	}

        unsigned char LibrasSection::getVersionNumber(){
		return version_number;
	}

        unsigned char LibrasSection::getCurrentNextIndicator(){
		return current_next_indicator;
	}

        unsigned char LibrasSection::getSectionNumber(){
		return section_number;
	}

        unsigned char LibrasSection::getLastSectionNumber(){
		return last_section_number;
	}

        unsigned int LibrasSection::getCrc32(){
		return crc32;
	}
	
	LibrasControlSection *LibrasSection::getLibras(){
		return libras;
	}
	
	void LibrasSection::setSectionSyntaxIndicator(unsigned char section_syntax_indicator){
		this->section_syntax_indicator = section_syntax_indicator;
	}
	
	void LibrasSection::setLibrasSectionLength(unsigned short libras_section_length){
		this->libras_section_length = libras_section_length;
	}
	
	void LibrasSection::setTableIdExtension(unsigned short table_id_extension){
		this->table_id_extension = table_id_extension;
	}

        void LibrasSection::setVersionNumber(unsigned char version_number){
		this->version_number = version_number;
	}

        void LibrasSection::setCurrentNextIndicator(unsigned char current_next_indicator){
		this->current_next_indicator = current_next_indicator;
	}

        void LibrasSection::setSectionNumber(unsigned char section_number){
		this->section_number = section_number;
	}

        void LibrasSection::setLastSectionNumber(unsigned char last_section_number){
		this->last_section_number = last_section_number;
	}
	
	void LibrasSection::setLibras(LibrasControlSection *libras){
		this->libras = libras;
	}

	unsigned char *LibrasSection::generateBytes(unsigned short *librLen){
		int offset = 0;
		unsigned short librasLen;
		unsigned char *librSecBytes;
		unsigned char *librasBytes = libras->generateBytes(&librasLen);

		*librLen = librasLen + libras_section_length + 3;
                libras_section_length += librasLen;
                
		librSecBytes = new unsigned char[*librLen];

                librSecBytes[0] = tableid & 0xFF;
                librSecBytes[1] = (section_syntax_indicator << 7) & 0x80;
                librSecBytes[1] = (libras_section_length >> 8) & 0x0F;
                librSecBytes[2] = libras_section_length & 0xFF;
                librSecBytes[3] = (table_id_extension >> 8) & 0xFF;
                librSecBytes[4] = table_id_extension & 0xFF;
                librSecBytes[5] = (version_number << 5) & 0x3E;
                librSecBytes[5] = current_next_indicator & 0x01;
                librSecBytes[6] = section_number & 0xFF;
                librSecBytes[7] = last_section_number & 0xFF;

		memcpy(librSecBytes + 8, librasBytes, librasLen);

                crc32 = calculate_CRC_32(librSecBytes, *librLen - 4);
                offset = 8 + librasLen;

                librSecBytes[offset + 0]= (crc32 >> 24) & 0xFF;
                librSecBytes[offset + 1]= (crc32 >> 16) & 0xFF;
                librSecBytes[offset + 2]= (crc32 >> 8) & 0xFF;
                librSecBytes[offset + 3]= crc32 & 0xFF;
		
		delete []librasBytes;
		return librSecBytes;
	}
}