targets/Win32/plc_Win32_main_retain.c
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 07 Jun 2018 14:04:45 +0300
changeset 2174 55611282b909
child 2178 bd0d13d10b8e
permissions -rw-r--r--
Use the same retain implementation for Win32 targets

Currently plc_Win32_main_retain.c is exact copy of
plc_Linux_main_retain.c, because it's not possible to use symbol
links.

It'd be nice in the future to have possibility to modify targets.code.
2174
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     1
#ifndef HAVE_RETAIN
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     2
#include <stdio.h>
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     3
#include <stdint.h>
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     4
#include <unistd.h>
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     5
#include "iec_types.h"
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     6
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     7
int GetRetainSize();
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     8
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
     9
/* Retain buffer.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    10
FILE *retain_buffer;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    11
const char rb_file[]      = "retain_buffer_file";
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    12
const char rb_file_bckp[] = "retain_buffer_file.bak";
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    13
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    14
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    15
/* Retain header struct.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    16
struct retain_info_t {
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    17
	uint32_t retain_size;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    18
	uint32_t hash_size;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    19
	uint8_t* hash;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    20
	uint32_t header_offset;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    21
	uint32_t header_crc;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    22
};
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    23
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    24
/* Init retain info structure.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    25
struct retain_info_t retain_info;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    26
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    27
/* CRC lookup table and initial state.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    28
uint32_t crc32_table[256];
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    29
uint32_t retain_crc;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    30
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    31
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    32
/* Generate CRC32 lookup table.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    33
void GenerateCRC32Table(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    34
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    35
	unsigned int i, j;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    36
	/* Use CRC-32-IEEE 802.3 polynomial 0x04C11DB7 (bit reflected).  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    37
	uint32_t poly = 0xEDB88320;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    38
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    39
	for (i = 0; i <= 0xFF; i++)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    40
	{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    41
		uint32_t c = i;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    42
		for (j = 0 ; j < 8 ; j++)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    43
			c = (c & 1) ? (c >> 1 ) ^ poly : (c >> 1);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    44
		crc32_table[i] = c;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    45
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    46
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    47
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    48
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    49
/* Calculate CRC32 for len bytes from pointer buf with init starting value.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    50
uint32_t GenerateCRC32Sum(const void* buf, unsigned int len, uint32_t init)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    51
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    52
	uint32_t crc = ~init;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    53
	unsigned char* current = (unsigned char*) buf;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    54
	while (len--)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    55
		crc = crc32_table[(crc ^ *current++) & 0xFF] ^ (crc >> 8);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    56
	return ~crc;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    57
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    58
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    59
/* Calc CRC32 for retain file byte by byte.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    60
int CheckFileCRC(FILE* file_buffer)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    61
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    62
	/* Set the magic constant for one-pass CRC calc according to ZIP CRC32.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    63
	const uint32_t magic_number = 0x2144df1c;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    64
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    65
	/* CRC initial state.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    66
	uint32_t calc_crc32 = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    67
	char data_block = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    68
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    69
	while(!feof(file_buffer)){
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    70
		if (fread(&data_block, sizeof(data_block), 1, file_buffer))
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    71
			calc_crc32 = GenerateCRC32Sum(&data_block, sizeof(char), calc_crc32);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    72
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    73
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    74
	/* Compare crc result with a magic number.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    75
	return (calc_crc32 == magic_number) ? 1 : 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    76
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    77
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    78
/* Compare current hash with hash from file byte by byte.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    79
int CheckFilehash(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    80
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    81
	int k;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    82
	int offset = sizeof(retain_info.retain_size);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    83
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    84
	rewind(retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    85
	fseek(retain_buffer, offset , SEEK_SET);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    86
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    87
	uint32_t size;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    88
	fread(&size, sizeof(size), 1, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    89
	if (size != retain_info.hash_size)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    90
		return 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    91
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    92
	for(k = 0; k < retain_info.hash_size; k++){
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    93
		uint8_t file_digit;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    94
		fread(&file_digit, sizeof(char), 1, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    95
		if (file_digit != *(retain_info.hash+k))
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    96
			return 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    97
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    98
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    99
	return 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   100
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   101
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   102
void InitRetain(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   103
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   104
	int i;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   105
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   106
	/* Generate CRC32 lookup table.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   107
	GenerateCRC32Table();
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   108
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   109
	/* Get retain size in bytes */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   110
	retain_info.retain_size = GetRetainSize();
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   111
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   112
	/* Hash stored in retain file as array of char in hex digits
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   113
	   (that's why we divide strlen in two).  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   114
	retain_info.hash_size = PLC_ID ? strlen(PLC_ID)/2 : 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   115
	//retain_info.hash_size = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   116
	retain_info.hash = malloc(retain_info.hash_size);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   117
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   118
	/* Transform hash string into byte sequence.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   119
	for (i = 0; i < retain_info.hash_size; i++) {
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   120
		int byte = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   121
		sscanf((PLC_ID + i*2), "%02X", &byte);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   122
		retain_info.hash[i] = byte;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   123
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   124
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   125
	/* Calc header offset.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   126
	retain_info.header_offset = sizeof(retain_info.retain_size) + \
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   127
		sizeof(retain_info.hash_size) + \
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   128
		retain_info.hash_size;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   129
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   130
	/*  Set header CRC initial state.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   131
	retain_info.header_crc = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   132
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   133
	/* Calc crc for header.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   134
	retain_info.header_crc = GenerateCRC32Sum(
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   135
		&retain_info.retain_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   136
		sizeof(retain_info.retain_size),
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   137
		retain_info.header_crc);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   138
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   139
	retain_info.header_crc = GenerateCRC32Sum(
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   140
		&retain_info.hash_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   141
		sizeof(retain_info.hash_size),
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   142
		retain_info.header_crc);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   143
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   144
	retain_info.header_crc = GenerateCRC32Sum(
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   145
		retain_info.hash,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   146
		retain_info.hash_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   147
		retain_info.header_crc);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   148
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   149
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   150
void CleanupRetain(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   151
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   152
	/* Free hash memory.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   153
	free(retain_info.hash);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   154
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   155
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   156
int CheckRetainFile(const char * file)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   157
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   158
	retain_buffer = fopen(file, "rb");
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   159
	if (retain_buffer) {
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   160
		/* Check CRC32 and hash.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   161
		if (CheckFileCRC(retain_buffer))
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   162
			if (CheckFilehash())
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   163
				return 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   164
		fclose(retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   165
		retain_buffer = NULL;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   166
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   167
	return 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   168
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   169
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   170
int CheckRetainBuffer(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   171
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   172
	retain_buffer = NULL;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   173
	if (!retain_info.retain_size)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   174
		return 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   175
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   176
	/* Check latest retain file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   177
	if (CheckRetainFile(rb_file))
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   178
		return 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   179
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   180
	/* Check if we have backup.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   181
	if (CheckRetainFile(rb_file_bckp))
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   182
		return 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   183
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   184
	/* We don't have any valid retain buffer - nothing to remind.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   185
	return 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   186
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   187
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   188
#ifndef FILE_RETAIN_SAVE_PERIOD_S
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   189
#define FILE_RETAIN_SAVE_PERIOD_S 1.0
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   190
#endif
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   191
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   192
static double CalcDiffSeconds(IEC_TIME* t1, IEC_TIME *t2)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   193
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   194
	IEC_TIME dt ={
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   195
		t1->tv_sec  - t2->tv_sec,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   196
		t1->tv_nsec - t2->tv_nsec
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   197
	};
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   198
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   199
	if ((dt.tv_nsec < -1000000000) || ((dt.tv_sec > 0) && (dt.tv_nsec < 0))){
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   200
		dt.tv_sec--;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   201
		dt.tv_nsec += 1000000000;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   202
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   203
	if ((dt.tv_nsec > +1000000000) || ((dt.tv_sec < 0) && (dt.tv_nsec > 0))){
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   204
		dt.tv_sec++;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   205
		dt.tv_nsec -= 1000000000;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   206
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   207
	return dt.tv_sec + 1e-9*dt.tv_nsec;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   208
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   209
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   210
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   211
int RetainSaveNeeded(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   212
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   213
	int ret = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   214
	static IEC_TIME last_save;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   215
	IEC_TIME now;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   216
	double diff_s;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   217
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   218
	/* no retain */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   219
	if (!retain_info.retain_size)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   220
		return 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   221
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   222
	/* periodic retain flush to avoid high I/O load */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   223
	PLC_GetTime(&now);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   224
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   225
	diff_s = CalcDiffSeconds(&now, &last_save);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   226
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   227
	if ((diff_s > FILE_RETAIN_SAVE_PERIOD_S) || ForceSaveRetainReq()) {
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   228
		ret = 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   229
		last_save = now;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   230
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   231
	return ret;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   232
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   233
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   234
void ValidateRetainBuffer(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   235
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   236
	if (!retain_buffer)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   237
		return;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   238
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   239
	/* Add retain data CRC to the end of buffer file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   240
	fseek(retain_buffer, 0, SEEK_END);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   241
	fwrite(&retain_crc, sizeof(uint32_t), 1, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   242
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   243
	/* Sync file buffer and close file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   244
#ifdef __WIN32
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   245
	fflush(retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   246
#else
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   247
	fsync(fileno(retain_buffer));
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   248
#endif
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   249
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   250
	fclose(retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   251
	retain_buffer = NULL;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   252
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   253
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   254
void InValidateRetainBuffer(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   255
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   256
	if (!RetainSaveNeeded())
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   257
		return;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   258
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   259
	/* Rename old retain file into *.bak if it exists.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   260
	rename(rb_file, rb_file_bckp);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   261
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   262
	/* Set file CRC initial value.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   263
	retain_crc = retain_info.header_crc;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   264
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   265
	/* Create new retain file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   266
	retain_buffer = fopen(rb_file, "wb+");
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   267
	if (!retain_buffer) {
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   268
		fprintf(stderr, "Failed to create retain file : %s\n", rb_file);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   269
		return;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   270
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   271
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   272
	/* Write header to the new file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   273
	fwrite(&retain_info.retain_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   274
		sizeof(retain_info.retain_size), 1, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   275
	fwrite(&retain_info.hash_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   276
		sizeof(retain_info.hash_size),   1, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   277
	fwrite(retain_info.hash ,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   278
		sizeof(char), retain_info.hash_size, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   279
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   280
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   281
void Retain(unsigned int offset, unsigned int count, void *p)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   282
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   283
	if (!retain_buffer)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   284
		return;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   285
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   286
	/* Generate CRC 32 for each data block.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   287
	retain_crc = GenerateCRC32Sum(p, count, retain_crc);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   288
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   289
	/* Save current var in file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   290
	fseek(retain_buffer, retain_info.header_offset+offset, SEEK_SET);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   291
	fwrite(p, count, 1, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   292
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   293
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   294
void Remind(unsigned int offset, unsigned int count, void *p)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   295
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   296
	/* Remind variable from file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   297
	fseek(retain_buffer, retain_info.header_offset+offset, SEEK_SET);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   298
	fread((void *)p, count, 1, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   299
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   300
#endif // !HAVE_RETAIN