void zVerse::findOffset(char testmt, long idxoff, long *start, unsigned short *size, unsigned long *buffnum) const { __u32 ulBuffNum = 0; // buffer number __u32 ulVerseStart = 0; // verse offset within buffer __u16 usVerseSize = 0; // verse size // set start to offset in // set size to // set *start = *size = *buffnum = 0; //fprintf(stderr, "Finding offset %ld\n", idxoff); idxoff *= 10; if (!testmt) { testmt = ((idxfp[0]) ? 1:2); } // assert we have and valid file descriptor if (compfp[testmt-1]->getFd() < 1) return; long newOffset = compfp[testmt-1]->seek(idxoff, SEEK_SET); if (newOffset == idxoff) { if (compfp[testmt-1]->read(&ulBuffNum, 4) != 4) { fprintf(stderr, "Error reading ulBuffNum\n"); return; } } else return; if (compfp[testmt-1]->read(&ulVerseStart, 4) < 2) { fprintf(stderr, "Error reading ulVerseStart\n"); return; } if (compfp[testmt-1]->read(&usVerseSize, 2) < 2) { fprintf(stderr, "Error reading usVerseSize\n"); return; } *buffnum = swordtoarch32(ulBuffNum); *start = swordtoarch32(ulVerseStart); *size = swordtoarch16(usVerseSize); }
void TreeKeyIdx::getTreeNodeFromDatOffset(long ioffset, TreeNode *node) const { unsnappedKeyText = ""; char ch; __s32 tmp; __u16 tmp2; if (datfd > 0) { datfd->seek(ioffset, SEEK_SET); datfd->read(&tmp, 4); node->parent = swordtoarch32(tmp); datfd->read(&tmp, 4); node->next = swordtoarch32(tmp); datfd->read(&tmp, 4); node->firstChild = swordtoarch32(tmp); SWBuf name; do { datfd->read(&ch, 1); name += ch; } while (ch); stdstr(&(node->name), name.c_str()); datfd->read(&tmp2, 2); node->dsize = swordtoarch16(tmp2); if (node->dsize) { if (node->userData) delete [] node->userData; node->userData = new char [node->dsize]; datfd->read(node->userData, node->dsize); } } }
void RawVerse::findOffset(char testmt, long idxoff, long *start, unsigned short *size) const { idxoff *= 6; if (!testmt) testmt = ((idxfp[1]) ? 1:2); if (idxfp[testmt-1]->getFd() >= 0) { idxfp[testmt-1]->seek(idxoff, SEEK_SET); __s32 tmpStart; __u16 tmpSize; idxfp[testmt-1]->read(&tmpStart, 4); long len = idxfp[testmt-1]->read(&tmpSize, 2); // read size *start = swordtoarch32(tmpStart); *size = swordtoarch16(tmpSize); if (len < 2) { *size = (unsigned short)((*start) ? (textfp[testmt-1]->seek(0, SEEK_END) - (long)*start) : 0); // if for some reason we get an error reading size, make size to end of file } } else { *start = 0; *size = 0; } }
signed char RawStr::findOffset(const char *ikey, __u32 *start, __u16 *size, long away, __u32 *idxoff) const { char *trybuf, *maxbuf, *key = 0, quitflag = 0; signed char retval = -1; long headoff, tailoff, tryoff = 0, maxoff = 0; int diff = 0; bool awayFromSubstrCheck = false; if (idxfd->getFd() >=0) { tailoff = maxoff = idxfd->seek(0, SEEK_END) - 6; retval = (tailoff >= 0) ? 0 : -2; // if NOT new file if (*ikey && retval != -2) { headoff = 0; stdstr(&key, ikey, 3); if (!caseSensitive) toupperstr_utf8(key, strlen(key)*3); int keylen = strlen(key); bool substr = false; trybuf = maxbuf = 0; getIDXBuf(maxoff, &maxbuf); while (headoff < tailoff) { tryoff = (lastoff == -1) ? headoff + ((((tailoff / 6) - (headoff / 6))) / 2) * 6 : lastoff; lastoff = -1; getIDXBuf(tryoff, &trybuf); if (!*trybuf && tryoff) { // In case of extra entry at end of idx (not first entry) tryoff += (tryoff > (maxoff / 2))?-6:6; retval = -1; break; } diff = strcmp(key, trybuf); if (!diff) break; if (!strncmp(trybuf, key, keylen)) substr = true; if (diff < 0) tailoff = (tryoff == headoff) ? headoff : tryoff; else headoff = tryoff; if (tailoff == headoff + 6) { if (quitflag++) headoff = tailoff; } } // didn't find exact match if (headoff >= tailoff) { tryoff = headoff; if (!substr && ((tryoff != maxoff)||(strncmp(key, maxbuf, keylen)<0))) { awayFromSubstrCheck = true; away--; // if our entry doesn't startwith our key, prefer the previous entry over the next } } if (trybuf) free(trybuf); delete [] key; if (maxbuf) free(maxbuf); } else tryoff = 0; idxfd->seek(tryoff, SEEK_SET); __u32 tmpStart; __u16 tmpSize; *start = *size = tmpStart = tmpSize = 0; idxfd->read(&tmpStart, 4); idxfd->read(&tmpSize, 2); if (idxoff) *idxoff = tryoff; *start = swordtoarch32(tmpStart); *size = swordtoarch16(tmpSize); while (away) { unsigned long laststart = *start; unsigned short lastsize = *size; long lasttry = tryoff; tryoff += (away > 0) ? 6 : -6; bool bad = false; if (((tryoff + (away*6)) < -6) || (tryoff + (away*6) > (maxoff+6))) bad = true; else if (idxfd->seek(tryoff, SEEK_SET) < 0) bad = true; if (bad) { if(!awayFromSubstrCheck) retval = -1; *start = laststart; *size = lastsize; tryoff = lasttry; if (idxoff) *idxoff = tryoff; break; } idxfd->read(&tmpStart, 4); idxfd->read(&tmpSize, 2); if (idxoff) *idxoff = tryoff; *start = swordtoarch32(tmpStart); *size = swordtoarch16(tmpSize); if (((laststart != *start) || (lastsize != *size)) && (*size)) away += (away < 0) ? 1 : -1; } lastoff = tryoff; } else { *start = 0; *size = 0; if (idxoff) *idxoff = 0; retval = -1; } return retval; }
int main(int /* argc */, char ** /* argv */) { printf("0x%.4x 0x%.4x\n", 255, swordtoarch16(255)); printf("0x%.8x 0x%.8x\n", 255, swordtoarch32(255)); // printf("0x%.16x 0x%.16llx\n", 255, __swap64(255)); return 0; }