// Convert the token information to the SEC_INFO SEC_INFO *TokenInfoToSecInfo(void *p_t) { SEC_INFO *s; char buf[MAX_SIZE]; CK_TOKEN_INFO *t = (CK_TOKEN_INFO *)p_t; // Validate arguments if (t == NULL) { return NULL; } s = ZeroMalloc(sizeof(SEC_INFO)); // Label Zero(buf, sizeof(buf)); Copy(buf, t->label, sizeof(t->label)); s->Label = ZeroMalloc(CalcUtf8ToUni(buf, 0)); Utf8ToUni(s->Label, 0, buf, 0); // ManufacturerId Zero(buf, sizeof(buf)); Copy(buf, t->manufacturerID, sizeof(t->manufacturerID)); s->ManufacturerId = ZeroMalloc(CalcUtf8ToUni(buf, 0)); Utf8ToUni(s->ManufacturerId, 0, buf, 0); // Model Zero(buf, sizeof(buf)); Copy(buf, t->model, sizeof(t->model)); s->Model = ZeroMalloc(CalcUtf8ToUni(buf, 0)); Utf8ToUni(s->Model, 0, buf, 0); // SerialNumber Zero(buf, sizeof(buf)); Copy(buf, t->serialNumber, sizeof(t->serialNumber)); s->SerialNumber = ZeroMalloc(CalcUtf8ToUni(buf, 0)); Utf8ToUni(s->SerialNumber, 0, buf, 0); // Numeric value s->MaxSession = t->ulMaxSessionCount; s->MaxRWSession = t->ulMaxRwSessionCount; s->MinPinLen = t->ulMinPinLen; s->MaxPinLen = t->ulMaxPinLen; s->TotalPublicMemory = t->ulTotalPublicMemory; s->FreePublicMemory = t->ulFreePublicMemory; s->TotalPrivateMemory = t->ulTotalPrivateMemory; s->FreePrivateMemory = t->ulFreePrivateMemory; // Hardware version Format(buf, sizeof(buf), "%u.%02u", t->hardwareVersion.major, t->hardwareVersion.minor); s->HardwareVersion = CopyStr(buf); // Firmware version Format(buf, sizeof(buf), "%u.%02u", t->firmwareVersion.major, t->firmwareVersion.minor); s->FirmwareVersion = CopyStr(buf); return s; }
// Read an INI file LIST *ReadIni(BUF *b) { LIST *o; // Validate arguments if (b == NULL) { return NULL; } o = NewListFast(NULL); SeekBuf(b, 0, 0); while (true) { char *line = CfgReadNextLine(b); if (line == NULL) { break; } Trim(line); if (IsEmptyStr(line) == false) { if (StartWith(line, "#") == false && StartWith(line, "//") == false && StartWith(line, ";") == false) { char *key, *value; UINT size = StrLen(line) + 1; key = ZeroMalloc(size); value = ZeroMalloc(size); if (GetKeyAndValue(line, key, size, value, size, NULL)) { UINT uni_size; INI_ENTRY *e = ZeroMalloc(sizeof(INI_ENTRY)); e->Key = CopyStr(key); e->Value = CopyStr(value); uni_size = CalcUtf8ToUni((BYTE *)value, StrLen(value)); e->UnicodeValue = ZeroMalloc(uni_size); Utf8ToUni(e->UnicodeValue, uni_size, (BYTE *)value, StrLen(value)); Add(o, e); } Free(key); Free(value); } } Free(line); } return o; }
//! UTF-8→Unicodeコード変換 // 2007.08.13 kobake 作成 EConvertResult CUtf8::_UTF8ToUnicode( const CMemory& cSrc, CNativeW* pDstMem, bool bCESU8Mode/*, bool decodeMime*/ ) { // エラー状態 bool bError = false; // データ取得 int nSrcLen; const char* pSrc = reinterpret_cast<const char*>( cSrc.GetRawPtr(&nSrcLen) ); const char* psrc = pSrc; int nsrclen = nSrcLen; // CMemory cmem; // // MIME ヘッダーデコード // if( decodeMime == true ){ // bool bret = MIMEHeaderDecode( pSrc, nSrcLen, &cmem, CODE_UTF8 ); // if( bret == true ){ // psrc = reinterpret_cast<char*>( cmem.GetRawPtr() ); // nsrclen = cmem.GetRawLength(); // } // } // 必要なバッファサイズを調べて確保する wchar_t* pDst; try{ pDst = new wchar_t[nsrclen]; }catch( ... ){ pDst = NULL; } if( pDst == NULL ){ return RESULT_FAILURE; } // 変換 int nDstLen = Utf8ToUni( psrc, nsrclen, pDst, bCESU8Mode ); // pDstMem を更新 pDstMem->_GetMemory()->SetRawDataHoldBuffer( pDst, nDstLen*sizeof(wchar_t) ); // 後始末 delete [] pDst; if( bError == false ){ return RESULT_COMPLETE; }else{ return RESULT_LOSESOME; } }
// Read the next line from the input file wchar_t *ConsoleReadNextFromInFile(CONSOLE *c) { LOCAL_CONSOLE_PARAM *p; char *str; // Validate arguments if (c == NULL) { return NULL; } p = (LOCAL_CONSOLE_PARAM *)c->Param; if (p->InBuf == NULL) { return NULL; } while (true) { str = CfgReadNextLine(p->InBuf); if (str == NULL) { return NULL; } Trim(str); if (IsEmptyStr(str) == false) { UINT size; wchar_t *ret; size = CalcUtf8ToUni((BYTE *)str, StrLen(str)); ret = ZeroMalloc(size + 32); Utf8ToUni(ret, size, (BYTE *)str, StrLen(str)); Free(str); return ret; } Free(str); } }
// VALUE を読み込む VALUE *ReadValue(BUF *b, UINT type) { UINT len; BYTE *u; void *data; char *str; wchar_t *unistr; UINT unistr_size; UINT size; UINT u_size; VALUE *v = NULL; // 引数チェック if (b == NULL) { return NULL; } // データ項目 switch (type) { case VALUE_INT: // 整数 v = NewIntValue(ReadBufInt(b)); break; case VALUE_INT64: v = NewInt64Value(ReadBufInt64(b)); break; case VALUE_DATA: // データ size = ReadBufInt(b); if (size > MAX_VALUE_SIZE) { // サイズオーバー break; } data = Malloc(size); if (ReadBuf(b, data, size) != size) { // 読み込み失敗 Free(data); break; } v = NewDataValue(data, size); Free(data); break; case VALUE_STR: // ANSI 文字列 len = ReadBufInt(b); if ((len + 1) > MAX_VALUE_SIZE) { // サイズオーバー break; } str = Malloc(len + 1); // 文字列本体 if (ReadBuf(b, str, len) != len) { // 読み込み失敗 Free(str); break; } str[len] = 0; v = NewStrValue(str); Free(str); break; case VALUE_UNISTR: // Unicode 文字列 u_size = ReadBufInt(b); if (u_size > MAX_VALUE_SIZE) { // サイズオーバー break; } // UTF-8 の読み込み u = ZeroMalloc(u_size + 1); if (ReadBuf(b, u, u_size) != u_size) { // 読み込み失敗 Free(u); break; } // Unicode 文字列に変換 unistr_size = CalcUtf8ToUni(u, u_size); if (unistr_size == 0) { Free(u); break; } unistr = Malloc(unistr_size); Utf8ToUni(unistr, unistr_size, u, u_size); Free(u); v = NewUniStrValue(unistr); Free(unistr); break; } return v; }
// Read the next folder void CfgReadNextFolderBin(BUF *b, FOLDER *parent) { char name[MAX_SIZE]; FOLDER *f; UINT n, i; UINT size; UCHAR *buf; wchar_t *string; // Validate arguments if (b == NULL || parent == NULL) { return; } // Folder name ReadBufStr(b, name, sizeof(name)); f = CfgCreateFolder(parent, name); // The number of the subfolder n = ReadBufInt(b); for (i = 0;i < n;i++) { // Subfolder CfgReadNextFolderBin(b, f); } // The number of items n = ReadBufInt(b); for (i = 0;i < n;i++) { UINT type; // Name ReadBufStr(b, name, sizeof(name)); // Type type = ReadBufInt(b); switch (type) { case ITEM_TYPE_INT: // int CfgAddInt(f, name, ReadBufInt(b)); break; case ITEM_TYPE_INT64: // int64 CfgAddInt64(f, name, ReadBufInt64(b)); break; case ITEM_TYPE_BYTE: // data size = ReadBufInt(b); buf = ZeroMalloc(size); ReadBuf(b, buf, size); CfgAddByte(f, name, buf, size); Free(buf); break; case ITEM_TYPE_STRING: // string size = ReadBufInt(b); buf = ZeroMalloc(size + 1); ReadBuf(b, buf, size); string = ZeroMalloc(CalcUtf8ToUni(buf, StrLen(buf)) + 4); Utf8ToUni(string, 0, buf, StrLen(buf)); CfgAddUniStr(f, name, string); Free(string); Free(buf); break; case ITEM_TYPE_BOOL: // bool CfgAddBool(f, name, ReadBufInt(b) == 0 ? false : true); break; } } }
// Read the text stream bool CfgReadNextTextBUF(BUF *b, FOLDER *current) { char *buf; TOKEN_LIST *token; char *name; char *string; char *data; bool ret; FOLDER *f; // Validate arguments if (b == NULL || current == NULL) { return false; } ret = true; // Read one line buf = CfgReadNextLine(b); if (buf == NULL) { return false; } // Analyze this line token = ParseToken(buf, "\t "); if (token == NULL) { Free(buf); return false; } if (token->NumTokens >= 1) { if (!StrCmpi(token->Token[0], TAG_DECLARE)) { if (token->NumTokens >= 2) { // declare name = CfgUnescape(token->Token[1]); // Create a folder f = CfgCreateFolder(current, name); // Read the next folder while (true) { if (CfgReadNextTextBUF(b, f) == false) { break; } } Free(name); } } if (!StrCmpi(token->Token[0], "}")) { // end ret = false; } if (token->NumTokens >= 3) { name = CfgUnescape(token->Token[1]); data = token->Token[2]; if (!StrCmpi(token->Token[0], TAG_STRING)) { // string wchar_t *uni; UINT uni_size; string = CfgUnescape(data); uni_size = CalcUtf8ToUni(string, StrLen(string)); if (uni_size != 0) { uni = Malloc(uni_size); Utf8ToUni(uni, uni_size, string, StrLen(string)); CfgAddUniStr(current, name, uni); Free(uni); } Free(string); } if (!StrCmpi(token->Token[0], TAG_INT)) { // uint CfgAddInt(current, name, ToInt(data)); } if (!StrCmpi(token->Token[0], TAG_INT64)) { // uint64 CfgAddInt64(current, name, ToInt64(data)); } if (!StrCmpi(token->Token[0], TAG_BOOL)) { // bool bool b = false; if (!StrCmpi(data, TAG_TRUE)) { b = true; } else if (ToInt(data) != 0) { b = true; } CfgAddBool(current, name, b); } if (!StrCmpi(token->Token[0], TAG_BYTE)) { // byte char *unescaped_b64 = CfgUnescape(data); void *tmp = Malloc(StrLen(unescaped_b64) * 4 + 64); int size = B64_Decode(tmp, unescaped_b64, StrLen(unescaped_b64)); CfgAddByte(current, name, tmp, size); Free(tmp); Free(unescaped_b64); } Free(name); } } // Release of the token FreeToken(token); Free(buf); return ret; }
// Interpret a line TABLE *ParseTableLine(char *line, char *prefix, UINT prefix_size, LIST *replace_list) { UINT i, len; UINT len_name; UINT string_start; char *name; char *name2; UINT name2_size; wchar_t *unistr; char *str; UINT unistr_size, str_size; TABLE *t; // Validate arguments if (line == NULL || prefix == NULL) { return NULL; } TrimLeft(line); // No line len = StrLen(line); if (len == 0) { return NULL; } // Comment if (line[0] == '#' || (line[0] == '/' && line[1] == '/')) { return NULL; } // Search to the end position of the name len_name = 0; for (i = 0;;i++) { if (line[i] == 0) { // There is only one token return NULL; } if (line[i] == ' ' || line[i] == '\t') { break; } len_name++; } name = Malloc(len_name + 1); StrCpy(name, len_name + 1, line); string_start = len_name; for (i = len_name;i < len;i++) { if (line[i] != ' ' && line[i] != '\t') { break; } string_start++; } if (i == len) { Free(name); return NULL; } // Unescape UnescapeStr(&line[string_start]); // Convert to Unicode unistr_size = CalcUtf8ToUni(&line[string_start], StrLen(&line[string_start])); if (unistr_size == 0) { Free(name); return NULL; } unistr = Malloc(unistr_size); Utf8ToUni(unistr, unistr_size, &line[string_start], StrLen(&line[string_start])); if (UniInChar(unistr, L'$')) { // Replace the replacement string wchar_t *tmp; UINT tmp_size = (UniStrSize(unistr) + 1024) * 2; UINT i; tmp = Malloc(tmp_size); UniStrCpy(tmp, tmp_size, unistr); for (i = 0; i < LIST_NUM(replace_list);i++) { TABLE *r = LIST_DATA(replace_list, i); UniReplaceStrEx(tmp, tmp_size, tmp, (wchar_t *)r->name, r->unistr, false); } unistr = CopyUniStr(tmp); Free(tmp); } // Convert to ANSI str_size = CalcUniToStr(unistr); if (str_size == 0) { str_size = 1; str = Malloc(1); str[0] = 0; } else { str = Malloc(str_size); UniToStr(str, str_size, unistr); } if (StrCmpi(name, "PREFIX") == 0) { // Prefix is specified StrCpy(prefix, prefix_size, str); Trim(prefix); if (StrCmpi(prefix, "$") == 0 || StrCmpi(prefix, "NULL") == 0) { prefix[0] = 0; } Free(name); Free(str); Free(unistr); return NULL; } name2_size = StrLen(name) + StrLen(prefix) + 2; name2 = ZeroMalloc(name2_size); if (prefix[0] != 0) { StrCat(name2, name2_size, prefix); StrCat(name2, name2_size, "@"); } StrCat(name2, name2_size, name); Free(name); // Create a TABLE t = Malloc(sizeof(TABLE)); StrUpper(name2); t->name = name2; t->str = str; t->unistr = unistr; return t; }
// Read the language list LIST *LoadLangList() { LIST *o = NewListFast(NULL); char *filename = LANGLIST_FILENAME; BUF *b; b = ReadDump(filename); if (b == NULL) { return NULL; } while (true) { char *line = CfgReadNextLine(b); if (line == NULL) { break; } Trim(line); if (IsEmptyStr(line) == false && StartWith(line, "#") == false) { TOKEN_LIST *t = ParseToken(line, "\t "); if (t != NULL) { if (t->NumTokens == 6) { LANGLIST *e = ZeroMalloc(sizeof(LANGLIST)); TOKEN_LIST *t2; e->Id = ToInt(t->Token[0]); StrCpy(e->Name, sizeof(e->Name), t->Token[1]); Utf8ToUni(e->TitleEnglish, sizeof(e->TitleEnglish), t->Token[2], StrLen(t->Token[2])); Utf8ToUni(e->TitleLocal, sizeof(e->TitleLocal), t->Token[3], StrLen(t->Token[3])); UniReplaceStrEx(e->TitleEnglish, sizeof(e->TitleEnglish), e->TitleEnglish, L"_", L" ", true); UniReplaceStrEx(e->TitleLocal, sizeof(e->TitleLocal), e->TitleLocal, L"_", L" ", true); e->LcidList = NewIntList(false); t2 = ParseToken(t->Token[4], ","); if (t2 != NULL) { UINT i; for (i = 0;i < t2->NumTokens;i++) { UINT id = ToInt(t2->Token[i]); AddIntDistinct(e->LcidList, id); } FreeToken(t2); } e->LangList = NewListFast(NULL); t2 = ParseToken(t->Token[5], ","); if (t2 != NULL) { UINT i; for (i = 0;i < t2->NumTokens;i++) { Add(e->LangList, CopyStr(t2->Token[i])); } FreeToken(t2); } Add(o, e); } FreeToken(t); } } Free(line); } FreeBuf(b); return o; }
// Read a string table from the buffer bool LoadTableFromBuf(BUF *b) { char *tmp; char prefix[MAX_SIZE]; LIST *replace_list = NULL; UINT i; // Validate arguments if (b == NULL) { return false; } // If the table already exists, delete it FreeTable(); // Create a list TableList = NewList(CmpTableName); Zero(prefix, sizeof(prefix)); replace_list = NewListFast(NULL); // Read the contents of the buffer line by line while (true) { TABLE *t; bool ok = true; tmp = CfgReadNextLine(b); if (tmp == NULL) { break; } if (tmp[0] == '$') { char key[128]; char value[MAX_SIZE]; if (GetKeyAndValue(tmp, key, sizeof(key), value, sizeof(value), " \t")) { if (StartWith(key, "$") && EndWith(key, "$") && StrLen(key) >= 3) { TABLE *t; wchar_t univalue[MAX_SIZE]; wchar_t uniname[MAX_SIZE]; t = ZeroMalloc(sizeof(TABLE)); Zero(univalue, sizeof(univalue)); Utf8ToUni(univalue, sizeof(univalue), value, StrLen(value)); StrToUni(uniname, sizeof(uniname), key); t->name = (char *)CopyUniStr(uniname); t->unistr = CopyUniStr(univalue); Add(replace_list, t); // Found a replacement definition ok = false; } } } if (ok) { t = ParseTableLine(tmp, prefix, sizeof(prefix), replace_list); if (t != NULL) { // Register Insert(TableList, t); } } Free(tmp); } for (i = 0;i < LIST_NUM(replace_list);i++) { TABLE *t = LIST_DATA(replace_list, i); Free(t->name); Free(t->str); Free(t->unistr); Free(t); } ReleaseList(replace_list); return true; }