1

Please help ,

Problem : I am reading file content using HTML5 FileReaderAPI -> ReadAsArrayBuffer. I am storing that buffer in a variable. Now I want to compute CRC of that ArrayBuffer ?

How shall I proceed. No clues. All operations are to be performed on browser side.

Let me tell you I am using arayBuffer because, that file contains Binary Data. So, I can not use anything other than ArrayBuffer. If we can use anything else than please tell that also.

maddy
  • 117
  • 7

1 Answers1

2

You can make an slight modification of this solution to work on arrays instead of on strings. Basically change the str.charCodeAt(i) to str[i].

Use a data view on your array buffer to access the elements in the array:

var dataview = new DataView(buffer);

Then use this:

dataview.getInt8(i);
Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • JotaBe: I know how how to compute CRC. As you also know that CRC function takes string str as input. But I have arrayBuffer as my input to this function. This will not work for mu sure. – maddy Sep 18 '14 at 09:24
  • That's why I'm saying that you have to slightly modify it. Try. if you cannot, show why. – JotaBe Sep 18 '14 at 09:29
  • I've added more clues fro your modification. – JotaBe Sep 18 '14 at 09:35
  • See, first of all I dont have access to data of ArrayBuffer. So how shall I extract info. – maddy Sep 18 '14 at 10:11
  • I dont wanna to convert to Int8 , Actually , if I convert it to int8 then the orignal length of the array buffer and the converted int8 length or size will mismatch. As, i am using one parameter that keeps record of the orignal size of arraybuffer. And If i convert it to int8 the size will get increased. Please suggest. – maddy Sep 22 '14 at 17:35
  • You only have to convert to int8, or whatever you want, the content of the data view constructed over the array buffer in order to compute the CRC. The original array buffer and its lenght is not modified in any way. I don't understand what the problem is. Please, be more clear. – JotaBe Sep 23 '14 at 07:59
  • see, I am explaing in detail: See, I have to read some binary file (file content may be some exe/zip/text/combination of any thoes ...), using File Reader API. I am using readFileAsBuffer to read the content. I my case i have a custom file that contains, meta data: like size , CRC and all. The size atribute tells about the total binary content in Bytes. Now , if I convert that Buffer using view and Int8 or flot64 or anything. It will convert the binary content accordingly. And size of file will get increased. Now, if I compute CRC in this state, then I will not match on server side. – maddy Sep 23 '14 at 09:03
  • As, we POST this file content on server, which again validated the CRC of the file on server end to validate. – maddy Sep 23 '14 at 09:06
  • 1) The original file is not covnerted to anything, so you can get the size from it. 2) To compute the CRC on the server you must, without any other possibility, break in chunks of some size (int8, 16, 32 or whatever) to compute it. Do it exactly the same way in the browser side to get the same CRC. (there are CRC 16, 32, etc.) – JotaBe Sep 23 '14 at 09:19
  • See this is the only problem. 1) On server side we have c++ code, that can operate on raw binary data. And compute the CRC32. 2) But for browser, we can get raw binary in ArrayBuffer. If we get size of the array buffer it matches the exact file bytes. OK. So, if we convert the ArayBuffer using views Bytes get increaed ?? So we can not operate on raw Binary directly, as my server is doing on files. This is the gap. Are you geting my points, my limitation :( – maddy Sep 23 '14 at 09:28
  • Please, read my comments with attention. The size problem is solved, ok? The DataView doesn't change the original array buffer, ok? The getInt32 would get the data in groups of four bytes, to allow you to calculate the CRC 32 on the browser, ok? Where is the problem man??? – JotaBe Sep 23 '14 at 09:31
  • I cant comment, unless I implement. Give me some time, i will come back , if stuck – maddy Sep 23 '14 at 09:56