File: INK_MD5.h

package info (click to toggle)
trafficserver 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,428 kB
  • sloc: cpp: 222,273; sh: 12,193; ansic: 8,967; makefile: 1,809; perl: 1,158; java: 277; lex: 124; yacc: 63; sed: 6
file content (164 lines) | stat: -rw-r--r-- 3,816 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#ifndef _INK_MD5_h_
#define	_INK_MD5_h_
#include "MMH.h"

#include "ink_bool.h"
#include "ink_code.h"
#include "ink_port.h"

// #define USE_MMH_FOR_MD5

struct INK_MD5
{
  uint64_t b[2];
  const INK_MD5 & operator =(const INK_MD5 & md5)
  {
    b[0] = md5.b[0];
    b[1] = md5.b[1];
    return md5;
  }
  uint32_t word(int i)
  {
    uint32_t *p = (uint32_t *) & b[0];
    return p[i];
  }
  unsigned char byte(int i)
  {
    unsigned char *p = (unsigned char *) &b[0];
    return p[i];
  }
  INK_MD5 & loadFromBuffer(char *md5_buf) {
    int i;
    char *s, *d;

    for (i = 0, s = md5_buf, d = (char *) (&(b[0])); i < 8; i++, *d++ = *s++);
    for (i = 0, d = (char *) (&(b[1])); i < 8; i++, *d++ = *s++);
    return (*this);
  }
  INK_MD5 & storeToBuffer(char *md5_buf) {
    int i;
    char *s, *d;

    for (i = 0, d = md5_buf, s = (char *) (&(b[0])); i < 8; i++, *d++ = *s++);
    for (i = 0, s = (char *) (&(b[1])); i < 8; i++, *d++ = *s++);
    return (*this);
  }
  INK_MD5 & operator =(char *md5) {
    return (loadFromBuffer(md5));
  }
  INK_MD5 & operator =(unsigned char *md5) {
    return (loadFromBuffer((char *) md5));
  }

  char *toStr(char *md5_str)
  {
    int i;
    char *s, *d;

    for (i = 0, d = md5_str, s = (char *) (&(b[0])); i < 8; i++, *d++ = *s++);
    for (i = 0, s = (char *) (&(b[1])); i < 8; i++, *d++ = *s++);
    return (md5_str);
  }
  void encodeBuffer(unsigned char *buffer, int len)
  {
    unsigned char md5[16];
    ink_code_md5(buffer, len, md5);
    *this = md5;
  }
  void encodeBuffer(const char *buffer, int len)
  {
    encodeBuffer((unsigned char *) buffer, len);
  }
  char *str()
  {
    return ((char *) b);
  }
  char *toHexStr(char hex_md5[33])
  {
    return (ink_code_md5_stringify_fast(hex_md5, str()));
  }
  void set(INK_MD5 & md5)
  {
    loadFromBuffer((char *) &md5);
  }
  void set(INK_MD5 * md5)
  {
    loadFromBuffer((char *) md5);
  }
  void set(char *p)
  {
    loadFromBuffer(p);
  }
  void set(uint64_t a1, uint64_t a2)
  {
    b[0] = a1;
    b[1] = a2;
  }
  char *string(char buf[33])
  {
    char hex_digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    int i, j;
    unsigned char *u = (unsigned char *) &b[0];

    for (i = 0, j = 0; i < 16; i += 1, j += 2) {
      buf[j + 0] = hex_digits[u[i] >> 4];
      buf[j + 1] = hex_digits[u[i] & 0xF];
    }
    buf[32] = '\0';
    return buf;
  }

  uint64_t fold()
  {
    return (b[0] ^ b[1]);
  }

  uint64_t operator[] (int i)
  {
    return b[i];
  }
  bool operator==(INK_MD5 & md5)
  {
    return b[0] == md5.b[0] && b[1] == md5.b[1];
  }
  INK_MD5() {
    b[0] = 0;
    b[1] = 0;
  }
  INK_MD5(uint64_t a1, uint64_t a2) {
    b[0] = a1;
    b[1] = a2;
  }
};

#ifdef USE_MMH_FOR_MD5
#define INK_MD5 MMH
#define ink_code_incr_MD5_init ink_code_incr_MMH_init
#define ink_code_incr_MD5_update ink_code_incr_MMH_update
#define ink_code_incr_MD5_final ink_code_incr_MMH_final
#endif

#endif