OSDN Git Service

commit local dev ver
[rec10/rec10-git.git] / epgdump / eit.c
index 23857df..f977e0c 100755 (executable)
@@ -1,3 +1,6 @@
+// -*- tab-width:4 -*-
+// see TR-B14 4-296 (p.318)
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -5,18 +8,11 @@
 #include "eit.h"
 
 char           *subtitle_cnv_str[] = {
-//     "¡¡Âè",
-//     "¡¼Âè",
-//     "-Âè",
-//     " Âè",
-//     "¡¡¡Ö",
-//     "¡¡¡ô",
-//     "¡Ê",
-//     "¡Ö",
        NULL
 };
 static void timecmp(int *,int *,int *,
                                        int, int, int);
+void append_desc(EIT_CONTROL* eittop, int service_id, int event_id, EEVTDitem* eevtitem);
 
 int parseEIThead(unsigned char *data, EIThead *h) {
        int boff = 0;
@@ -62,7 +58,7 @@ int parseEITbody(unsigned char *data, EITbody *b)
        b->free_CA_mode = getBit(data, &boff, 1);
        b->descriptors_loop_length = getBit(data, &boff, 12);
 
-       /* ÆüÉÕÊÑ´¹ */
+       /* 日付変換 */
        tnum = (b->start_time[0] & 0xFF) << 8 | (b->start_time[1] & 0xFF);
   
        b->yy = (tnum - 15078.2) / 365.25;
@@ -96,14 +92,14 @@ int parseEITbody(unsigned char *data, EITbody *b)
                b->dhh = b->dhm = b->dss = 0;
        }else{
                memset(buf, '\0', sizeof(buf));
-       sprintf(buf, "%x", b->duration[0]);
-       b->dhh = atoi(buf);
+               sprintf(buf, "%x", b->duration[0]);
+               b->dhh = atoi(buf);
                memset(buf, '\0', sizeof(buf));
-       sprintf(buf, "%x", b->duration[1]);
-       b->dhm = atoi(buf);
+               sprintf(buf, "%x", b->duration[1]);
+               b->dhm = atoi(buf);
                memset(buf, '\0', sizeof(buf));
-       sprintf(buf, "%x", b->duration[2]);
-       b->dss = atoi(buf);
+               sprintf(buf, "%x", b->duration[2]);
+               b->dss = atoi(buf);
        }
        return 12;
 }
@@ -140,7 +136,7 @@ int parseContentDesc(unsigned char *data, ContentDesc *desc) {
        }
        desc->descriptor_length = getBit(data, &boff, 8);
        memcpy(desc->content, data+(boff/8), desc->descriptor_length);
-       //getStr(desc->content, data, &boff, desc->descriptor_length);
+       /* getStr(desc->content, data, &boff, desc->descriptor_length); */
        return desc->descriptor_length + 2;
 }
 
@@ -197,11 +193,13 @@ int parseEEVTDitem(unsigned char *data, EEVTDitem *desc) {
        memset(desc, 0, sizeof(EEVTDitem));
 
        desc->item_description_length = getBit(data, &boff, 8);
+       memset(desc->item_description, 0, MAXSECLEN);
        getStr(desc->item_description, data, &boff, desc->item_description_length);
 
        desc->item_length = getBit(data, &boff, 8);
-//     memcpy(desc->item, data + (boff / 8), desc->item_length);
-       getStr(desc->item, data, &boff, desc->item_length);
+       memset(desc->item, 0, MAXSECLEN);
+       memcpy(desc->item, data + (boff / 8), desc->item_length);
+       /* getStr(desc->item, data, &boff, desc->item_length); */
 
        return desc->item_description_length + desc->item_length + 2;
 }
