# Setting Up Maven+GAE in Eclipse

Project Setup:

The needed packages to start google app-engine project with maven.

4. Create Dynamic web project with JPA
5. Now convert dynamic web project to maven project
6. Run the maven project on server

# Cryptography: RSA Concepts

RSA is an asymmetric key cryptography algorithm also popularly known as public-key cryptography. In asymmetric key cryptography two keys are used: one public and one private. RSA was developed by three persons named Rivest, Shamir, and Adleman.

The RSA algorithm is given as-

Key-Exchange-

1. Choose two large prime numbers p and q
2. Calculate n = pq
3. Calculate Φ(n) = (p-1)X(q-1)
4. Choose e between 1<e<Φ(n), e and Φ(n) are co-prime or GCD of e and Φ(n) is equal 1
5. Calculate d, where d= e-1 (mod n)
6. Keep (e, n) public and (d, Φ(n)) private

Encryption-Decryption-

1. Sender encryption, C = Pe (mod n), for the plain text P.
2. Receiver decryption, P = Cd (mod n), for the cipher text C.

Sender_Encrypt(P,e,n){
C = Pe (mod n)
}

P = Cd (mod n)
}

We can easily understand RSA crypto system by taking an example.
Suppose Bob chooses p and q as 3 and 11.
He calculates n and Φ(n) from p and q as-
n=pq=3X11=33
Φ(n)=(p-1)(q-1)=2X10=20.
Bob chooses e= 7, where 1<7<Φ(n)
He then calculates, d as
d= e-1 mod Φ(n)
d= 7-1 mod 20
d= 3
Now, Bob announces his public-key pair (e,n) as (7,33) and keep secret private key pair(d, Φ(n)).

Suppose, Alice want to send a message to Bob. In that case, Alice takes the public-key pair of Bob to encrypt the message. Let’s take the plaintext is 2.

Alice’s encryption for plaintext = 2,
C = Pe (mod n)
C = 27 (mod 33)
C = 64X2(mod 33)
C = 31X2 (mod 33)
C = 62(mod 33)
C = 29

Now Alice send the cipher text = 29 to Bob. Trudy cannot decrypt this message because d and Φ(n) are unknown.(Not in this case because we chose small prime numbers p and q!!!)

After receiving the cipher text Bob decrypts it with his private key pair as-
P = Cd (mod n)
P = 293 (mod 33)
P = 24389 % 33 (modulus)
P = 2

Finally Bob gets the plaintext 2 from Alice.

Magic Φ(n)

Φ(n) is kept private, only n is known to us. If Bob chooses two large prime numbers p and q and calculates n, can it be possible to calculate Φ(n) using brute-force. How much time will it take?
If we can get the Φ(n) then only we can calculate d. After we get Φ(n), d can be calculated using Euler’s extended theorem or Chinese remainder method.

There are possible attacks in RSA but not feasible for very large n (p and q). (In PC’s)
1. Chosen cipher-text attack
2. Factorization attack

# C Concepts: Functions

In computer programming, a function is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Subprograms may be defined within programs, or separately in libraries that can be used by multiple programs.

In different programming languages, a function may be called a procedure, a subroutine, a routine, a method, or a subprogram.[wikipedia]

C functions can be categorized into two categories- library functions and user defined functions. The library functions are provided to the programmer and the programmer can add her user defined function to the library if she intend to do so. C has a small set of library functions built in including the I/O functions, string handling functions, math functions etc.

In C, to make a user-defined function we need the following-

1. A function declaration or prototype
2. A function definition or body
3. A function call

Function declaration:

To use a function what we need first is the function declaration statement. The function declaration statement is as follows-

Return-type function-name (parameter list);

e.g., int sum(int a, int b);

The variations for this statement may vary-

int sum(int, int);

int sum(int a,int);

int sum(int, int b);

etc.

Function declaration works as the signature for the function and the function must use the same return data type, same type of arguments, same number of arguments and same name in function definition and call.

Function definition:

The function definition or body consists of the tasks that the function going to perform. The syntax for function definition or body is as follows-

return-type function-name(parameter list){

Codes for the function

}

