├── .gitignore ├── LICENSE ├── README.md ├── base14.go ├── base14_amd64.go ├── base14_amd64.s ├── base14_arm64.go ├── base14_arm64.s ├── base14_generic.go ├── base14_noasm.go ├── base14_test.go ├── c └── base1432.c ├── conv.go ├── cpuid.go ├── cpuid_amd64.s ├── decoder.go ├── encoder.go ├── go.mod ├── go.sum ├── helper.go └── slice.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-base16384 2 | base16384 interface of golang 3 | 4 | # Usage 5 | ## Quick start 6 | 7 | ```go 8 | package main 9 | 10 | import ( 11 | "fmt" 12 | 13 | b14 "github.com/fumiama/go-base16384" 14 | ) 15 | 16 | func main() { 17 | str := b14.EncodeString("1234567") 18 | fmt.Println(str, b14.DecodeString(str)) 19 | } 20 | ``` 21 | 22 | ## API 23 | 24 | ```go 25 | func Encode(b []byte) (encd []byte) 26 | 27 | func EncodeLen(in int) (out int) 28 | 29 | func EncodeTo(b, encd []byte) error 30 | 31 | func EncodeToString(b []byte) string 32 | 33 | func EncodeFromString(s string) []byte 34 | 35 | func EncodeString(s string) string 36 | 37 | func DecodeLen(in, offset int) (out int) 38 | 39 | func Decode(b []byte) (decd []byte) 40 | 41 | func DecodeTo(b []byte, decd []byte) error 42 | 43 | func DecodeToString(d []byte) string 44 | 45 | func DecodeFromString(s string) []byte 46 | 47 | func DecodeString(s string) string 48 | ``` 49 | 50 | ## Stream API 51 | 52 | ```go 53 | package main 54 | 55 | import ( 56 | "bytes" 57 | "crypto/rand" 58 | "io" 59 | 60 | b14 "github.com/fumiama/go-base16384" 61 | ) 62 | 63 | func main() { 64 | buf := make([]byte, 1024*1024+1) 65 | _, err := rand.Read(buf) 66 | if err != nil { 67 | panic(err) 68 | } 69 | w := bytes.NewBuffer(make([]byte, 0, 1024*1024+1)) 70 | e := b14.NewEncoder(bytes.NewReader(buf)) 71 | _, err = io.Copy(w, e) 72 | if err != nil { 73 | panic(err) 74 | } 75 | w2 := bytes.NewBuffer(make([]byte, 0, 1024*1024+1)) 76 | d := b14.NewDecoder(bytes.NewReader(w.Bytes())) 77 | _, err = io.Copy(w2, d) 78 | if err != nil { 79 | panic(err) 80 | } 81 | if !bytes.Equal(buf, w2.Bytes()) { 82 | panic("fail!") 83 | } 84 | } 85 | ``` -------------------------------------------------------------------------------- /base14.go: -------------------------------------------------------------------------------- 1 | // Package base14 base16384 的 go 接口 2 | package base14 3 | 4 | import ( 5 | "errors" 6 | ) 7 | 8 | //go:nosplit 9 | func EncodeLen(in int) (out int) { 10 | out = in / 7 * 8 11 | offset := in % 7 12 | switch offset { //算上偏移标志字符占用的2字节 13 | case 0: 14 | break 15 | case 1: 16 | out += 4 17 | case 2, 3: 18 | out += 6 19 | case 4, 5: 20 | out += 8 21 | case 6: 22 | out += 10 23 | } 24 | return 25 | } 26 | 27 | func Encode(b []byte) (encd []byte) { 28 | outlen := len(b) / 7 * 8 29 | offset := len(b) % 7 30 | switch offset { //算上偏移标志字符占用的2字节 31 | case 0: 32 | break 33 | case 1: 34 | outlen += 4 35 | case 2, 3: 36 | outlen += 6 37 | case 4, 5: 38 | outlen += 8 39 | case 6: 40 | outlen += 10 41 | } 42 | encd = make([]byte, outlen) 43 | encode(offset, outlen, b, encd) 44 | return 45 | } 46 | 47 | func EncodeTo(b, encd []byte) (int, error) { 48 | outlen := len(b) / 7 * 8 49 | offset := len(b) % 7 50 | switch offset { //算上偏移标志字符占用的2字节 51 | case 0: 52 | break 53 | case 1: 54 | outlen += 4 55 | case 2, 3: 56 | outlen += 6 57 | case 4, 5: 58 | outlen += 8 59 | case 6: 60 | outlen += 10 61 | } 62 | if len(encd) < outlen { 63 | return 0, errors.New("encd too small") 64 | } 65 | encode(offset, outlen, b, encd) 66 | return outlen, nil 67 | } 68 | 69 | //go:nosplit 70 | func DecodeLen(in, offset int) (out int) { 71 | out = in 72 | switch offset { //算上偏移标志字符占用的2字节 73 | case 0: 74 | break 75 | case 1: 76 | out -= 4 77 | case 2, 3: 78 | out -= 6 79 | case 4, 5: 80 | out -= 8 81 | case 6: 82 | out -= 10 83 | } 84 | out = out/8*7 + offset 85 | return 86 | } 87 | 88 | //go:nosplit 89 | func Decode(b []byte) (decd []byte) { 90 | outlen := len(b) 91 | offset := 0 92 | if b[len(b)-2] == '=' { 93 | offset = int(b[len(b)-1]) 94 | switch offset { //算上偏移标志字符占用的2字节 95 | case 0: 96 | break 97 | case 1: 98 | outlen -= 4 99 | case 2, 3: 100 | outlen -= 6 101 | case 4, 5: 102 | outlen -= 8 103 | case 6: 104 | outlen -= 10 105 | } 106 | } 107 | outlen = outlen/8*7 + offset 108 | decd = make([]byte, outlen+8) 109 | decode(offset, outlen, b, decd) 110 | return decd[:outlen] 111 | } 112 | 113 | //go:nosplit 114 | func DecodeTo(b []byte, decd []byte) (int, error) { 115 | outlen := len(b) 116 | offset := 0 117 | if b[len(b)-2] == '=' { 118 | offset = int(b[len(b)-1]) 119 | switch offset { //算上偏移标志字符占用的2字节 120 | case 0: 121 | break 122 | case 1: 123 | outlen -= 4 124 | case 2, 3: 125 | outlen -= 6 126 | case 4, 5: 127 | outlen -= 8 128 | case 6: 129 | outlen -= 10 130 | } 131 | } 132 | outlen = outlen/8*7 + offset 133 | if len(decd) < outlen { 134 | return 0, errors.New("decd too small") 135 | } 136 | decode(offset, outlen, b, decd) 137 | return outlen, nil 138 | } 139 | -------------------------------------------------------------------------------- /base14_amd64.go: -------------------------------------------------------------------------------- 1 | //go:build amd64 2 | // +build amd64 3 | 4 | package base14 5 | 6 | import ( 7 | "encoding/binary" 8 | ) 9 | 10 | //go:noescape 11 | //go:nosplit 12 | func _encode(offset int, b, encd []byte) (sum uint64, n uint64) 13 | 14 | //go:noescape 15 | //go:nosplit 16 | func _decode(offset, outlen int, b, decd []byte) 17 | 18 | func encode(offset, outlen int, b, encd []byte) { 19 | if movbe { 20 | sum, n := _encode(offset, b, encd) 21 | if offset == 0 { 22 | return 23 | } 24 | var tmp [8]byte 25 | binary.LittleEndian.PutUint64(tmp[:], sum) 26 | copy(encd[n:], tmp[:]) 27 | encd[outlen-2] = '=' 28 | encd[outlen-1] = byte(offset) 29 | } else { 30 | encodeGeneric(offset, outlen, b, encd) 31 | } 32 | } 33 | 34 | func decode(offset, outlen int, b, decd []byte) { 35 | if movbe { 36 | _decode(offset, outlen, b, decd) 37 | } else { 38 | decodeGeneric(offset, outlen, b, decd) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /base14_amd64.s: -------------------------------------------------------------------------------- 1 | //go:build amd64 2 | // +build amd64 3 | 4 | #include "textflag.h" 5 | 6 | // func _encode(offset int, b, encd []byte) (sum uint64, n uint64) 7 | TEXT ·_encode(SB), NOSPLIT, $0-72 8 | MOVQ ·offset+0(FP), R10 9 | MOVQ ·data+8(FP), DI 10 | MOVQ ·dlen+16(FP), R8 11 | MOVQ ·encd+32(FP), R9 12 | XORQ CX, CX 13 | XORQ SI, SI 14 | SUBQ $6, R8 15 | JLE encrem 16 | MOVQ $4611404543450677248, BP 17 | MOVQ $70364449210368, BX 18 | MOVQ $5620578098173988352, R11 19 | 20 | enclop: 21 | MOVBEQ (DI)(CX*1), DX 22 | INCQ SI 23 | ADDQ $7, CX 24 | MOVQ DX, R13 25 | MOVQ DX, R12 26 | SHRQ $2, R13 27 | SHRQ $4, R12 28 | ANDQ BX, R12 29 | ANDQ BP, R13 30 | ORQ R12, R13 31 | MOVQ DX, R12 32 | SHRQ $8, DX 33 | SHRQ $6, R12 34 | ANDL $16383, DX 35 | ANDL $1073676288, R12 36 | ORQ R13, R12 37 | ORQ R12, DX 38 | ADDQ R11, DX 39 | MOVBEQ DX, -8(R9)(SI*8) 40 | CMPQ CX, R8 41 | JL enclop 42 | 43 | encrem: 44 | TESTQ R10, R10 45 | JE encend 46 | 47 | MOVBLZX (DI)(CX*1), DX 48 | MOVL DX, R8 49 | SALQ $14, DX 50 | SHRB $2, R8 51 | ANDL $49152, DX 52 | MOVBLZX R8, R8 53 | ORQ R8, DX 54 | CMPL R10, $1 55 | JE encsav 56 | 57 | MOVBQSX 1(DI)(CX*1), R8 58 | MOVQ R8, R11 59 | SALQ $20, R8 60 | SALQ $6, R11 61 | ANDL $3145728, R8 62 | ANDL $16128, R11 63 | ORQ R11, DX 64 | ORQ R8, DX 65 | CMPL R10, $2 66 | JE encsav 67 | 68 | MOVBQSX 2(DI)(CX*1), R8 69 | MOVQ R8, R11 70 | SALQ $28, R8 71 | SALQ $12, R11 72 | MOVL R8, R8 73 | ANDL $983040, R11 74 | ORQ R11, R8 75 | ORQ R8, DX 76 | CMPL R10, $3 77 | JE encsav 78 | 79 | MOVQ $257698037760, BX 80 | MOVBQSX 3(DI)(CX*1), R8 81 | MOVQ R8, R11 82 | SALQ $34, R8 83 | SALQ $20, R11 84 | ANDQ BX, R8 85 | ANDL $251658240, R11 86 | ORQ R11, R8 87 | ORQ R8, DX 88 | CMPL R10, $4 89 | JE encsav 90 | 91 | MOVQ $12884901888, BX 92 | MOVBQSX 4(DI)(CX*1), R8 93 | MOVQ R8, R11 94 | SALQ $42, R8 95 | SALQ $26, R11 96 | ANDQ BX, R11 97 | MOVQ $277076930199552, BX 98 | ANDQ BX, R8 99 | ORQ R11, R8 100 | ORQ R8, DX 101 | CMPL R10, $5 102 | JE encsav 103 | 104 | MOVQ $3298534883328, R8 105 | MOVBQSX 5(DI)(CX*1), CX 106 | MOVQ CX, DI 107 | SALQ $48, CX 108 | SALQ $34, DI 109 | ANDQ R8, DI 110 | MOVQ $17732923532771328, R8 111 | ANDQ R8, CX 112 | ORQ DI, CX 113 | ORQ CX, DX 114 | 115 | encsav: 116 | MOVQ $21955383195992142, CX 117 | ADDQ CX, DX 118 | SHLQ $3, SI 119 | MOVQ DX, ·sum+56(FP) 120 | MOVQ SI, ·n+64(FP) 121 | 122 | encend: 123 | RET 124 | 125 | 126 | // func _decode(offset, outlen int, b, decd []byte) 127 | TEXT ·_decode(SB), NOSPLIT, $0-64 128 | MOVQ ·offset+0(FP), BX 129 | MOVQ ·outlen+8(FP), R8 130 | MOVQ ·data+16(FP), DI 131 | MOVQ ·decd+40(FP), R9 132 | XORQ CX, CX 133 | XORQ SI, SI 134 | SUBQ $6, R8 135 | JLE decrem 136 | MOVQ $-5620578098173988352, R12 137 | MOVQ $-1125899906842624, BP 138 | MOVQ $1125831187365888, R11 139 | MOVQ $68715282432, R10 140 | 141 | declop: 142 | MOVBEQ (DI)(SI*8), DX 143 | INCQ SI 144 | ADDQ R12, DX 145 | MOVQ DX, R13 146 | LEAQ 0(DX*4), R14 147 | SALQ $4, R13 148 | ANDQ BP, R14 149 | ANDQ R11, R13 150 | ORQ R13, R14 151 | MOVQ DX, R13 152 | SALQ $8, DX 153 | SALQ $6, R13 154 | ANDL $4194048, DX 155 | ANDQ R10, R13 156 | ORQ R14, R13 157 | ORQ R13, DX 158 | MOVBEQ DX, (R9)(CX*1) 159 | ADDQ $7, CX 160 | CMPQ CX, R8 161 | JL declop 162 | 163 | decrem: 164 | TESTQ BX, BX 165 | JE decend 166 | 167 | MOVQ (DI)(SI*8), DI 168 | LEAQ -78(DI), SI 169 | MOVQ SI, DX 170 | SALL $2, SI 171 | SHRQ $14, DX 172 | ANDL $3, DX 173 | ORL SI, DX 174 | MOVB DX, 0(R9)(CX*1) 175 | CMPL BX, $1 176 | JE decend 177 | 178 | LEAQ -5111886(DI), DX 179 | MOVQ DX, SI 180 | MOVQ DX, R8 181 | SHRQ $6, SI 182 | SHRQ $20, R8 183 | ANDL $-4, SI 184 | ANDL $3, R8 185 | ORL R8, SI 186 | MOVB SI, 1(R9)(CX*1) 187 | CMPL BX, $2 188 | JE decend 189 | 190 | MOVQ DX, SI 191 | SHRQ $28, DX 192 | SHRQ $12, SI 193 | ANDL $15, DX 194 | ANDL $-16, SI 195 | ORL SI, DX 196 | MOVB DX, 2(R9)(CX*1) 197 | CMPL BX, $3 198 | JE decend 199 | 200 | MOVQ $-335012560974, DX 201 | ADDQ DI, DX 202 | MOVQ DX, SI 203 | MOVQ DX, R8 204 | SHRQ $20, SI 205 | SHRQ $34, R8 206 | ANDL $-16, SI 207 | ANDL $15, R8 208 | ORL R8, SI 209 | MOVB SI, 3(R9)(CX*1) 210 | CMPL BX, $4 211 | JE decend 212 | 213 | MOVQ DX, SI 214 | SHRQ $42, DX 215 | SHRQ $26, SI 216 | ANDL $63, DX 217 | ANDL $-64, SI 218 | ORL SI, DX 219 | MOVB DX, 4(R9)(CX*1) 220 | CMPL BX, $5 221 | JE decend 222 | 223 | MOVQ $-21955383195992142, DX 224 | ADDQ DX, DI 225 | MOVQ DI, DX 226 | SHRQ $48, DI 227 | SHRQ $34, DX 228 | ANDL $63, DI 229 | ANDL $-64, DX 230 | ORL DI, DX 231 | MOVB DX, 5(R9)(CX*1) 232 | 233 | decend: 234 | RET 235 | -------------------------------------------------------------------------------- /base14_arm64.go: -------------------------------------------------------------------------------- 1 | //go:build arm64 2 | // +build arm64 3 | 4 | package base14 5 | 6 | import ( 7 | "encoding/binary" 8 | ) 9 | 10 | //go:noescape 11 | //go:nosplit 12 | func _encode(offset int, b, encd []byte) (sum uint64, n int) 13 | 14 | //go:noescape 15 | //go:nosplit 16 | func _decode(offset, outlen int, b, decd []byte) 17 | 18 | func encode(offset, outlen int, b, encd []byte) { 19 | sum, n := _encode(offset, b, encd) 20 | if offset == 0 { 21 | return 22 | } 23 | var tmp [4]byte 24 | binary.LittleEndian.PutUint32(tmp[:], uint32(sum)) 25 | copy(encd[n:], tmp[:]) 26 | encd[outlen-2] = '=' 27 | encd[outlen-1] = byte(offset) 28 | } 29 | 30 | func decode(offset, outlen int, b, decd []byte) { 31 | _decode(offset, outlen, b, decd) 32 | } 33 | -------------------------------------------------------------------------------- /base14_arm64.s: -------------------------------------------------------------------------------- 1 | //go:build arm64 2 | // +build arm64 3 | 4 | #include "textflag.h" 5 | 6 | // func _encode(offset, b, encd []byte) (sum uint64, n int) 7 | TEXT ·_encode(SB), NOSPLIT, $0-72 8 | MOVD ·offset+0(FP), R0 9 | MOVD ·data+8(FP), R9 10 | MOVD ·dlen+16(FP), R3 11 | MOVD ·encd+32(FP), R5 12 | 13 | SUBW $6, R3, R3 14 | CMPW $0, R3 15 | BLE enctil 16 | MOVW $0x4e00, R11 17 | SUB $8, R5, R14 18 | SUB $4, R5, R13 19 | MOVD $2, R8 20 | MOVW $0, R10 // int32_t i = 0 21 | MOVK $(0x4e00<<16), R11 22 | enclop: 23 | MOVW (R9), R4 24 | ADDW $7, R10, R10 25 | MOVW R8, R12 26 | CMPW R3, R10 27 | REVW R4, R4 28 | ADD $7, R9, R9 29 | LSRW $2, R4, R6 30 | UBFX $4, R4, $14, R15 31 | ANDW $0x3fff0000, R6, R6 32 | UBFIZW $26, R4, $4, R7 33 | ORRW R15, R6, R6 34 | ADDW R11, R6, R6 35 | REVW R6, R6 36 | MOVW R6, (R14)(R8<<2) 37 | MOVW -3(R9), R4 38 | REVW R4, R4 39 | LSRW $6, R4, R4 40 | ANDW $0x3fffffc, R4, R4 41 | ORRW R7, R4, R4 42 | ANDW $0x3fff0000, R4, R6 43 | UBFX $2, R4, $14, R4 44 | ORRW R6, R4, R4 45 | ADDW R11, R4, R4 46 | REVW R4, R4 47 | MOVW R4, (R13)(R8<<2) 48 | ADDW $2, R8, R8 49 | BLT enclop 50 | encrem: 51 | ANDSW $0xff, R0, R0 52 | BEQ encret 53 | 54 | MOVBU (R2)(R10.SXTW), R3 55 | UXTW R12, R8 56 | CMPW $1, R0 57 | SXTW R10, R10 58 | ADD R8<<2, R5, R7 59 | UBFIZW $14, R3, $2, R4 60 | ORRW R3>>2, R4, R3 61 | BEQ encsum 62 | 63 | ADD R10, R2, R9 64 | CMPW $2, R0 65 | MOVBU 1(R9), R6 66 | LSLW $6, R6, R4 67 | UBFIZW $20, R6, $2, R6 68 | ANDW $0x3f00, R4, R4 69 | ORRW R3, R4, R3 70 | ORRW R3, R6, R3 71 | BEQ encsum 72 | 73 | MOVBU 2(R9), R4 74 | CMPW $3, R0 75 | LSLW $12, R4, R6 76 | ANDW $0xf0000, R6, R6 77 | ORRW R4<<28, R6, R4 78 | ORRW R4, R3, R3 79 | BEQ encsum 80 | 81 | ADD $3, R10, R10 82 | ADDW $1, R12, R12 83 | CMPW $4, R0 84 | ADD R12<<2, R5, R7 85 | MOVBU (R2)(R10), R4 86 | LSLW $20, R4, R4 87 | ANDW $0xf000000, R4, R4 88 | ORRW R3, R4, R3 89 | ADDW $0x4e0000, R3, R3 90 | ADDW $0x4e, R3, R3 91 | MOVW R3, (R5)(R8<<2) 92 | MOVBU (R2)(R10), R3 93 | UBFIZW $2, R3, $4, R3 94 | BEQ encsum 95 | 96 | MOVBU 4(R9), R4 97 | CMPW $5, R0 98 | UBFIZW $10, R4, $6, R2 99 | ORRW R3, R2, R3 100 | ORRW R4>>6, R3, R3 101 | BEQ encsum 102 | 103 | MOVBU 5(R9), R4 104 | LSLW $2, R4, R2 105 | UBFIZW $16, R4, $6, R4 106 | ANDW $0x300, R2, R2 107 | ORRW R4, R2, R2 108 | ORRW R2, R3, R3 109 | encsum: 110 | ADDW $0x4e0000, R3, R3 111 | ADDW $0x4e, R3, R3 112 | SUB R5, R7, R7 113 | MOVD R3, ·sum+56(FP) 114 | MOVD R7, ·n+64(FP) 115 | encret: 116 | RET 117 | enctil: 118 | MOVW $0, R10 119 | MOVW $0, R12 120 | JMP encrem 121 | 122 | // func _decode(offset, outlen int, b, decd []byte) 123 | TEXT ·_decode(SB), NOSPLIT, $0-64 124 | MOVD ·offset+0(FP), R0 125 | MOVD ·outlen+8(FP), R1 126 | MOVD ·data+16(FP), R2 127 | MOVD ·decd+40(FP), R5 128 | 129 | SUBW $6, R1, R1 // sub w1, w1, #6 130 | CMPW $0, R1 // cmp w1, 0 131 | BLE dectil // ble .L7 132 | MOVW $0xb200, R11 // mov w11, 45568 133 | MOVD R5, R9 // mov x9, x5 134 | SUB $8, R2, R14 // sub x14, x2, #8 135 | SUB $4, R2, R13 // sub x13, x2, #4 136 | MOVD $2, R8 // mov x8, 2 137 | MOVW $0, R10 // mov w10, 0 138 | MOVK $(0xb1ff<<16), R11 // movk w11, 0xb1ff, lsl 16 139 | declop: 140 | MOVW (R14)(R8<<2), R4 141 | ADDW $7, R10, R10 142 | MOVW (R13)(R8<<2), R3 143 | MOVW R8, R12 144 | REVW R4, R4 145 | CMPW R1, R10 146 | ADDW R11, R4, R4 147 | REVW R3, R3 148 | ADDW R11, R3, R3 149 | ADD $2, R8, R8 150 | LSLW $2, R4, R7 151 | UBFIZW $4, R4, $14, R4 152 | LSLW $6, R3, R6 153 | ANDW $-262144, R7, R7 154 | ORRW R4, R7, R7 155 | ANDW $-4194304, R6, R4 156 | UBFIZW $8, R3, $14, R6 157 | ORRW R3>>26, R7, R3 158 | ORRW R6, R4, R4 159 | REVW R3, R3 160 | REVW R4, R4 161 | STPW (R3, R4), (R9) 162 | ADD $7, R9, R9 163 | BLT declop 164 | decrem: 165 | CBZW R0, decret // cbz w0, .L1 166 | MOVW (R2)(R12.UXTW<<2), R1 // ldr w1, [x2, w12, uxtw 2] 167 | CMPW $1, R0 // cmp w0, 1 168 | SUBW $0x4e, R1, R3 // sub w3, w1, #78 169 | UBFX $14, R3, $2, R4 // ubfx x4, x3, 14, 2 170 | ORRW R3<<2, R4, R3 // orr w3, w4, w3, lsl 2 171 | MOVB R3, (R5)(R10.SXTW) // strb w3, [x5, w10, sxtw] 172 | BEQ decret // beq .L1 173 | 174 | MOVW $0xffb2, R7 // mov w7, 65458 175 | ADDW $1, R10, R6 // add w6, w10, 1 176 | MOVK $(0xffb1<<16), R7 // movk w7, 0xffb1, lsl 16 177 | ADDW R7, R1, R1 // add w1, w1, w7 178 | CMPW $2, R0 // cmp w0, 2 179 | UBFX $20, R1, $8, R4 // ubfx x4, x1, 20, 8 180 | LSRW $6, R1, R3 // lsr w3, w1, 6 181 | ANDW $3, R4, R8 // and w8, w4, 3 182 | ANDW $-4, R3, R3 // and w3, w3, -4 183 | ORRW R8, R3, R3 // orr w3, w3, w8 184 | MOVB R3, (R5)(R6.SXTW) // strb w3, [x5, w6, sxtw] 185 | BEQ decret // beq .L1 186 | 187 | ADDW $2, R10, R3 // add w3, w10, 2 188 | LSRW $12, R1, R6 // lsr w6, w1, 12 189 | ANDW $-16, R6, R6 // and w6, w6, -16 190 | CMPW $3, R0 // cmp w0, 3 191 | ORRW R1>>28, R6, R1 // orr w1, w6, w1, lsr 28 192 | MOVB R1, (R5)(R3.SXTW) // strb w1, [x5, w3, sxtw] 193 | BEQ decret // beq .L1 194 | 195 | ADDW $3, R10, R1 // add w1, w10, 3 196 | ADDW $1, R12, R12 // add w12, w12, 1 197 | ANDW $0xf0, R4, R4 // and w4, w4, 240 198 | CMPW $4, R0 // cmp w0, 4 199 | MOVW (R2)(R12<<2), R3 // ldr w3, [x2, x12, lsl 2] 200 | SUBW $0x4e, R3, R2 // sub w2, w3, #78 201 | UBFX $2, R2, $4, R6 // ubfx x6, x2, 2, 4 202 | ORRW R6, R4, R4 // orr w4, w4, w6 203 | MOVB R4, (R5)(R1.SXTW) // strb w4, [x5, w1, sxtw] 204 | BEQ decret // beq .L1 205 | 206 | ADDW $4, R10, R1 // add w1, w10, 4 207 | UBFX $10, R2, $6, R4 // ubfx x4, x2, 10, 6 208 | ORRW R2<<6, R4, R2 // orr w2, w4, w2, lsl 6 209 | CMPW $5, R0 // cmp w0, 5 210 | MOVB R2, (R5)(R1.SXTW) // strb w2, [x5, w1, sxtw] 211 | BEQ decret // beq .L1 212 | 213 | ADDW R7, R3, R3 // add w3, w3, w7 214 | ADDW $5, R10, R10 // add w10, w10, 5 215 | LSRW $2, R3, R0 // lsr w0, w3, 2 216 | UBFX $16, R3, $6, R3 // ubfx x3, x3, 16, 6 217 | ANDW $-64, R0, R0 // and w0, w0, -64 218 | ORRW R3, R0, R3 // orr w3, w0, w3 219 | MOVB R3, (R5)(R10.SXTW) // strb w3, [x5, w10, sxtw] 220 | decret: 221 | RET 222 | dectil: 223 | MOVW $0, R10 224 | MOVW $0, R12 225 | JMP decrem 226 | -------------------------------------------------------------------------------- /base14_generic.go: -------------------------------------------------------------------------------- 1 | package base14 2 | 3 | import "encoding/binary" 4 | 5 | //go:nosplit 6 | func encodeGeneric(offset, outlen int, b, encd []byte) { 7 | var n int 8 | i := 0 9 | if len(b)%7 == 0 { 10 | b = append(b, 0) 11 | } 12 | for ; i <= len(b)-7; i += 7 { 13 | shift := binary.BigEndian.Uint64(b[i:]) >> 2 14 | sum := shift 15 | sum &= 0x3fff000000000000 16 | shift >>= 2 17 | sum |= shift & 0x00003fff00000000 18 | shift >>= 2 19 | sum |= shift & 0x000000003fff0000 20 | shift >>= 2 21 | sum |= shift & 0x0000000000003fff 22 | sum += 0x4e004e004e004e00 23 | binary.BigEndian.PutUint64(encd[n:], sum) 24 | n += 8 25 | } 26 | if offset > 0 { 27 | sum := 0x000000000000003f & ((uint64)(b[i]) >> 2) 28 | sum |= ((uint64)(b[i]) << 14) & 0x000000000000c000 29 | if offset > 1 { 30 | sum |= ((uint64)(b[i+1]) << 6) & 0x0000000000003f00 31 | sum |= ((uint64)(b[i+1]) << 20) & 0x0000000000300000 32 | if offset > 2 { 33 | sum |= ((uint64)(b[i+2]) << 12) & 0x00000000000f0000 34 | sum |= ((uint64)(b[i+2]) << 28) & 0x00000000f0000000 35 | if offset > 3 { 36 | sum |= ((uint64)(b[i+3]) << 20) & 0x000000000f000000 37 | sum |= ((uint64)(b[i+3]) << 34) & 0x0000003c00000000 38 | if offset > 4 { 39 | sum |= ((uint64)(b[i+4]) << 26) & 0x0000000300000000 40 | sum |= ((uint64)(b[i+4]) << 42) & 0x0000fc0000000000 41 | if offset > 5 { 42 | sum |= ((uint64)(b[i+5]) << 34) & 0x0000030000000000 43 | sum |= ((uint64)(b[i+5]) << 48) & 0x003f000000000000 44 | } 45 | } 46 | } 47 | } 48 | } 49 | sum += 0x004e004e004e004e 50 | var tmp [8]byte 51 | binary.LittleEndian.PutUint64(tmp[:], sum) 52 | copy(encd[n:], tmp[:]) 53 | encd[outlen-2] = '=' 54 | encd[outlen-1] = byte(offset) 55 | } 56 | } 57 | 58 | //go:nosplit 59 | func decodeGeneric(offset, outlen int, b, decd []byte) { 60 | var n uintptr 61 | i := 0 62 | for ; i <= outlen-7; n += 8 { 63 | shift := binary.BigEndian.Uint64(b[n:]) - 0x4e004e004e004e00 64 | shift <<= 2 65 | sum := shift & 0xfffc000000000000 66 | shift <<= 2 67 | sum |= shift & 0x0003fff000000000 68 | shift <<= 2 69 | sum |= shift & 0x0000000fffc00000 70 | shift <<= 2 71 | sum |= shift & 0x00000000003fff00 72 | binary.BigEndian.PutUint64(decd[i:], sum) 73 | i += 7 74 | } 75 | if offset > 0 { 76 | var tmp [8]byte 77 | copy(tmp[:], b[n:]) 78 | sum := binary.LittleEndian.Uint64(tmp[:]) - 0x000000000000004e 79 | decd[i] = byte(((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14)) 80 | i++ 81 | if offset > 1 { 82 | sum -= 0x00000000004e0000 83 | decd[i] = byte(((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20)) 84 | i++ 85 | if offset > 2 { 86 | decd[i] = byte(((sum & 0x00000000000f0000) >> 12) | ((sum & 0x00000000f0000000) >> 28)) 87 | i++ 88 | if offset > 3 { 89 | sum -= 0x0000004e00000000 90 | decd[i] = byte(((sum & 0x000000000f000000) >> 20) | ((sum & 0x0000003c00000000) >> 34)) 91 | i++ 92 | if offset > 4 { 93 | decd[i] = byte(((sum & 0x0000000300000000) >> 26) | ((sum & 0x0000fc0000000000) >> 42)) 94 | i++ 95 | if offset > 5 { 96 | sum -= 0x004e000000000000 97 | decd[i] = byte(((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48)) 98 | i++ 99 | } 100 | } 101 | } 102 | } 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /base14_noasm.go: -------------------------------------------------------------------------------- 1 | //go:build !amd64 && !arm64 2 | // +build !amd64,!arm64 3 | 4 | package base14 5 | 6 | import _ "unsafe" 7 | 8 | //go:linkname encode github.com/fumiama/go-base16384.encodeGeneric 9 | func encode(offset, outlen int, b, encd []byte) 10 | 11 | //go:linkname decode github.com/fumiama/go-base16384.decodeGeneric 12 | func decode(offset, outlen int, b, decd []byte) 13 | -------------------------------------------------------------------------------- /base14_test.go: -------------------------------------------------------------------------------- 1 | package base14 2 | 3 | import ( 4 | "bytes" 5 | "encoding/hex" 6 | "io" 7 | "math/rand" 8 | "testing" 9 | 10 | "github.com/stretchr/testify/assert" 11 | ) 12 | 13 | func TestBase14(t *testing.T) { 14 | assert.Equal(t, "蜮嘎惢磦筢貊豔耹嫹桊涖犧蟦癎摖壥禦籋萷犸粹瘛榞梄螢圓因苧璡屨灇炀瞸瘊暍严帉戀㴃", EncodeString("一个测试293大大的啊定位为恶的我284的我……#@%@%@")) 15 | assert.Equal(t, "婀㴁", EncodeString("1")) 16 | assert.Equal(t, "婌渀㴂", EncodeString("12")) 17 | assert.Equal(t, "婌焰㴃", EncodeString("123")) 18 | assert.Equal(t, "婌焳帀㴄", EncodeString("1234")) 19 | assert.Equal(t, "婌焳廔㴅", EncodeString("12345")) 20 | assert.Equal(t, "婌焳廔萀㴆", EncodeString("123456")) 21 | assert.Equal(t, "婌焳廔萷", EncodeString("1234567")) 22 | assert.Equal(t, "婌焳廔萷尀㴁", EncodeString("12345678")) 23 | buf := make([]byte, 4096) 24 | for i := 1; i < 4096; i++ { 25 | rand.Read(buf[:i]) 26 | out := Decode(Encode(buf[:i])) 27 | if !assert.Equal(t, hex.EncodeToString(buf[:i]), hex.EncodeToString(out)) { 28 | t.Fatal() 29 | } 30 | } 31 | } 32 | 33 | func TestEncoder(t *testing.T) { 34 | buf := make([]byte, 1024*1024+1) 35 | _, err := rand.Read(buf) 36 | if err != nil { 37 | t.Fatal(err) 38 | } 39 | w := bytes.NewBuffer(make([]byte, 0, 1024*1024+1)) 40 | for i := 0; i <= 1024*1024; i += rand.Intn(128) * 7 { 41 | e := NewEncoder(bytes.NewReader(buf[:i])) 42 | _, err = io.Copy(w, e) 43 | if err != nil { 44 | t.Fatal(err) 45 | } 46 | if !bytes.Equal(Encode(buf[:i]), w.Bytes()) { 47 | t.Fail() 48 | } 49 | w.Reset() 50 | } 51 | } 52 | 53 | func TestBufferedEncoder(t *testing.T) { 54 | buf := make([]byte, 1024*1024+1) 55 | _, err := rand.Read(buf) 56 | if err != nil { 57 | t.Fatal(err) 58 | } 59 | w := bytes.NewBuffer(make([]byte, 0, 1024*1024+1)) 60 | for i := 0; i <= 1024*1024; i += rand.Intn(128) * 7 { 61 | e := NewBufferedEncoder(buf[:i]) 62 | _, err = io.Copy(w, e) 63 | if err != nil { 64 | t.Fatal(err) 65 | } 66 | if !bytes.Equal(Encode(buf[:i]), w.Bytes()) { 67 | t.Fail() 68 | } 69 | w.Reset() 70 | } 71 | } 72 | 73 | func TestDecoder(t *testing.T) { 74 | buf := make([]byte, 1024*1024+1) 75 | _, err := rand.Read(buf) 76 | if err != nil { 77 | t.Fatal(err) 78 | } 79 | encd := Encode(buf) 80 | w := bytes.NewBuffer(make([]byte, 0, 1024*1024+1)) 81 | for i := 0; i <= 1024*1024; i += rand.Intn(128) * 8 { 82 | d := NewDecoder(bytes.NewReader(encd[:i])) 83 | _, err = io.Copy(w, d) 84 | if err != nil { 85 | t.Fatal(err) 86 | } 87 | if !bytes.Equal(buf[:i/8*7], w.Bytes()) { 88 | t.Fail() 89 | } 90 | w.Reset() 91 | } 92 | } 93 | 94 | func TestBufferedDecoder(t *testing.T) { 95 | buf := make([]byte, 1024*1024+1) 96 | _, err := rand.Read(buf) 97 | if err != nil { 98 | t.Fatal(err) 99 | } 100 | encd := Encode(buf) 101 | w := bytes.NewBuffer(make([]byte, 0, 1024*1024+1)) 102 | for i := 0; i <= 1024*1024; i += rand.Intn(128) * 8 { 103 | d := NewBufferedDecoder(encd[:i]) 104 | _, err = io.Copy(w, d) 105 | if err != nil { 106 | t.Fatal(err) 107 | } 108 | if !bytes.Equal(buf[:i/8*7], w.Bytes()) { 109 | t.Fail() 110 | } 111 | w.Reset() 112 | } 113 | } 114 | 115 | func benchEncode(b *testing.B, data []byte) { 116 | _, err := rand.Read(data) 117 | if err != nil { 118 | panic(err) 119 | } 120 | buf := make([]byte, EncodeLen(len(data))) 121 | b.SetBytes(int64(len(data))) 122 | b.ResetTimer() 123 | for i := 0; i < b.N; i++ { 124 | _, _ = EncodeTo(data, buf) 125 | } 126 | } 127 | 128 | func benchDecode(b *testing.B, data []byte) { 129 | _, err := rand.Read(data) 130 | if err != nil { 131 | panic(err) 132 | } 133 | buf := make([]byte, EncodeLen(len(data))) 134 | _, err = EncodeTo(data, buf) 135 | if err != nil { 136 | panic(err) 137 | } 138 | b.SetBytes(int64(len(buf))) 139 | b.ResetTimer() 140 | for i := 0; i < b.N; i++ { 141 | _, _ = DecodeTo(buf, data) 142 | } 143 | } 144 | 145 | func BenchmarkEncodeTo(b *testing.B) { 146 | b.Run("16", func(b *testing.B) { 147 | data := make([]byte, 16) 148 | benchEncode(b, data) 149 | }) 150 | b.Run("256", func(b *testing.B) { 151 | data := make([]byte, 256) 152 | benchEncode(b, data) 153 | }) 154 | b.Run("4K", func(b *testing.B) { 155 | data := make([]byte, 1024*4) 156 | benchEncode(b, data) 157 | }) 158 | b.Run("32K", func(b *testing.B) { 159 | data := make([]byte, 1024*32) 160 | benchEncode(b, data) 161 | }) 162 | } 163 | 164 | func BenchmarkDecodeTo(b *testing.B) { 165 | b.Run("16", func(b *testing.B) { 166 | data := make([]byte, 16) 167 | benchDecode(b, data) 168 | }) 169 | b.Run("256", func(b *testing.B) { 170 | data := make([]byte, 256) 171 | benchDecode(b, data) 172 | }) 173 | b.Run("4K", func(b *testing.B) { 174 | data := make([]byte, 4096) 175 | benchDecode(b, data) 176 | }) 177 | b.Run("32K", func(b *testing.B) { 178 | data := make([]byte, 1024*32) 179 | benchDecode(b, data) 180 | }) 181 | } 182 | 183 | func benchEncoder(b *testing.B, cnt int64) { 184 | enc := NewEncoder(rand.New(rand.NewSource(0))) 185 | buf := bytes.NewBuffer(make([]byte, 0, cnt)) 186 | b.SetBytes(cnt) 187 | b.ResetTimer() 188 | for i := 0; i < b.N; i++ { 189 | _, _ = io.CopyN(buf, enc, cnt) 190 | buf.Reset() 191 | } 192 | } 193 | 194 | func benchDecoder(b *testing.B, cnt int64) { 195 | enc := NewEncoder(rand.New(rand.NewSource(0))) 196 | buf := bytes.NewBuffer(make([]byte, 0, cnt)) 197 | _, err := io.CopyN(buf, enc, cnt) 198 | if err != nil { 199 | panic(err) 200 | } 201 | buf2 := bytes.NewBuffer(make([]byte, 0, cnt)) 202 | b.SetBytes(cnt) 203 | b.ResetTimer() 204 | for i := 0; i < b.N; i++ { 205 | _, _ = io.Copy(buf2, NewDecoder(bytes.NewReader(buf.Bytes()))) 206 | buf2.Reset() 207 | } 208 | } 209 | 210 | func BenchmarkEncoder(b *testing.B) { 211 | b.Run("16", func(b *testing.B) { 212 | benchEncoder(b, 16) 213 | }) 214 | b.Run("256", func(b *testing.B) { 215 | benchEncoder(b, 256) 216 | }) 217 | b.Run("4K", func(b *testing.B) { 218 | benchEncoder(b, 1024*4) 219 | }) 220 | b.Run("32K", func(b *testing.B) { 221 | benchEncoder(b, 1024*32) 222 | }) 223 | } 224 | 225 | func BenchmarkDecoder(b *testing.B) { 226 | b.Run("16", func(b *testing.B) { 227 | benchDecoder(b, 16) 228 | }) 229 | b.Run("256", func(b *testing.B) { 230 | benchDecoder(b, 256) 231 | }) 232 | b.Run("4K", func(b *testing.B) { 233 | benchDecoder(b, 1024*4) 234 | }) 235 | b.Run("32K", func(b *testing.B) { 236 | benchDecoder(b, 1024*32) 237 | }) 238 | } 239 | -------------------------------------------------------------------------------- /c/base1432.c: -------------------------------------------------------------------------------- 1 | #ifdef __cosmopolitan // always le 2 | # define be16toh(x) bswap_16(x) 3 | # define be32toh(x) bswap_32(x) 4 | # define htobe16(x) bswap_16(x) 5 | # define htobe32(x) bswap_32(x) 6 | #else 7 | #include 8 | #include 9 | #include 10 | #ifdef __linux__ 11 | # include 12 | #endif 13 | #ifdef __FreeBSD__ 14 | # include 15 | #endif 16 | #ifdef __NetBSD__ 17 | # include 18 | #endif 19 | #ifdef __OpenBSD__ 20 | # include 21 | # define be16toh(x) betoh16(x) 22 | # define be32toh(x) betoh32(x) 23 | #endif 24 | #ifdef __MAC_10_0 25 | # define be16toh(x) ntohs(x) 26 | # define be32toh(x) ntohl(x) 27 | # define htobe16(x) ntohs(x) 28 | # define htobe32(x) htonl(x) 29 | #endif 30 | #ifdef _WIN32 31 | #ifdef WORDS_BIGENDIAN 32 | # define be16toh(x) (x) 33 | # define be32toh(x) (x) 34 | # define htobe16(x) (x) 35 | # define htobe32(x) (x) 36 | #else 37 | # define be16toh(x) _byteswap_ushort(x) 38 | # define be32toh(x) _byteswap_ulong(x) 39 | # define htobe16(x) _byteswap_ushort(x) 40 | # define htobe32(x) _byteswap_ulong(x) 41 | #endif 42 | #endif 43 | #endif 44 | 45 | int base16384_encode(int offset, int outlen, const char* data, int dlen, int dcap, char* buf, int blen, int bcap) { 46 | uint32_t* vals = (uint32_t*)buf; 47 | uint32_t n = 0; 48 | int32_t i = 0; 49 | for(; i <= dlen - 7; i += 7) { 50 | register uint32_t sum = 0; 51 | register uint32_t shift = htobe32(*(uint32_t*)(data+i)); 52 | sum |= (shift>>2) & 0x3fff0000; 53 | sum |= (shift>>4) & 0x00003fff; 54 | sum += 0x4e004e00; 55 | vals[n++] = be32toh(sum); 56 | shift <<= 26; 57 | shift &= 0x3c000000; 58 | sum = 0; 59 | shift |= (htobe32(*(uint32_t*)(data+i+4))>>6)&0x03fffffc; 60 | sum |= shift & 0x3fff0000; 61 | shift >>= 2; 62 | sum |= shift & 0x00003fff; 63 | sum += 0x4e004e00; 64 | vals[n++] = be32toh(sum); 65 | } 66 | uint8_t o = offset; 67 | if(o--) { 68 | register uint32_t sum = 0x0000003f & (data[i] >> 2); 69 | sum |= ((uint32_t)data[i] << 14) & 0x0000c000; 70 | if(o--) { 71 | sum |= ((uint32_t)data[i + 1] << 6) & 0x00003f00; 72 | sum |= ((uint32_t)data[i + 1] << 20) & 0x00300000; 73 | if(o--) { 74 | sum |= ((uint32_t)data[i + 2] << 12) & 0x000f0000; 75 | sum |= ((uint32_t)data[i + 2] << 28) & 0xf0000000; 76 | if(o--) { 77 | sum |= ((uint32_t)data[i + 3] << 20) & 0x0f000000; 78 | sum += 0x004e004e; 79 | #ifdef WORDS_BIGENDIAN 80 | vals[n++] = __builtin_bswap32(sum); 81 | #else 82 | vals[n++] = sum; 83 | #endif 84 | sum = (((uint32_t)data[i + 3] << 2)) & 0x0000003c; 85 | if(o--) { 86 | sum |= (((uint32_t)data[i + 4] >> 6)) & 0x00000003; 87 | sum |= ((uint32_t)data[i + 4] << 10) & 0x0000fc00; 88 | if(o--) { 89 | sum |= ((uint32_t)data[i + 5] << 2) & 0x00000300; 90 | sum |= ((uint32_t)data[i + 5] << 16) & 0x003f0000; 91 | } 92 | } 93 | } 94 | } 95 | } 96 | sum += 0x004e004e; 97 | #ifdef WORDS_BIGENDIAN 98 | vals[n] = __builtin_bswap32(sum); 99 | #else 100 | vals[n] = sum; 101 | #endif 102 | buf[outlen - 2] = '='; 103 | buf[outlen - 1] = offset; 104 | } 105 | return outlen; 106 | } 107 | 108 | void base16384_decode(int offset, int outlen, const char* data, int dlen, int dcap, char* buf, int blen, int bcap) { 109 | uint32_t* vals = (uint32_t*)data; 110 | uint32_t n = 0; 111 | int32_t i = 0; 112 | for(; i <= outlen - 7; i+=7) { // n实际每次自增2 113 | register uint32_t sum = 0; 114 | register uint32_t shift = htobe32(vals[n++]) - 0x4e004e00; 115 | shift <<= 2; 116 | sum |= shift & 0xfffc0000; 117 | shift <<= 2; 118 | sum |= shift & 0x0003fff0; 119 | shift = htobe32(vals[n++]) - 0x4e004e00; 120 | sum |= shift >> 26; 121 | *(uint32_t*)(buf+i) = be32toh(sum); 122 | sum = 0; 123 | shift <<= 6; 124 | sum |= shift & 0xffc00000; 125 | shift <<= 2; 126 | sum |= shift & 0x003fff00; 127 | *(uint32_t*)(buf+i+4) = be32toh(sum); 128 | } 129 | if(offset--) { 130 | // 这里有读取越界 131 | #ifdef WORDS_BIGENDIAN 132 | register uint32_t sum = __builtin_bswap32(vals[n++]); 133 | #else 134 | register uint32_t sum = vals[n++]; 135 | #endif 136 | sum -= 0x0000004e; 137 | buf[i++] = ((sum & 0x0000003f) << 2) | ((sum & 0x0000c000) >> 14); 138 | if(offset--) { 139 | sum -= 0x004e0000; 140 | buf[i++] = ((sum & 0x00003f00) >> 6) | ((sum & 0x00300000) >> 20); 141 | if(offset--) { 142 | buf[i++] = ((sum & 0x000f0000) >> 12) | ((sum & 0xf0000000) >> 28); 143 | if(offset--) { 144 | buf[i] = (sum & 0x0f000000) >> 20; 145 | // 这里有读取越界 146 | sum = vals[n]; 147 | sum -= 0x0000004e; 148 | buf[i++] |= (sum & 0x0000003c) >> 2; 149 | if(offset--) { 150 | buf[i++] = ((sum & 0x00000003) << 6) | ((sum & 0x0000fc00) >> 10); 151 | if(offset--) { 152 | sum -= 0x004e0000; 153 | buf[i] = ((sum & 0x00000300) >> 2) | ((sum & 0x003f0000) >> 16); 154 | } 155 | } 156 | } 157 | } 158 | } 159 | } 160 | return; 161 | } 162 | -------------------------------------------------------------------------------- /conv.go: -------------------------------------------------------------------------------- 1 | package base14 2 | 3 | import ( 4 | "golang.org/x/text/encoding/unicode" 5 | ) 6 | 7 | var format = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM) 8 | 9 | // UTF16BE2UTF8 to display the result as string 10 | func UTF16BE2UTF8(b []byte) ([]byte, error) { 11 | return format.NewDecoder().Bytes(b) 12 | } 13 | 14 | // UTF82UTF16BE to decode from string 15 | func UTF82UTF16BE(b []byte) ([]byte, error) { 16 | return format.NewEncoder().Bytes(b) 17 | } 18 | 19 | func EncodeToString(b []byte) string { 20 | out, err := UTF16BE2UTF8(Encode(b)) 21 | if err != nil { 22 | return "" 23 | } 24 | return BytesToString(out) 25 | } 26 | 27 | func EncodeFromString(s string) []byte { 28 | return Encode(StringToBytes(s)) 29 | } 30 | 31 | func EncodeString(s string) string { 32 | out, err := UTF16BE2UTF8(Encode(StringToBytes(s))) 33 | if err != nil { 34 | return "" 35 | } 36 | return BytesToString(out) 37 | } 38 | 39 | func DecodeToString(d []byte) string { 40 | return BytesToString(Decode(d)) 41 | } 42 | 43 | func DecodeFromString(s string) []byte { 44 | d, err := UTF82UTF16BE(StringToBytes(s)) 45 | if err != nil { 46 | return nil 47 | } 48 | return Decode(d) 49 | } 50 | 51 | func DecodeString(s string) string { 52 | d, err := UTF82UTF16BE(StringToBytes(s)) 53 | if err != nil { 54 | return "" 55 | } 56 | return BytesToString(Decode(d)) 57 | } 58 | -------------------------------------------------------------------------------- /cpuid.go: -------------------------------------------------------------------------------- 1 | //go:build amd64 2 | // +build amd64 3 | 4 | package base14 5 | 6 | func cpuid(op uint32) (eax, ebx, ecx, edx uint32) 7 | 8 | // True when MOVBEx instructions are available. 9 | var movbe = func() bool { 10 | _, _, c, _ := cpuid(1) 11 | return c&(1<<22) > 0 12 | }() 13 | -------------------------------------------------------------------------------- /cpuid_amd64.s: -------------------------------------------------------------------------------- 1 | //go:build amd64 2 | // +build amd64 3 | 4 | // Copyright (c) 2015 Klaus Post, released under MIT License. See LICENSE file. 5 | 6 | // func cpuid(op uint32) (eax, ebx, ecx, edx uint32) 7 | TEXT ·cpuid(SB), 7, $0 8 | XORQ CX, CX 9 | MOVL op+0(FP), AX 10 | CPUID 11 | MOVL AX, eax+8(FP) 12 | MOVL BX, ebx+12(FP) 13 | MOVL CX, ecx+16(FP) 14 | MOVL DX, edx+20(FP) 15 | RET 16 | -------------------------------------------------------------------------------- /decoder.go: -------------------------------------------------------------------------------- 1 | package base14 2 | 3 | import ( 4 | "io" 5 | ) 6 | 7 | type Decoder struct { 8 | b []byte 9 | r io.Reader 10 | io.Reader 11 | } 12 | 13 | func NewDecoder(r io.Reader) *Decoder { 14 | return &Decoder{r: r} 15 | } 16 | 17 | func NewBufferedDecoder(b []byte) *Decoder { 18 | return &Decoder{b: b} 19 | } 20 | 21 | func (d *Decoder) Read(p []byte) (n int, err error) { 22 | i := len(d.b) 23 | if i == 0 && d.r == nil { 24 | err = io.EOF 25 | return 26 | } 27 | inlen := len(p) / 7 * 8 28 | if d.r != nil { 29 | d.b = append(d.b, make([]byte, inlen)...) 30 | n, err = d.r.Read(d.b[i:]) 31 | if n <= 0 { 32 | return 33 | } 34 | inlen = i + n 35 | if err != nil { 36 | if inlen > 0 { 37 | n, _ = DecodeTo(d.b[:inlen], p) 38 | d.b = nil 39 | d.r = nil 40 | } 41 | return 42 | } 43 | if inlen%2 != 0 { 44 | d.b = d.b[:inlen] 45 | n = 0 46 | return 47 | } 48 | offset := 0 49 | if d.b[inlen-2] == '=' { 50 | offset = int(d.b[inlen-1]) 51 | } 52 | if offset > 0 { 53 | n, _ = DecodeTo(d.b[:inlen], p) 54 | d.b = nil 55 | d.r = nil 56 | } else { 57 | n, _ = DecodeTo(d.b[:inlen], p) 58 | d.b = d.b[:0] 59 | } 60 | return 61 | } else if inlen > len(d.b) { 62 | inlen = len(d.b) 63 | } 64 | if inlen <= 2 { 65 | err = io.EOF 66 | return 67 | } 68 | if len(d.b[inlen:]) == 2 { 69 | inlen += 2 70 | } 71 | n, _ = DecodeTo(d.b[:inlen], p) 72 | d.b = d.b[inlen:] 73 | return 74 | } 75 | -------------------------------------------------------------------------------- /encoder.go: -------------------------------------------------------------------------------- 1 | package base14 2 | 3 | import ( 4 | "io" 5 | ) 6 | 7 | type Encoder struct { 8 | b []byte 9 | r io.Reader 10 | io.Reader 11 | } 12 | 13 | func NewEncoder(r io.Reader) *Encoder { 14 | return &Encoder{r: r} 15 | } 16 | 17 | func NewBufferedEncoder(b []byte) *Encoder { 18 | return &Encoder{b: b} 19 | } 20 | 21 | func (e *Encoder) Read(p []byte) (n int, err error) { 22 | i := len(e.b) 23 | if i == 0 && e.r == nil { 24 | err = io.EOF 25 | return 26 | } 27 | inlen := len(p) / 8 * 7 28 | if e.r != nil { 29 | e.b = append(e.b, make([]byte, inlen)...) 30 | n, err = e.r.Read(e.b[i:]) 31 | inlen = i + n 32 | if err != nil { 33 | if len(e.b) > 0 { 34 | n, _ = EncodeTo(e.b[:inlen], p) 35 | } 36 | e.b = nil 37 | e.r = nil 38 | return 39 | } 40 | n, err = EncodeTo(e.b[:inlen], p) 41 | e.b = e.b[:0] 42 | return 43 | } else if inlen > len(e.b) { 44 | inlen = len(e.b) 45 | } 46 | n, err = EncodeTo(e.b[:inlen], p) 47 | e.b = e.b[inlen:] 48 | return 49 | } 50 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/fumiama/go-base16384 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/stretchr/testify v1.7.1 7 | golang.org/x/text v0.3.7 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 6 | github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= 7 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 8 | golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= 9 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 10 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 11 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 12 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 13 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 14 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 15 | -------------------------------------------------------------------------------- /helper.go: -------------------------------------------------------------------------------- 1 | package base14 2 | 3 | import ( 4 | "unsafe" 5 | ) 6 | 7 | // BytesToString 没有内存开销的转换 8 | func BytesToString(b []byte) string { 9 | return *(*string)(unsafe.Pointer(&b)) 10 | } 11 | 12 | // StringToBytes 没有内存开销的转换 13 | func StringToBytes(s string) (b []byte) { 14 | bh := (*slice)(unsafe.Pointer(&b)) 15 | sh := (*slice)(unsafe.Pointer(&s)) 16 | bh.data = sh.data 17 | bh.len = sh.len 18 | bh.cap = sh.len 19 | return b 20 | } 21 | -------------------------------------------------------------------------------- /slice.go: -------------------------------------------------------------------------------- 1 | package base14 2 | 3 | import "unsafe" 4 | 5 | // slice is the runtime representation of a slice. 6 | // It cannot be used safely or portably and its representation may 7 | // change in a later release. 8 | // 9 | // Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the 10 | // data it references will not be garbage collected. 11 | type slice struct { 12 | data unsafe.Pointer 13 | len int 14 | cap int 15 | } 16 | --------------------------------------------------------------------------------