CRC32 in Java ME - java-me

Anyway of creating a 'CRC32' checksum value in 'Java ME (Micro Edition)'?
The Micro Edition SDK I am using does not include CRC methods or 'Biginteger', and anything I migrate from C is running into problems with signed long.

Found source code for CRC32 in 'Java'
http://developer.classpath.org/doc/java/util/zip/CRC32-source.html
public void update (int bval)
{
int c = ~crc;
c = crc_table[(c ^ bval) & 0xff] ^ (c >>> 8);
crc = ~c;
}

Related

groovyc adds statements after ireturn in bytecode

Simple experiment where a method adds two ints.
First in Java:
gravadlax#durum byte_me> cat Add.java
public class Add {
public int add(int i, int j) {
return i + j;
}
}
Which leads to nothing unexpected in the bytecode (irrelevant parts removed):
gravadlax#durum byte_me> javac -g:none Add.java && javap -c Add
public int add(int, int);
Code:
0: iload_1
1: iload_2
2: iadd
3: ireturn
Now in Groovy, using #CompileStatic:
gravadlax#durum byte_me> cat Add.groovy
#groovy.transform.CompileStatic
class Add {
int add(int i, int j) {
i + j
}
}
And the bytecode:
gravadlax#durum byte_me> groovyc Add.groovy && javap -c Add
public int add(int, int);
Code:
0: iload_1
1: iload_2
2: iadd
3: ireturn
4: ldc #34 // int 0
6: ireturn
Now that's weird. What are 4 and 6 doing here? Not that it would change anything in practice, but out of curiosity, why would those two extra statements be generated?
Groovy Version: 2.3.4 JVM: 1.7.0_60 Vendor: Oracle Corporation OS: Mac OS X
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

VC++ Compiler options

I have a VC++ 6.0 project which I am now compiling using VS2008 . I have this piece of code that used to compile under VC++ 6 but throws an error under VS2008 :
int CIDStorage::Length()
{
CIDStorage* m_ptr = this;
for(int i = 0;m_ptr->m_ptrNext != NULL;i++)
m_ptr = m_ptr->m_ptrNext;
if(i == 0)
if(m_ID.IsEmpty())
return 0;
return i+1;
}
the error is 'i' : undeclared identifier
No probs with that I can see how that came about . So ... Do I change the source code . Or is there a compiler setting I could set that cures this ?
VC++ 6 (normally1) follows a pre-standard rule where a variable defined in a for loop remains defined for the rest of the scope in which that for loop resides. VC++ 2008 follows the standard rule where the for loop defines a new scope, and the variable is defined only within that scope.
The cure is pretty simple -- define the variable outside the loop:
int CIDStorage::Length()
{
CIDStorage* m_ptr = this;
int i;
for(i = 0; m_ptr->m_ptrNext != NULL; i++)
m_ptr = m_ptr->m_ptrNext;
if(i == 0)
if(m_ID.IsEmpty())
return 0;
return i+1;
}
1 The compiler in VC++ is actually capable of following the correct rules for scoping of variables defined in a for loop. Unfortunately, to follow the rule, you have to use the /Za flag, which tries to enforce all the rules it knows as strictly as possible. That turns out to be completely unusable because with that turned on, it rejects (virtually?) all of its own headers as containing errors!

How to implement ACTION (move/rename, set permissions) operation in J2ME Bluetooth OBEX?

