Esempio n. 1
0
 public boolean visit(Scalar s) throws Exception {
   Map<String, String> parameters = createInitialParameters(s);
   parameters.put("type", s.getType());
   parameters.put("value", s.getValue());
   xmlWriter.startTag("Scalar", parameters);
   return true;
 }
  /**
   * @param scalar
   * @return true if the scalar is defines as $GLOBALS call
   */
  private static boolean checkGLOBALS(Scalar scalar) {
    final String stringValue = scalar.getStringValue();
    if (scalar.getScalarType() != Scalar.TYPE_STRING || stringValue.length() < 3) {
      return false;
    }
    final char charAtZero = stringValue.charAt(0);
    final char charAtEnd = stringValue.charAt(stringValue.length() - 1);

    if (!detectString(charAtZero) || !detectString(charAtEnd)) {
      return false;
    }

    if (scalar.getParent().getType() == ASTNode.ARRAY_ACCESS) {
      ArrayAccess arrayAccess = (ArrayAccess) scalar.getParent();
      final Expression variableName = arrayAccess.getName();
      if (variableName.getType() == ASTNode.VARIABLE) {
        Variable var = (Variable) variableName;
        if (var.isDollared() && var.getName() instanceof Identifier) {
          final Identifier id = (Identifier) var.getName();
          return id.getName().equals("_GLOBALS") // $NON-NLS-1$
              || id.getName().equals("GLOBALS"); // $NON-NLS-1$
        }
      }
    }
    return false;
  }
Esempio n. 3
0
 public void run() throws InterruptedException {
   Row row;
   while ((row = source.receive()) != null) {
     context.values = row.getValues();
     Object[] values = new Object[projects.size()];
     for (int i = 0; i < projects.size(); i++) {
       Scalar scalar = projects.get(i);
       values[i] = scalar.execute(context);
     }
     sink.send(new Row(values));
   }
 }
 public boolean visit(Include include) throws Exception {
   // special case for include statements; we need to cache this
   // information in order to access it quickly:
   if (include.getExpr() instanceof Scalar) {
     Scalar filePath = (Scalar) include.getExpr();
     fRequestor.acceptMethodReference(
         "include",
         0, //$NON-NLS-1$
         filePath.sourceStart(),
         filePath.sourceEnd());
   }
   return true;
 }
Esempio n. 5
0
  @Override
  protected StringBuffer format(Scalar scalar, StringBuffer toAppendTo, FieldPosition pos) {
    if (scalar == null || scalar.getValue() == null) {
      return toAppendTo;
    }

    if (scalar instanceof Display && nf(scalar) != null) {
      NumberFormat f = nf(scalar);
      return f.format(scalar.getValue(), toAppendTo, pos);
    }

    toAppendTo.append(scalar.getValue());
    return toAppendTo;
  }
    public boolean apply(ASTNode node) {
      // stops when found - that's the reason to use ApplyAll
      if (exists) return false;

      if (node.getType() == ASTNode.SCALAR) {
        final Scalar scalar = (Scalar) node;

        final String stringValue = scalar.getStringValue();
        if (scalar.getScalarType() != Scalar.TYPE_STRING || stringValue == null) {
          return false;
        }

        final int length = stringValue.length() - 1;
        if (stringValue.charAt(0) != '"'
            && stringValue.charAt(length) != '"'
            && stringValue.equals(name)) {
          exists = true;
        }
      } else if (node.getType() == ASTNode.FUNCTION_INVOCATION) {
        FunctionInvocation functionInvocation = (FunctionInvocation) node;
        final Expression functionName = functionInvocation.getFunctionName().getName();
        if (!(functionName instanceof Identifier)) {
          return false;
        }

        final Identifier identifier = (Identifier) functionName;
        final List<Expression> parameters = functionInvocation.parameters();
        if (!"define".equalsIgnoreCase(identifier.getName())
            || parameters == null
            || parameters.size() == 0) { // $NON-NLS-1$
          return false;
        }

        final Expression expression = parameters.get(0);
        if (expression.getType() != ASTNode.SCALAR) {
          return false;
        }

        Scalar scalar = (Scalar) expression;
        final String stringValue = scalar.getStringValue();
        if (stringValue.length() < 2 || stringValue.charAt(0) != '"') {
          return false;
        }
        exists = name.equals(stringValue.substring(1, stringValue.length() - 1));
      }
      return true;
    }
 public boolean visit(Statement e) throws Exception {
   if (typeDeclaration.sourceStart() < e.sourceStart()
       && typeDeclaration.sourceEnd() > e.sourceEnd()) {
     if (e instanceof PHPFieldDeclaration) {
       PHPFieldDeclaration phpFieldDecl = (PHPFieldDeclaration) e;
       if (phpFieldDecl.getDeclarationStart() == offset
           && phpFieldDecl.sourceEnd() - phpFieldDecl.getDeclarationStart() == length) {
         result = ((PHPFieldDeclaration) e).getVariableValue();
         if (result instanceof Scalar) {
           Scalar scalar = (Scalar) result;
           if (scalar.getScalarType() == Scalar.TYPE_STRING
               && scalar.getValue().toLowerCase().equals(NULL)) {
             result = null;
           }
         }
         context = contextStack.peek();
       }
     }
   }
   return visitGeneral(e);
 }
