From e7f611ea33c1a8c8165088db41f04c6a903018c8 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 10 Nov 2016 02:39:37 +0000 Subject: [PATCH] improved vcf parser (speed, security) svn path=/trunk/; revision=139 --- pages/cards.sh | 108 +++++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 52 deletions(-) diff --git a/pages/cards.sh b/pages/cards.sh index 08b88b9..0ead803 100755 --- a/pages/cards.sh +++ b/pages/cards.sh @@ -102,59 +102,63 @@ listcards() { } vcf_parse() { - tr -d '\n' <"$1" |sed -r 's:\r ::g;s:\r:\n:g' \ + sed -r ':X;N;$!bX; s;\r\n[ \t];;g; s;\r\n;\n;g;' "$1" \ | sed -rn ' - s:^X-MS-CARDPICTURE:PHOTO:p; - s:^X-GENDER:GENDER:p; - s:^X-ANNIVERSARY:ANNIVERSARY:p; - s:^X-EVOLUTION-ANNIVERSARY:ANNIVERSARY:p; - s:^X-KADDRESSBOOK-X-Anniversary:ANNIVERSARY:p; - s:^X-AIM(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:aim\:\2:p; - s:^X-ICQ(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:aim\:\2:p; - s:^X-GOOGLE-TALK(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:xmpp\:\2:p; - s:^X-JABBER(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:xmpp\:\2:p; - s:^X-MSN(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:msn\:\2:p; - s:^X-YAHOO(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:ymsgr\:\2:p; - s:^X-SIP(;[^"\:]+|;"[^"]+")*\:(sip\:)?(.*)$:IMPP\1\:sip\:\3:p; - s:^LABEL(;[^"\:]+|;"[^"]+")*\:(.*)$:ADR;LABEL="\2"\1\::p; - s:^X-EVOLUTION-BLOG-URL:URL:p; - - s:^AGENT:RELATED\;TYPE=agent:p; - s:^X-ASSISTANT:RELATED\;TYPE=assistant;VALUE=text:p; - s:^X-EVOLUTION-ASSISTANT:RELATED\;TYPE=assistant;VALUE=text:p; - s:^X-KADDRESSBOOK-X-AssistantsName:RELATED\;TYPE=assistant;VALUE=text:p; - s:^X-MANAGER:RELATED\;TYPE=manager;VALUE=text:p; - s:^X-EVOLUTION-MANAGER:RELATED\;TYPE=manager;VALUE=text:p; - s:^X-KADDRESSBOOK-X-ManagersName:RELATED\;TYPE=manager;VALUE=text:p; - s:^X-SPOUSE:RELATED\;TYPE=spouse;VALUE=text:p; - s:^X-EVOLUTION-SPOUSE:RELATED\;TYPE=spouse;VALUE=text:p; - s:^X-KADDRESSBOOK-X-SpouseName:RELATED\;TYPE=spouse;VALUE=text:p; - - s:^([A-Z].*)$:\1:p; - ' \ - | sed -r 's:^([^;\:]+)(;[^"\:]+|;"[^"]+")*\:(.*)$:key="\1"\nvalue="\3"\ntag=\2:g' \ - | while read -r line; do - case "$line" in - key=*) printf %s\\n "$line" - ;; - value=*) printf %s\\n "${line}" - ;; - tag=*) ot='' - printf %s "$line" \ - | sed -r 's:^tag=::;s:\;([A-Z+_-]+="[^"]+"|[A-Z+_-]+=[^\;]+):\n\1:g;' \ - | sed -rn 's:([A-Z+_-]+)="?(.+)"?:tag\[\1\]="\2":gp' \ - | sed -r '/^ *$/d' \ - | sort |while read -r tag; do - nt="$(printf %s "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\1:')" - nv="$(printf %s "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\2:')" - [ "$nt" = "$ot" ] && vl="$nv,$vl" || vl="$nv" - printf %s\\n "tag[$nt]=\"$vl\"" - ot="$nt" - done - ;; - esac - done \ - | sed -r 's:[\\$`]:\\&:g' + # === turn property names to upper case === + h; s;^([^\;:]+);;; + x; s;^([^\;:]+).*$;\1;; + y;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ; + G; s;\n;;; + + # === Normalise various known vendor properties === + s;^X-MS-CARDPICTURE(\;|:);PHOTO\1;; + s;^X-GENDER(\;|:);GENDER\1;; + s;^X-ANNIVERSARY(\;|:);ANNIVERSARY\1;; + s;^X-EVOLUTION-ANNIVERSARY(\;|:);ANNIVERSARY\1;; + s;^X-KADDRESSBOOK-X-ANNIVERSARY(\;|:);ANNIVERSARY\1;; + s;^X-EVOLUTION-BLOG-URL(\;|:);URL\1;; + s;^AGENT(\;|:);RELATED\;VALUE=text\;TYPE=agent\1;; + s;^X-ASSISTANT(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;; + s;^X-EVOLUTION-ASSISTANT(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;; + s;^X-KADDRESSBOOK-X-ASSISTANTSNAME(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;; + s;^X-MANAGER(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;; + s;^X-EVOLUTION-MANAGER(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;; + s;^X-KADDRESSBOOK-X-MANAGERSNAME(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;; + s;^X-SPOUSE(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;; + s;^X-EVOLUTION-SPOUSE(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;; + s;^X-KADDRESSBOOK-X-SPOUSENAME(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;; + + # === Normalise obsolete vendor IM properties === + s;^X-AIM((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;; + s;^X-ICQ((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;; + s;^X-GOOGLE-TALK((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;; + s;^X-JABBER((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;; + s;^X-MSN((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:msn:;; + s;^X-YAHOO((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:ymsgr:;; + s;^X-SIP((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):(sip:)?;IMPP\1:sip:;; + + # === Update obsolete LABEL property === + s;^LABEL((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):(.*)$;ADR\1\;LABEL="\5":;; + + /^([a-zA-Z0-9-]+)((\;[a-zA-Z0-9-]+=?|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):(.*)$/{ + h; + s;^([a-zA-Z0-9-]+)((\;[a-zA-Z0-9-]+=?|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):(.*)$;key='\''\1'\'';; + H; g; + s;^([a-zA-Z0-9-]+)((\;[a-zA-Z0-9-]+=?|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\6;; + s;'\'';'\'\\\\\'\'';g + s;^.*$;value='\''&'\''; + H; g; + s;^([a-zA-Z0-9-]+)((\;[a-zA-Z0-9-]+=?|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\2;; + s;'\'';'\'\\\\\'\'';g + s;\;([a-zA-Z0-9-]+)(=|=(([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*))?;\ntag[\1]='\''\3'\'';g + H; g; + s;^[^\n]*[\n ]+;; + + s;\n[\n ]+;\n;g; s;\n$;;; + + p; + } + ' } view_card() { #Parameter: Cardfile -- 2.39.2