@@ -212,6 +210,7 @@ int parseEEVTDtail(unsigned char *data, EEVTDtail *desc) {
        memset(desc, 0, sizeof(EEVTDtail));
 
        desc->text_length = getBit(data, &boff, 8);
+       memset(desc->text, 0, MAXSECLEN);
        getStr(desc->text, data, &boff, desc->text_length);
 
        return desc->text_length + 1;
@@ -221,76 +220,44 @@ int checkEEVTDitem(EEVTDitem *save, EEVTDitem *new, int descriptor_number) {
 
        EEVTDitem swap;
        int boff = 0;
+
        if(new == NULL) {
                if(save->item_length != 0) {
                        swap = *save;
+                       memset(save->item, 0, MAXSECLEN);
                        getStr(save->item, (unsigned char*)swap.item, &boff, swap.item_length);
-
-
-                        //save->descriptor_number = descriptor_number;
-                        //*new = swap;
-                       //*save = swap;
-                        if (swap.descriptor_number==1){
-//                            printf("descriptor_num %d   typeB\n",swap.descriptor_number);
-                        }
-
                        return 1;
                } else {
-
-
-                        //printf("descriptor_num %d   typeC\n",swap.descriptor_number);
-
-
-
                        return 0;
                }
        }
 
        if(new->item_description_length == 0) {
-               /* Â³¤­ Êݸ */
+               /* 続き 保存 */
                memcpy(save->item + save->item_length, new->item, new->item_length);
                save->item_length += new->item_length;
-
-
-                if (swap.descriptor_number==1){
-                            printf("descriptor_num %d   typeD\n",swap.descriptor_number);
-                        }
-
-
                return 0;
        } else {
-               /* ¥Ö¥ì¡¼¥¯¡£save¤ò°õºþÂоݤˤ¹¤ë¡£save¤ò¥¯¥ê¥¢? */
+               /* ã\83\96ã\83¬ã\83¼ã\82¯ã\80\82saveã\82\92å\8d°å\88·å¯¾è±¡ã\81«ã\81\99ã\82\8bã\80\82saveã\82\92ã\82¯ã\83ªã\82¢? */
                if(save->item_length != 0) {
-                       /* ÂàÈòºÑ¤ß¤¬¤¢¤ê */
+                       /* 退避済みがあり */
                        swap = *save;
+                       memset(save->item, 0, MAXSECLEN);
                        getStr(save->item, (unsigned char*)swap.item, &boff, swap.item_length);
+
+                       // swap save new -> swap save new
+                       //         b   c -> ( c)    c   b
                        swap = *new;
                        *new = *save;
                        *save = swap;
                        save->descriptor_number = descriptor_number;
-                       
-
-
-
-                        if (swap.descriptor_number==1){
-                            printf("descriptor_num %d   typeE\n",swap.descriptor_number);
-                        }
-                        
-
+                       return 1;
                } else {
                        *save = *new;
                        save->descriptor_number = descriptor_number;
-
-                        if (swap.descriptor_number==1){
-                            printf("descriptor_num %d   typeF\n",swap.descriptor_number);
-                        }
-
-
                        return 0;
                }
        }
-
-       return 1;
 }
 EIT_CONTROL    *searcheit(EIT_CONTROL *top, int servid, int eventid)
 {
@@ -306,11 +273,34 @@ EIT_CONTROL       *searcheit(EIT_CONTROL *top, int servid, int eventid)
        }
        return NULL ;
 }
