protobuf c string decode - c

Experts, I need your help in decoding the string value in the protobuf-c message. Say the message
m { optional string id =0 }
the string is stored with a value "test", by using the C api m.id = "test" and pack it using m__pack(&m,buf); the message stream will look like 0a 04 74 65 73 74, where my string value is 74 65 73 74 in ascii format. I try to get my string value back by using api m__unpack(NULL,length,buf) where buf contains the stream. Now when I try to print the string printf("%s\n",msg->id) I get seg fault. Shouldn't it print the ascii values(74 65 73 74)? Can you please help in getting the string value. Thanks for the help.

solved..
its my mistake, i was printing the msg->id after the free_unpack api, hence the issue.
thanks for your time.

Related

Parse a JSON object with the value as JSON string array

I am trying to parse a JSON string which is of the following format
{"edgeNodeRegistrationStatus": ["{\"CONFIRMED\":\"TRUE\"}"]}
I have written a code to parse it.
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText('{"edgeNodeRegistrationStatus": ["{\"CONFIRMED\":\"TRUE\"}"]}')
println(object["edgeNodeRegistrationStatus"][0])
I expect the code to print {"CONFIRMED":"TRUE"}. But its throwing an error
Caught: groovy.json.JsonException: expecting a ',' or a ']', but got
the current character of 'C' with an int value of 67 on array index of 1
The current character read is 'C' with an int value of 67
expecting a ',' or a ']', but got
the current character of 'C' with an int value of 67 on array index of 1
line number 1
index number 35
{"edgeNodeRegistrationStatus": ["{"CONFIRMED":"TRUE"}"]}
...................................^
groovy.json.JsonException: expecting a ',' or a ']', but got
the current character of 'C' with an int value of 67 on array index of 1
The current character read is 'C' with an int value of 67
expecting a ',' or a ']', but got
the current character of 'C' with an int value of 67 on array index of 1
line number 1
index number 35
{"edgeNodeRegistrationStatus": ["{"CONFIRMED":"TRUE"}"]}
...................................^
at jdoodle.run(jdoodle.groovy:4)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Command exited with non-zero status 1
Using \" inside a ''-String will give you just " inside the string itself (same as in a ""-String). But you want to quote the \" for the JSON (not groovy). So you need to use \\" instead.
Unless you really want to have that string for testing, you are better off just generating the JSON you expect there in your code. So you don't have to battle that. E.g.
JsonOutput.toJson([edgeNodeRegistrationStatus: [JsonOutput.toJson([CONFIRMED: "TRUE"])]])
Or, you can use a different String delimiter, thusly:
def text = $/{"edgeNodeRegistrationStatus": ["{\"CONFIRMED\":\"TRUE\"}"]}/$
def object = jsonSlurper.parseText(text)
println object.edgeNodeRegistrationStatus[0]

How to decode (non-standard) alphanumeric SMS Sender ID?

