/// Converts a C string representing a UUID to a uint8_t array. /// Supports either the format "1c1bbda2-304b-4cbf-ba3f-75324b044c73" or "1c1bbda2304b4cbfba3f75324b044c73". /// If the inputted string is zero, or if the length is zero, or if a parsing error occurs, the UUID will be /// set to null. void UUID::fromString(const char *str) { const int strLen = (str == 0) ? 0 : strlen(str); if (strLen == 0) { clear(); return; } int curIndex = 0; for(int i = 0; i < SIZE; ++i) { if (curIndex >= strLen) { clear(); return; } ///\bug Tighten parsing, now accepts all characters, like 'g' or 'Y'. while(!isalpha(str[curIndex]) && !isdigit(str[curIndex]) && !str[curIndex] == '\0') ++curIndex; // The following parse needs to read two characters from the string, hence the +1. if (curIndex + 1 >= strLen) { clear(); return; } ///\bug Here, if str[curIndex+1] is not an appropriate character, there's going to be an error! data[i] = StringToByte(str + curIndex); curIndex += 2; } }
BOOL CEditBinDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here SetDlgItemText(IDC_EDT_REGVALUE, m_strValue); if (m_bIsBinary) { BYTE sData[512] = {0}; int len = StringToByte(m_strData,sData,512); m_EditBinCtrl.SetData(sData,len); char buff[1024] = {0}; ByteToHex(sData,len,buff); m_strData = buff; } else { int len = m_strData.GetLength(),i; BYTE *pBuff = new BYTE[len+1]; for (i=0;i<len;i++) pBuff[i] = (BYTE)m_strData.GetAt(i); m_EditBinCtrl.SetData(pBuff,len); delete []pBuff; } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
int main(int argc, char* argv[]) { ValidateArgs(argc,argv); CLisence lisence; BYTE byKey[KEY_LEN] = {0}; if (g_bLockFile) { BYTE *pByte = lisence.GetKeyFromFile(g_szLockFile); memcpy(byKey,pByte,KEY_LEN); } else if (g_bLockString) { // 把string转换成byte; BYTE *pLock = new BYTE[strlen(g_szLockString) / 2]; memset(pLock,0,strlen(g_szLockString) / 2); int len = StringToByte(pLock,g_szLockString); BYTE *pByte = lisence.GetKey(pLock,len); memcpy(byKey,pByte,KEY_LEN); delete []pLock; } else { BYTE *pByte = lisence.GetLock(); printf("Machine Lock Code:\n"); for (int i = 0;i < LOCK_LEN;i++) { printf("%02X",pByte[i]); } printf("\n\n"); pByte = lisence.GetKey(); memcpy(byKey,pByte,KEY_LEN); } FILE *fp = fopen(LISENCE_FILE,"wb"); if (fp != NULL) { fwrite(byKey,1,sizeof(byKey),fp); fclose(fp); printf("Generate Key:\n"); for (int i = 0; i < KEY_LEN;i++) { printf("%02X",byKey[i]); } printf("\n\n"); return 0; } else { printf("create %s file error\n\n",LISENCE_FILE); return 1; } }