/*-
* Copyright (c)2003 Citrus Project,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* conv_esdb:
* external representation -> local structure.
*/
static int
conv_esdb(struct _citrus_esdb *esdb, struct _region *fr)
{
int ret;
struct _citrus_db *db;
uint32_t version, num_charsets, csid, i, tmp;
char buf[100];
const char *str;
/* open db */
ret = _db_open(&db, fr, _CITRUS_ESDB_MAGIC, &_db_hash_std, NULL);
if (ret)
goto err0;
/* check version */
ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_VERSION, &version, NULL);
if (ret)
goto err1;
switch (version) {
case 0x00000001:
/* current version */
/* initial version */
break;
default:
ret = EFTYPE;
goto err1;
}
/* get encoding/variable */
ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_ENCODING, &str, NULL);
if (ret)
goto err1;
esdb->db_encname = strdup(str);
if (esdb->db_encname == NULL) {
ret = errno;
goto err1;
}
esdb->db_len_variable = 0;
esdb->db_variable = NULL;
ret = _db_lookupstr_by_s(db, _CITRUS_ESDB_SYM_VARIABLE, &str, NULL);
if (ret == 0) {
esdb->db_len_variable = strlen(str)+1;
esdb->db_variable = strdup(str);
if (esdb->db_variable == NULL) {
ret = errno;
goto err2;
}
} else if (ret != ENOENT)
goto err2;
/* get number of charsets */
ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_NUM_CHARSETS,
&num_charsets, NULL);
if (ret)
goto err3;
esdb->db_num_charsets = num_charsets;
/* get invalid character */
ret = _db_lookup32_by_s(db, _CITRUS_ESDB_SYM_INVALID, &tmp, NULL);
if (ret == 0) {
esdb->db_use_invalid = 1;
esdb->db_invalid = tmp;
} else if (ret == ENOENT)
esdb->db_use_invalid = 0;
else
goto err3;
/* get charsets */
esdb->db_charsets = malloc(num_charsets * sizeof(*esdb->db_charsets));
if (esdb->db_charsets == NULL) {
ret = errno;
goto err3;
}
for (i = 0; i < num_charsets; i++) {
snprintf(buf, sizeof(buf),
_CITRUS_ESDB_SYM_CSID_PREFIX "%d", i);
ret = _db_lookup32_by_s(db, buf, &csid, NULL);
if (ret)
goto err4;
esdb->db_charsets[i].ec_csid = csid;
snprintf(buf, sizeof(buf),
_CITRUS_ESDB_SYM_CSNAME_PREFIX "%d", i);
ret = _db_lookupstr_by_s(db, buf, &str, NULL);
if (ret)
goto err4;
esdb->db_charsets[i].ec_csname = strdup(str);
if (esdb->db_charsets[i].ec_csname == NULL) {
ret = errno;
goto err4;
}
}
_db_close(db);
return 0;
err4:
for (; i > 0; i--)
free(esdb->db_charsets[i - 1].ec_csname);
free(esdb->db_charsets);
err3:
free(esdb->db_variable);
err2:
free(esdb->db_encname);
err1:
_db_close(db);
if (ret == ENOENT)
ret = EFTYPE;
err0:
return ret;
}
/*
* _citrus_esdb_open:
* open an ESDB file.
*/
int
_citrus_esdb_open(struct _citrus_esdb *db, const char *esname)
{
int ret;
const char *realname, *encfile;
char buf1[PATH_MAX], buf2[PATH_MAX], path[PATH_MAX];
struct _region fr;