OSDN Git Service

add EIT descriptor parser (video,audio)
[rec10/rec10-git.git] / epgdump / epgdump.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <getopt.h>
6 #include <iconv.h>
7 #include <time.h>
8
9 #include "ts.h"
10 #include "psi.h"
11 #include "sdt.h"
12 #include "sdtt.h"
13 #include "eit.h"
14 #include "tot.h"
15 #include "dsmcc.h"
16 #include "ts_ctl.h"
17 #include "util.h"
18
19 typedef         struct  _ContentTYPE{
20         char    *japanese ;
21         char    *english ;
22 }CONTENT_TYPE;
23
24 #define         CAT_COUNT               16
25 static  CONTENT_TYPE    ContentCatList[CAT_COUNT] = {
26         { "ニュース・報道", "news" },
27         { "スポーツ", "sports" },
28         { "情報", "information" },
29         { "ドラマ", "drama" },
30         { "音楽", "music" },
31         { "バラエティ", "variety" },
32         { "映画", "cinema" },
33         { "アニメ・特撮", "anime" },
34         { "ドキュメンタリー・教養", "documentary" },
35         { "演劇", "stage" },
36         { "趣味・実用", "hobby" },
37         { "福祉", "etc" },                    //福祉
38         { "予備", "etc" }, //予備
39         { "予備", "etc" }, //予備
40         { "予備", "etc" }, //予備
41         { "その他", "etc" } //その他
42 };
43
44
45 SVT_CONTROL     *svttop = NULL;
46 DSM_CONTROL     dsmctl[1024];
47 #define         SECCOUNT        64
48 static unsigned char *base64 = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
49
50 static void base64_char(unsigned long bb, int srclen, unsigned char *dest, int j)
51 {
52         int x, i, base;
53
54         /* 最終位置の計算 */
55         for ( i = srclen; i < 2; i++ ) 
56                 bb <<= 8;
57
58         /* BASE64変換 */
59         for ( base = 18, x = 0; x < srclen + 2; x++, base -= 6) {
60                 dest[j++] = base64[ (unsigned long)((bb>>base) & 0x3F) ];
61         }
62
63         /* 端数の判断 */
64         for ( i = x; i < 4; i++ ) {
65                 dest[j++] = (unsigned char)'=';         /* 端数 */
66         }
67         
68 }
69
70 static void base64_encode(unsigned char *dest, const unsigned char *src, int len)
71 {
72         unsigned char *p = src;
73         unsigned long bb = (unsigned long)0;
74         int i = 0, j = 0;
75
76         while (len--)
77         {
78                 bb <<= 8;
79                 bb |= (unsigned long)*p;
80
81                 /* 24bit単位に編集 */
82                 if (i == 2) {
83                         base64_char(bb, i, dest, j);
84
85                         j = j + 4;
86                         i = 0;
87                         bb = 0;
88                 } else
89                         i++;
90
91                 p++;
92         }
93
94         /* 24bitに満たない場合 */
95         if (i) base64_char(bb, i - 1, dest, j);
96
97 }
98
99 void    xmlspecialchars(char *str)
100 {
101         strrep(str, "&", "&amp;");
102         strrep(str, "'", "&apos;");
103         strrep(str, "\"", "&quot;");
104         strrep(str, "<", "&lt;");
105         strrep(str, ">", "&gt;");
106 }
107
108
109
110 void    GetSDT(FILE *infile, SVT_CONTROL *svttop, SECcache *secs, 
111         int count, STATION **station, int * station_count, char *header, int is_logo)
112 {
113         SECcache  *bsecs;
114         int pmtpids[SECCOUNT];
115         memset(pmtpids, 0, sizeof(pmtpids));
116         int dsmccpids[SECCOUNT];
117         memset(dsmccpids, 0, sizeof(dsmccpids));
118         int i = 0 , downloadDataId = 0;
119
120         while((bsecs = readTS(infile, secs, count)) != NULL) {
121                 /* SDT */
122                 if((bsecs->pid & 0xFF) == 0x11) {
123                         dumpSDT(bsecs->buf, svttop, station, station_count, header);
124                 }
125                 /* TOT */
126                 else if((bsecs->pid & 0xFF) == 0x14) {
127                         dumpTOT(bsecs->buf);
128                 }
129                 /* SDTT */
130                 //else if((bsecs->pid & 0xFF) == 0x23) {
131                 //      dumpSDTT(bsecs->buf, *station, *station_count);
132                 //}
133                 /* CDT */
134                 else if((bsecs->pid & 0xFF) == 0x29) {
135                         dumpCDT(bsecs->buf, *station, *station_count);
136                 }
137                 else if ( is_logo ) {
138                         /* PAT */
139                         if((bsecs->pid & 0xFF) == 0x00) {
140                                 dumpPAT(bsecs->buf, secs, count, pmtpids);
141                         }
142                         /* PMT */
143                         for ( i = 1; i < SECCOUNT; i++ ) {
144                                 if ( pmtpids[i] == 0 ) {
145                                         break;
146                                 }
147                                 /* PMT specified by PAT */
148                                 if ( bsecs->pid == pmtpids[i] ) {
149                                         dumpPMT(bsecs->buf, secs, count, dsmccpids);
150                                 }
151                         }
152                         /* DSM-CC */
153                         for ( i = 0; i < SECCOUNT; i++ ) {
154                                 if ( dsmccpids[i] == 0 ) {
155                                         break;
156                                 }
157                                 /* DSM-CC specified by PMT */
158                                 if ( bsecs->pid == dsmccpids[i] ) {
159                                         dumpDSMCC(bsecs->buf, &downloadDataId, &dsmctl);
160                                 }
161                         }
162                 }
163         }
164 }
165
166 void    GetEIT(FILE *infile, FILE *outfile, STATION *psta, SECcache *secs)
167 {
168         SECcache  *bsecs;
169         EIT_CONTROL     *eitcur ;
170         EIT_CONTROL     *eitnext ;
171         EIT_CONTROL     *eittop = NULL;
172         time_t  l_time ;
173         time_t  end_time ;
174         struct  tm      tl ;
175         struct  tm      *endtl ;
176         char    cendtime[32];
177         char    cstarttime[32];
178
179         char    title[1024];
180         char    subtitle[1024];
181         char    desc[102400] = {0};
182         char    Category[1024];
183         char    VideoType[1024];
184         char    AudioType[1024];
185
186         memset(secs, 0,  sizeof(SECcache) * SECCOUNT);
187         secs[0].pid = 0x12; /* EIT  */
188         secs[1].pid = 0x26; /* EIT  */
189         secs[2].pid = 0x27; /* EIT  */
190
191         eittop = calloc(1, sizeof(EIT_CONTROL));
192         eitcur = eittop ;
193         fseek(infile, 0, SEEK_SET);
194         while((bsecs = readTS(infile, secs, SECCOUNT)) != NULL) {
195                 /* EIT */
196                 if((bsecs->pid & 0xFF) == 0x12) {
197                         dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
198                 }else if((bsecs->pid & 0xFF) == 0x26) {
199                         dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
200                 }else if((bsecs->pid & 0xFF) == 0x27) {
201                         dumpEIT(bsecs->buf, psta->svId, psta->onId, psta->tsId, eittop);
202                 }
203         }
204         eitcur = eittop ;
205         while(eitcur != NULL){
206                 if(!eitcur->servid){
207                         eitcur = eitcur->next ;
208                         continue ;
209                 }
210                 if(eitcur->content_type > CAT_COUNT){
211                         eitcur->content_type = CAT_COUNT -1 ;
212                 }
213                 memset(title, '\0', sizeof(title));
214                 strcpy(title, eitcur->title);
215                 xmlspecialchars(title);
216
217                 memset(subtitle, '\0', sizeof(subtitle));
218                 strcpy(subtitle, eitcur->subtitle);
219                 xmlspecialchars(subtitle);
220
221                 memset(desc, '\0', sizeof(desc));
222                 if ( eitcur->desc ) {
223                         strcpy(desc, eitcur->desc);
224                         xmlspecialchars(desc);
225                 }
226
227                 memset(Category, '\0', sizeof(Category));
228                 strcpy(Category, ContentCatList[eitcur->content_type].japanese);
229                 xmlspecialchars(Category);
230
231                 memset(VideoType, '\0', sizeof(VideoType));
232                 strcpy(VideoType, parseComponentDescType(eitcur->video_type));
233                 xmlspecialchars(VideoType);
234
235                 memset(AudioType, '\0', sizeof(AudioType));
236                 strcpy(AudioType, parseAudioComponentDescType(eitcur->audio_type));
237                 xmlspecialchars(AudioType);
238
239                 tl.tm_sec = eitcur->ss ;
240                 tl.tm_min = eitcur->hm ;
241                 tl.tm_hour = eitcur->hh ;
242                 tl.tm_mday = eitcur->dd ;
243                 tl.tm_mon = (eitcur->mm - 1);
244                 tl.tm_year = (eitcur->yy - 1900);
245                 tl.tm_wday = 0;
246                 tl.tm_isdst = 0;
247                 tl.tm_yday = 0;
248                 l_time = mktime(&tl);
249                 if((eitcur->ehh == 0) && (eitcur->emm == 0) && (eitcur->ess == 0)){
250                         (void)time(&l_time);
251                         end_time = l_time + (60 * 5);           // 5分後に設定
252                 endtl = localtime(&end_time);
253                 }else{
254                         end_time = l_time + eitcur->ehh * 3600 + eitcur->emm * 60 + eitcur->ess;
255                         endtl = localtime(&end_time);
256                 }
257                 memset(cendtime, '\0', sizeof(cendtime));
258                 memset(cstarttime, '\0', sizeof(cstarttime));
259                 strftime(cendtime, (sizeof(cendtime) - 1), "%Y%m%d%H%M%S", endtl);
260                 strftime(cstarttime, (sizeof(cstarttime) - 1), "%Y%m%d%H%M%S", &tl);
261
262                 fprintf(outfile, "  <programme start=\"%s +0900\" stop=\"%s +0900\" channel=\"%s\" event=\"%d\">\n",    
263                                 cstarttime, cendtime, psta->ontv, eitcur->event_id);
264                 fprintf(outfile, "    <title lang=\"ja_JP\">%s</title>\n", title);
265                 fprintf(outfile, "    <desc lang=\"ja_JP\">%s</desc>\n", subtitle);
266                 fprintf(outfile, "    <longdesc lang=\"ja_JP\">%s</longdesc>\n", desc);
267                 fprintf(outfile, "    <category lang=\"ja_JP\">%s</category>\n", Category);
268                 fprintf(outfile, "    <video type=\"%d\">%s</video>\n", eitcur->video_type, VideoType);
269                 fprintf(outfile, "    <audio type=\"%d\" multi=\"%d\">%s</audio>\n", eitcur->audio_type, eitcur->multi_type, AudioType);
270                 //fprintf(outfile, "    <category lang=\"en\">%s</category>\n", ContentCatList[eitcur->content_type].english);
271                 fprintf(outfile, "  </programme>\n");
272 #if 0
273                 fprintf(outfile, "(%x:%x:%x)%s,%s,%s,%s,%s,%s\n",
274                                         eitcur->servid, eitcur->table_id, eitcur->event_id,
275                                         cstarttime, cendtime,
276                                         title, subtitle,
277                                         Category,
278                                         ContentCatList[eitcur->content_type].english);
279
280                 fprintf(outfile, "(%x:%x)%04d/%02d/%02d,%02d:%02d:%02d,%02d:%02d:%02d,%s,%s,%s,%s\n",
281                                         eitcur->table_id, eitcur->event_id,
282                                         eitcur->yy, eitcur->mm, eitcur->dd,
283                                         eitcur->hh, eitcur->hm, eitcur->ss,
284                                         eitcur->ehh, eitcur->emm, eitcur->ess,
285                                         eitcur->title, eitcur->subtitle,
286                                         ContentCatList[eitcur->content_type].japanese,
287                                         ContentCatList[eitcur->content_type].english);
288 #endif
289                 eitnext = eitcur->next ;
290                 free(eitcur->title);
291                 free(eitcur->subtitle);
292                 free(eitcur);
293                 eitcur = eitnext ;
294         }
295         free(eittop);
296         eittop = NULL;
297 }
298
299 void checkSta(STATION **station,int *stalength){
300         STATION *statmp;
301         int chl[90];
302         int chlt = 0;
303         int stal = 0;
304         STATION * statin = *station;
305         statmp = malloc( sizeof(STATION) * 2 );
306         for (int i = 0 ; i < *stalength ; i++){
307                 int noidinchl = 1;
308                 for (int j = 0 ; j < chlt ; j++){
309                         if ( chl[j] == statin[i].svId ) {
310                                 noidinchl = 0;
311                         }
312                 }
313                 if ( noidinchl == 1 ) {
314                         statmp = realloc(statmp, (stal+1) * sizeof(STATION));
315                         statmp[stal] = statin[i];
316                         chl[chlt] = statin[i].svId;
317                         chlt++;
318                         stal++;
319                 }
320         }
321         *station = statmp;
322         *stalength = stal;//ここいらが怪しい
323         //memcpy(statin,statmp,chlt*sizeof(STATION));
324         //free(statmp);
325         return;
326 }
327
328 int main(int argc, char *argv[])
329 {
330
331         FILE *infile = stdin;
332         FILE *outfile = stdout;
333         char    *arg_onTV ;
334         int             staCount ;
335         int   inclose = 0;
336         int   outclose = 0;
337         SVT_CONTROL     *svtcur ;
338         SVT_CONTROL     *svtsave ;
339         SECcache   secs[SECCOUNT];
340         int             lp ;
341         STATION *pStas ;
342         int             act = 0;
343         int             i , j, k ;
344         int             is_logo = 0;
345         SDTTdata  sdtd;
346         SDTTdataLoop *loop;
347         SDTTdataService *service;
348
349         memset(dsmctl, 0,  sizeof(dsmctl));
350
351         if(argc == 5 && strcmp(argv[1], "/LOGO") == 0){
352                 argc--;
353                 argv[1] = argv[2];
354                 argv[2] = argv[3];
355                 argv[3] = argv[4];
356                 is_logo = 1;
357         }
358         if(argc == 4){
359                 arg_onTV = argv[1];
360                 if(strcmp(argv[2], "-")) {
361                         infile = fopen(argv[2], "r");
362                         if ( !infile) {
363                           printf( "tsFile not found (Can't open file: %s)\n", argv[2] );
364                           exit( -1 );
365                         }
366                         inclose = 1;
367                 }
368                 else {
369                         infile = stdin;
370                 }
371                 if(strcmp(argv[3], "-")) {
372                         outfile = fopen(argv[3], "w+");
373                         if ( !outfile) {
374                           printf( "xmlFile not found (Can't open file: %s)\n", argv[3] );
375                           exit( -1 );
376                         }
377                         outclose = 1;
378                 }
379                 else {
380                         outfile = stdout;
381                 }
382         }else{
383                 fprintf(stdout, "Usage : %s (/LOGO) {/BS|/CS|<id>} <tsFile> <outfile>\n", argv[0]);
384                 fprintf(stdout, "\n");
385                 fprintf(stdout, "/LOGO    ロゴ取得モード。独立して指定し、番組表の出力を行ないません。\n");
386                 fprintf(stdout, "id       チャンネル識別子。地上波の物理チャンネルを与えます。\n");
387                 fprintf(stdout, "/BS      BSモード。一つのTSからBS全局のデータを読み込みます。\n");
388                 fprintf(stdout, "/CS      CSモード。一つのTSから複数局のデータを読み込みます。\n");
389                 fprintf(stdout, "/TIME    時刻合わせモード。TSからTOT(Time Offset Table)を読み込みます。\n");
390                 fprintf(stdout, "         recpt1 <任意> 10(秒以上) - | epgdump /TIME - <任意>の形で使用してください。\n");
391                 fprintf(stdout, "         TOTは5秒に1回しか来ないため、recpt1に与える時間をある程度長くしてください。\n");
392 /*
393                 fprintf(stdout, "  ontvcode   Channel identifier (ex. ****.ontvjapan.com)\n");
394                 fprintf(stdout, "  /BS        BS mode\n");
395                 fprintf(stdout, "               This mode reads the data of all BS TV stations\n");
396                 fprintf(stdout, "               from one TS data.\n");
397                 fprintf(stdout, "  /CS        CS mode\n");
398                 fprintf(stdout, "               This mode reads the data of two or more CS TV stations\n");
399                 fprintf(stdout, "               from one TS data.\n");
400 */
401                 return 0;
402         }
403
404         pStas = NULL;
405         staCount = 0;
406         svttop = calloc(1, sizeof(SVT_CONTROL));
407         act = 0 ;
408
409         /* 興味のあるpidを指定 */
410         if ( is_logo ) {
411                 memset(secs, 0,  sizeof(SECcache) * SECCOUNT);
412                 secs[0].pid = 0x00; /* PAT  */
413                 secs[1].pid = 0x11; /* SDT  */
414                 secs[2].pid = 0x29; /* CDT  */
415         }
416         else {
417                 memset(secs, 0,  sizeof(SECcache) * SECCOUNT);
418                 secs[0].pid = 0x00; /* PAT  */
419                 secs[1].pid = 0x11; /* SDT  */
420                 secs[2].pid = 0x12; /* EIT  */
421                 secs[3].pid = 0x23; /* SDTT */
422                 secs[4].pid = 0x26; /* EIT  */
423                 secs[5].pid = 0x27; /* EIT  */
424                 secs[6].pid = 0x29; /* CDT  */
425         }
426
427         if(strcmp(arg_onTV, "/TIME") == 0){
428                 printf("TSに載っている時刻データは2秒ほど早めてあるのかもしれません。\n");
429                 memset(secs, 0,  sizeof(SECcache) * SECCOUNT);
430                 secs[0].pid = 0x14; /* TOT  */
431
432                 GetSDT(infile, NULL, secs, SECCOUNT,NULL, NULL,NULL, 0);
433
434                 goto cleanup;
435         }else if(strcmp(arg_onTV, "/BS") == 0){
436                 char *head = "BS";
437                 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, is_logo);
438         }else if(strcmp(arg_onTV, "/CS") == 0){
439                 char *head = "CS";
440                 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, is_logo);
441         }else if(strcmp(arg_onTV, "/TEST") == 0){
442                 char *head = "TEST";
443                 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, head, 0);
444                 //if (sta_count) 
445                 //      printf("Station count: %d\n1st ontv=%s,name=%s\n",staCount, pStas[0].ontv, pStas[0].name);
446         }else{
447                 GetSDT(infile, svttop, secs, SECCOUNT, &pStas, &staCount, arg_onTV, 0);
448
449                 // 地上波のマルチチャンネル対応のためコメントアウト
450                 /*
451                 act = 1 ;
452                 svttop = calloc(1, sizeof(SVT_CONTROL));
453                 GetSDT(infile, svttop, secs, SECCOUNT);
454                 svtcur = svttop->next ; //先頭
455                 if(svtcur == NULL){
456                         free(svttop);
457                         return 1;
458                 }
459
460                 pStas = calloc(1, sizeof(STATION));
461                 pStas->tsId = svtcur->transport_stream_id ;
462                 pStas->onId = svtcur->original_network_id ;
463                 pStas->svId = svtcur->event_id ;
464                 pStas->ontv = arg_onTV ;
465                 pStas->name = svtcur->servicename ;
466                 staCount = 1;
467                 */
468         }
469         checkSta(&pStas, &staCount);
470
471         fprintf(outfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
472         fprintf(outfile, "<!DOCTYPE tv SYSTEM \"xmltv.dtd\">\n\n");
473         fprintf(outfile, "<tv generator-info-name=\"tsEPG2xml\" generator-info-url=\"http://localhost/\">\n");
474
475         char    ServiceName[1024];
476         char    Logo[8192];
477
478         if ( is_logo ) {
479                 memset(Logo, '\0', sizeof(Logo));
480                 for(lp = 0 ; lp < staCount ; lp++){
481                         for ( i = 0 ; i < 6 ; i++) {
482                                 if (pStas[lp].logo_array[i].logo) {
483                                         base64_encode(Logo, pStas[lp].logo_array[i].logo, pStas[lp].logo_array[i].logo_size);
484                                         xmlspecialchars(Logo);
485                                         fprintf(outfile, "    <logo ts=\"%d\" on=\"%d\" sv=\"%d\" type=\"%d\">%s</logo>\n", 
486                                                 pStas[lp].tsId, 
487                                                 pStas[lp].onId, 
488                                                 pStas[lp].svId, 
489                                                 i, 
490                                                 Logo);
491                                 }
492                         }
493                 }
494
495                 for ( i = 0; i < 1024; i++) {
496                         if ( dsmctl[i].isUsed == 0 ) break;
497                         parseSDTTdata(dsmctl[i].blockData, &sdtd);
498
499                         for (j = 0; j < sdtd.number_of_loop; j++) {
500                                 loop = sdtd.loop + sizeof(SDTTdataLoop) * j;
501
502                                 for ( k = 0; k < loop->number_of_services; k++) {
503                                         service = loop->services + sizeof(SDTTdataService) * k;
504
505                                         /*
506                                         for(lp = 0 ; lp < staCount ; lp++){
507                                                 if ( 
508                                                         pStas[lp].tsId == service->transport_stream_id && 
509                                                         pStas[lp].onId == service->original_network_id && 
510                                                         pStas[lp].svId == service->service_id
511                                                 ) {
512                                                         clt2png(loop->data, 
513                                                                 &pStas[lp].logo_array[sdtd.logo_type].logo, 
514                                                                 &pStas[lp].logo_array[sdtd.logo_type].logo_size);
515                                                 }
516                                         }
517                                         */
518
519                                         #if 0
520                                         printf( "SDTTdataLoop (%d:%d) %d:%d[%d:%d:%d]%d\n", 
521                                                 i, j, 
522                                                 sdtd.logo_type, 
523                                                 loop->logo_id, 
524                                                 service->transport_stream_id, 
525                                                 service->original_network_id, 
526                                                 service->service_id, 
527                                                 loop->data_size
528                                         );
529                                         #endif
530
531                                 
532                                         void* logo = NULL;
533                                         int logo_size = 0;
534
535                                         clt2png(loop->data, &logo, &logo_size);
536                                         memset(Logo, '\0', sizeof(Logo));
537                                         base64_encode(Logo, logo, logo_size);
538                                         xmlspecialchars(Logo);
539
540                                         fprintf(outfile, "  <logo ts=\"%d\" on=\"%d\" sv=\"%d\" type=\"%d\" dlid=\"%d\">%s</logo>\n", 
541                                                 service->transport_stream_id, 
542                                                 service->original_network_id, 
543                                                 service->service_id, 
544                                                 sdtd.logo_type, 
545                                                 loop->logo_id, 
546                                                 Logo);
547                                 }
548
549                         }
550                 }
551         }
552         for(lp = 0 ; lp < staCount ; lp++){
553                 memset(ServiceName, '\0', sizeof(ServiceName));
554                 strcpy(ServiceName, pStas[lp].name);
555                 xmlspecialchars(ServiceName);
556
557                 fprintf(outfile, "  <channel id=\"%s\">\n", pStas[lp].ontv);
558                 fprintf(outfile, "    <display-name lang=\"ja_JP\">%s</display-name>\n", ServiceName);
559                 fprintf(outfile, "    <id ts=\"%d\" on=\"%d\" sv=\"%d\"/>\n", pStas[lp].tsId, pStas[lp].onId, pStas[lp].svId);
560
561                 fprintf(outfile, "  </channel>\n");
562         }
563         if ( is_logo ) {
564                 fprintf(outfile, "</tv>\n");
565                 goto cleanup;
566         }
567         for(lp = 0 ; lp < staCount ; lp++){
568                 GetEIT(infile, outfile, &pStas[lp], secs);
569         }
570         fprintf(outfile, "</tv>\n");
571 cleanup: 
572         if(inclose) {
573                 fclose(infile);
574         }
575         if(outclose) {
576                 fclose(outfile);
577         }
578         if(act){
579                 free(pStas);
580                 svtcur = svttop ;       //先頭
581                 while(svtcur != NULL){
582                         svtsave = svtcur->next ;
583                         free(svtcur);
584                         svtcur = svtsave ;
585                 }
586         }
587
588         return 0;
589 }
590