xiph_metadata.c 19.4 KB
Newer Older
1
/*****************************************************************************
2
 * xiph_metadata.h: Vorbis Comment parser
3
 *****************************************************************************
4
 * Copyright © 2008-2013 VLC authors and VideoLAN
5 6 7
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8
 *          Jean-Baptiste Kempf <jb@videolan.org>
9
 *
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (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
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24
 *****************************************************************************/

25 26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

29 30
#include <assert.h>

31
#include <vlc_common.h>
32
#include <vlc_charset.h>
33
#include <vlc_strings.h>
34
#include <vlc_arrays.h>
35
#include <vlc_input.h>
36
#include "xiph_metadata.h"
37

38
input_attachment_t* ParseFlacPicture( const uint8_t *p_data, size_t size,
39
    int i_attachments, int *i_cover_score, int *i_cover_idx )
40
{
41
    /* TODO: Merge with ID3v2 copy in modules/meta_engine/taglib.cpp. */
42
    static const char pi_cover_score[] = {
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        0,  /* Other */
        5,  /* 32x32 PNG image that should be used as the file icon */
        4,  /* File icon of a different size or format. */
        20, /* Front cover image of the album. */
        19, /* Back cover image of the album. */
        13, /* Inside leaflet page of the album. */
        18, /* Image from the album itself. */
        17, /* Picture of the lead artist or soloist. */
        16, /* Picture of the artist or performer. */
        14, /* Picture of the conductor. */
        15, /* Picture of the band or orchestra. */
        9,  /* Picture of the composer. */
        8,  /* Picture of the lyricist or text writer. */
        7,  /* Picture of the recording location or studio. */
        10, /* Picture of the artists during recording. */
        11, /* Picture of the artists during performance. */
        6,  /* Picture from a movie or video related to the track. */
        1,  /* Picture of a large, coloured fish. */
        12, /* Illustration related to the track. */
        3,  /* Logo of the band or performer. */
        2   /* Logo of the publisher (record company). */
64 65
    };

66
    uint32_t type, len;
67

68
    if( size < 8 )
69
        return NULL;
70 71 72 73 74 75
#define RM(x) \
    do { \
        assert(size >= (x)); \
        size -= (x); \
        p_data += (x); \
    } while (0)
76

77 78 79 80 81 82 83
    type = GetDWBE( p_data );
    RM(4);
    len = GetDWBE( p_data );
    RM(4);

    if( size < len )
        return NULL;
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    char *mime = strndup( (const char *)p_data, len );
    if( unlikely(mime == NULL) )
        return NULL;
    RM(len);

    if( size < 4 )
    {
        free( mime );
        return NULL;
    }

    len = GetDWBE( p_data );
    RM(4);

    if( size < len )
    {
        free( mime );
        return NULL;
    }

    input_attachment_t *p_attachment = NULL;
    char *description = strndup( (const char *)p_data, len );
    if( unlikely(description == NULL) )
108
        goto error;
109 110 111 112 113
    RM(len);

    EnsureUTF8( description );

    if( size < 20 )
114
        goto error;
115 116 117 118 119 120 121

    RM(4 * 4); /* skip */

    len = GetDWBE( p_data );
    RM(4);

    if( size < len )
122 123
        goto error;

124 125 126 127
    /* printf( "Picture type=%"PRIu32" mime=%s description='%s' "
               "file length=%zu\n", type, mime, description, len ); */

    char name[7 + (sizeof (i_attachments) * 3) + 4 + 1];
128

129
    snprintf( name, sizeof (name), "picture%u", i_attachments );
130

131 132 133 134
    if( !strcasecmp( mime, "image/jpeg" ) )
        strcat( name, ".jpg" );
    else if( !strcasecmp( mime, "image/png" ) )
        strcat( name, ".png" );
135

136 137 138
    p_attachment = vlc_input_attachment_New( name, mime, description, p_data,
                                             size /* XXX: len instead? */ );

139
    if( type < ARRAY_SIZE(pi_cover_score) &&
140
        *i_cover_score < pi_cover_score[type] )
141 142
    {
        *i_cover_idx = i_attachments;
143
        *i_cover_score = pi_cover_score[type];
144 145
    }

146
error:
147 148
    free( mime );
    free( description );
149 150 151
    return p_attachment;
}

152 153 154 155 156 157 158 159
#undef RM
#define RM(x) \
    do { \
        i_data -= (x); \
        p_data += (x); \
    } while (0)


160 161
typedef struct chapters_array_t
{
162 163
    unsigned int i_size;
    seekpoint_t ** pp_chapters;
164 165
} chapters_array_t;

