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.
24 #define GIT_HASH_SIZE 20
29 unsigned char m_hash
[GIT_HASH_SIZE
];
33 memset(m_hash
,0, GIT_HASH_SIZE
);
37 memcpy(m_hash
,p
,GIT_HASH_SIZE
);
39 CGitHash
& operator = (CString
&str
)
45 CGitHash
& operator = (const unsigned char *p
)
47 memcpy(m_hash
, p
, GIT_HASH_SIZE
);
50 CGitHash(CString
&str
)
52 if (!IsValidSHA1(str
))
57 memset(m_hash
, 0, GIT_HASH_SIZE
);
61 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
65 for (int j
= 2 * i
; j
<= 2 * i
+ 1; ++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;
82 void ConvertFromStrA(const char *str
)
84 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
88 for (int j
= 2 * i
; j
<= 2 * i
+ 1; ++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;
106 memset(m_hash
,0, GIT_HASH_SIZE
);
110 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
118 CString
ToString() const
122 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
124 a
.Format(_T("%02x"),m_hash
[i
]);
134 bool operator == (const CGitHash
&hash
)
136 return memcmp(m_hash
,hash
.m_hash
,GIT_HASH_SIZE
) == 0;
140 friend bool operator<(const CGitHash
& left
, const CGitHash
& right
)
142 return memcmp(left
.m_hash
,right
.m_hash
,GIT_HASH_SIZE
) < 0;
145 friend bool operator>(const CGitHash
& left
, const CGitHash
& right
)
147 return memcmp(left
.m_hash
, right
.m_hash
, GIT_HASH_SIZE
) > 0;
150 friend bool operator != (const CGitHash
& left
, const CGitHash
& right
)
152 return memcmp(left
.m_hash
, right
.m_hash
, GIT_HASH_SIZE
) != 0;
154 #if defined(_MFC_VER)
155 friend CArchive
& AFXAPI
operator<<(CArchive
& ar
, CGitHash
& hash
)
157 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
161 friend CArchive
& AFXAPI
operator>>(CArchive
& ar
, CGitHash
& hash
)
163 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
168 static bool IsValidSHA1(const CString
&possibleSHA1
)
170 if (possibleSHA1
.GetLength() != 2 * GIT_HASH_SIZE
)
172 for (int i
= 0; i
< possibleSHA1
.GetLength(); ++i
)
174 if (!((possibleSHA1
[i
] >= '0' && possibleSHA1
[i
] <= '9') || (possibleSHA1
[i
] >= 'a' && possibleSHA1
[i
] <= 'f') || (possibleSHA1
[i
] >= 'A' && possibleSHA1
[i
] <= 'F')))