gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r18231 - gnunet-java/src/org/gnunet/construct


From: gnunet
Subject: [GNUnet-SVN] r18231 - gnunet-java/src/org/gnunet/construct
Date: Mon, 21 Nov 2011 11:20:42 +0100

Author: dold
Date: 2011-11-21 11:20:42 +0100 (Mon, 21 Nov 2011)
New Revision: 18231

Modified:
   gnunet-java/src/org/gnunet/construct/Construct.java
   gnunet-java/src/org/gnunet/construct/UnsignedIntegerParser.java
Log:
fix

Modified: gnunet-java/src/org/gnunet/construct/Construct.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/Construct.java 2011-11-21 10:07:58 UTC 
(rev 18230)
+++ gnunet-java/src/org/gnunet/construct/Construct.java 2011-11-21 10:20:42 UTC 
(rev 18231)
@@ -25,19 +25,22 @@
 public class Construct {
     private static final Logger logger = LoggerFactory
             .getLogger(Construct.class);
-    
-    
-    
+
+    /**
+     * Thrown if a parser requires more space in the buffer.
+     * 
+     * @author Florian Dold
+     * 
+     */
     static class SizeError extends RuntimeException {
-        
+
     }
-    
 
-    
     /**
+     * Embed another constructable message.
      * 
      * @author Florian Dold
-     *
+     * 
      */
     @Retention(RetentionPolicy.RUNTIME)
     @Target(ElementType.FIELD)
@@ -60,6 +63,12 @@
         int value() default 4;
     }
 
+    /**
+     * Stores and retrieves the size of the enclosing message.
+     * 
+     * @author Florian Dold
+     * 
+     */
     @Retention(RetentionPolicy.RUNTIME)
     @Target(ElementType.FIELD)
     public @interface TotalSize {
@@ -72,6 +81,12 @@
         int length();
     }
 
+    /**
+     * Acceptable targets: byte, short, int, long, BigInteger.
+     * 
+     * @author Florian Dold
+     * 
+     */
     @Retention(RetentionPolicy.RUNTIME)
     @Target(ElementType.FIELD)
     public @interface UnsignedInteger {
@@ -80,7 +95,7 @@
         // if useSignBit is true, the sign bit may be used to store a bit
         // of an unsigned number (this makes the number negative), if 
useSignBit
         // is false
-        // an error is signaled if the sign of the target field is on
+        // an error is signaled if the sign of the target field is set
         boolean useSignBit() default true;
     }
 
@@ -92,6 +107,12 @@
         String lengthField();
     }
 
+    /**
+     * Parse and unparse a zero-terminated string with the specified encoding.
+     * 
+     * @author Florian Dold
+     * 
+     */
     @Retention(RetentionPolicy.RUNTIME)
     public @interface ZeroTerminatedString {
 
@@ -99,7 +120,7 @@
     }
 
     /**
-     * Fill a byte array with the rest of the message in the buffer
+     * Fills a byte array with the rest of the message in the buffer.
      * 
      * @author Florian Dold
      * 
@@ -134,6 +155,19 @@
                 Parser child = getFieldParser(f.getType(), anns, 1);
                 tsp.addSizeField(child, f);
                 op.add(child, f);
+            } else if (anns[0] instanceof VariableSizeArray) {
+                VariableSizeArray ann = (VariableSizeArray) anns[0];
+                Parser child = getFieldParser(f.getType().getComponentType(), 
anns, 1);
+                Parser p = new VariableSizeArrayParser(child);
+                
+                try {
+                    op.add(p, f, cls.getDeclaredField(ann.lengthField()));
+                } catch (SecurityException e) {
+                   throw new RuntimeException();
+                } catch (NoSuchFieldException e) {
+                    throw new RuntimeException();
+                }
+                
             } else {
                 Parser p = getFieldParser(f.getType(), anns, 0);
                 op.add(p, f);

Modified: gnunet-java/src/org/gnunet/construct/UnsignedIntegerParser.java
===================================================================
--- gnunet-java/src/org/gnunet/construct/UnsignedIntegerParser.java     
2011-11-21 10:07:58 UTC (rev 18230)
+++ gnunet-java/src/org/gnunet/construct/UnsignedIntegerParser.java     
2011-11-21 10:20:42 UTC (rev 18231)
@@ -12,15 +12,20 @@
 
     private final int byte_size;
 
+    private boolean allow_sign = true;
+
     public UnsignedIntegerParser(int byte_size) {
         this.byte_size = byte_size;
     }
 
+    public UnsignedIntegerParser(int byte_size, boolean allow_sign) {
+        this.byte_size = byte_size;
+        this.allow_sign = allow_sign;
+    }
 
     public int parse(byte[] data, int offset, Location[] dest) {
         assert dest.length == 1;
-        
-        
+
         if (offset + byte_size > data.length) {
             throw new Construct.SizeError();
         }
@@ -54,12 +59,19 @@
                 bit_size = 8;
             } else {
                 throw new RuntimeException(
-                        "construct: target type not supported: " + 
loc.getType());
+                        "construct: target type not supported: "
+                                + loc.getType());
             }
 
-            // +1 is because java has only signed numbers
-            if (num.bitLength() + 1 > bit_size) {
-                logger.warn("construct: number truncated");
+            if (allow_sign) {
+                if (num.bitLength() > bit_size) {
+                    logger.warn("construct: number truncated");
+                }
+            } else {
+                // +1 is because java has only signed numbers
+                if (num.bitLength() + 1 > bit_size) {
+                    logger.warn("construct: number truncated");
+                }
             }
         }
         return byte_size;
@@ -70,9 +82,9 @@
         assert src.length == 1;
         Object obj = src[0].get();
         assert obj != null;
-        
+
         BigInteger num;
-        
+
         if (obj instanceof BigInteger) {
             num = (BigInteger) obj;
         } else if (obj instanceof Number) {
@@ -80,30 +92,27 @@
         } else {
             throw new RuntimeException("invalid number type");
         }
-        
+
         if (num.signum() == -1) {
-            throw new RuntimeException("negative value not allowed for 
unsigned integer");
+            throw new RuntimeException(
+                    "negative value not allowed for unsigned integer");
         }
-        
-        if (num.bitLength() > byte_size*8) {
+
+        if (num.bitLength() > byte_size * 8) {
             throw new RuntimeException("value to large for serialization");
         }
-        
+
         byte[] buf = num.toByteArray();
-        
-        
+
         int dst_pos = offset + (byte_size - buf.length);
-        
+
         System.arraycopy(buf, 0, dst_data, dst_pos, buf.length);
-        
+
         Arrays.fill(dst_data, offset, dst_pos, (byte) 0);
-        
+
         return byte_size;
     }
-    
-    
 
-
     @Override
     public int estimateSize(Location[] args) {
         return byte_size;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]