e.g., A function definition or body to multiply two numbers with return type-

int multiply(int x, int y){

int result;

result = x * y;

return result;

}

void multiply(int x, int y){

int result;

result = x * y;

printf(“\nThe result is: %d”, result);

}

The function definition primarily depends on the function declaration. It must match with the function name and parameter list. Further the function return type should must match the return variable’s data type.

Function call:

Functions can be called from other function or from main function. The syntax for function call statement is as follows-

function-name(parameter list without the data type);

void and return statements in functions:

When the return statement in function declaration or definition (must same) is declared as void then the return statement in function definition or body is not mandatory. The return statement may be there for other purposes.

When the return statement in function declaration or definition(must same) is declared as one of the C data type (int, float etc.) then the function definition or body must have the return statement and match the return data type of function declaration.

#include<stdio.h>

int sum1(int a, int b); // function declaration

void sum2(int a, int b); // function declaration

int main(){

int x=10,y=20;

int result;

result = sum1(x,y);

sum2(x,y);

printf(“Using int, sum is: %d”,result);

}

int sum1(int a, int b){

int result;

result = a+b;

return result;

}

void sum2(int a, int b){

int result;

result = a + b;

printf(“Using void, sum is: %d\n”,result);

}

How the program is being executed?

Procedures behind the scene:

First we declared the function prototype. The syntax of the function prototype is already discussed.

Second, inside the main function two variable values are passed with the caller to the called function. The caller function consist only the function name and variables names.

Third, the caller function passes the arguments by copying the variable values to the called function. In the called function we get the values of the caller function and can perform the operations and returning the result for non-void return type and printing the result for void type.

In void return type we cannot get the values of (generated or derived inside the function) called function in the same scope of the caller function. So we are printing the result of the called function inside it in the case of void in the above example. But this can be possible using pointers, which is described later.

Can we return more than one value in a function call?

Technically, no. We cannot return more than one value. But there are way to do this. Using global variable or pointer.

What is actual argument and formal parameter?

Argument is an expression passed to a function and parameter is a reference declared in a function declaration/definition/prototype. Argument and parameters are used interchangeably but in programming paradigm literature argument and parameter has this distinction.

So, Actual argument is the argument passed to a called function from the caller. Formal parameter is the parameter that receives the values of the caller function in the called function. People use the term actual argument and formal parameter the most. But the terms formal argument or actual parameter is very odd.

# C Concepts: Recursion

Definition: In many programming languages, a function can call itself, and this activity is called as recursion, and the function is called recursive function. Recursion is sometimes referred to as circular definition.

Some of the languages that support recursion are C, C++, and Java etc. Scheme, Haskell, Prolog use recursion as a core-control structure in their language constructs.

How to calculate factorial of a number using recursion? Explain briefly.
The program to calculate the factorial of a number is as follows-(In C)
int fact(int n){
int f=1;
if(n<2){
f=1;
return f;
}
else{
f = n*fact(n-1);
return f;
}
}

Evaluation-
f= 5 * (4 * fact(3));
f= 5 * (4 * (3 * fact(2)));
f= 5 * (4 * (3 * (2 * fact(1))));
f= 5 * (4 * (3 * (2 * (1))));

How the variables are maintained in each recursive function call?
The function variables, parameters and return addresses are stored in a run-time stack when each recursive function is called.

What is a run-time stack? (Call stack, execution stack, control stack, or machine stack)
The stack used to store return addresses of function call, function variables and parameters, is usually referred to as run-time stack in programming language literature. A run-time stack is a stack of activation records used to model the semantics of function call and return.

What is an activation records or activation frame or stack frames? Explain briefly.
An activation record is a block of information associated with a function activation, including the functions parameters and local variables. Activation records consist of following items- Static Link/Dynamic Link, Parameters, Local Variables, Return Address, Saved Registers, Temporary Value, and Return Value.

What is static and dynamic link in activation record?
The static link points to the bottom of the Activation Records Instance of the callee’s static parent.
The dynamic link points to the top of the Activation Records Instance of the caller.

Is the instructions that we write after the recursive function call are get executed?
Yes. The instructions that resides after the recursive function call, are get executed. Actually all the instructions are get executed before the return statement without taking no concern with the recursive function call, and there values are stored in the run-time stack.

