dmap_parser.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #include "dmap_parser.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #define DMAP_STRINGIFY_(x) #x
  7. #define DMAP_STRINGIFY(x) DMAP_STRINGIFY_(x)
  8. typedef enum {
  9. DMAP_UNKNOWN,
  10. DMAP_UINT,
  11. DMAP_INT,
  12. DMAP_STR,
  13. DMAP_DATA,
  14. DMAP_DATE,
  15. DMAP_VERS,
  16. DMAP_DICT,
  17. DMAP_ITEM
  18. } DMAP_TYPE;
  19. typedef struct {
  20. /**
  21. * The four-character code used in the encoded message.
  22. */
  23. const char *code;
  24. /**
  25. * The type of data associated with the content code.
  26. */
  27. DMAP_TYPE type;
  28. /**
  29. * For listings, the type of their listing item children.
  30. *
  31. * Listing items (mlit) can be of any type, and as with other content codes
  32. * their type information is not encoded in the message. Parsers must
  33. * determine the type of the listing items based on their parent context.
  34. */
  35. DMAP_TYPE list_item_type;
  36. /**
  37. * A human-readable name for the content code.
  38. */
  39. const char *name;
  40. } dmap_field;
  41. static const dmap_field dmap_fields[] = {
  42. { "abal", DMAP_DICT, DMAP_STR, "daap.browsealbumlisting" },
  43. { "abar", DMAP_DICT, DMAP_STR, "daap.browseartistlisting" },
  44. { "abcp", DMAP_DICT, DMAP_STR, "daap.browsecomposerlisting" },
  45. { "abgn", DMAP_DICT, DMAP_STR, "daap.browsegenrelisting" },
  46. { "abpl", DMAP_UINT, 0, "daap.baseplaylist" },
  47. { "abro", DMAP_DICT, 0, "daap.databasebrowse" },
  48. { "adbs", DMAP_DICT, 0, "daap.databasesongs" },
  49. { "aeAD", DMAP_DICT, 0, "com.apple.itunes.adam-ids-array" },
  50. { "aeAI", DMAP_UINT, 0, "com.apple.itunes.itms-artistid" },
  51. { "aeCD", DMAP_DATA, 0, "com.apple.itunes.flat-chapter-data" },
  52. { "aeCF", DMAP_UINT, 0, "com.apple.itunes.cloud-flavor-id" },
  53. { "aeCI", DMAP_UINT, 0, "com.apple.itunes.itms-composerid" },
  54. { "aeCK", DMAP_UINT, 0, "com.apple.itunes.cloud-library-kind" },
  55. { "aeCM", DMAP_UINT, 0, "com.apple.itunes.cloud-match-type" },
  56. { "aeCR", DMAP_STR, 0, "com.apple.itunes.content-rating" } ,
  57. { "aeCS", DMAP_UINT, 0, "com.apple.itunes.artworkchecksum" },
  58. { "aeCU", DMAP_UINT, 0, "com.apple.itunes.cloud-user-id" },
  59. { "aeCd", DMAP_UINT, 0, "com.apple.itunes.cloud-id" },
  60. { "aeDE", DMAP_STR, 0, "com.apple.itunes.longest-content-description" },
  61. { "aeDL", DMAP_UINT, 0, "com.apple.itunes.drm-downloader-user-id" },
  62. { "aeDP", DMAP_UINT, 0, "com.apple.itunes.drm-platform-id" },
  63. { "aeDR", DMAP_UINT, 0, "com.apple.itunes.drm-user-id" },
  64. { "aeDV", DMAP_UINT, 0, "com.apple.itunes.drm-versions" },
  65. { "aeEN", DMAP_STR, 0, "com.apple.itunes.episode-num-str" },
  66. { "aeES", DMAP_UINT, 0, "com.apple.itunes.episode-sort" },
  67. { "aeFA", DMAP_UINT, 0, "com.apple.itunes.drm-family-id" },
  68. { "aeGD", DMAP_UINT, 0, "com.apple.itunes.gapless-enc-dr" } ,
  69. { "aeGE", DMAP_UINT, 0, "com.apple.itunes.gapless-enc-del" },
  70. { "aeGH", DMAP_UINT, 0, "com.apple.itunes.gapless-heur" },
  71. { "aeGI", DMAP_UINT, 0, "com.apple.itunes.itms-genreid" },
  72. { "aeGR", DMAP_UINT, 0, "com.apple.itunes.gapless-resy" },
  73. { "aeGU", DMAP_UINT, 0, "com.apple.itunes.gapless-dur" },
  74. { "aeGs", DMAP_UINT, 0, "com.apple.itunes.can-be-genius-seed" },
  75. { "aeHC", DMAP_UINT, 0, "com.apple.itunes.has-chapter-data" },
  76. { "aeHD", DMAP_UINT, 0, "com.apple.itunes.is-hd-video" },
  77. { "aeHV", DMAP_UINT, 0, "com.apple.itunes.has-video" },
  78. { "aeK1", DMAP_UINT, 0, "com.apple.itunes.drm-key1-id" },
  79. { "aeK2", DMAP_UINT, 0, "com.apple.itunes.drm-key2-id" },
  80. { "aeMC", DMAP_UINT, 0, "com.apple.itunes.playlist-contains-media-type-count" },
  81. { "aeMK", DMAP_UINT, 0, "com.apple.itunes.mediakind" },
  82. { "aeMX", DMAP_STR, 0, "com.apple.itunes.movie-info-xml" },
  83. { "aeMk", DMAP_UINT, 0, "com.apple.itunes.extended-media-kind" },
  84. { "aeND", DMAP_UINT, 0, "com.apple.itunes.non-drm-user-id" },
  85. { "aeNN", DMAP_STR, 0, "com.apple.itunes.network-name" },
  86. { "aeNV", DMAP_UINT, 0, "com.apple.itunes.norm-volume" },
  87. { "aePC", DMAP_UINT, 0, "com.apple.itunes.is-podcast" },
  88. { "aePI", DMAP_UINT, 0, "com.apple.itunes.itms-playlistid" },
  89. { "aePP", DMAP_UINT, 0, "com.apple.itunes.is-podcast-playlist" },
  90. { "aePS", DMAP_UINT, 0, "com.apple.itunes.special-playlist" },
  91. { "aeRD", DMAP_UINT, 0, "com.apple.itunes.rental-duration" },
  92. { "aeRP", DMAP_UINT, 0, "com.apple.itunes.rental-pb-start" },
  93. { "aeRS", DMAP_UINT, 0, "com.apple.itunes.rental-start" },
  94. { "aeRU", DMAP_UINT, 0, "com.apple.itunes.rental-pb-duration" },
  95. { "aeRf", DMAP_UINT, 0, "com.apple.itunes.is-featured" },
  96. { "aeSE", DMAP_UINT, 0, "com.apple.itunes.store-pers-id" },
  97. { "aeSF", DMAP_UINT, 0, "com.apple.itunes.itms-storefrontid" },
  98. { "aeSG", DMAP_UINT, 0, "com.apple.itunes.saved-genius" },
  99. { "aeSI", DMAP_UINT, 0, "com.apple.itunes.itms-songid" },
  100. { "aeSN", DMAP_STR, 0, "com.apple.itunes.series-name" },
  101. { "aeSP", DMAP_UINT, 0, "com.apple.itunes.smart-playlist" },
  102. { "aeSU", DMAP_UINT, 0, "com.apple.itunes.season-num" },
  103. { "aeSV", DMAP_VERS, 0, "com.apple.itunes.music-sharing-version" },
  104. { "aeXD", DMAP_STR, 0, "com.apple.itunes.xid" },
  105. { "aecp", DMAP_STR, 0, "com.apple.itunes.collection-description" },
  106. { "aels", DMAP_UINT, 0, "com.apple.itunes.liked-state" },
  107. { "aemi", DMAP_DICT, 0, "com.apple.itunes.media-kind-listing-item" },
  108. { "aeml", DMAP_DICT, 0, "com.apple.itunes.media-kind-listing" },
  109. { "agac", DMAP_UINT, 0, "daap.groupalbumcount" },
  110. { "agma", DMAP_UINT, 0, "daap.groupmatchedqueryalbumcount" },
  111. { "agmi", DMAP_UINT, 0, "daap.groupmatchedqueryitemcount" },
  112. { "agrp", DMAP_STR, 0, "daap.songgrouping" },
  113. { "ajAE", DMAP_UINT, 0, "com.apple.itunes.store.ams-episode-type" },
  114. { "ajAS", DMAP_UINT, 0, "com.apple.itunes.store.ams-episode-sort-order" },
  115. { "ajAT", DMAP_UINT, 0, "com.apple.itunes.store.ams-show-type" },
  116. { "ajAV", DMAP_UINT, 0, "com.apple.itunes.store.is-ams-video" },
  117. { "ajal", DMAP_UINT, 0, "com.apple.itunes.store.album-liked-state" },
  118. { "ajcA", DMAP_UINT, 0, "com.apple.itunes.store.show-composer-as-artist" },
  119. { "ajca", DMAP_UINT, 0, "com.apple.itunes.store.show-composer-as-artist" },
  120. { "ajuw", DMAP_UINT, 0, "com.apple.itunes.store.use-work-name-as-display-name" },
  121. { "amvc", DMAP_UINT, 0, "daap.songmovementcount" },
  122. { "amvm", DMAP_STR, 0, "daap.songmovementname" },
  123. { "amvn", DMAP_UINT, 0, "daap.songmovementnumber" },
  124. { "aply", DMAP_DICT, 0, "daap.databaseplaylists" },
  125. { "aprm", DMAP_UINT, 0, "daap.playlistrepeatmode" },
  126. { "apro", DMAP_VERS, 0, "daap.protocolversion" },
  127. { "apsm", DMAP_UINT, 0, "daap.playlistshufflemode" },
  128. { "apso", DMAP_DICT, 0, "daap.playlistsongs" },
  129. { "arif", DMAP_DICT, 0, "daap.resolveinfo" },
  130. { "arsv", DMAP_DICT, 0, "daap.resolve" },
  131. { "asaa", DMAP_STR, 0, "daap.songalbumartist" },
  132. { "asac", DMAP_UINT, 0, "daap.songartworkcount" },
  133. { "asai", DMAP_UINT, 0, "daap.songalbumid" },
  134. { "asal", DMAP_STR, 0, "daap.songalbum" },
  135. { "asar", DMAP_STR, 0, "daap.songartist" },
  136. { "asas", DMAP_UINT, 0, "daap.songalbumuserratingstatus" },
  137. { "asbk", DMAP_UINT, 0, "daap.bookmarkable" },
  138. { "asbo", DMAP_UINT, 0, "daap.songbookmark" },
  139. { "asbr", DMAP_UINT, 0, "daap.songbitrate" },
  140. { "asbt", DMAP_UINT, 0, "daap.songbeatsperminute" },
  141. { "ascd", DMAP_UINT, 0, "daap.songcodectype" },
  142. { "ascm", DMAP_STR, 0, "daap.songcomment" },
  143. { "ascn", DMAP_STR, 0, "daap.songcontentdescription" },
  144. { "asco", DMAP_UINT, 0, "daap.songcompilation" },
  145. { "ascp", DMAP_STR, 0, "daap.songcomposer" },
  146. { "ascr", DMAP_UINT, 0, "daap.songcontentrating" },
  147. { "ascs", DMAP_UINT, 0, "daap.songcodecsubtype" },
  148. { "asct", DMAP_STR, 0, "daap.songcategory" },
  149. { "asda", DMAP_DATE, 0, "daap.songdateadded" },
  150. { "asdb", DMAP_UINT, 0, "daap.songdisabled" },
  151. { "asdc", DMAP_UINT, 0, "daap.songdisccount" },
  152. { "asdk", DMAP_UINT, 0, "daap.songdatakind" },
  153. { "asdm", DMAP_DATE, 0, "daap.songdatemodified" },
  154. { "asdn", DMAP_UINT, 0, "daap.songdiscnumber" },
  155. { "asdp", DMAP_DATE, 0, "daap.songdatepurchased" },
  156. { "asdr", DMAP_DATE, 0, "daap.songdatereleased" },
  157. { "asdt", DMAP_STR, 0, "daap.songdescription" },
  158. { "ased", DMAP_UINT, 0, "daap.songextradata" },
  159. { "aseq", DMAP_STR, 0, "daap.songeqpreset" },
  160. { "ases", DMAP_UINT, 0, "daap.songexcludefromshuffle" },
  161. { "asfm", DMAP_STR, 0, "daap.songformat" },
  162. { "asgn", DMAP_STR, 0, "daap.songgenre" },
  163. { "asgp", DMAP_UINT, 0, "daap.songgapless" },
  164. { "asgr", DMAP_UINT, 0, "daap.supportsgroups" },
  165. { "ashp", DMAP_UINT, 0, "daap.songhasbeenplayed" },
  166. { "askd", DMAP_DATE, 0, "daap.songlastskipdate" },
  167. { "askp", DMAP_UINT, 0, "daap.songuserskipcount" },
  168. { "asky", DMAP_STR, 0, "daap.songkeywords" },
  169. { "aslc", DMAP_STR, 0, "daap.songlongcontentdescription" },
  170. { "aslr", DMAP_UINT, 0, "daap.songalbumuserrating" },
  171. { "asls", DMAP_UINT, 0, "daap.songlongsize" },
  172. { "aspc", DMAP_UINT, 0, "daap.songuserplaycount" },
  173. { "aspl", DMAP_DATE, 0, "daap.songdateplayed" },
  174. { "aspu", DMAP_STR, 0, "daap.songpodcasturl" },
  175. { "asri", DMAP_UINT, 0, "daap.songartistid" },
  176. { "asrs", DMAP_UINT, 0, "daap.songuserratingstatus" },
  177. { "asrv", DMAP_INT, 0, "daap.songrelativevolume" },
  178. { "assa", DMAP_STR, 0, "daap.sortartist" },
  179. { "assc", DMAP_STR, 0, "daap.sortcomposer" },
  180. { "assl", DMAP_STR, 0, "daap.sortalbumartist" },
  181. { "assn", DMAP_STR, 0, "daap.sortname" },
  182. { "assp", DMAP_UINT, 0, "daap.songstoptime" },
  183. { "assr", DMAP_UINT, 0, "daap.songsamplerate" },
  184. { "asss", DMAP_STR, 0, "daap.sortseriesname" },
  185. { "asst", DMAP_UINT, 0, "daap.songstarttime" },
  186. { "assu", DMAP_STR, 0, "daap.sortalbum" },
  187. { "assz", DMAP_UINT, 0, "daap.songsize" },
  188. { "astc", DMAP_UINT, 0, "daap.songtrackcount" },
  189. { "astm", DMAP_UINT, 0, "daap.songtime" },
  190. { "astn", DMAP_UINT, 0, "daap.songtracknumber" },
  191. { "asul", DMAP_STR, 0, "daap.songdataurl" },
  192. { "asur", DMAP_UINT, 0, "daap.songuserrating" },
  193. { "asvc", DMAP_UINT, 0, "daap.songprimaryvideocodec" },
  194. { "asyr", DMAP_UINT, 0, "daap.songyear" },
  195. { "ated", DMAP_UINT, 0, "daap.supportsextradata" },
  196. { "avdb", DMAP_DICT, 0, "daap.serverdatabases" },
  197. { "awrk", DMAP_STR, 0, "daap.songwork" },
  198. { "caar", DMAP_UINT, 0, "dacp.availablerepeatstates" },
  199. { "caas", DMAP_UINT, 0, "dacp.availableshufflestates" },
  200. { "caci", DMAP_DICT, 0, "caci" },
  201. { "cafe", DMAP_UINT, 0, "dacp.fullscreenenabled" },
  202. { "cafs", DMAP_UINT, 0, "dacp.fullscreen" },
  203. { "caia", DMAP_UINT, 0, "dacp.isactive" },
  204. { "cana", DMAP_STR, 0, "dacp.nowplayingartist" },
  205. { "cang", DMAP_STR, 0, "dacp.nowplayinggenre" },
  206. { "canl", DMAP_STR, 0, "dacp.nowplayingalbum" },
  207. { "cann", DMAP_STR, 0, "dacp.nowplayingname" },
  208. { "canp", DMAP_UINT, 0, "dacp.nowplayingids" },
  209. { "cant", DMAP_UINT, 0, "dacp.nowplayingtime" },
  210. { "capr", DMAP_VERS, 0, "dacp.protocolversion" },
  211. { "caps", DMAP_UINT, 0, "dacp.playerstate" },
  212. { "carp", DMAP_UINT, 0, "dacp.repeatstate" },
  213. { "cash", DMAP_UINT, 0, "dacp.shufflestate" },
  214. { "casp", DMAP_DICT, 0, "dacp.speakers" },
  215. { "cast", DMAP_UINT, 0, "dacp.songtime" },
  216. { "cavc", DMAP_UINT, 0, "dacp.volumecontrollable" },
  217. { "cave", DMAP_UINT, 0, "dacp.visualizerenabled" },
  218. { "cavs", DMAP_UINT, 0, "dacp.visualizer" },
  219. { "ceJC", DMAP_UINT, 0, "com.apple.itunes.jukebox-client-vote" },
  220. { "ceJI", DMAP_UINT, 0, "com.apple.itunes.jukebox-current" },
  221. { "ceJS", DMAP_UINT, 0, "com.apple.itunes.jukebox-score" },
  222. { "ceJV", DMAP_UINT, 0, "com.apple.itunes.jukebox-vote" },
  223. { "ceQR", DMAP_DICT, 0, "com.apple.itunes.playqueue-contents-response" },
  224. { "ceQa", DMAP_STR, 0, "com.apple.itunes.playqueue-album" },
  225. { "ceQg", DMAP_STR, 0, "com.apple.itunes.playqueue-genre" },
  226. { "ceQn", DMAP_STR, 0, "com.apple.itunes.playqueue-name" },
  227. { "ceQr", DMAP_STR, 0, "com.apple.itunes.playqueue-artist" },
  228. { "cmgt", DMAP_DICT, 0, "dmcp.getpropertyresponse" },
  229. { "cmmk", DMAP_UINT, 0, "dmcp.mediakind" },
  230. { "cmpr", DMAP_VERS, 0, "dmcp.protocolversion" },
  231. { "cmsr", DMAP_UINT, 0, "dmcp.serverrevision" },
  232. { "cmst", DMAP_DICT, 0, "dmcp.playstatus" },
  233. { "cmvo", DMAP_UINT, 0, "dmcp.volume" },
  234. { "f\215ch", DMAP_UINT, 0, "dmap.haschildcontainers" },
  235. { "ipsa", DMAP_DICT, 0, "dpap.iphotoslideshowadvancedoptions" },
  236. { "ipsl", DMAP_DICT, 0, "dpap.iphotoslideshowoptions" },
  237. { "mbcl", DMAP_DICT, 0, "dmap.bag" },
  238. { "mccr", DMAP_DICT, 0, "dmap.contentcodesresponse" },
  239. { "mcna", DMAP_STR, 0, "dmap.contentcodesname" },
  240. { "mcnm", DMAP_UINT, 0, "dmap.contentcodesnumber" },
  241. { "mcon", DMAP_DICT, 0, "dmap.container" },
  242. { "mctc", DMAP_UINT, 0, "dmap.containercount" },
  243. { "mcti", DMAP_UINT, 0, "dmap.containeritemid" },
  244. { "mcty", DMAP_UINT, 0, "dmap.contentcodestype" },
  245. { "mdbk", DMAP_UINT, 0, "dmap.databasekind" },
  246. { "mdcl", DMAP_DICT, 0, "dmap.dictionary" },
  247. { "mdst", DMAP_UINT, 0, "dmap.downloadstatus" },
  248. { "meds", DMAP_UINT, 0, "dmap.editcommandssupported" },
  249. { "meia", DMAP_UINT, 0, "dmap.itemdateadded" },
  250. { "meip", DMAP_UINT, 0, "dmap.itemdateplayed" },
  251. { "mext", DMAP_UINT, 0, "dmap.objectextradata" },
  252. { "miid", DMAP_UINT, 0, "dmap.itemid" },
  253. { "mikd", DMAP_UINT, 0, "dmap.itemkind" },
  254. { "mimc", DMAP_UINT, 0, "dmap.itemcount" },
  255. { "minm", DMAP_STR, 0, "dmap.itemname" },
  256. { "mlcl", DMAP_DICT, DMAP_DICT, "dmap.listing" },
  257. { "mlid", DMAP_UINT, 0, "dmap.sessionid" },
  258. { "mlit", DMAP_ITEM, 0, "dmap.listingitem" },
  259. { "mlog", DMAP_DICT, 0, "dmap.loginresponse" },
  260. { "mpco", DMAP_UINT, 0, "dmap.parentcontainerid" },
  261. { "mper", DMAP_UINT, 0, "dmap.persistentid" },
  262. { "mpro", DMAP_VERS, 0, "dmap.protocolversion" },
  263. { "mrco", DMAP_UINT, 0, "dmap.returnedcount" },
  264. { "mrpr", DMAP_UINT, 0, "dmap.remotepersistentid" },
  265. { "msal", DMAP_UINT, 0, "dmap.supportsautologout" },
  266. { "msas", DMAP_UINT, 0, "dmap.authenticationschemes" },
  267. { "msau", DMAP_UINT, 0, "dmap.authenticationmethod" },
  268. { "msbr", DMAP_UINT, 0, "dmap.supportsbrowse" },
  269. { "msdc", DMAP_UINT, 0, "dmap.databasescount" },
  270. { "msex", DMAP_UINT, 0, "dmap.supportsextensions" },
  271. { "msix", DMAP_UINT, 0, "dmap.supportsindex" },
  272. { "mslr", DMAP_UINT, 0, "dmap.loginrequired" },
  273. { "msma", DMAP_UINT, 0, "dmap.machineaddress" },
  274. { "msml", DMAP_DICT, 0, "msml" },
  275. { "mspi", DMAP_UINT, 0, "dmap.supportspersistentids" },
  276. { "msqy", DMAP_UINT, 0, "dmap.supportsquery" },
  277. { "msrs", DMAP_UINT, 0, "dmap.supportsresolve" },
  278. { "msrv", DMAP_DICT, 0, "dmap.serverinforesponse" },
  279. { "mstc", DMAP_DATE, 0, "dmap.utctime" },
  280. { "mstm", DMAP_UINT, 0, "dmap.timeoutinterval" },
  281. { "msto", DMAP_INT, 0, "dmap.utcoffset" },
  282. { "msts", DMAP_STR, 0, "dmap.statusstring" },
  283. { "mstt", DMAP_UINT, 0, "dmap.status" },
  284. { "msup", DMAP_UINT, 0, "dmap.supportsupdate" },
  285. { "mtco", DMAP_UINT, 0, "dmap.specifiedtotalcount" },
  286. { "mudl", DMAP_DICT, 0, "dmap.deletedidlisting" },
  287. { "mupd", DMAP_DICT, 0, "dmap.updateresponse" },
  288. { "musr", DMAP_UINT, 0, "dmap.serverrevision" },
  289. { "muty", DMAP_UINT, 0, "dmap.updatetype" },
  290. { "pasp", DMAP_STR, 0, "dpap.aspectratio" },
  291. { "pcmt", DMAP_STR, 0, "dpap.imagecomments" },
  292. { "peak", DMAP_UINT, 0, "com.apple.itunes.photos.album-kind" },
  293. { "peed", DMAP_DATE, 0, "com.apple.itunes.photos.exposure-date" },
  294. { "pefc", DMAP_DICT, 0, "com.apple.itunes.photos.faces" },
  295. { "peki", DMAP_UINT, 0, "com.apple.itunes.photos.key-image-id" },
  296. { "pekm", DMAP_DICT, 0, "com.apple.itunes.photos.key-image" },
  297. { "pemd", DMAP_DATE, 0, "com.apple.itunes.photos.modification-date" },
  298. { "pfai", DMAP_DICT, 0, "dpap.failureids" },
  299. { "pfdt", DMAP_DICT, 0, "dpap.filedata" },
  300. { "pfmt", DMAP_STR, 0, "dpap.imageformat" },
  301. { "phgt", DMAP_UINT, 0, "dpap.imagepixelheight" },
  302. { "picd", DMAP_DATE, 0, "dpap.creationdate" },
  303. { "pifs", DMAP_UINT, 0, "dpap.imagefilesize" },
  304. { "pimf", DMAP_STR, 0, "dpap.imagefilename" },
  305. { "plsz", DMAP_UINT, 0, "dpap.imagelargefilesize" },
  306. { "ppro", DMAP_VERS, 0, "dpap.protocolversion" },
  307. { "prat", DMAP_UINT, 0, "dpap.imagerating" },
  308. { "pret", DMAP_DICT, 0, "dpap.retryids" },
  309. { "pwth", DMAP_UINT, 0, "dpap.imagepixelwidth" }
  310. };
  311. static const size_t dmap_field_count = sizeof(dmap_fields) / sizeof(dmap_field);
  312. typedef int (*sort_func) (const void *, const void *);
  313. int dmap_version(void) {
  314. return DMAP_VERSION;
  315. }
  316. const char *dmap_version_string(void) {
  317. return DMAP_STRINGIFY(DMAP_VERSION_MAJOR) "."
  318. DMAP_STRINGIFY(DMAP_VERSION_MINOR) "."
  319. DMAP_STRINGIFY(DMAP_VERSION_PATCH);
  320. }
  321. static int dmap_field_sort(const dmap_field *a, const dmap_field *b) {
  322. return memcmp(a->code, b->code, 4);
  323. }
  324. static const dmap_field *dmap_field_from_code(const char *code) {
  325. dmap_field key;
  326. key.code = code;
  327. return bsearch(&key, dmap_fields, dmap_field_count, sizeof(dmap_field), (sort_func)dmap_field_sort);
  328. }
  329. const char *dmap_name_from_code(const char *code) {
  330. const dmap_field *field;
  331. if (!code)
  332. return NULL;
  333. field = dmap_field_from_code(code);
  334. return field ? field->name : NULL;
  335. }
  336. static uint16_t dmap_read_u16(const char *buf) {
  337. return (uint16_t)(((buf[0] & 0xff) << 8) | (buf[1] & 0xff));
  338. }
  339. static int16_t dmap_read_i16(const char *buf) {
  340. return (int16_t)dmap_read_u16(buf);
  341. }
  342. static uint32_t dmap_read_u32(const char *buf) {
  343. return ((uint32_t)(buf[0] & 0xff) << 24) |
  344. ((uint32_t)(buf[1] & 0xff) << 16) |
  345. ((uint32_t)(buf[2] & 0xff) << 8) |
  346. ((uint32_t)(buf[3] & 0xff));
  347. }
  348. static int32_t dmap_read_i32(const char *buf) {
  349. return (int32_t)dmap_read_u32(buf);
  350. }
  351. static uint64_t dmap_read_u64(const char *buf) {
  352. return ((uint64_t)(buf[0] & 0xff) << 56) |
  353. ((uint64_t)(buf[1] & 0xff) << 48) |
  354. ((uint64_t)(buf[2] & 0xff) << 40) |
  355. ((uint64_t)(buf[3] & 0xff) << 32) |
  356. ((uint64_t)(buf[4] & 0xff) << 24) |
  357. ((uint64_t)(buf[5] & 0xff) << 16) |
  358. ((uint64_t)(buf[6] & 0xff) << 8) |
  359. ((uint64_t)(buf[7] & 0xff));
  360. }
  361. static int64_t dmap_read_i64(const char *buf) {
  362. return (int64_t)dmap_read_u64(buf);
  363. }
  364. static int dmap_parse_internal(const dmap_settings *settings, const char *buf, size_t len, const dmap_field *parent) {
  365. const dmap_field *field;
  366. DMAP_TYPE field_type;
  367. size_t field_len;
  368. const char *field_name;
  369. const char *p = buf;
  370. const char *end = buf + len;
  371. char code[5] = {0};
  372. if (!settings || !buf)
  373. return -1;
  374. while (end - p >= 8) {
  375. memcpy(code, p, 4);
  376. field = dmap_field_from_code(code);
  377. p += 4;
  378. field_len = dmap_read_u32(p);
  379. p += 4;
  380. if (p + field_len > end)
  381. return -1;
  382. if (field) {
  383. field_type = field->type;
  384. field_name = field->name;
  385. if (field_type == DMAP_ITEM) {
  386. if (parent != NULL && parent->list_item_type) {
  387. field_type = parent->list_item_type;
  388. } else {
  389. field_type = DMAP_DICT;
  390. }
  391. }
  392. } else {
  393. /* Make a best guess of the type */
  394. field_type = DMAP_UNKNOWN;
  395. field_name = code;
  396. if (field_len >= 8) {
  397. /* Look for a four char code followed by a length within the current field */
  398. if (isalpha(p[0] & 0xff) &&
  399. isalpha(p[1] & 0xff) &&
  400. isalpha(p[2] & 0xff) &&
  401. isalpha(p[3] & 0xff)) {
  402. if (dmap_read_u32(p + 4) < field_len)
  403. field_type = DMAP_DICT;
  404. }
  405. }
  406. if (field_type == DMAP_UNKNOWN) {
  407. size_t i;
  408. int is_string = 1;
  409. for (i=0; i < field_len; i++) {
  410. if (!isprint(p[i] & 0xff)) {
  411. is_string = 0;
  412. break;
  413. }
  414. }
  415. field_type = is_string ? DMAP_STR : DMAP_UINT;
  416. }
  417. }
  418. switch (field_type) {
  419. case DMAP_UINT:
  420. /* Determine the integer's type based on its size */
  421. switch (field_len) {
  422. case 1:
  423. if (settings->on_uint32)
  424. settings->on_uint32(settings->ctx, code, field_name, (unsigned char)*p);
  425. break;
  426. case 2:
  427. if (settings->on_uint32)
  428. settings->on_uint32(settings->ctx, code, field_name, dmap_read_u16(p));
  429. break;
  430. case 4:
  431. if (settings->on_uint32)
  432. settings->on_uint32(settings->ctx, code, field_name, dmap_read_u32(p));
  433. break;
  434. case 8:
  435. if (settings->on_uint64)
  436. settings->on_uint64(settings->ctx, code, field_name, dmap_read_u64(p));
  437. break;
  438. default:
  439. if (settings->on_data)
  440. settings->on_data(settings->ctx, code, field_name, p, field_len);
  441. break;
  442. }
  443. break;
  444. case DMAP_INT:
  445. switch (field_len) {
  446. case 1:
  447. if (settings->on_int32)
  448. settings->on_int32(settings->ctx, code, field_name, *p);
  449. break;
  450. case 2:
  451. if (settings->on_int32)
  452. settings->on_int32(settings->ctx, code, field_name, dmap_read_i16(p));
  453. break;
  454. case 4:
  455. if (settings->on_int32)
  456. settings->on_int32(settings->ctx, code, field_name, dmap_read_i32(p));
  457. break;
  458. case 8:
  459. if (settings->on_int64)
  460. settings->on_int64(settings->ctx, code, field_name, dmap_read_i64(p));
  461. break;
  462. default:
  463. if (settings->on_data)
  464. settings->on_data(settings->ctx, code, field_name, p, field_len);
  465. break;
  466. }
  467. break;
  468. case DMAP_STR:
  469. if (settings->on_string)
  470. settings->on_string(settings->ctx, code, field_name, p, field_len);
  471. break;
  472. case DMAP_DATA:
  473. if (settings->on_data)
  474. settings->on_data(settings->ctx, code, field_name, p, field_len);
  475. break;
  476. case DMAP_DATE:
  477. /* Seconds since epoch */
  478. if (settings->on_date)
  479. settings->on_date(settings->ctx, code, field_name, dmap_read_u32(p));
  480. break;
  481. case DMAP_VERS:
  482. if (settings->on_string && field_len >= 4) {
  483. char version[20];
  484. sprintf(version, "%u.%u", dmap_read_u16(p), dmap_read_u16(p+2));
  485. settings->on_string(settings->ctx, code, field_name, version, strlen(version));
  486. }
  487. break;
  488. case DMAP_DICT:
  489. if (settings->on_dict_start)
  490. settings->on_dict_start(settings->ctx, code, field_name);
  491. if (dmap_parse_internal(settings, p, field_len, field) != 0)
  492. return -1;
  493. if (settings->on_dict_end)
  494. settings->on_dict_end(settings->ctx, code, field_name);
  495. break;
  496. case DMAP_ITEM:
  497. /* Unreachable: listing item types are always mapped to another type */
  498. abort();
  499. case DMAP_UNKNOWN:
  500. break;
  501. }
  502. p += field_len;
  503. }
  504. if (p != end)
  505. return -1;
  506. return 0;
  507. }
  508. int dmap_parse(const dmap_settings *settings, const char *buf, size_t len) {
  509. return dmap_parse_internal(settings, buf, len, NULL);
  510. }