summaryrefslogtreecommitdiff
path: root/rfile.c
blob: ae75bf743e788e20fe03f56c8ce8877380381485 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>

#include "prism.h"
#include "md5.h"
#include "rfile.h"
#include "compress.h"

static const char hex_str[]= "0123456789abcdef";

int get_file_hash(const char *filename, char *hash)
{
FILE *fp;
MD5_CTX hb;
char buf[16];
int i;

    if(filename == NULL || hash == NULL)
        return PRET_ERROR;
        
    fp = fopen(filename, "rb");
    if(fp == NULL)
        return PRET_READERROR;
    
    MD5_Init(&hb);
    while((i = fread(buf, sizeof(char), 15, fp)) > 0) {
        MD5_Update(&hb, buf, i);
    }
    fclose(fp);
    
    MD5_Final(buf, &hb);
    
    for(i=0; i<16; i++) {
        hash[2*i] = hex_str[buf[i] >> 4];
        hash[2*i+1] = hex_str[buf[i] & 0x0F];
    }
    
    return PRET_OK;
}    

int compress_file(const char *filename, int id, int revision)
{
char *newfile;

    /* Arbitrary size... */
    newfile = (char *)malloc(128*sizeof(char));
    if(newfile == NULL)
        return PRET_ERROR;
    snprintf(newfile, 128, "%s%s%x.%x", PDB_DIR, DIR_SEP, id, revision);
    
    if(file_compress(filename, newfile) >= 0)
        return PRET_OK;
    
    return PRET_GZFAILED;
}

int decompress_file(const char *destination, int id, int revision)
{
char *stored_name;

    /* Arbitrary size... */
    stored_name = (char *)malloc(128*sizeof(char));
    if(stored_name == NULL)
        return PRET_ERROR;
    snprintf(stored_name, 128, "%s%s%x.%x", PDB_DIR, DIR_SEP, id, revision);
    
    unlink(destination);
    
    if(file_uncompress(stored_name, destination) >= 0)
        return PRET_OK;
    
    return PRET_GZFAILED;
}