Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 Daniel Stenberg |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include <file.h> |
| 23 | |
| 24 | #include "language.h" |
| 25 | #include "lang.h" |
| 26 | #include "debug.h" |
Jens Arnold | 0f04029 | 2005-01-19 21:43:15 +0000 | [diff] [blame] | 27 | #include "string.h" |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 28 | |
Nils Wallménius | 4317ff9 | 2008-12-03 12:14:46 +0000 | [diff] [blame] | 29 | /* The following header is generated by the build system and only defines |
| 30 | MAX_LANGUAGE_SIZE to be the size of the largest currently available |
| 31 | language! */ |
| 32 | #include "max_language_size.h" |
| 33 | |
Tomer Shalev | 2a91a9a | 2009-10-04 22:25:52 +0000 | [diff] [blame] | 34 | /* These defines must match the initial bytes in the binary lang file */ |
| 35 | /* See tools/genlang (TODO: Use common include for both) */ |
| 36 | #define LANGUAGE_COOKIE 0x1a |
| 37 | #define LANGUAGE_VERSION 0x04 |
Nils Wallménius | 4317ff9 | 2008-12-03 12:14:46 +0000 | [diff] [blame] | 38 | |
Tomer Shalev | 2a91a9a | 2009-10-04 22:25:52 +0000 | [diff] [blame] | 39 | #define HEADER_SIZE 3 |
Nils Wallménius | 4317ff9 | 2008-12-03 12:14:46 +0000 | [diff] [blame] | 40 | |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 41 | static unsigned char language_buffer[MAX_LANGUAGE_SIZE]; |
| 42 | |
Jens Arnold | 0f04029 | 2005-01-19 21:43:15 +0000 | [diff] [blame] | 43 | void lang_init(void) |
| 44 | { |
| 45 | int i; |
| 46 | unsigned char *ptr = (unsigned char *) language_builtin; |
| 47 | |
| 48 | for (i = 0; i < LANG_LAST_INDEX_IN_ARRAY; i++) { |
| 49 | language_strings[i] = ptr; |
Daniel Stenberg | f981ea9 | 2005-12-05 22:44:42 +0000 | [diff] [blame] | 50 | ptr += strlen((char *)ptr) + 1; /* advance pointer to next string */ |
Jens Arnold | 0f04029 | 2005-01-19 21:43:15 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Jens Arnold | 8fb3361 | 2004-08-18 01:09:31 +0000 | [diff] [blame] | 54 | int lang_load(const char *filename) |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 55 | { |
Marcoen Hirschberg | 87c6f54 | 2005-12-06 00:44:57 +0000 | [diff] [blame] | 56 | int fsize; |
Daniel Stenberg | 571e8a2 | 2002-09-24 12:48:23 +0000 | [diff] [blame] | 57 | int fd = open(filename, O_RDONLY); |
Daniel Stenberg | 2d8aef9 | 2002-09-24 13:53:41 +0000 | [diff] [blame] | 58 | int retcode=0; |
Tomer Shalev | 2a91a9a | 2009-10-04 22:25:52 +0000 | [diff] [blame] | 59 | unsigned char lang_header[HEADER_SIZE]; |
Nils Wallménius | a019964 | 2008-04-16 19:51:43 +0000 | [diff] [blame] | 60 | if(fd < 0) |
Daniel Stenberg | 2d8aef9 | 2002-09-24 13:53:41 +0000 | [diff] [blame] | 61 | return 1; |
Tomer Shalev | 2a91a9a | 2009-10-04 22:25:52 +0000 | [diff] [blame] | 62 | fsize = filesize(fd) - HEADER_SIZE; |
Marcoen Hirschberg | 87c6f54 | 2005-12-06 00:44:57 +0000 | [diff] [blame] | 63 | if(fsize <= MAX_LANGUAGE_SIZE) { |
Tomer Shalev | 2a91a9a | 2009-10-04 22:25:52 +0000 | [diff] [blame] | 64 | read(fd, lang_header, HEADER_SIZE); |
Marcoen Hirschberg | 87c6f54 | 2005-12-06 00:44:57 +0000 | [diff] [blame] | 65 | if((lang_header[0] == LANGUAGE_COOKIE) && |
Nils Wallménius | b311367 | 2007-08-05 19:19:39 +0000 | [diff] [blame] | 66 | (lang_header[1] == LANGUAGE_VERSION) && |
| 67 | (lang_header[2] == TARGET_ID)) { |
Marcoen Hirschberg | 87c6f54 | 2005-12-06 00:44:57 +0000 | [diff] [blame] | 68 | read(fd, language_buffer, MAX_LANGUAGE_SIZE); |
| 69 | unsigned char *ptr = language_buffer; |
Daniel Stenberg | 571e8a2 | 2002-09-24 12:48:23 +0000 | [diff] [blame] | 70 | int id; |
Jens Arnold | d66139c | 2005-01-19 22:27:48 +0000 | [diff] [blame] | 71 | lang_init(); /* initialize with builtin */ |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 72 | |
Marcoen Hirschberg | 87c6f54 | 2005-12-06 00:44:57 +0000 | [diff] [blame] | 73 | while(fsize>3) { |
Daniel Stenberg | 6823fe8 | 2002-09-24 12:50:45 +0000 | [diff] [blame] | 74 | id = (ptr[0]<<8) | ptr[1]; /* get two-byte id */ |
| 75 | ptr+=2; /* pass the id */ |
Daniel Stenberg | 4ce3f72 | 2002-10-29 12:31:34 +0000 | [diff] [blame] | 76 | if(id < LANG_LAST_INDEX_IN_ARRAY) { |
Daniel Stenberg | 5c83487 | 2005-02-22 12:55:21 +0000 | [diff] [blame] | 77 | #if 0 |
Tom Ross | aaf3f5f | 2009-03-02 04:48:53 +0000 | [diff] [blame] | 78 | DEBUGF("%2x New: %30s ", id, ptr); |
| 79 | DEBUGF("Replaces: %s\n", language_strings[id]); |
Daniel Stenberg | 4ce3f72 | 2002-10-29 12:31:34 +0000 | [diff] [blame] | 80 | #endif |
Daniel Stenberg | c7e0bea | 2002-10-14 12:20:53 +0000 | [diff] [blame] | 81 | language_strings[id] = ptr; /* point to this string */ |
Daniel Stenberg | 4ce3f72 | 2002-10-29 12:31:34 +0000 | [diff] [blame] | 82 | } |
Daniel Stenberg | 6823fe8 | 2002-09-24 12:50:45 +0000 | [diff] [blame] | 83 | while(*ptr) { /* pass the string */ |
Marcoen Hirschberg | 87c6f54 | 2005-12-06 00:44:57 +0000 | [diff] [blame] | 84 | fsize--; |
Daniel Stenberg | 571e8a2 | 2002-09-24 12:48:23 +0000 | [diff] [blame] | 85 | ptr++; |
| 86 | } |
Marcoen Hirschberg | 87c6f54 | 2005-12-06 00:44:57 +0000 | [diff] [blame] | 87 | fsize-=3; /* the id and the terminating zero */ |
Daniel Stenberg | 6823fe8 | 2002-09-24 12:50:45 +0000 | [diff] [blame] | 88 | ptr++; /* pass the terminating zero-byte */ |
Daniel Stenberg | 571e8a2 | 2002-09-24 12:48:23 +0000 | [diff] [blame] | 89 | } |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 90 | } |
Daniel Stenberg | 571e8a2 | 2002-09-24 12:48:23 +0000 | [diff] [blame] | 91 | else { |
| 92 | DEBUGF("Illegal language file\n"); |
Daniel Stenberg | 2d8aef9 | 2002-09-24 13:53:41 +0000 | [diff] [blame] | 93 | retcode = 2; |
Daniel Stenberg | 571e8a2 | 2002-09-24 12:48:23 +0000 | [diff] [blame] | 94 | } |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 95 | } |
| 96 | else { |
Marcoen Hirschberg | 87c6f54 | 2005-12-06 00:44:57 +0000 | [diff] [blame] | 97 | DEBUGF("Language %s too large: %d\n", filename, fsize); |
Daniel Stenberg | 2d8aef9 | 2002-09-24 13:53:41 +0000 | [diff] [blame] | 98 | retcode = 3; |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 99 | } |
Daniel Stenberg | 571e8a2 | 2002-09-24 12:48:23 +0000 | [diff] [blame] | 100 | close(fd); |
Daniel Stenberg | 2d8aef9 | 2002-09-24 13:53:41 +0000 | [diff] [blame] | 101 | return retcode; |
Daniel Stenberg | 8d38c96 | 2002-09-24 12:39:33 +0000 | [diff] [blame] | 102 | } |
Jonathan Gordon | 340f323 | 2009-09-26 00:58:32 +0000 | [diff] [blame] | 103 | |
| 104 | int lang_english_to_id(const char* english) |
| 105 | { |
| 106 | int i; |
| 107 | unsigned char *ptr = (unsigned char *) language_builtin; |
| 108 | |
| 109 | for (i = 0; i < LANG_LAST_INDEX_IN_ARRAY; i++) { |
| 110 | if (!strcmp(ptr, english)) |
| 111 | return i; |
| 112 | ptr += strlen((char *)ptr) + 1; /* advance pointer to next string */ |
| 113 | } |
| 114 | return -1; |
| 115 | } |