Class: CoBreak::Cipher::Base64

Inherits:
Object
  • Object
show all
Defined in:
ext/cobreak/cobreak_cipher.c

Class Method Summary collapse

Class Method Details

.decode(full) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'ext/cobreak/cobreak_cipher.c', line 149

VALUE b64_decode(VALUE self, VALUE full){
//	check_Type(full, T_STRING);
	int c, phase, i;
	unsigned char in[4];
	char *p;

	char *myb64 = RSTRING_PTR(full);
	char strb64[1024] = "";
	char *clrdst = strb64;
	clrdst[0] = '\0';
	phase = 0; i=0;
	while(myb64[i]){
		c = (int) myb64[i];
		if(c == '='){
			decodeblock64(in, clrdst);
			break;
		}
		p = strchr(b64, c);
		if(p){
			in[phase] = p - b64;
			phase = (phase + 1) % 4;
			if(phase == 0){
				decodeblock64(in, clrdst);
				in[0]=in[1]=in[2]=in[3]=0;
			}
		}
		i++;
	}
	return rb_str_new2(strb64);
}

.encode(full) ⇒ Object

Define method for class Base64



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'ext/cobreak/cobreak_cipher.c', line 190

VALUE b64_encode(VALUE self, VALUE full){
//	check_Type(full, T_STRING);
	unsigned char in[3];
	int i, len = 0;
	int j = 0;
	//char b64dst[1024];
	char *strb64 = RSTRING_PTR(full);
	char myb64[1024] = "";
	char *b64dst = myb64;
	b64dst[0] = '\0';
	while(strb64[j]){
		len = 0;
		for(i=0; i<3; i++){
			in[i] = (unsigned char) strb64[j];
			if(strb64[j]) {
				len++; j++;
			}
			else in[i] = 0;
		}
		if(len){
			encodeblock64(in, b64dst, len);
		}
	}
	return rb_str_new2(b64dst);
}