Constify and staticify methods
[TortoiseGit.git] / src / Git / GitHash.h
blobe81b2a30958cf0caccdc12b7acacfb11448d5b5f
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #if defined(_MFC_VER)
22 #include "afx.h"
23 #endif
24 #define GIT_HASH_SIZE 20
26 class CGitHash
28 public:
29 unsigned char m_hash[GIT_HASH_SIZE];
31 CGitHash()
33 memset(m_hash,0, GIT_HASH_SIZE);
35 CGitHash(const char *p)
37 memcpy(m_hash,p,GIT_HASH_SIZE);
39 CGitHash & operator = (const CString &str)
41 CGitHash hash(str);
42 *this = hash;
43 return *this;
45 CGitHash & operator = (const unsigned char *p)
47 memcpy(m_hash, p, GIT_HASH_SIZE);
48 return *this;
50 CGitHash(const CString &str)
52 if (!IsValidSHA1(str))
54 #ifdef ASSERT
55 ASSERT(FALSE);
56 #endif
57 memset(m_hash, 0, GIT_HASH_SIZE);
58 return;
61 for (int i = 0; i < GIT_HASH_SIZE; ++i)
63 unsigned char a;
64 a=0;
65 for (int j = 2 * i; j <= 2 * i + 1; ++j)
67 a =a<<4;
69 TCHAR ch = str[j];
70 if(ch >= _T('0') && ch <= _T('9'))
71 a |= (ch - _T('0'))&0xF;
72 else if(ch >=_T('A') && ch <= _T('F'))
73 a |= ((ch - _T('A'))&0xF) + 10 ;
74 else if(ch >=_T('a') && ch <= _T('f'))
75 a |= ((ch - _T('a'))&0xF) + 10;
78 m_hash[i]=a;
82 void ConvertFromStrA(const char *str)
84 for (int i = 0; i < GIT_HASH_SIZE; ++i)
86 unsigned char a;
87 a=0;
88 for (int j = 2 * i; j <= 2 * i + 1; ++j)
90 a =a<<4;
92 char ch = str[j];
93 if(ch >= '0' && ch <= '9')
94 a |= (ch - ('0'))&0xF;
95 else if(ch >=('A') && ch <= ('F'))
96 a |= ((ch - ('A'))&0xF) + 10 ;
97 else if(ch >=_T('a') && ch <= ('f'))
98 a |= ((ch - ('a'))&0xF) + 10;
101 m_hash[i]=a;
104 void Empty()
106 memset(m_hash,0, GIT_HASH_SIZE);
108 bool IsEmpty() const
110 for (int i = 0; i < GIT_HASH_SIZE; ++i)
112 if(m_hash[i] != 0)
113 return false;
115 return true;
118 CString ToString() const
120 CString str;
121 CString a;
122 for (int i = 0; i < GIT_HASH_SIZE; ++i)
124 a.Format(_T("%02x"),m_hash[i]);
125 str+=a;
127 return str;
129 operator CString () const
131 return ToString();
134 bool operator == (const CGitHash &hash) const
136 return memcmp(m_hash,hash.m_hash,GIT_HASH_SIZE) == 0;
139 static friend bool operator<(const CGitHash& left, const CGitHash& right)
141 return memcmp(left.m_hash,right.m_hash,GIT_HASH_SIZE) < 0;
144 static friend bool operator>(const CGitHash& left, const CGitHash& right)
146 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) > 0;
149 static friend bool operator != (const CGitHash& left, const CGitHash& right)
151 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) != 0;
153 #if defined(_MFC_VER)
154 friend CArchive& AFXAPI operator<<(CArchive& ar, CGitHash& hash)
156 for (int i = 0; i < GIT_HASH_SIZE; ++i)
157 ar<<hash.m_hash[i];
158 return ar;
160 friend CArchive& AFXAPI operator>>(CArchive& ar, CGitHash& hash)
162 for (int i = 0; i < GIT_HASH_SIZE; ++i)
163 ar>>hash.m_hash[i];
164 return ar;
166 #endif
167 static bool IsValidSHA1(const CString &possibleSHA1)
169 if (possibleSHA1.GetLength() != 2 * GIT_HASH_SIZE)
170 return false;
171 for (int i = 0; i < possibleSHA1.GetLength(); ++i)
173 if (!((possibleSHA1[i] >= '0' && possibleSHA1[i] <= '9') || (possibleSHA1[i] >= 'a' && possibleSHA1[i] <= 'f') || (possibleSHA1[i] >= 'A' && possibleSHA1[i] <= 'F')))
174 return false;
176 return true;