Esempio n. 8
0
  public boolean visit(Include include) throws Exception {
    // special case for include statements; we need to cache this
    // information in order to access it quickly:
    if (include.getExpr() instanceof Scalar) {
      Scalar filePath = (Scalar) include.getExpr();
      modifyReference(
          include,
          new ReferenceInfo(
              IModelElement.METHOD,
              filePath.sourceStart(),
              filePath.sourceEnd() - filePath.sourceStart(),
              "include",
              Integer //$NON-NLS-1$
                  .toString(1),
              null));

      String fullPath = ASTUtils.stripQuotes(((Scalar) filePath).getValue());
      int idx = Math.max(fullPath.lastIndexOf('/'), fullPath.lastIndexOf('\\'));

      String lastSegment = fullPath;
      if (idx != -1) {
        lastSegment = lastSegment.substring(idx + 1);
      }
      modifyDeclaration(
          include,
          new DeclarationInfo(
              IModelElement.IMPORT_DECLARATION,
              0,
              include.sourceStart(),
              include.sourceEnd() - include.sourceStart(),
              filePath.sourceStart(),
              filePath.sourceEnd() - filePath.sourceStart(),
              lastSegment,
              fullPath,
              null,
              null,
              null));
    }

    return visitGeneral(include);
  }
  /**
   * Identifies a constant use
   *
   * @param locateNode
   * @return
   */
  private static boolean isConstant(ASTNode locateNode) {
    assert locateNode != null;
    Scalar scalar = null;
    // check if it is an identifier
    if (locateNode.getType() != ASTNode.SCALAR) {
      ASTNode parent = locateNode.getParent();
      ASTNode node = null;
      // php 5.3, the parent is NamespaceName
      if (parent instanceof NamespaceName) {
        node = parent.getParent().getParent();
      } else { // non-php 5.3
        node = parent.getParent();
      }

      // check if the node is 'define'
      if ((locateNode instanceof Identifier)
          && "define".equals(((Identifier) locateNode).getName()) // $NON-NLS-1$
          && node instanceof FunctionInvocation) {
        FunctionInvocation inv = (FunctionInvocation) node;
        List<Expression> parameters = inv.parameters();
        if (parameters != null && parameters.size() > 0) {
          Expression param = parameters.get(0);
          if (param instanceof Scalar) {
            scalar = (Scalar) param;
          } else {
            return false;
          }
        }
      } else {
        return false;
      }
    } else {
      scalar = (Scalar) locateNode;
    }

    // if it is not a dollared variable - it is not a global one
    if (scalar == null
        || scalar.getScalarType() != Scalar.TYPE_STRING
        || scalar.getStringValue() == null) {
      return false;
    }

    final int length = scalar.getStringValue().length() - 1;
    final char charAtBegining = scalar.getStringValue().charAt(0);
    final char charAtEnd = scalar.getStringValue().charAt(length);
    if (!detectString(charAtEnd) && !detectString(charAtBegining)) {
      return true;
    }

    // check if it is part of define
    ASTNode previous = locateNode.getParent();
    if (previous instanceof NamespaceName) {
      previous = previous.getParent();
    }
    if (previous.getType() == ASTNode.FUNCTION_NAME) {
      previous = previous.getParent();
    }

    if (previous.getType() != ASTNode.FUNCTION_INVOCATION) {
      return false;
    }

    final FunctionInvocation functionInvocation = (FunctionInvocation) previous;
    if (!(functionInvocation.getFunctionName().getName() instanceof Identifier)) {
      return false;
    }

    final Identifier identifier = (Identifier) functionInvocation.getFunctionName().getName();
    return "define".equalsIgnoreCase(identifier.getName())
        || "constant".equalsIgnoreCase(identifier.getName()); // $NON-NLS-1$ //$NON-NLS-2$
  }
