const char* getMSError(DWORD error, char *buf, unsigned len){ LPVOID lpMsgBuf; char *tmp = NULL; char *ret = NULL; if(buf == NULL) goto cleanup; if(!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL)){ goto cleanup; } tmp = (char*)lpMsgBuf; tmp[strlen(tmp)-2] = 0; if(KSI_strncpy(buf, tmp, len) == NULL) goto cleanup; ret = buf; cleanup: LocalFree(lpMsgBuf); return ret; }
static int addVerificationStepResult(KSI_VerificationResult *info, KSI_VerificationStep step, const char *desc, bool succeeded) { int res = KSI_UNKNOWN_ERROR; KSI_VerificationStepResult *result = NULL; if (info == NULL) { res = KSI_INVALID_ARGUMENT; goto cleanup; } if (!succeeded) { info->stepsFailed |= step; } info->stepsPerformed |= step; result = info->steps + info->steps_len++; result->step = step; result->succeeded = succeeded; result->description[0] = '\0'; if (desc != NULL) { KSI_strncpy(result->description, desc, sizeof(result->description)); } res = KSI_OK; cleanup: return res; }
int KSI_ERR_getBaseErrorMessage(KSI_CTX *ctx, char *buf, size_t len, int *error, int *ext){ KSI_ERR *err = NULL; if (ctx == NULL || buf == NULL){ return KSI_INVALID_ARGUMENT; } err = ctx->errors; if (ctx->errors_count) { KSI_strncpy(buf, err->message, len); if (error != NULL) *error = err->statusCode; if (ext != NULL) *ext = err->extErrorCode; } else { KSI_strncpy(buf, KSI_getErrorString(KSI_OK), len); if (error != NULL) *error = KSI_OK; if (ext != NULL) *ext = 0; } return KSI_OK; }
void KSI_ERR_push(KSI_CTX *ctx, int statusCode, long extErrorCode, const char *fileName, unsigned int lineNr, const char *message) { KSI_ERR *ctxErr = NULL; const char *tmp = NULL; /* Do nothing if the context is missing. */ if (ctx == NULL) return; /* Do nothing if there's no error. */ if (statusCode == KSI_OK) return; /* Get the error container to use for storage. */ ctxErr = &ctx->errors[ctx->errors_count % ctx->errors_size]; ctxErr->statusCode = statusCode; ctxErr->extErrorCode = extErrorCode; ctxErr->lineNr = lineNr; tmp = KSI_strnvl(fileName); KSI_strncpy(ctxErr->fileName, tmp, sizeof(ctxErr->fileName)); tmp = KSI_strnvl(message); KSI_strncpy(ctxErr->message, tmp, sizeof(ctxErr->message)); ctx->errors_count++; }
int KSI_PublicationsFile_serialize(KSI_CTX *ctx, KSI_PublicationsFile *pubFile, char **raw, size_t *raw_len) { int res; const unsigned char *buf = NULL; size_t buf_len = 0; KSI_TLV *tlv = NULL; unsigned char *tmp = NULL; size_t tmp_len = 0; size_t sig_len = 0; KSI_ERR_clearErrors(ctx); if (ctx == NULL || pubFile == 0 || raw == NULL || raw_len == NULL) { KSI_pushError(ctx, res = KSI_INVALID_ARGUMENT, NULL); goto cleanup; } /** * Create TLV 0x700 that contains nested list of publication file TLVs. * Calculate signed data length assuming that signature TLV is always the last. */ res = KSI_TLV_new(ctx, 0x700, 0, 0, &tlv); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TlvTemplate_construct(ctx, tlv, pubFile, KSI_TLV_TEMPLATE(KSI_PublicationsFile)); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = publicationsFileTLV_getSignatureTLVLength(tlv, &sig_len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } res = KSI_TLV_getRawValue(tlv, &buf, &buf_len); if (res != KSI_OK) { KSI_pushError(ctx, res, NULL); goto cleanup; } /** * Append raw value to publication file header. Copy raw publication file into * internal and external buffer. */ tmp_len = buf_len + sizeof(PUB_FILE_HEADER_ID) - 1; tmp = (unsigned char *) KSI_malloc(tmp_len); if (tmp == NULL) { KSI_pushError(ctx, res = KSI_OUT_OF_MEMORY, NULL); goto cleanup; } if (KSI_strncpy((char *)tmp, PUB_FILE_HEADER_ID, tmp_len) == NULL) { KSI_pushError(ctx, res = KSI_UNKNOWN_ERROR, NULL); goto cleanup; } memcpy(tmp + sizeof(PUB_FILE_HEADER_ID) - 1, buf, buf_len); if (pubFile->raw != NULL) KSI_free(pubFile->raw); pubFile->raw = tmp; pubFile->raw_len = tmp_len; pubFile->signedDataLength = tmp_len - sig_len; tmp = NULL; tmp = (unsigned char *) KSI_malloc(pubFile->raw_len); if (tmp == NULL) { KSI_pushError(ctx, res = KSI_OUT_OF_MEMORY, NULL); goto cleanup; } memcpy(tmp, pubFile->raw, pubFile->raw_len); *raw = (char *)tmp; *raw_len = pubFile->raw_len; tmp = NULL; res = KSI_OK; cleanup: KSI_TLV_free(tlv); KSI_free(tmp); return res; }