I'm receiving text messages (SMS) via GSM modem and processing them automatically with a custom script. How do I decode an alphanumeric sender ID?
Example:
Sender ID = +6;91<06130
...which should read "Holvi" (if I'm not mistaken).
I guess there is something like a translation table which converts this number to an alphanumeric string.
6; = H
91 = o
<0 = l
61 = v
30 = i
So far, so good. Sadly I cannot find any information about this special encoding. Also, I could not find any relation to a standard ASCII table (maybe special characters like ; or < could be substitutes for hexadecimal a-f).
Any ideas?
How do you read the message ? Is it already somehow decoded for this output ?
Alphanumeric sender number are usually by SMS standard GSM7 encoded, for example, "Halvi" would be in hex format: 0AC837DB9E06

printf not working after executing external function

I have the following c code which calls a cobol program:
#include <stdio.h>
#include "libcob.h"
//#pragma linkage (verkoop, COBOL)
extern void VERKOOP(char *productid, char *aantal, char*resultaat);
main(int argc, char *argv[])
{
int return_status;
COB_RTD = cob_get_rtd();
char *productid = "20 ";
char *aantal = "000020";
char resultaat[30];
cob_init(rtd, 0, NULL);
printf("hallo");//prints
VERKOOP(productid, aantal, resultaat);
printf("hallo");//doesn't print
printf("resultaat:%s", resultaat);// doesn't print
cob_stop_run (rtd, return_status);
}
I'm using the printf to see if resultaat has been assigned correctly. However, both of the lines after VERKOOP don't print for some reason.
This is the COBOL code of VERKOOP(he does fill in LS-RESULTAAT correctly here, I tried it with DISPLAY and this part works):
*************************************************************
* VERKOOP
*************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. VERKOOP.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PRODUCTEN ASSIGN TO "BESTANDEN/LIJSTPRODUCTEN"
ACCESS MODE IS RANDOM
ORGANIZATION IS INDEXED
RECORD KEY IS PRODUCTID
FILE STATUS IS WS-FILE-STATUS.
DATA DIVISION.
FILE SECTION.
FD PRODUCTEN BLOCK CONTAINS 10 RECORDS.
01 PRODUCT.
02 PRODUCTID PIC X(6).
02 LEVERANCIERID PIC X(6).
02 AANTAL PIC 9(6).
WORKING-STORAGE SECTION.
77 FOUT PIC X.
88 PRODUCT-NIET-GEVONDEN VALUE 1.
77 WS-PRODUCTID PIC X(6).
77 WS-AANTAL PIC 9(6).
77 WS-FILE-STATUS PIC XX.
77 WS-RESULTAAT PIC X(30).
LINKAGE SECTION.
01 LS-PRODUCTID PIC X(6).
01 LS-AANTAL PIC 9(6).
01 LS-RESULTAAT PIC X(30).
PROCEDURE DIVISION USING LS-PRODUCTID, LS-AANTAL, LS-RESULTAAT.
MAIN.
PERFORM INITIALISEER
PERFORM LEES-PRODUCT-IN
PERFORM LEES-BESTAND
PERFORM SLUIT-BESTAND
STOP RUN.
INITIALISEER.
OPEN I-O PRODUCTEN.
* DISPLAY WS-FILE-STATUS..
LEES-PRODUCT-IN.
MOVE LS-PRODUCTID TO WS-PRODUCTID
MOVE LS-AANTAL TO WS-AANTAL
MOVE 'OK' TO WS-RESULTAAT
* DISPLAY WS-RESULTAAT
MOVE WS-RESULTAAT TO LS-RESULTAAT.
* DISPLAY "GEEF PRODUCTID OP: "
* ACCEPT WS-PRODUCTID
* DISPLAY "GEEF AANTAL OP: "
* ACCEPT WS-AANTAL.
LEES-BESTAND.
* DISPLAY "LEES-BESTAND"
MOVE WS-PRODUCTID TO PRODUCTID
* DISPLAY PRODUCTID
* DISPLAY WS-FILE-STATUS
READ PRODUCTEN INVALID KEY SET PRODUCT-NIET-GEVONDEN TO TRUE
END-READ
IF PRODUCT-NIET-GEVONDEN PERFORM FOUTJE
ELSE
* MOVE WS-PRODUCTID TO PRODUCTID
SUBTRACT WS-AANTAL FROM AANTAL
PERFORM UPDATE-PRODUCT
END-IF.
UPDATE-PRODUCT.
REWRITE PRODUCT INVALID KEY PERFORM FOUTJE.
SLUIT-BESTAND.
* DISPLAY "SLUIT-BESTAND"
CLOSE PRODUCTEN.
FOUTJE.
DISPLAY "ER IS EEN FOUT OPGETREDEN"
DISPLAY WS-FILE-STATUS
STOP RUN.
UPDATE: I tried removing both "STOP RUN's", however now for some reason he substracts 40 instead of 20 and prints "ER IS EEN FOUT OPGETREDEN". So he is running the COBOL Program twice for some reason.
UPDATE: After replacing STOP RUN by GOBACK it works perfectly
Your STOP RUN is returning here, cob_stop_run (rtd, return_status), so your prior code after the invocation of the COBOL program does not run.
If EXIT PROGRAM is in a "main" program (at least in the case of the pragma-usage it seems that is what you have) then it is treated the same as STOP RUN.
GOBACK is returning control to where you expect it to.
If you use the COBOL-IT API, as has already been suggested, then perhaps the EXIT PROGRAM will behave differently. Perhaps not.
You are using an undocumented way to call a COBOL-IT program. Exactly how it behaves is not known, and cannot be known, to someone without COBOL-IT and the same operating system as you have, and the patience to do something in a non-obvious way.
If things are suggested which you then ignore, it is difficult to keep answering your questions.
Again, you have an Assignment. The Assignment expects you to use the API. You should use the API and get your programs working. If you have time later, you can look at the pragma-usage to effect the interoperability.

What are some good ways to compress data across time?

I have an array of objects with time and value property. Looks something like this.
UPDATE: dataset with epoch times rather than time strings
[{datetime:1383661634, value: 43},{datetime:1383661856, value: 40}, {datetime:1383662133, value: 23}, {datetime:1383662944, value: 23}]
The array is far larger than this. Possibly a 6 digit length. I intend to build a graph to represent this array. Due to obvious reasons, I cannot use every bit of the data to build this graph (value vs time); so I need to normalize it across time.
So here's the main problem - There is no trend in the timestamp for these objects; so I need to dynamically choose slots of time in which I either average out the values or show counts of objects in that slot.
How can I calculate slots that user friendly. i.e per minute, hour, day, eight hours or so. I am looking at having a maximum of 25 slots done out of the array, which I show up on the graph.
I hope this helps get my point through.
You can convert the date/time into epoch and use numpy.histogram to get the ranges:
import random, numpy
l = [ random.randint(0, 1000) for x in range(1000) ]
num_items_bins, bin_ranges = numpy.histogram(l, 25)
print num_items_bins
print bin_ranges
Gives:
[34 38 42 41 43 50 34 29 37 46 31 47 43 29 30 42 38 52 42 44 42 42 51 34 39]
[ 1. 40.96 80.92 120.88 160.84 200.8 240.76 280.72
320.68 360.64 400.6 440.56 480.52 520.48 560.44 600.4
640.36 680.32 720.28 760.24 800.2 840.16 880.12 920.08
960.04 1000. ]
Hard to say without knowing the nature of your values, compressing values for display is a matter of what you can afford to discard and what you can't. Some ideas though:
histogram
candlestick chart
Is this JSON and the DateTimes transmitted as text?
Why not transmit the Date as a long (Int64), and use a method to convert to/from DateTime? Depending on which language you could use these implementations:
DateTime to Long in C#
Date to long using Unix timestamp in Java
That alone would save you a considerable amount of space, since strings are 16-bits per character and the long TimeStamp would be just 64 bits.

HMAC-MD5 using openSSL

I'm trying to study NTLMv2 response, Eric Glass's work.
but stuck at HMAC-MD5 section. (using function like this)
I got the right NTLM hash, and unicode username & domain.
(he only emphasize the "USERDOMAIN" but no username, so I suppose it's "USER")
unsigned char v1hash[16]; // "0xcd06ca7c7e10c99b1d33b7485a2ed808"
unsigned short udata[14];
// concated unicode USER+USERDOMAIN "0x550053004500520044004f004d00410049004e00"
unsigned char v2hash[16];
int iLen;
HMAC(EVP_md5(), v1hash, 16, udata, sizeof(udata), v2hash, &iLen);
but the result is:
v2hash(16): 23 d2 3c a4 dd 1a 20 81 35 cf 3a 42 1c e1 5a 17
which should be "0x04b8e0ba74289cc540826bab1dee63ae"
am I doing something wrong here?
I'm not a C programmer but...
From my reading of that article USERDOMAIN should be just that - NOT USER+USERDOMAIN ie the uppercase username = "USER" concat with the target = "DOMAIN" to give USERDOMAIN which gives the unicode bytes beginning 55005300...
Then it would appear your udata array is the wrong size. You're only processing 20 bytes of information so don't set the array to 28 otherwise the HMAC/MD5 functions will be processing extra random data at the end of the input array.

Resources