Build a huffman tree from a code table - c++

I am confused about how to build a huffman tree from a code table. The code table consists of 2 columns, the string code (binary presentation) and symb (the hexadecimal value)
the symbCode struct:
struct symbCode
{
char symb;
string code; //string of '0' and '1'
};
the Function:
void huffmanTree::buildTreeFromCodeTable(symbCode *table, int n)
{
//construct the Huffman tree from the code table
//n = number of symbols in the code table
}
I have googled a few websites providing tutorials for huffman tree. But I still can't figure it out.
Should i new a tree node or do something else?
The reference table:
Num_Alphabet 96
ASCII Huffman_Code
a 011000
20 0100
21 11101110110
22 1110111010
23 11101111000
24 11101111001
25 11110100001
26 0000010000
27 0000010001
28 100100100
29 100100101
2a 000000100
2b 000000101
2c 0110010
2d 101101000
2e 1110010
2f 0000000110
30 11110101
31 11110110
32 11110111
33 11111010
34 11111011
35 11111100
36 11111101
37 11111110
38 11111111
39 0000011
3a 101101001
3b 01100110
3c 000001001
3d 00000011
3e 000000000
3f 01100111
40 0000000111
41 00011
42 1110011
43 001010
44 011010
45 01010
46 001000
47 1110110
48 1001000
49 10101
4a 0010011
4b 1000001
4c 011111
4d 100001
4e 010110
4f 00010
50 1111100
51 00000101
52 010111
53 10100
54 110000
55 110011
56 10010011
57 100000010
58 000000001
59 10110101
5a 000000010
5b 11101111100
5c 11101111101
5d 11101111110
5e 11101111111
5f 11101110010
60 11101110011
61 10001
62 100101
63 110001
64 110010
65 10011
66 101111
67 101100
68 011110
69 11010
6a 001011
6b 011011
6c 111100
6d 00001
6e 111010
6f 01110
70 101110
71 111101001
72 111000
73 11011
74 00110
75 00111
76 0010010
77 10000000
78 100000011
79 1011011
7a 1111010001
7b 1110111101
7c 11110100000
7d 1110111000
7e 11101110111

Use binary tree, and for every node you have two path: to left child, and to right child. To left child means 0, to right child means 1. After whole tree is completed, from root to a leaf node, the path (sequenct of 1s and 0s) is the code of the value in leaf node.
So, every code in your table is actually a path from root to leaf. Pick codes one by one, check the path from root(and the code from left), if not exist, creat all nodes (including leaf); if partially exist(leftist numbers in code are same), complete the path to leaf.

Related

How can you download the feed processing report from a S3 presigned URL (Amazon Feed API)

