std::string UrlCode::Decode(std::string url) { const char *pstr = url.c_str(); char *buf = new char[url.length() + 1]; char *pbuf = buf; while (*pstr) { if (*pstr == '%') { if (pstr[1] && pstr[2]) { *pbuf++ = FromHex(pstr[1]) << 4 | FromHex(pstr[2]); pstr += 2; } } else if (*pstr == '+') { *pbuf++ = ' '; } else { *pbuf++ = *pstr; } pstr++; } *pbuf = '\0'; std::string decoded = buf; delete buf; return decoded; }
CStringA Util::String::PHP_URLDecode( const CStringA& src ) { CStringA decodeURL; for(int i = 0; i < src.GetLength(); i++) { unsigned char ch = 0; if(src[i]=='%') { ATLASSERT(i + 2 < src.GetLength()); unsigned char c1 = src[i + 1]; unsigned char c2 = src[i + 2]; ch = ((FromHex(c1) << 4) & 0xf0); ch |= (FromHex(c2) & 0x0f); i += 2; } else if(src[i] == '+') { ch = ' '; } else { ch = src[i]; } decodeURL += (unsigned char)ch; } return decodeURL; }
bool StringToHex(wxString ws, std::vector<wxByte> &hex) { ws.Trim(false); size_t len=ws.Len(); if(len==0) return true; // it's OK!!! wxString errmsg(_("The input string is not a valid Hex-String:")); const wxChar *pc=ws.c_str(); do { if(len<2) { wxMessageDialog dlg(nullptr, errmsg+wxT("\n\n")+ws, wxT("wxMEdit"), wxOK|wxICON_ERROR ); dlg.ShowModal(); return false; } int b0=FromHex(*pc++); int b1=FromHex(*pc++); len-=2; if(b0<0 || b1<0) { wxMessageDialog dlg(nullptr, errmsg+wxT("\n\n")+ws, wxT("wxMEdit"), wxOK|wxICON_ERROR ); dlg.ShowModal(); return false; } hex.push_back( (b0<<4) | b1 ); while(len>0 && ((*pc)==0x20 || (*pc==0x09))) // ignore spaces { --len; ++pc; } } while(len>0); return true; }
std::string UrlDecode(const std::string& str) { std::string strTemp = ""; size_t length = str.length(); for (size_t i = 0; i < length; i++) { if (str[i] == '+') strTemp += ' '; else if (str[i] == '%') { assert(i + 2 < length); unsigned char high = FromHex((unsigned char)str[++i]); unsigned char low = FromHex((unsigned char)str[++i]); strTemp += high*16 + low; } else strTemp += str[i]; } return strTemp; }
/** new Buffer: * Creates a new Buffer object * * @param string hex * Optional Hex Encoded payload to be put into the new buffer * * @return object * New Buffer object */ void CJSBuffer::JSBuffer(const v8::FunctionCallbackInfo<v8::Value> &args) { v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); CJSScript* jScript = GetJSObject<CJSScript>(args.Holder()); CBufferObj* pBuffer = new CBufferObj(); if(args.Length() > 0) *pBuffer = FromHex(CJSEngine::GetWStr(args[0])); CJSBuffer* jObject = new CJSBuffer(pBuffer, jScript); args.GetReturnValue().Set(jObject->GetInstance()); }
unsigned char *UrlDecode(const unsigned char *srcstr, unsigned char *desstr, size_t nlen) { size_t n = 0; for (size_t i = 0; i < nlen && srcstr[i]; i++) { unsigned char uc = srcstr[i]; if (uc == '+') { desstr[n++] = ' '; } else if (uc == '%') { assert(i + 2 < nlen); unsigned char high = FromHex(srcstr[++i]); unsigned char low = FromHex(srcstr[++i]); desstr[n++] = high * 16 + low; } else { desstr[n++] = uc; } } desstr[n] = 0; return desstr; }
bool SetSendingAttributes(HCOSE hMsg, const cn_cbor * pIn, int base) { bool f = false; if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "protected"), COSE_PROTECT_ONLY, base, true)) goto returnError; if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unprotected"), COSE_UNPROTECT_ONLY, base, true)) goto returnError; if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unsent"), COSE_DONT_SEND, base, false)) goto returnError; cn_cbor * pExternal = cn_cbor_mapget_string(pIn, "external"); if (pExternal != NULL) { cn_cbor * pcn = cn_cbor_clone(pExternal, CBOR_CONTEXT_PARAM_COMMA NULL); if (pcn == NULL) goto returnError; switch (base) { #if INCLUDE_ENCRYPT0 case Attributes_Encrypt_protected: if (!COSE_Encrypt_SetExternal((HCOSE_ENCRYPT)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_ENCRYPT case Attributes_Enveloped_protected: if (!COSE_Enveloped_SetExternal((HCOSE_ENVELOPED)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_MAC case Attributes_MAC_protected: if (!COSE_Mac_SetExternal((HCOSE_MAC)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_MAC0 case Attributes_MAC0_protected: if (!COSE_Mac0_SetExternal((HCOSE_MAC0)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_SIGN case Attributes_Signer_protected: if (!COSE_Signer_SetExternal((HCOSE_SIGNER)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif #if INCLUDE_SIGN0 case Attributes_Sign0_protected: if (!COSE_Sign0_SetExternal((HCOSE_SIGN0)hMsg, FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2, NULL)) goto returnError; break; #endif } } f = true; returnError: return f; }
bool SetAttributes(HCOSE hHandle, const cn_cbor * pAttributes, int which, int msgType, bool fPublicKey) { const cn_cbor * pKey; const cn_cbor * pValue; int keyNew; cn_cbor * pValueNew; bool f = false; if (pAttributes == NULL) return true; if (pAttributes->type != CN_CBOR_MAP) return false; for (pKey = pAttributes->first_child; pKey != NULL; pKey = pKey->next->next) { pValue = pKey->next; if (pKey->type != CN_CBOR_TEXT) return false; if (strcmp(pKey->v.str, "alg") == 0) { keyNew = COSE_Header_Algorithm; pValueNew = cn_cbor_int_create(MapAlgorithmName(pValue), CBOR_CONTEXT_PARAM_COMMA NULL); } else if (strcmp(pKey->v.str, "ctyp") == 0) { keyNew = COSE_Header_Content_Type; pValueNew = cn_cbor_clone(pValue, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "IV_hex") == 0) { keyNew = COSE_Header_IV; pValueNew = cn_cbor_data_create(FromHex(pValue->v.str, (int) pValue->length), (int) pValue->length / 2, CBOR_CONTEXT_PARAM_COMMA NULL); } else if (strcmp(pKey->v.str, "apu_id") == 0) { keyNew = COSE_Header_KDF_U_name; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "apv_id") == 0) { keyNew = COSE_Header_KDF_V_name; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "pub_other") == 0) { keyNew = COSE_Header_KDF_PUB_other; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "priv_other") == 0) { keyNew = COSE_Header_KDF_PRIV; pValueNew = cn_cbor_data_create(pValue->v.bytes, (int)pValue->length, CBOR_CONTEXT_PARAM_COMMA NULL); if (pValueNew == NULL) return false; } else if (strcmp(pKey->v.str, "spk") == 0) { keyNew = COSE_Header_ECDH_STATIC; pValueNew = BuildKey(pValue, fPublicKey); if (pValueNew == NULL) return false; } else { continue; } switch (msgType) { #if INCLUDE_MAC case Attributes_MAC_protected: f = COSE_Mac_map_put_int((HCOSE_MAC)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_MAC0 case Attributes_MAC0_protected: f = COSE_Mac0_map_put_int((HCOSE_MAC0)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_ENCRYPT || INCLUDE_MAC case Attributes_Recipient_protected: f = COSE_Recipient_map_put_int((HCOSE_RECIPIENT)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_ENCRYPT case Attributes_Enveloped_protected: f = COSE_Enveloped_map_put_int((HCOSE_ENVELOPED)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_ENCRYPT0 case Attributes_Encrypt_protected: f = COSE_Encrypt_map_put_int((HCOSE_ENCRYPT)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_SIGN case Attributes_Sign_protected: f = COSE_Sign_map_put_int((HCOSE_SIGN)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_SIGN case Attributes_Signer_protected: f = COSE_Signer_map_put_int((HCOSE_SIGNER)hHandle, keyNew, pValueNew, which, NULL); break; #endif #if INCLUDE_SIGN0 case Attributes_Sign0_protected: f = COSE_Sign0_map_put_int((HCOSE_SIGN0)hHandle, keyNew, pValueNew, which, NULL); break; #endif } // assert(f); } return true; }