Esempio n. 10
0
  /**
   * Identifies the color in the frame
   *
   * @param in the Mat image in the region of interest
   * @return the color
   */
  public char identifyColor(Mat in) {
    // Mat blue = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);
    // Mat green = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);
    // Mat red = new Mat(in.rows(), in.cols(), CvType.CV_8UC1);

    // split the channels of the image
    Mat blue = new Mat(); // default is CV_8UC3
    Mat green = new Mat();
    Mat red = new Mat();
    List<Mat> channels = new ArrayList<Mat>(3);
    Core.split(in, channels);
    blue = channels.get(0); // makes all 3 CV_8UC1
    green = channels.get(1);
    red = channels.get(2);
    // System.out.println(blue.toString());

    // add the intensities
    Mat intensity = new Mat(in.rows(), in.cols(), CvType.CV_32F);
    // Mat mask = new Mat();
    Core.add(blue, green, intensity); // , mask, CvType.CV_32F);
    Core.add(intensity, red, intensity); // , mask, CvType.CV_32F);

    // not sure if correct from here to ...

    Mat inten = new Mat();
    Core.divide(intensity, Scalar.all(3.0), inten);
    // System.out.println(intensity.toString());
    // Core.divide(3.0, intensity, inten);
    // if intensity = intensity / 3.0; means element-wise division
    // use intensity.muls(Mat m)
    // so make new Mat m of same size that has each element of 1/3

    /*
    * or
    * About per-element division you can use Core.divide()

    Core.divide(A,Scalar.all(d), B);

    It's equivalent to B=A/d
    */

    // find normalized values
    Mat bnorm = new Mat();
    Mat gnorm = new Mat();
    Mat rnorm = new Mat();
    // blue.convertTo(blue, CvType.CV_32F);
    // green.convertTo(green, CvType.CV_32F);
    // red.convertTo(red, CvType.CV_32F);

    Core.divide(blue, inten, bnorm);
    Core.divide(green, inten, gnorm);
    Core.divide(red, inten, rnorm);

    // find average norm values
    Scalar val = new Scalar(0);
    val = Core.mean(bnorm);
    String value[] = val.toString().split(",");
    String s = value[0].substring(1);
    double bavg = Double.parseDouble(s);
    val = Core.mean(gnorm);
    String value1[] = val.toString().split(",");
    String s1 = value1[0].substring(1);
    double gavg = Double.parseDouble(s1);
    val = Core.mean(rnorm);
    String value2[] = val.toString().split(",");
    String s2 = value2[0].substring(1);
    double ravg = Double.parseDouble(s2);

    // ... here

    // original values
    /*
    // define the reference color values
    //double RED[] = {0.4, 0.5, 1.8};
    //double GREEN[] = {1.0, 1.2, 1.0};
    double BLUE[] = {1.75, 1.0, 0.5};
    //double YELLOW[] = {0.82, 1.7, 1.7};
    double ORANGE[] = {0.2, 1.0, 2.0};
    double WHITE[] = {2.0, 1.7, 1.7};
    //double BLACK[] = {0.0, 0.3, 0.3};
    */

    // define the reference color values
    // double RED[] = {0.4, 0.5, 1.8};
    // double GREEN[] = {1.0, 1.2, 1.0};
    double BLUE[] = {1.75, 1.0, 0.5};
    // double YELLOW[] = {0.82, 1.7, 1.7};
    double ORANGE[] = {0.2, 1.0, 2.0};
    double WHITE[] = {2.0, 1.7, 1.7};
    // double BLACK[] = {0.0, 0.3, 0.3};

    // compute the square error relative to the reference color values
    // double minError = 3.0;
    double minError = 2.0;
    double errorSqr;
    char bestFit = 'x';

    // test++;
    // System.out.print("\n\n" + test + "\n\n");

    // check BLUE fitness
    errorSqr = normSqr(BLUE[0], BLUE[1], BLUE[2], bavg, gavg, ravg);
    System.out.println("Blue: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_BLUE;
    }
    // check ORANGE fitness
    errorSqr = normSqr(ORANGE[0], ORANGE[1], ORANGE[2], bavg, gavg, ravg);
    System.out.println("Orange: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_ORANGE;
    }
    // check WHITE fitness
    errorSqr = normSqr(WHITE[0], WHITE[1], WHITE[2], bavg, gavg, ravg);
    System.out.println("White: " + errorSqr);
    if (errorSqr < minError) {
      minError = errorSqr;
      bestFit = COLOR_WHITE;
    }
    // check BLACK fitness
    /*errorSqr = normSqr(BLACK[0], BLACK[1], BLACK[2], bavg, gavg, ravg);
    System.out.println("Black: " + errorSqr);
    if(errorSqr < minError)
    {
    	minError = errorSqr;
    	bestFit = COLOR_BLACK;
    }*/

    // return the best fit color label
    return bestFit;
  }
