hewei.it 5 سال پیش
والد
کامیت
abf41e1dba
16فایلهای تغییر یافته به همراه224 افزوده شده و 254 حذف شده
  1. 0 1
      README.md
  2. 2 4
      base/hbuf.h
  3. 0 15
      base/herr.h
  4. 17 20
      base/hlog.c
  5. 1 1
      base/hlog.h
  6. 2 2
      base/hversion.h
  7. 3 1
      examples/hmain_test.cpp
  8. 49 52
      examples/nmap.cpp
  9. 3 3
      http/HttpMessage.h
  10. 2 2
      http/http_content.cpp
  11. 4 2
      http/http_content.h
  12. 0 1
      hv.h
  13. 27 28
      readme_cn.md
  14. 102 110
      utils/base64.c
  15. 9 0
      utils/md5.c
  16. 3 12
      utils/md5.h

+ 0 - 1
README.md

@@ -20,7 +20,6 @@ but simpler apis and richer protocols.
     - ftp
     - smtp
 - apps
-    - ls,mkdir_p,rmdir_p
     - ifconfig
     - ping
     - nc

+ 2 - 4
base/hbuf.h

@@ -56,9 +56,7 @@ public:
     void*  data() { return base; }
     size_t size() { return len; }
 
-    bool isNull() {
-        return base == NULL || len == 0;
-    }
+    bool isNull() { return base == NULL || len == 0; }
 
     void cleanup() {
         if (cleanup_) {
@@ -102,7 +100,7 @@ public:
     HVLBuf(size_t cap) : HBuf(cap) {_offset = _size = 0;}
     virtual ~HVLBuf() {}
 
-    char* data() { return base+_offset; }
+    char* data() { return base + _offset; }
     size_t size() { return _size; }
 
     void push_front(void* ptr, size_t len) {

+ 0 - 15
base/herr.h

@@ -50,8 +50,6 @@
     \
     F(1400, REQUEST,        "Bad request")      \
     F(1401, RESPONSE,       "Bad response")     \
-    \
-    F(1429, BUSY,           "Busy")             \
 
 // [-1xxx]
 #define FOREACH_ERR_FUNC(F)   \
@@ -72,18 +70,6 @@
     F(-1020,    SETSOCKOPT, "setsockopt() error")   \
     F(-1021,    GETSOCKOPT, "getsockopt() error")   \
 
-// [3xxx]
-#define FOREACH_ERR_SERVICE(F)  \
-    F(3000, RESOURCE_NOT_FOUND,     "resource not found")   \
-    F(3001, GROUP_NOT_FOUND,        "group not found")      \
-    F(3002, PERSON_NOT_FOUND,       "person not found")     \
-    F(3003, FACE_NOT_FOUND,         "face not found")       \
-    F(3004, DEVICE_NOT_FOUND,       "device not found")     \
-    \
-    F(3010, DEVICE_DISCONNECT,      "device disconnect")    \
-    F(3011, DEVICE_DISABLE,         "device disable")       \
-    F(3012, DEVICE_BUSY,            "device busy")          \
-
 // grpc [4xxx]
 #define FOREACH_ERR_GRPC(F)     \
     F(4000, GRPC_FIRST,                     "grpc no error")                \
@@ -106,7 +92,6 @@
 #define FOREACH_ERR(F)      \
     FOREACH_ERR_COMMON(F)   \
     FOREACH_ERR_FUNC(F)     \
-    FOREACH_ERR_SERVICE(F)  \
     FOREACH_ERR_GRPC(F)     \
 
 #undef ERR_OK // prevent conflict

+ 17 - 20
base/hlog.c

@@ -187,7 +187,7 @@ const char* logger_get_cur_file(logger_t* logger) {
     return logger->cur_logfile;
 }
 
-static void ts_logfile(const char* filepath, time_t ts, char* buf, int len) {
+static void logfile_name(const char* filepath, time_t ts, char* buf, int len) {
     struct tm* tm = localtime(&ts);
     snprintf(buf, len, "%s-%04d-%02d-%02d.log",
             filepath,
@@ -196,7 +196,7 @@ static void ts_logfile(const char* filepath, time_t ts, char* buf, int len) {
             tm->tm_mday);
 }
 
-static FILE* shift_logfile(logger_t* logger) {
+static FILE* logfile_shift(logger_t* logger) {
     time_t ts_now = time(NULL);
     int interval_days = logger->last_logfile_ts == 0 ? 0 : (ts_now+s_gmtoff) / SECONDS_PER_DAY - (logger->last_logfile_ts+s_gmtoff) / SECONDS_PER_DAY;
     if (logger->fp_ == NULL || interval_days > 0) {
@@ -215,14 +215,14 @@ static FILE* shift_logfile(logger_t* logger) {
                 // remove [today-interval_days, today-remain_days] logfile
                 for (int i = interval_days; i >= logger->remain_days; --i) {
                     time_t ts_rm  = ts_now - i * SECONDS_PER_DAY;
-                    ts_logfile(logger->filepath, ts_rm, rm_logfile, sizeof(rm_logfile));
+                    logfile_name(logger->filepath, ts_rm, rm_logfile, sizeof(rm_logfile));
                     remove(rm_logfile);
                 }
             }
             else {
                 // remove today-remain_days logfile
                 time_t ts_rm  = ts_now - logger->remain_days * SECONDS_PER_DAY;
-                ts_logfile(logger->filepath, ts_rm, rm_logfile, sizeof(rm_logfile));
+                logfile_name(logger->filepath, ts_rm, rm_logfile, sizeof(rm_logfile));
                 remove(rm_logfile);
             }
         }
@@ -230,7 +230,7 @@ static FILE* shift_logfile(logger_t* logger) {
 
     // open today logfile
     if (logger->fp_ == NULL) {
-        ts_logfile(logger->filepath, ts_now, logger->cur_logfile, sizeof(logger->cur_logfile));
+        logfile_name(logger->filepath, ts_now, logger->cur_logfile, sizeof(logger->cur_logfile));
         logger->fp_ = fopen(logger->cur_logfile, "a");
         logger->last_logfile_ts = ts_now;
     }
@@ -258,6 +258,16 @@ static FILE* shift_logfile(logger_t* logger) {
     return logger->fp_;
 }
 
+static void logfile_write(logger_t* logger, const char* buf, int len) {
+    FILE* fp = logfile_shift(logger);
+    if (fp) {
+        fwrite(buf, 1, len, fp);
+        if (logger->enable_fsync) {
+            fflush(fp);
+        }
+    }
+}
+
 int logger_print(logger_t* logger, int level, const char* fmt, ...) {
     if (level < logger->level)
         return -10;
@@ -324,13 +334,7 @@ int logger_print(logger_t* logger, int level, const char* fmt, ...) {
         logger->handler(level, buf, len);
     }
     else {
-        FILE* fp = shift_logfile(logger);
-        if (fp) {
-            fwrite(buf, 1, len, fp);
-            if (logger->enable_fsync) {
-                fflush(fp);
-            }
-        }
+        logfile_write(logger, buf, len);
     }
 
     hmutex_unlock(&logger->mutex_);
@@ -354,12 +358,5 @@ void stderr_logger(int loglevel, const char* buf, int len) {
 }
 
 void file_logger(int loglevel, const char* buf, int len) {
-    logger_t* logger = hv_default_logger();
-    FILE* fp = shift_logfile(logger);
-    if (fp) {
-        fwrite(buf, 1, len, fp);
-        if (logger->enable_fsync) {
-            fflush(fp);
-        }
-    }
+    logfile_write(hv_default_logger(), buf, len);
 }

+ 1 - 1
base/hlog.h

@@ -103,7 +103,7 @@ HV_EXPORT const char* logger_get_cur_file(logger_t* logger);
 HV_EXPORT logger_t* hv_default_logger();
 
 // macro hlog*
-#define hlog hv_default_logger()
+#define hlog                            hv_default_logger()
 #define hlog_set_file(filepath)         logger_set_file(hlog, filepath)
 #define hlog_set_level(level)           logger_set_level(hlog, level)
 #define hlog_set_level_by_str(level)    logger_set_level_by_str(hlog, level)

+ 2 - 2
base/hversion.h

@@ -7,8 +7,8 @@
 BEGIN_EXTERN_C
 
 #define HV_VERSION_MAJOR    1
-#define HV_VERSION_MINOR    20
-#define HV_VERSION_PATCH    8
+#define HV_VERSION_MINOR    0
+#define HV_VERSION_PATCH    0
 
 #define HV_VERSION_STRING   STRINGIFY(HV_VERSION_MAJOR) "." \
                             STRINGIFY(HV_VERSION_MINOR) "." \

+ 3 - 1
examples/hmain_test.cpp

@@ -149,6 +149,7 @@ int main(int argc, char** argv) {
         exit(ret);
     }
 
+    /*
     printf("---------------arg------------------------------\n");
     printf("%s\n", g_main_ctx.cmdline);
     for (auto& pair : g_main_ctx.arg_kv) {
@@ -164,6 +165,7 @@ int main(int argc, char** argv) {
         printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
     }
     printf("================================================\n");
+    */
 
     // help
     if (get_arg("h")) {
@@ -222,6 +224,6 @@ void worker_fn(void* userdata) {
     long num = (long)(intptr_t)(userdata);
     while (1) {
         printf("num=%ld pid=%ld tid=%ld\n", num, hv_getpid(), hv_gettid());
-        hv_delay(10000);
+        hv_delay(60000);
     }
 }

+ 49 - 52
examples/nmap.cpp

@@ -4,12 +4,10 @@
 #include "nmap.h"
 #include "hthreadpool.h"
 
-/*
 int host_discover_task(std::string segment, void* nmap) {
     Nmap* hosts= (Nmap*)nmap;
     return host_discover(segment.c_str(), hosts);
 }
-*/
 
 int main(int argc, char* argv[]) {
     if (argc < 2) {
@@ -31,17 +29,16 @@ int main(int argc, char* argv[]) {
         }
     }
 
+    Nmap hosts;
+    char ip[INET_ADDRSTRLEN];
     if (n == 24) {
-        Nmap nmap;
-        return host_discover(segment, &nmap);
+        host_discover(segment, &hosts);
     }
-
-    char ip[INET_ADDRSTRLEN];
-    if (n == 16) {
+    else if (n == 16) {
         Nmap segs;
         int up_nsegs = segment_discover(segment, &segs);
         if (up_nsegs == 0) return 0;
-        Nmap hosts;
+#if 1
         for (auto& pair : segs) {
             if (pair.second == 1) {
                 uint32_t addr = pair.first;
@@ -54,62 +51,62 @@ int main(int argc, char* argv[]) {
             }
         }
         nmap_discover(&hosts);
-        // filter up hosts
-        std::vector<uint32_t> up_hosts;
-        for (auto& pair : hosts) {
-            if (pair.second == 1) {
-                up_hosts.push_back(pair.first);
-            }
-        }
-        // ThreadPool + host_discover
-        /*
+#else
         if (up_nsegs == 1) {
             for (auto& pair : segs) {
                 if (pair.second == 1) {
                     inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
-                    Nmap hosts;
-                    return host_discover(ip, &hosts);
+                    host_discover(ip, &hosts);
                 }
             }
         }
-        Nmap* hosts = new Nmap[up_nsegs];
-        // use ThreadPool
-        HThreadPool tp(4);
-        tp.start();
-        std::vector<std::future<int>> futures;
-        int i = 0;
-        for (auto& pair : nmap) {
-            if (pair.second == 1) {
-                inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
-                auto future = tp.commit(host_discover_task, std::string(ip), &hosts[i++]);
-                futures.push_back(std::move(future));
+        else {
+            // ThreadPool + host_discover
+            Nmap* hosts = new Nmap[up_nsegs];
+            // use ThreadPool
+            HThreadPool tp(4);
+            tp.start();
+            std::vector<std::future<int>> futures;
+            int i = 0;
+            for (auto& pair : segs) {
+                if (pair.second == 1) {
+                    inet_ntop(AF_INET, (void*)&pair.first, ip, sizeof(ip));
+                    auto future = tp.commit(host_discover_task, std::string(ip), &hosts[i++]);
+                    futures.push_back(std::move(future));
+                }
             }
-        }
-        // wait all task done
-        int nhosts = 0;
-        for (auto& future : futures) {
-            nhosts += future.get();
-        }
-        // filter up hosts
-        std::vector<uint32_t> up_hosts;
-        for (int i = 0; i < up_nsegs; ++i) {
-            Nmap& nmap = hosts[i];
-            for (auto& host : nmap) {
-                if (host.second == 1) {
-                    up_hosts.push_back(host.first);
+            // wait all task done
+            int nhosts = 0;
+            for (auto& future : futures) {
+                nhosts += future.get();
+            }
+            // filter up hosts
+            std::vector<uint32_t> up_hosts;
+            for (int i = 0; i < up_nsegs; ++i) {
+                Nmap& nmap = hosts[i];
+                for (auto& host : nmap) {
+                    if (host.second == 1) {
+                        up_hosts.push_back(host.first);
+                    }
                 }
             }
+            delete[] hosts;
         }
-        delete[] hosts;
-        */
-        // print up hosts
-        printf("Up hosts %lu:\n", up_hosts.size());
-        for (auto& host : up_hosts) {
-            inet_ntop(AF_INET, (void*)&host, ip, sizeof(ip));
-            printf("%s\n", ip);
-        }
-        return up_hosts.size();
+#endif
     }
 
+    // filter up hosts
+    std::vector<uint32_t> up_hosts;
+    for (auto& pair : hosts) {
+        if (pair.second == 1) {
+            up_hosts.push_back(pair.first);
+        }
+    }
+    // print up hosts
+    printf("Up hosts %lu:\n", up_hosts.size());
+    for (auto& host : up_hosts) {
+        inet_ntop(AF_INET, (void*)&host, ip, sizeof(ip));
+        printf("%s\n", ip);
+    }
     return 0;
 }

+ 3 - 3
http/HttpMessage.h

@@ -12,7 +12,7 @@
 typedef std::map<std::string, std::string, StringCaseLess>  http_headers;
 typedef std::string                                         http_body;
 
-struct NetAddr {
+struct HNetAddr {
     std::string     ip;
     int             port;
 
@@ -35,7 +35,7 @@ public:
     int                 content_length;
     http_content_type   content_type;
 #ifndef WITHOUT_HTTP_CONTENT
-    Json                json;       // APPLICATION_JSON
+    hv::Json            json;       // APPLICATION_JSON
     MultiPart           form;       // MULTIPART_FORM_DATA
     hv::KeyValue        kv;         // X_WWW_FORM_URLENCODED
 
@@ -150,7 +150,7 @@ public:
     std::string         path;
     QueryParams         query_params;
     // client_addr
-    NetAddr             client_addr;
+    HNetAddr            client_addr;
 
     HttpRequest() : HttpMessage() {
         type = HTTP_REQUEST;

+ 2 - 2
http/http_content.cpp

@@ -238,11 +238,11 @@ int parse_multipart(std::string& str, MultiPart& mp, const char* boundary) {
     return nparse == str.size() ? 0 : -1;
 }
 
-std::string dump_json(Json& json) {
+std::string dump_json(hv::Json& json) {
     return json.dump();
 }
 
-int parse_json(const char* str, Json& json, std::string& errmsg) {
+int parse_json(const char* str, hv::Json& json, std::string& errmsg) {
     try {
         json = nlohmann::json::parse(str);
     }

+ 4 - 2
http/http_content.h

@@ -54,11 +54,13 @@ HV_EXPORT int         parse_multipart(std::string& str, MultiPart& mp, const cha
 // Json
 // https://github.com/nlohmann/json
 #include "json.hpp"
+namespace hv { // NOTE: Avoid conflict with jsoncpp
 using Json = nlohmann::json;
 // using Json = nlohmann::ordered_json;
+}
 
-HV_EXPORT std::string dump_json(Json& json);
-HV_EXPORT int         parse_json(const char* str, Json& json, std::string& errmsg);
+HV_EXPORT std::string dump_json(hv::Json& json);
+HV_EXPORT int         parse_json(const char* str, hv::Json& json, std::string& errmsg);
 #endif
 
 #endif // HTTP_CONTENT_H_

+ 0 - 1
hv.h

@@ -28,7 +28,6 @@
 
 #include "hlog.h"
 #include "hbuf.h"
-#include "hgui.h"
 
 // cpp
 #ifdef __cplusplus

+ 27 - 28
readme_cn.md

@@ -2,8 +2,7 @@
 
 ## 简介
 
-`libhv`是一个类似于`libevent, libev, libuv`的跨平台事件循环库,
-提供了更加简单的API接口和更加丰富的协议。
+`libhv`是一个类似于`libevent、libev、libuv`的跨平台网络库,提供了更简单的接口和更丰富的协议。
 
 ## 特征
 
@@ -19,7 +18,6 @@
     - ftp
     - smtp
 - apps
-    - ls,mkdir_p,rmdir_p
     - ifconfig
     - ping
     - nc
@@ -237,10 +235,6 @@ cd echo-servers
 **echo-servers/benchmark**<br>
 ![echo-servers](html/downloads/echo-servers.png)
 
-注:因为客户端和服务端测试位于同一台机器,上图结果仅供参考。总的来说,这些库性能接近,各有千秋。
-
-## 模块
-
 ### 数据结构
 - array.h:       动态数组
 - list.h:        链表
@@ -248,44 +242,48 @@ cd echo-servers
 - heap.h:        堆
 
 ### base
+- hv.h:         总头文件
+- hexport.h:     导出宏
 - hplatform.h:   平台相关宏
-- hdef.h:        宏定义
+- hdef.h:        常用宏定义
 - hatomic.h:     原子操作
-- hversion.h:    版本
+- herr.h:        错误码
+- htime.h:       时间日期
+- hmath.h:       数学函数
 - hbase.h:       基本接口
+- hversion.h:    版本
 - hsysinfo.h:    系统信息
-- hproc.h:       子进程/线程类
-- hmath.h:       math扩展函数
-- htime.h:       时间
-- herr.h:        错误码
-- hlog.h:        日志
-- hmutex.h:     同步锁
+- hproc.h:       进程
 - hthread.h:    线程
-- hsocket.h:     socket操作
-- hbuf.h:        缓存类
-- hurl.h:        URL转义
-- hgui.h:        gui相关定义
+- hmutex.h:     互斥锁
+- hsocket.h:     套接字
+- hssl.h:        SSL/TLS加密通信
+- hlog.h:        日志
+- hbuf.h:        缓存
 - hstring.h:     字符串
 - hvar.h:        var变量
 - hobj.h:        对象基类
 - hfile.h:       文件类
 - hdir.h:        ls实现
-- hscope.h:      作用域RAII机制
+- hurl.h:        URL相关
+- hscope.h:      作用域
 - hthreadpool.h: 线程池
 - hobjectpool.h: 对象池
 - ifconfig.h:    ifconfig实现
 
 ### utils
-- hmain.h:       main_ctx: arg env
+- hmain.h:       命令行解析
 - hendian.h:     大小端
 - iniparser.h:   ini解析
 - singleton.h:   单例模式
-- md5.h
-- base64.h
-- json.hpp
+- md5.h:         MD5数字摘要
+- base64.h:      base64编码
+- json.hpp:      json解析
 
 ### event
 - hloop.h:       事件循环
+- nlog.h:        网络日志
+- nmap.h:        nmap实现
 
 #### iowatcher
 - EVENT_SELECT
@@ -299,10 +297,11 @@ cd echo-servers
 - http_client.h: http客户端
 - HttpServer.h:  http服务端
 
-### 其它
-
-- hv.h:         总头文件
-- Makefile.in:   通用Makefile模板
+### protocol
+- dns.h:         DNS域名查询
+- icmp.h:        ping实现
+- ftp.h:         FTP文件传输协议
+- smtp.h:        SMTP邮件传输协议
 
 ## 学习资料
 

+ 102 - 110
utils/base64.c

@@ -1,135 +1,127 @@
-/* This is a public domain base64 implementation written by WEI Zhicheng. */
-
 #include <stdio.h>
 
 #include "base64.h"
 
 /* BASE 64 encode table */
 static const char base64en[] = {
-	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
-	'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
-	'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
-	'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
-	'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
-	'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
-	'w', 'x', 'y', 'z', '0', '1', '2', '3',
-	'4', '5', '6', '7', '8', '9', '+', '/',
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+    'w', 'x', 'y', 'z', '0', '1', '2', '3',
+    '4', '5', '6', '7', '8', '9', '+', '/',
 };
 
-#define BASE64_PAD	'='
-
-
-#define BASE64DE_FIRST	'+'
-#define BASE64DE_LAST	'z'
+#define BASE64_PAD      '='
+#define BASE64DE_FIRST  '+'
+#define BASE64DE_LAST   'z'
 /* ASCII order for BASE 64 decode, -1 in unused character */
 static const signed char base64de[] = {
-	/* '+', ',', '-', '.', '/', '0', '1', '2', */ 
-	    62,  -1,  -1,  -1,  63,  52,  53,  54,
+    /* '+', ',', '-', '.', '/', '0', '1', '2', */
+        62,  -1,  -1,  -1,  63,  52,  53,  54,
 
-	/* '3', '4', '5', '6', '7', '8', '9', ':', */
-	    55,  56,  57,  58,  59,  60,  61,  -1,
+    /* '3', '4', '5', '6', '7', '8', '9', ':', */
+        55,  56,  57,  58,  59,  60,  61,  -1,
 
-	/* ';', '<', '=', '>', '?', '@', 'A', 'B', */
-	    -1,  -1,  -1,  -1,  -1,  -1,   0,   1, 
+    /* ';', '<', '=', '>', '?', '@', 'A', 'B', */
+        -1,  -1,  -1,  -1,  -1,  -1,   0,   1,
 
-	/* 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', */
-	     2,   3,   4,   5,   6,   7,   8,   9,
+    /* 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', */
+         2,   3,   4,   5,   6,   7,   8,   9,
 
-	/* 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', */ 
-	    10,  11,  12,  13,  14,  15,  16,  17,
+    /* 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', */
+        10,  11,  12,  13,  14,  15,  16,  17,
 
-	/* 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
-	    18,  19,  20,  21,  22,  23,  24,  25,
+    /* 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
+        18,  19,  20,  21,  22,  23,  24,  25,
 
-	/* '[', '\', ']', '^', '_', '`', 'a', 'b', */ 
-	    -1,  -1,  -1,  -1,  -1,  -1,  26,  27,
+    /* '[', '\', ']', '^', '_', '`', 'a', 'b', */
+        -1,  -1,  -1,  -1,  -1,  -1,  26,  27,
 
-	/* 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', */ 
-	    28,  29,  30,  31,  32,  33,  34,  35,
+    /* 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', */
+        28,  29,  30,  31,  32,  33,  34,  35,
 
-	/* 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', */
-	    36,  37,  38,  39,  40,  41,  42,  43,
+    /* 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', */
+        36,  37,  38,  39,  40,  41,  42,  43,
 
-	/* 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
-	    44,  45,  46,  47,  48,  49,  50,  51,
+    /* 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
+        44,  45,  46,  47,  48,  49,  50,  51,
 };
 
-int
-base64_encode(const unsigned char *in, unsigned int inlen, char *out)
-{
-	unsigned int i, j;
-
-	for (i = j = 0; i < inlen; i++) {
-		int s = i % 3; 			/* from 6/gcd(6, 8) */
-
-		switch (s) {
-		case 0:
-			out[j++] = base64en[(in[i] >> 2) & 0x3F];
-			continue;
-		case 1:
-			out[j++] = base64en[((in[i-1] & 0x3) << 4) + ((in[i] >> 4) & 0xF)];
-			continue;
-		case 2:
-			out[j++] = base64en[((in[i-1] & 0xF) << 2) + ((in[i] >> 6) & 0x3)];
-			out[j++] = base64en[in[i] & 0x3F];
-		}
-	}
-
-	/* move back */
-	i -= 1;
-
-	/* check the last and add padding */
-	if ((i % 3) == 0) {
-		out[j++] = base64en[(in[i] & 0x3) << 4];
-		out[j++] = BASE64_PAD;
-		out[j++] = BASE64_PAD;
-	} else if ((i % 3) == 1) {
-		out[j++] = base64en[(in[i] & 0xF) << 2];
-		out[j++] = BASE64_PAD;
-	}
-
-	return BASE64_OK;
+int base64_encode(const unsigned char *in, unsigned int inlen, char *out) {
+    unsigned int i, j;
+
+    for (i = j = 0; i < inlen; i++) {
+        int s = i % 3;
+
+        switch (s) {
+        case 0:
+            out[j++] = base64en[(in[i] >> 2) & 0x3F];
+            continue;
+        case 1:
+            out[j++] = base64en[((in[i-1] & 0x3) << 4) + ((in[i] >> 4) & 0xF)];
+            continue;
+        case 2:
+            out[j++] = base64en[((in[i-1] & 0xF) << 2) + ((in[i] >> 6) & 0x3)];
+            out[j++] = base64en[in[i] & 0x3F];
+        }
+    }
+
+    /* move back */
+    i -= 1;
+
+    /* check the last and add padding */
+    if ((i % 3) == 0) {
+        out[j++] = base64en[(in[i] & 0x3) << 4];
+        out[j++] = BASE64_PAD;
+        out[j++] = BASE64_PAD;
+    } else if ((i % 3) == 1) {
+        out[j++] = base64en[(in[i] & 0xF) << 2];
+        out[j++] = BASE64_PAD;
+    }
+
+    return BASE64_OK;
 }
 
-int
-base64_decode(const char *in, unsigned int inlen, unsigned char *out)
-{
-	unsigned int i, j;
-
-	for (i = j = 0; i < inlen; i++) {
-		int c;
-		int s = i % 4; 			/* from 8/gcd(6, 8) */
-
-		if (in[i] == '=')
-			return BASE64_OK;
-
-		if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST ||
-		    (c = base64de[in[i] - BASE64DE_FIRST]) == -1)
-			return BASE64_INVALID;
-
-		switch (s) {
-		case 0:
-			out[j] = ((unsigned int)c << 2) & 0xFF;
-			continue;
-		case 1:
-			out[j++] += ((unsigned int)c >> 4) & 0x3;
-
-			/* if not last char with padding */
-			if (i < (inlen - 3) || in[inlen - 2] != '=')
-				out[j] = ((unsigned int)c & 0xF) << 4; 
-			continue;
-		case 2:
-			out[j++] += ((unsigned int)c >> 2) & 0xF;
-
-			/* if not last char with padding */
-			if (i < (inlen - 2) || in[inlen - 1] != '=')
-				out[j] =  ((unsigned int)c & 0x3) << 6;
-			continue;
-		case 3:
-			out[j++] += (unsigned char)c;
-		}
-	}
-
-	return BASE64_OK;
+int base64_decode(const char *in, unsigned int inlen, unsigned char *out) {
+    unsigned int i, j;
+
+    for (i = j = 0; i < inlen; i++) {
+        int c;
+        int s = i % 4;
+
+        if (in[i] == '=')
+            return BASE64_OK;
+
+        if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST ||
+            (c = base64de[in[i] - BASE64DE_FIRST]) == -1)
+            return BASE64_INVALID;
+
+        switch (s) {
+        case 0:
+            out[j] = ((unsigned int)c << 2) & 0xFF;
+            continue;
+        case 1:
+            out[j++] += ((unsigned int)c >> 4) & 0x3;
+
+            /* if not last char with padding */
+            if (i < (inlen - 3) || in[inlen - 2] != '=')
+                out[j] = ((unsigned int)c & 0xF) << 4;
+            continue;
+        case 2:
+            out[j++] += ((unsigned int)c >> 2) & 0xF;
+
+            /* if not last char with padding */
+            if (i < (inlen - 2) || in[inlen - 1] != '=')
+                out[j] =  ((unsigned int)c & 0x3) << 6;
+            continue;
+        case 3:
+            out[j++] += (unsigned char)c;
+        }
+    }
+
+    return BASE64_OK;
 }
 

+ 9 - 0
utils/md5.c

@@ -45,6 +45,15 @@ documentation and/or software.
 #define S43 15
 #define S44 21
 
+/* POINTER defines a generic pointer type */
+typedef unsigned char *POINTER;
+
+/* UINT2 defines a two byte word */
+typedef unsigned short int UINT2;
+
+/* UINT4 defines a four byte word */
+typedef unsigned long int UINT4;
+
 static void MD5Transform(UINT4 [4], unsigned char [64]);
 static void Encode(unsigned char *, UINT4 *, unsigned int);
 static void Decode(UINT4 *, unsigned char *, unsigned int);

+ 3 - 12
utils/md5.h

@@ -27,20 +27,11 @@ documentation and/or software.
 
 #include "hexport.h"
 
-/* POINTER defines a generic pointer type */
-typedef unsigned char *POINTER;
-
-/* UINT2 defines a two byte word */
-typedef unsigned short int UINT2;
-
-/* UINT4 defines a four byte word */
-typedef unsigned long int UINT4;
-
 /* MD5 context. */
 typedef struct {
-  UINT4 state[4];                                   /* state (ABCD) */
-  UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */
-  unsigned char buffer[64];                         /* input buffer */
+  unsigned long int state[4];   /* state (ABCD) */
+  unsigned long int count[2];   /* number of bits, modulo 2^64 (lsb first) */
+  unsigned char     buffer[64]; /* input buffer */
 } MD5_CTX;
 
 BEGIN_EXTERN_C