I am using the below code
Step 6 from the link
https://github.com/amzn/selling-partner-api-docs/blob/main/guides/use-case-guides/feeds-api-use-case-guide-2020-09-04.md#step-5-get-information-for-retrieving-the-feed-processing-report
string path = Path.GetTempPath();
IRestRequest restRequest = new RestRequest(Method.GET);
RestClient restClient = new RestClient(presignedurl);
byte[] response = restClient.DownloadData(restRequest);
File.WriteAllBytes(#path + "\\words123.txt" , response);
The txt file returns Signature mismatch Error
SignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method.AWS4-HMAC-SHA256
20210127T204937Z
20210127/eu-west-1/s3/aws4_request
41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 32 31 30 31 32 37 54 32 30 34 39 33 37 5a 0a 32 30 32 31 30 31 32 37 2f 65 75 2d 77 65 73 74 2d 31 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 64 66 34 62 62 66 61 65 66 64 66 34 30 39 30 36 38 64 32 31 37 64 33 35 62 39 30 64 39 39 38 65 62 64 61 37 39 31 61 64 65 33 66 38 36 66 32 38 61 30 64 34 65 62 34 35 37 65 66 37 63 62 37 33GET
//NinetyDays/amzn1.tortuga.3.3bec27f6-e87b-493a-ae7f-fab8a639382f.T2P4950ASWH2IA
X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=%2F20210127%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20210127T204937Z&X-Amz-Expires=300&X-Amz-SignedHeaders=content-type%3Bhost
content-type:
host:tortuga-prod-eu.s3-eu-west-1.amazonaws.com
content-type;host
UNSIGNED-PAYLOAD47 45 54 0a 2f 2f 4e 69 6e 65 74 79 44 61 79 73 2f 61 6d 7a 6e 31 2e 74 6f 72 74 75 67 61 2e 33 2e 33 62 65 63 32 37 66 36 2d 65 38 37 62 2d 34 39 33 61 2d 61 65 37 66 2d 66 61 62 38 61 36 33 74 2d 31 2e 61 6d 61 7a 6f 6e 61 77 73 2e 63 6f 6d 0a 0a 63 6f 6e 74 65 6e 74 2d 74 79 70 65 3b 68 6f 73 74 0a 55 4e 53 49 47 4e 45 44 2d 50 41 59 4c 4f 41 44DD34A4A3A3CC0692AkCloh5JBLZKUaoKBjA9hFvoLYKjT+SG6E+oMy1pDHRhS7miIYw6/91bBN4eJCuGp9fkpTiEcPg=
Just for security purposes always redact the names of the private resource like in this acute case your s3 bucket name
From the error seems like your presignedUrl is not correct. This specific part.
X-Amz-SignedHeaders=content-type%3Bhost content-type: host:tortuga-prod-eu.s3-eu-west-1.amazonaws.com content-type;host UNSIGNED-PAYLOAD47 45 54 0a
this guide Signing AWS requests with Signature Version 4 can help you signing step of the request by step just to understand the process.
In end, you have a URL like:
https://iam.amazonaws.com?Action=ListUsers&Version=2010-05-08&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIDEXAMPLE%2F20150830%2Fus-east-1%2Fiam%2Faws4_request&X-Amz-Date=20150830T123600Z&X-Amz-Expires=60&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Signature=37ac2f4fde00b0ac9bd9eadeb459b1bbee224158d66e7ae5fcadb70b2d181d02
Not sure if checked this Generating a presigned object URL, there is a nice example of java.

Node, React Native Fetch Blob, S3 Upload invalid signature

So I've been staring at this for a while and I can't obviously see anything wrong, I'm grabbing a signedURL from my Lambda and then uploading to it using rn-fetch-blob.
Lambda Code is as follows:
export default async (event, context, callback, utils) => {
const { imageName } = JSON.parse(event.body)
console.log('​imageName', imageName)
// These access keys relate to a user with AdministratorAccess
utils.AWS.config.update({
accessKeyId: 'XXXXX',
secretAccessKey: 'XXXXXXX',
})
let s3 = new utils.AWS.S3({ signatureVersion: 'v4' })
let params = { Bucket: 'MY_BUCKET', Key: imageName, Expires: 60, ContentType: 'image/jpeg' }
let url = await s3.getSignedUrl('putObject', params)
callback(null, utils.responder.success({ url.data.url }))
}
React native code is as follows:
const uploadImageToS3Endpoint = (s3Url, imageUrl) => {
var source = imageUrl.replace('file://', '')
return RNFetchBlob.fetch('POST', s3Url, {
'Content-Type': 'image/jpeg'
}, RNFetchBlob.wrap(source))
}
I get back(along with a load of other stuff, edited for brevity)
The request signature we calculated does not match the signature you provided. Check your key and signing method.
I did also try allocating public write to the S3 bucket to prove out it wasn't a permissions issue, still got the same result. Any advice would be greatly appreciated as this should be simple!
EDIT
AWS Response RN Fetch
"<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>THE_KEY</AWSAccessKeyId><StringToSign>AWS4-HMAC-SHA256
20181230T120956Z
20181230/eu-west-2/s3/aws4_request
b7b755c6335c0401711fafa241bbd816b5c7ad225c41cc324b0daaac2ee9f587</StringToSign><SignatureProvided>5345073e95a1dd39fa28f0a3c5c7350b2d7da75a5dedf3c2a895fdbd0e354961</SignatureProvided><StringToSignBytes>41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 31 38 31 32 33 30 54 31 32 30 39 35 36 5a 0a 32 30 31 38 31 32 33 30 2f 65 75 2d 77 65 73 74 2d 32 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 62 37 62 37 35 35 63 36 33 33 35 63 30 34 30 31 37 31 31 66 61 66 61 32 34 31 62 62 64 38 31 36 62 35 63 37 61 64 32 32 35 63 34 31 63 63 33 32 34 62 30 64 61 61 61 63 32 65 65 39 66 35 38 37</StringToSignBytes><CanonicalRequest>POST
/C1E2DB45-CB94-4D3C-AEA7-C1CE4B42FCF1.jpg
Content-Type=image%2Fjpeg&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJMVYRTCLXJGL2JGQ%2F20181230%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20181230T120956Z&X-Amz-Expires=60&X-Amz-SignedHeaders=host
host:tthsshopproductimages.s3.eu-west-2.amazonaws.com
host
UNSIGNED-PAYLOAD</CanonicalRequest><CanonicalRequestBytes>50 4f 53 54 0a 2f 43 31 45 32 44 42 34 35 2d 43 42 39 34 2d 34 44 33 43 2d 41 45 41 37 2d 43 31 43 45 34 42 34 32 46 43 46 31 2e 6a 70 67 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3d 69 6d 61 67 65 25 32 46 6a 70 65 67 26 58 2d 41 6d 7a 2d 41 6c 67 6f 72 69 74 68 6d 3d 41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 26 58 2d 41 6d 7a 2d 43 72 65 64 65 6e 74 69 61 6c 3d 41 4b 49 41 4a 4d 56 59 52 54 43 4c 58 4a 47 4c 32 4a 47 51 25 32 46 32 30 31 38 31 32 33 30 25 32 46 65 75 2d 77 65 73 74 2d 32 25 32 46 73 33 25 32 46 61 77 73 34 5f 72 65 71 75 65 73 74 26 58 2d 41 6d 7a 2d 44 61 74 65 3d 32 30 31 38 31 32 33 30 54 31 32 30 39 35 36 5a 26 58 2d 41 6d 7a 2d 45 78 70 69 72 65 73 3d 36 30 26 58 2d 41 6d 7a 2d 53 69 67 6e 65 64 48 65 61 64 65 72 73 3d 68 6f 73 74 0a 68 6f 73 74 3a 74 74 68 73 73 68 6f 70 70 72 6f 64 75 63 74 69 6d 61 67 65 73 2e 73 33 2e 65 75 2d 77 65 73 74 2d 32 2e 61 6d 61 7a 6f 6e 61 77 73 2e 63 6f 6d 0a 0a 68 6f 73 74 0a 55 4e 53 49 47 4e 45 44 2d 50 41 59 4c 4f 41 44</CanonicalRequestBytes><RequestId>4E4DFD848923AC27</RequestId><HostId>DljdK6KPnzAeXxwUyYu32gb4g4JRI8kDTsdqZVqcM3wLYBsZ6kfT8UGZq6FI5/VimHdY6iL8eKg=</HostId></Error>"
RNFetchBlob.fetch(
'PUT',
link,
{
'Content-Type': undefined
},
RNFetchBlob.wrap(Platform.OS === "android" ? this.selectedImage.uri : this.selectedImage.uri.replace("file://", "")),
)
.then((resp) => {
console.log("Response data", resp)
this.onImageUploadSuccess(resp)
}).catch((err) => {
this.onUploadFailure(err)
})
This works for me

Parsing .csv files with CR LF EOL structure

I'm trying to parse a CSV file and getline() is reading the entire file as one line. On the assumption that getline() wasn't getting what it expected, I tried \r, \n, \n\r, \r\n, and \0 as arguments with no luck.
I took a look at the EOL characters and an seeing CR and then LF. Is getline() just ignoring this or am I missing something? Also, what's the fix here?
The goal of this function is a general purpose CSV parsing function that stores the data as a 2d vector of strings. Although advice on that front is welcome, I'm only looking for a way to fix this issue.
vector<vector<string>> Parse::parseCSV(string file)
{
// input fstream instance
ifstream inFile;
inFile.open(file);
// check for error
if (inFile.fail()) { cerr << "Cannot open file" << endl; exit(1); }
vector<vector<string>> data;
string line;
while (getline(inFile, line))
{
stringstream inputLine(line);
char delimeter = ',';
string word;
vector<string> brokenLine;
while (getline(inputLine, word, delimeter)) {
word.erase(remove(word.begin(), word.end(), ' '), word.end()); // remove all white spaces
brokenLine.push_back(word);
}
data.push_back(brokenLine);
}
inFile.close();
return data;
};
Here's the hexdump. I'm not sure what exactly this is showing.
0000000 55 4e 49 58 20 54 49 4d 45 2c 54 49 4d 45 2c 4c
0000010 41 54 2c 4c 4f 4e 47 2c 41 4c 54 2c 44 49 53 54
0000020 2c 48 52 2c 43 41 44 2c 54 45 4d 50 2c 50 4f 57
0000030 45 52 0d 31 34 32 34 31 30 35 38 30 38 2c 32 30
0000040 31 35 2d 30 32 2d 31 36 54 31 36 3a 35 36 3a 34
0000050 38 5a 2c 34 33 2e 38 39 36 34 2c 31 30 2e 32 32
0000060 34 34 34 2c 30 2e 38 37 2c 30 2c 30 2c 30 2c 4e
0000070 6f 20 44 61 74 61 2c 4e 6f 20 44 61 74 61 0d 31
0000080 34 32 34 31 30 35 38 38 35 2c 32 30 31 35 2d 30
0000090 32 2d 31 36 54 31 36 3a 35 38 3a 30 35 5a 2c 34
00000a0 33 2e 39 30 31 33 35 2c 31 30 2e 32 32 30 34 31
00000b0 2c 31 2e 30 32 2c 30 2e 36 33 39 2c 30 2c 30 2c
00000c0 4e 6f 20 44 61 74 61 2c 4e 6f 20 44 61 74 61 0d
00000d0 31 34 32 34 31 30 35 38 38 38 2c 32 30 31 35 2d
00000e0 30 32 2d 31 36 54 31 36 3a 35 38 3a 30 38 5a 2c
00000f0 34 33 2e 39 30 31 34 38 2c 31 30 2e 32 32 30 31
0000100
The first two lines of the file
UNIX TIME,TIME,LAT,LONG,ALT,DIST,HR,CAD,TEMP,POWER
1424105808,2015-02-16T16:56:48Z,43.8964,10.22444,0.87,0,0,0,No Data,No Data
UPDATE Looks like it was \r. Im not sure why it didn't work earlier, but I learned a few things while exploring. Thanks for the help guys.
A simple fix would be to write your own getline
For example one that ignores any combination of \n,\r
in the beginning of the line, and breaking on any too.
That will work on any platform, but wont preserve empty lines.
After looking at the hex-dump, the delimiter is 0d (\r)
Did you try to switch the order of the \r\n to \n\r?

POP3 Authentication

I am trying to write a simple POP3 server and I have a problem with authentication.
I run the server and try to connect to it with help of Icedove. When the client connects to the server, the following occurs:
Server:+OK POP3 server ready.\r\n
Client:CAPA\r\n
Server:-ERR incorrect operation\r\n
Client:USER sti#blabla.ru\r\n
Server:+OK please send PASS command\r\n
Client:PASS sadsd\r\n
Server:+OK sti#blabla.ru is welcome here\r\n
Client:QUIT\r\n
socket closed
I can't understand why client close connection after "+OK sti#blabla.ru is welcome here\r\n" and just says me that the user name or pass incorrect.
I am trying to experiment so new log is slightly different.
Wireshark log:
00000000 2b 4f 4b 20 50 4f 50 33 20 73 65 72 76 65 72 20 +OK POP3 server
00000010 72 65 61 64 79 2e 0d 0a ready...
00000000 41 55 54 48 0d 0a AUTH..
00000018 2d 45 52 52 20 69 6e 63 6f 72 72 65 63 74 20 6f -ERR inc orrect o
00000028 70 65 72 61 74 69 6f 0d 0a peratio. .
00000006 43 41 50 41 0d 0a CAPA..
00000031 2d 45 52 52 20 69 6e 63 6f 72 72 65 63 74 20 6f -ERR inc orrect o
00000041 70 65 72 61 74 69 6f 0d 0a peratio. .
0000000C 55 53 45 52 20 73 74 69 0d 0a USER sti ..
0000004A 2b 4f 4b 20 70 6c 65 61 73 65 20 73 65 6e 64 20 +OK plea se send
0000005A 50 41 53 53 20 63 6f 6d 6d 61 6e 64 0d 0a PASS com mand..
00000016 50 41 53 53 20 73 61 64 73 64 0d 0a PASS sad sd..
00000068 2b 4f 4b 20 73 74 69 27 73 20 6d 61 69 6c 64 72 +OK sti' s maildr
00000078 6f 70 20 68 61 73 20 35 20 6d 65 73 73 61 67 65 op has 5 message
00000088 73 20 28 35 35 20 6f 63 74 65 74 73 29 0d 0a s (55 oc tets)..
00000022 51 55 49 54 0d 0a QUIT..

Clean Hex dump of ASCII code

I've got a simple PDF file in Hex dump format, lines looking like this:
00000000: 25 50 44 46 2d 31 2e 34 0d 0a 25 e2 e3 cf d3 0d %PDF-1.4..%.....
00000010: 0a 31 20 30 20 6f 62 6a 0d 0a 3c 3c 20 2f 46 69 .1 0 obj..<< /Fi
00000020: 6c 74 65 72 20 2f 46 6c 61 74 65 44 65 63 6f 64 lter /FlateDecod
00000030: 65 20 2f 4c 65 6e 67 74 68 20 31 38 34 30 36 20 e /Length 18406
00000040: 3e 3e 0d 0a 73 74 72 65 61 6d 0d 0a 2b 2c 9c 77 >>..stream..+,.w
How can I clean up the file so I'm left with the Hex values only?
I'm guessing regular expressions but I fail to apply it successfully in an editor.
Am running on a Windows machine.
Notepad++
Ctrl+F -> Replace
- Tick Regular Expression in the bottom left
Find: ^(.{73}).*$
Replace With: \1
Or Alt+Drag for column selection & copy/pasta.