static int readFullRecord(char *errstr, char **ssp, char *buf, size_t sz, int *line_idx) { while(1) { if (sgets(buf, (int)sz, ssp) == NULL) { return EOF_CODE; } if (needSkip(buf)) { (*line_idx)++; continue; } size_t remain; char *line_end = findLineEnd(buf); char *ptr = line_end; char *open, *close; open = findChar(buf, '('); if (open != NULL) { *open = ' '; close = findChar(buf, ')'); if (close != NULL) { *close = ' '; } else { while (close == NULL) { *ptr++ = ' '; remain = sz - (ptr - buf); if (remain <= 0) { snprintf(errstr, ERR_STR_LEN, "Syntax error(line %d): the record is too long(more than %d)", *line_idx, RECORD_SIZE); return ERR_CODE; } if (sgets(ptr, remain, ssp) == NULL) { snprintf(errstr, ERR_STR_LEN, "syntax error(line: %d): no close parenthesis.", *line_idx); return ERR_CODE; } (*line_idx)++; if (needSkip(ptr)) continue; line_end = findLineEnd(ptr); close = findChar(ptr, ')'); ptr = line_end; } *close = ' '; } } break; } (*line_idx)++; // replace all invisible character to space. replaceInvisibleChar(buf); // LOG_DEBUG("read full record: %s", buf); return OK_CODE; }
bool extractAttributes(char **ptr, Attribute *a) { // Find attribute name. { skipSpace(ptr); a->name = *ptr; findSpaceOrChar(ptr, '='); if (**ptr == '\0') return false; **ptr = '\0'; ++(*ptr); } // Find attribute value. { findCharAOrB(ptr, '\'', '\"'); const char quot = **ptr; skipOne(ptr); a->value = *ptr; findChar(ptr, quot); if (**ptr == '\0') return false; **ptr = '\0'; ++(*ptr); } return true; }
bool TaskGoodsInfo::isMonsterDrop(char* deadMonsterId) { char* temp = monsterId; cout<<"[Bison Test drop task goods] MonsterIdSet is "<<temp<<endl; int pos = 0; char oneMonsterId[51]; int len = strlen(temp); while (len > 0) { pos = findChar(temp, ',', len); if (pos == -1) { //防止最后一个没加冒号 strncpy(oneMonsterId, temp, len + 1); temp = temp + len; } else { sub_str(oneMonsterId, temp, 0, pos); temp = temp + pos + 1; } len = strlen(temp); if (!strcmp(oneMonsterId, deadMonsterId)) { cout<<"[Bison Test drop task goods] oneMonsterId "<<oneMonsterId<<endl; return true; } } cout<<"[Bison test drop task goods monid is "<<deadMonsterId<<endl; return false; }
void fill( const char c ) const { const Char * ch = findChar(c); if(!ch || !ch->numTriangles) return; glVertexPointer(2, GL_FLOAT, 0, vertices + ch->vertexOffset); glDrawElements(GL_TRIANGLES, ch->numTriangles, GL_UNSIGNED_SHORT, triangles + ch->triangleOffset); }
int String::nextToken(const char* szSep, String& sPart) { if (empty()) { return 0; } // do nothing int bFound = 0; const char* pSep = findChar(szSep); if (pSep != 0) { sPart.append(szStr_, pSep - szStr_, 1); String sRemain; sRemain.append(pSep, nBytes_ - (pSep - szStr_)); const char* pNext = sRemain.findNotChar(szSep); if (pNext == 0) { clear(); } else { append(pNext, sRemain.bytes() - (pNext - sRemain.c_str()), 1); } bFound = 1; } else { if (nBytes_ > 0) { sPart = szStr_; clear(); bFound = 1; } else { sPart.clear(); } } return bFound; }
void outline( const char c ) const { const Char * ch = findChar(c); if(!ch || !ch->numOutlines) return; glVertexPointer(2, GL_FLOAT, 0, vertices + ch->vertexOffset); glDrawElements(GL_LINES, ch->numOutlines, GL_UNSIGNED_SHORT, outlines + ch->outlineOffset); }
// find the end position of a line. it will strip the comment and space at the end of line. static char *findLineEnd(char *start) { char *cmt = findChar(start, ';'); if (cmt == NULL) cmt = start + strlen(start); *cmt = 0; if (*(cmt - 1) == '\n') *--cmt = 0; while (*(cmt - 1) == ' ') *--cmt = 0; return cmt; }
static void buildBuf() { char line[MAX_LINE+1]; char temp1[16],temp2[16]; FILE *fp = fopen("/proc/usg/ctrl","rt"); char *rec = NULL; int pos=-1; sgBufs.num = 0; send2Ctrl("i"); while(rec=Readline(line,fp)) { if( *rec!='v' ) continue; strncpy(sgBufs.bufs[sgBufs.num].name,rec+2,5); pos = findChar(sgBufs.bufs[sgBufs.num].name,5,']'); if( pos==-1 ) continue; sgBufs.bufs[sgBufs.num].name[pos]='\0'; rec+=(pos+2); pos = findChar(rec,strlen(rec),'x'); if( pos==-1 ) continue; sscanf(rec+pos+1,"%lx",&sgBufs.bufs[sgBufs.num].virt); rec+=pos+1; pos = findChar(rec,strlen(rec),'x'); if( pos==-1 ) continue; sscanf(rec+pos+1,"%lx",&sgBufs.bufs[sgBufs.num].bus); rec+=pos+1; pos = findChar(rec,strlen(rec),'x'); if( pos==-1 ) continue; sscanf(rec+pos+1,"%lx",&sgBufs.bufs[sgBufs.num].size); printf("v[%s]\t=0x%lx | b[%s]\t=0x%lx | size[%s]\t=0x%lx\n", sgBufs.bufs[sgBufs.num].name, sgBufs.bufs[sgBufs.num].virt, sgBufs.bufs[sgBufs.num].name, sgBufs.bufs[sgBufs.num].bus, sgBufs.bufs[sgBufs.num].name, sgBufs.bufs[sgBufs.num].size ); sprintf(line,"/proc/usg/%s",sgBufs.bufs[sgBufs.num].name); sgBufs.bufs[sgBufs.num].mem=mmap_buf(line,sgBufs.bufs[sgBufs.num].size); sgBufs.num++; } }
void outline( const char c ) const { const Char * ch = findChar(c); if(!ch || !ch->numOutlines) return; glVertexPointer(2, GL_FLOAT, 0, vertices + ch->vertexOffset); glDisable(GL_LIGHTING); // Disable lights when drawing lines glDrawElements(GL_LINES, ch->numOutlines, GL_UNSIGNED_SHORT, outlines + ch->outlineOffset); glEnable(GL_LIGHTING); }
std::vector<unsigned char> Compressor::compress(void* data, unsigned length) { mFrequency = getFrequency(data, length); int bits = getBestRatio(length); char* ptr = (char*)data; int i; if (bits == -1) return std::vector<unsigned char>(); BitStream bs; unsigned totalSize = 27 + ((1 << bits) - 1) * 8; if (mIsLittleEndian) bs.insert((int)length, 24); else bs.insert(reverseEndianess((int)length), 24); bs.insert(bits, 3); for (i = 0; i < (1 << bits) - 1; i++) { if ((int)mFrequency.size() > i) bs.insert(mFrequency[i].character); else bs.insert(0); } std::vector<char> uncompressed; for (i = 0; i < (int)length; i++) { int vecPos = findChar(mFrequency, *ptr, (1 << bits) - 1); if (vecPos == -1) { bs.insert(0U, bits); uncompressed.push_back(*ptr); } else { bs.insert(vecPos + 1, bits); } totalSize += bits; ptr++; } for (i = 0; i < (int)uncompressed.size(); i++) { bs.insert(uncompressed[i]); totalSize += 8; } return bs.getData(); }
string Playfair::encrypt(string plainText) { plainText = sanitizeText(plainText); for(unsigned int i = 0; i < plainText.size() - 1; i += 2) { while(plainText[i] < 'a' || plainText[i] > 'z') i++; int c1 = i; vec2 a = findChar(plainText[c1]); while(plainText[i + 1] < 'a' || plainText[i + 1] > 'z') i++; int c2 = i + 1; vec2 b = findChar(plainText[c2]); encvec(a, b); plainText[c2] = matrix[a.x][a.y]; plainText[c1] = matrix[b.x][b.y]; } return plainText; }
string Playfair::decrypt(string cipherText) { cipherText = sanitizeText(cipherText); for(unsigned int i = 0; i < cipherText.size() - 1; i += 2) { while(cipherText[i] < 'a' || cipherText[i] > 'z') i++; int c1 = i; vec2 a = findChar(cipherText[c1]); while(cipherText[i + 1] < 'a' || cipherText[i + 1] > 'z') i++; int c2 = i + 1; vec2 b = findChar(cipherText[c2]); decvec(a, b); cipherText[c2] = matrix[a.x][a.y]; cipherText[c1] = matrix[b.x][b.y]; } return cipherText; }
/*! \internal */ int qFindByteArray( const char *haystack0, int haystackLen, int from, const char *needle, int needleLen) { const int l = haystackLen; const int sl = needleLen; if (from < 0) from += l; if (uint(sl + from) > (uint)l) return -1; if (!sl) return from; if (!l) return -1; if (sl == 1) return findChar(haystack0, haystackLen, needle[0], from); /* We use the Boyer-Moore algorithm in cases where the overhead for the skip table should pay off, otherwise we use a simple hash function. */ if (l > 500 && sl > 5) return qFindByteArrayBoyerMoore(haystack0, haystackLen, from, needle, needleLen); /* We use some hashing for efficiency's sake. Instead of comparing strings, we compare the hash value of str with that of a part of this QString. Only if that matches, we call memcmp(). */ const char *haystack = haystack0 + from; const char *end = haystack0 + (l - sl); const uint sl_minus_1 = sl - 1; uint hashNeedle = 0, hashHaystack = 0; int idx; for (idx = 0; idx < sl; ++idx) { hashNeedle = ((hashNeedle<<1) + needle[idx]); hashHaystack = ((hashHaystack<<1) + haystack[idx]); } hashHaystack -= *(haystack + sl_minus_1); while (haystack <= end) { hashHaystack += *(haystack + sl_minus_1); if (hashHaystack == hashNeedle && *needle == *haystack && memcmp(needle, haystack, sl) == 0) return haystack - haystack0; REHASH(*haystack); ++haystack; } return -1; }
axStatus axIString_<T> :: splitByChar( T ch, axIString_<T> &part1, axIString_<T> &part2 ) const { axStatus st; part1.clear(); part2.clear(); axSize idx = 0; if( findChar( ch, idx ) ) { return splitByIndex( idx, part1, part2 ); }else{ st = part1.copy( *this ); if( !st ) return st; return axStatus_Std::not_found; } }
void createDirectoryPath(string path) { string prefix; int cp; cp = 1; if (stringEqual(path, "")) return; while ((cp = findChar('/', path, cp + 1)) > 0) { prefix = substring(path, 0, cp - 1); createDirectory(prefix); } createDirectory(path); }
/** * Attempts to apply the cipher on STDIN and output results to STDOUT * * Will call exit(EXIT_FAILURE) if passThroughInvalidInput is false and a * character is received from STDIN that is not in the alphabet * * @param alphabet the unique ordered set of characters to encrypt * @param key the ordered set of characters contained within the alphabet * that indicates the amount to shift input by * @param trueToEncrypt specifies if data should be shifted up to encrypt or * down to decrypt * @param passThroughInvalidInput specifies if characters from STDIN that are * not in the alphabet should cause errors or simply be output * unencrypted * @param toUpper specifies if input should be capitalized * @param toLower specifies if input should be converted to lower case */ void applyCipher(char * alphabet, char * key, bool trueToEncrypt, bool passThroughInvalidInput, bool toUpper, bool toLower) { short alphabetSize = strnlen(alphabet, MAX_STRING_SIZE); short keySize = strnlen(key, MAX_STRING_SIZE); short indexKey[keySize]; for (short i = 0; 0 != key[i]; i++) { indexKey[i] = findChar(alphabet, key[i]); } int keyIndex = 0; int input; while (EOF != (input = getchar())) { if (toUpper) input = toupper(input); else if (toLower) input = tolower(input); short inputIndex = findChar(alphabet, input); if (0 > inputIndex) { if (passThroughInvalidInput) { putchar(input); continue; } else { fprintf(stderr, "Input has character '%c' that is not in the alphabet.\n", input); exit(EXIT_FAILURE); } } short outputIndex = inputIndex; if (trueToEncrypt) { outputIndex += (indexKey[keyIndex] + 1); } else { outputIndex -= (indexKey[keyIndex] + 1); } outputIndex = (outputIndex + alphabetSize) % alphabetSize; keyIndex = (keyIndex + 1) % keySize; putchar(alphabet[outputIndex]); } }
int main(void) { int a1, a2, b1, b2, *koords; while(1) { readChars(); koords=findChar(chars[c1]); c1IsUpper = isupper(chars[c1]); a1=koords[0]; a2=koords[1]; koords=findChar(chars[c2]); c2IsUpper = isupper(chars[c2]); b1=koords[0]; b2=koords[1]; char *result = getDecrypt(a1, a2, b1, b2); chars[c1]=result[0]; chars[c2]=result[1]; if(!c1IsUpper) chars[c1]=tolower(chars[c1]); if(!c2IsUpper) chars[c2]=tolower(chars[c2]); } }
int main( int argc, char ** argv) { Magick::InitializeMagick(*argv); readNetwork("base"); int failed = 0, all = 0; for (int i = 1; i < argc; i++) { Magick::Image img(argv[i]); prepareImage(img); char res = runNetwork(img); char et = tolower(findChar(argv[i])); //printf("%c-%c\n", res, et); if (res != et) failed++; all++; } printf("%d/%d=%.2lf", failed, all, ((double)failed)/all); }
void ABCFont::draw(uint32 c) const { TextureMan.set(_texture); const Char &cC = findChar(c); glTranslatef(cC.spaceL, 0.0, 0.0); glBegin(GL_QUADS); for (int i = 0; i < 4; i++) { glTexCoord2f(cC.tX[i], cC.tY[i]); glVertex2f (cC.vX[i], cC.vY[i]); } glEnd(); glTranslatef(cC.width + cC.spaceR, 0.0, 0.0); }
uint16 FontRenderer::charHeight(uint32 fontRes) { byte *charSet = _vm->_resman->openResource(fontRes); byte *charbuf; FrameHeader frame_head; charbuf = findChar(FIRST_CHAR, charSet); frame_head.read(charbuf); if (Sword2Engine::isPsx()) free(charbuf); _vm->_resman->closeResource(fontRes); return frame_head.height; }
uint16 FontRenderer::charWidth(byte ch, uint32 fontRes) { byte *charSet = _vm->_resman->openResource(fontRes); byte *charBuf; FrameHeader frame_head; charBuf = findChar(ch, charSet); frame_head.read(charBuf); if (Sword2Engine::isPsx()) free(charBuf); _vm->_resman->closeResource(fontRes); return frame_head.width; }
int main(){ const char cipher[1201] = { 79,59,12,2,79,35,8,28,20,2,3,68,8,9,68,45,0,12,9,67,68,4,7,5,23,27,1,21,79,85,78,79,85,71,38,10,71,27,12,2,79,6,2,8,13,9,1, 13,9,8,68,19,7,1,71,56,11,21,11,68,6,3,22,2,14,0,30,79,1,31,6,23,19,10,0,73,79,44,2,79,19,6,28,68,16,6,16,15,79,35,8,11,72, 71,14,10,3,79,12,2,79,19,6,28,68,32,0,0,73,79,86,71,39,1,71,24,5,20,79,13,9,79,16,15,10,68,5,10,3,14,1,10,14,1,3,71,24,13, 19,7,68,32,0,0,73,79,87,71,39,1,71,12,22,2,14,16,2,11,68,2,25,1,21,22,16,15,6,10,0,79,16,15,10,22,2,79,13,20,65,68,41,0,16, 15,6,10,0,79,1,31,6,23,19,28,68,19,7,5,19,79,12,2,79,0,14,11,10,64,27,68,10,14,15,2,65,68,83,79,40,14,9,1,71,6,16,20,10,8, 1,79,19,6,28,68,14,1,68,15,6,9,75,79,5,9,11,68,19,7,13,20,79,8,14,9,1,71,8,13,17,10,23,71,3,13,0,7,16,71,27,11,71,10,18,2, 29,29,8,1,1,73,79,81,71,59,12,2,79,8,14,8,12,19,79,23,15,6,10,2,28,68,19,7,22,8,26,3,15,79,16,15,10,68,3,14,22,12,1,1,20, 28,72,71,14,10,3,79,16,15,10,68,3,14,22,12,1,1,20,28,68,4,14,10,71,1,1,17,10,22,71,10,28,19,6,10,0,26,13,20,7,68,14,27,74, 71,89,68,32,0,0,71,28,1,9,27,68,45,0,12,9,79,16,15,10,68,37,14,20,19,6,23,19,79,83,71,27,11,71,27,1,11,3,68,2,25,1,21,22, 11,9,10,68,6,13,11,18,27,68,19,7,1,71,3,13,0,7,16,71,28,11,71,27,12,6,27,68,2,25,1,21,22,11,9,10,68,10,6,3,15,27,68,5,10,8, 14,10,18,2,79,6,2,12,5,18,28,1,71,0,2,71,7,13,20,79,16,2,28,16,14,2,11,9,22,74,71,87,68,45,0,12,9,79,12,14,2,23,2,3,2,71, 24,5,20,79,10,8,27,68,19,7,1,71,3,13,0,7,16,92,79,12,2,79,19,6,28,68,8,1,8,30,79,5,71,24,13,19,1,1,20,28,68,19,0,68,19,7,1, 71,3,13,0,7,16,73,79,93,71,59,12,2,79,11,9,10,68,16,7,11,71,6,23,71,27,12,2,79,16,21,26,1,71,3,13,0,7,16,75,79,19,15,0,68, 0,6,18,2,28,68,11,6,3,15,27,68,19,0,68,2,25,1,21,22,11,9,10,72,71,24,5,20,79,3,8,6,10,0,79,16,8,79,7,8,2,1,71,6,10,19,0,68, 19,7,1,71,24,11,21,3,0,73,79,85,87,79,38,18,27,68,6,3,16,15,0,17,0,7,68,19,7,1,71,24,11,21,3,0,71,24,5,20,79,9,6,11,1,71, 27,12,21,0,17,0,7,68,15,6,9,75,79,16,15,10,68,16,0,22,11,11,68,3,6,0,9,72,16,71,29,1,4,0,3,9,6,30,2,79,12,14,2,68,16,7,1,9, 79,12,2,79,7,6,2,1,73,79,85,86,79,33,17,10,10,71,6,10,71,7,13,20,79,11,16,1,68,11,14,10,3,79,5,9,11,68,6,2,11,9,8,68,15,6, 23,71,0,19,9,79,20,2,0,20,11,10,72,71,7,1,71,24,5,20,79,10,8,27,68,6,12,7,2,31,16,2,11,74,71,94,86,71,45,17,19,79,16,8,79, 5,11,3,68,16,7,11,71,13,1,11,6,1,17,10,0,71,7,13,10,79,5,9,11,68,6,12,7,2,31,16,2,11,68,15,6,9,75,79,12,2,79,3,6,25,1,71, 27,12,2,79,22,14,8,12,19,79,16,8,79,6,2,12,11,10,10,68,4,7,13,11,11,22,2,1,68,8,9,68,32,0,0,73,79,85,84,79,48,15,10,29,71, 14,22,2,79,22,2,13,11,21,1,69,71,59,12,14,28,68,14,28,68,9,0,16,71,14,68,23,7,29,20,6,7,6,3,68,5,6,22,19,7,68,21,10,23,18, 3,16,14,1,3,71,9,22,8,2,68,15,26,9,6,1,68,23,14,23,20,6,11,9,79,11,21,79,20,11,14,10,75,79,16,15,6,23,71,29,1,5,6,22,19,7, 68,4,0,9,2,28,68,1,29,11,10,79,35,8,11,74,86,91,68,52,0,68,19,7,1,71,56,11,21,11,68,5,10,7,6,2,1,71,7,17,10,14,10,71,14,10, 3,79,8,14,25,1,3,79,12,2,29,1,71,0,10,71,10,5,21,27,12,71,14,9,8,1,3,71,26,23,73,79,44,2,79,19,6,28,68,1,26,8,11,79,11,1, 79,17,9,9,5,14,3,13,9,8,68,11,0,18,2,79,5,9,11,68,1,14,13,19,7,2,18,3,10,2,28,23,73,79,37,9,11,68,16,10,68,15,14,18,2,79, 23,2,10,10,71,7,13,20,79,3,11,0,22,30,67,68,19,7,1,71,8,8,8,29,29,71,0,2,71,27,12,2,79,11,9,3,29,71,60,11,9,79,11,1,79,16, 15,10,68,33,14,16,15,10,22,73 }; //char message[1202] = {0}; int sum = 0; char password[4] = {0}; for(int i = 0; i < 3; ++i){ password[i] = findChar(cipher, 1201, i, 3); } //printf("password: \"%s\"\n", password); for(int i = 0; i < 1201; ++i){ sum += password[i%3]^cipher[i]; } //printf("message: \"%s\"\n", message); printf("sum: %i\n", sum); }
uint16_t MD_PZone::getTextWidth(char *p) // Get the width in columns for the text string passed to the function // This is the sum of all the characters and the space between them. { uint16_t sum = 0; PRINTS("\ngetTextWidth"); while (*p != '\0') { sum += findChar(*p++, ARRAY_SIZE(_cBuf), _cBuf); if (*p) sum += _charSpacing; } PRINT(": W=", sum); return(sum); }
int solveTest(char* s) { int len = strlen(s); int i; int distance = 0; char* s1; char* s2; char c = '\0'; int pos; if(len % 2 == 0) { s1 = (char*)(malloc(sizeof(char) * (len / 2 + 1))); s2 = (char*)(malloc(sizeof(char) * (len / 2 + 1))); for(i = 0 ; i < len / 2; ++ i) { s1[i] = s[i]; s2[i] = s[i + len / 2]; } s1[i] = '\0'; s2[i] = '\0'; printf("%s\n", s1); printf("%s\n", s2); for(i = 0 ; i < len / 2; ++ i) { if(s1[i] == s2[i]) { continue; } else { pos = findChar(s1, s2[i]); if(pos > 0) { c = s1[i]; s1[i] = s1[pos]; s1[pos] = c; } else { break; } } } distance = len / 2 - i; return distance; } else { return -1; } }
uint8_t MD_PZone::makeChar(char c) // Load a character bitmap and add in trailing char spacing blanks { uint8_t len; PRINTX("\nmakeChar 0x", c); // look for the character len = findChar((uint8_t)c, ARRAY_SIZE(_cBuf), _cBuf); // Add in the inter char spacing for (uint8_t i = 0; i<_charSpacing; i++) { if (len < ARRAY_SIZE(_cBuf)) _cBuf[len++] = 0; } PRINT(", len=", len); return(len); }
void insertStr(LString &t,LString &s,char c){ //¿éÁ´´æ´¢µÄ´®£¬°Ñs²åÔÚtÖеÄc×Ö·ûºóÃæ p=t.head->next; while(p&&!(findChar(p,c))) p=p->next; if(!p){//ûÕÒµ½×Ö·û t.tail->next=s.head; t.tail=s.tail; }else{ q=p->next; r=(Chunk*)malloc(sizeof(Chunk)); for(j=i;j<ChunkSize;j++){ r->ch[j]=p->ch[j]; p->ch[j]='@'; } p->next=s.head; s.tail->next=t; r->next=q; } t.len += s.len; s.len=0; }
byte *FontRenderer::buildTextSprite(byte *sentence, uint32 fontRes, uint8 pen, LineInfo *line, uint16 noOfLines) { uint16 i; // Find the width of the widest line in the output text uint16 spriteWidth = 0; for (i = 0; i < noOfLines; i++) if (line[i].width > spriteWidth) spriteWidth = line[i].width; // Check that text sprite has even horizontal resolution in PSX version // (needed to work around a problem in some sprites, which reports an odd // number as horiz resolution, but then have the next even number as true width) if (Sword2Engine::isPsx()) spriteWidth = (spriteWidth % 2) ? spriteWidth + 1 : spriteWidth; // Find the total height of the text sprite: the total height of the // text lines, plus the total height of the spacing between them. uint16 char_height = charHeight(fontRes); uint16 spriteHeight = char_height * noOfLines + _lineSpacing * (noOfLines - 1); // Allocate memory for the text sprite uint32 sizeOfSprite = spriteWidth * spriteHeight; byte *textSprite = (byte *)malloc(FrameHeader::size() + sizeOfSprite); // At this stage, textSprite points to an unmovable memory block. Set // up the frame header. FrameHeader frame_head; frame_head.compSize = 0; frame_head.width = spriteWidth; frame_head.height = spriteHeight; // Normally for PSX frame header we double the height // of the sprite artificially to regain correct aspect // ratio, but this is an "artificially generated" text // sprite, which gets created with correct aspect, so // fix the height. if (Sword2Engine::isPsx()) frame_head.height /= 2; frame_head.write(textSprite); debug(4, "Text sprite size: %ux%u", spriteWidth, spriteHeight); // Clear the entire sprite to make it transparent. byte *linePtr = textSprite + FrameHeader::size(); memset(linePtr, 0, sizeOfSprite); byte *charSet = _vm->_resman->openResource(fontRes); // Build the sprite, one line at a time uint16 pos = 0; for (i = 0; i < noOfLines; i++) { // Center each line byte *spritePtr = linePtr + (spriteWidth - line[i].width) / 2; // copy the sprite for each character in this line to the // text sprite and inc the sprite ptr by the character's // width minus the 'overlap' for (uint j = 0; j < line[i].length; j++) { byte *charPtr = findChar(sentence[pos++], charSet); frame_head.read(charPtr); assert(frame_head.height == char_height); copyChar(charPtr, spritePtr, spriteWidth, pen); // We must remember to free memory for generated character in psx, // as it is extracted differently than pc version (copyed from a // char atlas). if (Sword2Engine::isPsx()) free(charPtr); spritePtr += frame_head.width + _charSpacing; } // Skip space at end of last word in this line pos++; if (Sword2Engine::isPsx()) linePtr += (char_height / 2 + _lineSpacing) * spriteWidth; else linePtr += (char_height + _lineSpacing) * spriteWidth; } _vm->_resman->closeResource(fontRes); return textSprite; }
float getAdvance( const char c ) const { const Char * ch = findChar(c); if(!ch) return 0; return ch->advance; }
/** * Main! Handles program arguments and general execution * * @param argc the number of program arguments * @param argv the program arguments * @return 0 for success, any other value indicates failure */ int main(int argc, char **argv) { bool errors = false; bool encrypt = false; bool decrypt = false; bool passThroughInvalidInput = false; bool toUpper = false; bool toLower = false; char alphabet[MAX_STRING_SIZE + 1] = ""; char key[MAX_STRING_SIZE] = ""; static struct option long_options[] = { { "alphabet", required_argument, 0, 'a' }, { "encrypt", no_argument, 0, 'e' }, { "decrypt", no_argument, 0, 'd' }, { "help", no_argument, 0, 'h' }, { "key", required_argument, 0, 'k' }, { "lower", no_argument, 0, 'l' }, { "passthru", no_argument, 0, 'p' }, { "predefined-alpha", required_argument, 0, 'q' }, { "upper", no_argument, 0, 'u' }, { "version", no_argument, 0, 'v' }, { 0, 0, 0, 0 } }; // Handle command line options while (true) { int option_index = 0; int option = getopt_long(argc, argv, "a:edhk:lpq:uv", long_options, &option_index); if (option == -1) break; switch (option) { case 'a': if (strnlen(optarg, MAX_STRING_SIZE + 1) > MAX_STRING_SIZE) { fprintf(stderr, "Alphabet is longer than max supported size of %d.\n", MAX_STRING_SIZE); errors = true; } strncpy(alphabet, optarg, MAX_STRING_SIZE); break; case 'e': encrypt = true; break; case 'd': decrypt = true; break; case 'h': // Print help puts(HELP); return (EXIT_SUCCESS); break; case 'k': strncpy(key, optarg, MAX_STRING_SIZE); break; case 'l': toLower = true; break; case 'p': passThroughInvalidInput = true; break; case 'q': if (0 == strncmp("UC", optarg, MAX_STRING_SIZE)) { strncpy(alphabet, UC_ALPHA, MAX_STRING_SIZE); } else if (0 == strncmp("LC", optarg, MAX_STRING_SIZE)) { strncpy(alphabet, LC_ALPHA, MAX_STRING_SIZE); } else if (0 == strncmp("AC", optarg, MAX_STRING_SIZE)) { strncpy(alphabet, AC_ALPHA, MAX_STRING_SIZE); } else if (0 == strncmp("PRINT", optarg, MAX_STRING_SIZE)) { strncpy(alphabet, PRINTABLE, MAX_STRING_SIZE); } else { fprintf(stderr, "There is no predefined alphabet \"%s\"\n", optarg); fputs("Please run with --help for usage options.\n", stderr); errors = true; } break; case 'u': toUpper = true; break; case 'v': printf("%s\n", VERSION); return (EXIT_SUCCESS); case '?': fputs("Please run with --help for usage options.\n", stderr); errors = true; break; default: abort(); break; } } // Check for input errors before trying to apply cipher if (optind < argc) { fputs("Non-option arguments are not supported.\n", stderr); fputs("Please run with --help for usage options.\n", stderr); errors = true; } if (false == encrypt && false == decrypt) { fputs("Specify if you would like to encrypt or decrypt.\n", stderr); return (EXIT_FAILURE); } else if (true == encrypt && true == decrypt) { fputs("You cannot both encrypt and decrypt.\n", stderr); return (EXIT_FAILURE); } if (NULL == alphabet || 0 == strnlen(alphabet, MAX_STRING_SIZE)) { fputs("No alphabet provided.\n", stderr); errors = true; } else { for (short i = 0; 0 != key[i]; i++) { if (0 > findChar(alphabet, key[i])) { fprintf(stderr, "Key has character '%c' that is not in the alphabet.\n", key[i]); errors = true; } } } if (NULL == key || 0 == strnlen(key, MAX_STRING_SIZE)) { fputs("No key provided.\n", stderr); errors = true; } if (true == toUpper && true == toLower) { fputs("You cannot convert output to both upper case and lower case.\n", stderr); errors = true; } else if (true == toUpper) { stoupper(alphabet); stoupper(key); } else if (true == toLower) { stolower(alphabet); stolower(key); } if (doesAlphabetHaveDuplicates(alphabet)) { fputs("Alphabet cannot have duplicate characters.\n", stderr); errors = true; } // If there are no detected errors, attempt to apply the cipher on STDIN if (false == errors) { applyCipher(alphabet, key, encrypt && !decrypt, passThroughInvalidInput, toUpper, toLower); } else { return (EXIT_FAILURE); } return (EXIT_SUCCESS); }
void MO::parseMoFile(unsigned char* bytes) { if (!bytes || _isInited) { return; } /** precache some functions */ unsigned long (*peekLong)(unsigned char* bytes, unsigned long offs) = nullptr; auto peekLongBig = [](unsigned char* bytes, unsigned long offs)->unsigned long { unsigned char a = bytes[offs]; unsigned char b = bytes[offs+1]; unsigned char c = bytes[offs+2]; unsigned char d = bytes[offs+3]; return ((d*256+c)*256+b)*256+a; }; auto peekLongLittle = [](unsigned char* bytes, unsigned long offs)->unsigned long { unsigned char a = bytes[offs]; unsigned char b = bytes[offs+1]; unsigned char c = bytes[offs+2]; unsigned char d = bytes[offs+3]; return ((a*256+b)*256+c)*256+d; }; auto copyBytes = [](char* srcBytes, int len) { char* chars = new char[len+1];// +1 is null-terminated byte string pointed strncpy(chars, srcBytes, len); chars[len] = 0; std::string str(chars); delete [] chars; return str; }; /** * Check format in MO file. * @see http://www.gnu.org/savannah-checkouts/gnu/gettext/manual/html_node/MO-Files.html */ /** magic */ const unsigned long MAGIC_BIG = 0xde120495; const unsigned long MAGIC_LITTLE = 0x950412de; const int BYTE_SIZE = 4; unsigned char magicBytes[BYTE_SIZE]; memcpy(magicBytes, bytes, BYTE_SIZE); unsigned long magicNumber = 0; for (int i = 0; i < BYTE_SIZE; i++) magicNumber += (unsigned long)magicBytes[i] << ((BYTE_SIZE-1-i)*8); switch (magicNumber) { case MAGIC_BIG: peekLong = peekLongBig; break; case MAGIC_LITTLE: peekLong = peekLongLittle; break; default: //no valid mo-file return; break; } /** revision */ unsigned long revision = peekLong(bytes, 4); // support revision 0 of MO format specs, only if (0 != revision) { //unsupported version return; } /** get number of N, O, T, S, H */ unsigned long moN = peekLong(bytes, 8); unsigned long moO = peekLong(bytes, 12); unsigned long moT = peekLong(bytes, 16); unsigned long moS = peekLong(bytes, 20); unsigned long moH = peekLong(bytes, 24); // skip hash table (void)moS; (void)moH; /** traverse and get strings */ const int CHUNK_SIZE = 8; for (unsigned long i = 0; i < moN; i++) { auto originalLength = peekLong(bytes, moO); auto originalOffset = peekLong(bytes, moO + BYTE_SIZE); auto translationLength = peekLong(bytes, moT); auto translationOffset = peekLong(bytes, moT + BYTE_SIZE); char* originalBytes = new char[originalLength]; memcpy(originalBytes, bytes + originalOffset, originalLength); char* translationBytes = new char[translationLength]; memcpy(translationBytes, bytes + translationOffset, translationLength); moO += CHUNK_SIZE; moT += CHUNK_SIZE; /** mo file headers info */ if (0 == originalLength && _headersInfo.empty()) { _headersInfo = copyBytes(translationBytes, (int)translationLength); } else { /** * Build a TranslationEntry from original string and translation strings, * found in a MO file. * * char[] {originalBytes} original string to translate from MO file. Might contain * 0x04 as context separator or 0x00 as singular/plural separator. * char[] {translationBytes} translation string from MO file. Might contain * 0x00 as a plural translations separator. */ TranslateEntry entry; // check original do { auto originalItr = findChar(originalBytes, originalBytes + originalLength, CONTEXT_SEPARATOR); size_t bytesLen = originalItr - originalBytes; // look for context if (bytesLen < originalLength) { entry.setContext(copyBytes(originalBytes, (int)bytesLen)); bytesLen += sizeof(CONTEXT_SEPARATOR); } else { bytesLen = 0; } // look for singular original originalItr = findChar(originalBytes + bytesLen, originalBytes + originalLength, PLURAL_SEPARATOR); entry.setSingular(copyBytes(originalBytes + bytesLen, (int)(originalItr - originalBytes - bytesLen))); bytesLen = originalItr - originalBytes + sizeof(PLURAL_SEPARATOR); // look for plural original if (bytesLen < originalLength) { entry.setPlural(copyBytes(originalBytes + bytesLen, (int)(originalLength - (originalItr - originalBytes)))); } }while (0); // check translation do { size_t bytesLen = 0; std::vector<std::string> translations; // plural translations are also separated by \0 while (true) { auto translationItr = findChar(translationBytes + bytesLen, translationBytes + translationLength, PLURAL_SEPARATOR); translations.push_back(copyBytes(translationBytes + bytesLen, (int)(translationItr - translationBytes - bytesLen))); bytesLen = translationItr - translationBytes + sizeof(PLURAL_SEPARATOR); if (bytesLen >= translationLength) { break; } } entry.setTranslations(translations); } while (0); // save _entryMap[entry.getKey()] = entry; // debug //printf("%s\n", entry.toString().c_str()); } delete [] originalBytes; delete [] translationBytes; } _isInited = true; }