#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
typedef struct id3_v2_head
{
char Header[3]; //必須爲"ID3"否則認爲標籤不存在
unsigned char Ver; //版本號ID3V2.3就記錄3
unsigned char Revision; //副版本號此版本記錄爲0
unsigned char Flag; //存放標誌的字節,這個版本只定義了三位,稍後詳細解說
unsigned char Size[4]; //標籤大小,包括標籤頭的10個字節和所有的標籤幀的大小
}id3_v2_head_t;
typedef struct id3_v2_frame
{
char FrameID[4]; //用四個字符標識一個幀,說明其內容,稍後有常用的標識對照表
unsigned char Size[4]; //幀內容的大小,不包括幀頭,不得小於1
unsigned char Flags[2]; //存放標誌,只定義了6位,稍後詳細解說
}id3_v2_frame_t;
int get_id3_v2_info(char *string);
int publish_weibo(const char* hdr, const int info_idx[3][2]);
int convert_UTF16toUTF8(const char *input, int *insize, char *output, int *outsize);
int main(int argc,char **argv)
{
char *filename;
if(argc!=2) {
return -1;
}
filename = argv[1];
get_id3_v2_info(filename);
return 0;
}
int get_id3_v2_info(char *string)
{
int info_idx[3][2] = {0};
char *filename=NULL;
int ret = 0;
id3_v2_head_t * id3_v2_head=NULL;
id3_v2_frame_t* id3_v2_frame=NULL;
int fd;
char *hdr = NULL;
long int id3_v2_head_length=0;
long int id3_v2_frame_length=0;
long int total_read=0;
struct stat sb;
char *info = NULL;
filename=string;
fd = open(filename, O_RDONLY);
if(fd==-1)
{
return -1;
}
fstat(fd,&sb);
hdr=(char*)mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,fd,0);
if(hdr== MAP_FAILED)
{
return 0;
close(fd);
}
id3_v2_head=(id3_v2_head_t *)malloc(sizeof(id3_v2_head_t));
if(id3_v2_head==NULL)
{
munmap(hdr,sb.st_size);
close(fd);
return -1;
}
memcpy(id3_v2_head,hdr,sizeof(id3_v2_head_t));
if(strncmp(id3_v2_head->Header, "ID3", 3))
{
printf("have no id3 v2 info\n");
munmap(hdr,sb.st_size);
close(fd);
return -1;
}
printf("have id3 v2.%d.%d info\n",id3_v2_head->Ver,id3_v2_head->Revision);
id3_v2_head_length = (id3_v2_head->Size[0]&0x7F)*0x200000+(id3_v2_head->Size[1]&0x7F)*0x400+(id3_v2_head->Size[2]&0x7F)*0x80+(id3_v2_head->Size[3]&0x7F);
printf("head length:%ld\n",id3_v2_head_length);
total_read += sizeof(id3_v2_head_t);
id3_v2_frame = (id3_v2_frame_t *)malloc(sizeof(id3_v2_frame_t));