Bluetooth FTP specification says I need to use ACTION operation, here's a page
But the ClentSession provides only GET and PUT operations, and nothing mentioned in javadocs.
here's how the create file operation looks, it's pretty easy
public void create() throws IOException {
HeaderSet hs = cs.createHeaderSet();
hs.setHeader(HeaderSet.NAME, file);
op = cs.put(hs);
OutputStream os = op.openOutputStream();
os.close();
op.close();
}
Question 1: How do I implement ACTION operation with custom headers to perform move/rename and set permissions? It should be possible without JSR82 OBEX API. Please help me to do this.
Question 2:
Did I understand how to set permissions?
According to OBEX_Errata Compiled For 1.3.pdf (thanks alanjmcf!)
So, to set read-only, I should do the following:
int a = 0;
//byte 0 //zero
//byte 1 //user
//byte 2 //group
//byte 3 //other
//set read for user
a |= (1 << 7); //8th bit - byte 1, bit 0 -> set to 1
// a = 10000000
//for group
a |= (1 << 15); //16th bit - byte 2, bit 0 -> set to 1
// a = 1000000010000000
//for other
a |= (1 << 23); //24th bit - byte 3, bit 0 -> set to 1
// a = 100000001000000010000000
//or simply
private static final int READ = 8421504 //1000,0000,1000,0000,1000,0000
int value = 0 | READ;
//========== calculate write constant =========
a = 0;
a |= (1 << 8); //write user
a |= (1 << 16); //write group
a |= (1 << 24); //write other
// a = 1000000010000000100000000
private static final int WRITE = 16843008 // 1,0000,0001,0000,0001,0000,0000
//========= calculate delete constant ==========
a = 0;
a |= (1 << 9); //delete user
a |= (1 << 17); //delete group
a |= (1 << 25); //delete other
//a = 10000000100000001000000000
private static final DELETE = 33686016; //10,0000,0010,0000,0010,0000,0000
//========= calculate modify constant ==========
a = 0;
a |= (1 << (7 + 7)); //modify user
a |= (1 << (15 + 7)); //modify group
a |= (1 << (23 + 7)); //modify other
//a = 1000000010000000100000000000000
private static final MODIFY = 1077952512; // 100,0000,0100,0000,0100,0000,0000,0000
// now, if i want to set read-write-delete-modify, I will do the following:
int rwdm = 0 | READ | WRITE | DELETE | MODIFY;
// and put the value to the header... am I right?
if right, the only problem remains the question 1: how do I make ACTION operation and how to set the headers.
Note that the text you quote from the Bluetooth FTP specification mentions three headers: ActionId, Name, DestName. So you need to add one NAME header and one DestName header. Jsr-82 apparently doesn't define the const for that (new) header so quoting from the OBEX specification:
MODIFICATION
2.1 OBEX Headers
HI identifier | Header name | Description
0x94 Action Id Specifies the action to be performed (used in ACTION operation)
0x15 DestName The destination object name (used in certain ACTION operations)
0xD6 Permissions 4 byte bit mask for setting permissions
0x17 to 0x2F Reserved for future use. This range includes all combinations of the upper 2 bits
So create the following etc. (My Java's a bit rusty)
static final int DEST_NAME = 0x15;
And use that in your code.
[ADD] All the operations (actions) that are actions use the ACTION operation! :-,) That is use OBEX opcode ACTION instead of PUT or GET etc. The value of opcode ACTION is 0x86.
I'm reading this from "OBEX_Errata Compiled For 1.3.pdf". The IrDA did charge for specifications but seem to now provide them on request (http://www.irda.org). Ask for a copy of the latest OBEX specs (1.5 IIRC). I've done so myself but not yet got a response. Or you could maybe try googling for say "move/rename object action" to get that '1.3 Errata' PDF.
Anyway, if Java prevents you from using new Opcodes (only allowing GET and PUT) and also prevents you from using new HeaderId values then you can't proceed anyway. :-( *(There's no reason for them to do that as HeaderId encodes the data type it contains).
After having another look at the Java API I can't see any way of sending an arbitrary command over ClientSession. You'd have to manually build the packets, connect to the OBEX service and then send and receive packets over that connection. It isn't too difficult to build the packets...

Generating a comprehensive callgraph using GCC & Egypt

