Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Get serialized blob from Base64; stub reader.
  • Loading branch information
jnthn committed Jan 18, 2013
1 parent e06e1f2 commit 3c67db0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/org/perl6/nqp/runtime/Base64.java
@@ -0,0 +1,51 @@
package org.perl6.nqp.runtime;

import java.nio.ByteBuffer;

public class Base64 {
private static int POS(char c)
{
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
if (c == '=') return -1;
return -2;
}

public static ByteBuffer decode(String s)
{
if (s.length() % 4 != 0)
throw new RuntimeException("Invalid Base64 input");

byte[] data = new byte[s.length() / 4 * 3];
int n[] = new int[4];
int p = 0, q = 0;

while (p < s.length()) {
n[0] = POS(s.charAt(p++));
n[1] = POS(s.charAt(p++));
n[2] = POS(s.charAt(p++));
n[3] = POS(s.charAt(p++));

if (n[0] == -2 || n[1] == -2 || n[2] == -2 || n[3] == -2)
throw new RuntimeException("Invalid Base64 input");

if (n[0] == -1 || n[1] == -1)
throw new RuntimeException("Invalid Base64 input");

if (n[2] == -1 && n[3] != -1)
throw new RuntimeException("Invalid Base64 input");

data[q] = (byte)((n[0] << 2) + (n[1] >> 4));
if (n[2] != -1)
data[q + 1] = (byte)(((n[1] & 15) << 4) + (n[2] >> 2));
if (n[3] != -1)
data[q + 2] = (byte)(((n[2] & 3) << 6) + n[3]);
q += 3;
}

return ByteBuffer.wrap(data, 0, q - (n[2] == -1 ? 1 : 0) - (n[3]==-1 ? 1 : 0));
}
}
3 changes: 2 additions & 1 deletion src/org/perl6/nqp/runtime/CompilationUnit.java
Expand Up @@ -79,7 +79,8 @@ public void initializeCompilationUnit(ThreadContext tc) {
}
catch (Exception e)
{
throw new RuntimeException("Deserialization failed: " + e.getMessage());
e.printStackTrace(System.err);
throw new RuntimeException(e);
}
}

Expand Down
24 changes: 23 additions & 1 deletion src/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -1145,7 +1145,29 @@ public static String serialize(SixModelObject scRef, SixModelObject sh, ThreadCo
throw new RuntimeException("Serialization NYI");
}
public static String deserialize(String blob, SixModelObject scRef, SixModelObject sh, SixModelObject cr, SixModelObject conflict, ThreadContext tc) {
throw new RuntimeException("Deserialization NYI");
if (scRef instanceof SCRefInstance) {
SerializationContext sc = ((SCRefInstance)scRef).referencedSC;

String[] shArray = new String[(int)sh.elems(tc)];
for (int i = 0; i < shArray.length; i++) {
SixModelObject strObj = sh.at_pos_boxed(tc, i);
shArray[i] = strObj == null ? null : strObj.get_str(tc);
}

CodeRef[] crArray = new CodeRef[(int)cr.elems(tc)];
for (int i = 0; i < crArray.length; i++)
crArray[i] = (CodeRef)cr.at_pos_boxed(tc, i);

SerializationReader sr = new SerializationReader(
tc, sc, shArray, crArray,
Base64.decode(blob));
sr.deserialize();

return blob;
}
else {
throw new RuntimeException("deserialize was not passed a valid SCRef");
}
}
public static SixModelObject wval(String sc, long idx, ThreadContext tc) {
return tc.gc.scs.get(sc).root_objects.get((int)idx);
Expand Down
24 changes: 23 additions & 1 deletion src/org/perl6/nqp/sixmodel/SerializationReader.java
@@ -1,5 +1,27 @@
package org.perl6.nqp.sixmodel;

public class SerializationReader {
import java.nio.ByteBuffer;

import org.perl6.nqp.runtime.CodeRef;
import org.perl6.nqp.runtime.ThreadContext;

public class SerializationReader {
private ThreadContext tc;
private SerializationContext sc;
private String[] sh;
private CodeRef[] cr;
private ByteBuffer orig;

public SerializationReader(ThreadContext tc, SerializationContext sc,
String[] sh, CodeRef[] cr, ByteBuffer orig) {
this.tc = tc;
this.sc = sc;
this.sh = sh;
this.cr = cr;
this.orig = orig;
}

public void deserialize() {
throw new RuntimeException("Deserialization NYI");
}
}

0 comments on commit 3c67db0

Please sign in to comment.