How to access stack values in recursive function call?
Technically, all local variables and parameters are stored in a new stack in each recursive function call. And there is some mechanism like static/dynamic link to point to the top/bottom of the stack and we can access the values respectively.

What are the properties that must a problem exhibit to be a recursive?
There are two properties:
1. A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer
2. A set of rules that reduce all other cases toward the base case

What is base case or guard clause in recursion?
Base case is the case which terminates the infinite execution of recursive function call.

How to avoid infinite recursive function call?
Using the base case in recursive function, we can avoid infinite recursive function call. Though without the base case the run-time stack will be run-out of space and eventually the program terminates.

Debugging in fact.c where the program calculates factorial using recursion.
Debugging tool: “gdb”, MinGW()

#include
int fact(int n){
int f;
if(n<2){
return 1;
}
else{
f=n*fact(n-1);
printf(“\nCount: %d”,n);
return f;
}
}

int main(){
int x;
x = fact(5);
printf(“\nFactorial: %d\n”, x);
}

g++ -g fact.c -o fact
gdb fact

start

bt 10

step

# C Concepts: Structures

Definition: A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.

Structures help to organize complicated data, because it permits a group of related variables to be treated as a unit instead of as separate entities.

How to declare a structure?

The general form of a structure declaration is

struct tag{
type member-name;
type member-name;
type member-name;
.
.
.
} structure-variable;

Where either “tag” or “structure-variables” may be omitted, but not both.

An example of structure declaration is as follows:

struct book{

char title[10];

char author[10];

int isbn;

}library;

How to declare a structure template?

struct book{

char title[10];

char author[10];

int isbn;

} ;

This structure does not create an actual data object, but it describes what constitutes such an object. It actually creates a template because it outlines how data will be stored. The structure template tells the compiler how to represent the data, but it doesn’t make the computer allocate space for the data.

How to declare a structure variable?

To create the structure variable we have to write the following code-

struct book library;

This instruction creates the variable library. Using the book template, the compiler allocates space for char arrays and integers variables. This storage is grouped together in a single name library.

The Odd Way?

If we know that the program won’t need more structure variable in later of the program design, then we can omit the “tag” from the structure like this-

struct{

char title[10];

char author[10];

int isbn;

}x,y,z;

The structure “tag” is not declared so we cannot later define structure variable using the struct <tag> <variable-name>, if we want we have to edit above.

What are the operations that can be performed on structures?

The legal operations that can be performed on structures are-

1. Accessing its members.
2. Taking it’s address with &
3. Copying it or assigning to it as a unit

How to use structures?

Structure is not actually any core programming statement. It is user defined data type and the useful tasks are done using its member variables. The variables named in a structure are called as members of the structure.

struct book{

char title[10];

char author[10];

int isbn;

};

Here title, author and isbn of a book are grouped together in a single programming entity book.

Suppose we have to facilitate a user to add and retrieve all details of a book. The structure book will give us the power to handle this type of activity easily. The programming activity for this will be like given below:

#include <stdio.h>

#include <stdlib.h>

struct book{

char title[20];

char author[20];

int isbn;

}library;

void display_book();

int main(){

int ch;

printf(“\n1. ADD BOOK\n2. DISPLAY BOOK\n3. EXIT\n”);

do{

scanf(“%d”,&ch);

switch(ch){

break;

case 2: display_book();

break;

case 3: exit(0);

break;

default: printf(“Wrong Choice\n”);

}

}while(ch!=3);

return 0;

}

printf(“ENTER TITLE OF THE BOOK:\n”);

scanf(“%s”,library.title);

printf(“ENTER AUTHOR OF THE BOOK:\n”);

scanf(“%s”,library.author);

printf(“ENTER ISBN:\n”);

scanf(“%d”,&library.isbn);

}

void display_book(){

printf(“TITLE OF THE BOOK: %s\n”,library.title);

printf(“AUTHOR OF THE BOOK: %s\n”,library.author);

printf(“ISBN: %d\n”,library.isbn);

}

