summaryrefslogtreecommitdiff
path: root/src/modules/common/zstr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/common/zstr.cpp')
-rw-r--r--src/modules/common/zstr.cpp101
1 files changed, 57 insertions, 44 deletions
diff --git a/src/modules/common/zstr.cpp b/src/modules/common/zstr.cpp
index fc02572..92a4960 100644
--- a/src/modules/common/zstr.cpp
+++ b/src/modules/common/zstr.cpp
@@ -14,7 +14,6 @@
#include <unistd.h>
#endif
-#include <string.h>
#include <stdlib.h>
#include <utilfuns.h>
#include <zstr.h>
@@ -23,6 +22,8 @@
#include <sysdata.h>
#include <entriesblk.h>
+SWORD_NAMESPACE_START
+
/******************************************************************************
* zStr Statics
*/
@@ -124,14 +125,13 @@ void zStr::getKeyFromDatOffset(long ioffset, char **buf) {
if ((ch == '\\') || (ch == 10) || (ch == 13))
break;
}
- *buf = (*buf) ? (char *)realloc(*buf, size + 1) : (char *)malloc(size + 1);
+ *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
if (size) {
lseek(datfd->getFd(), ioffset, SEEK_SET);
read(datfd->getFd(), *buf, size);
}
(*buf)[size] = 0;
- for (size--; size > 0; size--)
- (*buf)[size] = SW_toupper((*buf)[size]);
+ toupperstr_utf8(*buf, size*2);
}
else {
*buf = (*buf) ? (char *)realloc(*buf, 1) : (char *)malloc(1);
@@ -183,8 +183,8 @@ signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) {
tailoff = maxoff = lseek(idxfd->getFd(), 0, SEEK_END) - IDXENTRYSIZE;
if (*ikey) {
headoff = 0;
- stdstr(&key, ikey);
- toupperstr(key);
+ stdstr(&key, ikey, 3);
+ toupperstr_utf8(key, strlen(key)*3);
while (headoff < tailoff) {
tryoff = (lastoff == -1) ? headoff + (((((tailoff / IDXENTRYSIZE) - (headoff / IDXENTRYSIZE))) / 2) * IDXENTRYSIZE) : lastoff;
@@ -281,11 +281,12 @@ signed char zStr::findKeyIndex(const char *ikey, long *idxoff, long away) {
* text.
*/
-void zStr::prepText(char *buf) {
- char *to, *from, space = 0, cr = 0, realdata = 0, nlcnt = 0;
-
- for (to = from = buf; *from; from++) {
- switch (*from) {
+void zStr::prepText(SWBuf &buf) {
+ unsigned int to, from;
+ char space = 0, cr = 0, realdata = 0, nlcnt = 0;
+ char *rawBuf = buf.getRawData();
+ for (to = from = 0; rawBuf[from]; from++) {
+ switch (rawBuf[from]) {
case 10:
if (!realdata)
continue;
@@ -294,14 +295,16 @@ void zStr::prepText(char *buf) {
nlcnt++;
if (nlcnt > 1) {
// *to++ = nl;
- *to++ = nl;
+ rawBuf[to++] = 10;
+// *to++ = nl[1];
// nlcnt = 0;
}
continue;
case 13:
if (!realdata)
continue;
- *to++ = nl;
+// *to++ = nl[0];
+ rawBuf[to++] = 10;
space = 0;
cr = 1;
continue;
@@ -310,27 +313,27 @@ void zStr::prepText(char *buf) {
nlcnt = 0;
if (space) {
space = 0;
- if (*from != ' ') {
- *to++ = ' ';
+ if (rawBuf[from] != ' ') {
+ rawBuf[to++] = ' ';
from--;
continue;
}
}
- *to++ = *from;
+ rawBuf[to++] = rawBuf[from];
}
- *to = 0;
+ buf.setSize(to);
- while (to > (buf+1)) { // remove trailing excess
+ while (to > 1) { // remove trailing excess
to--;
- if ((*to == 10) || (*to == ' '))
- *to = 0;
+ if ((rawBuf[to] == 10) || (rawBuf[to] == ' '))
+ buf.setSize(to);
else break;
}
}
/******************************************************************************
- * zStr::gettext - gets text at a given offset
+ * zStr::getText - gets text at a given offset
*
* ENT:
* offset - idxoffset where the key is located.
@@ -355,8 +358,8 @@ void zStr::getText(long offset, char **idxbuf, char **buf) {
start = swordtoarch32(start);
size = swordtoarch32(size);
- *buf = (*buf) ? (char *)realloc(*buf, size + 1) : (char *)malloc(size + 1);
- *idxbuf = (*idxbuf) ? (char *)realloc(*idxbuf, size + 1) : (char *)malloc(size + 1);
+ *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
+ *idxbuf = (*idxbuf) ? (char *)realloc(*idxbuf, size*2 + 1) : (char *)malloc(size*2 + 1);
memset(*buf, 0, size + 1);
memset(*idxbuf, 0, size + 1);
lseek(datfd->getFd(), start, SEEK_SET);
@@ -378,7 +381,7 @@ void zStr::getText(long offset, char **idxbuf, char **buf) {
break;
}
}
- findKeyIndex(*buf + IDXENTRYSIZE, &offset);
+ findKeyIndex(*buf + 6, &offset);
}
else break;
}
@@ -419,21 +422,24 @@ void zStr::getCompressedText(long block, long entry, char **buf) {
start = swordtoarch32(start);
size = swordtoarch32(size);
- *buf = (*buf) ? (char *)realloc(*buf, size + 1) : (char *)malloc(size + 1);
-
+ SWBuf buf;
+ buf.setSize(size + 5);
lseek(zdtfd->getFd(), start, SEEK_SET);
- read(zdtfd->getFd(), *buf, size);
+ read(zdtfd->getFd(), buf.getRawData(), size);
flushCache();
unsigned long len = size;
- compressor->zBuf(&len, *buf);
- char * rawBuf = compressor->Buf(0, &len);
+ buf.setSize(size);
+ rawZFilter(buf, 0); // 0 = decipher
+
+ compressor->zBuf(&len, buf.getRawData());
+ char *rawBuf = compressor->Buf(0, &len);
cacheBlock = new EntriesBlock(rawBuf, len);
cacheBlockIndex = block;
}
size = cacheBlock->getEntrySize(entry);
- *buf = (*buf) ? (char *)realloc(*buf, size + 1) : (char *)malloc(size + 1);
+ *buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
strcpy(*buf, cacheBlock->getEntry(entry));
}
@@ -461,8 +467,9 @@ void zStr::setText(const char *ikey, const char *buf, long len) {
char *outbuf = 0;
char *ch = 0;
- stdstr(&key, ikey);
- toupperstr(key);
+ len = (len < 0) ? strlen(buf) : len;
+ stdstr(&key, ikey, 3);
+ toupperstr_utf8(key, strlen(key)*3);
char notFound = findKeyIndex(ikey, &idxoff, 0);
if (!notFound) {
@@ -473,7 +480,7 @@ void zStr::setText(const char *ikey, const char *buf, long len) {
else if (diff > 0) {
idxoff += IDXENTRYSIZE;
}
- else if ((!diff) && (len || strlen(buf) /*we're not deleting*/)) { // got absolute entry
+ else if ((!diff) && (len > 0 /*we're not deleting*/)) { // got absolute entry
do {
lseek(idxfd->getFd(), idxoff, SEEK_SET);
read(idxfd->getFd(), &start, sizeof(__u32));
@@ -495,7 +502,7 @@ void zStr::setText(const char *ikey, const char *buf, long len) {
memmove(tmpbuf, ch, size - (unsigned long)(ch-tmpbuf));
// resolve link
- if (!strncmp(tmpbuf, "@LINK", 5) && (len ? len : strlen(buf))) {
+ if (!strncmp(tmpbuf, "@LINK", 5) && (len)) {
for (ch = tmpbuf; *ch; ch++) { // null before nl
if (*ch == 10) {
*ch = 0;
@@ -521,10 +528,10 @@ void zStr::setText(const char *ikey, const char *buf, long len) {
read(idxfd->getFd(), idxBytes, shiftSize);
}
- outbuf = new char [ (len ? len : strlen(buf)) + strlen(key) + 5 ];
+ outbuf = new char [ len + strlen(key) + 5 ];
sprintf(outbuf, "%s%c%c", key, 13, 10);
size = strlen(outbuf);
- if (len ? len : strlen(buf)) { // NOT a link
+ if (len > 0) { // NOT a link
if (!cacheBlock) {
flushCache();
cacheBlock = new EntriesBlock();
@@ -544,8 +551,8 @@ void zStr::setText(const char *ikey, const char *buf, long len) {
size += (sizeof(__u32) * 2);
}
else { // link
- memcpy(outbuf + size, buf, len ? len : strlen(buf));
- size += (len ? len : strlen(buf));
+ memcpy(outbuf + size, buf, len);
+ size += len;
}
start = lseek(datfd->getFd(), 0, SEEK_END);
@@ -554,7 +561,7 @@ void zStr::setText(const char *ikey, const char *buf, long len) {
outsize = archtosword32(size);
lseek(idxfd->getFd(), idxoff, SEEK_SET);
- if (len ? len : strlen(buf)) {
+ if (len > 0) {
lseek(datfd->getFd(), start, SEEK_SET);
write(datfd->getFd(), outbuf, size);
@@ -610,6 +617,12 @@ void zStr::flushCache() {
compressor->Buf(rawBuf, &size);
compressor->zBuf(&size);
+ SWBuf buf;
+ buf.setSize(size + 5);
+ memcpy(buf.getRawData(), compressor->zBuf(&size), size); // 1 = encipher
+ buf.setSize(size);
+ rawZFilter(buf, 1); // 1 = encipher
+
long zdxSize = lseek(zdxfd->getFd(), 0, SEEK_END);
long zdtSize = lseek(zdtfd->getFd(), 0, SEEK_END);
@@ -640,20 +653,18 @@ void zStr::flushCache() {
lseek(zdxfd->getFd(), cacheBlockIndex * ZDXENTRYSIZE, SEEK_SET);
lseek(zdtfd->getFd(), start, SEEK_SET);
- rawBuf = compressor->zBuf(&size);
- write(zdtfd->getFd(), rawBuf, size);
+ write(zdtfd->getFd(), buf, size);
// add a new line to make data file easier to read in an editor
write(zdtfd->getFd(), &nl, 2);
write(zdxfd->getFd(), &outstart, sizeof(__u32));
write(zdxfd->getFd(), &outsize, sizeof(__u32));
-
- delete cacheBlock;
}
+ delete cacheBlock;
+ cacheBlock = 0;
}
cacheBlockIndex = -1;
- cacheBlock = 0;
cacheDirty = false;
}
@@ -703,3 +714,5 @@ signed char zStr::createModule(const char *ipath) {
return 0;
}
+
+SWORD_NAMESPACE_END