Esempio n. 11
0
 private void bWriteScalar(ArrayList<SignatureSubPacket> subPackets) {
   Scalar scalSubPacket = new Scalar(getLenOfSubPackets(subPackets), 2);
   this.body.add(scalSubPacket.getWholeScalar()[0]);
   this.body.add(scalSubPacket.getWholeScalar()[1]);
 }
Esempio n. 12
0
  private void bWriteSignature(MessageDigest md, SignatureTypes sigType, Packet pack) {
    // https://tools.ietf.org/html/rfc4880#section-5.2.4
    /*
    * When a signature is made over a key, the hash data starts with the
    octet 0x99, followed by a two-octet length of the key, and then body
    of the key packet.  (Note that this is an old-style packet header for
    a key packet with two-octet length.)  A subkey binding signature
    (type 0x18) or primary key binding signature (type 0x19) then hashes
    the subkey using the same format as the main key (also using 0x99 as
    the first octet).  Key revocation signatures (types 0x20 and 0x28)
    hash only the key being revoked.
    */
    /*
    *  A certification signature (type 0x10 through 0x13) hashes the User
    ID being bound to the key into the hash context after the above
    data.  A V3 certification hashes the contents of the User ID or
    attribute packet packet, without any header.  A V4 certification
    hashes the constant 0xB4 for User ID certifications or the constant
    0xD1 for User Attribute certifications, followed by a four-octet
    number giving the length of the User ID or User Attribute data, and
    then the User ID or User Attribute data.
    */

    ArrayList<Byte> toBeHashed = new ArrayList<Byte>();
    /*
     * this switch fills only the toBeHashed arraylist, the actual generation will be performed at the bottom of the function
     */
    switch (sigType) {
      case POSITIVE_CERTIFICATION_OF_A_USER_ID_AND_PUBLIC_KEY_PACKET:
        /*0x13: Positive certification of a User ID and Public-Key packet.
        The issuer of this certification has done substantial
        verification of the claim of identity.

        Most OpenPGP implementations make their "key signatures" as 0x10
        certifications.  Some implementations can issue 0x11-0x13
        certifications, but few differentiate between the types.*/
        if (this.version == 4) {

          /*
           * A V4 certification
          hashes the constant 0xB4 for User ID certifications or the constant
          0xD1 for User Attribute certifications, followed by a four-octet
          number giving the length of the User ID or User Attribute data, and
          then the User ID or User Attribute data.
           */
          if (!(pack instanceof UserIDPacket) && !(pack instanceof UserAttributePacket)) {
            System.err.println(
                "one cannot compute and positive certifcation signature on a Packet which is not an user id or user attribute, but a "
                    + pack.getClass());
          }
          if (pack instanceof UserIDPacket) {
            System.out.println("A UserIDPacket is to be signed");
            toBeHashed.add((byte) 0xB4);
          } else if (pack instanceof UserAttributePacket) {
            System.out.println("A User Attribute is to be signed");
            toBeHashed.add((byte) 0xD1);
          }

          Scalar scal = new Scalar(pack.getWholePacketSize(), 4); // write 4 bytes size
          toBeHashed.add((byte) scal.getWholeScalar()[0]);
          toBeHashed.add((byte) scal.getWholeScalar()[1]);
          toBeHashed.add((byte) scal.getWholeScalar()[2]);
          toBeHashed.add((byte) scal.getWholeScalar()[3]);
          int bodyOffset = pack.getHeaderLength();

          byte[] packet2beSigned = pack.getWholePacket();

          for (int i = bodyOffset; i < pack.getWholePacketSize(); i++) {
            toBeHashed.add((byte) packet2beSigned[i]);
          }
        } else if (version == 3) {
          /*
          * The concatenation of the data to be signed, the signature type, and
            creation time from the Signature packet (5 additional octets) is
            hashed.  The resulting hash value is used in the signature algorithm.
            The high 16 bits (first two octets) of the hash are included in the
            Signature packet to provide a quick test to reject some invalid
            signatures.
          */
          int bodyOffset = pack.getHeaderLength();
          byte[] packet2beSigned = pack.getWholePacket();
          for (int i = bodyOffset;
              i < pack.getWholePacketSize();
              i++) { // add whole packet tot the to be signed byte array //TODO verify whole Packet?
            toBeHashed.add((byte) packet2beSigned[i]);
          }
          toBeHashed.add((byte) sigType.getNum());
          toBeHashed.add(
              (byte) 0x01); // add time 01020304 is my standard time, //TODO: change to time over a
          // parameter
          toBeHashed.add((byte) 0x02);
          toBeHashed.add((byte) 0x03);
          toBeHashed.add((byte) 0x04);

          // TODO: finish
        }
        break;
      case SUBKEY_BINDING_SIGNATURE:
        /*
        * This signature is a statement by the top-level signing key that
           indicates that it owns the subkey.  This signature is calculated
           directly on the primary key and subkey, and not on any User ID or
           other packets.  A signature that binds a signing subkey MUST have
           an Embedded Signature subpacket in this binding signature that
           contains a 0x19 signature made by the signing subkey on the
           primary key and subkey.

        */
        if (pack instanceof SubKeyPacket) {
          System.out.println("A SubKeyPacket is to sign");
        } else {
          System.err.println(
              "one cannot SUBKEY_BINDING_SIGNATURE on a Packet which is not an SubKeyPacket, but a "
                  + pack.getClass());
        }
        break;

      case PRIMARY_KEY_BINDING_SIGNATURE:
        /*
        * This signature is a statement by a signing subkey, indicating
           that it is owned by the primary key and subkey.  This signature
           is calculated the same way as a 0x18 signature: directly on the
           primary key and subkey, and not on any User ID or other packets.
        */
        if (pack instanceof SubKeyPacket) {
          System.out.println("A SubKeyPacket is to sign");
        } else {
          System.err.println(
              "one cannot comute a  PRIMARY_KEY_BINDING_SIGNATURE on a Packet which is not an SubKeyPacket, but a "
                  + pack.getClass());
        }
        break;

      case KEY_REVOCATION_SIGNATURE:
        /*
        * The signature is calculated directly on the key being revoked.  A
           revoked key is not to be used.  Only revocation signatures by the
           key being revoked, or by an authorized revocation key, should be
           considered valid revocation signatures.
        */
        if (pack instanceof PubKeyPacket) {
          System.out.println("A PubKeyPacket is to sign");
        } else {
          System.err.println(
              "one cannot comute a KEY_REVOCATION_SIGNATURE on a Packet which is not an PubKeyPacket, but a "
                  + pack.getClass());
        }
        break;

      case SUBKEY_REVOCATION_SIGNATURE:
        /*
        * The signature is calculated directly on the subkey being revoked.
           A revoked subkey is not to be used.  Only revocation signatures
           by the top-level signature key that is bound to this subkey, or
           by an authorized revocation key, should be considered valid
           revocation signatures.
        */
        if (pack instanceof SubKeyPacket) {
          System.out.println("A SubKeyPacket is to sign");
        } else {
          System.err.println(
              "one cannot comute a  SUBKEY_REVOCATION_SIGNATURE on a Packet which is not an SubKeyPacket, but a "
                  + pack.getClass());
        }
        break;

      default:
        System.err.println(
            "function bWriteSignature in Signature Packet: this should not happen, debug your code, dude");
    }

    if (this.version == 3) {

    } else if (this.version == 4) {

      for (int i = 0;
          i < bOffsetSizeUnhashed;
          i++) { // add body from version number up to tobe hashed subpackets to tobehashed
        // arraylist
        toBeHashed.add(body.get(i));
      }

      /*
       * https://tools.ietf.org/html/rfc4880#section-5.2.4
       * append trailer
       */
      toBeHashed.add((byte) this.version); // version
      toBeHashed.add((byte) 0xFF); // this is a fixed constatnt
      // rest is four byte len scalar of the tobehashed arrays sze
      Scalar scal = new Scalar(toBeHashed.size(), 4);
      toBeHashed.add((byte) scal.getWholeScalar()[0]);
      toBeHashed.add((byte) scal.getWholeScalar()[1]);
      toBeHashed.add((byte) scal.getWholeScalar()[2]);
      toBeHashed.add((byte) scal.getWholeScalar()[3]);

      /*
       * compute message digest
       */
      byte[] dataToBeHashed = new byte[toBeHashed.size()];
      for (int i = 0; i < dataToBeHashed.length; i++) {
        dataToBeHashed[i] = toBeHashed.get(i);
      }

      byte[] digest = md.digest(dataToBeHashed);
      this.digest = digest;
      /*
       * write low 16 bits of hash
       */
      body.add((byte) digest[0]);
      body.add((byte) digest[1]);
      /*
       * write computed signature to body
       */
      scal = new Scalar(digest.length * 8, 2); // size in bits for MPI
      body.add((byte) scal.getWholeScalar()[0]);
      body.add((byte) scal.getWholeScalar()[1]);
      for (int i = 0; i < digest.length; i++) {
        body.add((byte) digest[i]);
      }
    }
  }