File: checksum.cpp

package info (click to toggle)
fireflier 1.1.6-3etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 3,348 kB
  • ctags: 1,167
  • sloc: sh: 9,023; cpp: 8,370; makefile: 437; ansic: 300
file content (79 lines) | stat: -rw-r--r-- 1,500 bytes parent folder | download | duplicates (3)
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
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sys/stat.h>
#include <vector>
#include <fcntl.h>
#include <openssl/ssl.h>


using namespace std;

// initializes the checksum system
int init_checksum(int pid)
{
	ostringstream fst;
    struct stat st;
	fst << "/proc/";
	fst << pid;
	fst << "/maps";
	if (stat(fst.str().c_str(), &st)==-1)
		return 0;
    return 1;
}

int checksum_file(string filename, MD5_CTX *md5)
{
	int handle;
    char buffer[1024];
	if ((handle=open(filename.c_str(), O_RDONLY, 0))==-1)
		return 0;

    int len;
	while (len=read(handle, buffer, 1024)>0)
	{
        MD5_Update(md5, buffer, len);
	}
    close(handle);
}

// computes a checksum based on /proc/pid/maps
// result needs to be allocated already. size 16 bytes
char *checksum(int pid, char *result)
{
	ostringstream fst;
    struct stat st;
	fst << "/proc/";
	fst << pid;
	fst << "/maps";

	ifstream input(fst.str().c_str());
	string line;
    vector<string> files;
	while (getline(input, line))
	{
		if (line.length()>50)
            files.push_back(line.substr(49));
	}
	sort(files.begin(), files.end());

	MD5_CTX md5;
    MD5_Init(&md5);
	for (vector<string>::iterator it=files.begin(); it!=files.end();it++)
	{
        checksum_file(*it, &md5);
        cout << *it << endl;
	}
	MD5_Final((unsigned char *)result, &md5);
    return result;
}

int main()
{
	char result[30];
    memset(result, 0, 30);
	checksum(424, result);
    cout << result << endl;
}