Skip to content

Commit

Permalink
edit char2hex
Browse files Browse the repository at this point in the history
  • Loading branch information
hggq committed Jan 3, 2024
1 parent e663ab9 commit 5a337e9
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
4 changes: 4 additions & 0 deletions common/autocontrolmethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "admin/topics.h"
#include "admin/main.h"
#include "testformpost.h"
#include "teststr2int.h"
#include "testqrcode.h"
#include "imageapi.h"
#include "testpzcache.h"
Expand Down Expand Up @@ -205,6 +206,9 @@ namespace http
temp.regfun = testuploadpostfile;
methodcallback.emplace("addpostfile",temp);
temp.pre = nullptr;
temp.regfun = teststr2int;
methodcallback.emplace("teststr2int",temp);
temp.pre = nullptr;
temp.regfun = testqrcode;
methodcallback.emplace("testqrcode",temp);
temp.pre = nullptr;
Expand Down
12 changes: 12 additions & 0 deletions controller/include/teststr2int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#pragma once
#include <chrono>
#include <thread>
#include "httppeer.h"

namespace http
{


std::string teststr2int(std::shared_ptr<httppeer> peer);
}
25 changes: 25 additions & 0 deletions controller/src/teststr2int.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <chrono>
#include <thread>
#include <vector>
#include <list>
#include "httppeer.h"
#include "teststr2int.h"
#include "func.h"
namespace http
{
//@urlpath(null,teststr2int)
std::string teststr2int(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->getpeer();
std::string a = "-123456789";
client << "<p>str2int origin|" << a << "|</p>";
client << "<p>str2int result|" << str2int(a) << "|</p>";
a = "123456789";
client << "<p>str2int not result|" << str2int(a) << "|</p>";
client << "<p>str2int data(),size() result|" << str2int(a.data(), a.size()) << "|</p>";
client << "<p>char2hex result|" << char2hex((const unsigned char *)a.data(), a.size()) << "|</p>";
client << "<p>char2hex 4p result|" << char2hex((const unsigned char *)a.data(), a.size(), 4) << "|</p>";
return "";
}

}// namespace http
3 changes: 2 additions & 1 deletion vendor/httpserver/include/func.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ unsigned int mb_strlen(std::string_view);
std::map<std::string, std::string> filepath(std::string &);
struct stat filestat(std::string &);

long long str2int(std::string_view);
long long str2int(const char *source, unsigned int str_length);
std::string char2str(const unsigned char *source, unsigned int str_length);
std::string char2hex(const unsigned char *source, unsigned int str_length, unsigned char sp = 0);
std::string str2safepath(const char *source, unsigned int str_length);
std::string str2safefile(const char *source, unsigned int str_length);
std::string str2safemethold(const char *source, unsigned int str_length);
Expand Down
4 changes: 2 additions & 2 deletions vendor/httpserver/src/fastcgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ asio::awaitable<void> fastcgi::co_send()
}
else if (line_name.size() == 6 && str_casecmp(line_name, "Status"))
{
peer->status(str2int(&line_header[0], line_header.size()));
peer->status(str2int(line_header));
}
else if (line_name.size() == 10 && str_casecmp(line_name, "Set-Cookie"))
{
Expand All @@ -609,7 +609,7 @@ asio::awaitable<void> fastcgi::co_send()
}
else if (line_name.size() == 6 && str_casecmp(line_name, "Status"))
{
peer->status(str2int(&line_header[0], line_header.size()));
peer->status(str2int(line_header));
}
else if (line_name.size() == 10 && str_casecmp(line_name, "Set-Cookie"))
{
Expand Down
43 changes: 41 additions & 2 deletions vendor/httpserver/src/func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,37 @@ std::string html_encode(std::string_view str)
}
return temp;
}
long long str2int(std::string_view source)
{
long long temp = 0;
unsigned int qi = 0;
unsigned int length = source.size();
bool issub = false;
for (; qi < length; qi++)
{
if (source[qi] != 0x20)
{
break;
}
}
if (source[qi] == '-')
{
issub = true;
qi++;
}
for (; qi < length; qi++)
{
if (source[qi] < 0x3A && source[qi] > 0x2F)
{
temp = temp * 10 + (source[qi] - 0x30);
}
}
if (issub)
{
temp = ~temp + 1;
}
return temp;
}
long long str2int(const char *source, unsigned int str_length)
{
long long temp = 0;
Expand All @@ -1224,20 +1255,28 @@ long long str2int(const char *source, unsigned int str_length)
}
if (issub)
{
temp = 0 - temp;
temp = ~temp + 1;
}
return temp;
}
std::string char2str(const unsigned char *source, unsigned int str_length)
std::string char2hex(const unsigned char *source, unsigned int str_length, unsigned char sp)
{
static const unsigned char str[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
std::string obj;
unsigned int qi = 0;

for (; qi < str_length; qi++)
{
unsigned char tmc = source[qi];
obj.push_back(str[tmc >> 4 & 0x0F]);
obj.push_back(str[tmc & 0x0F]);
if (sp > 0)
{
if ((qi + 1) % sp == 0)
{
obj.push_back(0x20);
}
}
}
return obj;
}
Expand Down

0 comments on commit 5a337e9

Please sign in to comment.