├── .gitignore ├── Makefile ├── README.md ├── LICENSE └── chasm.c /.gitignore: -------------------------------------------------------------------------------- 1 | chasm 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC := gcc 2 | RM := rm 3 | CFLAGS += -fomit-frame-pointer -O3 4 | LDFLAGS += -lJudy 5 | 6 | all: chasm 7 | 8 | chasm: chasm.c 9 | $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) 10 | 11 | clean: 12 | $(RM) chasm 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CH.A.S.M. 2 | CHaracter Aware Split Method 3 | 4 | The main idea behind Chasm is to reuse password patterns in a way that is likely to produce probable password candidates. 5 | 6 | ## So how does this work? 7 | 8 | ![chasm1](https://user-images.githubusercontent.com/7359229/38832202-6fcb8316-4187-11e8-853c-25fef9035e4a.jpg) 9 | ![chasm2](https://user-images.githubusercontent.com/7359229/38832203-6fdf4f7c-4187-11e8-89bf-b609429e2723.jpg) 10 | ![chasm3](https://user-images.githubusercontent.com/7359229/38832204-6ff46eb6-4187-11e8-81f2-e75f8ed43571.jpg) 11 | ![chasm4](https://user-images.githubusercontent.com/7359229/38832205-70064fbe-4187-11e8-92cc-fea5531072af.jpg) 12 | 13 | ## Requirements 14 | * libjudy 15 | On debian based systems use: 16 | `$ sudo apt install libjudy-dev` 17 | 18 | ## Building 19 | `$ make` 20 | Or get the precompiled [binaries](https://github.com/Cynosureprime/chasm/releases). 21 | They are statically linked against libjudy. 22 | 23 | ### Chasm usage 24 | `$ chasm -l 30 -k 2 out/chasm < dictionary.txt` 25 | 26 | ## Some user definable options that Chasm supports: 27 | - -a (Analyze frequency, will output the frequency of the split part) 28 | - -c Create rules for dictionary attack eg left-side becomes prefix rules ^ conversely right-side becomes $ suffix rules 29 | - -s Sort by frequency, highest first 30 | - -o [charcode] Only splits if the split character matches the character code, eg -o 101 will only split on the letter 'e' at the defined midpoint/range 31 | - -l [string len] Will not split if the length of the input string if greater than this number -l 20 will skip all strings greater than length 20. 32 | - -k [min occurance] Will not write splits where occurrence is less than defined occurrence -k 2 will not output splits which only occur once 33 | - -m [mid point] Instead of splitting the string in half (len/2) you can specify a position to perform the split on 34 | - -r [number] Will split X chars around the middle including the middle, X denotes how far you want to branch out 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /chasm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #ifdef ICONV 11 | #include 12 | #endif 13 | #ifdef WIN 14 | #include 15 | #endif 16 | 17 | #define MAXLINE 20000 /* maximum line length */ 18 | 19 | /* 20 | *$Log: chasm.c,v $ 21 | *Revision 1.6 2016/11/18 14:42:30 waffle 22 | *Add new options from blazer, clean code, remove zero-length files. 23 | * 24 | *Revision 1.3 2016/11/18 02:05:10 waffle 25 | *Add rule generation 26 | * 27 | *Revision 1.2 2016/11/18 01:33:04 waffle 28 | *Added option processing, sorting, etc. 29 | * 30 | *Revision 1.1 2016/11/17 15:31:54 waffle 31 | *Initial revision 32 | * 33 | * 34 | */ 35 | 36 | 37 | Pvoid_t Left[256], Right[256], Lefthex[256], Righthex[256]; 38 | Pvoid_t Words; 39 | struct Sort { 40 | Word_t count; 41 | char *line; 42 | } *Tsort; 43 | 44 | unsigned char trhex[] = { 45 | 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 16, 16, 17, 16, 16, /* 00-0f */ 46 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 10-1f */ 47 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 20-2f */ 48 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 16, 16, 16, 16, 16, /* 30-3f */ 49 | 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 40-4f */ 50 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 50-5f */ 51 | 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 60-6f */ 52 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 70-7f */ 53 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 80-8f */ 54 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 90-9f */ 55 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* a0-af */ 56 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* b0-bf */ 57 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* c0-cf */ 58 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* d0-df */ 59 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* e0-ef */ 60 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};/* f0-ff */ 61 | 62 | static int xsort(const void *p1, const void *p2) { 63 | struct Sort *pa = (struct Sort *) p1; 64 | struct Sort *pb = (struct Sort *) p2; 65 | return (pb->count - pa->count); 66 | } 67 | 68 | static int get32(char *iline, unsigned char *dest, int len) { 69 | unsigned char c, c1, c2, *line = iline; 70 | int cnt; 71 | unsigned char *tdest; 72 | uint64_t *curi, i; 73 | 74 | cnt = 0; 75 | while ((c = *line++)) { 76 | c1 = trhex[c]; 77 | c2 = trhex[*line]; 78 | if (c1 > 16 || c2 > 16) 79 | break; 80 | if (c1 < 16 && c2 < 16) { 81 | tdest = dest; 82 | cnt = 1; 83 | *tdest++ = (c1 << 4) + c2; 84 | line++; 85 | curi = (uint64_t *) line; 86 | while (1) { 87 | i = *curi++; 88 | c1 = trhex[(i & 255)]; 89 | c2 = trhex[(i >> 8) & 255]; 90 | if (c1 > 15 || c2 > 15 || cnt >= len) 91 | goto get32_exit; 92 | *tdest++ = (c1 << 4) + c2; 93 | cnt++; 94 | i >>= 16; 95 | c1 = trhex[(i & 255)]; 96 | c2 = trhex[(i >> 8) & 255]; 97 | if (c1 > 15 || c2 > 15 || cnt >= len) 98 | goto get32_exit; 99 | *tdest++ = (c1 << 4) + c2; 100 | cnt++; 101 | i >>= 16; 102 | c1 = trhex[(i & 255)]; 103 | c2 = trhex[(i >> 8) & 255]; 104 | if (c1 > 15 || c2 > 15 || cnt >= len) 105 | goto get32_exit; 106 | *tdest++ = (c1 << 4) + c2; 107 | cnt++; 108 | i >>= 16; 109 | c1 = trhex[(i & 255)]; 110 | c2 = trhex[(i >> 8) & 255]; 111 | if (c1 > 15 || c2 > 15 || cnt >= len) 112 | goto get32_exit; 113 | *tdest++ = (c1 << 4) + c2; 114 | cnt++; 115 | } 116 | } 117 | } 118 | get32_exit: 119 | return (cnt); 120 | } 121 | 122 | char *prhex(unsigned char *hex, char *out, int len) { 123 | char *ob; 124 | static char hextab[16] = "0123456789abcdef"; 125 | int x; 126 | unsigned char v; 127 | 128 | ob = out; 129 | for (x = 0; x < len / 2; x++) { 130 | v = *hex++; 131 | *ob++ = hextab[(v >> 4) & 0xf]; 132 | *ob++ = hextab[v & 0xf]; 133 | } 134 | *ob = 0; 135 | return (out); 136 | } 137 | 138 | int main(int argc, char *argv[]) { 139 | FILE *FO1, *FO2; 140 | char buf[MAXLINE + 128], c, dbuf[(MAXLINE + 128) * 5], obuf[(MAXLINE + 128) * 5], *s; 141 | char *in, *out, *progname; 142 | size_t linecnt = 0; 143 | #ifdef ICONV 144 | size_t inlen, outlen; 145 | #endif 146 | unsigned char v; 147 | int len, hexflag, midflag, lenflag, x, opt, analfreq, dosort, curlen, dorule, ordflag, ordchar, maxlen, keepOccur; 148 | size_t tlen; 149 | Word_t *PV, rc, memsum; 150 | 151 | #ifdef ICONV 152 | iconv_t utf, cp1252; 153 | #endif 154 | 155 | #ifdef WIN 156 | setmode (0, O_BINARY); 157 | #endif 158 | 159 | midflag = 1; 160 | ordflag = 0; 161 | memsum = 0; 162 | lenflag = 0; 163 | maxlen = 0; 164 | keepOccur = 0; 165 | dorule = analfreq = ordchar = dosort = 0; 166 | 167 | progname = argv[0]; 168 | while ((opt = getopt(argc, argv, "acsm:r:o:l:k:")) != -1) { 169 | switch (opt) { 170 | case 'a': 171 | printf("Analyze frequency of word parts\n"); 172 | analfreq = 1; 173 | break; 174 | case 'c': 175 | printf("Create rule files for each left/right word lists\n"); 176 | dorule = 1; 177 | break; 178 | case 's': 179 | printf("Sort by analyzed frequency of parts\n"); 180 | dosort = 1; 181 | break; 182 | case 'o': 183 | if (*optarg >= '0' && *optarg <= '9') 184 | ordchar = atoi(optarg); 185 | else 186 | ordchar = *optarg; 187 | if (ordchar < 0 || ordchar > 255) { 188 | fprintf(stderr, "Invalid decimal or character specified for ordchar: %s\n", optarg); 189 | exit(1); 190 | } 191 | printf("Only output specific ordinal split character: %c (%d)\n", ordchar, ordchar); 192 | ordflag = 1; 193 | break; 194 | case 'l': 195 | maxlen = atoi(optarg); 196 | if (maxlen <= 0 || maxlen > MAXLINE) { 197 | fprintf(stderr, "Invalid maximum length: %s\n", optarg); 198 | exit(1); 199 | } 200 | printf("Skip processing all strings longer than %s characters\n", optarg); 201 | 202 | break; 203 | case 'k': 204 | printf("Ignore splits with occurrence less than %s\n", optarg); 205 | keepOccur = atoi(optarg); 206 | break; 207 | case 'm': 208 | midflag = 0; 209 | lenflag = atoi(optarg); 210 | if (lenflag <= 0 || lenflag > MAXLINE) { 211 | fprintf(stderr, "Invalid midpoint specified: %s\n", optarg); 212 | exit(1); 213 | } 214 | printf("Midpoint set to %d characters\n", lenflag); 215 | break; 216 | case 'r': 217 | midflag = 1; 218 | lenflag = atoi(optarg); 219 | if (lenflag <= 0 || lenflag > MAXLINE) { 220 | fprintf(stderr, "Invalid range specified: %s\n", optarg); 221 | exit(1); 222 | } 223 | printf("Midpoint range is +/- %d characters\n", lenflag); 224 | break; 225 | default: 226 | usage: 227 | printf("Usage %s [-a] [-s] [-m splitpoint] [-r range] [-o charcode] [-l maxLen] [-k minOccur] basefilename\n\n", progname); 228 | printf("-a will output the frequency analysis of each word part to the output\nfile. Primarily used for detailed analysis of data\n\n"); 229 | printf("-c creates rule files out of each of the left/right files. Use with\nany of the other switches.\n\n"); 230 | printf("-s sorts the output data by frequency - highest frequency first\n\n"); 231 | printf("-m changes from half of each word to an absolute character position\n\n"); 232 | printf("-r allows a range around the half-word point\n\n"); 233 | printf("-o only split and output on this character (ascii code/ordinal)\n\n"); 234 | printf("-l limit the max string length, strings longer than this value will not be split\n\n"); 235 | printf("-k keep split half if there are more than X instances\n\n"); 236 | printf("The base file name/path is used for each of the output files, by\nappending .L### and .R###, where ### is the ordinal value of the last\ncharacter of the left-hand word\n"); 237 | exit(0); 238 | } 239 | } 240 | argc -= optind; 241 | argv += optind; 242 | if (argc < 1) { 243 | goto usage; 244 | } 245 | if (argc > 1) { 246 | lenflag = atoi(argv[1]); 247 | if (lenflag > 0 && lenflag < MAXLINE) 248 | midflag = 0; 249 | if (argv[1][0] == '+') 250 | midflag = 1; 251 | } 252 | 253 | if (strlen(argv[0]) > MAXLINE / 2) { 254 | fprintf(stderr, "Invalid basename specified: %s\n", argv[0]); 255 | exit(1); 256 | } 257 | strcpy(dbuf, "$HEX["); 258 | #ifdef ICONV 259 | utf = iconv_open("windows-1252","utf8//IGNORE"); 260 | if (utf == (iconv_t)-1 ) { 261 | perror("iconv to utf8"); 262 | exit(1); 263 | } 264 | cp1252 = iconv_open("utf8","windows-1252//IGNORE"); 265 | if (cp1252 == (iconv_t)-1 ) { 266 | perror("iconv to windows-1252"); 267 | exit(1); 268 | } 269 | #endif 270 | 271 | while (fgets(buf, MAXLINE, stdin)) { 272 | linecnt++; 273 | len = 0; 274 | s = buf; 275 | hexflag = 0; 276 | 277 | while (len < MAXLINE && (c = *s)) { 278 | if (c == 10 || c == 13) { 279 | *s = 0; 280 | break; 281 | } 282 | if (c < ' ') 283 | hexflag |= 1; 284 | s++; 285 | len++; 286 | } 287 | if (len == 0) continue; 288 | 289 | 290 | if (len > 8 && strncmp(buf, "$HEX[", 5) == 0) { 291 | len = get32(buf + 5, buf, len - 6); 292 | hexflag = 1; 293 | } 294 | #ifdef ICONV 295 | /* Ignore this for now */ 296 | if (hexflag) { 297 | in = buf; out = obuf; inlen = len; outlen = 40000; 298 | tlen = iconv(utf,&in,&inlen,&out,&outlen); 299 | outlen = 40000-outlen; 300 | if (tlen != -1 && outlen) { 301 | prhex(obuf,&dbuf[5],outlen*2); 302 | dbuf[5+outlen*2] = ']'; 303 | dbuf[6+outlen*2] = '\n'; 304 | dbuf[7+outlen*2] = 0; 305 | fwrite(dbuf,outlen*2+7,1,FO1); 306 | } 307 | 308 | in = buf; out = obuf; inlen = len; outlen = 40000; 309 | tlen = iconv(cp1252,&in,&inlen,&out,&outlen); 310 | outlen = 40000-outlen; 311 | if (tlen != -1 && outlen) { 312 | prhex(obuf,&dbuf[5],outlen*2); 313 | dbuf[5+outlen*2] = ']'; 314 | dbuf[6+outlen*2] = '\n'; 315 | dbuf[7+outlen*2] = 0; 316 | fwrite(dbuf,outlen*2+7,1,FO2); 317 | } 318 | } 319 | #endif 320 | if (maxlen && len > maxlen) 321 | continue; 322 | if (midflag) { 323 | 324 | tlen = len / 2; 325 | for (curlen = len / 2 - abs(lenflag); curlen <= len / 2 + abs(lenflag); curlen++) { 326 | tlen = curlen; 327 | if (tlen < 0 || tlen > len) 328 | continue; 329 | if (tlen > 0) 330 | v = buf[tlen - 1]; 331 | else 332 | v = buf[0]; 333 | if (ordflag && v != ordchar) 334 | continue; 335 | if (hexflag) { 336 | prhex(buf, &dbuf[5], tlen * 2); 337 | JSLI(PV, Lefthex[v], &dbuf[5]); 338 | if (PV) 339 | (*PV)++; 340 | in = buf + tlen; 341 | tlen = len - tlen; 342 | prhex(in, &dbuf[5], tlen * 2); 343 | JSLI(PV, Righthex[v], &dbuf[5]); 344 | if (PV) 345 | (*PV)++; 346 | } else { 347 | strncpy(obuf, buf, tlen); 348 | obuf[tlen] = 0; 349 | JSLI(PV, Left[v], obuf); 350 | if (PV) 351 | (*PV)++; 352 | 353 | strncpy(obuf, buf + tlen, len - tlen); 354 | tlen = len - tlen; 355 | obuf[tlen] = 0; 356 | JSLI(PV, Right[v], obuf); 357 | if (PV) 358 | (*PV)++; 359 | } 360 | } 361 | } else { 362 | tlen = lenflag; 363 | if (tlen > len) 364 | tlen = len; 365 | if (tlen > 0) 366 | v = buf[tlen - 1]; 367 | else 368 | v = buf[0]; 369 | if (ordflag && v != ordchar) 370 | continue; 371 | if (hexflag) { 372 | prhex(buf, &dbuf[5], tlen * 2); 373 | JSLI(PV, Lefthex[v], &dbuf[5]); 374 | if (PV) 375 | (*PV)++; 376 | in = buf + tlen; 377 | tlen = len - tlen; 378 | if (tlen > 0) { 379 | prhex(in, &dbuf[5], tlen * 2); 380 | JSLI(PV, Righthex[v], &dbuf[5]); 381 | if (PV) 382 | (*PV)++; 383 | } 384 | } else { 385 | strncpy(obuf, buf, tlen); 386 | obuf[tlen] = 0; 387 | JSLI(PV, Left[v], obuf); 388 | if (PV) 389 | (*PV)++; 390 | 391 | if (tlen < len) { 392 | strncpy(obuf, buf + tlen, len - tlen); 393 | tlen = len - tlen; 394 | obuf[tlen] = 0; 395 | JSLI(PV, Right[v], obuf); 396 | if (PV) 397 | (*PV)++; 398 | } 399 | } 400 | } 401 | } 402 | printf("%ld lines processed\n", linecnt); 403 | if (dosort) { 404 | Tsort = calloc(linecnt, sizeof(struct Sort)); 405 | if (!Tsort) { 406 | fprintf(stderr, "Can't sort, out of memory\n"); 407 | exit(1); 408 | } 409 | } 410 | linecnt = 0; 411 | for (x = 0; x < 256; x++) { 412 | int cursize, y; 413 | 414 | if (Left[x] == NULL && Lefthex[x] == NULL) continue; 415 | sprintf(obuf, "%s.%03dL", argv[0], x); 416 | FO1 = fopen(obuf, "wb"); 417 | if (!FO1) { 418 | perror(obuf); 419 | exit(1); 420 | } 421 | if (dorule) { 422 | sprintf(obuf, "%s.%03dL.rule", argv[0], x); 423 | FO2 = fopen(obuf, "wb"); 424 | if (!FO2) { 425 | perror(obuf); 426 | exit(1); 427 | } 428 | } 429 | obuf[0] = 0; 430 | if (dosort) { 431 | cursize = 0; 432 | JSLF(PV, Left[x], obuf); 433 | while (PV) { 434 | if (keepOccur == 0 || *PV > keepOccur) { 435 | Tsort[cursize].count = *PV; 436 | Tsort[cursize].line = strdup(obuf); 437 | if (!Tsort[cursize].line) { 438 | fprintf(stderr, "Out of memory allocating string\n"); 439 | exit(1); 440 | } 441 | cursize++; 442 | } 443 | JSLN(PV, Left[x], obuf); 444 | } 445 | obuf[0] = 0; 446 | JSLF(PV, Lefthex[x], obuf); 447 | while (PV) { 448 | if (keepOccur == 0 || *PV > keepOccur) { 449 | in = obuf; 450 | out = dbuf + 5; 451 | tlen = 0; 452 | while ((*out = *in)) { 453 | tlen++; 454 | out++; 455 | in++; 456 | } 457 | *out++ = ']'; 458 | *out++ = '\n', *out++ = 0; 459 | Tsort[cursize].count = *PV; 460 | Tsort[cursize].line = strdup(dbuf); 461 | if (!Tsort[cursize].line) { 462 | fprintf(stderr, "Out of memory allocating string\n"); 463 | exit(1); 464 | } 465 | cursize++; 466 | } 467 | JSLN(PV, Lefthex[x], obuf); 468 | } 469 | qsort(Tsort, cursize, sizeof(struct Sort), xsort); 470 | linecnt += cursize; 471 | for (y = 0; y < cursize; y++) { 472 | if (analfreq) 473 | fprintf(FO1, "%ld ", Tsort[y].count); 474 | fputs(Tsort[y].line, FO1); 475 | fputc('\n', FO1); 476 | if (dorule) { 477 | int llen, olen;; 478 | if (strncmp(Tsort[y].line, "$HEX[", 5) != 0) { 479 | olen = llen = strlen(Tsort[y].line); 480 | for (out = buf; olen; olen--) { 481 | *out++ = '^'; 482 | *out++ = Tsort[y].line[olen - 1]; 483 | } 484 | *out++ = '\n'; 485 | *out = 0; 486 | fputs(buf, FO2); 487 | } 488 | } 489 | free(Tsort[y].line); 490 | } 491 | } else { 492 | JSLF(PV, Left[x], obuf); 493 | while (PV) { 494 | if (keepOccur == 0 || *PV > keepOccur) { 495 | if (analfreq) 496 | fprintf(FO1, "%ld ", *PV); 497 | fputs(obuf, FO1); 498 | fputc('\n', FO1); 499 | if (dorule) { 500 | int llen, olen;; 501 | olen = llen = strlen(obuf); 502 | for (out = buf; olen; olen--) { 503 | *out++ = '^'; 504 | *out++ = obuf[olen - 1]; 505 | } 506 | *out++ = '\n'; 507 | *out = 0; 508 | fputs(buf, FO2); 509 | } 510 | linecnt++; 511 | } 512 | JSLN(PV, Left[x], obuf); 513 | } 514 | obuf[0] = 0; 515 | JSLF(PV, Lefthex[x], obuf); 516 | while (PV) { 517 | if (keepOccur == 0 || *PV > keepOccur) { 518 | in = obuf; 519 | out = dbuf + 5; 520 | tlen = 0; 521 | while ((*out = *in)) { 522 | tlen++; 523 | out++; 524 | in++; 525 | } 526 | *out++ = ']'; 527 | *out++ = '\n', *out++ = 0; 528 | if (analfreq) fprintf(FO1, "%ld ", *PV); 529 | fputs(dbuf, FO1); 530 | linecnt++; 531 | } 532 | JSLN(PV, Lefthex[x], obuf); 533 | } 534 | } 535 | if (ftell(FO1)) 536 | fclose(FO1); 537 | else { 538 | fclose(FO1); 539 | sprintf(obuf, "%s.%03dL", argv[0], x); 540 | unlink(obuf); 541 | } 542 | if (dorule) { 543 | if (ftell(FO2)) 544 | fclose(FO2); 545 | else { 546 | fclose(FO2); 547 | sprintf(obuf, "%s.%03dL.rule", argv[0], x); 548 | unlink(obuf); 549 | } 550 | } 551 | 552 | sprintf(obuf, "%s.%03dR", argv[0], x); 553 | FO1 = fopen(obuf, "wb"); 554 | if (!FO1) { 555 | perror(obuf); 556 | exit(1); 557 | } 558 | if (dorule) { 559 | sprintf(obuf, "%s.%03dR.rule", argv[0], x); 560 | FO2 = fopen(obuf, "wb"); 561 | if (!FO2) { 562 | perror(obuf); 563 | exit(1); 564 | } 565 | } 566 | obuf[0] = 0; 567 | if (dosort) { 568 | cursize = 0; 569 | JSLF(PV, Right[x], obuf); 570 | while (PV) { 571 | if (keepOccur == 0 || *PV > keepOccur) { 572 | Tsort[cursize].count = *PV; 573 | Tsort[cursize].line = strdup(obuf); 574 | if (!Tsort[cursize].line) { 575 | fprintf(stderr, "Out of memory allocating string\n"); 576 | exit(1); 577 | } 578 | cursize++; 579 | } 580 | JSLN(PV, Right[x], obuf); 581 | } 582 | obuf[0] = 0; 583 | JSLF(PV, Righthex[x], obuf); 584 | while (PV) { 585 | if (keepOccur == 0 || *PV > keepOccur) { 586 | in = obuf; 587 | out = dbuf + 5; 588 | tlen = 0; 589 | while ((*out = *in)) { 590 | tlen++; 591 | out++; 592 | in++; 593 | } 594 | *out++ = ']'; 595 | *out++ = '\n', *out++ = 0; 596 | Tsort[cursize].count = *PV; 597 | Tsort[cursize].line = strdup(dbuf); 598 | if (!Tsort[cursize].line) { 599 | fprintf(stderr, "Out of memory allocating string\n"); 600 | exit(1); 601 | } 602 | cursize++; 603 | } 604 | JSLN(PV, Righthex[x], obuf); 605 | } 606 | qsort(Tsort, cursize, sizeof(struct Sort), xsort); 607 | linecnt += cursize; 608 | for (y = 0; y < cursize; y++) { 609 | if (analfreq) 610 | fprintf(FO1, "%ld ", Tsort[y].count); 611 | fputs(Tsort[y].line, FO1); 612 | fputc('\n', FO1); 613 | if (dorule) { 614 | int llen, olen;; 615 | if (strncmp(Tsort[y].line, "$HEX[", 5) != 0) { 616 | llen = strlen(Tsort[y].line); 617 | for (out = buf, olen = 0; olen < llen; olen++) { 618 | *out++ = '$'; 619 | *out++ = Tsort[y].line[olen]; 620 | } 621 | *out++ = '\n'; 622 | *out = 0; 623 | fputs(buf, FO2); 624 | } 625 | } 626 | free(Tsort[y].line); 627 | } 628 | } else { 629 | JSLF(PV, Right[x], obuf); 630 | while (PV) { 631 | if (keepOccur == 0 || *PV > keepOccur) { 632 | if (analfreq) fprintf(FO1, "%ld ", *PV); 633 | fputs(obuf, FO1); 634 | fputc('\n', FO1); 635 | if (dorule) { 636 | int llen, olen;; 637 | llen = strlen(obuf); 638 | for (out = buf, olen = 0; olen < llen; olen++) { 639 | *out++ = '$'; 640 | *out++ = obuf[olen]; 641 | } 642 | *out++ = '\n'; 643 | *out = 0; 644 | fputs(buf, FO2); 645 | } 646 | linecnt++; 647 | } 648 | JSLN(PV, Right[x], obuf); 649 | } 650 | obuf[0] = 0; 651 | JSLF(PV, Righthex[x], obuf); 652 | while (PV) { 653 | if (keepOccur == 0 || *PV > keepOccur) { 654 | in = obuf; 655 | out = dbuf + 5; 656 | tlen = 0; 657 | while ((*out = *in)) { 658 | tlen++; 659 | out++; 660 | in++; 661 | } 662 | *out++ = ']'; 663 | *out++ = '\n', *out++ = 0; 664 | if (analfreq) fprintf(FO1, "%ld ", *PV); 665 | fputs(dbuf, FO1); 666 | linecnt++; 667 | } 668 | JSLN(PV, Righthex[x], obuf); 669 | } 670 | } 671 | if (ftell(FO1)) 672 | fclose(FO1); 673 | else { 674 | fclose(FO1); 675 | sprintf(obuf, "%s.%03dR", argv[0], x); 676 | unlink(obuf); 677 | } 678 | if (dorule) { 679 | if (ftell(FO2)) 680 | fclose(FO2); 681 | else { 682 | fclose(FO2); 683 | sprintf(obuf, "%s.%03dR.rule", argv[0], x); 684 | unlink(obuf); 685 | } 686 | } 687 | JSLFA(rc, Left[x]); 688 | memsum += rc; 689 | JSLFA(rc, Lefthex[x]); 690 | memsum += rc; 691 | JSLFA(rc, Right[x]); 692 | memsum += rc; 693 | JSLFA(rc, Righthex[x]); 694 | memsum += rc; 695 | } 696 | printf("%lu lines written\n", linecnt); 697 | printf("%lu total memory used\n", memsum); 698 | 699 | return (0); 700 | } 701 | 702 | --------------------------------------------------------------------------------