Like this we can simply manage our book information. For persistent storage of the library data we can use file system.

How to access the members in a structure?

We efficiently created the book management system, but how we accessed the members in the structure. To access the structure members we did this-

structure-variable.member-variable-name

Here we should point out the use of dot operator. It has highest precedence as (), [] and -> operators. Here the dot operator is called as membership operator.

Yeah, all the members in this structure are char or integers, the primary data type of C. But if the members is pointer type then what will happen?

How to access the pointer-variable (member) in a structure?

#include <stdio.h>

struct pointerStruct{

int p,*q;

}x;

int main(){

x.p=10;

x.q=&x.p;

printf(“The value of y = %d\n”,*(x.q));

return 0;

}

How to use a structure pointer or pointer to a structure?

We can use structure pointer to point to a structure. Below program describes the same-

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct node{

char name[10];

int age;

};

int main(){

struct node person={“Probhat”,22};

struct node *temp = &person;

printf(“Name: %s”,temp->name);

printf(“\nAge: %d”,temp->age);

return 0;

}

Here *temp point to a “node” type of structure that is person in this case. It requires minimum memory of “node” data type. The following lines can be written to fulfil the requirement if the assignment is not taking place-

temp = (struct node *)malloc(sizeof(struct node));

temp->name=”Probhat”;

temp->age=22;

In the above example, we have used the arrow operator to access the members of the structure.

What is an arrow operator? Where to use dot operator and where to use the arrow operator in structure?

An arrow operator is an operator having highest precedence in C language similar to dot, () and [] operators. Arrow operators are used to access structure members using structure pointer.

Case I: When to use dot operator?

When we are accessing a member from a structure using structure variable then we use the dot operator (arrow operator cannot be used).

Case II: When to use arrow operator?

When we are accessing a member from a structure using structure pointer then we use the arrow operator (dot operator cannot be used).

What is a self-referential structure? And how to use a self-referential structure?

Self-referential structures are primarily used for implementing linked lists in C. We can declare self-referential structures like this-

struct node{

Int value;

Struct node *next;

};

This is a recursive declaration of a node, which is correct. *next is a pointer to a node, not a node itself.

struct node temp2;

struct node temp1; , which contains two members, an integer value and the *next pointer which can point to another node type of structure like temp2. The following program illustrates the concepts-

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

struct node{

int value;

struct node *next;

};

int main(){

struct node temp2;

struct node temp1 = {32,&temp2};

//temp1 = (struct node *)malloc(sizeof(struct node));

//temp2 = (struct node *)malloc(sizeof(struct node));

temp2.value = 10;

temp2.next = NULL;

printf(“Value of temp1.next: %u\n”,temp1.next);

printf(“Value of temp2.next: %u\n”,temp2.next);

printf(“Value of temp1: %u\n”,temp1.value);

printf(“Value of temp2: %u\n”,temp2.value);

return 0;

}

The first three printf statements will print the same values. Because the value of the temp.next is equal to the address of temp2 or temp2.value. This is happening for the following line given above as-

struct node temp1 = {32,&temp2};

Here address of temp2 is assigned to the temp1’s *next member, and it’s possible mainly because, *next is a self-referential structure and it can hold the address of another structure of node type.

A conceptual question:

Two structures are there:

struct node1{

int value;

struct node *next;

};

struct node2{

int value;

struct node *next;

};

And following are the two variables of the structure:

struct node2 temp2;

struct node1 temp1= {22, &temp2};

Is this assignment possible? If not why?

Ans: This assignment is wrong. Because node1 and node2 are not considered to be the same type of structure though there member variables are same. The compiler does not check what members are there in the template of the structure, (tag name different but member variables are same type and order) though they may same. So, it just produce an error.

Another simple question. We declared two structure templates and the variable names are the same in the both structure. Is it correct?

Ans: Yes. The structure variables are local only to the structure declared inside. There is no way of accessing it without using the structure name. So, it can be possibly local to a structure.

How to use structure as function return type? Can we do that?

Any C data type can be a function return type including void. We can use structure as function return type.

How can we declare an arrays of structures?

We can declare an arrays of structure like the following-

