Skip to content

Commit

Permalink
Fix 0x82 and 0x8303
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4n1um committed May 25, 2019
1 parent c8aa8e7 commit f384c8b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 59 deletions.
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Bytecode | Type | Note
`0x42` | double | [Double-precision floating-point](https://en.wikipedia.org/wiki/Double-precision_floating-point_format)
`0x44` | [uRTON_t](#unsigned-rton-number) | unsigned RTON number
`0x45` | [RTON_t](#rton-number) | RTON number
`0x81` | [RTON string](#0x81) |
`0x82` | [RTON string](#0x82) |
`0x81` | [String](#string) |
`0x82` | [Utf-8 string](#utf-8-string) |
`0x8303` | [RTID](#rtid) | RTON ID
`0x85` | [Object](#object) |
`0x86fd` | [Array](#array) |
Expand All @@ -41,8 +41,8 @@ Bytecode | Type | Note
`0xfe` | [End of array](#array) |
`0xff` | [End of object](#end-of-object) |

### Unsigned RTON Number
#### `0x24`, `0x28` and `0x44`
## Unsigned RTON Number
### `0x24`, `0x28` and `0x44`
* It read 1 byte at a time and keep reading til it found a byte that SMALLER or EQUAL `0x7f`

* After that it do something like this pseudocode:
Expand Down Expand Up @@ -81,29 +81,30 @@ Bytecode | Type | Note
}
```
### RTON Number
#### `0x25`, `0x29` and `0x45`
## RTON Number
### `0x25`, `0x29` and `0x45`
* Pseudocode:
```cpp
RTON_number = unsigned_RTON_number;
if (RTON_number % 2) RTON_number = -(RTON_number + 1);
RTON_number /= 2;
```
### String
#### `0x81`
## String
### `0x81`
* `81 xx [string]` create a `[string]` that has EXACTLY `xx` **unsigned RTON number** of bytes.
#### `0x82`
* `82 [L1] [L2] [string]` where `[L1]` = `[L2]` = **unsigned RTON number** length of `[string]`.
## UTF-8 String
### `0x82`
* `82 [L1] [L2] [string]` where `[L1]` is **unsigned RTON number** characters in utf-8 and `[L2]` is **unsigned RTON number** bytes of `[string]`.
### RTID
#### `0x8303`
## RTID
### `0x8303`
* `0x8303` begin the RTID (RTON ID???) of RTON (cross-reference???).
* It has 2 strings after the RTID: `RTID(2nd_string@1st_string)`.
* After `0x8303` is 2 strings format: `[L1] [L2] [string]` where `[L1]` = `[L2]` = **unsigned RTON number** length of `[string]`.
* After `0x8303` is 2 strings format: `[L1] [L2] [string]` where `[L1]` is **unsigned RTON number** characters in utf-8 and `[L2]` is **unsigned RTON number** bytes of `[string]`.
* Example:
```
Expand All @@ -122,8 +123,8 @@ Bytecode | Type | Note
}
```
### Object
#### `0x85`
## Object
### `0x85`
* Create a object as value
* Example:
Expand All @@ -145,8 +146,8 @@ Bytecode | Type | Note
}
```
### Array
#### `0x86fd`, `0xfe`
## Array
### `0x86fd`, `0xfe`
* Array begin with `0x86fd xx` and end with `0xfe`, where `xx` is the number of elements in array.
* Example:
Expand All @@ -172,8 +173,8 @@ Bytecode | Type | Note
}
```
### Cached String
#### `0x90` and `0x91`
## Cached String
### `0x90` and `0x91`
* `90 xx [string]` create a `[string]` that has EXACTLY `xx` **unsigned RTON number** of bytes.
* By using `0x90`, the string will push in a stack then it can be recall by `91 xx`, `xx` is **unsigned RTON number**-th element in the stack (starting from 0). Let's call the stack is ASCII_CACHE.
Expand All @@ -198,8 +199,8 @@ Bytecode | Type | Note
}
```
### Cached UTF-8 String
#### `0x92` and `0x93`
## Cached UTF-8 String
### `0x92` and `0x93`
* Very much like the **Cached String**, `0x92` and `0x93` different is use the utf-8 encode
* `92 xx yy [string]` create a utf-8 `[string]` that has EXACTLY `xx` **unsigned RTON number** of utf-8 characters with `yy` **unsigned RTON number** bytes.
Expand Down Expand Up @@ -227,8 +228,8 @@ Bytecode | Type | Note
}
```
### End of Object
#### `0xff`
## End of Object
### `0xff`
* Mark end of an object.
* Example:
Expand Down
25 changes: 15 additions & 10 deletions rton-json/json2rton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ int write_unsigned_RTON_num(std::vector <uint8_t> a){
return 0;
}

///https://en.wikipedia.org/wiki/UTF-8#Examples
int get_utf8_size(std::string q){
int utf8_size = 0;
for (uint8_t i : q){
if (i <= 0177) utf8_size += 1;
if (i >= 0302 && i <= 0337) utf8_size += 1;
if (i >= 0340 && i <= 0357) utf8_size += 1;
if (i >= 0360 && i <= 0364) utf8_size += 1;
}
return utf8_size;
}

int write_RTON_block(json js){
switch(js.type()){
//null
Expand Down Expand Up @@ -85,23 +97,16 @@ int write_RTON_block(json js){
//get 2 strings
std::string first_string = temp.substr(temp.find("@") + 1),
second_string = temp.substr(0, temp.find("@"));
write_unsigned_RTON_num(int2unsigned_RTON_num(first_string.size()));
write_unsigned_RTON_num(int2unsigned_RTON_num(get_utf8_size(first_string)));
write_unsigned_RTON_num(int2unsigned_RTON_num(first_string.size()));
output << first_string;
write_unsigned_RTON_num(int2unsigned_RTON_num(second_string.size()));
write_unsigned_RTON_num(int2unsigned_RTON_num(get_utf8_size(second_string)));
write_unsigned_RTON_num(int2unsigned_RTON_num(second_string.size()));
output << second_string;
}
//normal string
else{
///https://en.wikipedia.org/wiki/UTF-8#Examples
int utf8_size = 0;
for (uint8_t i : temp){
if (i <= 0177) utf8_size += 1;
if (i >= 0302 && i <= 0337) utf8_size += 1;
if (i >= 0340 && i <= 0357) utf8_size += 1;
if (i >= 0360 && i <= 0364) utf8_size += 1;
}
int utf8_size = get_utf8_size(temp);
//ascii
if (utf8_size == temp.size()){
auto it = std::find(stack_0x91.begin(), stack_0x91.end(), temp);
Expand Down
2 changes: 1 addition & 1 deletion rton-json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ template<class K, class V, class dummy_compare, class A>
using workaround_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
using json = nlohmann::basic_json<workaround_fifo_map>;

const std::string ver = "2.3.0";
const std::string ver = "2.3.1";

json json_decode();
int rton_encode();
Expand Down
38 changes: 13 additions & 25 deletions rton-json/rton2json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,11 @@ json read_RTON_block(){
case 0x82:{
//get string buffer
uint64_t s_buffer = unsigned_RTON_num2int(read_RTON_num());
uint64_t s_buffer_check = unsigned_RTON_num2int(read_RTON_num());
char* s;
if (s_buffer == s_buffer_check){
s = new char [s_buffer + 1];
input.read(s, s_buffer);
s[s_buffer] = 0;
res.push_back(std::string(s));
}
else bytecode_error();
s_buffer = unsigned_RTON_num2int(read_RTON_num());
char s[s_buffer +1];
input.read(s, s_buffer);
s[s_buffer] = 0;
res.push_back(std::string(s));
break;
}
//RTID
Expand All @@ -218,24 +214,16 @@ json read_RTON_block(){
if (check == 0x3){
//get 1st string
uint64_t s1_buffer = unsigned_RTON_num2int(read_RTON_num());
uint64_t s1_buffer_check = unsigned_RTON_num2int(read_RTON_num());
char* s1;
if (s1_buffer == s1_buffer_check){
s1 = new char [s1_buffer + 1];
input.read(s1, s1_buffer);
s1[s1_buffer] = 0;
}
else bytecode_error();
s1_buffer = unsigned_RTON_num2int(read_RTON_num());
char s1[s1_buffer + 1];
input.read(s1, s1_buffer);
s1[s1_buffer] = 0;
//get 2nd string
uint64_t s2_buffer = unsigned_RTON_num2int(read_RTON_num());
uint64_t s2_buffer_check = unsigned_RTON_num2int(read_RTON_num());
char* s2;
if (s2_buffer == s2_buffer_check){
s2 = new char [s2_buffer + 1];
input.read(s2, s2_buffer);
s2[s2_buffer] = 0;
}
else bytecode_error();
s2_buffer = unsigned_RTON_num2int(read_RTON_num());
char s2[s2_buffer + 1];
input.read(s2, s2_buffer);
s2[s2_buffer] = 0;
res.push_back(std::string() + "RTID(" + s2 + '@' + s1 + ')');
}
else bytecode_error();
Expand Down

0 comments on commit f384c8b

Please sign in to comment.