1
/*
2
 * TLSH is provided for use under two licenses: Apache OR BSD.
3
 * Users may opt to use either license depending on the license
4
 * restictions of the systems with which they plan to integrate
5
 * the TLSH code.
6
 */ 
7

8
/* ==============
9
 * Apache License
10
 * ==============
11
 * Copyright 2013 Trend Micro Incorporated
12
 *
13
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 *     http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

26
/* ===========
27
 * BSD License
28
 * ===========
29
 * Copyright (c) 2013, Trend Micro Incorporated
30
 * All rights reserved.
31
 *
32
 * Redistribution and use in source and binary forms, with or without modification,
33
 * are permitted provided that the following conditions are met:
34
 *
35
 * 1. Redistributions of source code must retain the above copyright notice, this
36
 *    list of conditions and the following disclaimer.
37
 *
38
 * 2. Redistributions in binary form must reproduce the above copyright notice,
39
 *    this list of conditions and the following disclaimer in the documentation
40
 *    and/or other materials provided with the distribution.
41

42
 * 3. Neither the name of the copyright holder nor the names of its contributors
43
 *    may be used to endorse or promote products derived from this software without
44
 *    specific prior written permission.
45
 *
46
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
47
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
48
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
50
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
51
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
54
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56
 */
57

58
#include "tlsh.h"
59
#include "tlsh_impl.h"
60
#include "stdio.h"
61
#include "version.h"
62
#include <errno.h>
63
#include <string.h>
64

65
/////////////////////////////////////////////////////
66
// C++ Implementation
67

68 99
Tlsh::Tlsh():impl(NULL)
69
{
70 99
    impl = new TlshImpl();
71 63
}
72

73 0
Tlsh::Tlsh(const Tlsh& other):impl(NULL)
74
{
75 0
    impl = new TlshImpl();
76 0
    *impl = *other.impl;
77 0
}
78

79 99
Tlsh::~Tlsh()
80
{
81 99
    delete impl;
82 63
}
83

84 0
const char *Tlsh::version()
85
{
86
    static char versionBuf[256];
87 0
    if (versionBuf[0] == '\0')
88
        snprintf(versionBuf, sizeof(versionBuf), "%d.%d.%d %s %s sliding_window=%d",
89 0
		VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, TLSH_HASH, TLSH_CHECKSUM, SLIDING_WND_SIZE);
90 0
    return versionBuf;
91
}
92

93 99
void Tlsh::update(const unsigned char* data, unsigned int len)
94
{
95 99
    if ( NULL != impl )
96 99
        impl->update(data, len);
97 63
}
98

99 99
void Tlsh::final(const unsigned char* data, unsigned int len, int force_option)
100
{
101 99
    if ( NULL != impl ){
102 99
        if ( NULL != data && len > 0 )
103 0
            impl->update(data, len);
104 99
        impl->final(force_option);
105
    }
106 63
}
107

108 99
const char* Tlsh::getHash() const
109
{
110 99
    if ( NULL != impl )
111 99
        return impl->hash();
112
    else
113 0
        return "";
114
}
115

116 0
const char* Tlsh::getHash (char *buffer, unsigned int bufSize) const
117
{
118 0
    if ( NULL != impl )
119 0
        return impl->hash(buffer, bufSize);
120
    else {
121 0
        buffer[0] = '\0';
122 0
        return buffer;
123
    }
124
}
125

126 99
void Tlsh::reset()
127
{
128 99
    if ( NULL != impl )
129 99
        impl->reset();
130 63
}
131

132 0
Tlsh& Tlsh::operator=(const Tlsh& other)
133
{
134 0
    if (this == &other) 
135 0
        return *this;
136

137 0
    *impl = *other.impl;
138 0
    return *this;
139
}
140

141 0
bool Tlsh::operator==(const Tlsh& other) const
142
{
143 0
    if( this == &other )
144 0
        return true;
145 0
    else if( NULL == impl || NULL == other.impl )
146 0
        return false;
147
    else
148 0
        return ( 0 == impl->compare(*other.impl) );
149
}
150

151 0
bool Tlsh::operator!=(const Tlsh& other) const 
152
{
153 0
    return !(*this==other);
154
}
155

156 0
int Tlsh::Lvalue()
157
{
158 0
	return( impl->Lvalue() );
159
}
160 0
int Tlsh::Q1ratio()
161
{
162 0
	return( impl->Q1ratio() );
163
}
164 0
int Tlsh::Q2ratio()
165
{
166 0
	return( impl->Q2ratio() );
167
}
168

169 0
int Tlsh::totalDiff(const Tlsh *other, bool len_diff) const
170
{
171 0
    if( NULL==impl || NULL == other || NULL == other->impl )
172 0
        return -(EINVAL);
173 0
    else if ( this == other )
174 0
        return 0;
175
    else
176 0
        return (impl->totalDiff(*other->impl, len_diff));
177
}
178

179 0
int Tlsh::fromTlshStr(const char* str)
180
{
181 0
    if ( NULL == impl )
182 0
        return -(ENOMEM);
183 0
    else if ( NULL == str )
184 0
        return -(EINVAL);
185
    else
186 0
        return impl->fromTlshStr(str);
187
}
188

189 0
bool Tlsh::isValid() const
190
{
191 0
    return (impl ? impl->isValid() : false);
192
}

Read our documentation on viewing source code .

Loading