targets/Win32/plc_Win32_main_retain.c
author Edouard Tisserant
Thu, 08 Nov 2018 11:14:06 +0100
changeset 2330 8c18b1a3e2bf
parent 2227 f150783ee8e7
child 2499 68f4f2d4516b
permissions -rw-r--r--
URI Editor Dialog : moved it to /dialog, and fixed naming.
2178
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     1
/*
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     2
  This file is part of Beremiz, a Integrated Development Environment for
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     3
  programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     4
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     5
  See COPYING.runtime
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     6
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     7
  Copyright (C) 2018: Sergey Surkov <surkov.sv@summatechnology.ru>
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     8
  Copyright (C) 2018: Andrey Skvortsov <andrej.skvortzov@gmail.com>
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
     9
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
    10
*/
bd0d13d10b8e Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2174
diff changeset
    11
2174
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    12
#ifndef HAVE_RETAIN
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    13
#include <stdio.h>
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    14
#include <stdint.h>
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    15
#include <unistd.h>
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    16
#include "iec_types.h"
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    17
2227
f150783ee8e7 Fix some compilation warnings for better ANSI C compliance
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2178
diff changeset
    18
int GetRetainSize(void);
2174
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    19
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    20
/* Retain buffer.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    21
FILE *retain_buffer;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    22
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
    23
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
    24
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    25
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    26
/* Retain header struct.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    27
struct retain_info_t {
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    28
	uint32_t retain_size;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    29
	uint32_t hash_size;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    30
	uint8_t* hash;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    31
	uint32_t header_offset;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    32
	uint32_t header_crc;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    33
};
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
/* Init retain info structure.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    36
struct retain_info_t retain_info;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    37
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    38
/* CRC lookup table and initial state.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    39
uint32_t crc32_table[256];
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    40
uint32_t retain_crc;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    41
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    42
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    43
/* Generate CRC32 lookup table.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    44
void GenerateCRC32Table(void)
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
	unsigned int i, j;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    47
	/* 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
    48
	uint32_t poly = 0xEDB88320;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    49
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    50
	for (i = 0; i <= 0xFF; i++)
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 c = i;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    53
		for (j = 0 ; j < 8 ; j++)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    54
			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
    55
		crc32_table[i] = c;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    56
	}
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
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    60
/* 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
    61
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
    62
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    63
	uint32_t crc = ~init;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    64
	unsigned char* current = (unsigned char*) buf;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    65
	while (len--)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    66
		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
    67
	return ~crc;
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
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    70
/* 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
    71
int CheckFileCRC(FILE* file_buffer)
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
	/* 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
    74
	const uint32_t magic_number = 0x2144df1c;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    75
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    76
	/* CRC initial state.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    77
	uint32_t calc_crc32 = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    78
	char data_block = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    79
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    80
	while(!feof(file_buffer)){
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    81
		if (fread(&data_block, sizeof(data_block), 1, file_buffer))
2227
f150783ee8e7 Fix some compilation warnings for better ANSI C compliance
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2178
diff changeset
    82
			calc_crc32 = GenerateCRC32Sum(&data_block, sizeof(data_block), calc_crc32);
2174
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
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    85
	/* 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
    86
	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
    87
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    88
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    89
/* 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
    90
int CheckFilehash(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    91
{
2227
f150783ee8e7 Fix some compilation warnings for better ANSI C compliance
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2178
diff changeset
    92
	unsigned int k;
2174
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    93
	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
    94
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    95
	rewind(retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    96
	fseek(retain_buffer, offset , SEEK_SET);
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
	uint32_t size;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
    99
	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
   100
	if (size != retain_info.hash_size)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   101
		return 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   102
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   103
	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
   104
		uint8_t file_digit;
2227
f150783ee8e7 Fix some compilation warnings for better ANSI C compliance
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2178
diff changeset
   105
		fread(&file_digit, sizeof(file_digit), 1, retain_buffer);
2174
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   106
		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
   107
			return 0;
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
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   110
	return 1;
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
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   113
void InitRetain(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   114
{
2227
f150783ee8e7 Fix some compilation warnings for better ANSI C compliance
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2178
diff changeset
   115
	unsigned int i;
2174
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   116
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   117
	/* Generate CRC32 lookup table.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   118
	GenerateCRC32Table();
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   119
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   120
	/* Get retain size in bytes */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   121
	retain_info.retain_size = GetRetainSize();
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   122
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   123
	/* 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
   124
	   (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
   125
	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
   126
	//retain_info.hash_size = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   127
	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
   128
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   129
	/* Transform hash string into byte sequence.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   130
	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
   131
		int byte = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   132
		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
   133
		retain_info.hash[i] = byte;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   134
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   135
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   136
	/* Calc header offset.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   137
	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
   138
		sizeof(retain_info.hash_size) + \
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   139
		retain_info.hash_size;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   140
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   141
	/*  Set header CRC initial state.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   142
	retain_info.header_crc = 0;
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
	/* Calc crc for header.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   145
	retain_info.header_crc = GenerateCRC32Sum(
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   146
		&retain_info.retain_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   147
		sizeof(retain_info.retain_size),
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   148
		retain_info.header_crc);
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
	retain_info.header_crc = GenerateCRC32Sum(
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   151
		&retain_info.hash_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   152
		sizeof(retain_info.hash_size),
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   153
		retain_info.header_crc);
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
	retain_info.header_crc = GenerateCRC32Sum(
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   156
		retain_info.hash,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   157
		retain_info.hash_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   158
		retain_info.header_crc);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   159
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   160
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   161
void CleanupRetain(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   162
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   163
	/* Free hash memory.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   164
	free(retain_info.hash);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   165
}
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
int CheckRetainFile(const char * file)
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
	retain_buffer = fopen(file, "rb");
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   170
	if (retain_buffer) {
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   171
		/* Check CRC32 and hash.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   172
		if (CheckFileCRC(retain_buffer))
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   173
			if (CheckFilehash())
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
		fclose(retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   176
		retain_buffer = NULL;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   177
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   178
	return 0;
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
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   181
int CheckRetainBuffer(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   182
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   183
	retain_buffer = NULL;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   184
	if (!retain_info.retain_size)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   185
		return 1;
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
	/* Check latest retain file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   188
	if (CheckRetainFile(rb_file))
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   189
		return 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   190
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   191
	/* Check if we have backup.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   192
	if (CheckRetainFile(rb_file_bckp))
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   193
		return 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   194
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   195
	/* 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
   196
	return 0;
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
#ifndef FILE_RETAIN_SAVE_PERIOD_S
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   200
#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
   201
#endif
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
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
   204
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   205
	IEC_TIME dt ={
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   206
		t1->tv_sec  - t2->tv_sec,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   207
		t1->tv_nsec - t2->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
	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
   211
		dt.tv_sec--;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   212
		dt.tv_nsec += 1000000000;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   213
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   214
	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
   215
		dt.tv_sec++;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   216
		dt.tv_nsec -= 1000000000;
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
	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
   219
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   220
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
int RetainSaveNeeded(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   223
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   224
	int ret = 0;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   225
	static IEC_TIME last_save;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   226
	IEC_TIME now;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   227
	double diff_s;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   228
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   229
	/* no retain */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   230
	if (!retain_info.retain_size)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   231
		return 0;
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
	/* 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
   234
	PLC_GetTime(&now);
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
	diff_s = CalcDiffSeconds(&now, &last_save);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   237
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   238
	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
   239
		ret = 1;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   240
		last_save = now;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   241
	}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   242
	return ret;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   243
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   244
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   245
void ValidateRetainBuffer(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   246
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   247
	if (!retain_buffer)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   248
		return;
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
	/* 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
   251
	fseek(retain_buffer, 0, SEEK_END);
2227
f150783ee8e7 Fix some compilation warnings for better ANSI C compliance
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2178
diff changeset
   252
	fwrite(&retain_crc, sizeof(retain_crc), 1, retain_buffer);
2174
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
	/* Sync file buffer and close file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   255
#ifdef __WIN32
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   256
	fflush(retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   257
#else
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   258
	fsync(fileno(retain_buffer));
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   259
#endif
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   260
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   261
	fclose(retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   262
	retain_buffer = NULL;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   263
}
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
void InValidateRetainBuffer(void)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   266
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   267
	if (!RetainSaveNeeded())
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   268
		return;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   269
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   270
	/* 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
   271
	rename(rb_file, rb_file_bckp);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   272
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   273
	/* Set file CRC initial value.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   274
	retain_crc = retain_info.header_crc;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   275
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   276
	/* Create new retain file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   277
	retain_buffer = fopen(rb_file, "wb+");
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   278
	if (!retain_buffer) {
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   279
		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
   280
		return;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   281
	}
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
	/* Write header to the new file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   284
	fwrite(&retain_info.retain_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   285
		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
   286
	fwrite(&retain_info.hash_size,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   287
		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
   288
	fwrite(retain_info.hash ,
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   289
		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
   290
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   291
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   292
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
   293
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   294
	if (!retain_buffer)
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   295
		return;
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   296
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   297
	/* 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
   298
	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
   299
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   300
	/* Save current var in file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   301
	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
   302
	fwrite(p, count, 1, retain_buffer);
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   303
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   304
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   305
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
   306
{
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   307
	/* Remind variable from file.  */
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   308
	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
   309
	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
   310
}
55611282b909 Use the same retain implementation for Win32 targets
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff changeset
   311
#endif // !HAVE_RETAIN