Add checkbox to select all tags in Delete Remote Tag Dialog
[TortoiseGit.git] / src / Git / GitHash.h
blob37f5c81e8e9e316e59f30d51cdc6911ce2276f0b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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(char *p)
37 memcpy(m_hash,p,GIT_HASH_SIZE);
39 CGitHash & operator = (CString &str)
41 CGitHash hash(str);
42 *this = hash;
43 return *this;
45 CGitHash(CString &str)
47 if (!IsValidSHA1(str))
49 #ifdef ASSERT
50 ASSERT(FALSE);
51 #endif
52 memset(m_hash, 0, GIT_HASH_SIZE);
53 return;
56 for(int i=0;i<GIT_HASH_SIZE;i++)
58 unsigned char a;
59 a=0;
60 for(int j=2*i;j<=2*i+1;j++)
62 a =a<<4;
64 TCHAR ch = str[j];
65 if(ch >= _T('0') && ch <= _T('9'))
66 a |= (ch - _T('0'))&0xF;
67 else if(ch >=_T('A') && ch <= _T('F'))
68 a |= ((ch - _T('A'))&0xF) + 10 ;
69 else if(ch >=_T('a') && ch <= _T('f'))
70 a |= ((ch - _T('a'))&0xF) + 10;
73 m_hash[i]=a;
77 void ConvertFromStrA(char *str)
79 for(int i=0;i<GIT_HASH_SIZE;i++)
81 unsigned char a;
82 a=0;
83 for(int j=2*i;j<=2*i+1;j++)
85 a =a<<4;
87 char ch = str[j];
88 if(ch >= '0' && ch <= '9')
89 a |= (ch - ('0'))&0xF;
90 else if(ch >=('A') && ch <= ('F'))
91 a |= ((ch - ('A'))&0xF) + 10 ;
92 else if(ch >=_T('a') && ch <= ('f'))
93 a |= ((ch - ('a'))&0xF) + 10;
96 m_hash[i]=a;
99 void Empty()
101 memset(m_hash,0, GIT_HASH_SIZE);
103 bool IsEmpty()
105 for(int i=0;i<GIT_HASH_SIZE;i++)
107 if(m_hash[i] != 0)
108 return false;
110 return true;
113 CString ToString()
115 CString str;
116 CString a;
117 for(int i=0;i<GIT_HASH_SIZE;i++)
119 a.Format(_T("%02x"),m_hash[i]);
120 str+=a;
122 return str;
124 operator CString ()
126 return ToString();
129 bool operator == (const CGitHash &hash)
131 return memcmp(m_hash,hash.m_hash,GIT_HASH_SIZE) == 0;
135 friend bool operator<(const CGitHash& left, const CGitHash& right)
137 return memcmp(left.m_hash,right.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;
149 #if defined(_MFC_VER)
150 friend CArchive& AFXAPI operator<<(CArchive& ar, CGitHash& hash)
152 for(int i=0;i<GIT_HASH_SIZE;i++)
153 ar<<hash.m_hash[i];
154 return ar;
156 friend CArchive& AFXAPI operator>>(CArchive& ar, CGitHash& hash)
158 for(int i=0;i<GIT_HASH_SIZE;i++)
159 ar>>hash.m_hash[i];
160 return ar;
162 #endif
163 static bool IsValidSHA1(const CString &possibleSHA1)
165 if (possibleSHA1.GetLength() != 2 * GIT_HASH_SIZE)
166 return false;
167 for (int i = 0; i < possibleSHA1.GetLength(); i++)
169 if (!((possibleSHA1[i] >= '0' && possibleSHA1[i] <= '9') || (possibleSHA1[i] >= 'a' && possibleSHA1[i] <= 'f') || (possibleSHA1[i] >= 'A' && possibleSHA1[i] <= 'F')))
170 return false;
172 return true;