crc iz celotne serijske
This commit is contained in:
18
uuid-gen.c
18
uuid-gen.c
@@ -8,12 +8,18 @@
|
||||
// Program se prevede z
|
||||
// gcc uuid-gen.c -lz
|
||||
|
||||
union serial_union // za razbijanje serijske na posamezne bajte
|
||||
{
|
||||
uint32_t serial; // serijska
|
||||
uint8_t bytes[4]; // za CRC32, ki pričakuje posamezne bajte
|
||||
};
|
||||
|
||||
|
||||
// struktura, ki skupaj poveze serijsko, nakljucni uuid in CRC32
|
||||
struct uuid_uint128
|
||||
{
|
||||
uint8_t uuid_byte[16];
|
||||
char serial;
|
||||
union serial_union serial;
|
||||
uint32_t crc;
|
||||
};
|
||||
|
||||
@@ -25,13 +31,13 @@ void get_uuid(struct uuid_uint128 *uuid)
|
||||
// N = varianta (0b10xx)
|
||||
// byte maske za te vrednosti
|
||||
|
||||
static char serial = 1;
|
||||
static uint32_t serial = 1;
|
||||
const uint8_t version_mask_or = 0x40;
|
||||
const uint8_t version_mask_and = 0x0F;
|
||||
const uint8_t variant_mask_or = 0x80;
|
||||
const uint8_t variant_mask_and = 0x3F;
|
||||
|
||||
uuid->serial = serial++; // nastavimo naslednjo serijsko
|
||||
uuid->serial.serial = serial++; // nastavimo naslednjo serijsko
|
||||
|
||||
|
||||
for (int c = 0; c < sizeof(uuid->uuid_byte); c++)
|
||||
@@ -49,7 +55,7 @@ void get_uuid(struct uuid_uint128 *uuid)
|
||||
// Zlib ima implementacijo CRC-32
|
||||
// Glej https://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/zlib-crc32-1.html
|
||||
uuid->crc = crc32(0, Z_NULL, 0); // inicializacija
|
||||
uuid->crc = crc32(uuid->crc, &uuid->serial, 1); // dodamo serijsko
|
||||
uuid->crc = crc32(uuid->crc, uuid->serial.bytes, sizeof(uuid->serial.bytes)); // dodamo serijsko
|
||||
uuid->crc = crc32(uuid->crc, uuid->uuid_byte, sizeof(uuid->uuid_byte)); // dodamo vse byte UUIDja
|
||||
};
|
||||
|
||||
@@ -57,7 +63,7 @@ void print_uuid(struct uuid_uint128 uuid)
|
||||
{
|
||||
// funkcija je samo alias za ta dolgovezni printf
|
||||
printf("%08d-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x %08x\n",
|
||||
uuid.serial,
|
||||
uuid.serial.serial,
|
||||
uuid.uuid_byte[0],
|
||||
uuid.uuid_byte[1],
|
||||
uuid.uuid_byte[2],
|
||||
@@ -82,7 +88,7 @@ int main(void)
|
||||
{
|
||||
srand(time(NULL)); // seed RNG
|
||||
struct uuid_uint128 uuid;
|
||||
for (int i = 0; i < 100; i++) // zaradi racunanja serijske in CRC je maksimum 255 iteracij
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
get_uuid(&uuid);
|
||||
print_uuid(uuid);
|
||||
|
||||
Reference in New Issue
Block a user