1 | /*
|
---|
2 | * Copyright (c) 2011 Oleg Romanenko
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup filecheck
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file filecrc.c
|
---|
35 | * @brief Tool for generating file with random data
|
---|
36 | *
|
---|
37 | */
|
---|
38 |
|
---|
39 |
|
---|
40 | #include <unistd.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <sys/types.h>
|
---|
44 | #include <fcntl.h>
|
---|
45 | #include <errno.h>
|
---|
46 | #include <sys/time.h>
|
---|
47 | #include "crc32.h"
|
---|
48 |
|
---|
49 | #define NAME "filegen"
|
---|
50 | #define VERSION "0.0.1"
|
---|
51 |
|
---|
52 | #define BUFFERSIZE 256
|
---|
53 |
|
---|
54 |
|
---|
55 | static void print_help(void);
|
---|
56 |
|
---|
57 |
|
---|
58 | int main(int argc, char **argv) {
|
---|
59 | int rc;
|
---|
60 | uint64_t size = 0;
|
---|
61 |
|
---|
62 | if (argc < 3) {
|
---|
63 | print_help();
|
---|
64 | return 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | int fd = open(argv[1], O_WRONLY | O_CREAT);
|
---|
68 | if (fd < 0) {
|
---|
69 | printf("Unable to open %s for writing\n", argv[1]);
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | rc = str_uint64(argv[2], NULL, 10, true, &size);
|
---|
74 | if (rc != EOK) {
|
---|
75 | printf("Cannot convert size to number\n");
|
---|
76 | return 1;
|
---|
77 | }
|
---|
78 |
|
---|
79 | struct timeval tv;
|
---|
80 | gettimeofday(&tv, NULL);
|
---|
81 | srandom(tv.tv_sec + tv.tv_usec / 100000);
|
---|
82 |
|
---|
83 | uint64_t i=0, pbuf=0;
|
---|
84 | uint32_t crc=~0;
|
---|
85 | char buf[BUFFERSIZE];
|
---|
86 |
|
---|
87 | while (i<size) {
|
---|
88 | pbuf=0;
|
---|
89 | while (i<size && pbuf<BUFFERSIZE) {
|
---|
90 | buf[pbuf] = rand() % 255;
|
---|
91 | i++;
|
---|
92 | pbuf++;
|
---|
93 | }
|
---|
94 | if (pbuf) {
|
---|
95 | crc32(buf, pbuf, &crc);
|
---|
96 | write(fd, buf, pbuf);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | close(fd);
|
---|
101 | crc = ~crc;
|
---|
102 | printf("%s : %x\n", argv[1], crc);
|
---|
103 |
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /* Displays help for filegen */
|
---|
109 | static void print_help(void)
|
---|
110 | {
|
---|
111 | printf(
|
---|
112 | "Usage: %s <file> <size in bytes>\n",
|
---|
113 | NAME);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * @}
|
---|
119 | */
|
---|