⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asx.c

📁 betaplayer的源码 tcpmp的老版本
💻 C
字号:
/*****************************************************************************
 *
 * This program is free software ; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * $Id: asx.c 11 2004-07-15 13:46:20Z picard $
 *
 * BetaPlayer
 * Copyright (c) 2004 Gabor Kovacs
 *
 ****************************************************************************/

#include "../stdafx.h"
#include "asx.h"

typedef struct asx
{
	format p;
	stream* Input;
	merge Buffer;
	int No;

} asx;

typedef struct guid
{
    uint32_t v1;
    uint16_t v2;
    uint16_t v3;
    uint8_t v4[8];
} guid;

static int Enum( asx* p, int No, datadef* Param )
{
	return NodeEnumType(&No,Param,NodeParams,FormatParams,FormatPlayListParams,NULL);
}

static int Get( asx* p, int No, void* Data, int Size )
{
	int Result = ERR_INVALID_PARAM;
	switch (No)
	{
	case NODE_ID: GETVALUE(ASX_ID,int); break;
	case FORMAT_EXTS: GETSTRING(T("asx:P;wmx:P;wvx:V;wax:A")); break;
	case FORMAT_INPUT: GETVALUE(p->Input,stream*); break;
	}
	return Result;
}

static guid ASFHeader = { 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }};

static int Init( asx* p )
{
	p->No = -1;
	MergeDone(&p->Buffer);
	if (p->Input)
	{
		MergeInit(&p->Buffer);
		MergeAlloc(&p->Buffer,4096,1);
		MergeStream(&p->Buffer,p->Input);

		if (p->Buffer.WritePos - p->Buffer.ReadPos >= 16)
		{
			// detect asf files (some people/companies use wrong extensions...)
			uint8_t* Data = p->Buffer.Data + p->Buffer.ReadPos;
			guid Id;
			Id.v1 = Data[0] | (Data[1] << 8) | (Data[2] << 16) | (Data[3] << 24);
			Data += 4;
			Id.v2 = (uint16_t)(Data[0] | (Data[1] << 8));
			Data += 2;
			Id.v3 = (uint16_t)(Data[0] | (Data[1] << 8));
			Data += 2;
			memcpy(Id.v4,Data,8);

			if (memcmp(&Id, &ASFHeader, sizeof(guid))==0)
			{
				MergeDone(&p->Buffer);
				return ERR_INVALID_DATA;
			}
		}
	}
	return ERR_NONE;
}

static int Set( asx* p, int No, const void* Data, int Size )
{
	int Result = ERR_INVALID_PARAM;
	switch (No)
	{
	case FORMAT_INPUT: SETVALUE(p->Input,stream*,Init(p)); break;
	}
	return Result;
}

static int ReadList( asx* p,tchar_t* Path,int PathLen,tchar_t* DispName,int DispNameLen,tick_t* Length)
{
	Path[0] = 0;
	DispName[0] = 0;
	*Length = -1;

	while (MergeStream(&p->Buffer,p->Input))
	{
		tchar_t Tag[8];
		StrToTcs(Tag,(char*)p->Buffer.Data+p->Buffer.ReadPos,8);
		TcsUpr(Tag);

		if (tcsncmp(Tag,T("</ENTRY"),7)==0)
		{
			p->Buffer.ReadPos += 7;
			if (Path[0])
				return ERR_NONE;
			DispName[0] = 0;
		}
		else
		if (tcsncmp(Tag,T("<TITLE"),6)==0)
		{
			int n,m;
			for (n=p->Buffer.ReadPos;n<p->Buffer.WritePos;++n)
				if (p->Buffer.Data[n] == '>')
					break;

			for (m=n+2;m<p->Buffer.WritePos;++m)
				if (p->Buffer.Data[m-1] == '<' &&
					p->Buffer.Data[m] == '/')
					break;
			
			if (m<p->Buffer.WritePos)
			{
				p->Buffer.Data[m-1] = 0;
				UTF8ToTcs(DispName,(char*)p->Buffer.Data+n+1,DispNameLen);
				p->Buffer.ReadPos = m-1;
			}
		}
		else
		if (tcsncmp(Tag,T("<REF"),4)==0 && !Path[0])
		{
			int n,m;
			for (n=p->Buffer.ReadPos;n<p->Buffer.WritePos;++n)
				if (p->Buffer.Data[n] == '"')
					break;

			for (m=n+1;m<p->Buffer.WritePos;++m)
				if (p->Buffer.Data[m] == '"')
					break;
			
			if (m<p->Buffer.WritePos)
			{
				p->Buffer.Data[m] = 0;
				UTF8ToTcs(Path,(char*)p->Buffer.Data+n+1,PathLen);
				p->Buffer.ReadPos = m;
			}
		}

		++p->Buffer.ReadPos;
	}

	return ERR_END_OF_FILE;
}

static int WriteList( asx* p, const tchar_t* Path,const tchar_t* DispName,tick_t Length)
{
	if (p->No<0)
	{
		p->No=0;
		StreamPrintf(p->Input,T("<ASX version = \"3.0\">\n"));
		StreamPrintf(p->Input,T("  <PARAM NAME = \"Encoding\" VALUE = \"UTF-8\" />\n"));
	}

	if (Path)
	{
	    StreamPrintf(p->Input,T("  <ENTRY>\n"));
		if (DispName)
		    StreamPrintfEx(p->Input,1,T("    <TITLE>%s</TITLE>\n"),DispName);
	    StreamPrintfEx(p->Input,1,T("    <REF HREF = \"%s\" />\n"),Path);
	    StreamPrintf(p->Input,T("  </ENTRY>\n"));
	}
	else
		StreamPrintf(p->Input,T("</ASX>\n"));

	return ERR_NONE;
}

static asx ASX;

void ASX_Init()
{
	NodeFill((node*)&ASX.p,sizeof(asx),(nodeenum)Enum,(nodeget)Get,(nodeset)Set);
	ASX.p.ReadList = (fmtreadlist)ReadList;
	ASX.p.WriteList = (fmtwritelist)WriteList;
	NodeRegister((node*)&ASX.p,PRI_DEFAULT);
}

void ASX_Done()
{
	NodeUnRegister((node*)&ASX.p);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -