blob: bba1359616b931d74aa8f88b9470afff859cb1ed [file] [log] [blame]
Daniel Stenberg8d38c962002-09-24 12:39:33 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Daniel Stenberg
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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 Stenberg8d38c962002-09-24 12:39:33 +000016 *
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 Arnold0f040292005-01-19 21:43:15 +000027#include "string.h"
Daniel Stenberg8d38c962002-09-24 12:39:33 +000028
Nils Wallménius4317ff92008-12-03 12:14:46 +000029/* 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 Shalev2a91a9a2009-10-04 22:25:52 +000034/* 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énius4317ff92008-12-03 12:14:46 +000038
Tomer Shalev2a91a9a2009-10-04 22:25:52 +000039#define HEADER_SIZE 3
Nils Wallménius4317ff92008-12-03 12:14:46 +000040
Daniel Stenberg8d38c962002-09-24 12:39:33 +000041static unsigned char language_buffer[MAX_LANGUAGE_SIZE];
42
Jens Arnold0f040292005-01-19 21:43:15 +000043void 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 Stenbergf981ea92005-12-05 22:44:42 +000050 ptr += strlen((char *)ptr) + 1; /* advance pointer to next string */
Jens Arnold0f040292005-01-19 21:43:15 +000051 }
52}
53
Jens Arnold8fb33612004-08-18 01:09:31 +000054int lang_load(const char *filename)
Daniel Stenberg8d38c962002-09-24 12:39:33 +000055{
Marcoen Hirschberg87c6f542005-12-06 00:44:57 +000056 int fsize;
Daniel Stenberg571e8a22002-09-24 12:48:23 +000057 int fd = open(filename, O_RDONLY);
Daniel Stenberg2d8aef92002-09-24 13:53:41 +000058 int retcode=0;
Tomer Shalev2a91a9a2009-10-04 22:25:52 +000059 unsigned char lang_header[HEADER_SIZE];
Nils Wallméniusa0199642008-04-16 19:51:43 +000060 if(fd < 0)
Daniel Stenberg2d8aef92002-09-24 13:53:41 +000061 return 1;
Tomer Shalev2a91a9a2009-10-04 22:25:52 +000062 fsize = filesize(fd) - HEADER_SIZE;
Marcoen Hirschberg87c6f542005-12-06 00:44:57 +000063 if(fsize <= MAX_LANGUAGE_SIZE) {
Tomer Shalev2a91a9a2009-10-04 22:25:52 +000064 read(fd, lang_header, HEADER_SIZE);
Marcoen Hirschberg87c6f542005-12-06 00:44:57 +000065 if((lang_header[0] == LANGUAGE_COOKIE) &&
Nils Wallméniusb3113672007-08-05 19:19:39 +000066 (lang_header[1] == LANGUAGE_VERSION) &&
67 (lang_header[2] == TARGET_ID)) {
Marcoen Hirschberg87c6f542005-12-06 00:44:57 +000068 read(fd, language_buffer, MAX_LANGUAGE_SIZE);
69 unsigned char *ptr = language_buffer;
Daniel Stenberg571e8a22002-09-24 12:48:23 +000070 int id;
Jens Arnoldd66139c2005-01-19 22:27:48 +000071 lang_init(); /* initialize with builtin */
Daniel Stenberg8d38c962002-09-24 12:39:33 +000072
Marcoen Hirschberg87c6f542005-12-06 00:44:57 +000073 while(fsize>3) {
Daniel Stenberg6823fe82002-09-24 12:50:45 +000074 id = (ptr[0]<<8) | ptr[1]; /* get two-byte id */
75 ptr+=2; /* pass the id */
Daniel Stenberg4ce3f722002-10-29 12:31:34 +000076 if(id < LANG_LAST_INDEX_IN_ARRAY) {
Daniel Stenberg5c834872005-02-22 12:55:21 +000077#if 0
Tom Rossaaf3f5f2009-03-02 04:48:53 +000078 DEBUGF("%2x New: %30s ", id, ptr);
79 DEBUGF("Replaces: %s\n", language_strings[id]);
Daniel Stenberg4ce3f722002-10-29 12:31:34 +000080#endif
Daniel Stenbergc7e0bea2002-10-14 12:20:53 +000081 language_strings[id] = ptr; /* point to this string */
Daniel Stenberg4ce3f722002-10-29 12:31:34 +000082 }
Daniel Stenberg6823fe82002-09-24 12:50:45 +000083 while(*ptr) { /* pass the string */
Marcoen Hirschberg87c6f542005-12-06 00:44:57 +000084 fsize--;
Daniel Stenberg571e8a22002-09-24 12:48:23 +000085 ptr++;
86 }
Marcoen Hirschberg87c6f542005-12-06 00:44:57 +000087 fsize-=3; /* the id and the terminating zero */
Daniel Stenberg6823fe82002-09-24 12:50:45 +000088 ptr++; /* pass the terminating zero-byte */
Daniel Stenberg571e8a22002-09-24 12:48:23 +000089 }
Daniel Stenberg8d38c962002-09-24 12:39:33 +000090 }
Daniel Stenberg571e8a22002-09-24 12:48:23 +000091 else {
92 DEBUGF("Illegal language file\n");
Daniel Stenberg2d8aef92002-09-24 13:53:41 +000093 retcode = 2;
Daniel Stenberg571e8a22002-09-24 12:48:23 +000094 }
Daniel Stenberg8d38c962002-09-24 12:39:33 +000095 }
96 else {
Marcoen Hirschberg87c6f542005-12-06 00:44:57 +000097 DEBUGF("Language %s too large: %d\n", filename, fsize);
Daniel Stenberg2d8aef92002-09-24 13:53:41 +000098 retcode = 3;
Daniel Stenberg8d38c962002-09-24 12:39:33 +000099 }
Daniel Stenberg571e8a22002-09-24 12:48:23 +0000100 close(fd);
Daniel Stenberg2d8aef92002-09-24 13:53:41 +0000101 return retcode;
Daniel Stenberg8d38c962002-09-24 12:39:33 +0000102}
Jonathan Gordon340f3232009-09-26 00:58:32 +0000103
104int 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}