bool _ScanDir(CStr path, int mask, CStr body, qCtx *ctx, qStr *out, DIRSTATE &st) { BOOL bMore; HANDLE hFind; BOOL showdot = false; // truncate trailing slashes char *b = path.GetBuffer(); if (!b || !*b) return false; char *p = path+path.Length() - 1; while (p >= b && ISDIRSEP(*p)) --p; if (p-b+1 > 0) { if (*p == ':') { showdot = true; if (!ISDIRSEP(p[1])) { path << '.'; b = path.GetBuffer(); p = path+path.Length() - 1; } else ++p; } st.path = path; // truncate path to parent while (p >= b && !ISPATHSEP(*p)) --p; if (p >= b) { st.path.Grow(p-b+1); } else { st.path.Grow(0); } } else { st.path = path; } // read all entries in the directory WIN32_FIND_DATA *r = &st.data; hFind = FindFirstFile(path, r); bMore = (hFind != (HANDLE) -1); while (bMore &&!st.bquit) { if ((mask & r->dwFileAttributes) && !(r->cFileName[0]=='.'&&r->cFileName[1]=='\0') ) { ctx->Parse(body, out); } else if (showdot && r->cFileName[0]=='.'&&r->cFileName[1]=='\0') { ctx->Parse(body, out); } bMore = FindNextFile(hFind, r); } FindClose(hFind); return true; } // dir_scan
void EvalTrim(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("trim", 1, 2); if (args->Count() > 1) { CStr str = (*args)[0]; if (str.IsEmpty()) return; CStr toks = (*args)[1]; if (toks.Length() == 0) out->PutS(str); else if (toks.Length() == 1) out->PutS(str.Trim(*toks)); else { const char *b, *p = str; size_t i = strspn(p,toks); b = p += i; p += str.Length() - 1 - i; while(p >= b && strchr(toks.Data(), *p)) --p; ++p; out->PutS(b, p - b); } } else out->PutS((*args)[0].Trim()); }
void EvalCsvQuote(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 0) { CStr s = (*args)[0]; char *bi = s.SafeP(); const char *pi = bi; while (*pi) { if (*pi == '\"' || *pi == ',') { CStr o(s.Length()*2 + 2); char *po = o.SafeP(); *po++ = '\"'; memcpy(po, s.Data(), pi-bi); CsvQuoteQ(pi, po); *po++ = '\"'; o.Grow(po - o.Data()); out->PutS(o,o.Length()); return; } ++pi; } out->PutS(s,s.Length()); } }
void EvalRTrim(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("ltrim", 1, 2); if (args->Count() > 1) { CStr str = (*args)[0]; if (str.IsEmpty()) return; CStr toks = (*args)[1]; if (toks.Length() == 0) out->PutS(str); else if (toks.Length() == 1) out->PutS(str.RTrim(*toks)); else { const char *p, *b = str; p = b + str.Length() - 1; while(p >= b && strchr((const char *)toks, *p)) --p; ++p; out->PutS(b, p - b); } } else out->PutS((*args)[0].RTrim()); }
CStr ReplaceStrI(CStr in, CStr from, CStr to) { char *p; int fl = from.Length(), tl = to.Length(), il = in.Length(); if (fl <= 0 || fl > il) return in; if (fl == tl) { char *i = in.SafeP(); char *b = i; while ((p = stristr(i, (il-(i-b)), from, fl))) { memcpy(p, to.Data(), tl); i = p + fl; } return in; } else { CStr res; char *i = in.SafeP(); while ((p = stristr(i, il-(i-in.Data()), from, fl))) { res.Append(i, p - i); if (tl > 0) res << to; i = p + fl; } if (i != in.Data()) { res.Append(i, il - (i - in.Data())); return res; } else { return in; } } }
CStr B64_decode(CStr strin) { if (!strin.IsEmpty()) { CStr strout((int) strin.Length()); int len = B64_decode(strin, strout.GetBuffer(), strin.Length()); if (len < 0) len = 0; return strout.Grow(len); } else return strin; }
CStr EVP_decrypt(CStr passw, CStr strin, const char *cipher) { int len = strin.Length(); if (len > 0 && passw.Length() > 0) { strin.Grow(len + EVP_MAX_IV_LENGTH); return strin.Grow( EVP_decrypt(passw.SafeP(), passw.Length(), strin.GetBuffer(), len, cipher) ); } else return strin; }
void EvalRight(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() >= 2) { CStr tmp = (*args)[0]; int index = ParseInt((*args)[1]); if (index > 0) { if (index < tmp.Length()) { out->PutS(tmp + tmp.Length() - index, index); } else { out->PutS(tmp); } } } }
// set search void EvalXin(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { CStr str = (*args)[0]; CStr cur; int len = str.Length(), i; for (i = 1; i < args->Count(); ++i) { cur = (*args)[i]; if (cur.Length() == len && !strncmp(str, cur, len)) { out->PutN(i); return; } } }
// expand an unexpanded (quoted) macro void EvalExpand(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("expand", 0, 1); CStr val = (*args)[0]; if (val.Length() > 0) { if (val.Length() > 4 && val.Data()[0] == CMAGIC_V1[0] && val.Data()[1] == CMAGIC_V1[1] && val.Data()[2] == CMAGIC_V1[2] && val.Data()[3] == CMAGIC_V1[3]) { qStrReadBuf tmp(val.Data()+4,val.Length()-4); RunCompiled(ctx, &tmp, out); } else { ctx->Parse(val, out); } } }
void EvalFileName(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("filename", 1, 1); CStr path = (*args)[0]; if (path) { const char *b = path; const char *r = b + path.Length() - 1; while (r >= b && !(ISPATHSEP(*r))) { --r; } ++r; out->PutS(r, path.Length() - (r - b)); } }
CStr CIpAddress::GetFullHostName() {_STT(); CStr sDomain = GetDomainName(); if ( sDomain.Length() ) return GetHostName() << oexT( "." ) << sDomain; return GetHostName(); }
CStr SmxQuote(CStr in) { CStr r; const char *p, *b = in; const char *e = in.Length() + (p = b); bool quot = false; while (p < e) { if (*p == '\'' || *p == '%' || *p == '\"' || *p == ',' || *p == '\\' || *p ==')' || *p == '(' || *p == 0 || *p == EOF || isspace(*p)) { quot = true; r = "\"" << CStr(b,p-b); while (p < e) { if (*p == '"' || *p =='\\' || *p =='(' || *p ==')') { r << '\\'; r << *p++; } else if (*p == '%') { r << '%'; r << '%'; ++p; } else { r << *p++; } } r << '"'; break; } else ++p; } if (quot) return r; else return in; }
void EvalGDefine(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGM("gdefine", 1); CStr name; qObj *obj = CreateDef(ctx, out, args, name); if (!name.Length()) { if (obj) obj->Free(); return; } if (obj) if (ctx->GetEnv() && ctx->GetEnv()->GetSessionCtx()) { ctx->MapObjTop(obj, name, ctx->GetEnv()->GetSessionCtx()); } else { ctx->MapObjTop(obj, name, NULL); } else if (ctx->GetEnv() && ctx->GetEnv()->GetSessionCtx()) { ctx->DelObjTop(name, ctx->GetEnv()->GetSessionCtx()); } else { ctx->DelObjTop(name, NULL); } }
bool SetError(int nResult, bool bFree = true) { if (!myErrRes && !SQL_SUCCEEDED(nResult)) { myErrBuf.Grow(1024); myErrState.Grow(6); SQLSMALLINT tlen = 0; if (!SQLGetDiagRec(myType, myHandle, 1, (SQLCHAR*) myErrState.GetBuffer(), &myErrCode, (SQLCHAR*) (myErrBuf.GetBuffer())+8, myErrBuf.Length()-8, &tlen)) { myErrBuf[0] = '('; memcpy(myErrBuf+1, (const char *)myErrState, 5); myErrBuf[6] = ')'; myErrBuf[7] = ' '; myErrBuf.Grow(min(tlen+8,myErrBuf.Length()-8)); myErrRes = nResult; } else { myErrState = "00000"; myErrBuf = "(00000) No error."; myErrRes = nResult; } if (bFree) { SQLFreeHandle(myType, myHandle); myHandle = 0; } return false; } return true; }
bool ConfirmChecksum(CStr CardNumber) { int CheckSum; // Holds the value of the operation bool Flag; // used to indicate when ready int Counter; // index counter int Number; // used to convert each digit to integer /************************************************************************** function is extracting each digit of the number and subjecting it to the checksum formula established by the credit card companies. It works from the end to the front. **************************************************************************/ // get the starting value for our counter Counter = CardNumber.Length() - 1; CheckSum = 0; Number = 0; Flag = false; while ( Counter >= 0 ) { // get the current digit Number = CardNumber.Data()[Counter] - '0'; if ( Flag ) // only do every other digit { Number = Number * 2; if ( Number >= 10 ) Number = Number - 9; } CheckSum = CheckSum + Number; Flag = !Flag; Counter = Counter - 1; } return ( ( CheckSum % 10 ) == 0 ); }
oexINT CImage::GetFileType( oexCSTR x_pFile ) {_STT(); #if !defined( OEX_ENABLE_XIMAGE ) return -1; #else // Sanity check if ( !oexCHECK_PTR( x_pFile ) ) return -1; oexINT type = -1; CStr sExt = CStr( x_pFile ).GetFileExtension(); if ( !sExt.Length() ) sExt = x_pFile; sExt.ToLower(); if ( 0 ); // Check for bitmap #if defined( CXIMAGE_SUPPORT_JPG ) && CXIMAGE_SUPPORT_JPG else if ( sExt == oexT( "jpg" ) || sExt == oexT( "jpeg" ) ) type = CXIMAGE_FORMAT_JPG; #endif #if defined( CXIMAGE_SUPPORT_BMP ) && CXIMAGE_SUPPORT_BMP else if ( sExt == oexT( "bmp" ) || sExt == oexT( "dib" ) ) type = CXIMAGE_FORMAT_BMP; #endif #if defined( CXIMAGE_SUPPORT_PNG ) && CXIMAGE_SUPPORT_PNG else if ( sExt == oexT( "png" ) ) type = CXIMAGE_FORMAT_PNG; #endif #if defined( CXIMAGE_SUPPORT_WMF ) && CXIMAGE_SUPPORT_WMF else if ( sExt == oexT( "wmf" ) ) type = CXIMAGE_FORMAT_WMF; else if ( sExt == oexT( "emf" ) ) type = CXIMAGE_FORMAT_WMF; #endif #if defined( CXIMAGE_SUPPORT_GIF ) && CXIMAGE_SUPPORT_GIF else if ( sExt == oexT( "gif" ) ) type = CXIMAGE_FORMAT_GIF; #endif //#if defined( CXIMAGE_SUPPORT_MNG ) && CXIMAGE_SUPPORT_MNG // else if ( sExt == oexT( "mng" ) ) type = CXIMAGE_FORMAT_MNG; //#endif #if defined( CXIMAGE_SUPPORT_ICO ) && CXIMAGE_SUPPORT_ICO else if ( sExt == oexT( "ico" ) ) type = CXIMAGE_FORMAT_ICO; #endif #if defined( CXIMAGE_SUPPORT_TIF ) && CXIMAGE_SUPPORT_TIF else if ( sExt == oexT( "tif" ) || sExt == oexT( "tiff" ) ) type = CXIMAGE_FORMAT_TIF; #endif #if defined( CXIMAGE_SUPPORT_TGA ) && CXIMAGE_SUPPORT_TGA else if ( sExt == oexT( "tga" ) ) type = CXIMAGE_FORMAT_TGA; #endif #if defined( CXIMAGE_SUPPORT_PCX ) && CXIMAGE_SUPPORT_PCX else if ( sExt == oexT( "pcx" ) ) type = CXIMAGE_FORMAT_PCX; #endif //#if defined( CXIMAGE_SUPPORT_JP2 ) && CXIMAGE_SUPPORT_JP2 // else if ( sExt == oexT( "jp2" ) ) type = CXIMAGE_FORMAT_JP2; //#endif return type; #endif }
void EvalLcase(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 0) { CStr tmp = (*args)[0]; if (tmp) out->PutS(strlwr(tmp.GetBuffer()), tmp.Length()); } }
void EvalMid(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() >= 2) { CStr tmp = (*args)[0]; int index = ParseInt((*args)[1]); if (args->Count() >= 3) { int len = ParseInt((*args)[2]); if (abs(index) < tmp.Length()) { if (index >= 0) { out->PutS(tmp.SafeP() + index, min(tmp.Length()-index, len)); } else { out->PutS(tmp.SafeP() + max(tmp.Length() + index - abs(len), 0), min(tmp.Length(), len)); } } } else { if (abs(index) < tmp.Length()) { if (index >= 0) { out->PutS(tmp.SafeP() + index, tmp.Length()-index); } else { out->PutS(tmp.SafeP(), tmp.Length() + index); } } } } }
int EvalCommWrite(qCtx *ctx, qStr *out, qArgAry *args) { CStr str = (*args)[0]; DWORD dwb, dwtot = str.Length(); if (!WriteFile(myCom, str, dwtot, &dwb, 0)) { ctx->ThrowF(out, 603, "Write failure : %y", GetLastError()); return false; } return 0; }
// blowfish decrypt void EvalDeobfuscate(const void *data, qCtx *ctx, qStr *pStream, qArgAry *args) { if (args->Count() > 0) { CStr key = ctx->Eval("obfuscate-key"); if (key.Length() <= 0) key = "PSX"; CStr out = EVP_decrypt(key,B64_decode((*args)[0].SafeP()),0); pStream->PutS(out); } }
void EvalLTrim(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("ltrim", 1, 2); if (args->Count() > 1) { CStr str = (*args)[0]; if (str.IsEmpty()) return; CStr toks = (*args)[1]; if (toks.Length() == 0) out->PutS(str); else if (toks.Length() == 1) out->PutS(str.LTrim(*toks)); else { const char *p = str; size_t i = strspn(p,toks); out->PutS(p+i, str.Length() - i); } } else out->PutS((*args)[0].LTrim()); }
// sha hash void EvalSha(const void *data, qCtx *ctx, qStr *pStream, qArgAry *args) { if (args->Count() > 0) { CStr out = HEX_encode(SHA1_string((*args)[0])); if (args->Count() > 1) pStream->PutS(out, max(0,min(ParseInt((*args)[1]), out.Length()))); else pStream->PutS(out); } }
void PadParams(qCtx *ctx, qArgAry *args, CStr &str, int &pad, CStr &fill, int &len) { str = (*args)[0]; pad = (args->Count() > 1) ? ParseInt((*args)[1]) : 0; if (args->Count() > 2) fill = (*args)[2]; else fill = " "; len = fill.Length(); }
int EvalCommRead(qCtx *ctx, qStr *out, qArgAry *args) { int maxbuf = args->GetAt(0).IsEmpty() ? 1 : ParseInt((*args)[0]); int timeout = (int) (1000.0 * ParseDbl((*args)[1])); CStr eol = (*args)[2]; CStr buf(maxbuf); char *p = buf.Data(); const char *b = p; DWORD dwb; COMMTIMEOUTS to, to2; if (timeout > 0) { GetCommTimeouts(myCom, &to); to2 = to; to2.ReadTotalTimeoutMultiplier = 0; to2.ReadTotalTimeoutConstant = timeout; BOOL rVal = SetCommTimeouts(myCom, &to2); } while (maxbuf > 0) { if (!ReadFile(myCom, p, maxbuf, &dwb, 0)) { ctx->ThrowF(out, 604, "Read failure : %y", GetLastError()); return false; } if (eol.IsEmpty() || dwb == 0) break; if (p-b >= eol.Length()) if (strstr(b, eol)) break; else b = p-eol.Length()+1; maxbuf-=dwb; if (maxbuf > 0) Sleep(1); } out->PutS(buf, p-buf.Data()); if (timeout > 0) { SetCommTimeouts(myCom, &to); } return 0; }
int CSys::Echo( oexCSTRW x_pFmt, oexLONG x_lLen ) { //_STT(); if ( !x_pFmt || 0 >= x_lLen ) return 0; #if defined( oexUNICODE ) CUtil::AddOutput( x_pFmt, x_lLen, oexTRUE ); #else CStr s = oexStrWToStr( CStrW( x_pFmt, x_lLen ) ); CUtil::AddOutput( s.Ptr(), s.Length(), oexTRUE ); #endif return ::fwrite( x_pFmt, 1, x_lLen, stdout ); }
// compile a macro (currently just - mostly obfuscates it) void EvalCompile(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("compile", 1, 2); CStr val = (*args)[0]; if (val.Length() > 0) { bool randomize = ParseBool((*args)[1]); qStrReadBuf tmp(val); qCtxComp comp(ctx, randomize); out->PutS(CMAGIC_V1); comp.Parse(&tmp, out); } }
void EvalUnpack(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGM("unpack", 2); CStr templ = (*args)[0]; CStr sdata = (*args)[1]; char t; const char * p = templ; char *endp = 0; if (!p) return; const char * d = sdata; int dlen = sdata.Length(); int nlen, j; while (*p) { switch(*p) { case 'a': case 'A': if (isdigit(p[1])) { nlen = strtol(p+1, &endp, 0); p = endp-1; if (nlen <= 0) nlen =1; } else nlen =1; nlen = min(nlen, dlen); out->PutS(d, nlen); d += nlen; dlen -=4; case 'l': case 'L': if (dlen >= 4) { t = *p; if (isdigit(p[1])) { nlen = strtol(p+1, &endp, 0); p = endp-1; if (nlen <= 0) nlen =1; } else nlen =1; for(j = 0; j < nlen; ++j) { if (t == 'l') out->PutN(*(long *) d); else if (t == 'L') out->PutN(*(unsigned int *) d); } } } ++p; } }
void EvalTCPSend(qCtx *ctx, qStr *out, qArgAry *args) { CStr str = (*args)[0]; int len = sock.Write(str, str.Length()); if (len < 0 ) { if (len == Sock::ERR_TIMEOUT) { ctx->ThrowF(out, 702, "Timeout while waiting to write data from host %sock:%d, %y", sock.GetHost(), sock.GetPort()); } else { ctx->ThrowF(out, 702, "Error while writing data from host %sock:%d, %y", sock.GetHost(), sock.GetPort(), GetLastError()); } } }
void EvalPack(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGM("pack", 2); CStr templ = (*args)[0]; CStr sarg; char t; const char * p = templ; char *endp = 0; if (!p) return; int nlen, plen, i = 1, j; while (*p) { switch(*p) { case 'a': case 'A': if (isdigit(p[1])) { nlen = strtol(p+1, &endp, 0); p = endp-1; if (nlen <= 0) nlen =1; } else nlen =1; sarg = (*args)[i++]; plen = sarg.Length(); sarg.Grow(nlen); if (nlen > plen) memset(sarg.Data()+plen,(*p == 'a' ? ' ' : '\0'),nlen-plen); out->PutS(sarg); case 'l': case 'L': t = *p; if (isdigit(p[1])) { nlen = strtol(p+1, &endp, 0); p = endp-1; if (nlen <= 0) nlen =1; } else nlen =1; for(j = 0; j < nlen; ++j) { DWORD dw; sarg = (*args)[i++]; if (t == 'l') dw = (DWORD) strtoul(sarg.Data(), &endp, 0); else if (t == 'L') dw = (DWORD) strtol(sarg.Data(), &endp, 0); out->PutS((char *)&dw,sizeof(dw)); } } ++p; } }