#include "protocol.h" char *separate_token(char *buf, char separetor, char **handle) { char *_handle; if (handle == NULL) handle = &_handle; if (buf) *handle = buf; if (*handle == NULL || **handle == 0) return NULL; while (**handle == separetor) (*handle)++; buf = *handle; if (**handle == 0) return NULL; while (**handle && **handle != separetor) (*handle)++; if (**handle == separetor) *(*handle)++ = 0; return buf; } _int64 hex2ll(char *buf) { _int64 ret = 0; for ( ; *buf; buf++) { if (*buf >= '0' && *buf <= '9') ret = (ret << 4) | (*buf - '0'); else if (toupper(*buf) >= 'A' && toupper(*buf) <= 'F') ret = (ret << 4) | (toupper(*buf) - 'A' + 10); else break; } return ret; } void ConvertShareMsgEscape(char *str) { char *ptr; while ((ptr = strstr(str, "::")) != NULL) { *ptr++ = ';'; memmove(ptr, ptr + 1, strlen(ptr)); } } ShareInfo *DecodeShareMsg(char *buf) { ShareInfo *shareInfo = new ShareInfo; FileInfo *fileInfo = NULL; char *tok, *p, *p2, *p3; char *file = separate_token(buf, FILELIST_SEPARATOR, &p); for (int cnt=0; file; cnt++, file=separate_token(NULL, FILELIST_SEPARATOR, &p)) { ConvertShareMsgEscape(file); // "::" -> ';' if ((tok = separate_token(file, ':', &p2)) == NULL) break; fileInfo = new FileInfo(atoi(tok)); if ((tok = separate_token(NULL, ':', &p2)) == NULL || strlen(tok) >= MAX_PATH) break; while ((p3 = strchr(tok, '?')) != NULL) *p3 = '_'; fileInfo->SetFname(tok); if ((tok = separate_token(NULL, ':', &p2)) == NULL) break; fileInfo->SetSize(hex2ll(tok)); if ((tok = separate_token(NULL, ':', &p2)) == NULL) break; fileInfo->SetMtime(strtoul(tok, 0, 16)); if ((tok = separate_token(NULL, ':', &p2))) { fileInfo->SetAttr(strtoul(tok, 0, 16)); u_int attr_type = GET_MODE(fileInfo->Attr()); if (attr_type != IPMSG_FILE_DIR && attr_type != IPMSG_FILE_REGULAR) { delete fileInfo; continue; } } else fileInfo->SetAttr(IPMSG_FILE_REGULAR); if ((shareInfo->fileCnt % BIG_ALLOC) == 0) shareInfo->fileInfo = (FileInfo **)realloc(shareInfo->fileInfo, (shareInfo->fileCnt + BIG_ALLOC) * sizeof(FileInfo *)); shareInfo->fileInfo[shareInfo->fileCnt++] = fileInfo; fileInfo = NULL; } if (fileInfo) delete fileInfo; if (shareInfo->fileCnt <= 0) { delete shareInfo; return NULL; } return shareInfo; } BOOL DecodeDirEntry(char *buf, FileInfo *info) { char *tok, *ptr, *p; ConvertShareMsgEscape(buf); // "::" -> ';' if ((tok = separate_token(buf, ':', &p)) == NULL) // header size return FALSE; if ((tok = separate_token(NULL, ':', &p)) == NULL) // fname return FALSE; info->SetFnameExt(tok); while ((ptr = strchr(tok, '?')) != NULL) // UNICODE ‚܂ł̎b’è *ptr = '_'; if (strlen(tok) >= MAX_PATH) return FALSE; if ((tok = separate_token(NULL, ':', &p)) == NULL) // size return FALSE; info->SetSize(hex2ll(tok)); if ((tok = separate_token(NULL, ':', &p)) == NULL) // attr return FALSE; info->SetAttr(strtoul(tok, 0, 16)); while ((tok = separate_token(NULL, ':', &p)) != NULL) // exattr { if ((ptr = strchr(tok, '=')) == NULL) continue; *ptr = 0; UINT exattr = strtoul(tok, 0, 16); UINT val = strtoul(ptr + 1, 0, 16); switch (exattr) { case IPMSG_FILE_MTIME: info->SetMtime(val); break; default: break; } } return TRUE; }