+char   *strstr_eucjp(const char *str, const char *search)
+{
+       char *pos ;
+       pos = (char *)str ;
+
+       while (*pos != '\0') {
+               if (*pos == *search) {
+                       if (strncmp(pos, search, strlen(search)) == 0) {
+                               return pos ;
+                       }
+               }
+               if ((unsigned char)*pos == 0x8Fu) {
+                       pos += 3 ;
+               } else if ((unsigned char)*pos >= 0x80u) {
+                       pos += 2 ;
+               } else {
+                       pos += 1 ;
+               }
+       }
+
+       return NULL ;
+}
 void   conv_title_subtitle(EIT_CONTROL *eitptr)
 {
        int             lp = 0 ;
-       size_t  addsize ;
+//     size_t  addsize ;
        char    *ptr ;
+       char    *ptr2 ;
        char    *newsubtitle ;
 
        for(lp = 0 ; subtitle_cnv_str[lp] != NULL ; lp++){
@@ -318,14 +308,25 @@ void      conv_title_subtitle(EIT_CONTROL *eitptr)
                if(ptr == NULL){
                        continue ;
                }
-               // ¥¿¥¤¥È¥ë¤¬¤Ê¤¯¤Ê¤é¤Ê¤¤¤è¤¦¤Ë
+               // タイトルがなくならないように
                if(ptr == eitptr->title){
                        continue ;
                }
+               /*
+               ptr2 = ptr ;
+               for( ; (unsigned char)*ptr2 == 0x20u ; ptr2++ );
+               for( ; (unsigned char)*ptr2 == 0xA1u && (unsigned char)*(ptr2+1) == 0xA1u ; ptr2 += 2);
+               for( ; (unsigned char)*ptr2 == 0x20u ; ptr2++ );
+               newsubtitle = calloc(1, ((strlen(ptr2) + 2) + (strlen(eitptr->subtitle) + 1)));
+               memcpy(newsubtitle, ptr2, strlen(ptr2));
+//             *(newsubtitle+strlen(ptr)) = ' ';
+               strcat(newsubtitle, "▽");
+               */
                newsubtitle = calloc(1, ((strlen(ptr) + 1) + (strlen(eitptr->subtitle) + 1)));
                memcpy(newsubtitle, ptr, strlen(ptr));
                newsubtitle[strlen(ptr)] = ' ';
-               *ptr = NULL ;
+
+               *ptr = '\0';
                strcat(newsubtitle, eitptr->subtitle);
                free(eitptr->subtitle);
                eitptr->subtitle = newsubtitle ;
@@ -407,7 +408,6 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
        int loop_len = 0;
        int loop_blen = 0;
        int loop_elen = 0;
-       int str_alen = 0;
 
        int ehh, emm, ess;
 
@@ -415,23 +415,29 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
        len = parseEIThead(ptr, &eith); 
 
        ptr += len;
-       loop_len = eith.section_length - (len - 3 + 4); // 3¤Ï¶¦Ḁ̈إåÀĹ 4¤ÏCRC
+       loop_len = eith.section_length - (len - 3 + 4); // 3は共通ヘッダ長 4はCRC
        while(loop_len > 0) {
-               /* Ï¢Â³¤¹¤ë³ÈÄ¥¥¤¥Ù¥ó¥È¤Ï¡¢´Á»ú¥³¡¼¥É¤¬µã¤­Ê̤줷¤Æ
-                  Ê¬³ä¤µ¤ì¤ë¤è¤¦¤À¡£Ï¢Â³¤«¤É¤¦¤«¤Ï¡¢item_description_length¤¬
-                  ÀßÄꤵ¤ì¤Æ¤¤¤ë¤«¤É¤¦¤«¤ÇȽÃǤǤ­¤ë¤è¤¦¤À¡£ */
+               /* 連続する拡張イベントは、漢字コードが泣き別れして
+                  分割されるようだ。連続かどうかは、item_description_lengthが
+                  設定されているかどうかで判断できるようだ。 */
                memset(&save_eevtitem, 0, sizeof(EEVTDitem));
 
                len = parseEITbody(ptr, &eitb);
                ptr += len;
                loop_len -= len;
-    
+
                /* printf("evtid:%d\n", eitb.event_id); */
-    
+
                loop_blen = eitb.descriptors_loop_length;
                loop_len -= loop_blen;
+
                while(loop_blen > 0) {
 
+                       /*yukikaze-test*/
+                       //printf("desc_tag");
+                       //int itt=getBit(*ptr,0,8);
+                       //printf("%x",itt);
+
                        len = parseSEVTdesc(ptr, &sevtd);
                        if(len > 0) {
 
@@ -483,10 +489,10 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
                                len = parseEEVTDhead(ptr, &eevthead);
 
                                /*
-                                 if(eith.service_id == 19304 && 
-                                 eitb.event_id == 46564) {
-                                 printf("aa");
-                                 }
+                               if(eith.service_id == 19304 && 
+                               eitb.event_id == 46564) {
+                               printf("aa");
+                               }
                                */
 
                                if(len > 0) {
@@ -501,60 +507,27 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
                                                ptr += len;
                                                loop_elen -= len;
                                                loop_blen -= len;
-                                                
-                                               if (1||checkEEVTDitem(&save_eevtitem, &eevtitem,
-                                                                                 eevthead.descriptor_number)) {
-#if 0
-                                                       {
-                                                            if (eevtitem.descriptor_number>0){
-                                                                //printf("descriptor_num %d\n",eevtitem.descriptor_number);
-                                                            }
-
-                                                            /* long format */
-                                                            //printf("descriptor_num %d\n",eevtitem.descriptor_number);
-#if 1
-                                                            printf("EEVT,%d,%d,%d,%s,%s\n",
-                                                                               eith.service_id,
-                                                                                       eitb.event_id,
-                                                                                       eevtitem.descriptor_number, // ÂàÈò¹àÌÜ
-                                                                               eevtitem.item_description,
-                                                                               eevtitem.item);
-#endif
 
-                                                       }
-#endif
-                                                       cur = searcheit(eittop, eith.service_id, eitb.event_id);
-                                                       if(cur == NULL){
-                                                               curtmp = NULL;
-                                                               cur = calloc(1, sizeof(EIT_CONTROL));
-                                                       }
-                                                       else {
-                                                               curtmp = cur;
-                                                       }
+                                               if(checkEEVTDitem(&save_eevtitem, &eevtitem, 
+                                                       eevthead.descriptor_number)) {
 
-#if 1
-                                                       if ( cur->desc ) {
-                                                               str_alen = strlen( cur->desc );
-                                                       }
-                                                       else {
-                                                               str_alen = 0;
-                                                       }
-                                                       eevtitem.item_description_length = strlen(eevtitem.item_description);
-                                                       eevtitem.item_length = strlen(eevtitem.item);
-                                                       cur->desc = realloc(cur->desc, str_alen + eevtitem.item_description_length + eevtitem.item_length + 1000);
-                                                       if ( !str_alen ) *cur->desc = '\0';
-
-                                                       if ( eevtitem.item_description_length && !strstr(cur->desc, eevtitem.item_description) ) {
-                                                               strcat(cur->desc, eevtitem.item_description);
-                                                               strcat(cur->desc, "\t");
+                                                       {
+                                                               if (eevtitem.descriptor_number>0){
+                                                                       //printf("descriptor_num %d\n",eevtitem.descriptor_number);
+                                                               }
+                                                               /* long format */
+                                                               //printf("descriptor_num %d\n",eevtitem.descriptor_number);
+#if 0
+                                                               printf("EEVT,%d,%d,%d,%s,%s\n",
+                                                                       eith.service_id,
+                                                                       eitb.event_id,
+                                                                       eevtitem.descriptor_number, // 退避項目
+                                                                       eevtitem.item_description,
+                                                                       eevtitem.item);
+#endif
                                                        }
+                                                       append_desc(eittop, eith.service_id, eitb.event_id, &eevtitem);
 
-                                                       if ( eevtitem.item_length && !strstr(cur->desc, eevtitem.item)  ) {
-                                                               strcat(cur->desc, eevtitem.item);
-                                                               strcat(cur->desc, "\\n");
-                                                       }
-#endif
-                                                       if ( !curtmp ) enqueue(eittop, cur);
                                                }
                                        }
 
@@ -572,7 +545,7 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
                                        ContentDesc contentDesc;
                                        len = parseContentDesc(ptr, &contentDesc);
                                        if (len > 0) {
-                                               int header_printed = 0;
+                                               //int header_printed = 0;
                                                for (int i = 0; i < contentDesc.descriptor_length - 1; i+=2) {
                                                        /*
                                                        if (0xff == (unsigned char)contentDesc.content[i])
@@ -580,16 +553,15 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
                                                        */
 #if 0
                                                        if (!header_printed) {
-                                                               fprintf(out, "Content,%d,%d",
+                                                               fprintf(out, "Content,%d,%d\n",
                                                                        eith.service_id,
                                                                        eitb.event_id);
                                                                header_printed = 1;
                                                        }
 #endif
 
-#if 0
-                                                       fprintf(out, ",%02x%02x", (unsigned char)contentDesc.content[i], (unsigned char)contentDesc.content[i+1]);
-#endif
+                                                       //fprintf(out, ",%02x%02x", (unsigned char)contentDesc.content[i], (unsigned char)contentDesc.content[i+1]);
+
                                                }
                                                if((eith.original_network_id == original_network_id) && (eith.transport_stream_id == transport_stream_id)){
                                                        cur = searcheit(eittop, eith.service_id, eitb.event_id);
@@ -600,8 +572,8 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
                                                                fprintf(stdout, ",%02x%02x", (unsigned char)contentDesc.content[0], (unsigned char)contentDesc.content[1]);
                                                                fprintf(stdout, ",%02x%02x\n", (unsigned char)contentDesc.content[2], (unsigned char)contentDesc.content[3]);
 #endif
-
                                                        }
+
 #if 0
                                                        if (header_printed) {
                                                                fprintf(out, "\n");
@@ -634,12 +606,12 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
                        ptr += len;
                        loop_blen -= len;
                }
-               /* ºÇ¸å¤Î¥Ö¥ì¡¼¥¯¥Á¥§¥Ã¥¯ */
-    
+               /* æ\9c\80å¾\8cã\81®ã\83\96ã\83¬ã\83¼ã\82¯ã\83\81ã\82§ã\83\83ã\82¯ */
+
                if(checkEEVTDitem(&save_eevtitem, NULL, 0)) {
 #if 0
-                       if(mode == 1) { /* long format */
-                               fprintf(out, "EEVT,%d,%d,%d,%s,%s\n", 
+                       { /* long format */
+                               fprintf(stdout, "EEVT,%d,%d,%d,%s,%s\n", 
                                                eith.service_id,
                                                eitb.event_id,
                                                save_eevtitem.descriptor_number,
@@ -647,12 +619,45 @@ void dumpEIT(unsigned char *ptr, int serv_id, int original_network_id, int trans
                                                save_eevtitem.item);
                        }
 #endif
+                       append_desc(eittop, eith.service_id, eitb.event_id, &save_eevtitem);
                }
        }
 
        return;
 }
 
+void append_desc(EIT_CONTROL* eittop, int service_id, int event_id, EEVTDitem* eevtitem) {
+       EIT_CONTROL *cur;
+       int str_alen = 0;
+
+       cur = searcheit(eittop, service_id, event_id);
+       if (cur == NULL) {
+               return;
+       }
+
+       if ( cur->desc ) {
+               str_alen = strlen( cur->desc );
+       }
+       else {
+               str_alen = 0;
+       }
+       //eevtitem->item_description_length = strlen(eevtitem->item_description);
+       //eevtitem->item_length = strlen(eevtitem->item);
+       cur->desc = realloc(cur->desc, str_alen + eevtitem->item_description_length + eevtitem->item_length + 1000);
+       if ( !str_alen ) *cur->desc = '\0';
+
+       if ( eevtitem->item_description_length && !strstr(cur->desc, eevtitem->item_description) ) {
+               strcat(cur->desc + str_alen, eevtitem->item_description);
+               strcat(cur->desc, "\t");
+       }
+
+       if ( eevtitem->item_length && !strstr(cur->desc, eevtitem->item) ) {
+               strcat(cur->desc + str_alen, eevtitem->item);
+       //printf("%s\n",eevtitem->item);
+               strcat(cur->desc, "\\n");
+       }
+}
+
 void timecmp(int *thh, int *tmm, int *tss,
                         int dhh, int dmm, int dss) {