Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/encode/char_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,17 @@ inline ssize_t utf16le_to_utf8(const unsigned char *utf16le_data, size_t utf16le

// Check buffer space *before* writing
// Need space for the bytes + 1 for potential null terminator later
if (utf8_idx + bytes_needed >= utf8_buf_len) { // Use >= to ensure space for null term
fprintf(stderr, "Error: Output UTF-8 buffer (size %zu) too small. Needs at least %zu bytes (+ null).\n",
utf8_buf_len, utf8_idx + bytes_needed + 1);
return -1; // Buffer overflow
}
if (utf8_idx + bytes_needed >= utf8_buf_len) {
size_t new_len = (utf8_idx + bytes_needed) * 2; // grow a bit more to reduce future reallocs
char* new_buf = (char*)realloc(utf8_buf, new_len);
if (!new_buf) {
fprintf(stderr, "Error: Failed to allocate memory for UTF-8 buffer\n");
return -1;
}
utf8_buf = reinterpret_cast<unsigned char*>(new_buf);
utf8_buf_len = new_len;
}


// Write the UTF-8 bytes
if (bytes_needed == 1) {
Expand Down
46 changes: 32 additions & 14 deletions src/mdict_extern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,48 @@ void *mdict_init(const char *dictionary_path) {
/**
lookup a word
*/

void mdict_lookup(void *dict, const char *word, char **result) {
auto *self = (mdict::Mdict *)dict;
std::string queryWord(word);
std::string s = self->lookup(queryWord);
auto *self = (mdict::Mdict *)dict;
std::string queryWord(word);

(*result) = (char *)calloc(sizeof(char), s.size() + 1);
std::copy(s.begin(), s.end(), (*result));
(*result)[s.size()] = '\0';
std::string s = self->lookup(queryWord);

// Create vector with null terminator
std::vector<char> buf(s.begin(), s.end());
buf.push_back('\0');

// Allocate result buffer once, copy vector content
*result = (char*)malloc(buf.size());
if (!*result) {
perror("malloc");
return;
}
memcpy(*result, buf.data(), buf.size());
}


/**
locate a word
*/
void mdict_locate(void *dict, const char *word, char **result,
mdict_encoding_t encoding) {
auto *self = (mdict::Mdict *)dict;
std::string queryWord(word);
std::string s = self->locate(queryWord, encoding);
void mdict_locate(void *dict, const char *word, char **result, mdict_encoding_t encoding) {
auto *self = (mdict::Mdict *)dict;
std::string queryWord(word);

(*result) = (char *)calloc(sizeof(char), s.size() + 1);
std::copy(s.begin(), s.end(), (*result));
(*result)[s.size()] = '\0';
std::string s = self->locate(queryWord, encoding);

std::vector<char> buf(s.begin(), s.end());
buf.push_back('\0');

*result = (char*)malloc(buf.size());
if (!*result) {
perror("malloc");
return;
}
memcpy(*result, buf.data(), buf.size());
}


void mdict_parse_definition(void *dict, const char *word,
unsigned long record_start, char **result) {
auto *self = (mdict::Mdict *)dict;
Expand Down
Loading