I am trying to generate a comprehensive callgraph (complete with low level calls to Linux, runtime, the lot).
I have statically compiled my source files with "-fdump-rtl-expand" and created RTL files, which I passed to a PERL script called Egypt (which I believe is Graphviz/Dot) and generated a PDF file of the callgraph. This works perfectly, no problems at all.
Except, there are calls being made into some libraries that are getting shown as built-in. I was looking to see if there is a way for the callgraph not to be printed as and instead the real calls made into the libraries ?
Please let me know if the question is unclear.
http://i.imgur.com/sp58v.jpg
Basically, I am trying to avoid the callgraph from generating < built-in >
Is there a way to do that ?
-------- CODE ---------
#include <cilk/cilk.h>
#include <stdio.h>
#include <stdlib.h>
unsigned long int t0, t5;
unsigned int NOSPAWN_THRESHOLD = 32;
int fib_nospawn(int n)
{
if (n < 2)
return n;
else
{
int x = fib_nospawn(n-1);
int y = fib_nospawn(n-2);
return x + y;
}
}
// spawning fibonacci function
int fib(long int n)
{
long int x, y;
if (n < 2)
return n;
else if (n <= NOSPAWN_THRESHOLD)
{
x = fib_nospawn(n-1);
y = fib_nospawn(n-2);
return x + y;
}
else
{
x = cilk_spawn fib(n-1);
y = cilk_spawn fib(n-2);
cilk_sync;
return x + y;
}
}
int main(int argc, char *argv[])
{
int n;
long int result;
long int exec_time;
n = atoi(argv[1]);
NOSPAWN_THRESHOLD = atoi(argv[2]);
result = fib(n);
printf("%ld\n", result);
return 0;
}
I compiled the Cilk Library from source.
I might have found the partial solution to the problem:
You need to pass the following option to egypt
--include-external
This produced a slightly more comprehensive callgraph, although there still is the " visible
http://i.imgur.com/GWPJO.jpg?1
Can anyone suggest if I get more depth in the callgraph ?
You can use the GCC VCG Plugin: A gcc plugin, which can be loaded when debugging gcc, to show internal structures graphically.
gcc -fplugin=/path/to/vcg_plugin.so -fplugin-arg-vcg_plugin-cgraph foo.c
Call-graph is place to store data needed
for inter-procedural optimization. All datastructures
are divided into three components:
local_info that is produced while analyzing
the function, global_info that is result
of global walking of the call-graph on the end
of compilation and rtl_info used by RTL
back-end to propagate data from already compiled
functions to their callers.

Load a .bin file in SQL server 2005 Express database in vc++ application

I would like to put a binary file in varbinary(MAX) column using _RecordsetPtr in a vc++ application.I am sure that lot of people have done this before. But I am new to database programming. Help is urgently needed ...Thanks in advance.
I found this example :
while(!rst->EndOfFile)
{
int shotid = rst->Fields->Item["shot_id"]->Value;
int shotPoint = rst->Fields->Item["shot_num"]->Value;
int length = rst->Fields->Item["length"]->Value;
--- I initialized tha variant and copied the variant into local memory. After that I accessed the SafeArray.
VariantInit(&varBlob);
//varBlob = rst->Fields->Item["data"]->GetChunk((long) MAX_HYDRO_SIZE);
varBlob = rst->Fields->Item["data"]->GetValue();
unsigned char* pData;
unsigned char data[MAX_HYDRO_SIZE];
int blobLength;
if (varBlob.vt == (VT_ARRAY | VT_UI1))
{
SafeArrayAccessData(varBlob.parray,(void **) &pData);
blobLength = varBlob.parray->rgsabound[0].cElements;
TRACE(" length %d : blobLength %d \n",length*4, blobLength);
if (length*4 > blobLength)
AfxMessageBox("Blob Short of Data");
memcpy(&data,pData,blobLength);
SafeArrayUnaccessData(varBlob.parray);
}
else
AfxMessageBox("BLOB returned wrong type");
on http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_21296032.html

Resources