OpenWrt – Diff between revs 2 and 3
?pathlinks?
Rev 2 | Rev 3 | |||
---|---|---|---|---|
Line 18... | Line 18... | |||
18 | |
18 | |
|
19 | |
19 | |
|
20 | #include <stdio.h> |
20 | #include <stdio.h> |
|
21 | #include <stdlib.h> |
- | ||
22 | #include <string.h> |
21 | #include <stdlib.h> |
|
23 | #include <stdbool.h> |
22 | #include <string.h> |
|
24 | #include <stdint.h> |
23 | #include <stdint.h> |
|
Line 25... | Line 24... | |||
25 | #include <unistd.h> |
24 | #include <unistd.h> |
|
26 | #include <sys/stat.h> |
- | ||
Line 27... | Line 25... | |||
27 | |
25 | #include <sys/stat.h> |
|
28 | static char default_pattern[] = "12345678"; |
26 | |
|
29 | static int is_hex_pattern; |
27 | static char default_pattern[] = "12345678"; |
|
Line 43... | Line 41... | |||
43 | |
41 | |
|
Line 44... | Line 42... | |||
44 | void usage(void) __attribute__ (( __noreturn__ )); |
42 | void usage(void) __attribute__ (( __noreturn__ )); |
|
45 | |
43 | |
|
46 | void usage(void) |
44 | void usage(void) |
|
47 | { |
45 | { |
|
48 | fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>] [-x]\n"); |
46 | fprintf(stderr, "Usage: xorimage [-i infile] [-o outfile] [-p <pattern>]\n"); |
|
Line 49... | Line 47... | |||
49 | exit(EXIT_FAILURE); |
47 | exit(EXIT_FAILURE); |
|
Line 56... | Line 54... | |||
56 | FILE *in = stdin; |
54 | FILE *in = stdin; |
|
57 | FILE *out = stdout; |
55 | FILE *out = stdout; |
|
58 | char *ifn = NULL; |
56 | char *ifn = NULL; |
|
59 | char *ofn = NULL; |
57 | char *ofn = NULL; |
|
60 | const char *pattern = default_pattern; |
58 | const char *pattern = default_pattern; |
|
61 | char hex_pattern[128]; |
- | ||
62 | unsigned int hex_buf; |
- | ||
63 | int c; |
59 | int c; |
|
64 | int v0, v1, v2; |
60 | int v0, v1, v2; |
|
65 | size_t n; |
61 | size_t n; |
|
66 | int p_len, p_off = 0; |
62 | int p_len, p_off = 0; |
|
Line 67... | Line 63... | |||
67 | |
63 | |
|
68 | while ((c = getopt(argc, argv, "i:o:p:xh")) != -1) { |
64 | while ((c = getopt(argc, argv, "i:o:p:h")) != -1) { |
|
69 | switch (c) { |
65 | switch (c) { |
|
70 | case 'i': |
66 | case 'i': |
|
71 | ifn = optarg; |
67 | ifn = optarg; |
|
72 | break; |
68 | break; |
|
73 | case 'o': |
69 | case 'o': |
|
74 | ofn = optarg; |
70 | ofn = optarg; |
|
75 | break; |
71 | break; |
|
76 | case 'p': |
72 | case 'p': |
|
77 | pattern = optarg; |
73 | pattern = optarg; |
|
78 | break; |
- | ||
79 | case 'x': |
- | ||
80 | is_hex_pattern = true; |
- | ||
81 | break; |
74 | break; |
|
82 | case 'h': |
75 | case 'h': |
|
83 | default: |
76 | default: |
|
84 | usage(); |
77 | usage(); |
|
85 | } |
78 | } |
|
Line 105... | Line 98... | |||
105 | if (p_len == 0) { |
98 | if (p_len == 0) { |
|
106 | fprintf(stderr, "pattern cannot be empty\n"); |
99 | fprintf(stderr, "pattern cannot be empty\n"); |
|
107 | usage(); |
100 | usage(); |
|
108 | } |
101 | } |
|
Line 109... | Line -... | |||
109 | |
- | ||
110 | if (is_hex_pattern) { |
- | ||
111 | int i; |
- | ||
112 | |
- | ||
113 | if ((p_len / 2) > sizeof(hex_pattern)) { |
- | ||
114 | fprintf(stderr, "provided hex pattern is too long\n"); |
- | ||
115 | usage(); |
- | ||
116 | } |
- | ||
117 | |
- | ||
118 | if (p_len % 2 != 0) { |
- | ||
119 | fprintf(stderr, "the number of characters (hex) is incorrect\n"); |
- | ||
120 | usage(); |
- | ||
121 | } |
- | ||
122 | |
- | ||
123 | for (i = 0; i < (p_len / 2); i++) { |
- | ||
124 | if (sscanf(pattern + (i * 2), "%2x", &hex_buf) < 0) { |
- | ||
125 | fprintf(stderr, "invalid hex digit around %d\n", i * 2); |
- | ||
126 | usage(); |
- | ||
127 | } |
- | ||
128 | hex_pattern[i] = (char)hex_buf; |
- | ||
129 | } |
- | ||
Line 130... | Line 102... | |||
130 | } |
102 | |
|
131 | |
103 | |
|
132 | while ((n = fread(buf, 1, sizeof(buf), in)) > 0) { |
104 | while ((n = fread(buf, 1, sizeof(buf), in)) > 0) { |
|
133 | if (n < sizeof(buf)) { |
105 | if (n < sizeof(buf)) { |
|
134 | if (ferror(in)) { |
106 | if (ferror(in)) { |
|
135 | FREAD_ERROR: |
107 | FREAD_ERROR: |
|
136 | fprintf(stderr, "fread error\n"); |
108 | fprintf(stderr, "fread error\n"); |
|
137 | return EXIT_FAILURE; |
109 | return EXIT_FAILURE; |
|
Line 138... | Line -... | |||
138 | } |
- | ||
139 | } |
- | ||
140 | |
- | ||
141 | if (is_hex_pattern) { |
- | ||
142 | p_off = xor_data(buf, n, hex_pattern, (p_len / 2), |
110 | } |
|
143 | p_off); |
- | ||
Line 144... | Line 111... | |||
144 | } else { |
111 | } |
|
145 | p_off = xor_data(buf, n, pattern, p_len, p_off); |
112 | |
|
146 | } |
113 | p_off = xor_data(buf, n, pattern, p_len, p_off); |
|
147 | |
114 | |