Add hashcat command lines
[pwdhash.git] / cencode.c
1 /**
2 * @file
3 * @author devolve <http://sourceforge.net/projects/libb64>
4 * @version 1.0
5 *
6 * @section LICENSE
7 *
8 * Public domain
9 *
10 * @section DESCRIPTION
11 *
12 * This is part of the libb64 project, and has been placed in the public domain.
13 * For details, see http://sourceforge.net/projects/libb64
14 *
15 * The cencode source provides support for base64 encoding data. It can be
16 * used in conjunction with the cdecode source. It's used by base64, which
17 * provides a higher-level interface to the functionality.
18 *
19 */
20
21 #include "pico/debug.h"
22 #include "pico/log.h"
23 #include "pico/cencode.h"
24
25 const int CHARS_PER_LINE = 72;
26
27 /**
28 * Internal function for encoding base64 strings
29 */
30 void base64_init_encodestate(base64_encodestate* state_in)
31 {
32 state_in->step = step_A;
33 state_in->result = 0;
34 state_in->stepcount = 0;
35 }
36
37 /**
38 * Internal function for encoding base64 strings
39 */
40 char base64_encode_value(char value_in)
41 {
42 static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43 if (value_in > 63) return '=';
44 return encoding[(int)value_in];
45 }
46
47 /**
48 * Internal function for encoding base64 strings
49 */
50 int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in)
51 {
52 const char* plainchar = plaintext_in;
53 const char* const plaintextend = plaintext_in + length_in;
54 char* codechar = code_out;
55 char result;
56 char fragment;
57
58 result = state_in->result;
59
60 switch (state_in->step)
61 {
62 while (1)
63 {
64 case step_A:
65 if (plainchar == plaintextend)
66 {
67 state_in->result = result;
68 state_in->step = step_A;
69 return codechar - code_out;
70 }
71 fragment = *plainchar++;
72 result = (fragment & 0x0fc) >> 2;
73 *codechar++ = base64_encode_value(result);
74 result = (fragment & 0x003) << 4;
75 case step_B:
76 if (plainchar == plaintextend)
77 {
78 state_in->result = result;
79 state_in->step = step_B;
80 return codechar - code_out;
81 }
82 fragment = *plainchar++;
83 result |= (fragment & 0x0f0) >> 4;
84 *codechar++ = base64_encode_value(result);
85 result = (fragment & 0x00f) << 2;
86 case step_C:
87 if (plainchar == plaintextend)
88 {
89 state_in->result = result;
90 state_in->step = step_C;
91 return codechar - code_out;
92 }
93 fragment = *plainchar++;
94 result |= (fragment & 0x0c0) >> 6;
95 *codechar++ = base64_encode_value(result);
96 result = (fragment & 0x03f) >> 0;
97 *codechar++ = base64_encode_value(result);
98
99 ++(state_in->stepcount);
100 if (state_in->stepcount == CHARS_PER_LINE/4)
101 {
102 //*codechar++ = '\n';
103 state_in->stepcount = 0;
104 }
105 }
106 }
107 /* control should not reach here */
108 return codechar - code_out;
109 }
110
111 /**
112 * Internal function for encoding base64 strings
113 */
114 int base64_encode_blockend(char* code_out, base64_encodestate* state_in)
115 {
116 char* codechar = code_out;
117
118 switch (state_in->step)
119 {
120 case step_B:
121 *codechar++ = base64_encode_value(state_in->result);
122 *codechar++ = '=';
123 *codechar++ = '=';
124 break;
125 case step_C:
126 *codechar++ = base64_encode_value(state_in->result);
127 *codechar++ = '=';
128 break;
129 case step_A:
130 break;
131 }
132 //*codechar++ = '\n';
133
134 return codechar - code_out;
135 }
136