From e62b4296121b89dd18f02ebba212b9a6d3276445 Mon Sep 17 00:00:00 2001 From: Artyom Beilis Date: Sat, 11 Feb 2012 12:02:19 +0000 Subject: [PATCH] Moved common "tohex" functionality to centralized location --- private/tohex.h | 26 ++++++++++++++++++++++++++ src/http_file.cpp | 6 +++--- src/make_key.cpp | 13 ++++++------- src/session_sid.cpp | 16 ++++++---------- src/util.cpp | 8 +++----- 5 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 private/tohex.h diff --git a/private/tohex.h b/private/tohex.h new file mode 100644 index 0000000..a08e4dd --- /dev/null +++ b/private/tohex.h @@ -0,0 +1,26 @@ +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) +// +// See accompanying file COPYING.TXT file for licensing details. +// +/////////////////////////////////////////////////////////////////////////////// +#ifndef CPPCMS_IMPL_TOHEX_H +#define CPPCMS_IMPL_TOHEX_H +namespace cppcms { + namespace impl { + inline void tohex(void const *vptr,size_t len,char *out) + { + unsigned char const *p=static_cast(vptr); + while(len>0) { + static char const table[17]="0123456789abcdef"; + unsigned char v=*p++; + *out++ = table[(v >> 4) & 0xF]; + *out++ = table[(v & 0xF)]; + len--; + } + *out++ = '\0'; + } + } +} +#endif diff --git a/src/http_file.cpp b/src/http_file.cpp index 168cc64..e82c2ed 100644 --- a/src/http_file.cpp +++ b/src/http_file.cpp @@ -14,6 +14,7 @@ #include #include +#include "tohex.h" namespace cppcms { namespace http { @@ -155,11 +156,10 @@ void file::move_to_file() tmp_file_name_ = tmp_dir + "/cppcms_uploads_"; urandom_device rnd; - unsigned char buf[16]; + char buf[16]; char rand[33]={0}; rnd.generate(buf,16); - for(unsigned i=0;i<16;i++) - sprintf(rand+i*2,"%02x",buf[i]); + impl::tohex(buf,sizeof(buf),rand); tmp_file_name_.append(rand); tmp_file_name_+=".tmp"; file_.open(tmp_file_name_.c_str(), diff --git a/src/make_key.cpp b/src/make_key.cpp index 737ff8a..de50ba0 100644 --- a/src/make_key.cpp +++ b/src/make_key.cpp @@ -11,6 +11,8 @@ #include #include +#include "tohex.h" + void help() { std::cerr << @@ -32,15 +34,12 @@ void help() std::string make_key(int size) { - std::vector key(size,0); + std::vector key(size,0); + std::vector res(size*2+1,0); cppcms::urandom_device r; r.generate(&key[0],size); - std::string s; - static const char part[]="0123456789abcdef"; - for(int i=0;i>4) & 0xF]; - s+=part[key[i] & 0xF]; - } + cppcms::impl::tohex(&key[0],size,&res[0]); + std::string s = &res[0]; return s; } diff --git a/src/session_sid.cpp b/src/session_sid.cpp index 4a69de3..43e406e 100644 --- a/src/session_sid.cpp +++ b/src/session_sid.cpp @@ -15,6 +15,9 @@ #include #include +#include "tohex.h" + + #include namespace cppcms { @@ -34,19 +37,12 @@ session_sid::~session_sid() std::string session_sid::get_new_sid() { - unsigned char sid[16]; + char sid[16]; char res[33]; urandom_device rnd; rnd.generate(sid,sizeof(sid)); - - for(int i=0;i<16;i++) { - #ifdef CPPCMS_HAVE_SNPRINTF - snprintf(res+i*2,3,"%02x",sid[i]); - #else - sprintf(res+i*2,"%02x",sid[i]); - #endif - } - return std::string(res); + cppcms::impl::tohex(sid,sizeof(sid),res); + return res; } bool session_sid::valid_sid(std::string const &cookie,std::string &id) diff --git a/src/util.cpp b/src/util.cpp index a2f2adc..fd1442c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -11,6 +11,7 @@ #include #include #include "md5.h" +#include "tohex.h" namespace cppcms { namespace util { @@ -147,11 +148,8 @@ std::string md5hex(std::string const &in) md5_append(&state,reinterpret_cast(in.c_str()),in.size()); md5_finish(&state,data); char buf[33]={0}; - for(int i=0;i<16;i++) { - unsigned val=data[i]; - sprintf(buf+i*2,"%02x",val); - } - return std::string(buf,32); + impl::tohex(data,sizeof(data),buf); + return buf; }