166
static seekpoint_t * getChapterEntry( unsigned int i_index, chapters_array_t *p_array )
167
{
168
    if ( i_index > 4096 ) return NULL;
169
    if ( i_index >= p_array->i_size )
170
    {
171 172 173 174
        unsigned int i_newsize = p_array->i_size;
        while( i_index >= i_newsize ) i_newsize += 50;

        if ( !p_array->pp_chapters )
175
        {
176 177 178 179 180 181 182 183 184 185
            p_array->pp_chapters = calloc( i_newsize, sizeof( seekpoint_t * ) );
            if ( !p_array->pp_chapters ) return NULL;
            p_array->i_size = i_newsize;
        } else {
            seekpoint_t **tmp = calloc( i_newsize, sizeof( seekpoint_t * ) );
            if ( !tmp ) return NULL;
            memcpy( tmp, p_array->pp_chapters, p_array->i_size * sizeof( seekpoint_t * ) );
            free( p_array->pp_chapters );
            p_array->pp_chapters = tmp;
            p_array->i_size = i_newsize;
186 187
        }
    }
188 189 190
    if ( !p_array->pp_chapters[i_index] )
        p_array->pp_chapters[i_index] = vlc_seekpoint_New();
    return p_array->pp_chapters[i_index];
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206
#define XIPHMETA_Title        (1 << 0)
#define XIPHMETA_Artist       (1 << 1)
#define XIPHMETA_Genre        (1 << 2)
#define XIPHMETA_Copyright    (1 << 3)
#define XIPHMETA_Album        (1 << 4)
#define XIPHMETA_TrackNum     (1 << 5)
#define XIPHMETA_Description  (1 << 6)
#define XIPHMETA_Rating       (1 << 7)
#define XIPHMETA_Date         (1 << 8)
#define XIPHMETA_Language     (1 << 9)
#define XIPHMETA_Publisher    (1 << 10)
#define XIPHMETA_EncodedBy    (1 << 11)
#define XIPHMETA_TrackTotal   (1 << 12)

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static char * xiph_ExtractCueSheetMeta( const char *psz_line,
                                        const char *psz_tag, int i_tag,
                                        bool b_quoted )
{
    if( !strncasecmp( psz_line, psz_tag, i_tag ) )
    {
        if( !b_quoted )
            return strdup( &psz_line[i_tag] );

        /* Unquote string value */
        char *psz_value = malloc( strlen( psz_line ) - i_tag + 1 );
        if( psz_value )
        {
            char *psz_out = psz_value;
            psz_line += i_tag;
            bool b_escaped = false;
            while( *psz_line )
            {
                switch( *psz_line )
                {
                    case '\\':
                        if( b_escaped )
                        {
                            b_escaped = false;
                            *(psz_out++) = *psz_line;
                        }
                        else
                        {
                            b_escaped = true;
                        }
                        break;
                    case '"':
                        if( b_escaped )
                        {
                            b_escaped = false;
                            *(psz_out++) = *psz_line;
                        }
                        break;
                    default:
                        *(psz_out++) = *psz_line;
                        break;
                }
                psz_line++;
            }
            *psz_out = 0;
            return psz_value;
        }
    }
    return NULL;
}

static void xiph_ParseCueSheetMeta( unsigned *pi_flags, vlc_meta_t *p_meta,
                                    const char *psz_line,
                                    int *pi_seekpoint, seekpoint_t ***ppp_seekpoint,
                                    seekpoint_t **pp_tmppoint, bool *pb_valid )
{
    VLC_UNUSED(pi_seekpoint);
    VLC_UNUSED(ppp_seekpoint);

    seekpoint_t *p_seekpoint = *pp_tmppoint;
    char *psz_string;

#define TRY_EXTRACT_CUEMETA(var, string, quoted) \
    if( !(*pi_flags & XIPHMETA_##var) &&\
         ( psz_string = xiph_ExtractCueSheetMeta( psz_line, string, sizeof(string) - 1, quoted ) ) )\
    {\
        vlc_meta_Set( p_meta, vlc_meta_##var, psz_string );\
        free( psz_string );\
        *pi_flags |= XIPHMETA_##var;\
    }

    TRY_EXTRACT_CUEMETA(Title, "TITLE \"", true)
    else TRY_EXTRACT_CUEMETA(Genre, "REM GENRE ", false)
    else TRY_EXTRACT_CUEMETA(Date, "REM DATE ", false)
    else TRY_EXTRACT_CUEMETA(Artist, "PERFORMER \"", true)
    else if( !strncasecmp( psz_line, "  TRACK ", 8 ) )
    {
        if( p_seekpoint )
        {
            if( *pb_valid )
                TAB_APPEND( *pi_seekpoint, *ppp_seekpoint, p_seekpoint );
            else
                vlc_seekpoint_Delete( p_seekpoint );
            *pb_valid = false;
        }
        *pp_tmppoint = p_seekpoint = vlc_seekpoint_New();
    }
    else if( p_seekpoint && !strncasecmp( psz_line, "    INDEX 01 ", 13 ) )
    {
        unsigned m, s, f;
        if( sscanf( &psz_line[13], "%u:%u:%u", &m, &s, &f ) == 3 )
        {
            p_seekpoint->i_time_offset = CLOCK_FREQ * (m * 60 + s) + f * CLOCK_FREQ/75;
            *pb_valid = true;
        }
    }
    else if( p_seekpoint && !p_seekpoint->psz_name )
    {
        p_seekpoint->psz_name = xiph_ExtractCueSheetMeta( psz_line, "    TITLE \"", 11, true );
    }
}

static void xiph_ParseCueSheet( unsigned *pi_flags, vlc_meta_t *p_meta,
                                const char *p_data, int i_data,
                                int *pi_seekpoint, seekpoint_t ***ppp_seekpoint )
{
    seekpoint_t *p_seekpoint = NULL;
    bool b_valid = false;

    const char *p_head = p_data;
    const char *p_tail = p_head;
    while( p_tail < p_data + i_data )
    {
        if( *p_tail == 0x0D )
        {
            char *psz = strndup( p_head, p_tail - p_head );
            if( psz )
            {
                xiph_ParseCueSheetMeta( pi_flags, p_meta, psz,
                                        pi_seekpoint, ppp_seekpoint,
                                        &p_seekpoint, &b_valid );
                free( psz );
            }
            if( *(++p_tail) == 0x0A )
                p_tail++;
            p_head = p_tail;
        }
        else
        {
            p_tail++;
        }
    }


    if( p_seekpoint )
    {
        if( b_valid )
            TAB_APPEND( *pi_seekpoint, *ppp_seekpoint, p_seekpoint );
        else
            vlc_seekpoint_Delete( p_seekpoint );
    }
}

350
void vorbis_ParseComment( es_format_t *p_fmt, vlc_meta_t **pp_meta,
351
        const uint8_t *p_data, size_t i_data,
352
        int *i_attachments, input_attachment_t ***attachments,
353
        int *i_cover_score, int *i_cover_idx,
354 355 356
        int *i_seekpoint, seekpoint_t ***ppp_seekpoint,
        float (* ppf_replay_gain)[AUDIO_REPLAY_GAIN_MAX],
        float (* ppf_replay_peak)[AUDIO_REPLAY_GAIN_MAX] )
357 358 359 360
{
    if( i_data < 8 )
        return;

361 362 363 364 365 366
    uint32_t vendor_length = GetDWLE(p_data); RM(4);

    if( vendor_length > i_data )
        return; /* invalid length */

    RM(vendor_length); /* TODO: handle vendor payload */
367 368 369 370

    if( i_data < 4 )
        return;

371 372 373 374
    uint32_t i_comment = GetDWLE(p_data); RM(4);

    if( i_comment > i_data || i_comment == 0 )
        return; /* invalid length */
375 376 377 378 379

    /* */
    vlc_meta_t *p_meta = *pp_meta;
    if( !p_meta )
        *pp_meta = p_meta = vlc_meta_New();
380 381

    if( unlikely( !p_meta ) )
382 383
        return;

384
    /* */
385
    unsigned hasMetaFlags = 0;
386

387
    chapters_array_t chapters_array = { 0, NULL };
388

389
    for( ; i_comment > 0 && i_data >= 4; i_comment-- )
390
    {
391 392 393
        uint32_t comment_size = GetDWLE(p_data); RM(4);

        if( comment_size > i_data )
394
            break;
395 396

        if( comment_size == 0 )
397 398
            continue;

399
        char* psz_comment = malloc( comment_size + 1 );
400

401 402 403 404 405
        if( unlikely( !psz_comment ) )
            goto next_comment;

        memcpy( psz_comment, p_data, comment_size );
        psz_comment[comment_size] = '\0';
406

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
407
        EnsureUTF8( psz_comment );
408 409

#define IF_EXTRACT(txt,var) \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
410
    if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
411 412
    { \
        const char *oldval = vlc_meta_Get( p_meta, vlc_meta_ ## var ); \
413
        if( oldval && (hasMetaFlags & XIPHMETA_##var)) \
414 415
        { \
            char * newval; \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
416
            if( asprintf( &newval, "%s,%s", oldval, &psz_comment[strlen(txt)] ) == -1 ) \
417 418 419 420 421
                newval = NULL; \
            vlc_meta_Set( p_meta, vlc_meta_ ## var, newval ); \
            free( newval ); \
        } \
        else \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
422
            vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
423
        hasMetaFlags |= XIPHMETA_##var; \
424
    }
425 426

#define IF_EXTRACT_ONCE(txt,var) \
427
    if( !strncasecmp(psz_comment, txt, strlen(txt)) && !(hasMetaFlags & XIPHMETA_##var) ) \
428 429
    { \
        vlc_meta_Set( p_meta, vlc_meta_ ## var, &psz_comment[strlen(txt)] ); \
430
        hasMetaFlags |= XIPHMETA_##var; \
431 432
    }

433
#define IF_EXTRACT_FMT(txt,var,fmt,target) \
434 435 436 437
    if( !strncasecmp(psz_comment, txt, strlen(txt)) ) \
    { \
        IF_EXTRACT(txt,var)\
        if( fmt )\
438
        {\
439
            free( fmt->target );\
440
            fmt->target = strdup(&psz_comment[strlen(txt)]);\
441 442
        }\
    }
443

444
        IF_EXTRACT("TITLE=", Title )
.'s avatar
. committed
445 446 447
        else IF_EXTRACT("ARTIST=", Artist )
        else IF_EXTRACT("GENRE=", Genre )
        else IF_EXTRACT("COPYRIGHT=", Copyright )
448
        else IF_EXTRACT("ALBUM=", Album )
449
        else if( !(hasMetaFlags & XIPHMETA_TrackNum) && !strncasecmp(psz_comment, "TRACKNUMBER=", strlen("TRACKNUMBER=" ) ) )
450 451 452 453
        {
            /* Yeah yeah, such a clever idea, let's put xx/xx inside TRACKNUMBER
             * Oh, and let's not use TRACKTOTAL or TOTALTRACKS... */
            short unsigned u_track, u_total;
454 455
            int nb_values = sscanf( &psz_comment[strlen("TRACKNUMBER=")], "%hu/%hu", &u_track, &u_total );
            if( nb_values >= 1 )
456 457
            {
                char str[6];
458
                snprintf(str, 6, "%u", u_track);
459
                vlc_meta_Set( p_meta, vlc_meta_TrackNumber, str );
460
                hasMetaFlags |= XIPHMETA_TrackNum;
461 462 463 464 465 466
                if( nb_values >= 2 )
                {
                    snprintf(str, 6, "%u", u_total);
                    vlc_meta_Set( p_meta, vlc_meta_TrackTotal, str );
                    hasMetaFlags |= XIPHMETA_TrackTotal;
                }
467
            }
468
        }
469 470
        else IF_EXTRACT_ONCE("TRACKTOTAL=", TrackTotal )
        else IF_EXTRACT_ONCE("TOTALTRACKS=", TrackTotal )
471
        else IF_EXTRACT("DESCRIPTION=", Description )
472
        else IF_EXTRACT("COMMENT=", Description )
473
        else IF_EXTRACT("COMMENTS=", Description )
474
        else IF_EXTRACT("RATING=", Rating )
475
        else IF_EXTRACT("DATE=", Date )
476
        else IF_EXTRACT_FMT("LANGUAGE=", Language, p_fmt, psz_language )
.'s avatar
. committed
477
        else IF_EXTRACT("ORGANIZATION=", Publisher )
478
        else IF_EXTRACT("ENCODER=", EncodedBy )
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
479
        else if( !strncasecmp( psz_comment, "METADATA_BLOCK_PICTURE=", strlen("METADATA_BLOCK_PICTURE=")))
480
        {
481
            if( attachments == NULL )
482
                goto next_comment;
483

484
            uint8_t *p_picture;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
485
            size_t i_size = vlc_b64_decode_binary( &p_picture, &psz_comment[strlen("METADATA_BLOCK_PICTURE=")]);
486 487 488
            input_attachment_t *p_attachment = ParseFlacPicture( p_picture,
                i_size, *i_attachments, i_cover_score, i_cover_idx );
            free( p_picture );
489 490
            if( p_attachment )
            {
491 492
                TAB_APPEND_CAST( (input_attachment_t**),
                    *i_attachments, *attachments, p_attachment );
493 494
            }
        }
495 496 497
        else if ( ppf_replay_gain && ppf_replay_peak && !strncmp(psz_comment, "REPLAYGAIN_", 11) )
        {
            char *p = strchr( psz_comment, '=' );
498
            if (!p) goto next_comment;
499
            if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_GAIN=", 22) )
500
            {
501
                (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( ++p );
502
            }
503
            else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_GAIN=", 22) )
504
            {
505
                (*ppf_replay_gain)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( ++p );
506
            }
507
            else if ( !strncasecmp(psz_comment, "REPLAYGAIN_ALBUM_PEAK=", 22) )
508 509 510
            {
                (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_ALBUM] = us_atof( ++p );
            }
511
            else if ( !strncasecmp(psz_comment, "REPLAYGAIN_TRACK_PEAK=", 22) )
512 513 514 515
            {
                (*ppf_replay_peak)[AUDIO_REPLAY_GAIN_TRACK] = us_atof( ++p );
            }
        }
516
        else if( !strncasecmp(psz_comment, "CHAPTER", 7) )
517
        {
518 519
            unsigned int i_chapt;
            seekpoint_t *p_seekpoint = NULL;
520 521 522 523 524 525 526

            for( int i = 0; psz_comment[i] && psz_comment[i] != '='; i++ )
                if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
                    psz_comment[i] -= 'a' - 'A';

            if( strstr( psz_comment, "NAME=" ) &&
                    sscanf( psz_comment, "CHAPTER%uNAME=", &i_chapt ) == 1 )
527 528
            {
                char *p = strchr( psz_comment, '=' );
529
                p_seekpoint = getChapterEntry( i_chapt, &chapters_array );
530
                if ( !p || ! p_seekpoint ) goto next_comment;
531 532
                if ( ! p_seekpoint->psz_name )
                    p_seekpoint->psz_name = strdup( ++p );
533
            }
534
            else if( sscanf( psz_comment, "CHAPTER%u=", &i_chapt ) == 1 )
535
            {
536
                unsigned int h, m, s, ms;
537
                char *p = strchr( psz_comment, '=' );
538
                if( p && sscanf( ++p, "%u:%u:%u.%u", &h, &m, &s, &ms ) == 4 )
539
                {
540
                    p_seekpoint = getChapterEntry( i_chapt, &chapters_array );
541
                    if ( ! p_seekpoint ) goto next_comment;
542
                    p_seekpoint->i_time_offset =
543
                      (((int64_t)h * 3600 + (int64_t)m * 60 + (int64_t)s) * 1000 + ms) * 1000;
544 545 546
                }
            }
        }
547 548
        else if( !strncasecmp(psz_comment, "cuesheet=", 9) )
        {
549
            xiph_ParseCueSheet( &hasMetaFlags, p_meta, &psz_comment[9], comment_size - 9,
550 551
                                i_seekpoint, ppp_seekpoint );
        }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
552
        else if( strchr( psz_comment, '=' ) )
553 554 555
        {
            /* generic (PERFORMER/LICENSE/ORGANIZATION/LOCATION/CONTACT/ISRC,
             * undocumented tags and replay gain ) */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
556
            char *p = strchr( psz_comment, '=' );
557
            *p++ = '\0';
558 559 560 561 562

            for( int i = 0; psz_comment[i]; i++ )
                if( psz_comment[i] >= 'a' && psz_comment[i] <= 'z' )
                    psz_comment[i] -= 'a' - 'A';

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
563
            vlc_meta_AddExtra( p_meta, psz_comment, p );
564 565
        }
#undef IF_EXTRACT
566
next_comment:
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
567
        free( psz_comment );
568
        RM( comment_size );
569 570
    }
#undef RM
571

572
    for ( unsigned int i=0; i<chapters_array.i_size; i++ )
573
    {
574
        if ( !chapters_array.pp_chapters[i] ) continue;
575
        TAB_APPEND_CAST( (seekpoint_t**), *i_seekpoint, *ppp_seekpoint,
576
                         chapters_array.pp_chapters[i] );
577
    }
578
    free( chapters_array.pp_chapters );
579 580
}

581 582 583 584 585 586 587 588 589 590
const char *FindKateCategoryName( const char *psz_tag )
{
    for( size_t i = 0; i < sizeof(Katei18nCategories)/sizeof(Katei18nCategories[0]); i++ )
    {
        if( !strcmp( psz_tag, Katei18nCategories[i].psz_tag ) )
            return Katei18nCategories[i].psz_i18n;
    }
    return N_("Unknown category");
}