Browse Source

Moved common "tohex" functionality to centralized location

master
Artyom Beilis 12 years ago
parent
commit
e62b429612
5 changed files with 44 additions and 25 deletions
  1. +26
    -0
      private/tohex.h
  2. +3
    -3
      src/http_file.cpp
  3. +6
    -7
      src/make_key.cpp
  4. +6
    -10
      src/session_sid.cpp
  5. +3
    -5
      src/util.cpp

+ 26
- 0
private/tohex.h View File

@@ -0,0 +1,26 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// 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<unsigned char const *>(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

+ 3
- 3
src/http_file.cpp View File

@@ -14,6 +14,7 @@
#include <stdlib.h>
#include <vector>

#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(),


+ 6
- 7
src/make_key.cpp View File

@@ -11,6 +11,8 @@
#include <iostream>
#include <fstream>

#include "tohex.h"

void help()
{
std::cerr <<
@@ -32,15 +34,12 @@ void help()

std::string make_key(int size)
{
std::vector<unsigned char> key(size,0);
std::vector<char> key(size,0);
std::vector<char> 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<size;i++) {
s+=part[(key[i]>>4) & 0xF];
s+=part[key[i] & 0xF];
}
cppcms::impl::tohex(&key[0],size,&res[0]);
std::string s = &res[0];
return s;
}



+ 6
- 10
src/session_sid.cpp View File

@@ -15,6 +15,9 @@
#include <stdio.h>
#include <time.h>

#include "tohex.h"


#include <cppcms/config.h>

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)


+ 3
- 5
src/util.cpp View File

@@ -11,6 +11,7 @@
#include <stdio.h>
#include <iterator>
#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<unsigned const char *>(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;
}




Loading…
Cancel
Save