In Rails 3, I want to post the bitmap image data in JSON format to server, so I do the following steps.
1. In client, translate the bitmap image to string.
2. Encode the string in JSON format and post to the server.
3. Decode the bitmap image data of JSON format.
Now the problem is: In bitmap image, there are many 0 bytes or other unreadable bytes, after encoding in JSON format, 0 byte will be translated to /u0000, space byte will to /u000a.
In the server end, I use ActiveResource::Formats::JsonFormat.decode to decode the JSON string, but the method will stop when it meets /u0000, for example,
JSON string "\u0066\u0066\u0000\u0066\u0066" will be decoded to be "ff", and the rest three bytes will be discarded silently.
So how to resolve this problem? should I write a function to decode the JSON string myself?
You should really be POSTing that data as binary in a multipart form.
If you must encode it into a string, use base64.
Related
I am working on importing a file from a legacy C++ codebase. The file itself is technically a JSON but one of the values is obfuscated with an XOR function in the C++ codebase before I receive it and looks something like this.
{"version": 15, "data": "C\u0016Q45\u0010 46QY\\\u0011\n\u0019a\u0003\u0019}\u001apg"}
The value for "data" is technically a UTF8 string.
I know the algorithm used to decode that data into a usable JSON string. However, I am unable to get swift to parse this into a [String : String] apparently due to formatting issues.
Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 31."
So far I have managed to get the specific data in question by isolating the subdata
let encryptedData = data.subdata(in: dataPrefixLength ..< dataLength - 2)
Even still, I cannot seem to get swift to parse this into a String when I attempt to do so with UTF8 encoding.
String(data: encryptedData, encoding: .utf8)
It's also worth noting that the String description seems alright in the debugger when inspecting the data itself.
I'd really appreciate any advice.
Thanks!
From your example it looks as if the obfuscation generates binary data and the binary data is put into a string. That's not allowed in JSON. Therefore, it's invalid JSON and any parser with a reasonable amount of validation will reject it.
As an example, take the start of the string:
"C\u0016
It starts with the character "C". That's valid.
Then an escaped character with a hexadecimal values follows: 0016. However, U+0016 is not a valid Unicode codepoint. Therefore it's rejected. It's probably supposed to be the binary byte 0x16. But you can't put that into a JSON string.
You have two options:
Fix the source of the data, e.g. by using a Base64 encoding before putting binary data into JSON.
Write your own JSON parser to handle the invalid JSON.
I have a log of a programs state. This log can be manualy or time interval saved on a file for persistant storage. Before saving it to the file it is encrypted with RNCryptor.
My current appending(saving) to file flow:
Read file
Decript information from the read string
Concat decrypted string with the new string
Encrypt the concatenated string
Write it to file
What I imagine:
Encode new string
Append to file
When I read this I will have to build a string from all the encoded strings. But I don't know how to decrypt the file with multiple encrypted blocks in it. How to differentiate where one ends and another begins.
Also is this the best performance choice. The text in the file at maximum could get to 100MB(Possibly it will never get this big).
Is using Core Data viable? Each append as different record or something. And core data could be encrypted so no need for RNCryptor.
Would appreciate code in Objective-C if any.
There are many things you can do:
Easiest would be to encode the ciphertexts to text (e.g. with Base64) and write each encoded ciphertext to a new line. You need encoding for that, because the ciphertext itself might contain bytes that can be interpreted as newline control characters, but that won't happen with a text encoding. The problem with this is that it blows up the logs unnecessarily (e.g. by 33% if Base64 is used)
You can prepend each unencoded ciphertext with its length (e.g. big-endian int32 encoding) and write both as-is to a file in binary mode. If you begin reading the file from the beginning, then you can distinguish each ciphertext, because you know how long the following ciphertext is and when the next encoded length starts. The blowup is only as big as the encoding of the ciphertext length for each ciphertext.
Use a binary delimiter such as 0x0101 between ciphertexts, but such a delimiter might still appear in the ciphertexts, so you need to escape it if you find it somewhere in the ciphertext. This is a little tricky to get right.
If the amount of logs is small (few MB), then you can find a library to append to a ZIP file.
You can use the array to store the information and then read and write that array to file. find Example here.
Steps :
Read Array from the file.
Add the New Encrypted string to array.
Write array to file.
I'm trying to base64 encode but in us-ascii.
The problem is that bs data method
Using UTF8
Quote from the docs:
"Create a Base-64, UTF-8 encoded NSData from the receiver's contents using the given options."
Is it possible to create a base-64 us-ascii encoded?
Thanks in advance.
Base 64 for is for transferring MIME types (http://en.wikipedia.org/wiki/Base64) and all characters in Base 64 encoded string falls under ASCII.
I have a service in Windows that returns a PDF data, which it's in the database in a varbinary field. This service returns the PDF data as a Byte Array in JSON format
How can I read this data and convert it to PDF again, in my app in Xcode?
Which is the correct structure data to retrieve this byte array? NSArray? NSMutableArray?
I want to store this data, and I'm using a NSData structure...
Thanks
An NSArray would be for arrays of objects, you want an array of bytes, which an NSData is.
To get the data out of JSON, you would use the NSJSONSerialization class to extract the data from your JSON object -- the deserializer will probably return your literal data as an NSString, which you then would convert into an NSData by decoding it.
Presumably your raw data is in the JSON as Base64 or some such.
I'm trying to convert a hex string into base64 on iPhone. The hex is:
5289be07c5c7edcc18f3a02c7b81c110b8637f8b2ddbc29cdabcbd7e394c1695
But I cant's seem to get the base64 version of this, which is:
Uom+B8XH7cwY86Ase4HBELhjf4st28Kc2ry9fjlMFpU=
How would I get this base64 string?
Since you are using SHA256 (I also use it), I assume you get an NSData output before you convert it to a hex string. Take that data, and use the category method shown in this question (the one with the most upvotes, that actually has the code inlined)