STDMETHODIMP CClassCommon::get_PublicKeySIGN(BSTR* pVal) { // TODO: 在此添加实现代码 long pb64_len = modp_b64_encode_len(SM2_BYTES_LEN * 2); char * pb64_data = (char *)malloc(pb64_len); pb64_len = modp_b64_encode(pb64_data, (char *)m_szPublicKeySIGN,SM2_BYTES_LEN * 2); ::FILE_LOG_STRING(file_log_name, "get_PublicKeySIGN"); ::FILE_LOG_STRING(file_log_name, "modp_b64_encode"); ::FILE_LOG_NUMBER(file_log_name, pb64_len); ::FILE_LOG_STRING(file_log_name, pb64_data); //long lLen = MultiByteToWideChar(CP_ACP,0,(LPCSTR)pb64_data,pb64_len,(LPWSTR)pVal,pb64_len * 2); BSTR wc_data = SysAllocStringByteLen(NULL, pb64_len * 2); MultiByteToWideChar(CP_ACP,0,(LPCSTR)pb64_data,pb64_len,(LPWSTR)wc_data,pb64_len); *pVal = wc_data; // 设置返回值指针。注:不释放内存 free(pb64_data); return S_OK; }
std::string& Base64::Encode(std::string &s) { std::string x(modp_b64_encode_len(s.size()), '\0'); size_t d = b64_stuff::modp_b64_encode(const_cast<char*>(x.data()), s.data(), (int)s.size()); x.erase(d, std::string::npos); s.swap(x); return s; }
bool appbase_push_frame(struct appbase *ab, const unsigned char *data, size_t length, struct timeval *timestamp) { CURLcode response_code; struct json_internal json; size_t b64_size = 0; char *b64_data; if (!ab || !ab->curl || !ab->url || !ab->json || !data || !length || !timestamp) return false; /* Transform raw frame data into base64 */ b64_size = modp_b64_encode_len(length); b64_data = ec_malloc(b64_size); if (modp_b64_encode(b64_data, (char *) data, length) == -1) return false; /* * Generate a JSON object with the format: * * { * "image": "<data>", * "sec": "<seconds>", * "usec": "<milliseconds>" * } */ json_object_object_add(ab->json, AB_KEY_IMAGE, json_object_new_string(b64_data)); json_object_object_add(ab->json, AB_KEY_SEC, json_object_new_int64(timestamp->tv_sec)); json_object_object_add(ab->json, AB_KEY_USEC, json_object_new_int64(timestamp->tv_usec)); json.json = json_object_to_json_string_ext(ab->json, JSON_C_TO_STRING_PLAIN); json.length = strlen(json.json); json.offset = 0; curl_easy_setopt(ab->curl, CURLOPT_URL, ab->url); curl_easy_setopt(ab->curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(ab->curl, CURLOPT_INFILESIZE, json.length); curl_easy_setopt(ab->curl, CURLOPT_READDATA, &json); curl_easy_setopt(ab->curl, CURLOPT_READFUNCTION, reader_cb); response_code = curl_easy_perform(ab->curl); /* * No need to free the JSON string. * We call json_object_put() on the root JSON object in appbase_close(), * and it will release the whole JSON object, including this string * for us. */ json.length = 0; json.offset = 0; free(b64_data); return (response_code == CURLE_OK); }
bool Base64Encode(const std::string& input, std::string* output) { std::string temp; temp.resize(modp_b64_encode_len(input.size())); // makes room for null byte // null terminates result since result is base64 text! int input_size = static_cast<int>(input.size()); int output_size= modp_b64_encode(&(temp[0]), input.data(), input_size); if (output_size < 0) return false; temp.resize(output_size); // strips off null byte output->swap(temp); return true; }
string EasyUtil::Base64Encode(const string &sInput) { std::string temp; temp.resize(modp_b64_encode_len(sInput.size())); // makes room for null byte // null terminates result since result is base64 text! int input_size = static_cast<int>(sInput.size()); int output_size = modp_b64_encode(&(temp[0]), sInput.data(), input_size); if (output_size < 0) return false; temp.resize(output_size); // strips off null byte; return temp; }
static bool Base64Encode(const std::string& input, std::string* output) { std::string temp; temp.resize(modp_b64_encode_len(input.size())); int input_size = static_cast<int>(input.size()); int output_size = modp_b64_encode(&(temp[0]), input.data(), input_size); if (output_size < 0) return false; temp.resize(output_size); output->swap(temp); return true; }
bool Base64Encode(const std::string& input, std::string* output) { std::string temp; temp.resize(modp_b64_encode_len(input.size())); // 预留空间分配空字节. // null结尾因为结果是base64的文本! int input_size = static_cast<int>(input.size()); int output_size = modp_b64_encode(&(temp[0]), input.data(), input_size); if(output_size < 0) { return false; } temp.resize(output_size); // 去除多余空字节 output->swap(temp); return true; }
STDMETHODIMP CClassCommon::get_sm2keys(BSTR* pVal) { // TODO: 在此添加实现代码 long keys_len; char * keys_value = NULL; long pb64_len; char * pb64_data = NULL; keys_len = sizeof(OPST_PCI_ECCrefPublicKey) + sizeof(OPST_PCI_ECCrefPrivateKey); keys_value = (char *)malloc(keys_len); memcpy(keys_value,&m_stPublicKey, sizeof(OPST_PCI_ECCrefPublicKey)); memcpy(keys_value + sizeof(OPST_PCI_ECCrefPublicKey),&m_stPrivateKey, sizeof(OPST_PCI_ECCrefPrivateKey)); pb64_len = modp_b64_encode_len(keys_len); pb64_data = (char *)malloc(pb64_len); pb64_len = modp_b64_encode(pb64_data, (char *)keys_value,keys_len); ::FILE_LOG_STRING(file_log_name, "modp_b64_encode"); ::FILE_LOG_NUMBER(file_log_name, pb64_len); ::FILE_LOG_STRING(file_log_name, pb64_data); //long lLen = MultiByteToWideChar(CP_ACP,0,(LPCSTR)pb64_data,pb64_len,(LPWSTR)pVal,pb64_len * 2); BSTR wc_data = SysAllocStringByteLen(NULL, pb64_len * 2); MultiByteToWideChar(CP_ACP,0,(LPCSTR)pb64_data,pb64_len,(LPWSTR)wc_data,pb64_len); *pVal = wc_data; // 设置返回值指针。注:不释放内存 free(keys_value); free(pb64_data); return S_OK; }
static ngx_int_t cache_loc_conf_options(ngx_conf_t *cf, passenger_loc_conf_t *conf) { ngx_uint_t i; ngx_keyval_t *env_vars; size_t unencoded_len; u_char *unencoded_buf; if (generated_cache_location_part(cf, conf) == 0) { return NGX_ERROR; } if (conf->env_vars != NULL) { size_t len = 0; u_char *buf; u_char *pos; /* Cache env vars data as base64-serialized string. * First, calculate the length of the unencoded data. */ unencoded_len = 0; env_vars = (ngx_keyval_t *) conf->env_vars->elts; for (i = 0; i < conf->env_vars->nelts; i++) { unencoded_len += env_vars[i].key.len + 1 + env_vars[i].value.len + 1; } /* Create the unecoded data. */ unencoded_buf = pos = (u_char *) malloc(unencoded_len); if (unencoded_buf == NULL) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "cannot allocate buffer of %z bytes for environment variables data", unencoded_len); return NGX_ERROR; } for (i = 0; i < conf->env_vars->nelts; i++) { pos = ngx_copy(pos, env_vars[i].key.data, env_vars[i].key.len); *pos = '\0'; pos++; pos = ngx_copy(pos, env_vars[i].value.data, env_vars[i].value.len); *pos = '\0'; pos++; } assert((size_t) (pos - unencoded_buf) == unencoded_len); /* Create base64-serialized string. */ buf = ngx_palloc(cf->pool, modp_b64_encode_len(unencoded_len)); if (buf == NULL) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "cannot allocate buffer of %z bytes for base64 encoding", modp_b64_encode_len(unencoded_len)); return NGX_ERROR; } len = modp_b64_encode((char *) buf, (const char *) unencoded_buf, unencoded_len); if (len == (size_t) -1) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "error during base64 encoding"); free(unencoded_buf); return NGX_ERROR; } conf->env_vars_cache.data = buf; conf->env_vars_cache.len = len; free(unencoded_buf); } return NGX_OK; }