예제 #1
0
파일: android.c 프로젝트: aswanth/MyAndroid
jstring Java_com_ingogo_android_cardreader_helpers_Swipe_decodeSquareSwipe(JNIEnv *environment, jobject thiz, jshortArray jArray, int length) {
	jshort *jSamples;
	jstring returnString;
	short *samples;

	jSamples = (*environment)->GetShortArrayElements(environment, jArray, NULL);
	if (jSamples == NULL) {
		return (*environment)->NewStringUTF(environment, "");
	}
	samples = jSamples;

	PcmData *pcm = pcm_init(samples, length, false);

	(*environment)->ReleaseShortArrayElements(environment, jArray, jSamples, JNI_ABORT);

	SquareDecoder *decoder = square_init(pcm);
	square_decode_swipe(decoder);

	if (decoder->lastError == SwipeError_Success) {
		returnString = (*environment)->NewStringUTF(environment, decoder->cardString);
	} else {
		char errorCode[8];
		sprintf(errorCode, "%d", decoder->lastError);
		returnString = (*environment)->NewStringUTF(environment, errorCode);
	}

	pcm_free(pcm);
	square_free(decoder);

	return returnString;
}
예제 #2
0
int main( int argc, const char **argv )
{
    if ( argc == 2 ) {
        square *q = square_init( atoi(argv[1]) );
        magic(q);
        square_print(q);
        printf("\nMagic sum : %d\n", square_sum(q));
        square_free(q);
    } else {
        printf("Wrong number of arguments, should be 1 and"
                " the argument have to be an integer\n");
    }

    return 0;
}
예제 #3
0
int main(int argc, char* argv[]) {
  long long row[100], previous_row[100];
  int current_row;
  int i;
  long long sum = 0;
  add(1);
  for (current_row=1;current_row<=51;current_row++) {
    row[0]=1;
    row[current_row-1]=1;
    for (i=1;i<current_row-1;i++) {
      row[i]=previous_row[i-1]+previous_row[i];
      add(row[i]);
    }
    for (i=0;i<current_row;i++) previous_row[i]=row[i];
  }
  for (i=0;i<nb;i++) if (square_free(numbers[i])) sum+=numbers[i];
  printf("%lli\n", sum);
  return 0;
}
예제 #4
0
int main(int argc, char **argv) {
	short *samples;
	int file_descriptor;
	struct stat file_stats;
	int offset = 0;

	if (argc == 1) {
		printf("%s <audio file>\n", argv[0]);
		return EXIT_FAILURE;
	}

	file_descriptor = open(argv[1], O_RDONLY);
	if (file_descriptor == -1) {
		perror("Failed to open audio file");
		return EXIT_FAILURE;
	}

	if (fstat(file_descriptor, &file_stats)) {
		perror("Failed to stat file");
		return EXIT_FAILURE;
	}

	if (!strncmp((argv[1] + strlen(argv[1] - 4)), ".wav", 4)) {
		offset = 44;
	}

	samples = mmap(0, file_stats.st_size, PROT_READ, MAP_SHARED,
			file_descriptor, offset);

	if (samples == MAP_FAILED) {
		close(file_descriptor);
		perror("Failed to map file");
		return EXIT_FAILURE;
	}

	printf("File length: %d\n", (int) (file_stats.st_size - offset));

	PcmData *pcm = pcm_init(samples, (file_stats.st_size - offset) / 2, false);

	printf("Decoding swipe using Square algorithm...\n");
	SquareDecoder *decoder = square_init(pcm);
	square_decode_swipe(decoder);

	if (decoder->lastError == SwipeError_Success) {
		printf("Decoded card characters: %s\n", decoder->cardString);
	} else {
		printf(
				"Card decoding using Square algorithm has failed! Using UniMag algorithm...\n");
		char *byteSamples = (char*) malloc(
				(file_stats.st_size - offset) * sizeof(char));
		char *buffer = (char*) samples;
		int i = 0;
		for (i = offset; i <= file_stats.st_size; ++i) {
			byteSamples[i] = buffer[i];
		}

		char *cardString = unimag_decode(byteSamples,
				file_stats.st_size - offset);
		printf("Decoded card characters: %s\n", cardString);
		free(byteSamples);
		free(cardString);
	}

	pcm_free(pcm);
	square_free(decoder);

	if (munmap(samples, file_stats.st_size) == -1) {
		perror("Failed to unmap file");
	}
	close(file_descriptor);

	return EXIT_SUCCESS;
}