struct key{

char *word;

int count;

} keytab[10];

In which situation we need an array of structures?

Ans: We have seen that structure is used to store information about particular object. In the above example of if we need to store more book information then we need is an array of structure.

How to optimize the structure size? Is it depend on the declaration of the members?

Ans: Structure size can be optimized by declaring structure member depending on the size of increasing or decreasing order.

Yes. It depends on the declaration of structure members.

# C Concepts: Type Casting

Definition: When an operator has operands of different types, they are converted to a common type to perform some kind of operations using some rules. A simple example to illustrate the concept:

float x=321.123;

int p=x;

Equal is an operator and the operands p and x are of different types. When we print out the value of p we get 321 truncating the value of x. We as a programmer adding nothing to do so. But the compiler is doing this job itself and printing the truncated value.

What are the type of type conversions usually C performs? OR what are the rules for type conversions?

C performs two types of type conversions:

Implicit: When the programmer does nothing to covert the type of operands but the compiler do it for the programmer’s sake. The compiler is implicitly doing it for you.

Explicit: When the programmer explicitly add something to convert the type of operands. So, the compiler must intend to do so because of explicit adding of some instruction.

How can explicit and implicit type conversions can be achieved? Give example.

Float x=123.321;

Int p=(int)x;

printf(“Value of p after explicit typecasting: %d\n”,p);

// Explicit typecasting truncation

x=321.123;

p=x;

printf(“Value of p after implicit typecasting: %d\n”,p);

// Implicit typecasting truncation

What is type casting?

When we explicitly convert an expression to be of specific type we use casting as above example. The general form of cast is-

(type) expression- where type is a valid data type.

An example,

int x=179;

(float)x/2; we get floating point value here. This is possible because of the cast.

What is type promotion and type demotion?

The both terms are relating to type conversions.

Type promotion: Converting a narrower operand into a wider one without losing information is called as type promotion. Example: converting an integer to floating point in some expression. We are promoting integer variable value to floating point value.

Type demotion: Converting a wider operand into a narrower one by losing information is called as type demotion. Example: converting a floating point to an integer in some expression. We are demoting the floating variable value to integer value.

What is truncation? In which case it occurs?

When we demote a wider operand to narrower operand then truncation occurs in the value of the operand for the demotion of the operand. For example, converting a floating point to an integer in assignment, the variable value will be truncated when we print out the integer value.

Truncation occurs only in type demotion of the variable. In both cases, implicit or explicit type conversions there may be a possibility of truncation, but it must follow type demotion.

What is automatic type conversion? Is there any rule for automatic type conversions while performing operations on operands?

The implicit type conversion is also referred to as automatic type conversion. Yes, there are some rules, which C compiler follows for automatic type conversions.

What does it happen when we mix up different type of operands in an expression?

When we mix up different type of operands into an expression, the smaller size operands are promoted to larger size operands using some rules.

What are the rules for automatic type conversions?

IF an operand is a long double

THEN the second is converted to long double

And so on.

For example, suppose we are performing an operation on floating point operand value with integer operand value then the integer variable is promoted to floating point to perform the operation.

In which situation of type conversion the compiler may draw a warning?

In type demotion, the compiler may draw warning.

The core concept:

Actually in the expressions, when automatic type conversion takes place the type is promoted. In assignment expression that is different and we can make demotion. Type casting is explicit type conversion. Truncation occurs in type demotion, it does not depend on the explicit or implicit type conversion (watch out the assignment case).

# C Concepts: Storage Class Specifiers

Storage Class Specifiers

Definition: Storage class specifiers are the language features that tells the compiler how to store the subsequent variable.

How can we declare storage class specifiers? Give the general syntax of storage class specifiers?

Storage class can be declared before a data type of variable name. The general form of declaration look like this-

storage_specifier type var_name;

How many storage class specifiers are there? What are they?

There are four storage class specifiers in C: extern, static, register and auto.

Concepts: (remembering in the easy way)

 Storage Class Storage Default Value Scope Life auto Memory Garbage Local Till the control remains within the block register CPU registers Garbage Local static Memory Zero Local Persist between function calls extern Memory Zero Global Persist till the end of the program